1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-12-20 00:22:54 +02:00

Settings: savefrequency is now configurable in launcher

* Client command-line can modify arbitrary settings now
* Only launcher auto-saves to settings.json
This commit is contained in:
Henning Koehler
2018-04-03 13:37:09 +12:00
committed by ArseniyShestakov
parent ce0b4b222d
commit 2ede3783dd
12 changed files with 119 additions and 24 deletions

View File

@@ -42,7 +42,7 @@ SettingsStorage::NodeAccessor<Accessor>::operator Accessor() const
}
template<typename Accessor>
SettingsStorage::NodeAccessor<Accessor> SettingsStorage::NodeAccessor<Accessor>::operator () (std::vector<std::string> _path)
SettingsStorage::NodeAccessor<Accessor> SettingsStorage::NodeAccessor<Accessor>::operator () (std::vector<std::string> _path) const
{
std::vector<std::string> newPath = path;
newPath.insert( newPath.end(), _path.begin(), _path.end());
@@ -51,11 +51,12 @@ SettingsStorage::NodeAccessor<Accessor> SettingsStorage::NodeAccessor<Accessor>:
SettingsStorage::SettingsStorage():
write(NodeAccessor<Settings>(*this, std::vector<std::string>() )),
listen(NodeAccessor<SettingsListener>(*this, std::vector<std::string>() ))
listen(NodeAccessor<SettingsListener>(*this, std::vector<std::string>() )),
autoSaveConfig(false)
{
}
void SettingsStorage::init()
void SettingsStorage::init(bool autoSave)
{
std::string confName = "config/settings.json";
@@ -67,6 +68,7 @@ void SettingsStorage::init()
JsonUtils::maximize(config, "vcmi:settings");
JsonUtils::validate(config, "vcmi:settings", "settings");
autoSaveConfig = autoSave;
}
void SettingsStorage::invalidateNode(const std::vector<std::string> &changedPath)
@@ -74,14 +76,14 @@ void SettingsStorage::invalidateNode(const std::vector<std::string> &changedPath
for(SettingsListener * listener : listeners)
listener->nodeInvalidated(changedPath);
JsonNode savedConf = config;
JsonNode schema(ResourceID("config/schemas/settings.json"));
if(autoSaveConfig)
{
JsonNode savedConf = config;
JsonUtils::minimize(savedConf, "vcmi:settings");
savedConf.Struct().erase("session");
JsonUtils::minimize(savedConf, "vcmi:settings");
FileStream file(*CResourceHandler::get()->getResourceName(ResourceID("config/settings.json")), std::ofstream::out | std::ofstream::trunc);
file << savedConf.toJson();
FileStream file(*CResourceHandler::get()->getResourceName(ResourceID("config/settings.json")), std::ofstream::out | std::ofstream::trunc);
file << savedConf.toJson();
}
}
JsonNode & SettingsStorage::getNode(std::vector<std::string> path)
@@ -98,11 +100,16 @@ Settings SettingsStorage::get(std::vector<std::string> path)
return Settings(*this, path);
}
const JsonNode& SettingsStorage::operator [](std::string value)
const JsonNode & SettingsStorage::operator [](std::string value) const
{
return config[value];
}
const JsonNode & SettingsStorage::toJsonNode() const
{
return config;
}
SettingsListener::SettingsListener(SettingsStorage &_parent, const std::vector<std::string> &_path):
parent(_parent),
path(_path)