From e9ee616c1f0981f0a574fede81b7a54f51be75c4 Mon Sep 17 00:00:00 2001 From: yairm210 Date: Wed, 30 Jul 2025 20:41:34 +0300 Subject: [PATCH] chore(purity): Remove more suppressions --- build.gradle.kts | 1 + core/src/com/unciv/logic/civilization/Civilization.kt | 11 +++++++---- .../unciv/logic/civilization/managers/TurnManager.kt | 2 +- core/src/com/unciv/logic/map/mapunit/MapUnit.kt | 2 +- core/src/com/unciv/models/metadata/LocaleCode.kt | 9 ++++++--- core/src/com/unciv/models/ruleset/unit/BaseUnit.kt | 6 +++--- 6 files changed, 19 insertions(+), 12 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 8c193724bb..c3f0c8b844 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -61,6 +61,7 @@ allprojects { "kotlin.collections.reversed" ) wellKnownPureClasses = setOf( + "java.util.Locale", ) } diff --git a/core/src/com/unciv/logic/civilization/Civilization.kt b/core/src/com/unciv/logic/civilization/Civilization.kt index 395ab50f6f..577b0eb4bf 100644 --- a/core/src/com/unciv/logic/civilization/Civilization.kt +++ b/core/src/com/unciv/logic/civilization/Civilization.kt @@ -55,6 +55,7 @@ import com.unciv.models.translations.tr import com.unciv.ui.components.extensions.toPercent import com.unciv.ui.screens.victoryscreen.RankingType import org.jetbrains.annotations.VisibleForTesting +import yairm210.purity.annotations.Cache import yairm210.purity.annotations.LocalState import yairm210.purity.annotations.Readonly import kotlin.math.max @@ -127,9 +128,6 @@ class Civilization : IsPartOfGameInfoSerialization { @Transient val cityStateFunctions = CityStateFunctions(this) - @Transient - var cachedMilitaryMight = -1 - @Transient var passThroughImpassableUnlocked = false // Cached Boolean equal to passableImpassables.isNotEmpty() @@ -714,12 +712,17 @@ class Civilization : IsPartOfGameInfoSerialization { } } - @Readonly @Suppress("purity") // caches + @Transient @Cache + private var cachedMilitaryMight = -1 + + @Readonly private fun getMilitaryMight(): Int { if (cachedMilitaryMight < 0) cachedMilitaryMight = calculateMilitaryMight() return cachedMilitaryMight } + + fun resetMilitaryMightCache() { cachedMilitaryMight = -1 } @Readonly private fun calculateMilitaryMight(): Int { diff --git a/core/src/com/unciv/logic/civilization/managers/TurnManager.kt b/core/src/com/unciv/logic/civilization/managers/TurnManager.kt index 02f2e5425e..52342db7ac 100644 --- a/core/src/com/unciv/logic/civilization/managers/TurnManager.kt +++ b/core/src/com/unciv/logic/civilization/managers/TurnManager.kt @@ -317,7 +317,7 @@ class TurnManager(val civInfo: Civilization) { civInfo.diplomacy.values.toList().forEach { it.nextTurn() } // we copy the diplomacy values so if it changes in-loop we won't crash civInfo.cache.updateHasActiveEnemyMovementPenalty() - civInfo.cachedMilitaryMight = -1 // Reset so we don't use a value from a previous turn + civInfo.resetMilitaryMightCache() updateWinningCiv() // Maybe we did something this turn to win } diff --git a/core/src/com/unciv/logic/map/mapunit/MapUnit.kt b/core/src/com/unciv/logic/map/mapunit/MapUnit.kt index dd9fc347bf..0e37dee96b 100644 --- a/core/src/com/unciv/logic/map/mapunit/MapUnit.kt +++ b/core/src/com/unciv/logic/map/mapunit/MapUnit.kt @@ -670,7 +670,7 @@ class MapUnit : IsPartOfGameInfoSerialization { return null } - @Readonly @Suppress("purity") // Updates escorting state + @Readonly fun isEscorting(): Boolean { if (escorting) { if (getOtherEscortUnit() != null) return true diff --git a/core/src/com/unciv/models/metadata/LocaleCode.kt b/core/src/com/unciv/models/metadata/LocaleCode.kt index 6da3e5388a..662b263c45 100644 --- a/core/src/com/unciv/models/metadata/LocaleCode.kt +++ b/core/src/com/unciv/models/metadata/LocaleCode.kt @@ -1,5 +1,6 @@ package com.unciv.models.metadata +import yairm210.purity.annotations.Cache import yairm210.purity.annotations.Readonly import java.text.NumberFormat import java.util.Locale @@ -67,19 +68,21 @@ enum class LocaleCode(val languageTag: String, private val fastlaneFolder: Strin Zulu("zu-ZA") ; - fun locale(): Locale = Locale.forLanguageTag(languageTag) + @Readonly fun locale(): Locale = Locale.forLanguageTag(languageTag) fun fastlaneFolder(): String = this.fastlaneFolder ?: locale().language companion object { private val bannedCharacters = listOf(' ', '_', '-', '(', ')') // Things not to have in enum names /** Find a LocaleCode for a [language] as stored in GameSettings */ + @Readonly fun find(language: String): LocaleCode? { val languageName = language.filterNot { it in bannedCharacters } return LocaleCode.entries.firstOrNull { it.name == languageName } } /** Get a Java Locale for a [language] as stored in GameSettings */ + @Readonly fun getLocale(language: String): Locale = find(language)?.locale() ?: Locale.getDefault() @@ -88,9 +91,9 @@ enum class LocaleCode(val languageTag: String, private val fastlaneFolder: Strin find(language)?.fastlaneFolder() ?: "en" // NumberFormat cache, key: language, value: NumberFormat - private val languageToNumberFormat = mutableMapOf() + @Cache private val languageToNumberFormat = mutableMapOf() - @Readonly @Suppress("purity") + @Readonly fun getNumberFormatFromLanguage(language: String): NumberFormat = languageToNumberFormat.getOrPut(language) { NumberFormat.getInstance(getLocale(language)) diff --git a/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt b/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt index 57d27c2dbe..15999a2736 100644 --- a/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt +++ b/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt @@ -500,10 +500,10 @@ class BaseUnit : RulesetObject(), INonPerpetualConstruction { .any { it.params[0].toInt() > 0 && it.hasModifier(UniqueType.ConditionalVsCity) } - @Transient - var cachedForceEvaluation: Int = -1 + @Transient @Cache + private var cachedForceEvaluation: Int = -1 - @Readonly @Suppress("purity") // caches + @Readonly fun getForceEvaluation(): Int { if (cachedForceEvaluation < 0) cachedForceEvaluation = evaluateForce()