Filesystem.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "AdapterLoaders.h"
  13. #include "ResourceID.h"
  14. class JsonNode;
  15. /// Helper class that allows generation of a ISimpleResourceLoader entry out of Json config(s)
  16. class DLL_LINKAGE CFilesystemGenerator
  17. {
  18. typedef boost::function<void(const std::string &, const JsonNode &)> TLoadFunctor;
  19. typedef std::map<std::string, TLoadFunctor> TLoadFunctorMap;
  20. CFilesystemList * filesystem;
  21. std::string prefix;
  22. template<EResType::Type archiveType>
  23. void loadArchive(const std::string & mountPoint, const JsonNode & config);
  24. void loadDirectory(const std::string & mountPoint, const JsonNode & config);
  25. void loadZipArchive(const std::string & mountPoint, const JsonNode & config);
  26. void loadJsonMap(const std::string & mountPoint, const JsonNode & config);
  27. TLoadFunctorMap genFunctorMap();
  28. public:
  29. /// prefix = prefix that will be given to file entries in all nodes of this filesystem
  30. CFilesystemGenerator(std::string prefix);
  31. /// loads configuration from json
  32. /// config - configuration to load, using format of "filesystem" entry in config/filesystem.json
  33. void loadConfig(const JsonNode & config);
  34. /// returns generated filesystem
  35. CFilesystemList * getFilesystem();
  36. };
  37. /**
  38. * This class has static methods for a global resource loader access.
  39. *
  40. * Class is not thread-safe.
  41. */
  42. class DLL_LINKAGE CResourceHandler
  43. {
  44. public:
  45. /**
  46. * Gets an instance of resource loader.
  47. *
  48. * Make sure that you've set an instance before using it. It'll throw an exception if no instance was set.
  49. *
  50. * @return Returns an instance of resource loader.
  51. */
  52. static CFilesystemList * get();
  53. static CFilesystemList * getInitial();
  54. static CFilesystemList * getCoreData();
  55. /**
  56. * Creates instance of initial resource loader.
  57. * Will not fill filesystem with data
  58. *
  59. */
  60. static void initialize();
  61. /**
  62. * Semi-debug method to track all possible cases of memory leaks
  63. * Used before exiting application
  64. *
  65. */
  66. static void clear();
  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 createModFileSystem - creates filesystem out of config file
  74. * @param prefix - prefix for all paths in filesystem config
  75. * @param fsConfig - configuration to load
  76. * @return generated filesystem that contains all config entries
  77. */
  78. static CFilesystemList * createFileSystem(const std::string &prefix, const JsonNode & fsConfig);
  79. /**
  80. * Checks all subfolders of MODS directory for presence of mods
  81. * If this directory has mod.json file it will be added to resources
  82. */
  83. static std::vector<std::string> getAvailableMods();
  84. private:
  85. /** Instance of resource loader */
  86. static CFilesystemList * resourceLoader;
  87. static CFilesystemList * initialLoader;
  88. static CFilesystemList * coreDataLoader;
  89. };