From 9b2c1b2aceaa35f91b1466c34b234e5ef8d8d2b0 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Sat, 14 Nov 2020 22:58:05 +0200 Subject: [PATCH] We now try to load the last tturn-autosave whn loading the regular autosaves failed (e.g. when the save file is corrupt) --- core/src/com/unciv/MainMenuScreen.kt | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/core/src/com/unciv/MainMenuScreen.kt b/core/src/com/unciv/MainMenuScreen.kt index 099da6a6b2..38e503cc04 100644 --- a/core/src/com/unciv/MainMenuScreen.kt +++ b/core/src/com/unciv/MainMenuScreen.kt @@ -6,6 +6,7 @@ import com.badlogic.gdx.scenes.scene2d.Touchable import com.badlogic.gdx.scenes.scene2d.actions.Actions import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane import com.badlogic.gdx.scenes.scene2d.ui.Table +import com.badlogic.gdx.utils.SerializationException import com.unciv.logic.GameInfo import com.unciv.logic.GameSaver import com.unciv.logic.GameStarter @@ -166,15 +167,20 @@ class MainMenuScreen: CameraStageBaseScreen() { private fun autoLoadGame() { ToastPopup("Loading...", this) thread { // Load game from file to class on separate thread to avoid ANR... - val savedGame: GameInfo + var savedGame: GameInfo try { savedGame = GameSaver.loadGameByName(autosave) } catch (outOfMemory: OutOfMemoryError) { ToastPopup("Not enough memory on phone to load game!", this) return@thread - } catch (ex: Exception) { // silent fail if we can't read the autosave for any reason - ToastPopup("Cannot resume game!", this) - return@thread + } catch (ex: Exception) { // silent fail if we can't read the autosave for any reason - try to load the last autosave by turn number first + try { + val autosaves = GameSaver.getSaves().filter { it.name() != autosave && it.name().startsWith(autosave) } + savedGame = GameSaver.loadGameFromFile(autosaves.maxBy { it.lastModified() }!!) + } catch (ex: Exception) { + ToastPopup("Cannot resume game!", this) + return@thread + } } Gdx.app.postRunnable { /// ... and load it into the screen on main thread for GL context