ResourceID.cpp 4.2 KB

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