resource-hash.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "Crown.h"
  4. using namespace crown;
  5. const char* resource_in = NULL;
  6. const char* resource_out = NULL;
  7. uint32_t hash_seed = 0;
  8. void print_help_message(const char* program_name);
  9. void parse_command_line(int argc, char** argv);
  10. /// Generates 64bit hash for the specified resource based on a seed
  11. int main(int argc, char** argv)
  12. {
  13. parse_command_line(argc, argv);
  14. char resource_basename[256];
  15. char resource_extension[256];
  16. path::filename_without_extension(resource_in, resource_basename, 256);
  17. path::extension(resource_in, resource_extension, 256);
  18. uint32_t resource_basename_hash = hash::murmur2_32(resource_basename, string::strlen(resource_basename), hash_seed);
  19. uint32_t resource_extension_hash = hash::murmur2_32(resource_extension, string::strlen(resource_extension), hash_seed);
  20. char out_filename[512];
  21. out_filename[0] = '\0';
  22. snprintf(resource_basename, 256, "%X", resource_basename_hash);
  23. snprintf(resource_extension, 256, "%X", resource_extension_hash);
  24. string::strncat(out_filename, resource_basename, 512);
  25. string::strncat(out_filename, resource_extension, 512);
  26. resource_out = out_filename;
  27. printf("%s\n", resource_out);
  28. return 0;
  29. }
  30. //-----------------------------------------------------------------------------
  31. void parse_command_line(int argc, char** argv)
  32. {
  33. // Parse arguments
  34. ArgsOption options[] =
  35. {
  36. "help", AOA_NO_ARGUMENT, NULL, 'h',
  37. "resource-in", AOA_REQUIRED_ARGUMENT, NULL, 'i',
  38. "seed", AOA_REQUIRED_ARGUMENT, NULL, 's',
  39. NULL, 0, NULL, 0
  40. };
  41. Args args(argc, argv, "", options);
  42. while (1)
  43. {
  44. int32_t ret = args.next_option();
  45. switch (ret)
  46. {
  47. case -1:
  48. {
  49. return;
  50. }
  51. // Help message
  52. case 'h':
  53. {
  54. print_help_message(argv[0]);
  55. exit(0);
  56. }
  57. // Resource in
  58. case 'i':
  59. {
  60. if (args.option_argument() == NULL)
  61. {
  62. printf("%s: ERROR: missing path after `--resource-in`\n", argv[0]);
  63. exit(-1);
  64. }
  65. resource_in = args.option_argument();
  66. break;
  67. }
  68. case 's':
  69. {
  70. if (args.option_argument() == NULL)
  71. {
  72. printf("%s: ERROR: missing seed value after `--seed`\n", argv[0]);
  73. exit(-1);
  74. }
  75. hash_seed = atoi(args.option_argument());
  76. break;
  77. }
  78. default:
  79. {
  80. break;
  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. }