2
0

main.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <unistd.h>
  6. #include "lua.hpp"
  7. #include "Crown.h"
  8. using namespace crown;
  9. const char* root_path = NULL;
  10. const char* resource_in = NULL;
  11. const char* resource_out = NULL;
  12. void parse_command_line(int argc, char** argv);
  13. void print_help_message(const char* program_name);
  14. void compile_script(char* tmp_out);
  15. /// Lua scripts compiler
  16. int main(int argc, char** argv)
  17. {
  18. // parse_command_line(argc, argv);
  19. // // FIXME: validate input
  20. // Filesystem fs_root(root_path);
  21. // if (!fs_root.exists(resource_in))
  22. // {
  23. // printf("%s: ERROR: %s does not exist. Aborting.\n", argv[0], resource_in);
  24. // return -1;
  25. // }
  26. // char resource_basename[256];
  27. // char resource_extension[256];
  28. // path::filename_without_extension(resource_in, resource_basename, 256);
  29. // path::extension(resource_in, resource_extension, 256);
  30. // uint32_t resource_basename_hash = hash::fnv1a_32(resource_basename, string::strlen(resource_basename));
  31. // uint32_t resource_extension_hash = hash::fnv1a_32(resource_extension, string::strlen(resource_extension));
  32. // char tmp_file[256];
  33. // compile_script(tmp_file);
  34. // FileStream* src_file = (FileStream*)fs_root.open(tmp_file, SOM_READ);
  35. // size_t src_file_size = src_file->size();
  36. // ArchiveEntry archive_entry;
  37. // archive_entry.name = resource_basename_hash;
  38. // archive_entry.type = resource_extension_hash;
  39. // archive_entry.offset = sizeof (ArchiveEntry);
  40. // archive_entry.size = src_file_size + sizeof(uint32_t);
  41. // void* buffer = new uint8_t[src_file_size];
  42. // src_file->read(buffer, src_file_size);
  43. // fs_root.close(src_file);
  44. // FileStream* dest_file = (FileStream*)fs_root.open(resource_out, SOM_WRITE);
  45. // dest_file->write(&archive_entry, sizeof(ArchiveEntry));
  46. // dest_file->write(&src_file_size, sizeof(uint32_t));
  47. // dest_file->write(buffer, src_file_size);
  48. // fs_root.delete_file(tmp_file);
  49. // fs_root.close(dest_file);
  50. // printf("Resource compilation completed: %s\n", resource_out);
  51. // lua_State *L;
  52. // = lua_open();
  53. // lua_close(L);
  54. return 0;
  55. }
  56. void parse_command_line(int argc, char** argv)
  57. {
  58. // Parse arguments
  59. ArgsOption options[] =
  60. {
  61. "help", AOA_NO_ARGUMENT, NULL, 'h',
  62. "root-path", AOA_REQUIRED_ARGUMENT, NULL, 'r',
  63. "resource-in", AOA_REQUIRED_ARGUMENT, NULL, 'i',
  64. "resource-out", AOA_REQUIRED_ARGUMENT, NULL, 'o',
  65. NULL, 0, NULL, 0
  66. };
  67. Args args(argc, argv, "", options);
  68. while (1)
  69. {
  70. int32_t ret = args.next_option();
  71. switch (ret)
  72. {
  73. case -1:
  74. {
  75. return;
  76. }
  77. // Help message
  78. case 'h':
  79. {
  80. print_help_message(argv[0]);
  81. exit(0);
  82. }
  83. // Root path
  84. case 'r':
  85. {
  86. if (args.option_argument() == NULL)
  87. {
  88. printf("%s: ERROR: missing path after `--root-path`\n", argv[0]);
  89. exit(-1);
  90. }
  91. root_path = args.option_argument();
  92. break;
  93. }
  94. // Resource in
  95. case 'i':
  96. {
  97. if (args.option_argument() == NULL)
  98. {
  99. printf("%s: ERROR: missing path after `--resource-in`\n", argv[0]);
  100. exit(-1);
  101. }
  102. resource_in = args.option_argument();
  103. break;
  104. }
  105. // Resource out
  106. case 'o':
  107. {
  108. if (args.option_argument() == NULL)
  109. {
  110. printf("%s: ERROR: missing path after `--resource-out`\n", argv[0]);
  111. exit(-1);
  112. }
  113. resource_out = args.option_argument();
  114. break;
  115. }
  116. default:
  117. {
  118. break;
  119. }
  120. }
  121. }
  122. }
  123. void print_help_message(const char* program_name)
  124. {
  125. printf("Usage: %s [options]\n", program_name);
  126. printf("Options:\n\n");
  127. printf(" --help Show this help.\n");
  128. printf(" --root-path <path> The _absolute_ <path> whether to look for the input resource.\n");
  129. printf(" --resource-in <path> The _relative_ <path> of the input resource.\n");
  130. printf(" --resource-out <width> The _relative_ <path> of the output resource.\n");
  131. }
  132. void compile_script(char* tmp_out)
  133. {
  134. char in[256];
  135. strcpy(in, root_path);
  136. strcat(in, resource_in);
  137. char rel_out[256];
  138. strncpy(rel_out, resource_in, strlen(resource_in) - 3);
  139. strcat(rel_out, "tmp");
  140. char out[256];
  141. strcpy(out, root_path);
  142. strcat(out, rel_out);
  143. strcpy(tmp_out, rel_out);
  144. // Fork for execl
  145. pid_t child = 0;
  146. child = fork();
  147. if (child < 0)
  148. {
  149. printf("Failed fork during compile_script() call.");
  150. return;
  151. }
  152. if (child == 0)
  153. {
  154. wait(NULL);
  155. }
  156. else
  157. {
  158. execl("/usr/local/bin/luajit", "luajit", "-bl", in, out, NULL);
  159. }
  160. }