ResourceID.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * ResourceID.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "ResourceID.h"
  12. #include "FileInfo.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. // trivial to_upper that completely ignores localization and only work with ASCII
  15. // Technically not a problem since
  16. // 1) Right now VCMI does not supports unicode in filenames on Win
  17. // 2) Filesystem case-sensivity is only problem for H3 data which uses ASCII-only symbols
  18. // for me (Ivan) this define gives notable decrease in loading times
  19. // #define ENABLE_TRIVIAL_TOUPPER
  20. #ifdef ENABLE_TRIVIAL_TOUPPER
  21. static inline void toUpper(char & symbol)
  22. {
  23. static const int diff = 'a' - 'A';
  24. if (symbol >= 'a' && symbol <= 'z')
  25. symbol -= diff;
  26. }
  27. static inline void toUpper(std::string & string)
  28. {
  29. for (char & symbol : string)
  30. toUpper(symbol);
  31. }
  32. #else
  33. static inline void toUpper(std::string & string)
  34. {
  35. boost::to_upper(string);
  36. }
  37. #endif
  38. static inline EResType::Type readType(const std::string& name)
  39. {
  40. return EResTypeHelper::getTypeFromExtension(FileInfo::GetExtension(name).to_string());
  41. }
  42. static inline std::string readName(std::string name, EResType::Type type)
  43. {
  44. const auto dotPos = name.find_last_of('.');
  45. //do not cut "extension" of directory name
  46. auto delimPos = name.find_last_of('/');
  47. if(delimPos == std::string::npos)
  48. delimPos = name.find_last_of('\\');
  49. if((delimPos == std::string::npos || delimPos < dotPos) && dotPos != std::string::npos)
  50. {
  51. auto type = EResTypeHelper::getTypeFromExtension(name.substr(dotPos));
  52. if(type != EResType::OTHER)
  53. name.resize(dotPos);
  54. }
  55. if(type != EResType::MAP && type != EResType::CAMPAIGN && type != EResType::SAVEGAME)
  56. toUpper(name);
  57. return name;
  58. }
  59. #if 0
  60. ResourceID::ResourceID()
  61. :type(EResType::OTHER)
  62. {
  63. }
  64. #endif
  65. ResourceID::ResourceID(std::string name_):
  66. type{readType(name_)},
  67. name{readName(std::move(name_), readType(name_))}
  68. {}
  69. ResourceID::ResourceID(std::string name_, EResType::Type type_):
  70. type{type_},
  71. name{readName(std::move(name_), type_)}
  72. {}
  73. #if 0
  74. std::string ResourceID::getName() const
  75. {
  76. return name;
  77. }
  78. EResType::Type ResourceID::getType() const
  79. {
  80. return type;
  81. }
  82. void ResourceID::setName(std::string name)
  83. {
  84. // setName shouldn't be used if type is UNDEFINED
  85. assert(type != EResType::UNDEFINED);
  86. this->name = std::move(name);
  87. size_t dotPos = this->name.find_last_of("/.");
  88. if(dotPos != std::string::npos && this->name[dotPos] == '.'
  89. && this->type == EResTypeHelper::getTypeFromExtension(this->name.substr(dotPos)))
  90. {
  91. this->name.erase(dotPos);
  92. }
  93. toUpper(this->name);
  94. }
  95. void ResourceID::setType(EResType::Type type)
  96. {
  97. this->type = type;
  98. }
  99. #endif
  100. EResType::Type EResTypeHelper::getTypeFromExtension(std::string extension)
  101. {
  102. toUpper(extension);
  103. static const std::map<std::string, EResType::Type> stringToRes =
  104. {
  105. {".TXT", EResType::TEXT},
  106. {".JSON", EResType::TEXT},
  107. {".DEF", EResType::ANIMATION},
  108. {".MSK", EResType::MASK},
  109. {".MSG", EResType::MASK},
  110. {".H3C", EResType::CAMPAIGN},
  111. {".H3M", EResType::MAP},
  112. {".FNT", EResType::BMP_FONT},
  113. {".TTF", EResType::TTF_FONT},
  114. {".BMP", EResType::IMAGE},
  115. {".GIF", EResType::IMAGE},
  116. {".JPG", EResType::IMAGE},
  117. {".PCX", EResType::IMAGE},
  118. {".PNG", EResType::IMAGE},
  119. {".TGA", EResType::IMAGE},
  120. {".WAV", EResType::SOUND},
  121. {".82M", EResType::SOUND},
  122. {".MP3", EResType::SOUND},
  123. {".OGG", EResType::SOUND},
  124. {".FLAC", EResType::SOUND},
  125. {".SMK", EResType::VIDEO},
  126. {".BIK", EResType::VIDEO},
  127. {".MJPG", EResType::VIDEO},
  128. {".MPG", EResType::VIDEO},
  129. {".AVI", EResType::VIDEO},
  130. {".ZIP", EResType::ARCHIVE_ZIP},
  131. {".LOD", EResType::ARCHIVE_LOD},
  132. {".PAC", EResType::ARCHIVE_LOD},
  133. {".VID", EResType::ARCHIVE_VID},
  134. {".SND", EResType::ARCHIVE_SND},
  135. {".PAL", EResType::PALETTE},
  136. {".VSGM1", EResType::SAVEGAME},
  137. {".ERM", EResType::ERM},
  138. {".ERT", EResType::ERT},
  139. {".ERS", EResType::ERS},
  140. {".VMAP", EResType::MAP},
  141. {".VCMP", EResType::CAMPAIGN},
  142. {".VERM", EResType::ERM},
  143. {".LUA", EResType::LUA}
  144. };
  145. auto iter = stringToRes.find(extension);
  146. if (iter == stringToRes.end())
  147. return EResType::OTHER;
  148. return iter->second;
  149. }
  150. std::string EResTypeHelper::getEResTypeAsString(EResType::Type type)
  151. {
  152. #define MAP_ENUM(value) {EResType::value, #value},
  153. static const std::map<EResType::Type, std::string> stringToRes =
  154. {
  155. MAP_ENUM(TEXT)
  156. MAP_ENUM(ANIMATION)
  157. MAP_ENUM(MASK)
  158. MAP_ENUM(CAMPAIGN)
  159. MAP_ENUM(MAP)
  160. MAP_ENUM(BMP_FONT)
  161. MAP_ENUM(TTF_FONT)
  162. MAP_ENUM(IMAGE)
  163. MAP_ENUM(VIDEO)
  164. MAP_ENUM(SOUND)
  165. MAP_ENUM(ARCHIVE_ZIP)
  166. MAP_ENUM(ARCHIVE_LOD)
  167. MAP_ENUM(ARCHIVE_SND)
  168. MAP_ENUM(ARCHIVE_VID)
  169. MAP_ENUM(PALETTE)
  170. MAP_ENUM(SAVEGAME)
  171. MAP_ENUM(DIRECTORY)
  172. MAP_ENUM(ERM)
  173. MAP_ENUM(ERT)
  174. MAP_ENUM(ERS)
  175. MAP_ENUM(OTHER)
  176. };
  177. #undef MAP_ENUM
  178. auto iter = stringToRes.find(type);
  179. assert(iter != stringToRes.end());
  180. return iter->second;
  181. }
  182. VCMI_LIB_NAMESPACE_END