CResourceLoader.h 11 KB

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