Prevent deserialization problem with Espionage (#9809)

* Prevent deserialization problem with Espionage

* Move comment and explain context a little better
This commit is contained in:
SomeTroglodyte 2023-07-18 17:08:18 +02:00 committed by GitHub
parent 0e64b8230f
commit 37465b5032
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View File

@ -56,6 +56,13 @@ import java.util.UUID
* When you change the structure of any class with this interface in a way which makes it impossible * When you change the structure of any class with this interface in a way which makes it impossible
* to load the new saves from an older game version, increment [CURRENT_COMPATIBILITY_NUMBER]! And don't forget * to load the new saves from an older game version, increment [CURRENT_COMPATIBILITY_NUMBER]! And don't forget
* to add backwards compatibility for the previous format. * to add backwards compatibility for the previous format.
*
* Reminder: In all subclasse, do use only actual Collection types, not abstractions like
* `= mutableSetOf<Something>()`. That would make the reflection type of the field an interface, which
* hides the actual implementation from Gdx Json, so it will not try to call a no-args constructor but
* will instead deserialize a List in the jsonData.isArray() -> isAssignableFrom(Collection) branch of readValue:
* https://github.com/libgdx/libgdx/blob/75612dae1eeddc9611ed62366858ff1d0ac7898b/gdx/src/com/badlogic/gdx/utils/Json.java#L1111
* .. which will crash later (when readFields actually assigns it) unless empty.
*/ */
interface IsPartOfGameInfoSerialization interface IsPartOfGameInfoSerialization

View File

@ -8,15 +8,15 @@ import com.unciv.models.Spy
class EspionageManager : IsPartOfGameInfoSerialization { class EspionageManager : IsPartOfGameInfoSerialization {
var spyList = mutableListOf<Spy>() var spyList = ArrayList<Spy>()
val erasSpyEarnedFor = mutableSetOf<String>() val erasSpyEarnedFor = LinkedHashSet<String>()
@Transient @Transient
lateinit var civInfo: Civilization lateinit var civInfo: Civilization
fun clone(): EspionageManager { fun clone(): EspionageManager {
val toReturn = EspionageManager() val toReturn = EspionageManager()
toReturn.spyList.addAll(spyList.map { it.clone() }) spyList.mapTo(toReturn.spyList) { it.clone() }
toReturn.erasSpyEarnedFor.addAll(erasSpyEarnedFor) toReturn.erasSpyEarnedFor.addAll(erasSpyEarnedFor)
return toReturn return toReturn
} }