main.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. if (compiler.compile() == false)
  37. {
  38. printf("%s: ERROR: compilation failed for resource %s\n", argv[0], compiler.resource_path());
  39. exit(-1);
  40. }
  41. compiler.write();
  42. return 0;
  43. }
  44. //-----------------------------------------------------------------------------
  45. void parse_command_line(int argc, char** argv)
  46. {
  47. // Parse arguments
  48. ArgsOption options[] =
  49. {
  50. "help", AOA_NO_ARGUMENT, NULL, 'h',
  51. "root-path", AOA_REQUIRED_ARGUMENT, NULL, 'r',
  52. "dest-path", AOA_REQUIRED_ARGUMENT, NULL, 'd',
  53. "resource-in", AOA_REQUIRED_ARGUMENT, NULL, 'i',
  54. "seed", AOA_REQUIRED_ARGUMENT, NULL, 's',
  55. NULL, 0, NULL, 0
  56. };
  57. Args args(argc, argv, "", options);
  58. while (1)
  59. {
  60. int32_t ret = args.next_option();
  61. switch (ret)
  62. {
  63. case -1:
  64. {
  65. return;
  66. }
  67. // Help message
  68. case 'h':
  69. {
  70. print_help_message(argv[0]);
  71. exit(0);
  72. }
  73. // Root path
  74. case 'r':
  75. {
  76. if (args.option_argument() == NULL)
  77. {
  78. printf("%s: ERROR: missing path after `--root-path`\n", argv[0]);
  79. exit(-1);
  80. }
  81. root_path = args.option_argument();
  82. break;
  83. }
  84. // Dest path
  85. case 'd':
  86. {
  87. if (args.option_argument() == NULL)
  88. {
  89. printf("%s: ERROR: missing path after `--dest-path`\n", argv[0]);
  90. exit(-1);
  91. }
  92. dest_path = args.option_argument();
  93. break;
  94. }
  95. // Resource in
  96. case 'i':
  97. {
  98. if (args.option_argument() == NULL)
  99. {
  100. printf("%s: ERROR: missing path after `--resource-in`\n", argv[0]);
  101. exit(-1);
  102. }
  103. resource_in = args.option_argument();
  104. break;
  105. }
  106. case 's':
  107. {
  108. if (args.option_argument() == NULL)
  109. {
  110. printf("%s: ERROR: missing seed value after `--seed`\n", argv[0]);
  111. exit(-1);
  112. }
  113. hash_seed = atoi(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. }