main.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <unistd.h>
  6. #include "Crown.h"
  7. #include "LuaCompiler.h"
  8. using namespace crown;
  9. const char* root_path = NULL;
  10. const char* dest_path = NULL;
  11. const char* resource_in = NULL;
  12. uint32_t hash_seed = 0;
  13. void parse_command_line(int argc, char** argv);
  14. void print_help_message(const char* program_name);
  15. void compile_script(char* tmp_out);
  16. /// Lua scripts compiler
  17. int main(int argc, char** argv)
  18. {
  19. parse_command_line(argc, argv);
  20. if (root_path == NULL)
  21. {
  22. printf("%s: ERROR: you have to specify the root path with `--root-path`\n", argv[0]);
  23. exit(-1);
  24. }
  25. if (dest_path == NULL)
  26. {
  27. printf("%s: ERROR: you have to specify the destination path with `--dest-path`\n", argv[0]);
  28. exit(-1);
  29. }
  30. if (resource_in == NULL)
  31. {
  32. printf("%s: ERROR: you have to specify the resource name with `--resource-in`\n", argv[0]);
  33. exit(-1);
  34. }
  35. LuaCompiler compiler(root_path, dest_path, resource_in, hash_seed);
  36. compiler.compile();
  37. // ArchiveEntry archive_entry;
  38. // archive_entry.name = resource_basename_hash;
  39. // archive_entry.type = resource_extension_hash;
  40. // archive_entry.offset = sizeof (ArchiveEntry);
  41. // archive_entry.size = src_file_size + sizeof(uint32_t);
  42. // void* buffer = new uint8_t[src_file_size];
  43. // src_file->read(buffer, src_file_size);
  44. // fs_root.close(src_file);
  45. // FileStream* dest_file = (FileStream*)fs_root.open(resource_out, SOM_WRITE);
  46. // dest_file->write(&archive_entry, sizeof(ArchiveEntry));
  47. // dest_file->write(&src_file_size, sizeof(uint32_t));
  48. // dest_file->write(buffer, src_file_size);
  49. // fs_root.delete_file(tmp_file);
  50. // fs_root.close(dest_file);
  51. // printf("Resource compilation completed: %s\n", resource_out);
  52. return 0;
  53. }
  54. //-----------------------------------------------------------------------------
  55. void parse_command_line(int argc, char** argv)
  56. {
  57. // Parse arguments
  58. ArgsOption options[] =
  59. {
  60. "help", AOA_NO_ARGUMENT, NULL, 'h',
  61. "root-path", AOA_REQUIRED_ARGUMENT, NULL, 'r',
  62. "dest-path", AOA_REQUIRED_ARGUMENT, NULL, 'd',
  63. "resource-in", AOA_REQUIRED_ARGUMENT, NULL, 'i',
  64. "seed", AOA_REQUIRED_ARGUMENT, NULL, 's',
  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. // Dest path
  95. case 'd':
  96. {
  97. if (args.option_argument() == NULL)
  98. {
  99. printf("%s: ERROR: missing path after `--dest-path`\n", argv[0]);
  100. exit(-1);
  101. }
  102. dest_path = args.option_argument();
  103. break;
  104. }
  105. // Resource in
  106. case 'i':
  107. {
  108. if (args.option_argument() == NULL)
  109. {
  110. printf("%s: ERROR: missing path after `--resource-in`\n", argv[0]);
  111. exit(-1);
  112. }
  113. resource_in = args.option_argument();
  114. break;
  115. }
  116. case 's':
  117. {
  118. if (args.option_argument() == NULL)
  119. {
  120. printf("%s: ERROR: missing seed value after `--seed`\n", argv[0]);
  121. exit(-1);
  122. }
  123. hash_seed = atoi(args.option_argument());
  124. break;
  125. }
  126. default:
  127. {
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. void print_help_message(const char* program_name)
  134. {
  135. printf("Usage: %s [options]\n", program_name);
  136. printf("Options:\n\n");
  137. printf(" --help Show this help.\n");
  138. printf(" --root-path <path> The _absolute_ <path> whether to look for the input resource.\n");
  139. printf(" --resource-in <path> The _relative_ <path> of the input resource.\n");
  140. printf(" --resource-out <width> The _relative_ <path> of the output resource.\n");
  141. }
  142. void compile_script(char* tmp_out)
  143. {
  144. char in[256];
  145. strcpy(in, root_path);
  146. strcat(in, resource_in);
  147. char rel_out[256];
  148. strncpy(rel_out, resource_in, strlen(resource_in) - 3);
  149. strcat(rel_out, "tmp");
  150. char out[256];
  151. strcpy(out, root_path);
  152. strcat(out, rel_out);
  153. strcpy(tmp_out, rel_out);
  154. // Fork for execl
  155. pid_t child = 0;
  156. child = fork();
  157. if (child < 0)
  158. {
  159. printf("Failed fork during compile_script() call.");
  160. return;
  161. }
  162. if (child == 0)
  163. {
  164. wait(NULL);
  165. }
  166. else
  167. {
  168. execl("/usr/local/bin/luajit", "luajit", "-bl", in, out, NULL);
  169. }
  170. }