resource-hash.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "Crown.h"
  4. using namespace crown;
  5. const char* resource_in = NULL;
  6. uint32_t hash_seed = 0;
  7. void print_help_message(const char* program_name);
  8. void parse_command_line(int argc, char** argv);
  9. /// Generates 64bit hash for the specified resource based on a seed
  10. int main(int argc, char** argv)
  11. {
  12. parse_command_line(argc, argv);
  13. if (resource_in == NULL)
  14. {
  15. printf("%s: ERROR: you have to specify the resource name with `--resource-in`\n", argv[0]);
  16. exit(-1);
  17. }
  18. char resource_basename[256];
  19. char resource_extension[256];
  20. path::filename_without_extension(resource_in, resource_basename, 256);
  21. path::extension(resource_in, resource_extension, 256);
  22. uint32_t resource_basename_hash = hash::murmur2_32(resource_basename, string::strlen(resource_basename), hash_seed);
  23. uint32_t resource_extension_hash = hash::murmur2_32(resource_extension, string::strlen(resource_extension), 0);
  24. if (resource_extension_hash != TEXTURE_TYPE &&
  25. resource_extension_hash != MESH_TYPE &&
  26. resource_extension_hash != SCRIPT_TYPE &&
  27. resource_extension_hash != TEXT_TYPE &&
  28. resource_extension_hash != MATERIAL_TYPE)
  29. {
  30. printf("%s: ERROR: cannot generate hash for resource '%s': Unknown type.\n", argv[0], resource_in);
  31. exit(-1);
  32. }
  33. char out_filename[512];
  34. out_filename[0] = '\0';
  35. snprintf(out_filename, 256, "%.8X%.8X", resource_basename_hash, resource_extension_hash);
  36. printf("%s\n", out_filename);
  37. return 0;
  38. }
  39. //-----------------------------------------------------------------------------
  40. void parse_command_line(int argc, char** argv)
  41. {
  42. // Parse arguments
  43. ArgsOption options[] =
  44. {
  45. "help", AOA_NO_ARGUMENT, NULL, 'h',
  46. "resource-in", AOA_REQUIRED_ARGUMENT, NULL, 'i',
  47. "seed", AOA_REQUIRED_ARGUMENT, NULL, 's',
  48. NULL, 0, NULL, 0
  49. };
  50. Args args(argc, argv, "", options);
  51. while (1)
  52. {
  53. int32_t ret = args.next_option();
  54. switch (ret)
  55. {
  56. case -1:
  57. {
  58. return;
  59. }
  60. // Help message
  61. case 'h':
  62. {
  63. print_help_message(argv[0]);
  64. exit(0);
  65. }
  66. // Resource in
  67. case 'i':
  68. {
  69. if (args.option_argument() == NULL)
  70. {
  71. printf("%s: ERROR: missing path after `--resource-in`\n", argv[0]);
  72. exit(-1);
  73. }
  74. resource_in = args.option_argument();
  75. break;
  76. }
  77. case 's':
  78. {
  79. if (args.option_argument() == NULL)
  80. {
  81. printf("%s: ERROR: missing seed value after `--seed`\n", argv[0]);
  82. exit(-1);
  83. }
  84. hash_seed = atoi(args.option_argument());
  85. break;
  86. }
  87. default:
  88. {
  89. break;
  90. }
  91. }
  92. }
  93. }
  94. //-----------------------------------------------------------------------------
  95. void print_help_message(const char* program_name)
  96. {
  97. printf("Usage: %s [options]\n", program_name);
  98. printf("Options:\n\n");
  99. printf(" --help Show this help.\n");
  100. printf(" --resource-in <name> The <name> of the input resource.\n");
  101. printf(" --seed <value> The unsigned integer <value> of the hash seed.\n");
  102. }