Filesystem.cpp 8.1 KB

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