Filesystem.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "StdInc.h"
  2. #include "Filesystem.h"
  3. #include "CFileInfo.h"
  4. #include "CArchiveLoader.h"
  5. #include "CFilesystemLoader.h"
  6. #include "AdapterLoaders.h"
  7. #include "CZipLoader.h"
  8. //For filesystem initialization
  9. #include "../JsonNode.h"
  10. #include "../GameConstants.h"
  11. #include "../VCMIDirs.h"
  12. #include "../CStopWatch.h"
  13. std::map<std::string, ISimpleResourceLoader*> CResourceHandler::knownLoaders = std::map<std::string, ISimpleResourceLoader*>();
  14. CFilesystemGenerator::CFilesystemGenerator(std::string prefix):
  15. filesystem(new CFilesystemList()),
  16. prefix(prefix)
  17. {
  18. }
  19. CFilesystemGenerator::TLoadFunctorMap CFilesystemGenerator::genFunctorMap()
  20. {
  21. TLoadFunctorMap map;
  22. map["map"] = std::bind(&CFilesystemGenerator::loadJsonMap, this, _1, _2);
  23. map["dir"] = std::bind(&CFilesystemGenerator::loadDirectory, this, _1, _2);
  24. map["lod"] = std::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_LOD>, this, _1, _2);
  25. map["snd"] = std::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_SND>, this, _1, _2);
  26. map["vid"] = std::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_VID>, this, _1, _2);
  27. map["zip"] = std::bind(&CFilesystemGenerator::loadZipArchive, this, _1, _2);
  28. return map;
  29. }
  30. void CFilesystemGenerator::loadConfig(const JsonNode & config)
  31. {
  32. for(auto & mountPoint : config.Struct())
  33. {
  34. for(auto & entry : mountPoint.second.Vector())
  35. {
  36. CStopWatch timer;
  37. logGlobal->debugStream() << "\t\tLoading resource at " << prefix + entry["path"].String();
  38. auto map = genFunctorMap();
  39. auto functor = map.find(entry["type"].String());
  40. if (functor != map.end())
  41. {
  42. functor->second(mountPoint.first, entry);
  43. logGlobal->debugStream() << "Resource loaded in " << timer.getDiff() << " ms.";
  44. }
  45. else
  46. {
  47. logGlobal->errorStream() << "Unknown filesystem format: " << functor->first;
  48. }
  49. }
  50. }
  51. }
  52. CFilesystemList * CFilesystemGenerator::getFilesystem()
  53. {
  54. return filesystem;
  55. }
  56. void CFilesystemGenerator::loadDirectory(const std::string &mountPoint, const JsonNode & config)
  57. {
  58. std::string URI = prefix + config["path"].String();
  59. int depth = 16;
  60. if (!config["depth"].isNull())
  61. depth = (int)config["depth"].Float();
  62. ResourceID resID(URI, EResType::DIRECTORY);
  63. for(auto & loader : CResourceHandler::get("initial")->getResourcesWithName(resID))
  64. {
  65. auto filename = loader->getResourceName(resID);
  66. filesystem->addLoader(new CFilesystemLoader(mountPoint, *filename, depth), false);
  67. }
  68. }
  69. void CFilesystemGenerator::loadZipArchive(const std::string &mountPoint, const JsonNode & config)
  70. {
  71. std::string URI = prefix + config["path"].String();
  72. auto filename = CResourceHandler::get("initial")->getResourceName(ResourceID(URI, EResType::ARCHIVE_ZIP));
  73. if (filename)
  74. filesystem->addLoader(new CZipLoader(mountPoint, *filename), false);
  75. }
  76. template<EResType::Type archiveType>
  77. void CFilesystemGenerator::loadArchive(const std::string &mountPoint, const JsonNode & config)
  78. {
  79. std::string URI = prefix + config["path"].String();
  80. auto filename = CResourceHandler::get("initial")->getResourceName(ResourceID(URI, archiveType));
  81. if (filename)
  82. filesystem->addLoader(new CArchiveLoader(mountPoint, *filename), false);
  83. }
  84. void CFilesystemGenerator::loadJsonMap(const std::string &mountPoint, const JsonNode & config)
  85. {
  86. std::string URI = prefix + config["path"].String();
  87. auto filename = CResourceHandler::get("initial")->getResourceName(ResourceID(URI, EResType::TEXT));
  88. if (filename)
  89. {
  90. auto configData = CResourceHandler::get("initial")->load(ResourceID(URI, EResType::TEXT))->readAll();
  91. const JsonNode config((char*)configData.first.get(), configData.second);
  92. filesystem->addLoader(new CMappedFileLoader(mountPoint, config), false);
  93. }
  94. }
  95. void CResourceHandler::clear()
  96. {
  97. delete knownLoaders["root"];
  98. }
  99. ISimpleResourceLoader * CResourceHandler::createInitial()
  100. {
  101. //temporary filesystem that will be used to initialize main one.
  102. //used to solve several case-sensivity issues like Mp3 vs MP3
  103. auto initialLoader = new CFilesystemList;
  104. //recurse only into specific directories
  105. auto recurseInDir = [&](std::string URI, int depth)
  106. {
  107. ResourceID ID(URI, EResType::DIRECTORY);
  108. for(auto & loader : initialLoader->getResourcesWithName(ID))
  109. {
  110. auto filename = loader->getResourceName(ID);
  111. if (filename)
  112. {
  113. auto dir = new CFilesystemLoader(URI + "/", *filename, depth, true);
  114. initialLoader->addLoader(dir, false);
  115. }
  116. }
  117. };
  118. // TODO: CFilesystemLoader: Should take boost::filesystem::path in argument
  119. for (auto & path : VCMIDirs::get().dataPaths())
  120. {
  121. if (boost::filesystem::is_directory(path)) // some of system-provided paths may not exist
  122. initialLoader->addLoader(new CFilesystemLoader("", path.string(), 0, true), false);
  123. }
  124. initialLoader->addLoader(new CFilesystemLoader("", VCMIDirs::get().userDataPath().string(), 0, true), false);
  125. recurseInDir("CONFIG", 0);// look for configs
  126. recurseInDir("DATA", 0); // look for archives
  127. recurseInDir("MODS", 64); // look for mods.
  128. return initialLoader;
  129. }
  130. void CResourceHandler::initialize()
  131. {
  132. // Create tree-loke structure that looks like this:
  133. // root
  134. // |
  135. // |- initial
  136. // |
  137. // |- data
  138. // | |-core
  139. // | |-mod1
  140. // | |-modN
  141. // |
  142. // |- local
  143. // |-saves
  144. // |-config
  145. // TODO: CFilesystemLoader should take boost::filesystem::path
  146. knownLoaders["root"] = new CFilesystemList();
  147. knownLoaders["saves"] = new CFilesystemLoader("SAVES/", VCMIDirs::get().userSavePath().string());
  148. knownLoaders["config"] = new CFilesystemLoader("CONFIG/", VCMIDirs::get().userConfigPath().string());
  149. auto localFS = new CFilesystemList();
  150. localFS->addLoader(knownLoaders["saves"], true);
  151. localFS->addLoader(knownLoaders["config"], true);
  152. addFilesystem("root", "initial", createInitial());
  153. addFilesystem("root", "data", new CFilesystemList());
  154. addFilesystem("root", "local", localFS);
  155. }
  156. ISimpleResourceLoader * CResourceHandler::get()
  157. {
  158. return get("root");
  159. }
  160. ISimpleResourceLoader * CResourceHandler::get(std::string identifier)
  161. {
  162. return knownLoaders.at(identifier);
  163. }
  164. void CResourceHandler::load(const std::string &fsConfigURI)
  165. {
  166. auto fsConfigData = get("initial")->load(ResourceID(fsConfigURI, EResType::TEXT))->readAll();
  167. const JsonNode fsConfig((char*)fsConfigData.first.get(), fsConfigData.second);
  168. addFilesystem("data", "core", createFileSystem("", fsConfig["filesystem"]));
  169. }
  170. void CResourceHandler::addFilesystem(const std::string & parent, const std::string & identifier, ISimpleResourceLoader * loader)
  171. {
  172. assert(knownLoaders.count(identifier) == 0);
  173. auto list = dynamic_cast<CFilesystemList *>(knownLoaders.at(parent));
  174. assert(list);
  175. list->addLoader(loader, false);
  176. knownLoaders[identifier] = loader;
  177. }
  178. ISimpleResourceLoader * CResourceHandler::createFileSystem(const std::string & prefix, const JsonNode &fsConfig)
  179. {
  180. CFilesystemGenerator generator(prefix);
  181. generator.loadConfig(fsConfig);
  182. return generator.getFilesystem();
  183. }