ResourcePath.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 .ogv .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. AI_MODEL,
  60. OTHER,
  61. UNDEFINED,
  62. };
  63. /**
  64. * A struct which identifies a resource clearly.
  65. */
  66. class DLL_LINKAGE ResourcePath
  67. {
  68. protected:
  69. /// Constructs resource path based on JsonNode and selected type. File extension is ignored
  70. ResourcePath(const JsonNode & name, EResType type);
  71. public:
  72. /// Constructs resource path based on full name including extension
  73. explicit ResourcePath(const std::string & fullName);
  74. /// Constructs resource path based on filename and selected type. File extension is ignored
  75. ResourcePath(const std::string & name, EResType type);
  76. inline bool operator==(const ResourcePath & other) const
  77. {
  78. return name == other.name && type == other.type;
  79. }
  80. inline bool operator!=(const ResourcePath & other) const
  81. {
  82. return name != other.name || type != other.type;
  83. }
  84. inline bool operator<(const ResourcePath & other) const
  85. {
  86. if (type != other.type)
  87. return type < other.type;
  88. return name < other.name;
  89. }
  90. bool empty() const {return name.empty();}
  91. std::string getName() const {return name;}
  92. std::string getOriginalName() const {return originalName;}
  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. }
  101. protected:
  102. /// Specifies the resource type. EResType::OTHER if not initialized.
  103. /// Required to prevent conflicts if files with different types (e.g. text and image) have the same name.
  104. EResType type;
  105. /// Specifies the resource name. No extension so .pcx and .png can override each other, always in upper case.
  106. std::string name;
  107. /// name in original case
  108. std::string originalName;
  109. };
  110. template<EResType Type>
  111. class ResourcePathTempl : public ResourcePath
  112. {
  113. template <EResType>
  114. friend class ResourcePathTempl;
  115. ResourcePathTempl(const ResourcePath & copy)
  116. :ResourcePath(copy)
  117. {
  118. type = Type;
  119. }
  120. ResourcePathTempl(const std::string & path)
  121. :ResourcePath(path, Type)
  122. {}
  123. ResourcePathTempl(const JsonNode & name)
  124. :ResourcePath(name, Type)
  125. {}
  126. public:
  127. ResourcePathTempl()
  128. :ResourcePath("", Type)
  129. {}
  130. static ResourcePathTempl fromResource(const ResourcePath & resource)
  131. {
  132. assert(Type == resource.getType());
  133. return ResourcePathTempl(resource);
  134. }
  135. static ResourcePathTempl builtin(const std::string & filename)
  136. {
  137. return ResourcePathTempl(filename);
  138. }
  139. static ResourcePathTempl builtinTODO(const std::string & filename)
  140. {
  141. return ResourcePathTempl(filename);
  142. }
  143. static ResourcePathTempl fromJson(const JsonNode & path)
  144. {
  145. return ResourcePathTempl(path);
  146. }
  147. template<EResType Type2>
  148. ResourcePathTempl<Type2> toType() const
  149. {
  150. ResourcePathTempl<Type2> result(static_cast<ResourcePath>(*this));
  151. return result;
  152. }
  153. ResourcePathTempl addPrefix(const std::string & prefix) const
  154. {
  155. ResourcePathTempl result;
  156. result.name = prefix + this->getName();
  157. result.originalName = prefix + this->getOriginalName();
  158. return result;
  159. }
  160. };
  161. using AnimationPath = ResourcePathTempl<EResType::ANIMATION>;
  162. using ImagePath = ResourcePathTempl<EResType::IMAGE>;
  163. using TextPath = ResourcePathTempl<EResType::TEXT>;
  164. using JsonPath = ResourcePathTempl<EResType::JSON>;
  165. using VideoPath = ResourcePathTempl<EResType::VIDEO>;
  166. using AudioPath = ResourcePathTempl<EResType::SOUND>;
  167. namespace EResTypeHelper
  168. {
  169. /**
  170. * Converts a extension string to a EResType enum object.
  171. *
  172. * @param extension The extension string e.g. .BMP, .PNG
  173. * @return Returns a EResType enum object
  174. */
  175. EResType getTypeFromExtension(std::string extension);
  176. /**
  177. * Gets the EResType as a string representation.
  178. *
  179. * @param type the EResType
  180. * @return the type as a string representation
  181. */
  182. std::string getEResTypeAsString(EResType type);
  183. };
  184. VCMI_LIB_NAMESPACE_END
  185. namespace std
  186. {
  187. template <> struct hash<VCMI_LIB_WRAP_NAMESPACE(ResourcePath)>
  188. {
  189. size_t operator()(const VCMI_LIB_WRAP_NAMESPACE(ResourcePath) & resourceIdent) const
  190. {
  191. std::hash<int> intHasher;
  192. std::hash<std::string> stringHasher;
  193. return stringHasher(resourceIdent.getName()) ^ intHasher(static_cast<int>(resourceIdent.getType()));
  194. }
  195. };
  196. }