From ace4ac7731626d2392ec9420d1eb8b26e24986e2 Mon Sep 17 00:00:00 2001 From: yairm210 Date: Sun, 14 Jul 2024 17:27:27 +0300 Subject: [PATCH] Added autoreplace for deprecated modifiers --- .../ruleset/validation/UniqueAutoUpdater.kt | 62 +++++++++---------- .../ruleset/validation/UniqueValidator.kt | 10 +-- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/core/src/com/unciv/models/ruleset/validation/UniqueAutoUpdater.kt b/core/src/com/unciv/models/ruleset/validation/UniqueAutoUpdater.kt index ddcb7fac6a..b68c7c0983 100644 --- a/core/src/com/unciv/models/ruleset/validation/UniqueAutoUpdater.kt +++ b/core/src/com/unciv/models/ruleset/validation/UniqueAutoUpdater.kt @@ -1,30 +1,34 @@ package com.unciv.models.ruleset.validation +import com.unciv.models.ruleset.IRulesetObject import com.unciv.models.ruleset.Ruleset import com.unciv.models.ruleset.unique.Unique import com.unciv.utils.Log import com.unciv.utils.debug -object UniqueAutoUpdater{ +object UniqueAutoUpdater { + + private val fileToObjects: Map LinkedHashMap> = mapOf( + "Beliefs.json" to { it.beliefs }, + "Buildings.json" to { it.buildings }, + "Nations.json" to { it.nations }, + "Policies.json" to { it.policies }, + "Techs.json" to { it.technologies }, + "Terrains.json" to { it.terrains }, + "TileImprovements.json" to { it.tileImprovements }, + "UnitPromotions.json" to { it.unitPromotions }, + "UnitTypes.json" to { it.unitTypes }, + "Units.json" to { it.units }, + "Ruins.json" to { it.ruinRewards }, + // Note this does not currently + "Events.json" to { it.events } + ) fun autoupdateUniques( mod: Ruleset, replaceableUniques: HashMap = getDeprecatedReplaceableUniques(mod) ) { - val filesToReplace = listOf( - "Beliefs.json", - "Buildings.json", - "Nations.json", - "Policies.json", - "Techs.json", - "Terrains.json", - "TileImprovements.json", - "UnitPromotions.json", - "UnitTypes.json", - "Units.json", - "Ruins.json" - ) - + val filesToReplace = fileToObjects.keys val jsonFolder = mod.folderLocation!!.child("jsons") for (fileName in filesToReplace) { val file = jsonFolder.child(fileName) @@ -32,6 +36,7 @@ object UniqueAutoUpdater{ var newFileText = file.readString() for ((original, replacement) in replaceableUniques) { newFileText = newFileText.replace("\"$original\"", "\"$replacement\"") + newFileText = newFileText.replace("<$original>", "<$replacement>") // For modifiers } file.writeString(newFileText, false) } @@ -40,20 +45,7 @@ object UniqueAutoUpdater{ fun getDeprecatedReplaceableUniques(mod: Ruleset): HashMap { - - val objectsToCheck = sequenceOf( - mod.beliefs, - mod.buildings, - mod.nations, - mod.policies, - mod.technologies, - mod.terrains, - mod.tileImprovements, - mod.unitPromotions, - mod.unitTypes, - mod.units, - mod.ruinRewards - ) + val objectsToCheck = fileToObjects.values.map { it(mod) } val allDeprecatedUniques = HashSet() val deprecatedUniquesToReplacementText = HashMap() @@ -62,13 +54,16 @@ object UniqueAutoUpdater{ .flatMap { it.uniqueObjects } .filter { it.getDeprecationAnnotation() != null } - for (deprecatedUnique in deprecatedUniques) { + val deprecatedConditionals = objectsToCheck + .flatMap { it.values } + .flatMap { it.uniqueObjects } + .flatMap { it.conditionals } + .filter { it.getDeprecationAnnotation() != null } + + for (deprecatedUnique in deprecatedUniques + deprecatedConditionals) { if (allDeprecatedUniques.contains(deprecatedUnique.text)) continue allDeprecatedUniques.add(deprecatedUnique.text) - // note that this replacement does not contain conditionals attached to the original! - - var uniqueReplacementText = deprecatedUnique.getReplacementText(mod) while (Unique(uniqueReplacementText).getDeprecationAnnotation() != null) uniqueReplacementText = Unique(uniqueReplacementText).getReplacementText(mod) @@ -102,6 +97,7 @@ object UniqueAutoUpdater{ deprecatedUniquesToReplacementText[deprecatedUnique.text] = uniqueReplacementText debug("Replace \"%s\" with \"%s\"", deprecatedUnique.text, uniqueReplacementText) } + return deprecatedUniquesToReplacementText } } diff --git a/core/src/com/unciv/models/ruleset/validation/UniqueValidator.kt b/core/src/com/unciv/models/ruleset/validation/UniqueValidator.kt index 51fe3c0ca2..579a975dc6 100644 --- a/core/src/com/unciv/models/ruleset/validation/UniqueValidator.kt +++ b/core/src/com/unciv/models/ruleset/validation/UniqueValidator.kt @@ -137,7 +137,7 @@ class UniqueValidator(val ruleset: Ruleset) { var text = "$prefix contains the conditional \"${conditional.text}\"," + " which is of an unknown type!" - val similarConditionals = UniqueType.values().filter { + val similarConditionals = UniqueType.entries.filter { getRelativeTextDistance( it.placeholderText, conditional.placeholderText @@ -169,7 +169,7 @@ class UniqueValidator(val ruleset: Ruleset) { ) if (unique.type in resourceUniques && conditional.type in resourceConditionals - && ruleset.tileResources[conditional.params.last()]?.let { it.hasUnique(UniqueType.CityResource) } == true) + && ruleset.tileResources[conditional.params.last()]?.hasUnique(UniqueType.CityResource) == true) rulesetErrors.add( "$prefix contains the conditional \"${conditional.text}\"," + " which references a citywide resource. This is not a valid conditional for a resource uniques, " + @@ -184,14 +184,14 @@ class UniqueValidator(val ruleset: Ruleset) { continue rulesetErrors.add( - "$prefix contains conditional \"${conditional.text}\"." + + "$prefix contains modifier \"${conditional.text}\"." + " This contains the parameter ${complianceError.parameterName} which does not fit parameter type" + " ${complianceError.acceptableParameterTypes.joinToString(" or ") { it.parameterName }} !", complianceError.errorSeverity.getRulesetErrorSeverity(), uniqueContainer, unique ) } - addDeprecationAnnotationErrors(conditional, "$prefix contains conditional \"${conditional.text}\" which", rulesetErrors, uniqueContainer) + addDeprecationAnnotationErrors(conditional, "$prefix contains modifier \"${conditional.text}\" which", rulesetErrors, uniqueContainer) } private fun addDeprecationAnnotationErrors( @@ -283,7 +283,7 @@ class UniqueValidator(val ruleset: Ruleset) { } private fun tryFixUnknownUnique(unique: Unique, uniqueContainer: IHasUniques?, prefix: String): RulesetErrorList { - val similarUniques = UniqueType.values().filter { + val similarUniques = UniqueType.entries.filter { getRelativeTextDistance( it.placeholderText, unique.placeholderText