resource-compiler.cpp 4.6 KB

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