Better Validation of Nation colors (#13568)

* Fix json arrays that aren't [r,g,b] causing Ruleset load to throw

* Fix checkNation forgetting to call its super

* Ruleset-Validate array content of Nation colors
This commit is contained in:
SomeTroglodyte 2025-07-03 10:02:24 +02:00 committed by GitHub
parent 212a772190
commit 0846b6d486
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 3 deletions

View File

@ -97,10 +97,12 @@ class Nation : RulesetObject() {
var ignoreHillMovementCost = false
fun setTransients() {
outerColorObject = colorFromRGB(outerColor)
fun safeColorFromRGB(rgb: List<Int>) = if (rgb.size >= 3) colorFromRGB(rgb) else Color.PURPLE
outerColorObject = safeColorFromRGB(outerColor)
innerColorObject = if (innerColor == null) ImageGetter.CHARCOAL
else colorFromRGB(innerColor!!)
else safeColorFromRGB(innerColor!!)
forestsAndJunglesAreRoads = uniqueMap.hasUnique(UniqueType.ForestsAndJunglesAreRoads)
ignoreHillMovementCost = uniqueMap.hasUnique(UniqueType.IgnoreHillMovementCost)

View File

@ -153,6 +153,7 @@ internal class BaseRulesetValidator(
}
override fun checkNation(nation: Nation, lines: RulesetErrorList) {
super.checkNation(nation, lines)
if (nation.preferredVictoryType != Constants.neutralVictoryType && nation.preferredVictoryType !in ruleset.victories)
lines.add("${nation.name}'s preferredVictoryType is ${nation.preferredVictoryType} which does not exist!", sourceObject = nation)
if (nation.cityStateType != null && nation.cityStateType !in ruleset.cityStateTypes)

View File

@ -328,7 +328,21 @@ open class RulesetValidator protected constructor(
lines.add("${nation.name} can settle cities, but has no city names!", sourceObject = nation)
}
checkContrasts(nation.getInnerColor(), nation.getOuterColor(), nation, lines)
fun isColorFaulty(rgb: List<Int>?) = when {
rgb == null -> false
rgb.size != 3 -> true
rgb.any { it !in 0..255 } -> true
else -> false
}
val badInner = isColorFaulty(nation.innerColor)
val badOuter = isColorFaulty(nation.outerColor)
if (badInner)
lines.add("${nation.name}'s innerColor is not an array of three integers in the 0..255 range", sourceObject = nation)
if (badOuter)
lines.add("${nation.name}'s outerColor is not an array of three integers in the 0..255 range", sourceObject = nation)
if (!badInner && !badOuter)
checkContrasts(nation.getInnerColor(), nation.getOuterColor(), nation, lines)
}
protected open fun addPersonalityErrors(lines: RulesetErrorList) {