txt-compiler.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "Crown.h"
  4. using namespace crown;
  5. const char* root_path = NULL;
  6. const char* resource_in = NULL;
  7. const char* resource_out = NULL;
  8. void print_help_message(const char* program_name);
  9. void parse_command_line(int argc, char** argv);
  10. /// UTF-8 compiler for "txt" resource types
  11. int main(int argc, char** argv)
  12. {
  13. parse_command_line(argc, argv);
  14. // FIXME: validate input
  15. Filesystem fs_root(root_path);
  16. if (!fs_root.exists(resource_in))
  17. {
  18. printf("%s: ERROR: %s does not exist. Aborting.\n", argv[0], resource_in);
  19. return -1;
  20. }
  21. char resource_basename[256];
  22. char resource_extension[256];
  23. path::filename_without_extension(resource_in, resource_basename, 256);
  24. path::extension(resource_in, resource_extension, 256);
  25. uint32_t resource_basename_hash = hash::fnv1a_32(resource_basename, string::strlen(resource_basename));
  26. uint32_t resource_extension_hash = hash::fnv1a_32(resource_extension, string::strlen(resource_extension));
  27. FileStream* src_file = (FileStream*)fs_root.open(resource_in, SOM_READ);
  28. size_t src_file_size = src_file->size();
  29. ArchiveEntry archive_entry;
  30. archive_entry.name = resource_basename_hash;
  31. archive_entry.type = resource_extension_hash;
  32. archive_entry.offset = sizeof (ArchiveEntry);
  33. archive_entry.size = src_file_size + sizeof(uint32_t);
  34. void* buffer = new uint8_t[src_file_size];
  35. src_file->read(buffer, src_file_size);
  36. fs_root.close(src_file);
  37. FileStream* dest_file = (FileStream*)fs_root.open(resource_out, SOM_WRITE);
  38. dest_file->write(&archive_entry, sizeof(ArchiveEntry));
  39. dest_file->write(&src_file_size, sizeof(uint32_t));
  40. dest_file->write(buffer, src_file_size);
  41. fs_root.close(dest_file);
  42. return 0;
  43. }
  44. void parse_command_line(int argc, char** argv)
  45. {
  46. // Parse arguments
  47. ArgsOption options[] =
  48. {
  49. "help", AOA_NO_ARGUMENT, NULL, 'h',
  50. "root-path", AOA_REQUIRED_ARGUMENT, NULL, 'r',
  51. "resource-in", AOA_REQUIRED_ARGUMENT, NULL, 'i',
  52. "resource-out", AOA_REQUIRED_ARGUMENT, NULL, 'o',
  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. // Resource in
  83. case 'i':
  84. {
  85. if (args.option_argument() == NULL)
  86. {
  87. printf("%s: ERROR: missing path after `--resource-in`\n", argv[0]);
  88. exit(-1);
  89. }
  90. resource_in = args.option_argument();
  91. break;
  92. }
  93. // Resource out
  94. case 'o':
  95. {
  96. if (args.option_argument() == NULL)
  97. {
  98. printf("%s: ERROR: missing path after `--resource-out`\n", argv[0]);
  99. exit(-1);
  100. }
  101. resource_out = args.option_argument();
  102. break;
  103. }
  104. default:
  105. {
  106. break;
  107. }
  108. }
  109. }
  110. }
  111. void print_help_message(const char* program_name)
  112. {
  113. printf("Usage: %s [options]\n", program_name);
  114. printf("Options:\n\n");
  115. printf(" --help Show this help.\n");
  116. printf(" --root-path <path> The _absolute_ <path> whether to look for the input resource.\n");
  117. printf(" --resource-in <path> The _relative_ <path> of the input resource.\n");
  118. printf(" --resource-out <width> The _relative_ <path> of the output resource.\n");
  119. }