From 2854d88e11e6b89632f14e871e98191ce072b681 Mon Sep 17 00:00:00 2001 From: Xander Lenstra <71121390+xlenstra@users.noreply.github.com> Date: Tue, 9 Nov 2021 05:21:24 +0100 Subject: [PATCH] Cleaned up some code from the split (#5652) --- core/src/com/unciv/UncivGame.kt | 2 +- core/src/com/unciv/logic/GameStarter.kt | 2 +- .../unciv/models/metadata/GameParameters.kt | 4 ++++ core/src/com/unciv/models/ruleset/Ruleset.kt | 23 +++++++++++++++++- .../unciv/ui/mapeditor/MapEditorMenuPopup.kt | 23 ++---------------- .../com/unciv/ui/mapeditor/NewMapScreen.kt | 23 +++--------------- .../ui/newgamescreen/GameOptionsTable.kt | 24 ++++--------------- .../unciv/ui/newgamescreen/MapOptionsTable.kt | 2 +- .../unciv/ui/newgamescreen/NewGameScreen.kt | 2 +- .../src/com/unciv/ui/utils/CrashController.kt | 2 +- 10 files changed, 40 insertions(+), 67 deletions(-) diff --git a/core/src/com/unciv/UncivGame.kt b/core/src/com/unciv/UncivGame.kt index 2223645b60..fadae23534 100644 --- a/core/src/com/unciv/UncivGame.kt +++ b/core/src/com/unciv/UncivGame.kt @@ -129,7 +129,7 @@ class UncivGame(parameters: UncivGameParameters) : Game() { this.gameInfo = gameInfo ImageGetter.setNewRuleset(gameInfo.ruleSet) // Clone the mod list and add the base ruleset to it - val fullModList = (gameInfo.gameParameters.mods.toHashSet()).apply { add(gameInfo.gameParameters.baseRuleset) } + val fullModList = gameInfo.gameParameters.getModsAndBaseRuleset() musicController.setModList(fullModList) Gdx.input.inputProcessor = null // Since we will set the world screen when we're ready, if (gameInfo.civilizations.count { it.playerType == PlayerType.Human } > 1 && !gameInfo.gameParameters.isOnlineMultiplayer) diff --git a/core/src/com/unciv/logic/GameStarter.kt b/core/src/com/unciv/logic/GameStarter.kt index 4f5ce64a7a..5dcaeaa122 100644 --- a/core/src/com/unciv/logic/GameStarter.kt +++ b/core/src/com/unciv/logic/GameStarter.kt @@ -33,7 +33,7 @@ object GameStarter { // We need to remove the dead mods so there aren't problems later. gameSetupInfo.gameParameters.mods.removeAll { !RulesetCache.containsKey(it) } - // [Temporary] If we have a base ruleset in the mod list, we make that our base ruleset + // [TEMPORARY] If we have a base ruleset in the mod list, we make that our base ruleset val baseRulesetInMods = gameSetupInfo.gameParameters.mods.firstOrNull { RulesetCache[it]!!.modOptions.isBaseRuleset } if (baseRulesetInMods != null) gameSetupInfo.gameParameters.baseRuleset = baseRulesetInMods diff --git a/core/src/com/unciv/models/metadata/GameParameters.kt b/core/src/com/unciv/models/metadata/GameParameters.kt index 99c0398e1b..8667dafa95 100644 --- a/core/src/com/unciv/models/metadata/GameParameters.kt +++ b/core/src/com/unciv/models/metadata/GameParameters.kt @@ -73,4 +73,8 @@ class GameParameters { // Default values are the default new game yield(baseRuleset) yield(if (mods.isEmpty()) "no mods" else mods.joinToString(",", "mods=(", ")", 6) ) }.joinToString(prefix = "(", postfix = ")") + + fun getModsAndBaseRuleset(): HashSet { + return mods.toHashSet().apply { add(baseRuleset) } + } } \ No newline at end of file diff --git a/core/src/com/unciv/models/ruleset/Ruleset.kt b/core/src/com/unciv/models/ruleset/Ruleset.kt index 28a2e23e61..f77a4650db 100644 --- a/core/src/com/unciv/models/ruleset/Ruleset.kt +++ b/core/src/com/unciv/models/ruleset/Ruleset.kt @@ -269,7 +269,7 @@ class Ruleset { /** Used for displaying a RuleSet's name */ override fun toString() = when { name.isNotEmpty() -> name - mods.isEmpty() -> BaseRuleset.Civ_V_Vanilla.fullName //todo differentiate once more than 1 BaseRuleset + mods.size == 1 && RulesetCache[mods.first()]!!.modOptions.isBaseRuleset -> mods.first() else -> "Combined RuleSet" } @@ -634,6 +634,27 @@ object RulesetCache : HashMap() { fun getBaseRuleset() = this[BaseRuleset.Civ_V_Vanilla.fullName]!!.clone() // safeguard, so no-one edits the base ruleset by mistake + fun getSortedBaseRulesets(): List { + val baseRulesets = values + .filter { it.modOptions.isBaseRuleset } + .map { it.name } + .distinct() + if (baseRulesets.size < 2) return baseRulesets + + // We sort the base rulesets such that the ones unciv provides are on the top, + // and the rest is alphabetically ordered. + return baseRulesets.sortedWith( + compareBy( + { ruleset -> + BaseRuleset.values() + .firstOrNull { br -> br.fullName == ruleset }?.ordinal + ?: BaseRuleset.values().size + }, + { it } + ) + ) + } + /** * Creates a combined [Ruleset] from a list of mods. If no baseRuleset is listed in [mods], * then the vanilla Ruleset is included automatically. diff --git a/core/src/com/unciv/ui/mapeditor/MapEditorMenuPopup.kt b/core/src/com/unciv/ui/mapeditor/MapEditorMenuPopup.kt index 3f45fc8d1f..c7a5419ac0 100644 --- a/core/src/com/unciv/ui/mapeditor/MapEditorMenuPopup.kt +++ b/core/src/com/unciv/ui/mapeditor/MapEditorMenuPopup.kt @@ -41,8 +41,6 @@ class MapEditorMenuPopup(var mapEditorScreen: MapEditorScreen): Popup(mapEditorS val mods = mapParameters.mods val baseRuleset = mapParameters.baseRuleset - // FIXME - checkboxTable = ModCheckboxTable(mods, baseRuleset, mapEditorScreen) { ruleset.clear() val newRuleset = RulesetCache.getComplexRuleset(mods, baseRuleset) @@ -106,25 +104,8 @@ class MapEditorMenuPopup(var mapEditorScreen: MapEditorScreen): Popup(mapEditorS private fun getBaseRulesetSelectBox(): Table? { val rulesetSelectionBox = Table() - val baseRulesets = - RulesetCache.values - .filter { it.modOptions.isBaseRuleset } - .map { it.name } - .distinct() - if (baseRulesets.size < 2) return null - - // We sort the base rulesets such that the ones unciv provides are on the top, - // and the rest is alphabetically ordered. - val sortedBaseRulesets = baseRulesets.sortedWith( - compareBy( - { ruleset -> - BaseRuleset.values() - .firstOrNull { br -> br.fullName == ruleset }?.ordinal - ?: BaseRuleset.values().size - }, - { it } - ) - ) + val sortedBaseRulesets = RulesetCache.getSortedBaseRulesets() + if (sortedBaseRulesets.size < 2) return null rulesetSelectionBox.add("{Base Ruleset}:".toLabel()).left() val selectBox = TranslatedSelectBox(sortedBaseRulesets, mapParameters.baseRuleset, CameraStageBaseScreen.skin) diff --git a/core/src/com/unciv/ui/mapeditor/NewMapScreen.kt b/core/src/com/unciv/ui/mapeditor/NewMapScreen.kt index 915014f15a..f228dc5304 100644 --- a/core/src/com/unciv/ui/mapeditor/NewMapScreen.kt +++ b/core/src/com/unciv/ui/mapeditor/NewMapScreen.kt @@ -139,27 +139,10 @@ class NewMapScreen(val mapParameters: MapParameters = getDefaultParameters()) : private fun getBaseRulesetSelectBox(): Table? { val rulesetSelectionBox = Table() - - val baseRulesets = - RulesetCache.values - .filter { it.modOptions.isBaseRuleset } - .map { it.name } - .distinct() - if (baseRulesets.size < 2) return null - // We sort the base rulesets such that the ones unciv provides are on the top, - // and the rest is alphabetically ordered. - val sortedBaseRulesets = baseRulesets.sortedWith( - compareBy( - { ruleset -> - BaseRuleset.values() - .firstOrNull { br -> br.fullName == ruleset }?.ordinal - ?: BaseRuleset.values().size - }, - { it } - ) - ) - + val sortedBaseRulesets = RulesetCache.getSortedBaseRulesets() + if (sortedBaseRulesets.size < 2) return null + rulesetSelectionBox.add("{Base Ruleset}:".toLabel()).left() val selectBox = TranslatedSelectBox(sortedBaseRulesets, mapParameters.baseRuleset, skin) diff --git a/core/src/com/unciv/ui/newgamescreen/GameOptionsTable.kt b/core/src/com/unciv/ui/newgamescreen/GameOptionsTable.kt index 4756962d5b..330ea0f8f4 100644 --- a/core/src/com/unciv/ui/newgamescreen/GameOptionsTable.kt +++ b/core/src/com/unciv/ui/newgamescreen/GameOptionsTable.kt @@ -141,25 +141,9 @@ class GameOptionsTable( } private fun Table.addBaseRulesetSelectBox() { - val baseRulesets = - RulesetCache.values - .filter { it.modOptions.isBaseRuleset } - .map { it.name } - .distinct() - if (baseRulesets.size < 2) return + val sortedBaseRulesets = RulesetCache.getSortedBaseRulesets() + if (sortedBaseRulesets.size < 2) return - // We sort the base rulesets such that the ones unciv provides are on the top, - // and the rest is alphabetically ordered. - val sortedBaseRulesets = baseRulesets.sortedWith( - compareBy( - { ruleset -> - BaseRuleset.values() - .firstOrNull { br -> br.fullName == ruleset }?.ordinal - ?: BaseRuleset.values().size - }, - { it } - ) - ) addSelectBox( "{Base Ruleset}:", sortedBaseRulesets, @@ -252,7 +236,7 @@ class GameOptionsTable( ruleset.modOptions = newRuleset.modOptions ImageGetter.setNewRuleset(ruleset) - UncivGame.Current.musicController.setModList(gameParameters.mods.toHashSet().apply { add(gameParameters.baseRuleset) }) + UncivGame.Current.musicController.setModList(gameParameters.getModsAndBaseRuleset()) } fun getModCheckboxes(isPortrait: Boolean = false): ModCheckboxTable { @@ -262,7 +246,7 @@ class GameOptionsTable( } private fun onChooseMod(mod: String) { - val activeMods: LinkedHashSet = LinkedHashSet(gameParameters.mods + gameParameters.baseRuleset) + val activeMods: LinkedHashSet = LinkedHashSet(gameParameters.getModsAndBaseRuleset()) UncivGame.Current.translations.translationActiveMods = activeMods reloadRuleset() update() diff --git a/core/src/com/unciv/ui/newgamescreen/MapOptionsTable.kt b/core/src/com/unciv/ui/newgamescreen/MapOptionsTable.kt index 99fdc85548..3dc4abc0f0 100644 --- a/core/src/com/unciv/ui/newgamescreen/MapOptionsTable.kt +++ b/core/src/com/unciv/ui/newgamescreen/MapOptionsTable.kt @@ -103,7 +103,7 @@ class MapOptionsTable(private val newGameScreen: NewGameScreen): Table() { mapParameters.name = mapFile.name() newGameScreen.gameSetupInfo.mapFile = mapFile newGameScreen.gameSetupInfo.gameParameters.mods = LinkedHashSet(map.mapParameters.mods.filter { RulesetCache[it]?.modOptions?.isBaseRuleset != true }) - newGameScreen.gameSetupInfo.gameParameters.baseRuleset = map.mapParameters.mods.firstOrNull { RulesetCache[it]?.modOptions?.isBaseRuleset == true } ?: RulesetCache.getBaseRuleset().name + newGameScreen.gameSetupInfo.gameParameters.baseRuleset = map.mapParameters.mods.firstOrNull { RulesetCache[it]?.modOptions?.isBaseRuleset == true } ?: map.mapParameters.baseRuleset newGameScreen.updateRuleset() newGameScreen.updateTables() } diff --git a/core/src/com/unciv/ui/newgamescreen/NewGameScreen.kt b/core/src/com/unciv/ui/newgamescreen/NewGameScreen.kt index 95a91fa086..90071fc46b 100644 --- a/core/src/com/unciv/ui/newgamescreen/NewGameScreen.kt +++ b/core/src/com/unciv/ui/newgamescreen/NewGameScreen.kt @@ -228,7 +228,7 @@ class NewGameScreen( ruleset.clear() ruleset.add(RulesetCache.getComplexRuleset(gameSetupInfo.gameParameters.mods, gameSetupInfo.gameParameters.baseRuleset)) ImageGetter.setNewRuleset(ruleset) - game.musicController.setModList(gameSetupInfo.gameParameters.mods.toHashSet().apply { add(gameSetupInfo.gameParameters.baseRuleset) }) + game.musicController.setModList(gameSetupInfo.gameParameters.getModsAndBaseRuleset()) } fun lockTables() { diff --git a/core/src/com/unciv/ui/utils/CrashController.kt b/core/src/com/unciv/ui/utils/CrashController.kt index 7b64b1863f..d85568912d 100644 --- a/core/src/com/unciv/ui/utils/CrashController.kt +++ b/core/src/com/unciv/ui/utils/CrashController.kt @@ -62,7 +62,7 @@ interface CrashController { val zippedGameInfo = Json().toJson(gameInfo).let { Gzip.zip(it) } CrashReport( zippedGameInfo, - LinkedHashSet(listOf(*gameInfo.gameParameters.mods.toTypedArray(), gameInfo.gameParameters.baseRuleset)), + LinkedHashSet(gameInfo.gameParameters.getModsAndBaseRuleset()), version ) }