diff options
author | Linnea Gräf <nea@nea.moe> | 2024-11-01 23:00:53 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-11-01 23:00:53 +0100 |
commit | 8b410fbdf2cffb3ceaa51bbea150f5165848bc37 (patch) | |
tree | e4b4ec4ba24a7edbefa45b7deb2503b74214ee02 /src/main/kotlin/features/texturepack/TintOverrides.kt | |
parent | 653454e290a1bb6142e2bb40947239c2e450820b (diff) | |
download | Firmament-8b410fbdf2cffb3ceaa51bbea150f5165848bc37.tar.gz Firmament-8b410fbdf2cffb3ceaa51bbea150f5165848bc37.tar.bz2 Firmament-8b410fbdf2cffb3ceaa51bbea150f5165848bc37.zip |
Add tint override to texture packs
Diffstat (limited to 'src/main/kotlin/features/texturepack/TintOverrides.kt')
-rw-r--r-- | src/main/kotlin/features/texturepack/TintOverrides.kt | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/main/kotlin/features/texturepack/TintOverrides.kt b/src/main/kotlin/features/texturepack/TintOverrides.kt new file mode 100644 index 0000000..8006db8 --- /dev/null +++ b/src/main/kotlin/features/texturepack/TintOverrides.kt @@ -0,0 +1,75 @@ +package moe.nea.firmament.features.texturepack + +import com.google.gson.JsonObject +import com.google.gson.JsonPrimitive +import moe.nea.firmament.util.ErrorUtil +import moe.nea.firmament.util.assertNotNullOr + +data class TintOverrides( + val layerMap: Map<Int, TintOverride> = mapOf() +) { + val hasOverrides by lazy { layerMap.values.any { it !is Reset } } + + companion object { + val EMPTY = TintOverrides() + private val threadLocal = object : ThreadLocal<TintOverrides>() {} + fun enter(overrides: TintOverrides?) { + ErrorUtil.softCheck("Double entered tintOverrides") { + threadLocal.get() == null + } + threadLocal.set(overrides ?: EMPTY) + } + + fun exit(overrides: TintOverrides?) { + ErrorUtil.softCheck("Exited with non matching enter tintOverrides") { + threadLocal.get() == (overrides ?: EMPTY) + } + threadLocal.remove() + } + + fun getCurrentOverrides() = + assertNotNullOr(threadLocal.get(), "Got current tintOverrides without entering") { EMPTY } + + fun parse(jsonObject: JsonObject): TintOverrides { + val map = mutableMapOf<Int, TintOverride>() + for ((key, value) in jsonObject.entrySet()) { + val layerIndex = + ErrorUtil.notNullOr(key.toIntOrNull(), + "Unknown layer index $value. Should be integer") { continue } + if (value.isJsonNull) { + map[layerIndex] = Reset + continue + } + val override = (value as? JsonPrimitive) + ?.takeIf(JsonPrimitive::isNumber) + ?.asInt + ?.let(::Fixed) + if (override == null) { + ErrorUtil.softError("Invalid tint override for a layer: $value") + continue + } + map[layerIndex] = override + } + return TintOverrides(map) + } + } + + fun mergeWithParent(parent: TintOverrides): TintOverrides { + val mergedMap = parent.layerMap.toMutableMap() + mergedMap.putAll(this.layerMap) + return TintOverrides(mergedMap) + } + + fun hasOverrides(): Boolean = hasOverrides + fun getOverride(tintIndex: Int): Int? { + return when (val tint = layerMap[tintIndex]) { + is Reset -> null + is Fixed -> tint.color + null -> null + } + } + + sealed interface TintOverride + data object Reset : TintOverride + data class Fixed(val color: Int) : TintOverride +} |