diff --git a/core/src/com/unciv/models/ruleset/Ruleset.kt b/core/src/com/unciv/models/ruleset/Ruleset.kt index 0e07e70119..490e473df0 100644 --- a/core/src/com/unciv/models/ruleset/Ruleset.kt +++ b/core/src/com/unciv/models/ruleset/Ruleset.kt @@ -375,25 +375,10 @@ class Ruleset { // The tests are RulesetInvariant in nature, but RulesetSpecific is called for _all_ objects, invariant is not. continue - val deprecationAnnotation = unique.type.declaringClass.getField(unique.type.name) - .getAnnotation(Deprecated::class.java) + + val deprecationAnnotation = unique.getDeprecationAnnotation() if (deprecationAnnotation != null) { - var replacementUniqueText = deprecationAnnotation.replaceWith.expression - val deprecatedUniquePlaceholders = unique.type.text.getPlaceholderParameters() - - // Here, for once, we DO want the conditional placeholder parameters together with the regular ones, - // so we cheat the conditional detector by removing the '<' - for (parameter in replacementUniqueText.replace('<',' ').getPlaceholderParameters()) { - val parameterNumberInDeprecatedUnique = - deprecatedUniquePlaceholders.indexOf(parameter.removePrefix("+").removePrefix("-")) - if (parameterNumberInDeprecatedUnique == -1) continue - var replacementText = unique.params[parameterNumberInDeprecatedUnique] - if (parameter.startsWith('+')) replacementText = "+$replacementText" - else if(parameter.startsWith('-')) replacementText = "-$replacementText" - replacementUniqueText = - replacementUniqueText.replace("[$parameter]", "[$replacementText]") - } - + val replacementUniqueText = unique.getReplacementText() val deprecationText = "$name's unique \"${unique.text}\" is deprecated ${deprecationAnnotation.message}," + if (deprecationAnnotation.replaceWith.expression != "") " replace with \"${replacementUniqueText}\"" else "" diff --git a/core/src/com/unciv/models/ruleset/unique/Unique.kt b/core/src/com/unciv/models/ruleset/unique/Unique.kt index 7277a7e546..b428901203 100644 --- a/core/src/com/unciv/models/ruleset/unique/Unique.kt +++ b/core/src/com/unciv/models/ruleset/unique/Unique.kt @@ -46,6 +46,28 @@ class Unique(val text: String, val sourceObjectType: UniqueTarget? = null, val s return true } + fun getDeprecationAnnotation(): Deprecated? = type?.getDeprecationAnnotation() + + fun getReplacementText(): String { + val deprecationAnnotation = getDeprecationAnnotation() ?: return "" + var replacementUniqueText = deprecationAnnotation.replaceWith.expression + val deprecatedUniquePlaceholders = type!!.text.getPlaceholderParameters() + + // Here, for once, we DO want the conditional placeholder parameters together with the regular ones, + // so we cheat the conditional detector by removing the '<' + for (parameter in replacementUniqueText.replace('<',' ').getPlaceholderParameters()) { + val parameterNumberInDeprecatedUnique = + deprecatedUniquePlaceholders.indexOf(parameter.removePrefix("+").removePrefix("-")) + if (parameterNumberInDeprecatedUnique == -1) continue + var replacementText = params[parameterNumberInDeprecatedUnique] + if (parameter.startsWith('+')) replacementText = "+$replacementText" + else if(parameter.startsWith('-')) replacementText = "-$replacementText" + replacementUniqueText = + replacementUniqueText.replace("[$parameter]", "[$replacementText]") + } + return replacementUniqueText + } + private fun conditionalApplies( condition: Unique, state: StateForConditionals diff --git a/core/src/com/unciv/models/ruleset/unique/UniqueType.kt b/core/src/com/unciv/models/ruleset/unique/UniqueType.kt index b0b46fa6aa..732ba1b9ae 100644 --- a/core/src/com/unciv/models/ruleset/unique/UniqueType.kt +++ b/core/src/com/unciv/models/ruleset/unique/UniqueType.kt @@ -777,5 +777,8 @@ enum class UniqueType(val text: String, vararg targets: UniqueTarget, val flags: } return errorList } + + fun getDeprecationAnnotation(): Deprecated? = declaringClass.getField(name) + .getAnnotation(Deprecated::class.java) } diff --git a/core/src/com/unciv/models/translations/TranslationFileWriter.kt b/core/src/com/unciv/models/translations/TranslationFileWriter.kt index 7a7981b1ca..20b7d82d1c 100644 --- a/core/src/com/unciv/models/translations/TranslationFileWriter.kt +++ b/core/src/com/unciv/models/translations/TranslationFileWriter.kt @@ -95,8 +95,7 @@ object TranslationFileWriter { linesToTranslate.add("\n\n#################### Lines from Unique Types #######################\n") for (unique in UniqueType.values()) { - val deprecationAnnotation = unique.declaringClass.getField(unique.name) - .getAnnotation(Deprecated::class.java) + val deprecationAnnotation = unique.getDeprecationAnnotation() if (deprecationAnnotation != null) continue if (unique.flags.contains(UniqueFlag.HiddenToUsers)) continue diff --git a/desktop/src/com/unciv/app/desktop/UniqueDocsWriter.kt b/desktop/src/com/unciv/app/desktop/UniqueDocsWriter.kt index 6a0cb9b0c7..6c4911c7f3 100644 --- a/desktop/src/com/unciv/app/desktop/UniqueDocsWriter.kt +++ b/desktop/src/com/unciv/app/desktop/UniqueDocsWriter.kt @@ -64,8 +64,7 @@ class UniqueDocsWriter { lines += "## " + targetType.key.name + " uniques" for (uniqueType in targetType.value) { - val deprecationAnnotation = uniqueType.declaringClass.getField(uniqueType.name) - .getAnnotation(Deprecated::class.java) + val deprecationAnnotation = uniqueType.getDeprecationAnnotation() if (deprecationAnnotation != null) { deprecatedUniques += uniqueType continue @@ -83,8 +82,7 @@ class UniqueDocsWriter { lines += "## Deprecated uniques" for (deprecatedUnique in deprecatedUniques) { val deprecationAnnotation = - deprecatedUnique.declaringClass.getField(deprecatedUnique.name) - .getAnnotation(Deprecated::class.java) + deprecatedUnique.getDeprecationAnnotation()!! val deprecationText = "Deprecated ${deprecationAnnotation.message}," + if (deprecationAnnotation.replaceWith.expression != "") " replace with \"${deprecationAnnotation.replaceWith.expression}\"" else ""