ResourceID.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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, bool uppercase)
  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(uppercase)
  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(name_, true)},
  68. originalName{readName(std::move(name_), false)}
  69. {}
  70. ResourceID::ResourceID(std::string name_, EResType::Type type_):
  71. type{type_},
  72. name{readName(name_, true)},
  73. originalName{readName(std::move(name_), false)}
  74. {}
  75. #if 0
  76. std::string ResourceID::getName() const
  77. {
  78. return name;
  79. }
  80. EResType::Type ResourceID::getType() const
  81. {
  82. return type;
  83. }
  84. void ResourceID::setName(std::string name)
  85. {
  86. // setName shouldn't be used if type is UNDEFINED
  87. assert(type != EResType::UNDEFINED);
  88. this->name = std::move(name);
  89. size_t dotPos = this->name.find_last_of("/.");
  90. if(dotPos != std::string::npos && this->name[dotPos] == '.'
  91. && this->type == EResTypeHelper::getTypeFromExtension(this->name.substr(dotPos)))
  92. {
  93. this->name.erase(dotPos);
  94. }
  95. toUpper(this->name);
  96. }
  97. void ResourceID::setType(EResType::Type type)
  98. {
  99. this->type = type;
  100. }
  101. #endif
  102. EResType::Type EResTypeHelper::getTypeFromExtension(std::string extension)
  103. {
  104. toUpper(extension);
  105. static const std::map<std::string, EResType::Type> stringToRes =
  106. {
  107. {".TXT", EResType::TEXT},
  108. {".JSON", EResType::TEXT},
  109. {".DEF", EResType::ANIMATION},
  110. {".MSK", EResType::MASK},
  111. {".MSG", EResType::MASK},
  112. {".H3C", EResType::CAMPAIGN},
  113. {".H3M", EResType::MAP},
  114. {".TUT", EResType::MAP},
  115. {".FNT", EResType::BMP_FONT},
  116. {".TTF", EResType::TTF_FONT},
  117. {".BMP", EResType::IMAGE},
  118. {".GIF", EResType::IMAGE},
  119. {".JPG", EResType::IMAGE},
  120. {".PCX", EResType::IMAGE},
  121. {".PNG", EResType::IMAGE},
  122. {".TGA", EResType::IMAGE},
  123. {".WAV", EResType::SOUND},
  124. {".82M", EResType::SOUND},
  125. {".MP3", EResType::SOUND},
  126. {".OGG", EResType::SOUND},
  127. {".FLAC", EResType::SOUND},
  128. {".SMK", EResType::VIDEO},
  129. {".BIK", EResType::VIDEO},
  130. {".MJPG", EResType::VIDEO},
  131. {".MPG", EResType::VIDEO},
  132. {".AVI", EResType::VIDEO},
  133. {".ZIP", EResType::ARCHIVE_ZIP},
  134. {".LOD", EResType::ARCHIVE_LOD},
  135. {".PAC", EResType::ARCHIVE_LOD},
  136. {".VID", EResType::ARCHIVE_VID},
  137. {".SND", EResType::ARCHIVE_SND},
  138. {".PAL", EResType::PALETTE},
  139. {".VSGM1", EResType::SAVEGAME},
  140. {".ERM", EResType::ERM},
  141. {".ERT", EResType::ERT},
  142. {".ERS", EResType::ERS},
  143. {".VMAP", EResType::MAP},
  144. {".VCMP", EResType::CAMPAIGN},
  145. {".VERM", EResType::ERM},
  146. {".LUA", EResType::LUA}
  147. };
  148. auto iter = stringToRes.find(extension);
  149. if (iter == stringToRes.end())
  150. return EResType::OTHER;
  151. return iter->second;
  152. }
  153. std::string EResTypeHelper::getEResTypeAsString(EResType::Type type)
  154. {
  155. #define MAP_ENUM(value) {EResType::value, #value},
  156. static const std::map<EResType::Type, std::string> stringToRes =
  157. {
  158. MAP_ENUM(TEXT)
  159. MAP_ENUM(ANIMATION)
  160. MAP_ENUM(MASK)
  161. MAP_ENUM(CAMPAIGN)
  162. MAP_ENUM(MAP)
  163. MAP_ENUM(BMP_FONT)
  164. MAP_ENUM(TTF_FONT)
  165. MAP_ENUM(IMAGE)
  166. MAP_ENUM(VIDEO)
  167. MAP_ENUM(SOUND)
  168. MAP_ENUM(ARCHIVE_ZIP)
  169. MAP_ENUM(ARCHIVE_LOD)
  170. MAP_ENUM(ARCHIVE_SND)
  171. MAP_ENUM(ARCHIVE_VID)
  172. MAP_ENUM(PALETTE)
  173. MAP_ENUM(SAVEGAME)
  174. MAP_ENUM(DIRECTORY)
  175. MAP_ENUM(ERM)
  176. MAP_ENUM(ERT)
  177. MAP_ENUM(ERS)
  178. MAP_ENUM(OTHER)
  179. };
  180. #undef MAP_ENUM
  181. auto iter = stringToRes.find(type);
  182. assert(iter != stringToRes.end());
  183. return iter->second;
  184. }
  185. VCMI_LIB_NAMESPACE_END