1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-20 20:23:03 +02:00
vcmi/lib/CConfigHandler.cpp

192 lines
4.9 KiB
C++
Raw Normal View History

/*
* CConfigHandler.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 "CConfigHandler.h"
#include "../lib/filesystem/Filesystem.h"
#include "../lib/GameConstants.h"
#include "../lib/VCMIDirs.h"
VCMI_LIB_NAMESPACE_BEGIN
SettingsStorage settings;
2023-09-21 21:27:06 +02:00
SettingsStorage persistentStorage;
2012-01-12 23:41:32 +03:00
template<typename Accessor>
SettingsStorage::NodeAccessor<Accessor>::NodeAccessor(SettingsStorage & _parent, std::vector<std::string> _path):
parent(_parent),
2023-03-13 23:26:44 +02:00
path(std::move(_path))
{
}
2012-01-12 23:41:32 +03:00
template<typename Accessor>
2023-03-13 23:26:44 +02:00
SettingsStorage::NodeAccessor<Accessor> SettingsStorage::NodeAccessor<Accessor>::operator[](const std::string & nextNode) const
{
std::vector<std::string> newPath = path;
newPath.push_back(nextNode);
return NodeAccessor(parent, newPath);
}
2012-01-12 23:41:32 +03:00
template<typename Accessor>
SettingsStorage::NodeAccessor<Accessor>::operator Accessor() const
{
return Accessor(parent, path);
}
2012-01-12 23:41:32 +03:00
template<typename Accessor>
SettingsStorage::NodeAccessor<Accessor> SettingsStorage::NodeAccessor<Accessor>::operator () (std::vector<std::string> _path) const
2012-01-12 23:41:32 +03:00
{
std::vector<std::string> newPath = path;
newPath.insert( newPath.end(), _path.begin(), _path.end());
return NodeAccessor(parent, newPath);
}
SettingsStorage::SettingsStorage():
2012-01-12 23:41:32 +03:00
write(NodeAccessor<Settings>(*this, std::vector<std::string>() )),
listen(NodeAccessor<SettingsListener>(*this, std::vector<std::string>() ))
{
}
2012-01-12 23:41:32 +03:00
2023-09-21 21:27:06 +02:00
void SettingsStorage::init(const std::string & dataFilename, const std::string & schema)
2012-01-12 23:41:32 +03:00
{
2023-09-21 21:27:06 +02:00
this->dataFilename = dataFilename;
this->schema = schema;
2023-09-21 21:27:06 +02:00
JsonPath confName = JsonPath::builtin(dataFilename);
JsonUtils::assembleFromFiles(confName.getOriginalName()).swap(config);
// Probably new install. Create config file to save settings to
if (!CResourceHandler::get("local")->existsResource(confName))
2023-09-21 21:27:06 +02:00
CResourceHandler::get("local")->createResource(dataFilename);
2023-09-21 23:41:00 +02:00
if(!schema.empty())
{
2023-09-21 21:27:06 +02:00
JsonUtils::maximize(config, schema);
JsonUtils::validate(config, schema, "settings");
}
2012-01-12 23:41:32 +03:00
}
void SettingsStorage::invalidateNode(const std::vector<std::string> &changedPath)
{
for(SettingsListener * listener : listeners)
2012-01-12 23:41:32 +03:00
listener->nodeInvalidated(changedPath);
JsonNode savedConf = config;
savedConf.Struct().erase("session");
2023-09-21 23:41:00 +02:00
if(!schema.empty())
2023-09-21 21:27:06 +02:00
JsonUtils::minimize(savedConf, schema);
2012-01-12 23:41:32 +03:00
2023-09-21 21:27:06 +02:00
std::fstream file(CResourceHandler::get()->getResourceName(JsonPath::builtin(dataFilename))->c_str(), std::ofstream::out | std::ofstream::trunc);
file << savedConf.toJson();
}
2023-03-13 23:26:44 +02:00
JsonNode & SettingsStorage::getNode(const std::vector<std::string> & path)
{
JsonNode *node = &config;
2023-03-13 23:26:44 +02:00
for(const std::string & value : path)
node = &(*node)[value];
return *node;
}
2023-03-13 23:26:44 +02:00
Settings SettingsStorage::get(const std::vector<std::string> & path)
{
return Settings(*this, path);
}
2023-03-13 23:26:44 +02:00
const JsonNode & SettingsStorage::operator[](const std::string & value) const
{
return config[value];
}
const JsonNode & SettingsStorage::toJsonNode() const
{
return config;
}
2023-03-13 23:26:44 +02:00
SettingsListener::SettingsListener(SettingsStorage & _parent, std::vector<std::string> _path):
parent(_parent),
2023-03-13 23:26:44 +02:00
path(std::move(_path))
{
parent.listeners.insert(this);
}
2012-09-06 13:26:18 +03:00
SettingsListener::SettingsListener(const SettingsListener &sl):
parent(sl.parent),
path(sl.path),
callback(sl.callback)
{
parent.listeners.insert(this);
}
SettingsListener::~SettingsListener()
{
parent.listeners.erase(this);
}
void SettingsListener::nodeInvalidated(const std::vector<std::string> &changedPath)
2012-01-12 23:41:32 +03:00
{
if (!callback)
return;
size_t min = std::min(path.size(), changedPath.size());
size_t mismatch = std::mismatch(path.begin(), path.begin()+min, changedPath.begin()).first - path.begin();
if (min == mismatch)
callback(parent.getNode(path));
2012-01-12 23:41:32 +03:00
}
void SettingsListener::operator() (std::function<void(const JsonNode&)> _callback)
2012-01-12 23:41:32 +03:00
{
2023-03-13 23:26:44 +02:00
callback = std::move(_callback);
}
Settings::Settings(SettingsStorage &_parent, const std::vector<std::string> &_path):
parent(_parent),
path(_path),
node(_parent.getNode(_path)),
copy(_parent.getNode(_path))
{
}
Settings::~Settings()
{
if (node != copy)
parent.invalidateNode(path);
}
JsonNode* Settings::operator -> ()
{
return &node;
}
const JsonNode* Settings::operator ->() const
{
return &node;
}
2023-03-13 23:26:44 +02:00
const JsonNode & Settings::operator[](const std::string & value) const
{
return node[value];
}
2023-03-13 23:26:44 +02:00
JsonNode & Settings::operator[](const std::string & value)
{
return node[value];
}
// Force instantiation of the SettingsStorage::NodeAccessor class template.
// That way method definitions can sit in the cpp file
template struct SettingsStorage::NodeAccessor<SettingsListener>;
template struct SettingsStorage::NodeAccessor<Settings>;
VCMI_LIB_NAMESPACE_END