Filesystem.h 3.3 KB

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