CResourceLoader.h 11 KB

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