resource-compiler.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <iostream>
  24. #include <string>
  25. #include <map>
  26. #include "Args.h"
  27. #include "Path.h"
  28. #include "StringUtils.h"
  29. #include "Hash.h"
  30. #include "TGACompiler.h"
  31. #include "WAVCompiler.h"
  32. using namespace crown;
  33. using std::cout;
  34. using std::endl;
  35. using std::ofstream;
  36. using std::map;
  37. // Max number of requests per run
  38. const uint32_t MAX_COMPILE_REQUESTS = 512;
  39. const char* root_path = NULL;
  40. const char* dest_path = NULL;
  41. uint32_t hash_seed = 0;
  42. //-----------------------------------------------------------------------------
  43. static void print_help_message(const char* program_name)
  44. {
  45. cout << "Usage: " << program_name << " [options] [resources]" << endl;
  46. cout <<
  47. "Options:\n\n"
  48. " --help Show this help.\n"
  49. " --root-path <path> The absolute <path> whether to look for the input resources.\n"
  50. " --dest-path <path> The absolute <path> whether to put the compiled resources.\n"
  51. " --seed <value> The seed to use for generating output resource hashes.\n";
  52. }
  53. //-----------------------------------------------------------------------------
  54. static int32_t parse_command_line(int argc, char* argv[])
  55. {
  56. // Parse arguments
  57. static ArgsOption options[] =
  58. {
  59. "help", AOA_NO_ARGUMENT, NULL, 'h',
  60. "root-path", AOA_REQUIRED_ARGUMENT, NULL, 'r',
  61. "dest-path", AOA_REQUIRED_ARGUMENT, NULL, 'd',
  62. "seed", AOA_REQUIRED_ARGUMENT, NULL, 's',
  63. NULL, 0, NULL, 0
  64. };
  65. Args args(argc, argv, "", options);
  66. int32_t opt;
  67. while ((opt = args.getopt()) != -1)
  68. {
  69. switch (opt)
  70. {
  71. // Root path
  72. case 'r':
  73. {
  74. root_path = args.optarg();
  75. break;
  76. }
  77. // Dest path
  78. case 'd':
  79. {
  80. dest_path = args.optarg();
  81. break;
  82. }
  83. case 's':
  84. {
  85. hash_seed = atoi(args.optarg());
  86. break;
  87. }
  88. case 'h':
  89. case '?':
  90. default:
  91. {
  92. print_help_message(argv[0]);
  93. exit(EXIT_FAILURE);
  94. }
  95. }
  96. }
  97. return args.optind();
  98. }
  99. //-----------------------------------------------------------------------------
  100. static void check_arguments(const char* root_path, const char* dest_path)
  101. {
  102. if (root_path == NULL)
  103. {
  104. cout << "you have to specify the root path with `--root-path`" << endl;
  105. exit(EXIT_FAILURE);
  106. }
  107. if (dest_path == NULL)
  108. {
  109. cout << "you have to specify the destination path with `--dest-path`" << endl;
  110. exit(EXIT_FAILURE);
  111. }
  112. }
  113. //-----------------------------------------------------------------------------
  114. int main(int argc, char** argv)
  115. {
  116. int32_t first_resource = parse_command_line(argc, argv);
  117. // Check if all the mandatory options are set
  118. check_arguments(root_path, dest_path);
  119. // If there are no resources
  120. if (first_resource >= argc)
  121. {
  122. cout << "You have to specify at least one resource." << endl;
  123. exit(EXIT_FAILURE);
  124. }
  125. // Register compilers
  126. TGACompiler tga;
  127. WAVCompiler wav;
  128. map<const char*, Compiler*> compilers;
  129. compilers["tga"] = &tga;
  130. compilers["wav"] = &wav;
  131. // Open debug output file
  132. ofstream debug_file("/home/dani/Desktop/compiler.json");
  133. if (debug_file.is_open())
  134. {
  135. debug_file << "{\n";
  136. }
  137. for (int32_t i = 0; i < argc - first_resource; i++)
  138. {
  139. const char* resource = argv[first_resource + i];
  140. char resource_name[1024];
  141. char resource_type[1024];
  142. path::filename_without_extension(resource, resource_name, 1024);
  143. path::extension(resource, resource_type, 1024);
  144. uint32_t resource_name_hash = hash::murmur2_32(resource_name, string::strlen(resource_name), hash_seed);
  145. uint32_t resource_type_hash = hash::murmur2_32(resource_type, string::strlen(resource_type), 0);
  146. char out_name[1024];
  147. snprintf(out_name, 1024, "%.8X%.8X", resource_name_hash, resource_type_hash);
  148. cout << out_name << " <= " << argv[first_resource + i] << endl;
  149. map<const char*, Compiler*>::iterator it = compilers.find(resource_type);
  150. if (it != compilers.end())
  151. {
  152. if (!it->second->compile(root_path, dest_path, argv[first_resource + i], out_name))
  153. {
  154. cout << "Exiting." << endl;
  155. exit(EXIT_FAILURE);
  156. }
  157. }
  158. else
  159. {
  160. cout << "No compilers found for type '" << resource_type << "'." << endl;
  161. exit(EXIT_FAILURE);
  162. }
  163. // Debug stuff
  164. debug_file << " \"" << out_name << "\" : " << "\"" << argv[first_resource + i] << "\"";
  165. if (argc - first_resource - i != 1)
  166. {
  167. debug_file << ",";
  168. }
  169. debug_file << "\n";
  170. }
  171. debug_file << "}\n";
  172. debug_file.close();
  173. return 0;
  174. }