Filesystem.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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_SND,
  51. ARCHIVE_LOD,
  52. PALETTE,
  53. CLIENT_SAVEGAME,
  54. SERVER_SAVEGAME,
  55. DIRECTORY,
  56. ERM,
  57. ERT,
  58. ERS,
  59. OTHER
  60. };
  61. }
  62. /**
  63. * A struct which identifies a resource clearly.
  64. */
  65. class DLL_LINKAGE ResourceID
  66. {
  67. public:
  68. /**
  69. * Default c-tor.
  70. */
  71. ResourceID();
  72. /**
  73. * Ctor. Can be used to create indentifier for resource loading using one parameter
  74. *
  75. * @param name The resource name including extension.
  76. */
  77. explicit ResourceID(std::string fullName);
  78. /**
  79. * Ctor.
  80. *
  81. * @param name The resource name.
  82. * @param type The resource type. A constant from the enumeration EResType.
  83. */
  84. ResourceID(std::string name, EResType::Type type);
  85. /**
  86. * Compares this object with a another resource identifier.
  87. *
  88. * @param other The other resource identifier.
  89. * @return Returns true if both are equally, false if not.
  90. */
  91. inline bool operator==(ResourceID const & other) const
  92. {
  93. return name == other.name && type == other.type;
  94. }
  95. std::string getName() const;
  96. EResType::Type getType() const;
  97. void setName(std::string name);
  98. void setType(EResType::Type type);
  99. private:
  100. /** Specifies the resource name. No extension so .pcx and .png can override each other, always in upper case. **/
  101. std::string name;
  102. /**
  103. * Specifies the resource type. EResType::OTHER if not initialized.
  104. * Required to prevent conflicts if files with different types (e.g. text and image) have the same name.
  105. */
  106. EResType::Type type;
  107. };
  108. namespace std
  109. {
  110. template <> struct hash<ResourceID>
  111. {
  112. size_t operator()(const ResourceID & resourceIdent) const
  113. {
  114. std::hash<int> intHasher;
  115. std::hash<std::string> stringHasher;
  116. return stringHasher(resourceIdent.getName()) ^ intHasher(static_cast<int>(resourceIdent.getType()));
  117. }
  118. };
  119. }
  120. /**
  121. * This class has static methods for a global resource loader access.
  122. *
  123. * Class is not thread-safe. Make sure nobody is calling getInstance while somebody else is calling initialize.
  124. */
  125. class DLL_LINKAGE CResourceHandler
  126. {
  127. public:
  128. /**
  129. * Gets an instance of resource loader.
  130. *
  131. * Make sure that you've set an instance before using it. It'll throw an exception if no instance was set.
  132. *
  133. * @return Returns an instance of resource loader.
  134. */
  135. static ISimpleResourceLoader * get();
  136. /**
  137. * Creates instance of resource loader.
  138. * Will not fill filesystem with data
  139. *
  140. */
  141. static void initialize();
  142. /**
  143. * Semi-debug method to track all possible cases of memory leaks
  144. * Used before exiting application
  145. *
  146. */
  147. static void clear();
  148. /**
  149. * Will load all filesystem data from Json data at this path (config/filesystem.json)
  150. * @param prefix - prefix for all paths in filesystem config
  151. */
  152. static void loadMainFileSystem(const std::string & fsConfigURI);
  153. static void loadModFileSystem(const std::string &prefix, const JsonNode & fsConfig);
  154. static void loadDirectory(const std::string &prefix, const std::string & mountPoint, const JsonNode & config);
  155. static void loadArchive(const std::string &prefix, const std::string & mountPoint, const JsonNode & config, EResType::Type archiveType);
  156. static void loadJsonMap(const std::string &prefix, const std::string & mountPoint, const JsonNode & config);
  157. /**
  158. * Checks all subfolders of MODS directory for presence of mods
  159. * If this directory has mod.json file it will be added to resources
  160. */
  161. static std::vector<std::string> getAvailableMods();
  162. static void setActiveMods(std::vector<std::string> enabledMods); //WARNING: not reentrable. Do not call it twice!!!
  163. private:
  164. /** Instance of resource loader */
  165. static CFilesystemList * resourceLoader;
  166. static CFilesystemList * initialLoader;
  167. };
  168. /**
  169. * A helper class which provides a functionality to convert extension strings to EResTypes.
  170. */
  171. class DLL_LINKAGE EResTypeHelper
  172. {
  173. public:
  174. /**
  175. * Converts a extension string to a EResType enum object.
  176. *
  177. * @param extension The extension string e.g. .BMP, .PNG
  178. * @return Returns a EResType enum object
  179. */
  180. static EResType::Type getTypeFromExtension(std::string extension);
  181. /**
  182. * Gets the EResType as a string representation.
  183. *
  184. * @param type the EResType
  185. * @return the type as a string representation
  186. */
  187. static std::string getEResTypeAsString(EResType::Type type);
  188. };