resource-hash.cpp 2.9 KB

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