Harden map editor map loader against most bad maps (#4711)

* Harden map editor map loader against most bad maps

* Harden map editor map loader against most bad maps - patch1
This commit is contained in:
SomeTroglodyte 2021-08-02 18:03:16 +02:00 committed by GitHub
parent 5511c80eb5
commit 7d52cfbcab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 9 deletions

View File

@ -879,6 +879,7 @@ Clear current map =
Save map = Save map =
Download map = Download map =
Loading... = Loading... =
Error loading map! =
Filter: = Filter: =
OK = OK =
Exit map editor = Exit map editor =

View File

@ -10,6 +10,7 @@ import com.badlogic.gdx.utils.Json
import com.unciv.logic.MapSaver import com.unciv.logic.MapSaver
import com.unciv.logic.map.MapType import com.unciv.logic.map.MapType
import com.unciv.logic.map.TileMap import com.unciv.logic.map.TileMap
import com.unciv.models.ruleset.RulesetCache
import com.unciv.models.translations.tr import com.unciv.models.translations.tr
import com.unciv.ui.pickerscreens.PickerScreen import com.unciv.ui.pickerscreens.PickerScreen
import com.unciv.ui.saves.Gzip import com.unciv.ui.saves.Gzip
@ -54,17 +55,45 @@ class SaveAndLoadMapScreen(mapToSave: TileMap?, save:Boolean = false, previousSc
} else { } else {
rightSideButton.setText("Load map".tr()) rightSideButton.setText("Load map".tr())
rightSideButtonAction = { rightSideButtonAction = {
thread { thread(name = "MapLoader") {
var popup: Popup? = null
var needPopup = true // loadMap can fail faster than postRunnable runs
Gdx.app.postRunnable { Gdx.app.postRunnable {
val popup = Popup(this) if (!needPopup) return@postRunnable
popup.addGoodSizedLabel("Loading...") popup = Popup(this).apply {
popup.open() addGoodSizedLabel("Loading...")
open()
}
} }
val map = MapSaver.loadMap(chosenMap!!) try {
Gdx.app.postRunnable { val map = MapSaver.loadMap(chosenMap!!)
Gdx.input.inputProcessor = null // This is to stop ANRs happening here, until the map editor screen sets up. val missingMods = map.mapParameters.mods.filter { it !in RulesetCache }
game.setScreen(MapEditorScreen(map)) if (missingMods.isNotEmpty()) {
dispose() Gdx.app.postRunnable {
needPopup = false
popup?.close()
ToastPopup("Missing mods: [${missingMods.joinToString()}]", this)
}
} else Gdx.app.postRunnable {
Gdx.input.inputProcessor = null // This is to stop ANRs happening here, until the map editor screen sets up.
try {
game.setScreen(MapEditorScreen(map))
dispose()
} catch (ex: Throwable) {
needPopup = false
popup?.close()
println("Error displaying map \"$chosenMap\": ${ex.localizedMessage}")
Gdx.input.inputProcessor = stage
ToastPopup("Error loading map!", this)
}
}
} catch (ex: Throwable) {
needPopup = false
Gdx.app.postRunnable {
popup?.close()
println("Error loading map \"$chosenMap\": ${ex.localizedMessage}")
ToastPopup("Error loading map!", this)
}
} }
} }
} }