2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* Filesystem.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2013-07-28 17:49:50 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "Filesystem.h"
|
|
|
|
|
|
|
|
#include "CArchiveLoader.h"
|
|
|
|
#include "CFilesystemLoader.h"
|
|
|
|
#include "AdapterLoaders.h"
|
2013-07-30 18:02:55 +03:00
|
|
|
#include "CZipLoader.h"
|
2013-07-28 17:49:50 +03:00
|
|
|
|
|
|
|
//For filesystem initialization
|
|
|
|
#include "../JsonNode.h"
|
|
|
|
#include "../GameConstants.h"
|
|
|
|
#include "../VCMIDirs.h"
|
|
|
|
#include "../CStopWatch.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2013-12-11 20:12:39 +03:00
|
|
|
std::map<std::string, ISimpleResourceLoader*> CResourceHandler::knownLoaders = std::map<std::string, ISimpleResourceLoader*>();
|
2018-04-28 10:56:01 +02:00
|
|
|
CResourceHandler CResourceHandler::globalResourceHandler;
|
2013-07-28 17:49:50 +03:00
|
|
|
|
2022-11-20 18:48:31 +02:00
|
|
|
CFilesystemGenerator::CFilesystemGenerator(std::string prefix, bool extractArchives):
|
2013-11-08 23:36:26 +03:00
|
|
|
filesystem(new CFilesystemList()),
|
2022-11-22 19:35:30 +02:00
|
|
|
prefix(prefix),
|
|
|
|
extractArchives(extractArchives)
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
CFilesystemGenerator::TLoadFunctorMap CFilesystemGenerator::genFunctorMap()
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2013-11-08 23:36:26 +03:00
|
|
|
TLoadFunctorMap map;
|
2014-08-04 21:33:59 +03:00
|
|
|
map["map"] = std::bind(&CFilesystemGenerator::loadJsonMap, this, _1, _2);
|
|
|
|
map["dir"] = std::bind(&CFilesystemGenerator::loadDirectory, this, _1, _2);
|
|
|
|
map["lod"] = std::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_LOD>, this, _1, _2);
|
|
|
|
map["snd"] = std::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_SND>, this, _1, _2);
|
|
|
|
map["vid"] = std::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_VID>, this, _1, _2);
|
|
|
|
map["zip"] = std::bind(&CFilesystemGenerator::loadZipArchive, this, _1, _2);
|
2013-11-08 23:36:26 +03:00
|
|
|
return map;
|
2013-07-28 17:49:50 +03:00
|
|
|
}
|
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
void CFilesystemGenerator::loadConfig(const JsonNode & config)
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2013-11-08 23:36:26 +03:00
|
|
|
for(auto & mountPoint : config.Struct())
|
|
|
|
{
|
|
|
|
for(auto & entry : mountPoint.second.Vector())
|
|
|
|
{
|
|
|
|
CStopWatch timer;
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->trace("\t\tLoading resource at %s%s", prefix, entry["path"].String());
|
2013-07-28 17:49:50 +03:00
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
auto map = genFunctorMap();
|
2016-11-25 20:51:27 +02:00
|
|
|
auto typeName = entry["type"].String();
|
|
|
|
auto functor = map.find(typeName);
|
2013-11-08 23:36:26 +03:00
|
|
|
|
|
|
|
if (functor != map.end())
|
|
|
|
{
|
|
|
|
functor->second(mountPoint.first, entry);
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->trace("Resource loaded in %d ms", timer.getDiff());
|
2013-11-08 23:36:26 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-25 20:51:27 +02:00
|
|
|
logGlobal->error("Unknown filesystem format: %s", typeName);
|
2013-11-08 23:36:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-28 17:49:50 +03:00
|
|
|
}
|
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
CFilesystemList * CFilesystemGenerator::getFilesystem()
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2013-11-08 23:36:26 +03:00
|
|
|
return filesystem;
|
2013-07-28 17:49:50 +03:00
|
|
|
}
|
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
void CFilesystemGenerator::loadDirectory(const std::string &mountPoint, const JsonNode & config)
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2013-11-08 23:36:26 +03:00
|
|
|
std::string URI = prefix + config["path"].String();
|
|
|
|
int depth = 16;
|
|
|
|
if (!config["depth"].isNull())
|
2014-08-11 00:42:39 +03:00
|
|
|
depth = (int)config["depth"].Float();
|
2013-07-28 17:49:50 +03:00
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
ResourceID resID(URI, EResType::DIRECTORY);
|
2013-07-28 17:49:50 +03:00
|
|
|
|
2014-03-08 19:05:23 +03:00
|
|
|
for(auto & loader : CResourceHandler::get("initial")->getResourcesWithName(resID))
|
2013-11-08 23:36:26 +03:00
|
|
|
{
|
|
|
|
auto filename = loader->getResourceName(resID);
|
|
|
|
filesystem->addLoader(new CFilesystemLoader(mountPoint, *filename, depth), false);
|
|
|
|
}
|
2013-07-28 17:49:50 +03:00
|
|
|
}
|
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
void CFilesystemGenerator::loadZipArchive(const std::string &mountPoint, const JsonNode & config)
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2013-11-08 23:36:26 +03:00
|
|
|
std::string URI = prefix + config["path"].String();
|
2014-03-08 19:05:23 +03:00
|
|
|
auto filename = CResourceHandler::get("initial")->getResourceName(ResourceID(URI, EResType::ARCHIVE_ZIP));
|
2013-11-08 23:36:26 +03:00
|
|
|
if (filename)
|
|
|
|
filesystem->addLoader(new CZipLoader(mountPoint, *filename), false);
|
2013-07-28 17:49:50 +03:00
|
|
|
}
|
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
template<EResType::Type archiveType>
|
|
|
|
void CFilesystemGenerator::loadArchive(const std::string &mountPoint, const JsonNode & config)
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2013-11-08 23:36:26 +03:00
|
|
|
std::string URI = prefix + config["path"].String();
|
2014-03-08 19:05:23 +03:00
|
|
|
auto filename = CResourceHandler::get("initial")->getResourceName(ResourceID(URI, archiveType));
|
2013-11-08 23:36:26 +03:00
|
|
|
if (filename)
|
2022-11-20 18:48:31 +02:00
|
|
|
filesystem->addLoader(new CArchiveLoader(mountPoint, *filename, extractArchives), false);
|
2013-07-28 17:49:50 +03:00
|
|
|
}
|
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
void CFilesystemGenerator::loadJsonMap(const std::string &mountPoint, const JsonNode & config)
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2013-11-08 23:36:26 +03:00
|
|
|
std::string URI = prefix + config["path"].String();
|
2014-03-08 19:05:23 +03:00
|
|
|
auto filename = CResourceHandler::get("initial")->getResourceName(ResourceID(URI, EResType::TEXT));
|
2013-11-08 23:36:26 +03:00
|
|
|
if (filename)
|
|
|
|
{
|
2014-03-08 19:05:23 +03:00
|
|
|
auto configData = CResourceHandler::get("initial")->load(ResourceID(URI, EResType::TEXT))->readAll();
|
2018-10-29 17:56:14 +02:00
|
|
|
const JsonNode configInitial((char*)configData.first.get(), configData.second);
|
|
|
|
filesystem->addLoader(new CMappedFileLoader(mountPoint, configInitial), false);
|
2013-11-08 23:36:26 +03:00
|
|
|
}
|
2013-07-28 17:49:50 +03:00
|
|
|
}
|
|
|
|
|
2014-03-08 19:05:23 +03:00
|
|
|
ISimpleResourceLoader * CResourceHandler::createInitial()
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2014-03-08 19:05:23 +03:00
|
|
|
//temporary filesystem that will be used to initialize main one.
|
|
|
|
//used to solve several case-sensivity issues like Mp3 vs MP3
|
2017-07-16 11:58:05 +02:00
|
|
|
auto initialLoader = new CFilesystemList();
|
2014-03-08 19:05:23 +03:00
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
//recurse only into specific directories
|
2014-03-08 19:05:23 +03:00
|
|
|
auto recurseInDir = [&](std::string URI, int depth)
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
|
|
|
ResourceID ID(URI, EResType::DIRECTORY);
|
|
|
|
|
|
|
|
for(auto & loader : initialLoader->getResourcesWithName(ID))
|
|
|
|
{
|
|
|
|
auto filename = loader->getResourceName(ID);
|
|
|
|
if (filename)
|
|
|
|
{
|
2014-08-21 23:26:28 +03:00
|
|
|
auto dir = new CFilesystemLoader(URI + '/', *filename, depth, true);
|
2013-07-28 17:49:50 +03:00
|
|
|
initialLoader->addLoader(dir, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto & path : VCMIDirs::get().dataPaths())
|
2014-03-04 17:51:10 +03:00
|
|
|
{
|
|
|
|
if (boost::filesystem::is_directory(path)) // some of system-provided paths may not exist
|
2014-08-21 23:26:28 +03:00
|
|
|
initialLoader->addLoader(new CFilesystemLoader("", path, 0, true), false);
|
2014-03-04 17:51:10 +03:00
|
|
|
}
|
2014-08-21 23:26:28 +03:00
|
|
|
initialLoader->addLoader(new CFilesystemLoader("", VCMIDirs::get().userDataPath(), 0, true), false);
|
2013-07-28 17:49:50 +03:00
|
|
|
|
|
|
|
recurseInDir("CONFIG", 0);// look for configs
|
|
|
|
recurseInDir("DATA", 0); // look for archives
|
2014-03-08 19:05:23 +03:00
|
|
|
recurseInDir("MODS", 64); // look for mods.
|
|
|
|
|
|
|
|
return initialLoader;
|
2013-07-28 17:49:50 +03:00
|
|
|
}
|
|
|
|
|
2014-03-08 19:05:23 +03:00
|
|
|
void CResourceHandler::initialize()
|
2013-07-30 18:02:55 +03:00
|
|
|
{
|
2022-09-18 14:47:49 +02:00
|
|
|
// Create tree-like structure that looks like this:
|
2014-03-08 19:05:23 +03:00
|
|
|
// root
|
|
|
|
// |
|
|
|
|
// |- initial
|
|
|
|
// |
|
|
|
|
// |- data
|
|
|
|
// | |-core
|
|
|
|
// | |-mod1
|
|
|
|
// | |-modN
|
|
|
|
// |
|
|
|
|
// |- local
|
|
|
|
// |-saves
|
|
|
|
// |-config
|
|
|
|
|
2022-08-15 11:20:38 +02:00
|
|
|
// when built as single process, server can be started multiple times
|
|
|
|
if (globalResourceHandler.rootLoader)
|
|
|
|
return;
|
|
|
|
|
2018-04-28 10:56:01 +02:00
|
|
|
globalResourceHandler.rootLoader = vstd::make_unique<CFilesystemList>();
|
|
|
|
knownLoaders["root"] = globalResourceHandler.rootLoader.get();
|
2014-08-21 23:26:28 +03:00
|
|
|
knownLoaders["saves"] = new CFilesystemLoader("SAVES/", VCMIDirs::get().userSavePath());
|
|
|
|
knownLoaders["config"] = new CFilesystemLoader("CONFIG/", VCMIDirs::get().userConfigPath());
|
2014-03-08 19:05:23 +03:00
|
|
|
|
|
|
|
auto localFS = new CFilesystemList();
|
|
|
|
localFS->addLoader(knownLoaders["saves"], true);
|
|
|
|
localFS->addLoader(knownLoaders["config"], true);
|
|
|
|
|
|
|
|
addFilesystem("root", "initial", createInitial());
|
|
|
|
addFilesystem("root", "data", new CFilesystemList());
|
|
|
|
addFilesystem("root", "local", localFS);
|
2013-07-30 18:02:55 +03:00
|
|
|
}
|
|
|
|
|
2014-03-08 19:05:23 +03:00
|
|
|
ISimpleResourceLoader * CResourceHandler::get()
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2014-03-08 19:05:23 +03:00
|
|
|
return get("root");
|
2013-07-28 17:49:50 +03:00
|
|
|
}
|
|
|
|
|
2014-03-08 19:05:23 +03:00
|
|
|
ISimpleResourceLoader * CResourceHandler::get(std::string identifier)
|
2013-11-11 23:11:51 +03:00
|
|
|
{
|
2014-03-08 19:05:23 +03:00
|
|
|
return knownLoaders.at(identifier);
|
2013-11-11 23:11:51 +03:00
|
|
|
}
|
|
|
|
|
2022-11-20 18:48:31 +02:00
|
|
|
void CResourceHandler::load(const std::string &fsConfigURI, bool extractArchives)
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2014-03-08 19:05:23 +03:00
|
|
|
auto fsConfigData = get("initial")->load(ResourceID(fsConfigURI, EResType::TEXT))->readAll();
|
2013-07-28 17:49:50 +03:00
|
|
|
|
|
|
|
const JsonNode fsConfig((char*)fsConfigData.first.get(), fsConfigData.second);
|
|
|
|
|
2022-11-20 18:48:31 +02:00
|
|
|
addFilesystem("data", "core", createFileSystem("", fsConfig["filesystem"], extractArchives));
|
2013-07-28 17:49:50 +03:00
|
|
|
}
|
|
|
|
|
2022-11-22 18:55:18 +02:00
|
|
|
void CResourceHandler::addFilesystem(const std::string & parent, const std::string & identifier, ISimpleResourceLoader * loader)
|
2013-12-11 20:12:39 +03:00
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
if(knownLoaders.count(identifier) != 0)
|
|
|
|
{
|
|
|
|
logMod->error("[CRITICAL] Virtual filesystem %s already loaded!", identifier);
|
|
|
|
delete loader;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(knownLoaders.count(parent) == 0)
|
|
|
|
{
|
|
|
|
logMod->error("[CRITICAL] Parent virtual filesystem %s for %s not found!", parent, identifier);
|
|
|
|
delete loader;
|
|
|
|
return;
|
|
|
|
}
|
2014-03-08 19:05:23 +03:00
|
|
|
|
|
|
|
auto list = dynamic_cast<CFilesystemList *>(knownLoaders.at(parent));
|
|
|
|
assert(list);
|
|
|
|
list->addLoader(loader, false);
|
2013-12-11 20:12:39 +03:00
|
|
|
knownLoaders[identifier] = loader;
|
|
|
|
}
|
|
|
|
|
2022-09-17 13:04:01 +02:00
|
|
|
bool CResourceHandler::removeFilesystem(const std::string & parent, const std::string & identifier)
|
|
|
|
{
|
|
|
|
if(knownLoaders.count(identifier) == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(knownLoaders.count(parent) == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto list = dynamic_cast<CFilesystemList *>(knownLoaders.at(parent));
|
|
|
|
assert(list);
|
|
|
|
list->removeLoader(knownLoaders[identifier]);
|
|
|
|
knownLoaders.erase(identifier);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-20 18:48:31 +02:00
|
|
|
ISimpleResourceLoader * CResourceHandler::createFileSystem(const std::string & prefix, const JsonNode &fsConfig, bool extractArchives)
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2022-11-20 18:48:31 +02:00
|
|
|
CFilesystemGenerator generator(prefix, extractArchives);
|
2013-11-08 23:36:26 +03:00
|
|
|
generator.loadConfig(fsConfig);
|
|
|
|
return generator.getFilesystem();
|
2013-07-28 17:49:50 +03:00
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|