Selaa lähdekoodia

add persistent storage & completed campaign support

Laserlicht 2 vuotta sitten
vanhempi
sitoutus
dfb5ccbeaf

+ 3 - 0
client/CServerHandler.cpp

@@ -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);

+ 3 - 0
client/mainmenu/CCampaignScreen.cpp

@@ -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 - 7
lib/CConfigHandler.cpp

@@ -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();
 }
 

+ 5 - 1
lib/CConfigHandler.h

@@ -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

+ 1 - 0
lib/VCMI_Lib.cpp

@@ -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);
 
 }