2009-04-15 17:03:31 +03:00
|
|
|
/*
|
2015-12-08 08:53:14 +02:00
|
|
|
* CConfigHandler.h, part of VCMI engine
|
2009-04-15 17:03:31 +03:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
2009-04-16 14:14:13 +03:00
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../lib/JsonNode.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
class Settings;
|
|
|
|
class SettingsListener;
|
|
|
|
|
|
|
|
/// Main storage of game settings
|
2012-09-29 13:59:43 +03:00
|
|
|
class DLL_LINKAGE SettingsStorage
|
2009-04-16 14:14:13 +03:00
|
|
|
{
|
2012-01-12 18:23:00 +03:00
|
|
|
//Helper struct to access specific node either via chain of operator[] or with one operator() (vector)
|
|
|
|
template<typename Accessor>
|
2012-09-29 13:59:43 +03:00
|
|
|
struct DLL_LINKAGE NodeAccessor
|
2009-04-16 14:14:13 +03:00
|
|
|
{
|
2012-01-12 18:23:00 +03:00
|
|
|
SettingsStorage & parent;
|
|
|
|
std::vector<std::string> path;
|
|
|
|
|
|
|
|
NodeAccessor(SettingsStorage & _parent, std::vector<std::string> _path);
|
2023-03-13 23:26:44 +02:00
|
|
|
NodeAccessor<Accessor> operator[](const std::string & nextNode) const;
|
2018-04-03 03:37:09 +02:00
|
|
|
NodeAccessor<Accessor> operator () (std::vector<std::string> _path) const;
|
2012-01-12 18:23:00 +03:00
|
|
|
operator Accessor() const;
|
2009-04-16 14:14:13 +03:00
|
|
|
};
|
2012-01-12 18:23:00 +03:00
|
|
|
|
|
|
|
std::set<SettingsListener*> listeners;
|
|
|
|
JsonNode config;
|
2018-04-03 03:37:09 +02:00
|
|
|
|
2023-09-21 21:27:06 +02:00
|
|
|
std::string dataFilename;
|
|
|
|
std::string schema;
|
2023-09-20 22:18:53 +02:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
JsonNode & getNode(const std::vector<std::string> & path);
|
2012-01-12 18:23:00 +03:00
|
|
|
|
|
|
|
// Calls all required listeners
|
|
|
|
void invalidateNode(const std::vector<std::string> &changedPath);
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
Settings get(const std::vector<std::string> & path);
|
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
public:
|
|
|
|
// Initialize config structure
|
|
|
|
SettingsStorage();
|
2023-09-21 21:27:06 +02:00
|
|
|
void init(const std::string & dataFilename, const std::string & schema);
|
2011-02-22 13:52:36 +02:00
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
// Get write access to config node at path
|
|
|
|
const NodeAccessor<Settings> write;
|
|
|
|
|
|
|
|
// Get access to listener at path
|
|
|
|
const NodeAccessor<SettingsListener> listen;
|
|
|
|
|
|
|
|
//Read access, see JsonNode::operator[]
|
2023-03-13 23:26:44 +02:00
|
|
|
const JsonNode & operator[](const std::string & value) const;
|
2018-04-03 03:37:09 +02:00
|
|
|
const JsonNode & toJsonNode() const;
|
2012-01-12 18:23:00 +03:00
|
|
|
|
|
|
|
friend class SettingsListener;
|
|
|
|
friend class Settings;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Class for listening changes in specific part of configuration (e.g. change of music volume)
|
2012-09-29 13:59:43 +03:00
|
|
|
class DLL_LINKAGE SettingsListener
|
2012-01-12 18:23:00 +03:00
|
|
|
{
|
|
|
|
SettingsStorage &parent;
|
|
|
|
// Path to this node
|
|
|
|
std::vector<std::string> path;
|
|
|
|
// Callback
|
2013-06-26 14:18:27 +03:00
|
|
|
std::function<void(const JsonNode&)> callback;
|
2012-01-12 18:23:00 +03:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
SettingsListener(SettingsStorage & _parent, std::vector<std::string> _path);
|
2012-01-12 18:23:00 +03:00
|
|
|
|
|
|
|
// Executes callback if changedpath begins with path
|
2012-09-26 16:13:39 +03:00
|
|
|
void nodeInvalidated(const std::vector<std::string> & changedPath);
|
2012-01-12 18:23:00 +03:00
|
|
|
|
|
|
|
public:
|
2012-09-06 13:26:18 +03:00
|
|
|
SettingsListener(const SettingsListener &sl);
|
2012-01-12 18:23:00 +03:00
|
|
|
~SettingsListener();
|
|
|
|
|
|
|
|
// assign callback function
|
2013-06-26 14:18:27 +03:00
|
|
|
void operator()(std::function<void(const JsonNode&)> _callback);
|
2012-01-12 18:23:00 +03:00
|
|
|
|
|
|
|
friend class SettingsStorage;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// System options, provides write access to config tree with auto-saving on change
|
2012-09-29 13:59:43 +03:00
|
|
|
class DLL_LINKAGE Settings
|
2012-01-12 18:23:00 +03:00
|
|
|
{
|
|
|
|
SettingsStorage &parent;
|
|
|
|
//path to this node
|
|
|
|
std::vector<std::string> path;
|
|
|
|
JsonNode &node;
|
|
|
|
JsonNode copy;
|
|
|
|
|
|
|
|
//Get access to node pointed by path
|
|
|
|
Settings(SettingsStorage &_parent, const std::vector<std::string> &_path);
|
|
|
|
|
|
|
|
public:
|
|
|
|
//Saves config if it was modified
|
|
|
|
~Settings();
|
|
|
|
|
|
|
|
//Returns node selected during construction
|
|
|
|
JsonNode* operator ->();
|
|
|
|
const JsonNode* operator ->() const;
|
|
|
|
|
|
|
|
//Helper, replaces JsonNode::operator[]
|
2023-03-13 23:26:44 +02:00
|
|
|
JsonNode & operator[](const std::string & value);
|
|
|
|
const JsonNode & operator[](const std::string & value) const;
|
2012-01-12 18:23:00 +03:00
|
|
|
|
|
|
|
friend class SettingsStorage;
|
|
|
|
};
|
|
|
|
|
2012-09-29 13:59:43 +03:00
|
|
|
extern DLL_LINKAGE SettingsStorage settings;
|
2023-09-21 21:27:06 +02:00
|
|
|
extern DLL_LINKAGE SettingsStorage persistentStorage;
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|