From 57c033ff3411b6d7db922bf7f53c63aacbc69314 Mon Sep 17 00:00:00 2001 From: yairm210 Date: Fri, 12 Nov 2021 00:19:52 +0200 Subject: [PATCH] More error handling for out of memory errors --- .../app/LimitOrientationsHelperAndroid.kt | 2 +- .../unciv/app/MultiplayerTurnCheckWorker.kt | 4 ++- core/src/com/unciv/MainMenuScreen.kt | 30 ++++++++++++------- .../com/unciv/ui/worldscreen/WorldScreen.kt | 2 +- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/android/src/com/unciv/app/LimitOrientationsHelperAndroid.kt b/android/src/com/unciv/app/LimitOrientationsHelperAndroid.kt index 918c081b51..97a4a14338 100644 --- a/android/src/com/unciv/app/LimitOrientationsHelperAndroid.kt +++ b/android/src/com/unciv/app/LimitOrientationsHelperAndroid.kt @@ -53,7 +53,7 @@ class LimitOrientationsHelperAndroid(private val activity: Activity) : LimitOrie GameSettingsPreview() } else try { GameSaver.json().fromJson(GameSettingsPreview::class.java, settingsFile.reader()) - } catch (ex: java.lang.Exception) { + } catch (throwable: Throwable) { GameSettingsPreview() } allowPortrait(setting.allowAndroidPortrait) diff --git a/android/src/com/unciv/app/MultiplayerTurnCheckWorker.kt b/android/src/com/unciv/app/MultiplayerTurnCheckWorker.kt index 4719bd816e..ca59c1a160 100644 --- a/android/src/com/unciv/app/MultiplayerTurnCheckWorker.kt +++ b/android/src/com/unciv/app/MultiplayerTurnCheckWorker.kt @@ -173,7 +173,7 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame gameIds[count] = GameSaver.getGameIdFromFile(gameFile) gameNames[count] = gameFile.name() count++ - } catch (ex: Exception) { + } catch (ex: Throwable) { //only getGameIdFromFile can throw an exception //nothing will be added to the arrays if it fails //just skip one file @@ -287,6 +287,8 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame val inputDataFailIncrease = Data.Builder().putAll(inputData).putInt(FAIL_COUNT, failCount + 1).build() enqueue(applicationContext, 1, inputDataFailIncrease) } + } catch (outOfMemory: OutOfMemoryError){ // no point in trying multiple times if this was an oom error + return Result.failure() } return Result.success() } diff --git a/core/src/com/unciv/MainMenuScreen.kt b/core/src/com/unciv/MainMenuScreen.kt index ad7ace2113..f49568739f 100644 --- a/core/src/com/unciv/MainMenuScreen.kt +++ b/core/src/com/unciv/MainMenuScreen.kt @@ -198,21 +198,31 @@ class MainMenuScreen: CameraStageBaseScreen() { val loadingPopup = Popup(this) loadingPopup.addGoodSizedLabel("Loading...") loadingPopup.open() - thread { // Load game from file to class on separate thread to avoid ANR... - var savedGame: GameInfo - try { - savedGame = GameSaver.loadGameByName(autosave) - } catch (outOfMemory: OutOfMemoryError) { + thread { + // Load game from file to class on separate thread to avoid ANR... + fun outOfMemory() { Gdx.app.postRunnable { loadingPopup.close() ToastPopup("Not enough memory on phone to load game!", this) } + } + + var savedGame: GameInfo + try { + savedGame = GameSaver.loadGameByName(autosave) + } catch (oom: OutOfMemoryError) { + outOfMemory() 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 // This can help for situations when the autosave is corrupted try { - val autosaves = GameSaver.getSaves().filter { it.name() != autosave && it.name().startsWith(autosave) } - savedGame = GameSaver.loadGameFromFile(autosaves.maxByOrNull { it.lastModified() }!!) + val autosaves = GameSaver.getSaves() + .filter { it.name() != autosave && it.name().startsWith(autosave) } + savedGame = + GameSaver.loadGameFromFile(autosaves.maxByOrNull { it.lastModified() }!!) + } catch (oom: OutOfMemoryError) { // The autosave could have oom problems as well... smh + outOfMemory() + return@thread } catch (ex: Exception) { Gdx.app.postRunnable { loadingPopup.close() @@ -226,11 +236,9 @@ class MainMenuScreen: CameraStageBaseScreen() { try { game.loadGame(savedGame) dispose() - } catch (outOfMemory: OutOfMemoryError) { - loadingPopup.close() - ToastPopup("Not enough memory on phone to load game!", this) + } catch (oom: OutOfMemoryError) { + outOfMemory() } - } } } diff --git a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt index 9c54057d73..d0f23f92ed 100644 --- a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt +++ b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt @@ -344,7 +344,7 @@ class WorldScreen(val gameInfo: GameInfo, val viewingCiv:CivilizationInfo) : Cam Gdx.app.postRunnable { createNewWorldScreen(latestGame) } } - } catch (ex: Exception) { + } catch (ex: Throwable) { Gdx.app.postRunnable { val couldntDownloadLatestGame = Popup(this) couldntDownloadLatestGame.addGoodSizedLabel("Couldn't download the latest game state!").row()