mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-29 15:01:09 -04:00
Fixed some concurrency problems
This commit is contained in:
parent
fe67fda906
commit
8b90571a77
@ -105,6 +105,7 @@ class GameInfo {
|
|||||||
toReturn.tileMap=tileMap.clone()
|
toReturn.tileMap=tileMap.clone()
|
||||||
toReturn.notifications.addAll(notifications)
|
toReturn.notifications.addAll(notifications)
|
||||||
toReturn.turns=turns
|
toReturn.turns=turns
|
||||||
|
toReturn.setTransients()
|
||||||
return toReturn
|
return toReturn
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package com.unciv.logic
|
|||||||
|
|
||||||
import com.badlogic.gdx.Gdx
|
import com.badlogic.gdx.Gdx
|
||||||
import com.badlogic.gdx.files.FileHandle
|
import com.badlogic.gdx.files.FileHandle
|
||||||
import com.badlogic.gdx.utils.Json
|
|
||||||
import com.unciv.GameSettings
|
import com.unciv.GameSettings
|
||||||
import com.unciv.OldGameSettings
|
import com.unciv.OldGameSettings
|
||||||
import com.unciv.UnCivGame
|
import com.unciv.UnCivGame
|
||||||
@ -10,6 +9,8 @@ import com.unciv.UnCivGame
|
|||||||
class GameSaver {
|
class GameSaver {
|
||||||
private val saveFilesFolder = "SaveFiles"
|
private val saveFilesFolder = "SaveFiles"
|
||||||
|
|
||||||
|
fun json() = UnCivGame.Current.json
|
||||||
|
|
||||||
fun getSave(GameName: String): FileHandle {
|
fun getSave(GameName: String): FileHandle {
|
||||||
return Gdx.files.local("$saveFilesFolder/$GameName")
|
return Gdx.files.local("$saveFilesFolder/$GameName")
|
||||||
}
|
}
|
||||||
@ -19,11 +20,11 @@ class GameSaver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun saveGame(game: GameInfo, GameName: String) {
|
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 {
|
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()
|
game.setTransients()
|
||||||
return game
|
return game
|
||||||
}
|
}
|
||||||
@ -40,14 +41,14 @@ class GameSaver {
|
|||||||
val settingsFile = getGeneralSettingsFile()
|
val settingsFile = getGeneralSettingsFile()
|
||||||
if(!settingsFile.exists()) return GameSettings()
|
if(!settingsFile.exists()) return GameSettings()
|
||||||
try {
|
try {
|
||||||
return UnCivGame.Current.json.fromJson(GameSettings::class.java, settingsFile)
|
return json().fromJson(GameSettings::class.java, settingsFile)
|
||||||
}
|
}
|
||||||
catch(ex:Exception) {
|
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){
|
fun setGeneralSettings(gameSettings: GameSettings){
|
||||||
getGeneralSettingsFile().writeString(Json().toJson(gameSettings), false)
|
getGeneralSettingsFile().writeString(json().toJson(gameSettings), false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -321,5 +321,3 @@ class CivilizationInfo {
|
|||||||
return toReturn
|
return toReturn
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -84,6 +84,7 @@ class TileMap {
|
|||||||
fun clone(): TileMap {
|
fun clone(): TileMap {
|
||||||
val toReturn = TileMap()
|
val toReturn = TileMap()
|
||||||
toReturn.tiles.putAll(tiles.values.map { it.clone() }.associateBy{it.position.toString()})
|
toReturn.tiles.putAll(tiles.values.map { it.clone() }.associateBy{it.position.toString()})
|
||||||
|
setTransients()
|
||||||
return toReturn
|
return toReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,7 +83,11 @@ class WorldScreen : CameraStageBaseScreen() {
|
|||||||
|
|
||||||
|
|
||||||
fun update() {
|
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){
|
if(UnCivGame.Current.settings.hasCrashedRecently){
|
||||||
displayTutorials("GameCrashed")
|
displayTutorials("GameCrashed")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user