1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/lib/filesystem/Filesystem.cpp

265 lines
8.1 KiB
C++
Raw Normal View History

/*
* 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
*
*/
#include "StdInc.h"
#include "Filesystem.h"
#include "CArchiveLoader.h"
#include "CFilesystemLoader.h"
#include "AdapterLoaders.h"
#include "CZipLoader.h"
//For filesystem initialization
#include "../GameConstants.h"
#include "../VCMIDirs.h"
#include "../CStopWatch.h"
#include "../json/JsonNode.h"
#include "../modding/ModScope.h"
VCMI_LIB_NAMESPACE_BEGIN
std::map<std::string, ISimpleResourceLoader*> CResourceHandler::knownLoaders = std::map<std::string, ISimpleResourceLoader*>();
2018-04-28 10:56:01 +02:00
CResourceHandler CResourceHandler::globalResourceHandler;
CFilesystemGenerator::CFilesystemGenerator(std::string prefix, bool extractArchives):
filesystem(new CFilesystemList()),
2023-02-15 00:09:07 +02:00
prefix(std::move(prefix)),
2022-11-22 19:35:30 +02:00
extractArchives(extractArchives)
{
}
CFilesystemGenerator::TLoadFunctorMap CFilesystemGenerator::genFunctorMap()
{
TLoadFunctorMap map;
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);
return map;
}
void CFilesystemGenerator::loadConfig(const JsonNode & config)
{
2023-02-15 00:09:07 +02:00
for(const auto & mountPoint : config.Struct())
{
2023-02-15 00:09:07 +02:00
for(const auto & entry : mountPoint.second.Vector())
{
CStopWatch timer;
logGlobal->trace("\t\tLoading resource at %s%s", prefix, entry["path"].String());
auto map = genFunctorMap();
2016-11-25 20:51:27 +02:00
auto typeName = entry["type"].String();
auto functor = map.find(typeName);
if (functor != map.end())
{
functor->second(mountPoint.first, entry);
logGlobal->trace("Resource loaded in %d ms", timer.getDiff());
}
else
{
2016-11-25 20:51:27 +02:00
logGlobal->error("Unknown filesystem format: %s", typeName);
}
}
}
}
CFilesystemList * CFilesystemGenerator::getFilesystem()
{
return filesystem;
}
void CFilesystemGenerator::loadDirectory(const std::string &mountPoint, const JsonNode & config)
{
std::string URI = prefix + config["path"].String();
int depth = 16;
if (!config["depth"].isNull())
2023-02-15 00:09:07 +02:00
depth = static_cast<int>(config["depth"].Float());
ResourcePath resID(URI, EResType::DIRECTORY);
for(auto & loader : CResourceHandler::get("initial")->getResourcesWithName(resID))
{
auto filename = loader->getResourceName(resID);
filesystem->addLoader(new CFilesystemLoader(mountPoint, *filename, depth), false);
}
}
void CFilesystemGenerator::loadZipArchive(const std::string &mountPoint, const JsonNode & config)
{
std::string URI = prefix + config["path"].String();
auto filename = CResourceHandler::get("initial")->getResourceName(ResourcePath(URI, EResType::ARCHIVE_ZIP));
if (filename)
filesystem->addLoader(new CZipLoader(mountPoint, *filename), false);
}
template<EResType archiveType>
void CFilesystemGenerator::loadArchive(const std::string &mountPoint, const JsonNode & config)
{
std::string URI = prefix + config["path"].String();
auto filename = CResourceHandler::get("initial")->getResourceName(ResourcePath(URI, archiveType));
if (filename)
filesystem->addLoader(new CArchiveLoader(mountPoint, *filename, extractArchives), false);
}
void CFilesystemGenerator::loadJsonMap(const std::string &mountPoint, const JsonNode & config)
{
std::string URI = prefix + config["path"].String();
2023-09-04 12:03:15 +02:00
auto filename = CResourceHandler::get("initial")->getResourceName(JsonPath::builtin(URI));
if (filename)
{
2023-09-04 12:03:15 +02:00
auto configData = CResourceHandler::get("initial")->load(JsonPath::builtin(URI))->readAll();
const JsonNode configInitial(reinterpret_cast<std::byte *>(configData.first.get()), configData.second);
filesystem->addLoader(new CMappedFileLoader(mountPoint, configInitial), false);
}
}
ISimpleResourceLoader * CResourceHandler::createInitial()
{
//temporary filesystem that will be used to initialize main one.
//used to solve several case-sensivity issues like Mp3 vs MP3
2023-02-15 00:09:07 +02:00
auto * initialLoader = new CFilesystemList();
//recurse only into specific directories
2023-02-15 00:09:07 +02:00
auto recurseInDir = [&](const std::string & URI, int depth)
{
ResourcePath ID(URI, EResType::DIRECTORY);
for(auto & loader : initialLoader->getResourcesWithName(ID))
{
auto filename = loader->getResourceName(ID);
if (filename)
{
2023-02-15 00:09:07 +02:00
auto * dir = new CFilesystemLoader(URI + '/', *filename, depth, true);
initialLoader->addLoader(dir, false);
}
}
};
for (auto & path : VCMIDirs::get().dataPaths())
{
if (boost::filesystem::is_directory(path)) // some of system-provided paths may not exist
2023-08-06 21:39:39 +02:00
initialLoader->addLoader(new CFilesystemLoader("", path, 1, true), false);
}
initialLoader->addLoader(new CFilesystemLoader("", VCMIDirs::get().userDataPath(), 0, true), false);
recurseInDir("CONFIG", 0);// look for configs
recurseInDir("DATA", 0); // look for archives
recurseInDir("MODS", 64); // look for mods.
return initialLoader;
}
void CResourceHandler::initialize()
{
2022-09-18 14:47:49 +02:00
// Create tree-like structure that looks like this:
// root
// |
// |- initial
// |
// |- data
// | |-core
// | |-mod1
// | |-modN
// |
// |- local
// |-saves
// |-config
// when built as single process, server can be started multiple times
if (globalResourceHandler.rootLoader)
return;
2022-12-07 23:36:20 +02:00
globalResourceHandler.rootLoader = std::make_unique<CFilesystemList>();
2018-04-28 10:56:01 +02:00
knownLoaders["root"] = globalResourceHandler.rootLoader.get();
knownLoaders["saves"] = new CFilesystemLoader("SAVES/", VCMIDirs::get().userSavePath());
knownLoaders["config"] = new CFilesystemLoader("CONFIG/", VCMIDirs::get().userConfigPath());
2023-02-15 00:09:07 +02: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);
}
2023-03-11 00:57:55 +02:00
void CResourceHandler::destroy()
{
knownLoaders.clear();
globalResourceHandler.rootLoader.reset();
}
ISimpleResourceLoader * CResourceHandler::get()
{
return get("root");
}
2023-02-15 00:09:07 +02:00
ISimpleResourceLoader * CResourceHandler::get(const std::string & identifier)
{
return knownLoaders.at(identifier);
}
void CResourceHandler::load(const std::string &fsConfigURI, bool extractArchives)
{
2023-09-04 12:03:15 +02:00
auto fsConfigData = get("initial")->load(JsonPath::builtin(fsConfigURI))->readAll();
const JsonNode fsConfig(reinterpret_cast<std::byte *>(fsConfigData.first.get()), fsConfigData.second);
addFilesystem("data", ModScope::scopeBuiltin(), createFileSystem("", fsConfig["filesystem"], extractArchives));
}
2022-11-22 18:55:18 +02:00
void CResourceHandler::addFilesystem(const std::string & parent, const std::string & identifier, ISimpleResourceLoader * loader)
{
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;
}
2023-02-15 00:09:07 +02:00
auto * list = dynamic_cast<CFilesystemList *>(knownLoaders.at(parent));
assert(list);
list->addLoader(loader, false);
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;
2023-02-15 00:09:07 +02:00
auto * list = dynamic_cast<CFilesystemList *>(knownLoaders.at(parent));
2022-09-17 13:04:01 +02:00
assert(list);
list->removeLoader(knownLoaders[identifier]);
knownLoaders.erase(identifier);
return true;
}
ISimpleResourceLoader * CResourceHandler::createFileSystem(const std::string & prefix, const JsonNode &fsConfig, bool extractArchives)
{
CFilesystemGenerator generator(prefix, extractArchives);
generator.loadConfig(fsConfig);
return generator.getFilesystem();
}
VCMI_LIB_NAMESPACE_END