Autosave fix (#8517)

* Update UncivFiles.kt

* Update WorldScreen.kt
This commit is contained in:
Gualdimar
2023-01-31 22:24:04 +02:00
committed by GitHub
parent dc6d1cb9f5
commit 7258507088
2 changed files with 18 additions and 16 deletions

View File

@ -388,25 +388,25 @@ class UncivFiles(
/** /**
* Auto-saves a snapshot of the [gameInfo] in a new thread. * 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. // 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. // 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. // 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! * 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") { val job = Concurrency.run("autoSaveUnCloned") {
autoSave(gameInfo) autoSave(gameInfo, nextTurn)
} }
autoSaveJob = job autoSaveJob = job
return job return job
} }
fun autoSave(gameInfo: GameInfo) { fun autoSave(gameInfo: GameInfo, nextTurn: Boolean = false) {
try { try {
saveGame(gameInfo, AUTOSAVE_FILE_NAME) saveGame(gameInfo, AUTOSAVE_FILE_NAME)
} catch (oom: OutOfMemoryError) { } catch (oom: OutOfMemoryError) {
@ -414,16 +414,18 @@ class UncivFiles(
} }
// keep auto-saves for the last 10 turns for debugging purposes // keep auto-saves for the last 10 turns for debugging purposes
val newAutosaveFilename = if(nextTurn) {
SAVE_FILES_FOLDER + File.separator + AUTOSAVE_FILE_NAME + "-${gameInfo.currentPlayer}-${gameInfo.turns}" val newAutosaveFilename =
getSave(AUTOSAVE_FILE_NAME).copyTo(files.local(newAutosaveFilename)) SAVE_FILES_FOLDER + File.separator + AUTOSAVE_FILE_NAME + "-${gameInfo.currentPlayer}-${gameInfo.turns}"
getSave(AUTOSAVE_FILE_NAME).copyTo(files.local(newAutosaveFilename))
fun getAutosaves(): Sequence<FileHandle> { fun getAutosaves(): Sequence<FileHandle> {
return getSaves().filter { it.name().startsWith(AUTOSAVE_FILE_NAME) } return getSaves().filter { it.name().startsWith(AUTOSAVE_FILE_NAME) }
} }
while (getAutosaves().count() > 10) { while (getAutosaves().count() > 10) {
val saveToDelete = getAutosaves().minByOrNull { it.lastModified() }!! val saveToDelete = getAutosaves().minByOrNull { it.lastModified() }!!
deleteSave(saveToDelete.name()) deleteSave(saveToDelete.name())
}
} }
} }

View File

@ -889,7 +889,7 @@ class WorldScreen(
fun autoSave() { fun autoSave() {
waitingForAutosave = true waitingForAutosave = true
shouldUpdate = 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 // only enable the user to next turn once we've saved the current one
waitingForAutosave = false waitingForAutosave = false
shouldUpdate = true shouldUpdate = true