Filesystem.cpp 9.2 KB

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