From 2ef5ed14e12ecb50d8fcb88af4d4106c945fed72 Mon Sep 17 00:00:00 2001 From: SomeTroglodyte <63000004+SomeTroglodyte@users.noreply.github.com> Date: Thu, 11 May 2023 12:01:00 +0200 Subject: [PATCH] Fix 4.6.10 no longer loading older games (#9370) * Fix 4.6.10 no longer loading older games * Save games with correct current version info --- .../com/unciv/json/NonStringKeyMapSerializer.kt | 14 +++++++++++++- core/src/com/unciv/logic/files/UncivFiles.kt | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/core/src/com/unciv/json/NonStringKeyMapSerializer.kt b/core/src/com/unciv/json/NonStringKeyMapSerializer.kt index 809213dc1a..388aaea3fe 100644 --- a/core/src/com/unciv/json/NonStringKeyMapSerializer.kt +++ b/core/src/com/unciv/json/NonStringKeyMapSerializer.kt @@ -3,6 +3,7 @@ package com.unciv.json import com.badlogic.gdx.utils.Json import com.badlogic.gdx.utils.Json.Serializer import com.badlogic.gdx.utils.JsonValue +import com.unciv.logic.automation.civilization.Encampment /** * A [Serializer] for gdx's [Json] that serializes a map that does not have [String] as its key class. @@ -49,7 +50,18 @@ class NonStringKeyMapSerializer, KT>( var entry = entries.child while (entry != null) { val key = json.readValue(keyClass, entry.child) - val value = json.readValue(null, entry.child.next) + + // 4.6.10 moved the Encampment class, but deserialization of old games which had the old + // full package name written out depended on the class loader finding it under the serialized name... + // This kludge steps in and allows both fully qualified class names until a better way is found + // See #9367 + val isOldEncampment = entry.child.next.child.run { + name == "class" && isString && asString() == "com.unciv.logic.Encampment" + } + val value = if (isOldEncampment) + json.readValue(Encampment::class.java, entry.child.next.child.next) + else json.readValue(null, entry.child.next) + result[key!!] = value!! entry = entry.next diff --git a/core/src/com/unciv/logic/files/UncivFiles.kt b/core/src/com/unciv/logic/files/UncivFiles.kt index 319e016bca..3ea2a8c5cb 100644 --- a/core/src/com/unciv/logic/files/UncivFiles.kt +++ b/core/src/com/unciv/logic/files/UncivFiles.kt @@ -353,7 +353,6 @@ class UncivFiles( // this means there wasn't an immediate error while serializing, but this version will cause other errors later down the line throw IncompatibleGameInfoVersionException(gameInfo.version) } - gameInfo.version = GameInfo.CURRENT_COMPATIBILITY_VERSION gameInfo.setTransients() return gameInfo } @@ -368,6 +367,7 @@ class UncivFiles( /** Returns gzipped serialization of [game], optionally gzipped ([forceZip] overrides [saveZipped]) */ fun gameInfoToString(game: GameInfo, forceZip: Boolean? = null): String { + game.version = GameInfo.CURRENT_COMPATIBILITY_VERSION val plainJson = json().toJson(game) return if (forceZip ?: saveZipped) Gzip.zip(plainJson) else plainJson }