More error handling for out of memory errors

This commit is contained in:
yairm210 2021-11-12 00:19:52 +02:00
parent c45b6c8f7a
commit 57c033ff34
4 changed files with 24 additions and 14 deletions

View File

@ -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)

View File

@ -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()
}

View File

@ -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()
}
}
}
}

View File

@ -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()