Filesystem.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #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 boost::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. public:
  46. /**
  47. * Gets an instance of resource loader.
  48. *
  49. * Make sure that you've set an instance before using it. It'll throw an exception if no instance was set.
  50. *
  51. * @return Returns an instance of resource loader.
  52. */
  53. static ISimpleResourceLoader * get();
  54. static ISimpleResourceLoader * get(std::string identifier);
  55. static ISimpleResourceLoader * getInitial();
  56. /**
  57. * Creates instance of initial resource loader.
  58. * Will not fill filesystem with data
  59. *
  60. */
  61. static void initialize();
  62. /**
  63. * Semi-debug method to track all possible cases of memory leaks
  64. * Used before exiting application
  65. *
  66. */
  67. static void clear();
  68. /**
  69. * Will load all filesystem data from Json data at this path (normally - config/filesystem.json)
  70. * @param fsConfigURI - URI from which data will be loaded
  71. */
  72. static void load(const std::string & fsConfigURI);
  73. /**
  74. * @brief addFilesystem adds filesystem into global resource loader
  75. * @param identifier name of this loader by which it can be retrieved later
  76. * @param loader resource loader to add
  77. */
  78. static void addFilesystem(const std::string & identifier, ISimpleResourceLoader * loader);
  79. /**
  80. * @brief createModFileSystem - creates filesystem out of config file
  81. * @param prefix - prefix for all paths in filesystem config
  82. * @param fsConfig - configuration to load
  83. * @return generated filesystem that contains all config entries
  84. */
  85. static ISimpleResourceLoader * createFileSystem(const std::string &prefix, const JsonNode & fsConfig);
  86. /**
  87. * Checks all subfolders of MODS directory for presence of mods
  88. * If this directory has mod.json file it will be added to resources
  89. */
  90. static std::vector<std::string> getAvailableMods();
  91. private:
  92. /** Instance of resource loader */
  93. static std::map<std::string, ISimpleResourceLoader*> knownLoaders;
  94. static CFilesystemList * resourceLoader;
  95. static CFilesystemList * initialLoader;
  96. };