resource-hash.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. int32_t opt;
  54. while ((opt = args.getopt()) != -1)
  55. {
  56. switch (opt)
  57. {
  58. // Resource in
  59. case 'i':
  60. {
  61. resource_in = args.optarg();
  62. break;
  63. }
  64. case 's':
  65. {
  66. hash_seed = atoi(args.optarg());
  67. break;
  68. }
  69. case 'h':
  70. case '?':
  71. default:
  72. {
  73. print_help_message(argv[0]);
  74. exit(-1);
  75. }
  76. }
  77. }
  78. }
  79. //-----------------------------------------------------------------------------
  80. void print_help_message(const char* program_name)
  81. {
  82. printf("Usage: %s [options]\n", program_name);
  83. printf("Options:\n\n");
  84. printf(" --help Show this help.\n");
  85. printf(" --resource-in <name> The <name> of the input resource.\n");
  86. printf(" --seed <value> The unsigned integer <value> of the hash seed.\n");
  87. }