ResourceID.cpp 4.4 KB

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