From 8b90571a77c79dd3059255569f318bc81b0077cf Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Fri, 17 Aug 2018 11:38:19 +0300 Subject: [PATCH] Fixed some concurrency problems --- core/src/com/unciv/logic/GameInfo.kt | 1 + core/src/com/unciv/logic/GameSaver.kt | 13 +++++++------ .../unciv/logic/civilization/CivilizationInfo.kt | 4 +--- core/src/com/unciv/logic/map/TileMap.kt | 1 + core/src/com/unciv/ui/worldscreen/WorldScreen.kt | 6 +++++- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/core/src/com/unciv/logic/GameInfo.kt b/core/src/com/unciv/logic/GameInfo.kt index 35ed11a493..5af30f7ab6 100644 --- a/core/src/com/unciv/logic/GameInfo.kt +++ b/core/src/com/unciv/logic/GameInfo.kt @@ -105,6 +105,7 @@ class GameInfo { toReturn.tileMap=tileMap.clone() toReturn.notifications.addAll(notifications) toReturn.turns=turns + toReturn.setTransients() return toReturn } } diff --git a/core/src/com/unciv/logic/GameSaver.kt b/core/src/com/unciv/logic/GameSaver.kt index 293ce12aca..f3d8408b14 100644 --- a/core/src/com/unciv/logic/GameSaver.kt +++ b/core/src/com/unciv/logic/GameSaver.kt @@ -2,7 +2,6 @@ package com.unciv.logic import com.badlogic.gdx.Gdx import com.badlogic.gdx.files.FileHandle -import com.badlogic.gdx.utils.Json import com.unciv.GameSettings import com.unciv.OldGameSettings import com.unciv.UnCivGame @@ -10,6 +9,8 @@ import com.unciv.UnCivGame class GameSaver { private val saveFilesFolder = "SaveFiles" + fun json() = UnCivGame.Current.json + fun getSave(GameName: String): FileHandle { return Gdx.files.local("$saveFilesFolder/$GameName") } @@ -19,11 +20,11 @@ class GameSaver { } fun saveGame(game: GameInfo, GameName: String) { - getSave(GameName).writeString(Json().toJson(game), false) + getSave(GameName).writeString(json().toJson(game), false) } fun loadGame(GameName: String) : GameInfo { - val game = UnCivGame.Current.json.fromJson(GameInfo::class.java, getSave(GameName).readString()) + val game = json().fromJson(GameInfo::class.java, getSave(GameName).readString()) game.setTransients() return game } @@ -40,14 +41,14 @@ class GameSaver { val settingsFile = getGeneralSettingsFile() if(!settingsFile.exists()) return GameSettings() try { - return UnCivGame.Current.json.fromJson(GameSettings::class.java, settingsFile) + return json().fromJson(GameSettings::class.java, settingsFile) } catch(ex:Exception) { - return UnCivGame.Current.json.fromJson(OldGameSettings::class.java, settingsFile).toGameSettings() + return json().fromJson(OldGameSettings::class.java, settingsFile).toGameSettings() } } fun setGeneralSettings(gameSettings: GameSettings){ - getGeneralSettingsFile().writeString(Json().toJson(gameSettings), false) + getGeneralSettingsFile().writeString(json().toJson(gameSettings), false) } } diff --git a/core/src/com/unciv/logic/civilization/CivilizationInfo.kt b/core/src/com/unciv/logic/civilization/CivilizationInfo.kt index dba36af5fa..34973113fc 100644 --- a/core/src/com/unciv/logic/civilization/CivilizationInfo.kt +++ b/core/src/com/unciv/logic/civilization/CivilizationInfo.kt @@ -320,6 +320,4 @@ class CivilizationInfo { toReturn.civName=civName return toReturn } -} - - +} \ No newline at end of file diff --git a/core/src/com/unciv/logic/map/TileMap.kt b/core/src/com/unciv/logic/map/TileMap.kt index ca4c6dcf10..bc333ec17c 100644 --- a/core/src/com/unciv/logic/map/TileMap.kt +++ b/core/src/com/unciv/logic/map/TileMap.kt @@ -84,6 +84,7 @@ class TileMap { fun clone(): TileMap { val toReturn = TileMap() toReturn.tiles.putAll(tiles.values.map { it.clone() }.associateBy{it.position.toString()}) + setTransients() return toReturn } diff --git a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt index 4a6ad29110..33dbeea045 100644 --- a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt +++ b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt @@ -83,7 +83,11 @@ class WorldScreen : CameraStageBaseScreen() { fun update() { - kotlin.concurrent.thread { civInfo.happiness = civInfo.getHappinessForNextTurn().values.sum().toInt() } + val gameClone = gameInfo.clone() + // so we don't get a concurrent modification exception, we clone the entire game (yes really, it's actually very fast) + kotlin.concurrent.thread { + civInfo.happiness = gameClone.getPlayerCivilization().getHappinessForNextTurn().values.sum().toInt() + } if(UnCivGame.Current.settings.hasCrashedRecently){ displayTutorials("GameCrashed")