2
0

ResourceID.cpp 3.8 KB

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