resource-hash.cpp 2.8 KB

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