ResourcePath.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. 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. EResType getType() const {return type;}
  93. void serializeJson(JsonSerializeFormat & handler);
  94. template <typename Handler> void serialize(Handler & h)
  95. {
  96. h & type;
  97. h & name;
  98. h & originalName;
  99. }
  100. protected:
  101. /// Specifies the resource type. EResType::OTHER if not initialized.
  102. /// Required to prevent conflicts if files with different types (e.g. text and image) have the same name.
  103. EResType type;
  104. /// Specifies the resource name. No extension so .pcx and .png can override each other, always in upper case.
  105. std::string name;
  106. /// name in original case
  107. std::string originalName;
  108. };
  109. template<EResType Type>
  110. class ResourcePathTempl : public ResourcePath
  111. {
  112. template <EResType>
  113. friend class ResourcePathTempl;
  114. ResourcePathTempl(const ResourcePath & copy)
  115. :ResourcePath(copy)
  116. {
  117. type = Type;
  118. }
  119. ResourcePathTempl(const std::string & path)
  120. :ResourcePath(path, Type)
  121. {}
  122. ResourcePathTempl(const JsonNode & name)
  123. :ResourcePath(name, Type)
  124. {}
  125. public:
  126. ResourcePathTempl()
  127. :ResourcePath("", Type)
  128. {}
  129. static ResourcePathTempl fromResource(const ResourcePath & resource)
  130. {
  131. assert(Type == resource.getType());
  132. return ResourcePathTempl(resource);
  133. }
  134. static ResourcePathTempl builtin(const std::string & filename)
  135. {
  136. return ResourcePathTempl(filename);
  137. }
  138. static ResourcePathTempl builtinTODO(const std::string & filename)
  139. {
  140. return ResourcePathTempl(filename);
  141. }
  142. static ResourcePathTempl fromJson(const JsonNode & path)
  143. {
  144. return ResourcePathTempl(path);
  145. }
  146. template<EResType Type2>
  147. ResourcePathTempl<Type2> toType() const
  148. {
  149. ResourcePathTempl<Type2> result(static_cast<ResourcePath>(*this));
  150. return result;
  151. }
  152. ResourcePathTempl addPrefix(const std::string & prefix) const
  153. {
  154. ResourcePathTempl result;
  155. result.name = prefix + this->getName();
  156. result.originalName = prefix + this->getOriginalName();
  157. return result;
  158. }
  159. };
  160. using AnimationPath = ResourcePathTempl<EResType::ANIMATION>;
  161. using ImagePath = ResourcePathTempl<EResType::IMAGE>;
  162. using TextPath = ResourcePathTempl<EResType::TEXT>;
  163. using JsonPath = ResourcePathTempl<EResType::JSON>;
  164. using VideoPath = ResourcePathTempl<EResType::VIDEO>;
  165. using AudioPath = ResourcePathTempl<EResType::SOUND>;
  166. namespace EResTypeHelper
  167. {
  168. /**
  169. * Converts a extension string to a EResType enum object.
  170. *
  171. * @param extension The extension string e.g. .BMP, .PNG
  172. * @return Returns a EResType enum object
  173. */
  174. EResType getTypeFromExtension(std::string extension);
  175. /**
  176. * Gets the EResType as a string representation.
  177. *
  178. * @param type the EResType
  179. * @return the type as a string representation
  180. */
  181. std::string getEResTypeAsString(EResType type);
  182. };
  183. VCMI_LIB_NAMESPACE_END
  184. namespace std
  185. {
  186. template <> struct hash<VCMI_LIB_WRAP_NAMESPACE(ResourcePath)>
  187. {
  188. size_t operator()(const VCMI_LIB_WRAP_NAMESPACE(ResourcePath) & resourceIdent) const
  189. {
  190. std::hash<int> intHasher;
  191. std::hash<std::string> stringHasher;
  192. return stringHasher(resourceIdent.getName()) ^ intHasher(static_cast<int>(resourceIdent.getType()));
  193. }
  194. };
  195. }