Filesystem.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Filesystem.cpp, 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. #include "StdInc.h"
  11. #include "Filesystem.h"
  12. #include "CArchiveLoader.h"
  13. #include "CFilesystemLoader.h"
  14. #include "AdapterLoaders.h"
  15. #include "CZipLoader.h"
  16. //For filesystem initialization
  17. #include "../GameConstants.h"
  18. #include "../VCMIDirs.h"
  19. #include "../CStopWatch.h"
  20. #include "../json/JsonNode.h"
  21. #include "../modding/ModScope.h"
  22. VCMI_LIB_NAMESPACE_BEGIN
  23. std::map<std::string, ISimpleResourceLoader*> CResourceHandler::knownLoaders = std::map<std::string, ISimpleResourceLoader*>();
  24. CResourceHandler CResourceHandler::globalResourceHandler;
  25. CFilesystemGenerator::CFilesystemGenerator(std::string prefix, bool extractArchives):
  26. filesystem(std::make_unique<CFilesystemList>()),
  27. prefix(std::move(prefix)),
  28. extractArchives(extractArchives)
  29. {
  30. }
  31. CFilesystemGenerator::~CFilesystemGenerator() = default;
  32. CFilesystemGenerator::TLoadFunctorMap CFilesystemGenerator::genFunctorMap()
  33. {
  34. TLoadFunctorMap map;
  35. map["map"] = std::bind(&CFilesystemGenerator::loadJsonMap, this, _1, _2);
  36. map["dir"] = std::bind(&CFilesystemGenerator::loadDirectory, this, _1, _2);
  37. map["lod"] = std::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_LOD>, this, _1, _2);
  38. map["snd"] = std::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_SND>, this, _1, _2);
  39. map["vid"] = std::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_VID>, this, _1, _2);
  40. map["zip"] = std::bind(&CFilesystemGenerator::loadZipArchive, this, _1, _2);
  41. return map;
  42. }
  43. void CFilesystemGenerator::loadConfig(const JsonNode & config)
  44. {
  45. for(const auto & mountPoint : config.Struct())
  46. {
  47. for(const auto & entry : mountPoint.second.Vector())
  48. {
  49. CStopWatch timer;
  50. logGlobal->trace("\t\tLoading resource at %s%s", prefix, entry["path"].String());
  51. auto map = genFunctorMap();
  52. auto typeName = entry["type"].String();
  53. auto functor = map.find(typeName);
  54. if (functor != map.end())
  55. {
  56. functor->second(mountPoint.first, entry);
  57. logGlobal->trace("Resource loaded in %d ms", timer.getDiff());
  58. }
  59. else
  60. {
  61. logGlobal->error("Unknown filesystem format: %s", typeName);
  62. }
  63. }
  64. }
  65. }
  66. std::unique_ptr<CFilesystemList> CFilesystemGenerator::acquireFilesystem()
  67. {
  68. return std::move(filesystem);
  69. }
  70. void CFilesystemGenerator::loadDirectory(const std::string &mountPoint, const JsonNode & config)
  71. {
  72. std::string URI = prefix + config["path"].String();
  73. int depth = 16;
  74. if (!config["depth"].isNull())
  75. depth = static_cast<int>(config["depth"].Float());
  76. ResourcePath resID(URI, EResType::DIRECTORY);
  77. for(auto & loader : CResourceHandler::get("initial")->getResourcesWithName(resID))
  78. {
  79. auto filename = loader->getResourceName(resID);
  80. filesystem->addLoader(std::make_unique<CFilesystemLoader>(mountPoint, *filename, depth), false);
  81. }
  82. }
  83. void CFilesystemGenerator::loadZipArchive(const std::string &mountPoint, const JsonNode & config)
  84. {
  85. std::string URI = prefix + config["path"].String();
  86. auto filename = CResourceHandler::get("initial")->getResourceName(ResourcePath(URI, EResType::ARCHIVE_ZIP));
  87. if (filename)
  88. filesystem->addLoader(std::make_unique<CZipLoader>(mountPoint, *filename), false);
  89. }
  90. template<EResType archiveType>
  91. void CFilesystemGenerator::loadArchive(const std::string &mountPoint, const JsonNode & config)
  92. {
  93. std::string URI = prefix + config["path"].String();
  94. auto filename = CResourceHandler::get("initial")->getResourceName(ResourcePath(URI, archiveType));
  95. if (filename)
  96. filesystem->addLoader(std::make_unique<CArchiveLoader>(mountPoint, *filename, extractArchives), false);
  97. }
  98. void CFilesystemGenerator::loadJsonMap(const std::string &mountPoint, const JsonNode & config)
  99. {
  100. std::string URI = prefix + config["path"].String();
  101. auto filename = CResourceHandler::get("initial")->getResourceName(JsonPath::builtin(URI));
  102. if (filename)
  103. {
  104. auto configData = CResourceHandler::get("initial")->load(JsonPath::builtin(URI))->readAll();
  105. const JsonNode configInitial(reinterpret_cast<std::byte *>(configData.first.get()), configData.second, URI);
  106. filesystem->addLoader(std::make_unique<CMappedFileLoader>(mountPoint, configInitial), false);
  107. }
  108. }
  109. std::unique_ptr<ISimpleResourceLoader> CResourceHandler::createInitial()
  110. {
  111. //temporary filesystem that will be used to initialize main one.
  112. //used to solve several case-sensivity issues like Mp3 vs MP3
  113. auto initialLoader = std::make_unique<CFilesystemList>();
  114. //recurse only into specific directories
  115. auto recurseInDir = [&](const std::string & URI, int depth)
  116. {
  117. ResourcePath ID(URI, EResType::DIRECTORY);
  118. for(auto & loader : initialLoader->getResourcesWithName(ID))
  119. {
  120. auto filename = loader->getResourceName(ID);
  121. if (filename)
  122. {
  123. auto dir = std::make_unique<CFilesystemLoader>(URI + '/', *filename, depth, true);
  124. initialLoader->addLoader(std::move(dir), false);
  125. }
  126. }
  127. };
  128. for (auto & path : VCMIDirs::get().dataPaths())
  129. {
  130. if (boost::filesystem::is_directory(path)) // some of system-provided paths may not exist
  131. initialLoader->addLoader(std::make_unique<CFilesystemLoader>("", path, 1, true), false);
  132. }
  133. initialLoader->addLoader(std::make_unique<CFilesystemLoader>("", VCMIDirs::get().userDataPath(), 0, true), false);
  134. recurseInDir("CONFIG", 0);// look for configs
  135. recurseInDir("DATA", 0); // look for archives
  136. recurseInDir("MODS", 64); // look for mods.
  137. return initialLoader;
  138. }
  139. void CResourceHandler::initialize()
  140. {
  141. // Create tree-like structure that looks like this:
  142. // root
  143. // |
  144. // |- initial
  145. // |
  146. // |- data
  147. // | |-core
  148. // | |-mod1
  149. // | |-modN
  150. // |
  151. // |- local
  152. // |-saves
  153. // |-config
  154. // when built as single process, server can be started multiple times
  155. if (globalResourceHandler.rootLoader)
  156. return;
  157. auto savesLoader = std::make_unique<CFilesystemLoader>("SAVES/", VCMIDirs::get().userSavePath());
  158. auto configLoader = std::make_unique<CFilesystemLoader>("CONFIG/", VCMIDirs::get().userConfigPath());
  159. globalResourceHandler.rootLoader = std::make_unique<CFilesystemList>();
  160. knownLoaders["root"] = globalResourceHandler.rootLoader.get();
  161. knownLoaders["saves"] = savesLoader.get();
  162. knownLoaders["config"] = configLoader.get();
  163. auto localFS = std::make_unique<CFilesystemList>();
  164. localFS->addLoader(std::move(savesLoader), true);
  165. localFS->addLoader(std::move(configLoader), true);
  166. addFilesystem("root", "initial", createInitial());
  167. addFilesystem("root", "data", std::make_unique<CFilesystemList>());
  168. addFilesystem("root", "local", std::move(localFS));
  169. }
  170. void CResourceHandler::destroy()
  171. {
  172. knownLoaders.clear();
  173. globalResourceHandler.rootLoader.reset();
  174. }
  175. ISimpleResourceLoader * CResourceHandler::get()
  176. {
  177. return get("root");
  178. }
  179. ISimpleResourceLoader * CResourceHandler::get(const std::string & identifier)
  180. {
  181. assert(knownLoaders.count(identifier));
  182. return knownLoaders.at(identifier);
  183. }
  184. void CResourceHandler::load(const std::string &fsConfigURI, bool extractArchives)
  185. {
  186. auto fsConfigData = get("initial")->load(JsonPath::builtin(fsConfigURI))->readAll();
  187. const JsonNode fsConfig(reinterpret_cast<std::byte *>(fsConfigData.first.get()), fsConfigData.second, fsConfigURI);
  188. addFilesystem("data", ModScope::scopeBuiltin(), createFileSystem("", fsConfig["filesystem"], extractArchives));
  189. }
  190. void CResourceHandler::addFilesystem(const std::string & parent, const std::string & identifier, std::unique_ptr<ISimpleResourceLoader> loader)
  191. {
  192. if(knownLoaders.count(identifier) != 0)
  193. {
  194. logMod->error("[CRITICAL] Virtual filesystem %s already loaded!", identifier);
  195. return;
  196. }
  197. if(knownLoaders.count(parent) == 0)
  198. {
  199. logMod->error("[CRITICAL] Parent virtual filesystem %s for %s not found!", parent, identifier);
  200. return;
  201. }
  202. auto * list = dynamic_cast<CFilesystemList *>(knownLoaders.at(parent));
  203. assert(list);
  204. knownLoaders[identifier] = loader.get();
  205. list->addLoader(std::move(loader), false);
  206. }
  207. bool CResourceHandler::removeFilesystem(const std::string & parent, const std::string & identifier)
  208. {
  209. if(knownLoaders.count(identifier) == 0)
  210. return false;
  211. if(knownLoaders.count(parent) == 0)
  212. return false;
  213. auto * list = dynamic_cast<CFilesystemList *>(knownLoaders.at(parent));
  214. assert(list);
  215. list->removeLoader(knownLoaders[identifier]);
  216. knownLoaders.erase(identifier);
  217. return true;
  218. }
  219. std::unique_ptr<ISimpleResourceLoader> CResourceHandler::createFileSystem(const std::string & prefix, const JsonNode &fsConfig, bool extractArchives)
  220. {
  221. CFilesystemGenerator generator(prefix, extractArchives);
  222. generator.loadConfig(fsConfig);
  223. return generator.acquireFilesystem();
  224. }
  225. VCMI_LIB_NAMESPACE_END