ResourcePath.cpp 4.3 KB

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