Filesystem.cpp 7.8 KB

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