mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-24 22:14:36 +02:00
add persistent storage & completed campaign support
This commit is contained in:
parent
52b86eb9c1
commit
dfb5ccbeaf
@ -683,6 +683,9 @@ void CServerHandler::startCampaignScenario(std::shared_ptr<CampaignState> cs)
|
||||
auto & epilogue = ourCampaign->scenario(*ourCampaign->lastScenario()).epilog;
|
||||
auto finisher = [=]()
|
||||
{
|
||||
Settings entry = persistent.write["campaign"][ourCampaign->campaignSet][ourCampaign->getFilename()]["completed"];
|
||||
entry->Bool() = true;
|
||||
|
||||
if(!ourCampaign->isCampaignFinished())
|
||||
{
|
||||
GH.windows().pushWindow(CMM);
|
||||
|
@ -107,6 +107,9 @@ CCampaignScreen::CCampaignButton::CCampaignButton(const JsonNode & config, std::
|
||||
auto header = CampaignHandler::getHeader(campFile);
|
||||
hoverText = header->getName();
|
||||
|
||||
if(persistent["campaign"][campaignSet][header->getFilename()]["completed"].Bool())
|
||||
status = CCampaignScreen::COMPLETED;
|
||||
|
||||
if(status != CCampaignScreen::DISABLED)
|
||||
{
|
||||
addUsedEvents(LCLICK | HOVER);
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -35,6 +35,9 @@ class DLL_LINKAGE SettingsStorage
|
||||
std::set<SettingsListener*> listeners;
|
||||
JsonNode config;
|
||||
|
||||
bool persistentStorage;
|
||||
std::string cfgName;
|
||||
|
||||
JsonNode & getNode(const std::vector<std::string> & path);
|
||||
|
||||
// Calls all required listeners
|
||||
@ -45,7 +48,7 @@ class DLL_LINKAGE SettingsStorage
|
||||
public:
|
||||
// Initialize config structure
|
||||
SettingsStorage();
|
||||
void init();
|
||||
void init(bool persistent = false);
|
||||
|
||||
// Get write access to config node at path
|
||||
const NodeAccessor<Settings> write;
|
||||
@ -113,5 +116,6 @@ public:
|
||||
};
|
||||
|
||||
extern DLL_LINKAGE SettingsStorage settings;
|
||||
extern DLL_LINKAGE SettingsStorage persistent;
|
||||
|
||||
VCMI_LIB_NAMESPACE_END
|
||||
|
@ -53,6 +53,7 @@ DLL_LINKAGE void preinitDLL(CConsoleHandler * Console, bool onlyEssential, bool
|
||||
VLC = new LibClasses();
|
||||
VLC->loadFilesystem(extractArchives);
|
||||
settings.init();
|
||||
persistent.init(true);
|
||||
VLC->loadModFilesystem(onlyEssential);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user