2
0

ResourcePath.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * ResourcePath.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 "ResourcePath.h"
  12. #include "FileInfo.h"
  13. #include "../serializer/JsonDeserializer.h"
  14. #include "../serializer/JsonSerializer.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. static inline void toUpper(std::string & string)
  17. {
  18. boost::to_upper(string);
  19. }
  20. static inline EResType readType(const std::string& name)
  21. {
  22. return EResTypeHelper::getTypeFromExtension(FileInfo::GetExtension(name).to_string());
  23. }
  24. static inline std::string readName(std::string name, bool uppercase)
  25. {
  26. const auto dotPos = name.find_last_of('.');
  27. //do not cut "extension" of directory name
  28. auto delimPos = name.find_last_of('/');
  29. if(delimPos == std::string::npos)
  30. delimPos = name.find_last_of('\\');
  31. if((delimPos == std::string::npos || delimPos < dotPos) && dotPos != std::string::npos)
  32. {
  33. auto type = EResTypeHelper::getTypeFromExtension(name.substr(dotPos));
  34. if(type != EResType::OTHER)
  35. name.resize(dotPos);
  36. }
  37. if(uppercase)
  38. toUpper(name);
  39. return name;
  40. }
  41. ResourcePath::ResourcePath(const std::string & name_):
  42. type(readType(name_)),
  43. name(readName(name_, true)),
  44. originalName(readName(name_, false))
  45. {}
  46. ResourcePath::ResourcePath(const std::string & name_, EResType type_):
  47. type(type_),
  48. name(readName(name_, true)),
  49. originalName(readName(name_, false))
  50. {}
  51. ResourcePath::ResourcePath(const JsonNode & name, EResType type):
  52. type(type),
  53. name(readName(name.String(), true)),
  54. originalName(readName(name.String(), false))
  55. {
  56. }
  57. void ResourcePath::serializeJson(JsonSerializeFormat & handler)
  58. {
  59. if (!handler.saving)
  60. {
  61. JsonNode const & node = handler.getCurrent();
  62. if (node.isString())
  63. {
  64. name = readName(node.String(), true);
  65. originalName = readName(node.String(), false);
  66. return;
  67. }
  68. }
  69. handler.serializeInt("type", type);
  70. handler.serializeString("name", name);
  71. handler.serializeString("originalName", originalName);
  72. }
  73. EResType EResTypeHelper::getTypeFromExtension(std::string extension)
  74. {
  75. toUpper(extension);
  76. static const std::map<std::string, EResType> stringToRes =
  77. {
  78. {".TXT", EResType::TEXT},
  79. {".JSON", EResType::JSON},
  80. {".DEF", EResType::ANIMATION},
  81. {".MSK", EResType::MASK},
  82. {".MSG", EResType::MASK},
  83. {".H3C", EResType::CAMPAIGN},
  84. {".H3M", EResType::MAP},
  85. {".TUT", EResType::MAP},
  86. {".FNT", EResType::BMP_FONT},
  87. {".TTF", EResType::TTF_FONT},
  88. {".BMP", EResType::IMAGE},
  89. {".GIF", EResType::IMAGE},
  90. {".JPG", EResType::IMAGE},
  91. {".PCX", EResType::IMAGE},
  92. {".PNG", EResType::IMAGE},
  93. {".TGA", EResType::IMAGE},
  94. {".WAV", EResType::SOUND},
  95. {".82M", EResType::SOUND},
  96. {".MP3", EResType::SOUND},
  97. {".OGG", EResType::SOUND},
  98. {".FLAC", EResType::SOUND},
  99. {".SMK", EResType::VIDEO_LOW_QUALITY},
  100. {".BIK", EResType::VIDEO},
  101. {".OGV", EResType::VIDEO},
  102. {".WEBM", EResType::VIDEO},
  103. {".ZIP", EResType::ARCHIVE_ZIP},
  104. {".LOD", EResType::ARCHIVE_LOD},
  105. {".PAC", EResType::ARCHIVE_LOD},
  106. {".VID", EResType::ARCHIVE_VID},
  107. {".SND", EResType::ARCHIVE_SND},
  108. {".PAL", EResType::PALETTE},
  109. {".VSGM1", EResType::SAVEGAME},
  110. {".ERM", EResType::ERM},
  111. {".ERT", EResType::ERT},
  112. {".ERS", EResType::ERS},
  113. {".VMAP", EResType::MAP},
  114. {".VCMP", EResType::CAMPAIGN},
  115. {".VERM", EResType::ERM},
  116. {".LUA", EResType::LUA}
  117. };
  118. auto iter = stringToRes.find(extension);
  119. if (iter == stringToRes.end())
  120. return EResType::OTHER;
  121. return iter->second;
  122. }
  123. std::string EResTypeHelper::getEResTypeAsString(EResType type)
  124. {
  125. #define MAP_ENUM(value) {EResType::value, #value},
  126. static const std::map<EResType, std::string> stringToRes =
  127. {
  128. MAP_ENUM(TEXT)
  129. MAP_ENUM(JSON)
  130. MAP_ENUM(ANIMATION)
  131. MAP_ENUM(MASK)
  132. MAP_ENUM(CAMPAIGN)
  133. MAP_ENUM(MAP)
  134. MAP_ENUM(BMP_FONT)
  135. MAP_ENUM(TTF_FONT)
  136. MAP_ENUM(IMAGE)
  137. MAP_ENUM(VIDEO)
  138. MAP_ENUM(VIDEO_LOW_QUALITY)
  139. MAP_ENUM(SOUND)
  140. MAP_ENUM(ARCHIVE_ZIP)
  141. MAP_ENUM(ARCHIVE_LOD)
  142. MAP_ENUM(ARCHIVE_SND)
  143. MAP_ENUM(ARCHIVE_VID)
  144. MAP_ENUM(PALETTE)
  145. MAP_ENUM(SAVEGAME)
  146. MAP_ENUM(DIRECTORY)
  147. MAP_ENUM(ERM)
  148. MAP_ENUM(ERT)
  149. MAP_ENUM(ERS)
  150. MAP_ENUM(OTHER)
  151. };
  152. #undef MAP_ENUM
  153. auto iter = stringToRes.find(type);
  154. assert(iter != stringToRes.end());
  155. return iter->second;
  156. }
  157. VCMI_LIB_NAMESPACE_END