resource-hash.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. resource_extension_hash != VERTEX_SHADER_TYPE &&
  30. resource_extension_hash != PIXEL_SHADER_TYPE)
  31. {
  32. printf("%s: ERROR: cannot generate hash for resource '%s': Unknown type.\n", argv[0], resource_in);
  33. exit(-1);
  34. }
  35. char out_filename[512];
  36. out_filename[0] = '\0';
  37. snprintf(out_filename, 256, "%.8X%.8X", resource_basename_hash, resource_extension_hash);
  38. printf("%s\n", out_filename);
  39. return 0;
  40. }
  41. //-----------------------------------------------------------------------------
  42. void parse_command_line(int argc, char** argv)
  43. {
  44. // Parse arguments
  45. ArgsOption options[] =
  46. {
  47. "help", AOA_NO_ARGUMENT, NULL, 'h',
  48. "resource-in", AOA_REQUIRED_ARGUMENT, NULL, 'i',
  49. "seed", AOA_REQUIRED_ARGUMENT, NULL, 's',
  50. NULL, 0, NULL, 0
  51. };
  52. Args args(argc, argv, "", options);
  53. while (1)
  54. {
  55. int32_t ret = args.next_option();
  56. switch (ret)
  57. {
  58. case -1:
  59. {
  60. return;
  61. }
  62. // Help message
  63. case 'h':
  64. {
  65. print_help_message(argv[0]);
  66. exit(0);
  67. }
  68. // Resource in
  69. case 'i':
  70. {
  71. if (args.option_argument() == NULL)
  72. {
  73. printf("%s: ERROR: missing path after `--resource-in`\n", argv[0]);
  74. exit(-1);
  75. }
  76. resource_in = args.option_argument();
  77. break;
  78. }
  79. case 's':
  80. {
  81. if (args.option_argument() == NULL)
  82. {
  83. printf("%s: ERROR: missing seed value after `--seed`\n", argv[0]);
  84. exit(-1);
  85. }
  86. hash_seed = atoi(args.option_argument());
  87. break;
  88. }
  89. default:
  90. {
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. //-----------------------------------------------------------------------------
  97. void print_help_message(const char* program_name)
  98. {
  99. printf("Usage: %s [options]\n", program_name);
  100. printf("Options:\n\n");
  101. printf(" --help Show this help.\n");
  102. printf(" --resource-in <name> The <name> of the input resource.\n");
  103. printf(" --seed <value> The unsigned integer <value> of the hash seed.\n");
  104. }