2
0

Filesystem.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Filesystem.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. #include "ISimpleResourceLoader.h"
  13. #include "ResourcePath.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class CFilesystemList;
  16. class JsonNode;
  17. /// Helper class that allows generation of a ISimpleResourceLoader entry out of Json config(s)
  18. class DLL_LINKAGE CFilesystemGenerator
  19. {
  20. using TLoadFunctor = std::function<void(const std::string &, const JsonNode &)>;
  21. using TLoadFunctorMap = std::map<std::string, TLoadFunctor>;
  22. CFilesystemList * filesystem;
  23. std::string prefix;
  24. template<EResType archiveType>
  25. void loadArchive(const std::string & mountPoint, const JsonNode & config);
  26. void loadDirectory(const std::string & mountPoint, const JsonNode & config);
  27. void loadZipArchive(const std::string & mountPoint, const JsonNode & config);
  28. void loadJsonMap(const std::string & mountPoint, const JsonNode & config);
  29. TLoadFunctorMap genFunctorMap();
  30. public:
  31. /// prefix = prefix that will be given to file entries in all nodes of this filesystem
  32. /// extractArchives = Specifies if Original H3 archives should be extracted to a separate folder
  33. CFilesystemGenerator(std::string prefix, bool extractArchives = false);
  34. /// loads configuration from json
  35. /// config - configuration to load, using format of "filesystem" entry in config/filesystem.json
  36. void loadConfig(const JsonNode & config);
  37. /// returns generated filesystem
  38. CFilesystemList * getFilesystem();
  39. /** Specifies if Original H3 archives should be extracted to a separate folder **/
  40. bool extractArchives;
  41. };
  42. /**
  43. * This class has static methods for a global resource loader access.
  44. *
  45. * Class is not thread-safe.
  46. */
  47. class DLL_LINKAGE CResourceHandler
  48. {
  49. /**
  50. * @brief createInitial - creates instance of initial loader
  51. * that contains data necessary to load main FS
  52. */
  53. static ISimpleResourceLoader * createInitial();
  54. public:
  55. /**
  56. * Gets an instance of resource loader.
  57. *
  58. * Make sure that you've set an instance before using it. It'll throw an exception if no instance was set.
  59. *
  60. * @return Returns an instance of resource loader.
  61. */
  62. static ISimpleResourceLoader * get();
  63. static ISimpleResourceLoader * get(const std::string & identifier);
  64. /**
  65. * Creates instance of initial resource loader.
  66. * Will not fill filesystem with data
  67. *
  68. */
  69. static void initialize();
  70. /**
  71. * Destroys all existing data in filesystem, bringing it into uninitialized state
  72. *
  73. */
  74. static void destroy();
  75. /**
  76. * Will load all filesystem data from Json data at this path (normally - config/filesystem.json)
  77. * @param fsConfigURI - URI from which data will be loaded
  78. */
  79. static void load(const std::string & fsConfigURI, bool extractArchives = false);
  80. /**
  81. * @brief addFilesystem adds filesystem into global resource loader
  82. * @param identifier name of this loader by which it can be retrieved later
  83. * @param loader resource loader to add
  84. */
  85. static void addFilesystem(const std::string & parent, const std::string & identifier, ISimpleResourceLoader * loader);
  86. /**
  87. * @brief removeFilesystem removes previously added filesystem from global resource holder
  88. * @param parent parent loader containing filesystem
  89. * @param identifier name of this loader
  90. * @return if filesystem was successfully removed
  91. */
  92. static bool removeFilesystem(const std::string & parent, const std::string & identifier);
  93. /**
  94. * @brief createModFileSystem - creates filesystem out of config file
  95. * @param prefix - prefix for all paths in filesystem config
  96. * @param fsConfig - configuration to load
  97. * @return generated filesystem that contains all config entries
  98. */
  99. static ISimpleResourceLoader * createFileSystem(const std::string &prefix, const JsonNode & fsConfig, bool extractArchives = false);
  100. ~CResourceHandler() = default;
  101. private:
  102. /** Instance of resource loader */
  103. static std::map<std::string, ISimpleResourceLoader*> knownLoaders;
  104. static CResourceHandler globalResourceHandler;
  105. CResourceHandler() {};
  106. std::unique_ptr<ISimpleResourceLoader> rootLoader;
  107. };
  108. VCMI_LIB_NAMESPACE_END