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 "../GameConstants.h"
|
|
|
|
#include "../VCMIDirs.h"
|
|
|
|
#include "../CStopWatch.h"
|
2024-02-11 23:09:01 +02:00
|
|
|
#include "../json/JsonNode.h"
|
2023-07-30 19:12:25 +02:00
|
|
|
#include "../modding/ModScope.h"
|
2013-07-28 17:49:50 +03:00
|
|
|
|
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()),
|
2023-02-15 00:09:07 +02:00
|
|
|
prefix(std::move(prefix)),
|
2022-11-22 19:35:30 +02:00
|
|
|
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
|
|
|
{
|
2023-02-15 00:09:07 +02:00
|
|
|
for(const auto & mountPoint : config.Struct())
|
2013-11-08 23:36:26 +03:00
|
|
|
{
|
2023-02-15 00:09:07 +02:00
|
|
|
for(const auto & entry : mountPoint.second.Vector())
|
2013-11-08 23:36:26 +03:00
|
|
|
{
|
|
|
|
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())
|
2023-02-15 00:09:07 +02:00
|
|
|
depth = static_cast<int>(config["depth"].Float());
|
2013-07-28 17:49:50 +03:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
ResourcePath 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();
|
2023-08-23 14:07:50 +02:00
|
|
|
auto filename = CResourceHandler::get("initial")->getResourceName(ResourcePath(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
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
template<EResType archiveType>
|
2013-11-08 23:36:26 +03:00
|
|
|
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();
|
2023-08-23 14:07:50 +02:00
|
|
|
auto filename = CResourceHandler::get("initial")->getResourceName(ResourcePath(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();
|
2023-09-04 12:03:15 +02:00
|
|
|
auto filename = CResourceHandler::get("initial")->getResourceName(JsonPath::builtin(URI));
|
2013-11-08 23:36:26 +03:00
|
|
|
if (filename)
|
|
|
|
{
|
2023-09-04 12:03:15 +02:00
|
|
|
auto configData = CResourceHandler::get("initial")->load(JsonPath::builtin(URI))->readAll();
|
2024-07-17 13:07:57 +02:00
|
|
|
const JsonNode configInitial(reinterpret_cast<std::byte *>(configData.first.get()), configData.second, URI);
|
2018-10-29 17:56:14 +02:00
|
|
|
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
|
2023-02-15 00:09:07 +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
|
2023-02-15 00:09:07 +02:00
|
|
|
auto recurseInDir = [&](const std::string & URI, int depth)
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
ResourcePath ID(URI, EResType::DIRECTORY);
|
2013-07-28 17:49:50 +03:00
|
|
|
|
|
|
|
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);
|
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
|
2023-08-06 21:39:39 +02:00
|
|
|
initialLoader->addLoader(new CFilesystemLoader("", path, 1, 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;
|
|
|
|
|
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();
|
2014-08-21 23:26:28 +03:00
|
|
|
knownLoaders["saves"] = new CFilesystemLoader("SAVES/", VCMIDirs::get().userSavePath());
|
|
|
|
knownLoaders["config"] = new CFilesystemLoader("CONFIG/", VCMIDirs::get().userConfigPath());
|
2024-08-21 23:01:32 +02:00
|
|
|
|
|
|
|
if(boost::filesystem::is_directory(VCMIDirs::get().userDataPath() / "Generated"))
|
|
|
|
boost::filesystem::remove_all(VCMIDirs::get().userDataPath() / "Generated");
|
2024-08-19 23:18:14 +02:00
|
|
|
knownLoaders["gen_data"] = new CFilesystemLoader("DATA/", VCMIDirs::get().userDataPath() / "Generated" / "Data");
|
|
|
|
knownLoaders["gen_sprites"] = new CFilesystemLoader("SPRITES/", VCMIDirs::get().userDataPath() / "Generated" / "Sprites");
|
2014-03-08 19:05:23 +03:00
|
|
|
|
2023-02-15 00:09:07 +02:00
|
|
|
auto * localFS = new CFilesystemList();
|
2014-03-08 19:05:23 +03:00
|
|
|
localFS->addLoader(knownLoaders["saves"], true);
|
|
|
|
localFS->addLoader(knownLoaders["config"], true);
|
2024-08-19 23:18:14 +02:00
|
|
|
localFS->addLoader(knownLoaders["gen_data"], true);
|
|
|
|
localFS->addLoader(knownLoaders["gen_sprites"], true);
|
2014-03-08 19:05:23 +03:00
|
|
|
|
|
|
|
addFilesystem("root", "initial", createInitial());
|
|
|
|
addFilesystem("root", "data", new CFilesystemList());
|
|
|
|
addFilesystem("root", "local", localFS);
|
2013-07-30 18:02:55 +03:00
|
|
|
}
|
|
|
|
|
2023-03-11 00:57:55 +02:00
|
|
|
void CResourceHandler::destroy()
|
|
|
|
{
|
|
|
|
knownLoaders.clear();
|
|
|
|
globalResourceHandler.rootLoader.reset();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-02-15 00:09:07 +02:00
|
|
|
ISimpleResourceLoader * CResourceHandler::get(const std::string & identifier)
|
2013-11-11 23:11:51 +03:00
|
|
|
{
|
2024-11-09 22:29:07 +02:00
|
|
|
assert(knownLoaders.count(identifier));
|
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
|
|
|
{
|
2023-09-04 12:03:15 +02:00
|
|
|
auto fsConfigData = get("initial")->load(JsonPath::builtin(fsConfigURI))->readAll();
|
2013-07-28 17:49:50 +03:00
|
|
|
|
2024-07-17 13:07:57 +02:00
|
|
|
const JsonNode fsConfig(reinterpret_cast<std::byte *>(fsConfigData.first.get()), fsConfigData.second, fsConfigURI);
|
2013-07-28 17:49:50 +03:00
|
|
|
|
2023-07-30 19:12:25 +02:00
|
|
|
addFilesystem("data", ModScope::scopeBuiltin(), 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
|
|
|
|
2023-02-15 00:09:07 +02:00
|
|
|
auto * list = dynamic_cast<CFilesystemList *>(knownLoaders.at(parent));
|
2014-03-08 19:05:23 +03:00
|
|
|
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;
|
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;
|
|
|
|
}
|
|
|
|
|
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
|