2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CConfigHandler.h"
|
|
|
|
|
|
|
|
#include "../lib/filesystem/Filesystem.h"
|
|
|
|
#include "../lib/filesystem/FileStream.h"
|
|
|
|
#include "../lib/GameConstants.h"
|
|
|
|
#include "../lib/VCMIDirs.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
SettingsStorage settings;
|
2012-01-12 23:41:32 +03:00
|
|
|
|
2012-01-12 18:23:00 +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 18:23:00 +03:00
|
|
|
{
|
|
|
|
}
|
2012-01-12 23:41:32 +03:00
|
|
|
|
2012-01-12 18:23:00 +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
|
2012-01-12 18:23:00 +03:00
|
|
|
{
|
|
|
|
std::vector<std::string> newPath = path;
|
|
|
|
newPath.push_back(nextNode);
|
|
|
|
return NodeAccessor(parent, newPath);
|
|
|
|
}
|
2012-01-12 23:41:32 +03:00
|
|
|
|
2012-01-12 18:23:00 +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>
|
2018-04-03 03:37:09 +02:00
|
|
|
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);
|
|
|
|
}
|
2012-01-12 18:23:00 +03:00
|
|
|
|
|
|
|
SettingsStorage::SettingsStorage():
|
2012-01-12 23:41:32 +03:00
|
|
|
write(NodeAccessor<Settings>(*this, std::vector<std::string>() )),
|
2018-07-16 22:05:42 +02:00
|
|
|
listen(NodeAccessor<SettingsListener>(*this, std::vector<std::string>() ))
|
2012-01-12 18:23:00 +03:00
|
|
|
{
|
|
|
|
}
|
2012-01-12 23:41:32 +03:00
|
|
|
|
2018-07-16 22:05:42 +02:00
|
|
|
void SettingsStorage::init()
|
2012-01-12 23:41:32 +03:00
|
|
|
{
|
2013-04-03 00:39:32 +03:00
|
|
|
std::string confName = "config/settings.json";
|
|
|
|
|
2014-03-08 19:05:23 +03:00
|
|
|
JsonUtils::assembleFromFiles(confName).swap(config);
|
|
|
|
|
|
|
|
// Probably new install. Create config file to save settings to
|
|
|
|
if (!CResourceHandler::get("local")->existsResource(ResourceID(confName)))
|
|
|
|
CResourceHandler::get("local")->createResource(confName);
|
2013-04-02 20:06:43 +03:00
|
|
|
|
|
|
|
JsonUtils::maximize(config, "vcmi:settings");
|
|
|
|
JsonUtils::validate(config, "vcmi:settings", "settings");
|
2012-01-12 23:41:32 +03:00
|
|
|
}
|
2012-01-12 18:23:00 +03:00
|
|
|
|
|
|
|
void SettingsStorage::invalidateNode(const std::vector<std::string> &changedPath)
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(SettingsListener * listener : listeners)
|
2012-01-12 23:41:32 +03:00
|
|
|
listener->nodeInvalidated(changedPath);
|
2012-01-12 18:23:00 +03:00
|
|
|
|
2018-07-16 22:05:42 +02:00
|
|
|
JsonNode savedConf = config;
|
|
|
|
savedConf.Struct().erase("session");
|
|
|
|
JsonUtils::minimize(savedConf, "vcmi:settings");
|
2012-01-12 23:41:32 +03:00
|
|
|
|
2018-07-16 22:05:42 +02:00
|
|
|
FileStream file(*CResourceHandler::get()->getResourceName(ResourceID("config/settings.json")), std::ofstream::out | std::ofstream::trunc);
|
|
|
|
file << savedConf.toJson();
|
2012-01-12 18:23:00 +03:00
|
|
|
}
|
2008-11-09 00:29:19 +02:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
JsonNode & SettingsStorage::getNode(const std::vector<std::string> & path)
|
2008-11-09 00:29:19 +02:00
|
|
|
{
|
2012-01-12 18:23:00 +03:00
|
|
|
JsonNode *node = &config;
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const std::string & value : path)
|
2012-01-12 18:23:00 +03:00
|
|
|
node = &(*node)[value];
|
|
|
|
|
|
|
|
return *node;
|
|
|
|
}
|
2011-08-24 05:19:53 +03:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
Settings SettingsStorage::get(const std::vector<std::string> & path)
|
2008-11-09 00:29:19 +02:00
|
|
|
{
|
2012-01-12 18:23:00 +03:00
|
|
|
return Settings(*this, path);
|
|
|
|
}
|
2008-11-12 20:26:23 +02:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
const JsonNode & SettingsStorage::operator[](const std::string & value) const
|
2008-11-09 00:29:19 +02:00
|
|
|
{
|
2012-01-12 18:23:00 +03:00
|
|
|
return config[value];
|
|
|
|
}
|
|
|
|
|
2018-04-03 03:37:09 +02:00
|
|
|
const JsonNode & SettingsStorage::toJsonNode() const
|
|
|
|
{
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
SettingsListener::SettingsListener(SettingsStorage & _parent, std::vector<std::string> _path):
|
2012-01-12 18:23:00 +03:00
|
|
|
parent(_parent),
|
2023-03-13 23:26:44 +02:00
|
|
|
path(std::move(_path))
|
2008-11-09 00:29:19 +02:00
|
|
|
{
|
2012-01-12 18:23:00 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
SettingsListener::~SettingsListener()
|
|
|
|
{
|
|
|
|
parent.listeners.erase(this);
|
|
|
|
}
|
|
|
|
|
2012-09-26 16:13:39 +03:00
|
|
|
void SettingsListener::nodeInvalidated(const std::vector<std::string> &changedPath)
|
2012-01-12 23:41:32 +03:00
|
|
|
{
|
|
|
|
if (!callback)
|
|
|
|
return;
|
2012-01-12 18:23:00 +03:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-26 14:18:27 +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);
|
2012-01-12 18:23:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2012-01-12 18:23:00 +03:00
|
|
|
{
|
|
|
|
return node[value];
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
JsonNode & Settings::operator[](const std::string & value)
|
2012-01-12 18:23:00 +03:00
|
|
|
{
|
|
|
|
return node[value];
|
|
|
|
}
|
2012-09-29 15:02:46 +03:00
|
|
|
|
|
|
|
// Force instantiation of the SettingsStorage::NodeAccessor class template.
|
|
|
|
// That way method definitions can sit in the cpp file
|
|
|
|
template struct SettingsStorage::NodeAccessor<SettingsListener>;
|
2014-03-08 19:05:23 +03:00
|
|
|
template struct SettingsStorage::NodeAccessor<Settings>;
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|