ResourceID.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "StdInc.h"
  2. #include "ResourceID.h"
  3. #include "CFileInfo.h"
  4. // trivial to_upper that completely ignores localization and only work with ASCII
  5. // Technically not a problem since
  6. // 1) Right now VCMI does not supports unicode in filenames on Win
  7. // 2) Filesystem case-sensivity is only problem for H3 data which uses ASCII-only symbols
  8. // for me (Ivan) this define gives notable decrease in loading times
  9. // #define ENABLE_TRIVIAL_TOUPPER
  10. #ifdef ENABLE_TRIVIAL_TOUPPER
  11. static inline void toUpper(char & symbol)
  12. {
  13. static const int diff = 'a' - 'A';
  14. if (symbol >= 'a' && symbol <= 'z')
  15. symbol -= diff;
  16. }
  17. static inline void toUpper(std::string & string)
  18. {
  19. for (char & symbol : string)
  20. toUpper(symbol);
  21. }
  22. #endif
  23. ResourceID::ResourceID()
  24. :type(EResType::OTHER)
  25. {
  26. }
  27. ResourceID::ResourceID(std::string name)
  28. {
  29. CFileInfo info(std::move(name));
  30. setName(info.getStem());
  31. setType(info.getType());
  32. }
  33. ResourceID::ResourceID(std::string name, EResType::Type type)
  34. {
  35. setName(std::move(name));
  36. setType(type);
  37. }
  38. std::string ResourceID::getName() const
  39. {
  40. return name;
  41. }
  42. EResType::Type ResourceID::getType() const
  43. {
  44. return type;
  45. }
  46. void ResourceID::setName(std::string name)
  47. {
  48. this->name = std::move(name);
  49. size_t dotPos = this->name.find_last_of("/.");
  50. if(dotPos != std::string::npos && this->name[dotPos] == '.')
  51. this->name.erase(dotPos);
  52. #ifdef ENABLE_TRIVIAL_TOUPPER
  53. toUpper(this->name);
  54. #else
  55. // strangely enough but this line takes 40-50% of filesystem loading time
  56. boost::to_upper(this->name);
  57. #endif
  58. }
  59. void ResourceID::setType(EResType::Type type)
  60. {
  61. this->type = type;
  62. }
  63. EResType::Type EResTypeHelper::getTypeFromExtension(std::string extension)
  64. {
  65. #ifdef ENABLE_TRIVIAL_TOUPPER
  66. toUpper(extension);
  67. #else
  68. boost::to_upper(extension);
  69. #endif
  70. static const std::map<std::string, EResType::Type> stringToRes =
  71. {
  72. {".TXT", EResType::TEXT},
  73. {".JSON", EResType::TEXT},
  74. {".DEF", EResType::ANIMATION},
  75. {".MSK", EResType::MASK},
  76. {".MSG", EResType::MASK},
  77. {".H3C", EResType::CAMPAIGN},
  78. {".H3M", EResType::MAP},
  79. {".FNT", EResType::BMP_FONT},
  80. {".TTF", EResType::TTF_FONT},
  81. {".BMP", EResType::IMAGE},
  82. {".JPG", EResType::IMAGE},
  83. {".PCX", EResType::IMAGE},
  84. {".PNG", EResType::IMAGE},
  85. {".TGA", EResType::IMAGE},
  86. {".WAV", EResType::SOUND},
  87. {".82M", EResType::SOUND},
  88. {".SMK", EResType::VIDEO},
  89. {".BIK", EResType::VIDEO},
  90. {".MJPG", EResType::VIDEO},
  91. {".MPG", EResType::VIDEO},
  92. {".AVI", EResType::VIDEO},
  93. {".MP3", EResType::MUSIC},
  94. {".OGG", EResType::MUSIC},
  95. {".FLAC", EResType::MUSIC},
  96. {".ZIP", EResType::ARCHIVE_ZIP},
  97. {".LOD", EResType::ARCHIVE_LOD},
  98. {".PAC", EResType::ARCHIVE_LOD},
  99. {".VID", EResType::ARCHIVE_VID},
  100. {".SND", EResType::ARCHIVE_SND},
  101. {".PAL", EResType::PALETTE},
  102. {".VCGM1", EResType::CLIENT_SAVEGAME},
  103. {".VSGM1", EResType::SERVER_SAVEGAME},
  104. {".ERM", EResType::ERM},
  105. {".ERT", EResType::ERT},
  106. {".ERS", EResType::ERS}
  107. };
  108. auto iter = stringToRes.find(extension);
  109. if (iter == stringToRes.end())
  110. return EResType::OTHER;
  111. return iter->second;
  112. }
  113. std::string EResTypeHelper::getEResTypeAsString(EResType::Type type)
  114. {
  115. #define MAP_ENUM(value) {EResType::value, #value},
  116. static const std::map<EResType::Type, std::string> stringToRes =
  117. {
  118. MAP_ENUM(TEXT)
  119. MAP_ENUM(ANIMATION)
  120. MAP_ENUM(MASK)
  121. MAP_ENUM(CAMPAIGN)
  122. MAP_ENUM(MAP)
  123. MAP_ENUM(BMP_FONT)
  124. MAP_ENUM(TTF_FONT)
  125. MAP_ENUM(IMAGE)
  126. MAP_ENUM(VIDEO)
  127. MAP_ENUM(SOUND)
  128. MAP_ENUM(MUSIC)
  129. MAP_ENUM(ARCHIVE_ZIP)
  130. MAP_ENUM(ARCHIVE_LOD)
  131. MAP_ENUM(ARCHIVE_SND)
  132. MAP_ENUM(ARCHIVE_VID)
  133. MAP_ENUM(PALETTE)
  134. MAP_ENUM(CLIENT_SAVEGAME)
  135. MAP_ENUM(SERVER_SAVEGAME)
  136. MAP_ENUM(DIRECTORY)
  137. MAP_ENUM(ERM)
  138. MAP_ENUM(ERT)
  139. MAP_ENUM(ERS)
  140. MAP_ENUM(OTHER)
  141. };
  142. #undef MAP_ENUM
  143. auto iter = stringToRes.find(type);
  144. assert(iter != stringToRes.end());
  145. return iter->second;
  146. }