resource-compiler.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "Crown.h"
  2. #include "tga/TGACompiler.h"
  3. #include "txt/TXTCompiler.h"
  4. #include "lua/LuaCompiler.h"
  5. #include "vs/VSCompiler.h"
  6. #include "ps/PSCompiler.h"
  7. using namespace crown;
  8. // Max number of requests per run
  9. const uint32_t MAX_COMPILE_REQUESTS = 512;
  10. const char* root_path = NULL;
  11. const char* dest_path = NULL;
  12. uint32_t hash_seed = 0;
  13. // Help functions
  14. int32_t parse_command_line(int argc, char* argv[]);
  15. void print_help_message(const char* program_name);
  16. void check_arguments(const char* root_path, const char* dest_path);
  17. void compile_by_type(const char* resource);
  18. //-----------------------------------------------------------------------------
  19. int main(int argc, char** argv)
  20. {
  21. int32_t first_resource = parse_command_line(argc, argv);
  22. // Check if all the mandatory options are set
  23. check_arguments(root_path, dest_path);
  24. // If there are no resources
  25. if (first_resource >= argc)
  26. {
  27. Log::e("you have to specify at least one resource.");
  28. exit(EXIT_FAILURE);
  29. }
  30. TGACompiler tga(root_path, dest_path);
  31. TXTCompiler txt(root_path, dest_path);
  32. LuaCompiler lua(root_path, dest_path);
  33. VSCompiler vs(root_path, dest_path);
  34. PSCompiler ps(root_path, dest_path);
  35. char resource_name[MAX_RESOURCE_NAME_LENGTH];
  36. char resource_type[MAX_RESOURCE_TYPE_LENGTH];
  37. // Dispatch requests to the appropriate compiler
  38. for (int32_t res = first_resource; res < argc; res++)
  39. {
  40. char* resource = argv[res];
  41. path::filename_without_extension(resource, resource_name, MAX_RESOURCE_NAME_LENGTH);
  42. path::extension(resource, resource_type, MAX_RESOURCE_TYPE_LENGTH);
  43. uint32_t resource_name_hash = hash::murmur2_32(resource_name, string::strlen(resource_name), hash_seed);
  44. uint32_t resource_type_hash = hash::murmur2_32(resource_type, string::strlen(resource_type), 0);
  45. switch (resource_type_hash)
  46. {
  47. case TEXTURE_TYPE:
  48. {
  49. tga.compile(resource, resource_name_hash, resource_type_hash);
  50. break;
  51. }
  52. case TEXT_TYPE:
  53. {
  54. txt.compile(resource, resource_name_hash, resource_type_hash);
  55. break;
  56. }
  57. case SCRIPT_TYPE:
  58. {
  59. lua.compile(resource, resource_name_hash, resource_type_hash);
  60. break;
  61. }
  62. case VERTEX_SHADER_TYPE:
  63. {
  64. vs.compile(resource, resource_name_hash, resource_type_hash);
  65. break;
  66. }
  67. case PIXEL_SHADER_TYPE:
  68. {
  69. ps.compile(resource, resource_name_hash, resource_type_hash);
  70. break;
  71. }
  72. default:
  73. {
  74. Log::e("Resource type not supported.");
  75. break;
  76. }
  77. }
  78. }
  79. return 0;
  80. }
  81. //-----------------------------------------------------------------------------
  82. int32_t parse_command_line(int argc, char* argv[])
  83. {
  84. // Parse arguments
  85. static ArgsOption options[] =
  86. {
  87. "help", AOA_NO_ARGUMENT, NULL, 'h',
  88. "root-path", AOA_REQUIRED_ARGUMENT, NULL, 'r',
  89. "dest-path", AOA_REQUIRED_ARGUMENT, NULL, 'd',
  90. "seed", AOA_REQUIRED_ARGUMENT, NULL, 's',
  91. NULL, 0, NULL, 0
  92. };
  93. Args args(argc, argv, "", options);
  94. int32_t opt;
  95. while ((opt = args.getopt()) != -1)
  96. {
  97. switch (opt)
  98. {
  99. // Root path
  100. case 'r':
  101. {
  102. root_path = args.optarg();
  103. break;
  104. }
  105. // Dest path
  106. case 'd':
  107. {
  108. dest_path = args.optarg();
  109. break;
  110. }
  111. case 's':
  112. {
  113. hash_seed = atoi(args.optarg());
  114. break;
  115. }
  116. case 'h':
  117. case '?':
  118. default:
  119. {
  120. print_help_message(argv[0]);
  121. exit(EXIT_FAILURE);
  122. }
  123. }
  124. }
  125. return args.optind();
  126. }
  127. //-----------------------------------------------------------------------------
  128. void print_help_message(const char* program_name)
  129. {
  130. printf("Usage: %s [options] [resources]\n", program_name);
  131. printf
  132. (
  133. "Options:\n\n"
  134. " --help Show this help.\n"
  135. " --root-path <path> The absolute <path> whether to look for the input resources.\n"
  136. " --dest-path <path> The absolute <path> whether to put the compiled resources.\n"
  137. " --seed <value> The seed to use for generating output resource hashes.\n"
  138. );
  139. }
  140. //-----------------------------------------------------------------------------
  141. void check_arguments(const char* root_path, const char* dest_path)
  142. {
  143. if (root_path == NULL)
  144. {
  145. Log::e("you have to specify the root path with `--root-path`\n");
  146. exit(EXIT_FAILURE);
  147. }
  148. if (dest_path == NULL)
  149. {
  150. Log::e("you have to specify the destination path with `--dest-path`\n");
  151. exit(EXIT_FAILURE);
  152. }
  153. }