CResourceLoader.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #include "StdInc.h"
  2. #include "CResourceLoader.h"
  3. #include "CFileInfo.h"
  4. #include "CLodArchiveLoader.h"
  5. #include "CFilesystemLoader.h"
  6. //For filesystem initialization
  7. #include "../JsonNode.h"
  8. #include "../GameConstants.h"
  9. #include "../VCMIDirs.h"
  10. //experimental support for ERA-style mods. Requires custom config in mod directory
  11. #define ENABLE_ERA_FILESYSTEM
  12. CResourceLoader * CResourceHandler::resourceLoader = nullptr;
  13. CResourceLoader * CResourceHandler::initialLoader = nullptr;
  14. ResourceID::ResourceID()
  15. :type(EResType::OTHER)
  16. {
  17. }
  18. ResourceID::ResourceID(const std::string & name)
  19. {
  20. CFileInfo info(name);
  21. setName(info.getStem());
  22. setType(info.getType());
  23. }
  24. ResourceID::ResourceID(const std::string & name, EResType::Type type)
  25. {
  26. setName(name);
  27. setType(type);
  28. }
  29. ResourceID::ResourceID(const std::string & prefix, const std::string & name, EResType::Type type)
  30. {
  31. setName(name);
  32. this->name = prefix + this->name;
  33. setType(type);
  34. }
  35. std::string ResourceID::getName() const
  36. {
  37. return name;
  38. }
  39. EResType::Type ResourceID::getType() const
  40. {
  41. return type;
  42. }
  43. void ResourceID::setName(const std::string & name)
  44. {
  45. this->name = name;
  46. size_t dotPos = this->name.find_last_of("/.");
  47. if(dotPos != std::string::npos && this->name[dotPos] == '.')
  48. this->name.erase(dotPos);
  49. // strangely enough but this line takes 40-50% of filesystem loading time
  50. boost::to_upper(this->name);
  51. }
  52. void ResourceID::setType(EResType::Type type)
  53. {
  54. this->type = type;
  55. }
  56. CResourceLoader::CResourceLoader()
  57. {
  58. }
  59. CResourceLoader::~CResourceLoader()
  60. {
  61. // Delete all loader objects
  62. BOOST_FOREACH ( auto & it, loaders)
  63. {
  64. delete it;
  65. }
  66. }
  67. std::unique_ptr<CInputStream> CResourceLoader::load(const ResourceID & resourceIdent) const
  68. {
  69. auto resource = resources.find(resourceIdent);
  70. if(resource == resources.end())
  71. {
  72. throw std::runtime_error("Resource with name " + resourceIdent.getName() + " and type "
  73. + EResTypeHelper::getEResTypeAsString(resourceIdent.getType()) + " wasn't found.");
  74. }
  75. // get the last added resource(most overriden)
  76. const ResourceLocator & locator = resource->second.back();
  77. // load the resource and return it
  78. return locator.getLoader()->load(locator.getResourceName());
  79. }
  80. std::pair<std::unique_ptr<ui8[]>, ui64> CResourceLoader::loadData(const ResourceID & resourceIdent) const
  81. {
  82. auto stream = load(resourceIdent);
  83. std::unique_ptr<ui8[]> data(new ui8[stream->getSize()]);
  84. size_t readSize = stream->read(data.get(), stream->getSize());
  85. assert(readSize == stream->getSize());
  86. return std::make_pair(std::move(data), stream->getSize());
  87. }
  88. ResourceLocator CResourceLoader::getResource(const ResourceID & resourceIdent) const
  89. {
  90. auto resource = resources.find(resourceIdent);
  91. if (resource == resources.end())
  92. return ResourceLocator(nullptr, "");
  93. return resource->second.back();
  94. }
  95. std::string CResourceLoader::getResourceName(const ResourceID & resourceIdent) const
  96. {
  97. auto locator = getResource(resourceIdent);
  98. if (locator.getLoader())
  99. return locator.getLoader()->getOrigin() + '/' + locator.getResourceName();
  100. return "";
  101. }
  102. bool CResourceLoader::existsResource(const ResourceID & resourceIdent) const
  103. {
  104. // Check if resource is registered
  105. return resources.find(resourceIdent) != resources.end();
  106. }
  107. void CResourceLoader::addLoader(std::string mountPoint, ISimpleResourceLoader * loader)
  108. {
  109. loaders.insert(loader);
  110. // Get entries and add them to the resources list
  111. const std::list<std::string> & entries = loader->getEntries();
  112. boost::to_upper(mountPoint);
  113. BOOST_FOREACH (const std::string & entry, entries)
  114. {
  115. CFileInfo file(entry);
  116. // Create identifier and locator and add them to the resources list
  117. ResourceID ident(mountPoint, file.getStem(), file.getType());
  118. ResourceLocator locator(loader, entry);
  119. resources[ident].push_back(locator);
  120. }
  121. }
  122. CResourceLoader * CResourceHandler::get()
  123. {
  124. if(resourceLoader != nullptr)
  125. {
  126. return resourceLoader;
  127. }
  128. else
  129. {
  130. std::stringstream string;
  131. string << "Error: Resource loader wasn't initialized. "
  132. << "Make sure that you set one via CResourceLoaderFactory::setInstance";
  133. throw std::runtime_error(string.str());
  134. }
  135. }
  136. //void CResourceLoaderFactory::setInstance(CResourceLoader * resourceLoader)
  137. //{
  138. // CResourceLoaderFactory::resourceLoader = resourceLoader;
  139. //}
  140. ResourceLocator::ResourceLocator(ISimpleResourceLoader * loader, const std::string & resourceName)
  141. : loader(loader), resourceName(resourceName)
  142. {
  143. }
  144. ISimpleResourceLoader * ResourceLocator::getLoader() const
  145. {
  146. return loader;
  147. }
  148. std::string ResourceLocator::getResourceName() const
  149. {
  150. return resourceName;
  151. }
  152. EResType::Type EResTypeHelper::getTypeFromExtension(std::string extension)
  153. {
  154. boost::to_upper(extension);
  155. static const std::map<std::string, EResType::Type> stringToRes =
  156. boost::assign::map_list_of
  157. (".TXT", EResType::TEXT)
  158. (".JSON", EResType::TEXT)
  159. (".DEF", EResType::ANIMATION)
  160. (".MSK", EResType::MASK)
  161. (".MSG", EResType::MASK)
  162. (".H3C", EResType::CAMPAIGN)
  163. (".H3M", EResType::MAP)
  164. (".FNT", EResType::FONT)
  165. (".BMP", EResType::IMAGE)
  166. (".JPG", EResType::IMAGE)
  167. (".PCX", EResType::IMAGE)
  168. (".PNG", EResType::IMAGE)
  169. (".TGA", EResType::IMAGE)
  170. (".WAV", EResType::SOUND)
  171. (".82M", EResType::SOUND)
  172. (".SMK", EResType::VIDEO)
  173. (".BIK", EResType::VIDEO)
  174. (".MJPG", EResType::VIDEO)
  175. (".MP3", EResType::MUSIC)
  176. (".OGG", EResType::MUSIC)
  177. (".LOD", EResType::ARCHIVE)
  178. (".PAC", EResType::ARCHIVE)
  179. (".VID", EResType::ARCHIVE)
  180. (".SND", EResType::ARCHIVE)
  181. (".VCGM1", EResType::CLIENT_SAVEGAME)
  182. (".VLGM1", EResType::LIB_SAVEGAME)
  183. (".VSGM1", EResType::SERVER_SAVEGAME);
  184. auto iter = stringToRes.find(extension);
  185. if (iter == stringToRes.end())
  186. return EResType::OTHER;
  187. return iter->second;
  188. }
  189. std::string EResTypeHelper::getEResTypeAsString(EResType::Type type)
  190. {
  191. #define MAP_ENUM(value) (EResType::value, #value)
  192. static const std::map<EResType::Type, std::string> stringToRes = boost::assign::map_list_of
  193. MAP_ENUM(TEXT)
  194. MAP_ENUM(ANIMATION)
  195. MAP_ENUM(MASK)
  196. MAP_ENUM(CAMPAIGN)
  197. MAP_ENUM(MAP)
  198. MAP_ENUM(FONT)
  199. MAP_ENUM(IMAGE)
  200. MAP_ENUM(VIDEO)
  201. MAP_ENUM(SOUND)
  202. MAP_ENUM(MUSIC)
  203. MAP_ENUM(ARCHIVE)
  204. MAP_ENUM(CLIENT_SAVEGAME)
  205. MAP_ENUM(LIB_SAVEGAME)
  206. MAP_ENUM(SERVER_SAVEGAME)
  207. MAP_ENUM(OTHER);
  208. #undef MAP_ENUM
  209. auto iter = stringToRes.find(type);
  210. assert(iter != stringToRes.end());
  211. return iter->second;
  212. }
  213. void CResourceHandler::initialize()
  214. {
  215. //temporary filesystem that will be used to initialize main one.
  216. //used to solve several case-sensivity issues like Mp3 vs MP3
  217. initialLoader = new CResourceLoader;
  218. resourceLoader = new CResourceLoader;
  219. auto rootDir = new CFilesystemLoader(GameConstants::DATA_DIR, 3);
  220. initialLoader->addLoader("GLOBAL/", rootDir);
  221. initialLoader->addLoader("ALL/", rootDir);
  222. auto userDir = rootDir;
  223. //add local directory to "ALL" but only if it differs from root dir (true for linux)
  224. if (GameConstants::DATA_DIR != GVCMIDirs.UserPath)
  225. {
  226. userDir = new CFilesystemLoader(GVCMIDirs.UserPath, 3);
  227. initialLoader->addLoader("ALL/", userDir);
  228. }
  229. //create "LOCAL" dir with current userDir (may be same as rootDir)
  230. initialLoader->addLoader("LOCAL/", userDir);
  231. //check for presence of "VCMI" mod. If found - add it to our initial FS
  232. std::string filename = initialLoader->getResourceName(ResourceID("ALL/MODS/VCMI"));
  233. if (!filename.empty())
  234. initialLoader->addLoader("ALL/", new CFilesystemLoader(filename, 2));
  235. }
  236. void CResourceHandler::loadFileSystem(const std::string fsConfigURI)
  237. {
  238. //TODO: better way to detect fs config.
  239. // right now it can be: global_dir/config/, local_dir/config, global/mods/vcmi/config, local/mods/vcmi/config
  240. auto fsConfigData = initialLoader->loadData(ResourceID(fsConfigURI, EResType::TEXT));
  241. const JsonNode fsConfig((char*)fsConfigData.first.get(), fsConfigData.second);
  242. BOOST_FOREACH(auto & mountPoint, fsConfig["filesystem"].Struct())
  243. {
  244. BOOST_FOREACH(auto & entry, mountPoint.second.Vector())
  245. {
  246. tlog5 << "loading resource at " << entry["path"].String() << ": ";
  247. std::string filename = initialLoader->getResourceName(ResourceID(entry["path"].String()));
  248. if (!filename.empty())
  249. {
  250. if (entry["type"].String() == "dir")
  251. {
  252. int depth = 16;
  253. if (!entry["depth"].isNull())
  254. depth = entry["depth"].Float();
  255. resourceLoader->addLoader(mountPoint.first, new CFilesystemLoader(filename, depth));
  256. }
  257. if (entry["type"].String() == "file")
  258. resourceLoader->addLoader(mountPoint.first, new CLodArchiveLoader(filename));
  259. tlog5 << "OK\n";
  260. }
  261. else
  262. tlog5 << "Not found\n";
  263. }
  264. }
  265. }
  266. void CResourceHandler::loadModsFilesystems()
  267. {
  268. #ifdef ENABLE_ERA_FILESYSTEM
  269. auto iterator = initialLoader->getIterator([](const ResourceID & ident) -> bool
  270. {
  271. std::string name = ident.getName();
  272. return ident.getType() == EResType::TEXT
  273. && std::count(name.begin(), name.end(), '/') == 3
  274. && boost::algorithm::starts_with(name, "ALL/MODS/")
  275. && boost::algorithm::ends_with(name, "FILESYSTEM");
  276. });
  277. while (iterator.hasNext())
  278. {
  279. tlog1 << "Found mod filesystem: " << iterator->getName() << "\n";
  280. loadFileSystem(iterator->getName());
  281. ++iterator;
  282. }
  283. #endif
  284. }