Browse Source

fix serialisation

Laserlicht 3 weeks ago
parent
commit
07621f795c
2 changed files with 22 additions and 5 deletions
  1. 15 4
      client/lobby/BattleOnlyModeTab.cpp
  2. 7 1
      lib/StartInfo.cpp

+ 15 - 4
client/lobby/BattleOnlyModeTab.cpp

@@ -76,9 +76,20 @@ BattleOnlyModeTab::BattleOnlyModeTab()
 {
 	OBJECT_CONSTRUCTION;
 
-	//JsonNode node = persistentStorage["lobby"]["battleModeSettings"];
-	//JsonDeserializer handler(nullptr, node);
-	//startInfo->serializeJson(handler);
+	try
+	{
+		JsonNode node = persistentStorage["battleModeSettings"];
+		if(!node.isNull())
+		{
+			node.setModScope(ModScope::scopeGame());
+			JsonDeserializer handler(nullptr, node);
+			startInfo->serializeJson(handler);
+		}
+	}
+	catch(std::exception & e)
+	{
+		logGlobal->error("Error loading saved battleModeSettings, received exception: %s", e.what());
+	}
 
 	init();
 
@@ -213,7 +224,7 @@ void BattleOnlyModeTab::update()
 	JsonNode node;
 	JsonSerializer handler(nullptr, node);
 	startInfo->serializeJson(handler);
-	Settings storage = persistentStorage.write["lobby"]["battleModeSettings"];
+	Settings storage = persistentStorage.write["battleModeSettings"];
 	storage->Struct() = node.Struct();
 }
 

+ 7 - 1
lib/StartInfo.cpp

@@ -259,6 +259,9 @@ void BattleOnlyModeStartInfo::serializeJson(JsonSerializeFormat & handler)
 {
 	handler.serializeId("selectedTerrain", selectedTerrain);
 	handler.serializeId("selectedTown", selectedTown);
+	if(!handler.saving && selectedTown == FactionID::NONE)
+		selectedTown = FactionID::ANY;
+
 	auto slots = handler.enterArray("slots");
 	slots.resize(2);
 	for(int i = 0; i < 2; i++)
@@ -305,10 +308,13 @@ void BattleOnlyModeStartInfo::serializeJson(JsonSerializeFormat & handler)
 			s->serializeIdMap("artifacts", tmp);
 			std::map<ArtifactID, ArtifactPosition> converted;
 			for (auto &[id, pos] : tmp)
-    			converted[id] = ArtifactPosition(pos);
+				if(id != ArtifactID::NONE)
+					converted[id] = ArtifactPosition(pos);
 			artifacts[i] = vstd::reverseMap(converted);
 		}
 		s->serializeIdArray("spells", spells[i]);
+		if(!handler.saving)
+			spells[i].erase(std::remove(spells[i].begin(), spells[i].end(), SpellID::NONE), spells[i].end());
 		s->serializeBool("warMachines", warMachines[i]);
 		s->serializeBool("spellBook", spellBook[i]);
 	}