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.ScrollPane
import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.utils.Align import com.badlogic.gdx.utils.Align
import com.unciv.logic.GameInfo
import com.unciv.logic.GameSaver import com.unciv.logic.GameSaver
import com.unciv.logic.GameStarter import com.unciv.logic.GameStarter
import com.unciv.logic.map.mapgenerator.MapGenerator import com.unciv.logic.map.mapgenerator.MapGenerator
@ -165,16 +166,26 @@ class MainMenuScreen: CameraStageBaseScreen() {
private fun autoLoadGame() { private fun autoLoadGame() {
ResponsePopup("Loading...", this)
thread { // Load game from file to class on separate thread to avoid ANR...
val savedGame: GameInfo
try { try {
game.loadGame(autosave) savedGame = GameSaver.loadGameByName(autosave)
dispose() } catch (outOfMemory: OutOfMemoryError) {
}
catch (outOfMemory:OutOfMemoryError){
ResponsePopup("Not enough memory on phone to load game!", this) ResponsePopup("Not enough memory on phone to load game!", this)
return@thread
} }
catch (ex: Exception) { // silent fail if we can't read the autosave
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) ResponsePopup("Cannot resume game!", this)
} }
}
}
} }
private fun quickstartNewGame() { private fun quickstartNewGame() {