Filesystem.cpp 6.6 KB

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