ResourceID.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "StdInc.h"
  2. #include "ResourceID.h"
  3. #include "FileInfo.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. #else
  23. static inline void toUpper(std::string & string)
  24. {
  25. boost::to_upper(string);
  26. }
  27. #endif
  28. static inline EResType::Type readType(const std::string& name)
  29. {
  30. return EResTypeHelper::getTypeFromExtension(FileInfo::GetExtension(name).to_string());
  31. }
  32. static inline std::string readName(std::string name)
  33. {
  34. const auto dotPos = name.find_last_of('.');
  35. //do not cut "extension" of directory name
  36. auto delimPos = name.find_last_of('/');
  37. if(delimPos == std::string::npos)
  38. delimPos = name.find_last_of('\\');
  39. if((delimPos == std::string::npos || delimPos < dotPos) && dotPos != std::string::npos)
  40. {
  41. auto type = EResTypeHelper::getTypeFromExtension(name.substr(dotPos));
  42. if(type != EResType::OTHER)
  43. name.resize(dotPos);
  44. }
  45. toUpper(name);
  46. return name;
  47. }
  48. #if 0
  49. ResourceID::ResourceID()
  50. :type(EResType::OTHER)
  51. {
  52. }
  53. #endif
  54. ResourceID::ResourceID(std::string name_):
  55. type{readType(name_)},
  56. name{readName(std::move(name_))}
  57. {}
  58. ResourceID::ResourceID(std::string name_, EResType::Type type_):
  59. type{type_},
  60. name{readName(std::move(name_))}
  61. {}
  62. #if 0
  63. std::string ResourceID::getName() const
  64. {
  65. return name;
  66. }
  67. EResType::Type ResourceID::getType() const
  68. {
  69. return type;
  70. }
  71. void ResourceID::setName(std::string name)
  72. {
  73. // setName shouldn't be used if type is UNDEFINED
  74. assert(type != EResType::UNDEFINED);
  75. this->name = std::move(name);
  76. size_t dotPos = this->name.find_last_of("/.");
  77. if(dotPos != std::string::npos && this->name[dotPos] == '.'
  78. && this->type == EResTypeHelper::getTypeFromExtension(this->name.substr(dotPos)))
  79. {
  80. this->name.erase(dotPos);
  81. }
  82. toUpper(this->name);
  83. }
  84. void ResourceID::setType(EResType::Type type)
  85. {
  86. this->type = type;
  87. }
  88. #endif
  89. EResType::Type EResTypeHelper::getTypeFromExtension(std::string extension)
  90. {
  91. toUpper(extension);
  92. static const std::map<std::string, EResType::Type> stringToRes =
  93. {
  94. {".TXT", EResType::TEXT},
  95. {".JSON", EResType::TEXT},
  96. {".DEF", EResType::ANIMATION},
  97. {".MSK", EResType::MASK},
  98. {".MSG", EResType::MASK},
  99. {".H3C", EResType::CAMPAIGN},
  100. {".H3M", EResType::MAP},
  101. {".FNT", EResType::BMP_FONT},
  102. {".TTF", EResType::TTF_FONT},
  103. {".BMP", EResType::IMAGE},
  104. {".JPG", EResType::IMAGE},
  105. {".PCX", EResType::IMAGE},
  106. {".PNG", EResType::IMAGE},
  107. {".TGA", EResType::IMAGE},
  108. {".WAV", EResType::SOUND},
  109. {".82M", EResType::SOUND},
  110. {".SMK", EResType::VIDEO},
  111. {".BIK", EResType::VIDEO},
  112. {".MJPG", EResType::VIDEO},
  113. {".MPG", EResType::VIDEO},
  114. {".AVI", EResType::VIDEO},
  115. {".MP3", EResType::MUSIC},
  116. {".OGG", EResType::MUSIC},
  117. {".FLAC", EResType::MUSIC},
  118. {".ZIP", EResType::ARCHIVE_ZIP},
  119. {".LOD", EResType::ARCHIVE_LOD},
  120. {".PAC", EResType::ARCHIVE_LOD},
  121. {".VID", EResType::ARCHIVE_VID},
  122. {".SND", EResType::ARCHIVE_SND},
  123. {".PAL", EResType::PALETTE},
  124. {".VCGM1", EResType::CLIENT_SAVEGAME},
  125. {".VSGM1", EResType::SERVER_SAVEGAME},
  126. {".ERM", EResType::ERM},
  127. {".ERT", EResType::ERT},
  128. {".ERS", EResType::ERS},
  129. {".VMAP", EResType::MAP}
  130. };
  131. auto iter = stringToRes.find(extension);
  132. if (iter == stringToRes.end())
  133. return EResType::OTHER;
  134. return iter->second;
  135. }
  136. std::string EResTypeHelper::getEResTypeAsString(EResType::Type type)
  137. {
  138. #define MAP_ENUM(value) {EResType::value, #value},
  139. static const std::map<EResType::Type, std::string> stringToRes =
  140. {
  141. MAP_ENUM(TEXT)
  142. MAP_ENUM(ANIMATION)
  143. MAP_ENUM(MASK)
  144. MAP_ENUM(CAMPAIGN)
  145. MAP_ENUM(MAP)
  146. MAP_ENUM(BMP_FONT)
  147. MAP_ENUM(TTF_FONT)
  148. MAP_ENUM(IMAGE)
  149. MAP_ENUM(VIDEO)
  150. MAP_ENUM(SOUND)
  151. MAP_ENUM(MUSIC)
  152. MAP_ENUM(ARCHIVE_ZIP)
  153. MAP_ENUM(ARCHIVE_LOD)
  154. MAP_ENUM(ARCHIVE_SND)
  155. MAP_ENUM(ARCHIVE_VID)
  156. MAP_ENUM(PALETTE)
  157. MAP_ENUM(CLIENT_SAVEGAME)
  158. MAP_ENUM(SERVER_SAVEGAME)
  159. MAP_ENUM(DIRECTORY)
  160. MAP_ENUM(ERM)
  161. MAP_ENUM(ERT)
  162. MAP_ENUM(ERS)
  163. MAP_ENUM(OTHER)
  164. };
  165. #undef MAP_ENUM
  166. auto iter = stringToRes.find(type);
  167. assert(iter != stringToRes.end());
  168. return iter->second;
  169. }