2013-07-28 17:49:50 +03:00
|
|
|
/*
|
|
|
|
* Filesystem.h, 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
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#pragma once
|
2013-07-28 17:49:50 +03:00
|
|
|
|
|
|
|
#include "CInputStream.h"
|
2013-12-11 20:12:39 +03:00
|
|
|
#include "ISimpleResourceLoader.h"
|
2023-08-23 14:07:50 +02:00
|
|
|
#include "ResourcePath.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
|
|
|
class CFilesystemList;
|
2013-07-28 17:49:50 +03:00
|
|
|
class JsonNode;
|
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
/// Helper class that allows generation of a ISimpleResourceLoader entry out of Json config(s)
|
|
|
|
class DLL_LINKAGE CFilesystemGenerator
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2023-04-17 23:11:16 +02:00
|
|
|
using TLoadFunctor = std::function<void(const std::string &, const JsonNode &)>;
|
|
|
|
using TLoadFunctorMap = std::map<std::string, TLoadFunctor>;
|
2013-07-28 17:49:50 +03:00
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
CFilesystemList * filesystem;
|
|
|
|
std::string prefix;
|
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 loadArchive(const std::string & mountPoint, const JsonNode & config);
|
|
|
|
void loadDirectory(const std::string & mountPoint, const JsonNode & config);
|
|
|
|
void loadZipArchive(const std::string & mountPoint, const JsonNode & config);
|
|
|
|
void loadJsonMap(const std::string & mountPoint, const JsonNode & config);
|
2013-07-28 17:49:50 +03:00
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
TLoadFunctorMap genFunctorMap();
|
|
|
|
public:
|
|
|
|
/// prefix = prefix that will be given to file entries in all nodes of this filesystem
|
2022-11-20 18:48:31 +02:00
|
|
|
/// extractArchives = Specifies if Original H3 archives should be extracted to a separate folder
|
|
|
|
CFilesystemGenerator(std::string prefix, bool extractArchives = false);
|
2013-07-28 17:49:50 +03:00
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
/// loads configuration from json
|
|
|
|
/// config - configuration to load, using format of "filesystem" entry in config/filesystem.json
|
|
|
|
void loadConfig(const JsonNode & config);
|
2013-07-28 17:49:50 +03:00
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
/// returns generated filesystem
|
|
|
|
CFilesystemList * getFilesystem();
|
2022-11-20 18:48:31 +02:00
|
|
|
|
|
|
|
/** Specifies if Original H3 archives should be extracted to a separate folder **/
|
|
|
|
bool extractArchives;
|
2013-07-28 17:49:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class has static methods for a global resource loader access.
|
|
|
|
*
|
2013-11-08 23:36:26 +03:00
|
|
|
* Class is not thread-safe.
|
2013-07-28 17:49:50 +03:00
|
|
|
*/
|
|
|
|
class DLL_LINKAGE CResourceHandler
|
|
|
|
{
|
2014-03-08 19:05:23 +03:00
|
|
|
/**
|
|
|
|
* @brief createInitial - creates instance of initial loader
|
|
|
|
* that contains data necessary to load main FS
|
|
|
|
*/
|
|
|
|
static ISimpleResourceLoader * createInitial();
|
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Gets an instance of resource loader.
|
|
|
|
*
|
|
|
|
* Make sure that you've set an instance before using it. It'll throw an exception if no instance was set.
|
|
|
|
*
|
|
|
|
* @return Returns an instance of resource loader.
|
|
|
|
*/
|
2013-12-11 20:12:39 +03:00
|
|
|
static ISimpleResourceLoader * get();
|
2023-02-15 00:09:07 +02:00
|
|
|
static ISimpleResourceLoader * get(const std::string & identifier);
|
2013-07-28 17:49:50 +03:00
|
|
|
|
|
|
|
/**
|
2013-11-08 23:36:26 +03:00
|
|
|
* Creates instance of initial resource loader.
|
2013-07-28 17:49:50 +03:00
|
|
|
* Will not fill filesystem with data
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void initialize();
|
|
|
|
|
2023-03-11 00:57:55 +02:00
|
|
|
/**
|
|
|
|
* Destroys all existing data in filesystem, bringing it into uninitialized state
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void destroy();
|
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
/**
|
2013-11-08 23:36:26 +03:00
|
|
|
* Will load all filesystem data from Json data at this path (normally - config/filesystem.json)
|
|
|
|
* @param fsConfigURI - URI from which data will be loaded
|
|
|
|
*/
|
2022-11-20 18:48:31 +02:00
|
|
|
static void load(const std::string & fsConfigURI, bool extractArchives = false);
|
2013-11-08 23:36:26 +03:00
|
|
|
|
2013-12-11 20:12:39 +03:00
|
|
|
/**
|
|
|
|
* @brief addFilesystem adds filesystem into global resource loader
|
|
|
|
* @param identifier name of this loader by which it can be retrieved later
|
|
|
|
* @param loader resource loader to add
|
|
|
|
*/
|
2022-11-22 18:55:18 +02:00
|
|
|
static void addFilesystem(const std::string & parent, const std::string & identifier, ISimpleResourceLoader * loader);
|
2022-09-17 13:04:01 +02:00
|
|
|
|
|
|
|
/**
|
2024-06-24 03:23:26 +02:00
|
|
|
* @brief removeFilesystem removes previously added filesystem from global resource holder
|
2022-09-17 13:04:01 +02:00
|
|
|
* @param parent parent loader containing filesystem
|
|
|
|
* @param identifier name of this loader
|
|
|
|
* @return if filesystem was successfully removed
|
|
|
|
*/
|
|
|
|
static bool removeFilesystem(const std::string & parent, const std::string & identifier);
|
2013-12-11 20:12:39 +03:00
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
/**
|
|
|
|
* @brief createModFileSystem - creates filesystem out of config file
|
2013-07-28 17:49:50 +03:00
|
|
|
* @param prefix - prefix for all paths in filesystem config
|
2013-11-08 23:36:26 +03:00
|
|
|
* @param fsConfig - configuration to load
|
|
|
|
* @return generated filesystem that contains all config entries
|
2013-07-28 17:49:50 +03:00
|
|
|
*/
|
2022-11-20 18:48:31 +02:00
|
|
|
static ISimpleResourceLoader * createFileSystem(const std::string &prefix, const JsonNode & fsConfig, bool extractArchives = false);
|
2013-07-28 17:49:50 +03:00
|
|
|
|
2018-04-28 10:56:01 +02:00
|
|
|
~CResourceHandler() = default;
|
2013-07-28 17:49:50 +03:00
|
|
|
private:
|
|
|
|
/** Instance of resource loader */
|
2013-12-11 20:12:39 +03:00
|
|
|
static std::map<std::string, ISimpleResourceLoader*> knownLoaders;
|
2018-04-28 10:56:01 +02:00
|
|
|
static CResourceHandler globalResourceHandler;
|
|
|
|
|
|
|
|
CResourceHandler() {};
|
|
|
|
std::unique_ptr<ISimpleResourceLoader> rootLoader;
|
2013-07-28 17:49:50 +03:00
|
|
|
};
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|