Filesystem.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. CFilesystemGenerator::CFilesystemGenerator(std::string prefix):
  23. filesystem(new CFilesystemList()),
  24. prefix(prefix)
  25. {
  26. }
  27. CFilesystemGenerator::TLoadFunctorMap CFilesystemGenerator::genFunctorMap()
  28. {
  29. TLoadFunctorMap map;
  30. map["map"] = std::bind(&CFilesystemGenerator::loadJsonMap, this, _1, _2);
  31. map["dir"] = std::bind(&CFilesystemGenerator::loadDirectory, this, _1, _2);
  32. map["lod"] = std::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_LOD>, this, _1, _2);
  33. map["snd"] = std::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_SND>, this, _1, _2);
  34. map["vid"] = std::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_VID>, this, _1, _2);
  35. map["zip"] = std::bind(&CFilesystemGenerator::loadZipArchive, this, _1, _2);
  36. return map;
  37. }
  38. void CFilesystemGenerator::loadConfig(const JsonNode & config)
  39. {
  40. for(auto & mountPoint : config.Struct())
  41. {
  42. for(auto & entry : mountPoint.second.Vector())
  43. {
  44. CStopWatch timer;
  45. logGlobal->debugStream() << "\t\tLoading resource at " << prefix + entry["path"].String();
  46. auto map = genFunctorMap();
  47. auto typeName = entry["type"].String();
  48. auto functor = map.find(typeName);
  49. if (functor != map.end())
  50. {
  51. functor->second(mountPoint.first, entry);
  52. logGlobal->debugStream() << "Resource loaded in " << timer.getDiff() << " ms.";
  53. }
  54. else
  55. {
  56. logGlobal->error("Unknown filesystem format: %s", typeName);
  57. }
  58. }
  59. }
  60. }
  61. CFilesystemList * CFilesystemGenerator::getFilesystem()
  62. {
  63. return filesystem;
  64. }
  65. void CFilesystemGenerator::loadDirectory(const std::string &mountPoint, const JsonNode & config)
  66. {
  67. std::string URI = prefix + config["path"].String();
  68. int depth = 16;
  69. if (!config["depth"].isNull())
  70. depth = (int)config["depth"].Float();
  71. ResourceID resID(URI, EResType::DIRECTORY);
  72. for(auto & loader : CResourceHandler::get("initial")->getResourcesWithName(resID))
  73. {
  74. auto filename = loader->getResourceName(resID);
  75. filesystem->addLoader(new CFilesystemLoader(mountPoint, *filename, depth), false);
  76. }
  77. }
  78. void CFilesystemGenerator::loadZipArchive(const std::string &mountPoint, const JsonNode & config)
  79. {
  80. std::string URI = prefix + config["path"].String();
  81. auto filename = CResourceHandler::get("initial")->getResourceName(ResourceID(URI, EResType::ARCHIVE_ZIP));
  82. if (filename)
  83. filesystem->addLoader(new CZipLoader(mountPoint, *filename), false);
  84. }
  85. template<EResType::Type archiveType>
  86. void CFilesystemGenerator::loadArchive(const std::string &mountPoint, const JsonNode & config)
  87. {
  88. std::string URI = prefix + config["path"].String();
  89. auto filename = CResourceHandler::get("initial")->getResourceName(ResourceID(URI, archiveType));
  90. if (filename)
  91. filesystem->addLoader(new CArchiveLoader(mountPoint, *filename), false);
  92. }
  93. void CFilesystemGenerator::loadJsonMap(const std::string &mountPoint, const JsonNode & config)
  94. {
  95. std::string URI = prefix + config["path"].String();
  96. auto filename = CResourceHandler::get("initial")->getResourceName(ResourceID(URI, EResType::TEXT));
  97. if (filename)
  98. {
  99. auto configData = CResourceHandler::get("initial")->load(ResourceID(URI, EResType::TEXT))->readAll();
  100. const JsonNode config((char*)configData.first.get(), configData.second);
  101. filesystem->addLoader(new CMappedFileLoader(mountPoint, config), false);
  102. }
  103. }
  104. void CResourceHandler::clear()
  105. {
  106. delete knownLoaders["root"];
  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 = [&](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-loke 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. knownLoaders["root"] = new CFilesystemList();
  154. knownLoaders["saves"] = new CFilesystemLoader("SAVES/", VCMIDirs::get().userSavePath());
  155. knownLoaders["config"] = new CFilesystemLoader("CONFIG/", VCMIDirs::get().userConfigPath());
  156. auto localFS = new CFilesystemList();
  157. localFS->addLoader(knownLoaders["saves"], true);
  158. localFS->addLoader(knownLoaders["config"], true);
  159. addFilesystem("root", "initial", createInitial());
  160. addFilesystem("root", "data", new CFilesystemList());
  161. addFilesystem("root", "local", localFS);
  162. }
  163. ISimpleResourceLoader * CResourceHandler::get()
  164. {
  165. return get("root");
  166. }
  167. ISimpleResourceLoader * CResourceHandler::get(std::string identifier)
  168. {
  169. return knownLoaders.at(identifier);
  170. }
  171. void CResourceHandler::load(const std::string &fsConfigURI)
  172. {
  173. auto fsConfigData = get("initial")->load(ResourceID(fsConfigURI, EResType::TEXT))->readAll();
  174. const JsonNode fsConfig((char*)fsConfigData.first.get(), fsConfigData.second);
  175. addFilesystem("data", "core", createFileSystem("", fsConfig["filesystem"]));
  176. }
  177. void CResourceHandler::addFilesystem(const std::string & parent, const std::string & identifier, ISimpleResourceLoader * loader)
  178. {
  179. assert(knownLoaders.count(identifier) == 0);
  180. auto list = dynamic_cast<CFilesystemList *>(knownLoaders.at(parent));
  181. assert(list);
  182. list->addLoader(loader, false);
  183. knownLoaders[identifier] = loader;
  184. }
  185. ISimpleResourceLoader * CResourceHandler::createFileSystem(const std::string & prefix, const JsonNode &fsConfig)
  186. {
  187. CFilesystemGenerator generator(prefix);
  188. generator.loadConfig(fsConfig);
  189. return generator.getFilesystem();
  190. }