Filesystem.cpp 7.7 KB

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