Filesystem.h 3.7 KB

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