Show json parsing errors for mods in the options menu

This commit is contained in:
Yair Morgenstern
2022-02-17 23:55:03 +02:00
parent a38034ed91
commit 4fe8450b69
2 changed files with 19 additions and 9 deletions

View File

@ -729,7 +729,8 @@ class Ruleset {
* save all of the loaded rulesets somewhere for later use * save all of the loaded rulesets somewhere for later use
* */ * */
object RulesetCache : HashMap<String,Ruleset>() { object RulesetCache : HashMap<String,Ruleset>() {
fun loadRulesets(consoleMode: Boolean = false, printOutput: Boolean = false, noMods: Boolean = false) { /** Returns error lines from loading the rulesets, so we can display the errors to users */
fun loadRulesets(consoleMode: Boolean = false, printOutput: Boolean = false, noMods: Boolean = false) :List<String> {
clear() clear()
for (ruleset in BaseRuleset.values()) { for (ruleset in BaseRuleset.values()) {
val fileName = "jsons/${ruleset.fullName}" val fileName = "jsons/${ruleset.fullName}"
@ -742,11 +743,12 @@ object RulesetCache : HashMap<String,Ruleset>() {
} }
} }
if (noMods) return if (noMods) return listOf()
val modsHandles = if (consoleMode) FileHandle("mods").list() val modsHandles = if (consoleMode) FileHandle("mods").list()
else Gdx.files.local("mods").list() else Gdx.files.local("mods").list()
val errorLines = ArrayList<String>()
for (modFolder in modsHandles) { for (modFolder in modsHandles) {
if (modFolder.name().startsWith('.')) continue if (modFolder.name().startsWith('.')) continue
if (!modFolder.isDirectory) continue if (!modFolder.isDirectory) continue
@ -761,13 +763,13 @@ object RulesetCache : HashMap<String,Ruleset>() {
println(modRuleset.checkModLinks().getErrorText()) println(modRuleset.checkModLinks().getErrorText())
} }
} catch (ex: Exception) { } catch (ex: Exception) {
if (printOutput) { errorLines += "Exception loading mod '${modFolder.name()}':"
println("Exception loading mod '${modFolder.name()}':") errorLines += " ${ex.localizedMessage}"
println(" ${ex.localizedMessage}") errorLines += " ${ex.cause?.localizedMessage}"
println(" ${ex.cause?.localizedMessage}")
}
} }
} }
if (printOutput) for (line in errorLines) println(line)
return errorLines
} }

View File

@ -264,7 +264,6 @@ class OptionsPopup(val previousScreen: BaseScreen) : Popup(previousScreen) {
private fun getModCheckTab() = Table(BaseScreen.skin).apply { private fun getModCheckTab() = Table(BaseScreen.skin).apply {
defaults().pad(10f).align(Align.top) defaults().pad(10f).align(Align.top)
val reloadModsButton = "Reload mods".toTextButton().onClick { val reloadModsButton = "Reload mods".toTextButton().onClick {
RulesetCache.loadRulesets()
runModChecker(modCheckCheckBox!!.isChecked) runModChecker(modCheckCheckBox!!.isChecked)
} }
add(reloadModsButton).row() add(reloadModsButton).row()
@ -277,10 +276,20 @@ class OptionsPopup(val previousScreen: BaseScreen) : Popup(previousScreen) {
} }
private fun runModChecker(complex: Boolean = false) { private fun runModChecker(complex: Boolean = false) {
modCheckFirstRun = false modCheckFirstRun = false
if (modCheckCheckBox == null) return if (modCheckCheckBox == null) return
modCheckResultTable.clear() modCheckResultTable.clear()
val rulesetErrors = RulesetCache.loadRulesets()
if (rulesetErrors.isNotEmpty()) {
val errorTable = Table().apply { defaults().pad(2f) }
for (rulesetError in rulesetErrors)
errorTable.add(rulesetError.toLabel()).width(stage.width / 2).row()
modCheckResultTable.add(errorTable)
}
modCheckResultTable.add("Checking mods for errors...".toLabel()).row() modCheckResultTable.add("Checking mods for errors...".toLabel()).row()
modCheckCheckBox!!.disable() modCheckCheckBox!!.disable()
@ -437,7 +446,6 @@ class OptionsPopup(val previousScreen: BaseScreen) : Popup(previousScreen) {
} }
val toastText = "Uniques updated!" val toastText = "Uniques updated!"
ToastPopup(toastText, screen).open(true) ToastPopup(toastText, screen).open(true)
RulesetCache.loadRulesets()
runModChecker() runModChecker()
} }