Filesystem.cpp 7.5 KB

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