resource-hash.cpp 2.8 KB

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