CResourceLoader.h 11 KB

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