Filesystem.h 4.3 KB

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