Filesystem.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. CFilesystemList * CResourceHandler::resourceLoader = nullptr;
  14. CFilesystemList * CResourceHandler::initialLoader = nullptr;
  15. std::map<std::string, ISimpleResourceLoader*> CResourceHandler::knownLoaders = std::map<std::string, ISimpleResourceLoader*>();
  16. CFilesystemGenerator::CFilesystemGenerator(std::string prefix):
  17. filesystem(new CFilesystemList()),
  18. prefix(prefix)
  19. {
  20. }
  21. CFilesystemGenerator::TLoadFunctorMap CFilesystemGenerator::genFunctorMap()
  22. {
  23. TLoadFunctorMap map;
  24. map["map"] = boost::bind(&CFilesystemGenerator::loadJsonMap, this, _1, _2);
  25. map["dir"] = boost::bind(&CFilesystemGenerator::loadDirectory, this, _1, _2);
  26. map["lod"] = boost::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_LOD>, this, _1, _2);
  27. map["snd"] = boost::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_SND>, this, _1, _2);
  28. map["vid"] = boost::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_VID>, this, _1, _2);
  29. map["zip"] = boost::bind(&CFilesystemGenerator::loadZipArchive, this, _1, _2);
  30. return map;
  31. }
  32. void CFilesystemGenerator::loadConfig(const JsonNode & config)
  33. {
  34. for(auto & mountPoint : config.Struct())
  35. {
  36. for(auto & entry : mountPoint.second.Vector())
  37. {
  38. CStopWatch timer;
  39. logGlobal->debugStream() << "\t\tLoading resource at " << prefix + entry["path"].String();
  40. auto map = genFunctorMap();
  41. auto functor = map.find(entry["type"].String());
  42. if (functor != map.end())
  43. {
  44. functor->second(mountPoint.first, entry);
  45. logGlobal->debugStream() << "Resource loaded in " << timer.getDiff() << " ms.";
  46. }
  47. else
  48. {
  49. logGlobal->errorStream() << "Unknown filesystem format: " << functor->first;
  50. }
  51. }
  52. }
  53. }
  54. CFilesystemList * CFilesystemGenerator::getFilesystem()
  55. {
  56. return filesystem;
  57. }
  58. void CFilesystemGenerator::loadDirectory(const std::string &mountPoint, const JsonNode & config)
  59. {
  60. std::string URI = prefix + config["path"].String();
  61. int depth = 16;
  62. if (!config["depth"].isNull())
  63. depth = config["depth"].Float();
  64. ResourceID resID(URI, EResType::DIRECTORY);
  65. for(auto & loader : CResourceHandler::getInitial()->getResourcesWithName(resID))
  66. {
  67. auto filename = loader->getResourceName(resID);
  68. filesystem->addLoader(new CFilesystemLoader(mountPoint, *filename, depth), false);
  69. }
  70. }
  71. void CFilesystemGenerator::loadZipArchive(const std::string &mountPoint, const JsonNode & config)
  72. {
  73. std::string URI = prefix + config["path"].String();
  74. auto filename = CResourceHandler::getInitial()->getResourceName(ResourceID(URI, EResType::ARCHIVE_ZIP));
  75. if (filename)
  76. filesystem->addLoader(new CZipLoader(mountPoint, *filename), false);
  77. }
  78. template<EResType::Type archiveType>
  79. void CFilesystemGenerator::loadArchive(const std::string &mountPoint, const JsonNode & config)
  80. {
  81. std::string URI = prefix + config["path"].String();
  82. auto filename = CResourceHandler::getInitial()->getResourceName(ResourceID(URI, archiveType));
  83. if (filename)
  84. filesystem->addLoader(new CArchiveLoader(mountPoint, *filename), false);
  85. }
  86. void CFilesystemGenerator::loadJsonMap(const std::string &mountPoint, const JsonNode & config)
  87. {
  88. std::string URI = prefix + config["path"].String();
  89. auto filename = CResourceHandler::getInitial()->getResourceName(ResourceID(URI, EResType::TEXT));
  90. if (filename)
  91. {
  92. auto configData = CResourceHandler::getInitial()->load(ResourceID(URI, EResType::TEXT))->readAll();
  93. const JsonNode config((char*)configData.first.get(), configData.second);
  94. filesystem->addLoader(new CMappedFileLoader(mountPoint, config), false);
  95. }
  96. }
  97. void CResourceHandler::clear()
  98. {
  99. delete resourceLoader;
  100. delete initialLoader;
  101. }
  102. void CResourceHandler::initialize()
  103. {
  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. //temporary filesystem that will be used to initialize main one.
  119. //used to solve several case-sensivity issues like Mp3 vs MP3
  120. initialLoader = new CFilesystemList;
  121. for (auto & path : VCMIDirs::get().dataPaths())
  122. initialLoader->addLoader(new CFilesystemLoader("", path, 0, true), false);
  123. if (VCMIDirs::get().dataPaths().back() != VCMIDirs::get().userDataPath())
  124. initialLoader->addLoader(new CFilesystemLoader("", VCMIDirs::get().userDataPath(), 0, true), false);
  125. recurseInDir("CONFIG", 0);// look for configs
  126. recurseInDir("DATA", 0); // look for archives
  127. //TODO: improve mod loading process so depth 2 will no longer be needed
  128. recurseInDir("MODS", 2); // look for mods. Depth 2 is required for now but won't cause speed issues if no mods present
  129. }
  130. ISimpleResourceLoader * CResourceHandler::get()
  131. {
  132. assert(resourceLoader);
  133. return resourceLoader;
  134. }
  135. ISimpleResourceLoader * CResourceHandler::get(std::string identifier)
  136. {
  137. return knownLoaders.at(identifier);
  138. }
  139. ISimpleResourceLoader * CResourceHandler::getInitial()
  140. {
  141. assert(initialLoader);
  142. return initialLoader;
  143. }
  144. void CResourceHandler::load(const std::string &fsConfigURI)
  145. {
  146. auto fsConfigData = initialLoader->load(ResourceID(fsConfigURI, EResType::TEXT))->readAll();
  147. const JsonNode fsConfig((char*)fsConfigData.first.get(), fsConfigData.second);
  148. resourceLoader = new CFilesystemList();
  149. addFilesystem("core", createFileSystem("", fsConfig["filesystem"]));
  150. // hardcoded system-specific path, may not be inside any of data directories
  151. resourceLoader->addLoader(new CFilesystemLoader("SAVES/", VCMIDirs::get().userSavePath()), true);
  152. resourceLoader->addLoader(new CFilesystemLoader("CONFIG/", VCMIDirs::get().userConfigPath()), true);
  153. }
  154. void CResourceHandler::addFilesystem(const std::string & identifier, ISimpleResourceLoader * loader)
  155. {
  156. resourceLoader->addLoader(loader, false);
  157. knownLoaders[identifier] = loader;
  158. }
  159. ISimpleResourceLoader * CResourceHandler::createFileSystem(const std::string & prefix, const JsonNode &fsConfig)
  160. {
  161. CFilesystemGenerator generator(prefix);
  162. generator.loadConfig(fsConfig);
  163. return generator.getFilesystem();
  164. }
  165. std::vector<std::string> CResourceHandler::getAvailableMods()
  166. {
  167. static const std::string modDir = "MODS/";
  168. auto list = initialLoader->getFilteredFiles([](const ResourceID & id) -> bool
  169. {
  170. return id.getType() == EResType::DIRECTORY
  171. && boost::range::count(id.getName(), '/') == 1
  172. && boost::algorithm::starts_with(id.getName(), modDir);
  173. });
  174. //storage for found mods
  175. std::vector<std::string> foundMods;
  176. for (auto & entry : list)
  177. {
  178. std::string name = entry.getName();
  179. name.erase(0, modDir.size()); //Remove path prefix
  180. if (name == "WOG") // check if wog is actually present. Hack-ish but better than crash
  181. {
  182. if (!initialLoader->existsResource(ResourceID("DATA/ZVS", EResType::DIRECTORY)) &&
  183. !initialLoader->existsResource(ResourceID("MODS/WOG/DATA/ZVS", EResType::DIRECTORY)))
  184. {
  185. continue;
  186. }
  187. }
  188. if (!name.empty())
  189. foundMods.push_back(name);
  190. }
  191. return foundMods;
  192. }