main.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "Crown.h"
  4. #include "TGACompiler.h"
  5. const char* root_path = NULL;
  6. const char* dest_path = NULL;
  7. const char* resource_in = NULL;
  8. uint32_t hash_seed = 0;
  9. using namespace crown;
  10. void print_help_message(const char* program_name);
  11. void parse_command_line(int argc, char** argv);
  12. /// TGA compiler for "tga" resource type
  13. /// TODO: Explain supported formats, usage etc.
  14. int main(int argc, char** argv)
  15. {
  16. parse_command_line(argc, argv);
  17. if (root_path == NULL)
  18. {
  19. printf("%s: ERROR: you have to specify the root path with `--root-path`\n", argv[0]);
  20. exit(-1);
  21. }
  22. if (dest_path == NULL)
  23. {
  24. printf("%s: ERROR: you have to specify the destination path with `--dest-path`\n", argv[0]);
  25. exit(-1);
  26. }
  27. if (resource_in == NULL)
  28. {
  29. printf("%s: ERROR: you have to specify the resource name with `--resource-in`\n", argv[0]);
  30. exit(-1);
  31. }
  32. // FIXME: validate input
  33. TGACompiler compiler(root_path, dest_path, resource_in, hash_seed);
  34. if (compiler.compile() == false)
  35. {
  36. printf("%s: ERROR: compilation failed for resource %s\n", argv[0], compiler.resource_path());
  37. exit(-1);
  38. }
  39. compiler.write();
  40. return 0;
  41. }
  42. //-----------------------------------------------------------------------------
  43. void parse_command_line(int argc, char** argv)
  44. {
  45. // Parse arguments
  46. ArgsOption options[] =
  47. {
  48. "help", AOA_NO_ARGUMENT, NULL, 'h',
  49. "root-path", AOA_REQUIRED_ARGUMENT, NULL, 'r',
  50. "dest-path", AOA_REQUIRED_ARGUMENT, NULL, 'd',
  51. "resource-in", AOA_REQUIRED_ARGUMENT, NULL, 'i',
  52. "seed", AOA_REQUIRED_ARGUMENT, NULL, 's',
  53. NULL, 0, NULL, 0
  54. };
  55. Args args(argc, argv, "", options);
  56. while (1)
  57. {
  58. int32_t ret = args.next_option();
  59. switch (ret)
  60. {
  61. case -1:
  62. {
  63. return;
  64. }
  65. // Help message
  66. case 'h':
  67. {
  68. print_help_message(argv[0]);
  69. exit(0);
  70. }
  71. // Root path
  72. case 'r':
  73. {
  74. if (args.option_argument() == NULL)
  75. {
  76. printf("%s: ERROR: missing path after `--root-path`\n", argv[0]);
  77. exit(-1);
  78. }
  79. root_path = args.option_argument();
  80. break;
  81. }
  82. // Dest path
  83. case 'd':
  84. {
  85. if (args.option_argument() == NULL)
  86. {
  87. printf("%s: ERROR: missing path after `--dest-path`\n", argv[0]);
  88. exit(-1);
  89. }
  90. dest_path = args.option_argument();
  91. break;
  92. }
  93. // Resource in
  94. case 'i':
  95. {
  96. if (args.option_argument() == NULL)
  97. {
  98. printf("%s: ERROR: missing path after `--resource-in`\n", argv[0]);
  99. exit(-1);
  100. }
  101. resource_in = args.option_argument();
  102. break;
  103. }
  104. case 's':
  105. {
  106. if (args.option_argument() == NULL)
  107. {
  108. printf("%s: ERROR: missing seed value after `--seed`\n", argv[0]);
  109. exit(-1);
  110. }
  111. hash_seed = atoi(args.option_argument());
  112. break;
  113. }
  114. default:
  115. {
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. //-----------------------------------------------------------------------------
  122. void print_help_message(const char* program_name)
  123. {
  124. printf("Usage: %s [options]\n", program_name);
  125. printf("Options:\n\n");
  126. printf(" --help Show this help.\n");
  127. printf(" --root-path <path> The _absolute_ <path> whether to look for the input resource.\n");
  128. printf(" --dest-path <path> The _absolute_ <path> whether to put the compiled resource.\n");
  129. printf(" --resource-in <path> The _relative_ <path> of the input resource.\n");
  130. printf(" --seed <value> The unsigned integer <value> of the hash seed.\n");
  131. }