From 7258507088a6c4b1b69b75d3b2ede41e21252f4a Mon Sep 17 00:00:00 2001 From: Gualdimar Date: Tue, 31 Jan 2023 22:24:04 +0200 Subject: [PATCH] Autosave fix (#8517) * Update UncivFiles.kt * Update WorldScreen.kt --- core/src/com/unciv/logic/files/UncivFiles.kt | 32 ++++++++++--------- .../com/unciv/ui/worldscreen/WorldScreen.kt | 2 +- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/core/src/com/unciv/logic/files/UncivFiles.kt b/core/src/com/unciv/logic/files/UncivFiles.kt index b275e6731e..eeb8e2404f 100644 --- a/core/src/com/unciv/logic/files/UncivFiles.kt +++ b/core/src/com/unciv/logic/files/UncivFiles.kt @@ -388,25 +388,25 @@ class UncivFiles( /** * Auto-saves a snapshot of the [gameInfo] in a new thread. */ - fun requestAutoSave(gameInfo: GameInfo): Job { + fun requestAutoSave(gameInfo: GameInfo, nextTurn: Boolean = false): Job { // The save takes a long time (up to a few seconds on large games!) and we can do it while the player continues his game. // On the other hand if we alter the game data while it's being serialized we could get a concurrent modification exception. // So what we do is we clone all the game data and serialize the clone. - return requestAutoSaveUnCloned(gameInfo.clone()) + return requestAutoSaveUnCloned(gameInfo.clone(), nextTurn) } /** * In a new thread, auto-saves the [gameInfo] directly - only use this with [GameInfo] objects that are guaranteed not to be changed while the autosave is in progress! */ - fun requestAutoSaveUnCloned(gameInfo: GameInfo): Job { + fun requestAutoSaveUnCloned(gameInfo: GameInfo, nextTurn: Boolean = false): Job { val job = Concurrency.run("autoSaveUnCloned") { - autoSave(gameInfo) + autoSave(gameInfo, nextTurn) } autoSaveJob = job return job } - fun autoSave(gameInfo: GameInfo) { + fun autoSave(gameInfo: GameInfo, nextTurn: Boolean = false) { try { saveGame(gameInfo, AUTOSAVE_FILE_NAME) } catch (oom: OutOfMemoryError) { @@ -414,16 +414,18 @@ class UncivFiles( } // keep auto-saves for the last 10 turns for debugging purposes - val newAutosaveFilename = - SAVE_FILES_FOLDER + File.separator + AUTOSAVE_FILE_NAME + "-${gameInfo.currentPlayer}-${gameInfo.turns}" - getSave(AUTOSAVE_FILE_NAME).copyTo(files.local(newAutosaveFilename)) - - fun getAutosaves(): Sequence { - return getSaves().filter { it.name().startsWith(AUTOSAVE_FILE_NAME) } - } - while (getAutosaves().count() > 10) { - val saveToDelete = getAutosaves().minByOrNull { it.lastModified() }!! - deleteSave(saveToDelete.name()) + if(nextTurn) { + val newAutosaveFilename = + SAVE_FILES_FOLDER + File.separator + AUTOSAVE_FILE_NAME + "-${gameInfo.currentPlayer}-${gameInfo.turns}" + getSave(AUTOSAVE_FILE_NAME).copyTo(files.local(newAutosaveFilename)) + + fun getAutosaves(): Sequence { + return getSaves().filter { it.name().startsWith(AUTOSAVE_FILE_NAME) } + } + while (getAutosaves().count() > 10) { + val saveToDelete = getAutosaves().minByOrNull { it.lastModified() }!! + deleteSave(saveToDelete.name()) + } } } diff --git a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt index e271f5da72..e99ed476c6 100644 --- a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt +++ b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt @@ -889,7 +889,7 @@ class WorldScreen( fun autoSave() { waitingForAutosave = true shouldUpdate = true - UncivGame.Current.files.requestAutoSave(gameInfo).invokeOnCompletion { + UncivGame.Current.files.requestAutoSave(gameInfo, true).invokeOnCompletion { // only enable the user to next turn once we've saved the current one waitingForAutosave = false shouldUpdate = true