2
0

Filesystem.cpp 6.7 KB

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