Filesystem.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #pragma once
  2. /*
  3. * Filesystem.h, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  11. #include "CInputStream.h"
  12. #include "ISimpleResourceLoader.h"
  13. class CFilesystemList;
  14. class JsonNode;
  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 .mpg
  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. BMP_FONT,
  44. TTF_FONT,
  45. IMAGE,
  46. VIDEO,
  47. SOUND,
  48. MUSIC,
  49. ARCHIVE_VID,
  50. ARCHIVE_ZIP,
  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. * Ctor. Can be used to create indentifier for resource loading using one parameter
  75. *
  76. * @param name The resource name including extension.
  77. */
  78. explicit ResourceID(std::string fullName);
  79. /**
  80. * Ctor.
  81. *
  82. * @param name The resource name.
  83. * @param type The resource type. A constant from the enumeration EResType.
  84. */
  85. ResourceID(std::string name, EResType::Type type);
  86. /**
  87. * Compares this object with a another resource identifier.
  88. *
  89. * @param other The other resource identifier.
  90. * @return Returns true if both are equally, false if not.
  91. */
  92. inline bool operator==(ResourceID const & other) const
  93. {
  94. return name == other.name && type == other.type;
  95. }
  96. std::string getName() const;
  97. EResType::Type getType() const;
  98. void setName(std::string name);
  99. void setType(EResType::Type type);
  100. private:
  101. /** Specifies the resource name. No extension so .pcx and .png can override each other, always in upper case. **/
  102. std::string name;
  103. /**
  104. * Specifies the resource type. EResType::OTHER if not initialized.
  105. * Required to prevent conflicts if files with different types (e.g. text and image) have the same name.
  106. */
  107. EResType::Type type;
  108. };
  109. namespace std
  110. {
  111. template <> struct hash<ResourceID>
  112. {
  113. size_t operator()(const ResourceID & resourceIdent) const
  114. {
  115. std::hash<int> intHasher;
  116. std::hash<std::string> stringHasher;
  117. return stringHasher(resourceIdent.getName()) ^ intHasher(static_cast<int>(resourceIdent.getType()));
  118. }
  119. };
  120. }
  121. /**
  122. * This class has static methods for a global resource loader access.
  123. *
  124. * Class is not thread-safe. Make sure nobody is calling getInstance while somebody else is calling initialize.
  125. */
  126. class DLL_LINKAGE CResourceHandler
  127. {
  128. public:
  129. /**
  130. * Gets an instance of resource loader.
  131. *
  132. * Make sure that you've set an instance before using it. It'll throw an exception if no instance was set.
  133. *
  134. * @return Returns an instance of resource loader.
  135. */
  136. static ISimpleResourceLoader * get();
  137. /**
  138. * Creates instance of resource loader.
  139. * Will not fill filesystem with data
  140. *
  141. */
  142. static void initialize();
  143. /**
  144. * Semi-debug method to track all possible cases of memory leaks
  145. * Used before exiting application
  146. *
  147. */
  148. static void clear();
  149. /**
  150. * Will load all filesystem data from Json data at this path (config/filesystem.json)
  151. * @param prefix - prefix for all paths in filesystem config
  152. */
  153. static void loadMainFileSystem(const std::string & fsConfigURI);
  154. static void loadModFileSystem(const std::string &prefix, const JsonNode & fsConfig);
  155. static void loadDirectory(const std::string &prefix, const std::string & mountPoint, const JsonNode & config);
  156. static void loadArchive(const std::string &prefix, const std::string & mountPoint, const JsonNode & config, EResType::Type archiveType);
  157. static void loadJsonMap(const std::string &prefix, const std::string & mountPoint, const JsonNode & config);
  158. /**
  159. * Checks all subfolders of MODS directory for presence of mods
  160. * If this directory has mod.json file it will be added to resources
  161. */
  162. static std::vector<std::string> getAvailableMods();
  163. static void setActiveMods(std::vector<std::string> enabledMods); //WARNING: not reentrable. Do not call it twice!!!
  164. private:
  165. /** Instance of resource loader */
  166. static CFilesystemList * resourceLoader;
  167. static CFilesystemList * initialLoader;
  168. };
  169. /**
  170. * A helper class which provides a functionality to convert extension strings to EResTypes.
  171. */
  172. class DLL_LINKAGE EResTypeHelper
  173. {
  174. public:
  175. /**
  176. * Converts a extension string to a EResType enum object.
  177. *
  178. * @param extension The extension string e.g. .BMP, .PNG
  179. * @return Returns a EResType enum object
  180. */
  181. static EResType::Type getTypeFromExtension(std::string extension);
  182. /**
  183. * Gets the EResType as a string representation.
  184. *
  185. * @param type the EResType
  186. * @return the type as a string representation
  187. */
  188. static std::string getEResTypeAsString(EResType::Type type);
  189. };