Filesystem.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. ResourceID::ResourceID()
  16. :type(EResType::OTHER)
  17. {
  18. }
  19. ResourceID::ResourceID(std::string name)
  20. {
  21. CFileInfo info(std::move(name));
  22. setName(info.getStem());
  23. setType(info.getType());
  24. }
  25. ResourceID::ResourceID(std::string name, EResType::Type type)
  26. {
  27. setName(std::move(name));
  28. setType(type);
  29. }
  30. std::string ResourceID::getName() const
  31. {
  32. return name;
  33. }
  34. EResType::Type ResourceID::getType() const
  35. {
  36. return type;
  37. }
  38. void ResourceID::setName(std::string name)
  39. {
  40. this->name = std::move(name);
  41. size_t dotPos = this->name.find_last_of("/.");
  42. if(dotPos != std::string::npos && this->name[dotPos] == '.')
  43. this->name.erase(dotPos);
  44. // strangely enough but this line takes 40-50% of filesystem loading time
  45. boost::to_upper(this->name);
  46. }
  47. void ResourceID::setType(EResType::Type type)
  48. {
  49. this->type = type;
  50. }
  51. void CResourceHandler::clear()
  52. {
  53. delete resourceLoader;
  54. delete initialLoader;
  55. }
  56. EResType::Type EResTypeHelper::getTypeFromExtension(std::string extension)
  57. {
  58. boost::to_upper(extension);
  59. static const std::map<std::string, EResType::Type> stringToRes =
  60. boost::assign::map_list_of
  61. (".TXT", EResType::TEXT)
  62. (".JSON", EResType::TEXT)
  63. (".DEF", EResType::ANIMATION)
  64. (".MSK", EResType::MASK)
  65. (".MSG", EResType::MASK)
  66. (".H3C", EResType::CAMPAIGN)
  67. (".H3M", EResType::MAP)
  68. (".FNT", EResType::BMP_FONT)
  69. (".TTF", EResType::TTF_FONT)
  70. (".BMP", EResType::IMAGE)
  71. (".JPG", EResType::IMAGE)
  72. (".PCX", EResType::IMAGE)
  73. (".PNG", EResType::IMAGE)
  74. (".TGA", EResType::IMAGE)
  75. (".WAV", EResType::SOUND)
  76. (".82M", EResType::SOUND)
  77. (".SMK", EResType::VIDEO)
  78. (".BIK", EResType::VIDEO)
  79. (".MJPG", EResType::VIDEO)
  80. (".MPG", EResType::VIDEO)
  81. (".AVI", EResType::VIDEO)
  82. (".MP3", EResType::MUSIC)
  83. (".OGG", EResType::MUSIC)
  84. (".ZIP", EResType::ARCHIVE_ZIP)
  85. (".LOD", EResType::ARCHIVE_LOD)
  86. (".PAC", EResType::ARCHIVE_LOD)
  87. (".VID", EResType::ARCHIVE_VID)
  88. (".SND", EResType::ARCHIVE_SND)
  89. (".PAL", EResType::PALETTE)
  90. (".VCGM1", EResType::CLIENT_SAVEGAME)
  91. (".VSGM1", EResType::SERVER_SAVEGAME)
  92. (".ERM", EResType::ERM)
  93. (".ERT", EResType::ERT)
  94. (".ERS", EResType::ERS);
  95. auto iter = stringToRes.find(extension);
  96. if (iter == stringToRes.end())
  97. return EResType::OTHER;
  98. return iter->second;
  99. }
  100. std::string EResTypeHelper::getEResTypeAsString(EResType::Type type)
  101. {
  102. #define MAP_ENUM(value) (EResType::value, #value)
  103. static const std::map<EResType::Type, std::string> stringToRes = boost::assign::map_list_of
  104. MAP_ENUM(TEXT)
  105. MAP_ENUM(ANIMATION)
  106. MAP_ENUM(MASK)
  107. MAP_ENUM(CAMPAIGN)
  108. MAP_ENUM(MAP)
  109. MAP_ENUM(BMP_FONT)
  110. MAP_ENUM(TTF_FONT)
  111. MAP_ENUM(IMAGE)
  112. MAP_ENUM(VIDEO)
  113. MAP_ENUM(SOUND)
  114. MAP_ENUM(MUSIC)
  115. MAP_ENUM(ARCHIVE_ZIP)
  116. MAP_ENUM(ARCHIVE_LOD)
  117. MAP_ENUM(ARCHIVE_SND)
  118. MAP_ENUM(ARCHIVE_VID)
  119. MAP_ENUM(PALETTE)
  120. MAP_ENUM(CLIENT_SAVEGAME)
  121. MAP_ENUM(SERVER_SAVEGAME)
  122. MAP_ENUM(DIRECTORY)
  123. MAP_ENUM(ERM)
  124. MAP_ENUM(ERT)
  125. MAP_ENUM(ERS)
  126. MAP_ENUM(OTHER);
  127. #undef MAP_ENUM
  128. auto iter = stringToRes.find(type);
  129. assert(iter != stringToRes.end());
  130. return iter->second;
  131. }
  132. void CResourceHandler::initialize()
  133. {
  134. //recurse only into specific directories
  135. auto recurseInDir = [](std::string URI, int depth)
  136. {
  137. ResourceID ID(URI, EResType::DIRECTORY);
  138. for(auto & loader : initialLoader->getResourcesWithName(ID))
  139. {
  140. auto filename = loader->getResourceName(ID);
  141. if (filename)
  142. {
  143. auto dir = new CFilesystemLoader(URI + "/", *filename, depth, true);
  144. initialLoader->addLoader(dir, false);
  145. }
  146. }
  147. };
  148. //temporary filesystem that will be used to initialize main one.
  149. //used to solve several case-sensivity issues like Mp3 vs MP3
  150. initialLoader = new CFilesystemList;
  151. resourceLoader = new CFilesystemList;
  152. for (auto & path : VCMIDirs::get().dataPaths())
  153. initialLoader->addLoader(new CFilesystemLoader("", path, 0, true), false);
  154. if (VCMIDirs::get().dataPaths().back() != VCMIDirs::get().userDataPath())
  155. initialLoader->addLoader(new CFilesystemLoader("", VCMIDirs::get().userDataPath(), 0, true), false);
  156. recurseInDir("CONFIG", 0);// look for configs
  157. recurseInDir("DATA", 0); // look for archives
  158. //TODO: improve mod loading process so depth 2 will no longer be needed
  159. recurseInDir("MODS", 2); // look for mods. Depth 2 is required for now but won't cause speed issues if no mods present
  160. }
  161. void CResourceHandler::loadDirectory(const std::string &prefix, const std::string &mountPoint, const JsonNode & config)
  162. {
  163. std::string URI = prefix + config["path"].String();
  164. int depth = 16;
  165. if (!config["depth"].isNull())
  166. depth = config["depth"].Float();
  167. ResourceID resID(URI, EResType::DIRECTORY);
  168. for(auto & loader : initialLoader->getResourcesWithName(resID))
  169. {
  170. auto filename = loader->getResourceName(resID);
  171. resourceLoader->addLoader(new CFilesystemLoader(mountPoint, *filename, depth), false);
  172. }
  173. }
  174. void CResourceHandler::loadZipArchive(const std::string &prefix, const std::string &mountPoint, const JsonNode & config)
  175. {
  176. std::string URI = prefix + config["path"].String();
  177. auto filename = initialLoader->getResourceName(ResourceID(URI, EResType::ARCHIVE_ZIP));
  178. if (filename)
  179. resourceLoader->addLoader(new CZipLoader(mountPoint, *filename), false);
  180. }
  181. void CResourceHandler::loadArchive(const std::string &prefix, const std::string &mountPoint, const JsonNode & config, EResType::Type archiveType)
  182. {
  183. std::string URI = prefix + config["path"].String();
  184. auto filename = initialLoader->getResourceName(ResourceID(URI, archiveType));
  185. if (filename)
  186. resourceLoader->addLoader(new CArchiveLoader(mountPoint, *filename), false);
  187. }
  188. void CResourceHandler::loadJsonMap(const std::string &prefix, const std::string &mountPoint, const JsonNode & config)
  189. {
  190. std::string URI = prefix + config["path"].String();
  191. auto filename = initialLoader->getResourceName(ResourceID(URI, EResType::TEXT));
  192. if (filename)
  193. {
  194. auto configData = initialLoader->load(ResourceID(URI, EResType::TEXT))->readAll();
  195. const JsonNode config((char*)configData.first.get(), configData.second);
  196. resourceLoader->addLoader(new CMappedFileLoader(mountPoint, config), false);
  197. }
  198. }
  199. void CResourceHandler::loadMainFileSystem(const std::string &fsConfigURI)
  200. {
  201. auto fsConfigData = initialLoader->load(ResourceID(fsConfigURI, EResType::TEXT))->readAll();
  202. const JsonNode fsConfig((char*)fsConfigData.first.get(), fsConfigData.second);
  203. loadModFileSystem("", fsConfig["filesystem"]);
  204. // hardcoded system-specific path, may not be inside any of data directories
  205. resourceLoader->addLoader(new CFilesystemLoader("SAVES/", VCMIDirs::get().userSavePath()), true);
  206. resourceLoader->addLoader(new CFilesystemLoader("CONFIG/", VCMIDirs::get().userConfigPath()), true);
  207. }
  208. void CResourceHandler::loadModFileSystem(const std::string & prefix, const JsonNode &fsConfig)
  209. {
  210. for(auto & mountPoint : fsConfig.Struct())
  211. {
  212. for(auto & entry : mountPoint.second.Vector())
  213. {
  214. CStopWatch timer;
  215. logGlobal->debugStream() << "\t\tLoading resource at " << prefix + entry["path"].String();
  216. //TODO: replace if's with string->functor map?
  217. if (entry["type"].String() == "map")
  218. loadJsonMap(prefix, mountPoint.first, entry);
  219. if (entry["type"].String() == "dir")
  220. loadDirectory(prefix, mountPoint.first, entry);
  221. if (entry["type"].String() == "lod")
  222. loadArchive(prefix, mountPoint.first, entry, EResType::ARCHIVE_LOD);
  223. if (entry["type"].String() == "snd")
  224. loadArchive(prefix, mountPoint.first, entry, EResType::ARCHIVE_SND);
  225. if (entry["type"].String() == "vid")
  226. loadArchive(prefix, mountPoint.first, entry, EResType::ARCHIVE_VID);
  227. if (entry["type"].String() == "zip")
  228. loadZipArchive(prefix, mountPoint.first, entry);
  229. logGlobal->debugStream() << "Resource loaded in " << timer.getDiff() << " ms.";
  230. }
  231. }
  232. }
  233. std::vector<std::string> CResourceHandler::getAvailableMods()
  234. {
  235. static const std::string modDir = "MODS/";
  236. auto list = initialLoader->getFilteredFiles([](const ResourceID & id) -> bool
  237. {
  238. return id.getType() == EResType::DIRECTORY
  239. && boost::range::count(id.getName(), '/') == 1
  240. && boost::algorithm::starts_with(id.getName(), modDir);
  241. });
  242. //storage for found mods
  243. std::vector<std::string> foundMods;
  244. for (auto & entry : list)
  245. {
  246. std::string name = entry.getName();
  247. name.erase(0, modDir.size()); //Remove path prefix
  248. if (name == "WOG") // check if wog is actually present. Hack-ish but better than crash
  249. {
  250. if (!initialLoader->existsResource(ResourceID("DATA/ZVS", EResType::DIRECTORY)) &&
  251. !initialLoader->existsResource(ResourceID("MODS/WOG/DATA/ZVS", EResType::DIRECTORY)))
  252. {
  253. continue;
  254. }
  255. }
  256. foundMods.push_back(name);
  257. }
  258. return foundMods;
  259. }
  260. void CResourceHandler::setActiveMods(std::vector<std::string> enabledMods)
  261. {
  262. // default FS config for mods: directory "Content" that acts as H3 root directory
  263. JsonNode defaultFS;
  264. defaultFS[""].Vector().resize(2);
  265. defaultFS[""].Vector()[0]["type"].String() = "zip";
  266. defaultFS[""].Vector()[0]["path"].String() = "/Content.zip";
  267. defaultFS[""].Vector()[1]["type"].String() = "dir";
  268. defaultFS[""].Vector()[1]["path"].String() = "/Content";
  269. for(std::string & modName : enabledMods)
  270. {
  271. ResourceID modConfFile("mods/" + modName + "/mod", EResType::TEXT);
  272. auto fsConfigData = initialLoader->load(modConfFile)->readAll();
  273. const JsonNode fsConfig((char*)fsConfigData.first.get(), fsConfigData.second);
  274. if (!fsConfig["filesystem"].isNull())
  275. loadModFileSystem("mods/" + modName, fsConfig["filesystem"]);
  276. else
  277. loadModFileSystem("mods/" + modName, defaultFS);
  278. }
  279. }