Resolved #3048 - Fixed ANRs on 'Resume' on huge save files

This commit is contained in:
Yair Morgenstern 2020-09-08 19:30:56 +03:00
parent bf2742350d
commit baa7044fd1

View File

@ -7,6 +7,7 @@ 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.Align
import com.unciv.logic.GameInfo
import com.unciv.logic.GameSaver
import com.unciv.logic.GameStarter
import com.unciv.logic.map.mapgenerator.MapGenerator
@ -165,15 +166,25 @@ class MainMenuScreen: CameraStageBaseScreen() {
private fun autoLoadGame() {
try {
game.loadGame(autosave)
dispose()
}
catch (outOfMemory:OutOfMemoryError){
ResponsePopup("Not enough memory on phone to load game!", this)
}
catch (ex: Exception) { // silent fail if we can't read the autosave
ResponsePopup("Cannot resume game!", this)
ResponsePopup("Loading...", this)
thread { // Load game from file to class on separate thread to avoid ANR...
val savedGame: GameInfo
try {
savedGame = GameSaver.loadGameByName(autosave)
} catch (outOfMemory: OutOfMemoryError) {
ResponsePopup("Not enough memory on phone to load game!", this)
return@thread
}
Gdx.app.postRunnable { /// ... and load it into the screen on main thread for GL context
try {
game.loadGame(savedGame)
dispose()
} catch (ex: Exception) { // silent fail if we can't read the autosave
ResponsePopup("Cannot resume game!", this)
}
}
}
}