From 9ae61d68829ad4243d3839f5fb93a0659d1c072b Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 20 Jan 2023 23:12:11 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D1=8E?= =?UTF-8?q?=D1=89=D0=B8=D0=B9=20racialDescription=20=D0=B2=20ThingDescript?= =?UTF-8?q?ion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/dbotthepony/kstarbound/Starbound.kt | 4 +- .../kstarbound/defs/IThingWithDescription.kt | 76 +++++++++++++++++-- .../dbotthepony/kstarbound/util/NotNullVar.kt | 6 +- 3 files changed, 74 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt index 901ee82d..6ae37dcb 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt @@ -61,6 +61,8 @@ const val PIXELS_IN_STARBOUND_UNITf = 8.0f // class TileDefLoadingException(message: String, cause: Throwable? = null) : IllegalStateException(message, cause) // class ProjectileDefLoadingException(message: String, cause: Throwable? = null) : IllegalStateException(message, cause) +fun String.sbIntern(): String = Starbound.STRING_INTERNER.intern(this) + object Starbound { private val LOGGER = LogManager.getLogger() @@ -171,7 +173,7 @@ object Starbound { .registerTypeAdapter(LeveledStatusEffect.ADAPTER) .registerTypeAdapter(MaterialReference.Companion) - .registerTypeAdapter(ThingDescription.ADAPTER) + .registerTypeAdapter(ThingDescription.Companion) .registerTypeAdapter(ItemPrototype.ADAPTER) .registerTypeAdapter(CurrencyItemPrototype.ADAPTER) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/IThingWithDescription.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/IThingWithDescription.kt index 14f3d6ab..e0a41c07 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/IThingWithDescription.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/IThingWithDescription.kt @@ -1,6 +1,13 @@ package ru.dbotthepony.kstarbound.defs +import com.google.common.collect.ImmutableMap +import com.google.gson.TypeAdapter +import com.google.gson.stream.JsonReader +import com.google.gson.stream.JsonToken +import com.google.gson.stream.JsonWriter import ru.dbotthepony.kstarbound.io.json.FactoryAdapter +import ru.dbotthepony.kstarbound.sbIntern +import ru.dbotthepony.kstarbound.util.NotNullVar interface IThingWithDescription { /** @@ -35,24 +42,77 @@ interface IThingWithDescription { * Human: A microwave. Gotta get me some jacket potatoes. * Hylotl: A strange, spinning oven. * Novakid: This lil' rotatin' oven cooks food at speed! + * + * Как это выглядит в JSON структуре: + * ```json + * { + * ... + * + * "apexDescription" : "A type of oven.", + * "avianDescription" : "A bizarre cooking device.", + * "floranDescription" : "Floran likess raw meat, sssometimes cooked meat is good too.", + * "glitchDescription" : "Irked. It is encrusted with spattered food, who left it in this state?", + * "humanDescription" : "A microwave. Gotta get me some jacket potatoes.", + * "hylotlDescription" : "A strange, spinning oven.", + * "novakidDescription" : "This lil' rotatin' oven cooks food at speed!", + * + * ... + * } + * ``` + * + * Для получения описания для определённой расы необходимо знать ID расы, как показано выше (`apex`, `avian`, ...) */ val racialDescription: Map get() = mapOf() /** - * Кратное описание штуки для определённых рас + * Кратное описание штуки для определённых рас. В оригинальной игре не встречается */ val racialShortDescription: Map get() = mapOf() } data class ThingDescription( override val shortdescription: String, - override val description: String + override val description: String, + override val racialDescription: Map, + override val racialShortDescription: Map, ) : IThingWithDescription { - companion object { - val ADAPTER = FactoryAdapter.Builder(ThingDescription::class, - ThingDescription::shortdescription, - ThingDescription::description) - .logMisses(false) - .build() + companion object : TypeAdapter() { + override fun write(out: JsonWriter, value: ThingDescription) { + TODO("Not yet implemented") + } + + override fun read(`in`: JsonReader): ThingDescription { + `in`.beginObject() + + var shortdescription: String by NotNullVar() + var description: String by NotNullVar() + val racial = ImmutableMap.Builder() + val racialShort = ImmutableMap.Builder() + + while (`in`.peek() !== JsonToken.END_OBJECT) { + when (val name = `in`.nextName()) { + "shortdescription" -> shortdescription = `in`.nextString() + "description" -> description = `in`.nextString() + else -> { + if (name.endsWith("shortdescription") || name.endsWith("shortDescription") || name.endsWith("Shortdescription") || name.endsWith("ShortDescription")) { + racialShort.put(name.substring(0, name.length - "shortdescription".length).sbIntern(), `in`.nextString()) + } else if (name.endsWith("description") || name.endsWith("Description")) { + racial.put(name.substring(0, name.length - "description".length).sbIntern(), `in`.nextString()) + } else { + `in`.skipValue() + } + } + } + } + + `in`.endObject() + + return ThingDescription( + shortdescription = shortdescription, + description = description, + racialDescription = racial.build(), + racialShortDescription = racialShort.build() + ) + } } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/util/NotNullVar.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/util/NotNullVar.kt index 4fa18d42..c64c6c36 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/util/NotNullVar.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/util/NotNullVar.kt @@ -7,7 +7,7 @@ import kotlin.properties.Delegates /** * Аналог [Delegates.notNull], но со свойством [isInitialized] */ -class NotNullVar : ReadWriteProperty { +class NotNullVar : ReadWriteProperty { private var value: V? = null /** @@ -16,11 +16,11 @@ class NotNullVar : ReadWriteProperty { val isInitialized: Boolean get() = value != null - override fun getValue(thisRef: Any, property: KProperty<*>): V { + override fun getValue(thisRef: Any?, property: KProperty<*>): V { return value ?: throw IllegalStateException("Property ${property.name} was not initialized") } - override fun setValue(thisRef: Any, property: KProperty<*>, value: V) { + override fun setValue(thisRef: Any?, property: KProperty<*>, value: V) { this.value = value } }