ResourceID.cpp 3.9 KB

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