1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-06-17 00:07:41 +02:00

add persistent storage & completed campaign support

This commit is contained in:
Laserlicht
2023-09-20 22:18:53 +02:00
committed by GitHub
parent 52b86eb9c1
commit dfb5ccbeaf
5 changed files with 29 additions and 8 deletions

View File

@ -17,6 +17,7 @@
VCMI_LIB_NAMESPACE_BEGIN
SettingsStorage settings;
SettingsStorage persistent;
template<typename Accessor>
SettingsStorage::NodeAccessor<Accessor>::NodeAccessor(SettingsStorage & _parent, std::vector<std::string> _path):
@ -53,18 +54,26 @@ SettingsStorage::SettingsStorage():
{
}
void SettingsStorage::init()
void SettingsStorage::init(bool p)
{
JsonPath confName = JsonPath::builtin("config/settings.json");
persistentStorage = p;
cfgName = "config/settings.json";
if(persistentStorage)
cfgName = "config/persistent.json";
JsonPath confName = JsonPath::builtin(cfgName);
JsonUtils::assembleFromFiles(confName.getOriginalName()).swap(config);
// Probably new install. Create config file to save settings to
if (!CResourceHandler::get("local")->existsResource(confName))
CResourceHandler::get("local")->createResource("config/settings.json");
CResourceHandler::get("local")->createResource(cfgName);
JsonUtils::maximize(config, "vcmi:settings");
JsonUtils::validate(config, "vcmi:settings", "settings");
if(!persistentStorage)
{
JsonUtils::maximize(config, "vcmi:settings");
JsonUtils::validate(config, "vcmi:settings", "settings");
}
}
void SettingsStorage::invalidateNode(const std::vector<std::string> &changedPath)
@ -74,9 +83,10 @@ void SettingsStorage::invalidateNode(const std::vector<std::string> &changedPath
JsonNode savedConf = config;
savedConf.Struct().erase("session");
JsonUtils::minimize(savedConf, "vcmi:settings");
if(!persistentStorage)
JsonUtils::minimize(savedConf, "vcmi:settings");
std::fstream file(CResourceHandler::get()->getResourceName(JsonPath::builtin("config/settings.json"))->c_str(), std::ofstream::out | std::ofstream::trunc);
std::fstream file(CResourceHandler::get()->getResourceName(JsonPath::builtin(cfgName))->c_str(), std::ofstream::out | std::ofstream::trunc);
file << savedConf.toJson();
}