ResourcePath.h 5.0 KB

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