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
This commit is contained in:
SomeTroglodyte 2023-05-11 12:01:00 +02:00 committed by GitHub
parent 3e4ba83bcc
commit 2ef5ed14e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View File

@ -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<MT: MutableMap<KT, Any>, KT>(
var entry = entries.child
while (entry != null) {
val key = json.readValue(keyClass, entry.child)
val value = json.readValue<Any>(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<Any>(null, entry.child.next)
result[key!!] = value!!
entry = entry.next

View File

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