ResourcePath.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * ResourcePath.h, 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. #pragma once
  11. VCMI_LIB_NAMESPACE_BEGIN
  12. class JsonNode;
  13. class JsonSerializeFormat;
  14. /**
  15. * Specifies the resource type.
  16. *
  17. * Supported file extensions:
  18. *
  19. * Text: .txt
  20. * Json: .json
  21. * Animation: .def
  22. * Mask: .msk .msg
  23. * Campaign: .h3c
  24. * Map: .h3m
  25. * Font: .fnt
  26. * Image: .bmp, .jpg, .pcx, .png, .tga
  27. * Sound: .wav .82m
  28. * Video: .smk, .bik .mjpg .mpg .webm
  29. * Music: .mp3, .ogg
  30. * Archive: .lod, .snd, .vid .pac .zip
  31. * Palette: .pal
  32. * Savegame: .v*gm1
  33. */
  34. enum class EResType
  35. {
  36. TEXT,
  37. JSON,
  38. ANIMATION,
  39. MASK,
  40. CAMPAIGN,
  41. MAP,
  42. BMP_FONT,
  43. TTF_FONT,
  44. IMAGE,
  45. VIDEO,
  46. VIDEO_LOW_QUALITY,
  47. SOUND,
  48. ARCHIVE_VID,
  49. ARCHIVE_ZIP,
  50. ARCHIVE_SND,
  51. ARCHIVE_LOD,
  52. PALETTE,
  53. SAVEGAME,
  54. DIRECTORY,
  55. ERM,
  56. ERT,
  57. ERS,
  58. LUA,
  59. OTHER,
  60. UNDEFINED,
  61. };
  62. /**
  63. * A struct which identifies a resource clearly.
  64. */
  65. class DLL_LINKAGE ResourcePath
  66. {
  67. protected:
  68. /// Constructs resource path based on JsonNode and selected type. File extension is ignored
  69. ResourcePath(const JsonNode & name, EResType type);
  70. public:
  71. /// Constructs resource path based on full name including extension
  72. explicit ResourcePath(const std::string & fullName);
  73. /// Constructs resource path based on filename and selected type. File extension is ignored
  74. ResourcePath(const std::string & name, EResType type);
  75. inline bool operator==(const ResourcePath & other) const
  76. {
  77. return name == other.name && type == other.type;
  78. }
  79. inline bool operator!=(const ResourcePath & other) const
  80. {
  81. return name != other.name || type != other.type;
  82. }
  83. inline bool operator<(const ResourcePath & other) const
  84. {
  85. if (type != other.type)
  86. return type < other.type;
  87. return name < other.name;
  88. }
  89. bool empty() const {return name.empty();}
  90. std::string getName() const {return name;}
  91. std::string getOriginalName() const {return originalName;}
  92. bool getOriginalResource() const {return originalResource;}
  93. EResType getType() const {return type;}
  94. void serializeJson(JsonSerializeFormat & handler);
  95. template <typename Handler> void serialize(Handler & h)
  96. {
  97. h & type;
  98. h & name;
  99. h & originalName;
  100. if (h.version >= Handler::Version::RESOURCE_GENERATION)
  101. h & originalResource;
  102. }
  103. protected:
  104. /// Specifies the resource type. EResType::OTHER if not initialized.
  105. /// Required to prevent conflicts if files with different types (e.g. text and image) have the same name.
  106. EResType type;
  107. /// Specifies the resource name. No extension so .pcx and .png can override each other, always in upper case.
  108. std::string name;
  109. /// name in original case
  110. std::string originalName;
  111. /// flag for requesting unmodded, original resource
  112. bool originalResource;
  113. };
  114. template<EResType Type>
  115. class ResourcePathTempl : public ResourcePath
  116. {
  117. template <EResType>
  118. friend class ResourcePathTempl;
  119. ResourcePathTempl(const ResourcePath & copy)
  120. :ResourcePath(copy)
  121. {
  122. type = Type;
  123. }
  124. ResourcePathTempl(const std::string & path)
  125. :ResourcePath(path, Type)
  126. {}
  127. ResourcePathTempl(const JsonNode & name)
  128. :ResourcePath(name, Type)
  129. {}
  130. public:
  131. ResourcePathTempl()
  132. :ResourcePath("", Type)
  133. {}
  134. static ResourcePathTempl fromResource(const ResourcePath & resource)
  135. {
  136. assert(Type == resource.getType());
  137. return ResourcePathTempl(resource);
  138. }
  139. static ResourcePathTempl builtin(const std::string & filename)
  140. {
  141. return ResourcePathTempl(filename);
  142. }
  143. static ResourcePathTempl builtinTODO(const std::string & filename)
  144. {
  145. return ResourcePathTempl(filename);
  146. }
  147. static ResourcePathTempl fromJson(const JsonNode & path)
  148. {
  149. return ResourcePathTempl(path);
  150. }
  151. template<EResType Type2>
  152. ResourcePathTempl<Type2> toType() const
  153. {
  154. ResourcePathTempl<Type2> result(static_cast<ResourcePath>(*this));
  155. return result;
  156. }
  157. ResourcePathTempl addPrefix(const std::string & prefix) const
  158. {
  159. ResourcePathTempl result;
  160. result.name = prefix + this->getName();
  161. result.originalName = prefix + this->getOriginalName();
  162. result.originalResource = this->getOriginalResource();
  163. return result;
  164. }
  165. ResourcePathTempl setOriginalResource(bool original) const
  166. {
  167. ResourcePathTempl result;
  168. result.name = this->getName();
  169. result.originalName = this->getOriginalName();
  170. result.originalResource = original;
  171. return result;
  172. }
  173. };
  174. using AnimationPath = ResourcePathTempl<EResType::ANIMATION>;
  175. using ImagePath = ResourcePathTempl<EResType::IMAGE>;
  176. using TextPath = ResourcePathTempl<EResType::TEXT>;
  177. using JsonPath = ResourcePathTempl<EResType::JSON>;
  178. using VideoPath = ResourcePathTempl<EResType::VIDEO>;
  179. using AudioPath = ResourcePathTempl<EResType::SOUND>;
  180. namespace EResTypeHelper
  181. {
  182. /**
  183. * Converts a extension string to a EResType enum object.
  184. *
  185. * @param extension The extension string e.g. .BMP, .PNG
  186. * @return Returns a EResType enum object
  187. */
  188. EResType getTypeFromExtension(std::string extension);
  189. /**
  190. * Gets the EResType as a string representation.
  191. *
  192. * @param type the EResType
  193. * @return the type as a string representation
  194. */
  195. std::string getEResTypeAsString(EResType type);
  196. };
  197. VCMI_LIB_NAMESPACE_END
  198. namespace std
  199. {
  200. template <> struct hash<VCMI_LIB_WRAP_NAMESPACE(ResourcePath)>
  201. {
  202. size_t operator()(const VCMI_LIB_WRAP_NAMESPACE(ResourcePath) & resourceIdent) const
  203. {
  204. std::hash<int> intHasher;
  205. std::hash<std::string> stringHasher;
  206. return stringHasher(resourceIdent.getName()) ^ intHasher(static_cast<int>(resourceIdent.getType()));
  207. }
  208. };
  209. }