Работающий racialDescription в ThingDescription

This commit is contained in:
DBotThePony 2023-01-20 23:12:11 +07:00
parent cae74c5e5e
commit 9ae61d6882
Signed by: DBot
GPG Key ID: DCC23B5715498507
3 changed files with 74 additions and 12 deletions

View File

@ -61,6 +61,8 @@ const val PIXELS_IN_STARBOUND_UNITf = 8.0f
// class TileDefLoadingException(message: String, cause: Throwable? = null) : IllegalStateException(message, cause) // class TileDefLoadingException(message: String, cause: Throwable? = null) : IllegalStateException(message, cause)
// class ProjectileDefLoadingException(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 { object Starbound {
private val LOGGER = LogManager.getLogger() private val LOGGER = LogManager.getLogger()
@ -171,7 +173,7 @@ object Starbound {
.registerTypeAdapter(LeveledStatusEffect.ADAPTER) .registerTypeAdapter(LeveledStatusEffect.ADAPTER)
.registerTypeAdapter(MaterialReference.Companion) .registerTypeAdapter(MaterialReference.Companion)
.registerTypeAdapter(ThingDescription.ADAPTER) .registerTypeAdapter(ThingDescription.Companion)
.registerTypeAdapter(ItemPrototype.ADAPTER) .registerTypeAdapter(ItemPrototype.ADAPTER)
.registerTypeAdapter(CurrencyItemPrototype.ADAPTER) .registerTypeAdapter(CurrencyItemPrototype.ADAPTER)

View File

@ -1,6 +1,13 @@
package ru.dbotthepony.kstarbound.defs 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.io.json.FactoryAdapter
import ru.dbotthepony.kstarbound.sbIntern
import ru.dbotthepony.kstarbound.util.NotNullVar
interface IThingWithDescription { interface IThingWithDescription {
/** /**
@ -35,24 +42,77 @@ interface IThingWithDescription {
* Human: A microwave. Gotta get me some jacket potatoes. * Human: A microwave. Gotta get me some jacket potatoes.
* Hylotl: A strange, spinning oven. * Hylotl: A strange, spinning oven.
* Novakid: This lil' rotatin' oven cooks food at speed! * 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<String, String> get() = mapOf() val racialDescription: Map<String, String> get() = mapOf()
/** /**
* Кратное описание штуки для определённых рас * Кратное описание штуки для определённых рас. В оригинальной игре не встречается
*/ */
val racialShortDescription: Map<String, String> get() = mapOf() val racialShortDescription: Map<String, String> get() = mapOf()
} }
data class ThingDescription( data class ThingDescription(
override val shortdescription: String, override val shortdescription: String,
override val description: String override val description: String,
override val racialDescription: Map<String, String>,
override val racialShortDescription: Map<String, String>,
) : IThingWithDescription { ) : IThingWithDescription {
companion object { companion object : TypeAdapter<ThingDescription>() {
val ADAPTER = FactoryAdapter.Builder(ThingDescription::class, override fun write(out: JsonWriter, value: ThingDescription) {
ThingDescription::shortdescription, TODO("Not yet implemented")
ThingDescription::description) }
.logMisses(false)
.build() override fun read(`in`: JsonReader): ThingDescription {
`in`.beginObject()
var shortdescription: String by NotNullVar()
var description: String by NotNullVar()
val racial = ImmutableMap.Builder<String, String>()
val racialShort = ImmutableMap.Builder<String, String>()
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()
)
}
} }
} }

View File

@ -7,7 +7,7 @@ import kotlin.properties.Delegates
/** /**
* Аналог [Delegates.notNull], но со свойством [isInitialized] * Аналог [Delegates.notNull], но со свойством [isInitialized]
*/ */
class NotNullVar<V : Any> : ReadWriteProperty<Any, V> { class NotNullVar<V : Any> : ReadWriteProperty<Any?, V> {
private var value: V? = null private var value: V? = null
/** /**
@ -16,11 +16,11 @@ class NotNullVar<V : Any> : ReadWriteProperty<Any, V> {
val isInitialized: Boolean val isInitialized: Boolean
get() = value != null 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") 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 this.value = value
} }
} }