CResourceLoader.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * CResourceLoader.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. #include "CInputStream.h"
  12. class ResourceLocator;
  13. class ISimpleResourceLoader;
  14. /**
  15. * Specifies the resource type.
  16. *
  17. * Supported file extensions:
  18. *
  19. * Text: .json
  20. * Animation: .def
  21. * Mask: .msk
  22. * Campaign: .h3c
  23. * Map: .h3m
  24. * Font: .fnt
  25. * Image: .bmp, .jpg, .pcx, .png, .tga
  26. * Sound: .wav
  27. * Video: .smk, .bik
  28. * Music: .mp3, .ogg
  29. * Archive: .lod, .snd, .vid, .zip, .tar.gz
  30. * Savegame: .vlgm1
  31. */
  32. enum class EResType
  33. {
  34. ANY,
  35. TEXT,
  36. ANIMATION,
  37. MASK,
  38. CAMPAIGN,
  39. MAP,
  40. FONT,
  41. IMAGE,
  42. VIDEO,
  43. SOUND,
  44. MUSIC,
  45. ARCHIVE,
  46. SAVEGAME,
  47. OTHER
  48. };
  49. /**
  50. * A struct which identifies a resource clearly.
  51. */
  52. class DLL_LINKAGE ResourceIdentifier
  53. {
  54. public:
  55. /**
  56. * Default c-tor.
  57. */
  58. ResourceIdentifier();
  59. /**
  60. * Ctor.
  61. *
  62. * @param name The resource name.
  63. * @param type The resource type. A constant from the enumeration EResType.
  64. */
  65. ResourceIdentifier(const std::string & name, EResType type);
  66. /**
  67. * Compares this object with a another resource identifier.
  68. *
  69. * @param other The other resource identifier.
  70. * @return Returns true if both are equally, false if not.
  71. */
  72. inline bool operator==(ResourceIdentifier const & other) const
  73. {
  74. return name == other.name && type == other.type;
  75. }
  76. /**
  77. * Gets the name of the identifier.
  78. *
  79. * @return the name of the identifier
  80. */
  81. std::string getName() const;
  82. /**
  83. * Gets the type of the identifier.
  84. *
  85. * @return the type of the identifier
  86. */
  87. EResType getType() const;
  88. /**
  89. * Sets the name of the identifier.
  90. *
  91. * @param name the name of the identifier. No extension, will be converted to uppercase.
  92. */
  93. void setName(const std::string & name);
  94. /**
  95. * Sets the type of the identifier.
  96. *
  97. * @param type the type of the identifier.
  98. */
  99. void setType(EResType type);
  100. private:
  101. /** Specifies the resource name. No extension so .pcx and .png can override each other, always in upper case. **/
  102. std::string name;
  103. /**
  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. */
  107. EResType type;
  108. };
  109. namespace std
  110. {
  111. /**
  112. * Template specialization for std::hash.
  113. */
  114. template <>
  115. class hash<ResourceIdentifier>
  116. {
  117. public:
  118. /**
  119. * Generates a hash value for the resource identifier object.
  120. *
  121. * @param resourceIdent The object from which a hash value should be generated.
  122. * @return the generated hash value
  123. */
  124. size_t operator()(const ResourceIdentifier & resourceIdent) const
  125. {
  126. return hash<string>()(resourceIdent.getName()) ^ hash<int>()(static_cast<int>(resourceIdent.getType()));
  127. }
  128. };
  129. };
  130. /**
  131. * This class manages the loading of resources whether standard
  132. * or derived from several container formats and the file system.
  133. */
  134. class DLL_LINKAGE CResourceLoader : public boost::noncopyable
  135. {
  136. public:
  137. CResourceLoader();
  138. /**
  139. * D-tor.
  140. */
  141. virtual ~CResourceLoader();
  142. /**
  143. * Loads the resource specified by the resource identifier.
  144. *
  145. * @param resourceIdent This parameter identifies the resource to load.
  146. * @return a pointer to the input stream, not null
  147. *
  148. * @throws std::runtime_error if the resource doesn't exists
  149. */
  150. std::unique_ptr<CInputStream> load(const ResourceIdentifier & resourceIdent) const;
  151. /**
  152. * Tests whether the specified resource exists.
  153. *
  154. * @param resourceIdent the resource which should be checked
  155. * @return true if the resource exists, false if not
  156. */
  157. bool existsResource(const ResourceIdentifier & resourceIdent) const;
  158. /**
  159. * Adds a simple resource loader to the loaders list and its entries to the resources list.
  160. *
  161. * The loader object will be destructed when this resource loader is destructed.
  162. * Don't delete it manually.
  163. *
  164. * @param loader The simple resource loader object to add
  165. */
  166. void addLoader(ISimpleResourceLoader * loader);
  167. private:
  168. /**
  169. * Contains lists of same resources which can be accessed uniquely by an
  170. * resource identifier.
  171. */
  172. std::unordered_map<ResourceIdentifier, std::list<ResourceLocator> > resources;
  173. /** A list of resource loader objects */
  174. std::list<ISimpleResourceLoader *> loaders;
  175. };
  176. /**
  177. * This class has static methods for a global resource loader access.
  178. *
  179. * Note: Compared to the singleton pattern it has the advantage that the class CResourceLoader
  180. * and classes which use it can be tested separately. CResourceLoader can be sub-classes as well.
  181. * Compared to a global variable the factory pattern can throw an exception if the resource loader wasn't
  182. * initialized.
  183. *
  184. * This class is not thread-safe. Make sure nobody is calling getInstance while somebody else is calling setInstance.
  185. */
  186. class DLL_LINKAGE CResourceLoaderFactory
  187. {
  188. public:
  189. /**
  190. * Gets an instance of resource loader.
  191. *
  192. * Make sure that you've set an instance before using it. It'll throw an exception if no instance was set.
  193. *
  194. * @return Returns an instance of resource loader.
  195. */
  196. static CResourceLoader * getInstance();
  197. /**
  198. * Sets an instance of resource loader.
  199. *
  200. * @param resourceLoader An instance of resource loader.
  201. */
  202. static void setInstance(CResourceLoader * resourceLoader);
  203. private:
  204. /** Instance of resource loader */
  205. static CResourceLoader * resourceLoader;
  206. };
  207. /**
  208. * A struct which describes the exact position of a resource.
  209. */
  210. class DLL_LINKAGE ResourceLocator
  211. {
  212. public:
  213. /**
  214. * Ctor.
  215. *
  216. * @param archive A pointer to the resource archive object.
  217. * @param resourceName Unique resource name in the space of the given resource archive.
  218. */
  219. ResourceLocator(ISimpleResourceLoader * loader, const std::string & resourceName);
  220. /**
  221. * Gets a pointer to the resource loader object.
  222. *
  223. * @return a pointer to the resource loader object
  224. */
  225. ISimpleResourceLoader * getLoader() const;
  226. /**
  227. * Gets the resource name.
  228. *
  229. * @return the resource name.
  230. */
  231. std::string getResourceName() const;
  232. private:
  233. /**
  234. * A pointer to the loader which knows where and how to construct a stream object
  235. * which does the loading process actually.
  236. */
  237. ISimpleResourceLoader * loader;
  238. /** A unique name of the resource in space of the loader. */
  239. std::string resourceName;
  240. };
  241. /**
  242. * A helper class which provides a functionality to convert extension strings to EResTypes.
  243. */
  244. class DLL_LINKAGE EResTypeHelper
  245. {
  246. public:
  247. /**
  248. * Converts a extension string to a EResType enum object.
  249. *
  250. * @param extension The extension string e.g. .BMP, .PNG
  251. * @return Returns a EResType enum object
  252. */
  253. static EResType getTypeFromExtension(std::string extension);
  254. /**
  255. * Gets the EResType as a string representation.
  256. *
  257. * @param type the EResType
  258. * @return the type as a string representation
  259. */
  260. static std::string getEResTypeAsString(EResType type);
  261. };