CResourceLoader.h 11 KB

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