From 2709f3d005045f67086189e16476b94bafd262b6 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 5 Feb 2023 16:39:03 +0700 Subject: [PATCH] DirectAssetReference --- .../ru/dbotthepony/kstarbound/Starbound.kt | 1 + .../kstarbound/defs/AssetReference.kt | 7 +++- .../kstarbound/defs/DirectAssetReference.kt | 36 +++++++++++++++++++ .../kstarbound/defs/IScriptable.kt | 2 +- .../kstarbound/defs/StatusEffectDefinition.kt | 2 +- .../defs/item/ArmorItemDefinition.kt | 3 +- .../defs/item/ArmorItemPrototype.kt | 3 +- .../defs/player/CompanionsConfig.kt | 3 +- .../defs/player/DeploymentConfig.kt | 3 +- 9 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 src/main/kotlin/ru/dbotthepony/kstarbound/defs/DirectAssetReference.kt diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt index 125b21a3..4195ee29 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt @@ -158,6 +158,7 @@ object Starbound { .registerTypeAdapterFactory(IItemDefinition.InventoryIcon.Factory(pathStack)) .registerTypeAdapter(SpriteReference.Adapter(pathStack)) .registerTypeAdapterFactory(IArmorItemDefinition.ArmorFrames.Factory(pathStack)) + .registerTypeAdapterFactory(DirectAssetReferenceFactory(pathStack)) .registerTypeAdapter(ImageReference.Adapter(pathStack)) .registerTypeAdapterFactory(AssetReferenceFactory(pathStack) { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/AssetReference.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/AssetReference.kt index b697f27e..d384eceb 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/AssetReference.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/AssetReference.kt @@ -61,7 +61,12 @@ class AssetReferenceFactory(val remapper: AssetPathStack, val reader: (String) - val value = remapper(fullPath) { adapter.read(JsonReader(reader).also { it.isLenient = true - }) ?: return AssetReference(path, fullPath, null) + }) + } + + if (value == null) { + missing.add(fullPath) + return AssetReference(path, fullPath, null) } cache[fullPath] = value diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/DirectAssetReference.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/DirectAssetReference.kt new file mode 100644 index 00000000..581d1428 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/DirectAssetReference.kt @@ -0,0 +1,36 @@ +package ru.dbotthepony.kstarbound.defs + +import com.google.gson.Gson +import com.google.gson.TypeAdapter +import com.google.gson.TypeAdapterFactory +import com.google.gson.reflect.TypeToken +import com.google.gson.stream.JsonReader +import com.google.gson.stream.JsonWriter +import ru.dbotthepony.kstarbound.util.AssetPathStack + +class DirectAssetReferenceFactory(val remapper: AssetPathStack) : TypeAdapterFactory { + override fun create(gson: Gson, type: TypeToken): TypeAdapter? { + if (type.rawType == DirectAssetReference::class.java) { + return object : TypeAdapter() { + private val strings = gson.getAdapter(String::class.java) + + override fun write(out: JsonWriter, value: DirectAssetReference?) { + if (value == null) + out.nullValue() + else + out.value(value.fullPath) + } + + override fun read(`in`: JsonReader): DirectAssetReference? { + val path = strings.read(`in`) ?: return null + if (path == "") return null + return DirectAssetReference(path, remapper.remap(path)) + } + } as TypeAdapter + } + + return null + } +} + +data class DirectAssetReference(val path: String, val fullPath: String) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/IScriptable.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/IScriptable.kt index a4ee9fb8..d95efd3c 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/IScriptable.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/IScriptable.kt @@ -4,7 +4,7 @@ interface IScriptable { /** * Lua скрипты для выполнения */ - val scripts: List + val scripts: List /** * Через какое количество тиков вызывать обновления скриптов diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/StatusEffectDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/StatusEffectDefinition.kt index 46059065..de43f6d1 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/StatusEffectDefinition.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/StatusEffectDefinition.kt @@ -12,7 +12,7 @@ data class StatusEffectDefinition( val blockingStat: String? = null, val label: String? = null, val icon: SpriteReference? = null, - override val scripts: ImmutableList = ImmutableList.of(), + override val scripts: ImmutableList = ImmutableList.of(), override val scriptDelta: Int = 1, val animationConfig: AssetReference? = null, ) : IScriptable diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ArmorItemDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ArmorItemDefinition.kt index c37a4767..54a93ae1 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ArmorItemDefinition.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ArmorItemDefinition.kt @@ -1,5 +1,6 @@ package ru.dbotthepony.kstarbound.defs.item +import ru.dbotthepony.kstarbound.defs.DirectAssetReference import ru.dbotthepony.kstarbound.defs.IThingWithDescription import ru.dbotthepony.kstarbound.defs.ThingDescription @@ -15,7 +16,7 @@ data class ArmorItemDefinition( override val eventCategory: String?, override val consumeOnPickup: Boolean, override val pickupQuestTemplates: List, - override val scripts: List, + override val scripts: List, override val scriptDelta: Int, override val tooltipKind: ItemTooltipKind, override val twoHanded: Boolean, diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ArmorItemPrototype.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ArmorItemPrototype.kt index 65d7a896..f8545a45 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ArmorItemPrototype.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ArmorItemPrototype.kt @@ -2,6 +2,7 @@ package ru.dbotthepony.kstarbound.defs.item import com.google.common.collect.ImmutableList import ru.dbotthepony.kstarbound.Starbound +import ru.dbotthepony.kstarbound.defs.DirectAssetReference import ru.dbotthepony.kstarbound.defs.util.enrollMap import ru.dbotthepony.kstarbound.io.json.builder.BuilderAdapter import ru.dbotthepony.kstarbound.io.json.builder.JsonBuilder @@ -19,7 +20,7 @@ open class ArmorItemPrototype : ItemPrototype(), IArmorItemDefinition { override var level: Double = 1.0 override var leveledStatusEffects: ImmutableList = ImmutableList.of() - override var scripts: ImmutableList = ImmutableList.of() + override var scripts: ImmutableList = ImmutableList.of() override var scriptDelta: Int = 1 @JsonIgnoreProperty diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/CompanionsConfig.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/CompanionsConfig.kt index 9aac24f0..a1b75cba 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/CompanionsConfig.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/CompanionsConfig.kt @@ -2,13 +2,14 @@ package ru.dbotthepony.kstarbound.defs.player import com.google.common.collect.ImmutableList import com.google.common.collect.ImmutableMap +import ru.dbotthepony.kstarbound.defs.DirectAssetReference import ru.dbotthepony.kstarbound.defs.IScriptable import ru.dbotthepony.kstarbound.util.SBPattern import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory @JsonFactory data class CompanionsConfig( - override val scripts: ImmutableList, + override val scripts: ImmutableList, override val scriptDelta: Int, val activePodLimit: Int, diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/DeploymentConfig.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/DeploymentConfig.kt index 8909cd96..166e088c 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/DeploymentConfig.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/DeploymentConfig.kt @@ -2,6 +2,7 @@ package ru.dbotthepony.kstarbound.defs.player import com.google.common.collect.ImmutableList import com.google.common.collect.ImmutableMap +import ru.dbotthepony.kstarbound.defs.DirectAssetReference import ru.dbotthepony.kstarbound.defs.IScriptable import ru.dbotthepony.kstarbound.defs.RegistryReference import ru.dbotthepony.kstarbound.defs.item.IItemDefinition @@ -9,7 +10,7 @@ import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory @JsonFactory data class DeploymentConfig( - override val scripts: ImmutableList, + override val scripts: ImmutableList, override val scriptDelta: Int, val starterMechSet: ImmutableMap>,