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

@ -683,6 +683,9 @@ void CServerHandler::startCampaignScenario(std::shared_ptr<CampaignState> cs)
auto & epilogue = ourCampaign->scenario(*ourCampaign->lastScenario()).epilog; auto & epilogue = ourCampaign->scenario(*ourCampaign->lastScenario()).epilog;
auto finisher = [=]() auto finisher = [=]()
{ {
Settings entry = persistent.write["campaign"][ourCampaign->campaignSet][ourCampaign->getFilename()]["completed"];
entry->Bool() = true;
if(!ourCampaign->isCampaignFinished()) if(!ourCampaign->isCampaignFinished())
{ {
GH.windows().pushWindow(CMM); GH.windows().pushWindow(CMM);

View File

@ -107,6 +107,9 @@ CCampaignScreen::CCampaignButton::CCampaignButton(const JsonNode & config, std::
auto header = CampaignHandler::getHeader(campFile); auto header = CampaignHandler::getHeader(campFile);
hoverText = header->getName(); hoverText = header->getName();
if(persistent["campaign"][campaignSet][header->getFilename()]["completed"].Bool())
status = CCampaignScreen::COMPLETED;
if(status != CCampaignScreen::DISABLED) if(status != CCampaignScreen::DISABLED)
{ {
addUsedEvents(LCLICK | HOVER); addUsedEvents(LCLICK | HOVER);

View File

@ -17,6 +17,7 @@
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
SettingsStorage settings; SettingsStorage settings;
SettingsStorage persistent;
template<typename Accessor> template<typename Accessor>
SettingsStorage::NodeAccessor<Accessor>::NodeAccessor(SettingsStorage & _parent, std::vector<std::string> _path): 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); JsonUtils::assembleFromFiles(confName.getOriginalName()).swap(config);
// Probably new install. Create config file to save settings to // Probably new install. Create config file to save settings to
if (!CResourceHandler::get("local")->existsResource(confName)) if (!CResourceHandler::get("local")->existsResource(confName))
CResourceHandler::get("local")->createResource("config/settings.json"); CResourceHandler::get("local")->createResource(cfgName);
JsonUtils::maximize(config, "vcmi:settings"); if(!persistentStorage)
JsonUtils::validate(config, "vcmi:settings", "settings"); {
JsonUtils::maximize(config, "vcmi:settings");
JsonUtils::validate(config, "vcmi:settings", "settings");
}
} }
void SettingsStorage::invalidateNode(const std::vector<std::string> &changedPath) 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; JsonNode savedConf = config;
savedConf.Struct().erase("session"); 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(); file << savedConf.toJson();
} }

View File

@ -35,6 +35,9 @@ class DLL_LINKAGE SettingsStorage
std::set<SettingsListener*> listeners; std::set<SettingsListener*> listeners;
JsonNode config; JsonNode config;
bool persistentStorage;
std::string cfgName;
JsonNode & getNode(const std::vector<std::string> & path); JsonNode & getNode(const std::vector<std::string> & path);
// Calls all required listeners // Calls all required listeners
@ -45,7 +48,7 @@ class DLL_LINKAGE SettingsStorage
public: public:
// Initialize config structure // Initialize config structure
SettingsStorage(); SettingsStorage();
void init(); void init(bool persistent = false);
// Get write access to config node at path // Get write access to config node at path
const NodeAccessor<Settings> write; const NodeAccessor<Settings> write;
@ -113,5 +116,6 @@ public:
}; };
extern DLL_LINKAGE SettingsStorage settings; extern DLL_LINKAGE SettingsStorage settings;
extern DLL_LINKAGE SettingsStorage persistent;
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -53,6 +53,7 @@ DLL_LINKAGE void preinitDLL(CConsoleHandler * Console, bool onlyEssential, bool
VLC = new LibClasses(); VLC = new LibClasses();
VLC->loadFilesystem(extractArchives); VLC->loadFilesystem(extractArchives);
settings.init(); settings.init();
persistent.init(true);
VLC->loadModFilesystem(onlyEssential); VLC->loadModFilesystem(onlyEssential);
} }