CResourceLoader.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 CResourceLoader;
  13. class ResourceLocator;
  14. class ISimpleResourceLoader;
  15. /**
  16. * Specifies the resource type.
  17. *
  18. * Supported file extensions:
  19. *
  20. * Text: .txt .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
  29. * Music: .mp3, .ogg
  30. * Archive: .lod, .snd, .vid .pac
  31. * Palette: .pal
  32. * Savegame: .v*gm1
  33. */
  34. namespace EResType
  35. {
  36. enum Type
  37. {
  38. TEXT,
  39. ANIMATION,
  40. MASK,
  41. CAMPAIGN,
  42. MAP,
  43. FONT,
  44. IMAGE,
  45. VIDEO,
  46. SOUND,
  47. MUSIC,
  48. ARCHIVE,
  49. PALETTE,
  50. CLIENT_SAVEGAME,
  51. LIB_SAVEGAME,
  52. SERVER_SAVEGAME,
  53. DIRECTORY,
  54. OTHER
  55. };
  56. }
  57. /**
  58. * A struct which identifies a resource clearly.
  59. */
  60. class DLL_LINKAGE ResourceID
  61. {
  62. public:
  63. /**
  64. * Default c-tor.
  65. */
  66. ResourceID();
  67. /**
  68. * Move Ctor.
  69. */
  70. ResourceID(ResourceID && other)
  71. : name(std::move(other.name)), type(other.getType())
  72. {
  73. }
  74. /**
  75. * Ctor. Can be used to create indentifier for resource loading using one parameter
  76. *
  77. * @param name The resource name including extension.
  78. */
  79. explicit ResourceID(std::string fullName);
  80. /**
  81. * Ctor.
  82. *
  83. * @param name The resource name.
  84. * @param type The resource type. A constant from the enumeration EResType.
  85. */
  86. ResourceID(std::string name, EResType::Type type);
  87. /**
  88. * Compares this object with a another resource identifier.
  89. *
  90. * @param other The other resource identifier.
  91. * @return Returns true if both are equally, false if not.
  92. */
  93. inline bool operator==(ResourceID const & other) const
  94. {
  95. return name == other.name && type == other.type;
  96. }
  97. /*
  98. * Move-assignment operator.
  99. */
  100. inline ResourceID& operator=(ResourceID && other)
  101. {
  102. name = std::move(other.name);
  103. type = other.getType();
  104. return *this;
  105. }
  106. /**
  107. * Gets the name of the identifier.
  108. *
  109. * @return the name of the identifier
  110. */
  111. std::string getName() const;
  112. /**
  113. * Gets the type of the identifier.
  114. *
  115. * @return the type of the identifier
  116. */
  117. EResType::Type getType() const;
  118. /**
  119. * Sets the name of the identifier.
  120. *
  121. * @param name the name of the identifier. No extension, will be converted to uppercase.
  122. */
  123. void setName(std::string name);
  124. /**
  125. * Sets the type of the identifier.
  126. *
  127. * @param type the type of the identifier.
  128. */
  129. void setType(EResType::Type type);
  130. protected:
  131. /**
  132. * Ctor for usage strictly in resourceLoader for some speedup
  133. *
  134. * @param prefix Prefix of ths filename, already in upper case
  135. * @param name The resource name, upper case
  136. * @param type The resource type. A constant from the enumeration EResType.
  137. */
  138. ResourceID(const std::string & prefix, const std::string & name, EResType::Type type);
  139. friend class CResourceLoader;
  140. private:
  141. /** Specifies the resource name. No extension so .pcx and .png can override each other, always in upper case. **/
  142. std::string name;
  143. /**
  144. * Specifies the resource type. EResType::OTHER if not initialized.
  145. * Required to prevent conflicts if files with different types (e.g. text and image) have the same name.
  146. */
  147. EResType::Type type;
  148. };
  149. /**
  150. * Generates a hash value for the resource identifier object.
  151. *
  152. * @param resourceIdent The object from which a hash value should be generated.
  153. * @return the generated hash value
  154. */
  155. inline size_t hash_value(const ResourceID & resourceIdent)
  156. {
  157. boost::hash<int> intHasher;
  158. boost::hash<std::string> stringHasher;
  159. return stringHasher(resourceIdent.getName()) ^ intHasher(static_cast<int>(resourceIdent.getType()));
  160. }
  161. // namespace std
  162. // {
  163. // /**
  164. // * Template specialization for std::hash.
  165. // */
  166. // template <>
  167. // class hash<ResourceID>
  168. // {
  169. // public:
  170. // /**
  171. // * Generates a hash value for the resource identifier object.
  172. // *
  173. // * @param resourceIdent The object from which a hash value should be generated.
  174. // * @return the generated hash value
  175. // */
  176. // size_t operator()(const ResourceID & resourceIdent) const
  177. // {
  178. // return hash<string>()(resourceIdent.getName()) ^ hash<int>()(static_cast<int>(resourceIdent.getType()));
  179. // }
  180. // };
  181. // };
  182. /**
  183. * This class manages the loading of resources whether standard
  184. * or derived from several container formats and the file system.
  185. */
  186. class DLL_LINKAGE CResourceLoader
  187. {
  188. typedef boost::unordered_map<ResourceID, std::vector<ResourceLocator> > ResourcesMap;
  189. public:
  190. /// class for iterating over all available files/Identifiers
  191. /// can be created via CResourceLoader::getIterator
  192. template <typename Comparator, typename Iter>
  193. class Iterator
  194. {
  195. public:
  196. /// find next available item.
  197. Iterator& operator++()
  198. {
  199. assert(begin != end);
  200. begin++;
  201. findNext();
  202. return *this;
  203. }
  204. bool hasNext()
  205. {
  206. return begin != end;
  207. }
  208. /// get identifier of current item
  209. const ResourceID & operator* () const
  210. {
  211. assert(begin != end);
  212. return begin->first;
  213. }
  214. /// get identifier of current item
  215. const ResourceID * operator -> () const
  216. {
  217. assert(begin != end);
  218. return &begin->first;
  219. }
  220. protected:
  221. Iterator(Iter begin, Iter end, Comparator comparator):
  222. begin(begin),
  223. end(end),
  224. comparator(comparator)
  225. {
  226. //find first applicable item
  227. findNext();
  228. }
  229. friend class CResourceLoader;
  230. private:
  231. Iter begin;
  232. Iter end;
  233. Comparator comparator;
  234. void findNext()
  235. {
  236. while (begin != end && !comparator(begin->first))
  237. begin++;
  238. }
  239. };
  240. CResourceLoader();
  241. /**
  242. * Loads the resource specified by the resource identifier.
  243. *
  244. * @param resourceIdent This parameter identifies the resource to load.
  245. * @return a pointer to the input stream, not null
  246. *
  247. * @throws std::runtime_error if the resource doesn't exists
  248. */
  249. std::unique_ptr<CInputStream> load(const ResourceID & resourceIdent) const;
  250. /// temporary member to ease transition to new filesystem classes
  251. std::pair<std::unique_ptr<ui8[]>, ui64> loadData(const ResourceID & resourceIdent) const;
  252. /**
  253. * Get resource locator for this identifier
  254. *
  255. * @param resourceIdent This parameter identifies the resource to load.
  256. * @return resource locator for this resource or empty one if resource was not found
  257. */
  258. ResourceLocator getResource(const ResourceID & resourceIdent) const;
  259. /// returns ALL overriden resources with same name, including last one acessible via getResource
  260. const std::vector<ResourceLocator> & getResourcesWithName(const ResourceID & resourceIdent) const;
  261. /// returns real name of file in filesystem. Not usable for archives
  262. std::string getResourceName(const ResourceID & resourceIdent) const;
  263. /**
  264. * Get iterator for looping all files matching filter
  265. * Notes:
  266. * - iterating over all files may be slow. Use with caution
  267. * - all filenames are in upper case
  268. *
  269. * @param filter functor with signature bool(ResourceIdentifier) used to check if this file is required
  270. * @return resource locator for this resource or empty one if resource was not found
  271. */
  272. template<typename Comparator>
  273. Iterator<Comparator, ResourcesMap::const_iterator> getIterator(Comparator filter) const
  274. {
  275. return Iterator<Comparator, ResourcesMap::const_iterator>(resources.begin(), resources.end(), filter);
  276. }
  277. /**
  278. * Tests whether the specified resource exists.
  279. *
  280. * @param resourceIdent the resource which should be checked
  281. * @return true if the resource exists, false if not
  282. */
  283. bool existsResource(const ResourceID & resourceIdent) const;
  284. /**
  285. * Creates new resource (if not exists) with specified URI.
  286. * Type will be determined from extension
  287. * File case will be same as in URI
  288. *
  289. * @param URI file to create
  290. * @return true on success, false if resource exists or on error
  291. */
  292. bool createResource(std::string URI);
  293. /**
  294. * Adds a simple resource loader to the loaders list and its entries to the resources list.
  295. *
  296. * The loader object will be destructed when this resource loader is destructed.
  297. * Don't delete it manually.
  298. * Same loader can be added multiple times (with different mount point)
  299. *
  300. * @param mountPoint prefix that will be added to all files in this loader
  301. * @param loader The simple resource loader object to add
  302. */
  303. void addLoader(std::string mountPoint, shared_ptr<ISimpleResourceLoader> loader, bool writeable);
  304. public:
  305. /**
  306. * Contains lists of same resources which can be accessed uniquely by an
  307. * resource identifier.
  308. */
  309. ResourcesMap resources;
  310. struct LoaderEntry
  311. {
  312. std::string prefix;
  313. shared_ptr<ISimpleResourceLoader> loader;
  314. bool writeable;
  315. };
  316. /** A list of resource loader objects */
  317. std::vector<LoaderEntry > loaders;
  318. };
  319. /**
  320. * This class has static methods for a global resource loader access.
  321. *
  322. * Class is not thread-safe. Make sure nobody is calling getInstance while somebody else is calling initialize.
  323. */
  324. class DLL_LINKAGE CResourceHandler
  325. {
  326. public:
  327. /**
  328. * Gets an instance of resource loader.
  329. *
  330. * Make sure that you've set an instance before using it. It'll throw an exception if no instance was set.
  331. *
  332. * @return Returns an instance of resource loader.
  333. */
  334. static CResourceLoader * get();
  335. /**
  336. * Creates instance of resource loader.
  337. * Will not fill filesystem with data
  338. *
  339. */
  340. static void initialize();
  341. /**
  342. * Will load all filesystem data from Json data at this path (config/filesystem.json)
  343. */
  344. static void loadFileSystem(const std::string fsConfigURI);
  345. /**
  346. * Experimental. Checks all subfolders of MODS directory for presence of ERA-style mods
  347. * If this directory has filesystem.json file it will be added to resources
  348. */
  349. static void loadModsFilesystems();
  350. private:
  351. /** Instance of resource loader */
  352. static CResourceLoader * resourceLoader;
  353. static CResourceLoader * initialLoader;
  354. };
  355. /**
  356. * A struct which describes the exact position of a resource.
  357. */
  358. class DLL_LINKAGE ResourceLocator
  359. {
  360. public:
  361. /**
  362. * Ctor.
  363. *
  364. * @param archive A pointer to the resource archive object.
  365. * @param resourceName Unique resource name in the space of the given resource archive.
  366. */
  367. ResourceLocator(ISimpleResourceLoader * loader, const std::string & resourceName);
  368. /**
  369. * Gets a pointer to the resource loader object.
  370. *
  371. * @return a pointer to the resource loader object
  372. */
  373. ISimpleResourceLoader * getLoader() const;
  374. /**
  375. * Gets the resource name.
  376. *
  377. * @return the resource name.
  378. */
  379. std::string getResourceName() const;
  380. private:
  381. /**
  382. * A pointer to the loader which knows where and how to construct a stream object
  383. * which does the loading process actually.
  384. */
  385. ISimpleResourceLoader * loader;
  386. /** A unique name of the resource in space of the loader. */
  387. std::string resourceName;
  388. };
  389. /**
  390. * A helper class which provides a functionality to convert extension strings to EResTypes.
  391. */
  392. class DLL_LINKAGE EResTypeHelper
  393. {
  394. public:
  395. /**
  396. * Converts a extension string to a EResType enum object.
  397. *
  398. * @param extension The extension string e.g. .BMP, .PNG
  399. * @return Returns a EResType enum object
  400. */
  401. static EResType::Type getTypeFromExtension(std::string extension);
  402. /**
  403. * Gets the EResType as a string representation.
  404. *
  405. * @param type the EResType
  406. * @return the type as a string representation
  407. */
  408. static std::string getEResTypeAsString(EResType::Type type);
  409. };