main.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include "Crown.h"
  25. #include "MATCompiler.h"
  26. using namespace crown;
  27. const char* root_path = NULL;
  28. const char* dest_path = NULL;
  29. const char* resource_in = NULL;
  30. uint32_t hash_seed = 0;
  31. void print_help_message(const char* program_name);
  32. void parse_command_line(int argc, char** argv);
  33. /// UTF-8 compiler for "txt" resource types
  34. int main(int argc, char** argv)
  35. {
  36. parse_command_line(argc, argv);
  37. if (root_path == NULL)
  38. {
  39. printf("%s: ERROR: you have to specify the root path with `--root-path`\n", argv[0]);
  40. exit(-1);
  41. }
  42. if (dest_path == NULL)
  43. {
  44. printf("%s: ERROR: you have to specify the destination path with `--dest-path`\n", argv[0]);
  45. exit(-1);
  46. }
  47. if (resource_in == NULL)
  48. {
  49. printf("%s: ERROR: you have to specify the resource name with `--resource-in`\n", argv[0]);
  50. exit(-1);
  51. }
  52. // FIXME: validate input
  53. MATCompiler compiler(root_path, dest_path, resource_in, hash_seed);
  54. if (compiler.compile() == false)
  55. {
  56. printf("%s: ERROR: compilation failed for resource %s\n", argv[0], compiler.resource_path());
  57. exit(-1);
  58. }
  59. compiler.write();
  60. return 0;
  61. }
  62. void parse_command_line(int argc, char** argv)
  63. {
  64. // Parse arguments
  65. ArgsOption options[] =
  66. {
  67. "help", AOA_NO_ARGUMENT, NULL, 'h',
  68. "root-path", AOA_REQUIRED_ARGUMENT, NULL, 'r',
  69. "dest-path", AOA_REQUIRED_ARGUMENT, NULL, 'd',
  70. "resource-in", AOA_REQUIRED_ARGUMENT, NULL, 'i',
  71. "seed", AOA_REQUIRED_ARGUMENT, NULL, 's',
  72. NULL, 0, NULL, 0
  73. };
  74. Args args(argc, argv, "", options);
  75. while (1)
  76. {
  77. int32_t ret = args.next_option();
  78. switch (ret)
  79. {
  80. case -1:
  81. {
  82. return;
  83. }
  84. // Help message
  85. case 'h':
  86. {
  87. print_help_message(argv[0]);
  88. exit(0);
  89. }
  90. // Root path
  91. case 'r':
  92. {
  93. if (args.option_argument() == NULL)
  94. {
  95. printf("%s: ERROR: missing path after `--root-path`\n", argv[0]);
  96. exit(-1);
  97. }
  98. root_path = args.option_argument();
  99. break;
  100. }
  101. // Dest path
  102. case 'd':
  103. {
  104. if (args.option_argument() == NULL)
  105. {
  106. printf("%s: ERROR: missing path after `--dest-path`\n", argv[0]);
  107. exit(-1);
  108. }
  109. dest_path = args.option_argument();
  110. break;
  111. }
  112. // Resource in
  113. case 'i':
  114. {
  115. if (args.option_argument() == NULL)
  116. {
  117. printf("%s: ERROR: missing path after `--resource-in`\n", argv[0]);
  118. exit(-1);
  119. }
  120. resource_in = args.option_argument();
  121. break;
  122. }
  123. case 's':
  124. {
  125. if (args.option_argument() == NULL)
  126. {
  127. printf("%s: ERROR: missing seed value after `--seed`\n", argv[0]);
  128. exit(-1);
  129. }
  130. hash_seed = atoi(args.option_argument());
  131. break;
  132. }
  133. default:
  134. {
  135. break;
  136. }
  137. }
  138. }
  139. }
  140. void print_help_message(const char* program_name)
  141. {
  142. printf("Usage: %s [options]\n", program_name);
  143. printf("Options:\n\n");
  144. printf(" --help Show this help.\n");
  145. printf(" --root-path <path> The _absolute_ <path> whether to look for the input resource.\n");
  146. printf(" --dest-path <path> The _absolute_ <path> whether to put the compiled resource.\n");
  147. printf(" --resource-in <path> The _relative_ <path> of the input resource.\n");
  148. printf(" --seed <value> The unsigned integer <value> of the hash seed.\n");
  149. }