2
0

ResourcePath.h 4.9 KB

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