Больше переноса на TypeAdapterFactory

This commit is contained in:
DBotThePony 2023-01-22 23:11:15 +07:00
parent a4f4320b04
commit c80372e0fa
Signed by: DBot
GPG Key ID: DCC23B5715498507
22 changed files with 247 additions and 168 deletions

View File

@ -41,7 +41,6 @@ import ru.dbotthepony.kstarbound.defs.world.dungeon.DungeonWorldDef
import ru.dbotthepony.kstarbound.io.*
import ru.dbotthepony.kstarbound.io.json.AABBTypeAdapter
import ru.dbotthepony.kstarbound.io.json.AABBiTypeAdapter
import ru.dbotthepony.kstarbound.io.json.MappedTypeFactories
import ru.dbotthepony.kstarbound.io.json.builder.EnumAdapter
import ru.dbotthepony.kstarbound.io.json.Vector2dTypeAdapter
import ru.dbotthepony.kstarbound.io.json.Vector2fTypeAdapter
@ -49,7 +48,6 @@ import ru.dbotthepony.kstarbound.io.json.Vector2iTypeAdapter
import ru.dbotthepony.kstarbound.io.json.Vector4iTypeAdapter
import ru.dbotthepony.kstarbound.io.json.factory.ImmutableCollectionAdapterFactory
import ru.dbotthepony.kstarbound.math.*
import ru.dbotthepony.kvector.vector.Color
import java.io.*
import java.text.DateFormat
import java.util.*
@ -147,52 +145,11 @@ object Starbound {
.setDateFormat(DateFormat.LONG)
.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY)
.setPrettyPrinting()
.registerTypeAdapter(ColorTypeAdapter.nullSafe())
.registerTypeAdapterFactory(ImmutableCollectionAdapterFactory)
// чтоб строки всегда intern'ились
.registerTypeAdapter(NULLABLE_STRING_ADAPTER)
// math
.registerTypeAdapter(AABBTypeAdapter)
.registerTypeAdapter(AABBiTypeAdapter)
.registerTypeAdapter(Vector2dTypeAdapter)
.registerTypeAdapter(Vector2fTypeAdapter)
.registerTypeAdapter(Vector2iTypeAdapter)
.registerTypeAdapter(Vector4iTypeAdapter)
.registerTypeAdapter(PolyTypeAdapter)
.also(ConfigurableProjectile::registerGson)
.also(SkyParameters::registerGson)
.also(DungeonWorldDef::registerGson)
.also(ParallaxPrototype::registerGson)
.also(JsonFunction::registerGson)
.also(MaterialModifier::registerGson)
.also(RenderParameters::registerGson)
.also(RenderTemplate::registerGson)
.also(TileDefinition::registerGson)
.also(LiquidDefinition::registerGson)
.also(SpriteReference::registerGson)
.also(AtlasConfiguration::registerGson)
.registerTypeAdapterFactory(LeveledStatusEffect.ADAPTER)
.registerTypeAdapter(MaterialReference.Companion)
.registerTypeAdapter(ThingDescription.Companion)
.registerTypeAdapterFactory(ItemPrototype.ADAPTER)
.registerTypeAdapterFactory(CurrencyItemPrototype.ADAPTER)
.registerTypeAdapterFactory(ArmorItemPrototype.ADAPTER)
.registerTypeAdapterFactory(MaterialItemPrototype.ADAPTER)
.registerTypeAdapterFactory(LiquidItemPrototype.ADAPTER)
.registerTypeAdapter(IItemDefinition.InventoryIcon.ADAPTER)
.registerTypeAdapter(IFossilItemDefinition.FossilSetDescription.ADAPTER)
.registerTypeAdapter(IArmorItemDefinition.ArmorFrames.ADAPTER)
.registerTypeAdapter(EnumAdapter(DamageType::class, default = DamageType.NORMAL).neverNull())
.registerTypeAdapter(EnumAdapter(ItemRarity::class).neverNull())
.registerTypeAdapter(EnumAdapter(ItemTooltipKind::class).neverNull())
.also(::addStarboundJsonAdapters)
.create()

View File

@ -0,0 +1,144 @@
package ru.dbotthepony.kstarbound
import com.google.gson.GsonBuilder
import ru.dbotthepony.kstarbound.defs.DamageType
import ru.dbotthepony.kstarbound.defs.JsonFunction
import ru.dbotthepony.kstarbound.defs.MaterialReference
import ru.dbotthepony.kstarbound.defs.ThingDescription
import ru.dbotthepony.kstarbound.defs.image.AtlasConfiguration
import ru.dbotthepony.kstarbound.defs.image.SpriteReference
import ru.dbotthepony.kstarbound.defs.item.ArmorItemPrototype
import ru.dbotthepony.kstarbound.defs.item.CurrencyItemPrototype
import ru.dbotthepony.kstarbound.defs.item.IArmorItemDefinition
import ru.dbotthepony.kstarbound.defs.item.IFossilItemDefinition
import ru.dbotthepony.kstarbound.defs.item.IItemDefinition
import ru.dbotthepony.kstarbound.defs.item.ItemPrototype
import ru.dbotthepony.kstarbound.defs.item.ItemRarity
import ru.dbotthepony.kstarbound.defs.item.ItemTooltipKind
import ru.dbotthepony.kstarbound.defs.item.LeveledStatusEffect
import ru.dbotthepony.kstarbound.defs.item.LiquidItemPrototype
import ru.dbotthepony.kstarbound.defs.item.MaterialItemPrototype
import ru.dbotthepony.kstarbound.defs.liquid.LiquidDefinition
import ru.dbotthepony.kstarbound.defs.parallax.ParallaxPrototype
import ru.dbotthepony.kstarbound.defs.parallax.ParallaxPrototypeLayer
import ru.dbotthepony.kstarbound.defs.projectile.ActionActions
import ru.dbotthepony.kstarbound.defs.projectile.ActionConfig
import ru.dbotthepony.kstarbound.defs.projectile.ActionLoop
import ru.dbotthepony.kstarbound.defs.projectile.ActionProjectile
import ru.dbotthepony.kstarbound.defs.projectile.ActionSound
import ru.dbotthepony.kstarbound.defs.projectile.ConfigurableProjectile
import ru.dbotthepony.kstarbound.defs.projectile.ProjectilePhysics
import ru.dbotthepony.kstarbound.defs.tile.MaterialModifier
import ru.dbotthepony.kstarbound.defs.tile.RenderMatch
import ru.dbotthepony.kstarbound.defs.tile.RenderMatchList
import ru.dbotthepony.kstarbound.defs.tile.RenderParameters
import ru.dbotthepony.kstarbound.defs.tile.RenderPiece
import ru.dbotthepony.kstarbound.defs.tile.RenderRuleList
import ru.dbotthepony.kstarbound.defs.tile.RenderTemplate
import ru.dbotthepony.kstarbound.defs.tile.TileDefinition
import ru.dbotthepony.kstarbound.defs.world.SkyColoring
import ru.dbotthepony.kstarbound.defs.world.SkyColoringManifold
import ru.dbotthepony.kstarbound.defs.world.SkyParameters
import ru.dbotthepony.kstarbound.defs.world.SkySatellite
import ru.dbotthepony.kstarbound.defs.world.SkyType
import ru.dbotthepony.kstarbound.defs.world.dungeon.BeamUpRule
import ru.dbotthepony.kstarbound.defs.world.dungeon.DungeonType
import ru.dbotthepony.kstarbound.defs.world.dungeon.DungeonWorldDef
import ru.dbotthepony.kstarbound.io.ColorTypeAdapter
import ru.dbotthepony.kstarbound.io.json.AABBTypeAdapter
import ru.dbotthepony.kstarbound.io.json.AABBiTypeAdapter
import ru.dbotthepony.kstarbound.io.json.Vector2dTypeAdapter
import ru.dbotthepony.kstarbound.io.json.Vector2fTypeAdapter
import ru.dbotthepony.kstarbound.io.json.Vector2iTypeAdapter
import ru.dbotthepony.kstarbound.io.json.Vector4iTypeAdapter
import ru.dbotthepony.kstarbound.io.json.builder.EnumAdapter
import ru.dbotthepony.kstarbound.io.json.factory.ImmutableCollectionAdapterFactory
import ru.dbotthepony.kstarbound.math.PolyTypeAdapter
fun addStarboundJsonAdapters(builder: GsonBuilder) {
with(builder) {
// ImmutableList, и прочее
registerTypeAdapterFactory(ImmutableCollectionAdapterFactory)
registerTypeAdapter(ColorTypeAdapter.nullSafe())
// математические классы
registerTypeAdapter(AABBTypeAdapter)
registerTypeAdapter(AABBiTypeAdapter)
registerTypeAdapter(Vector2dTypeAdapter)
registerTypeAdapter(Vector2fTypeAdapter)
registerTypeAdapter(Vector2iTypeAdapter)
registerTypeAdapter(Vector4iTypeAdapter)
registerTypeAdapter(PolyTypeAdapter)
// Снаряды
registerTypeAdapterFactory(ConfigurableProjectile.ADAPTER)
registerTypeAdapter(EnumAdapter(ProjectilePhysics::class).neverNull())
registerTypeAdapterFactory(ActionConfig.ADAPTER)
registerTypeAdapterFactory(ActionProjectile.ADAPTER)
registerTypeAdapterFactory(ActionSound.ADAPTER)
registerTypeAdapterFactory(ActionLoop.ADAPTER)
registerTypeAdapterFactory(ActionActions.ADAPTER)
// Параметры неба
registerTypeAdapterFactory(SkyParameters.ADAPTER)
registerTypeAdapter(SkyColoringManifold.ADAPTER)
registerTypeAdapterFactory(SkyColoring.ADAPTER)
registerTypeAdapter(EnumAdapter(SkyType::class))
registerTypeAdapterFactory(SkySatellite.ADAPTER)
registerTypeAdapterFactory(SkySatellite.LAYER_ADAPTER)
// Данные о данжах
registerTypeAdapterFactory(DungeonWorldDef.ADAPTER)
registerTypeAdapter(EnumAdapter(BeamUpRule::class))
registerTypeAdapter(EnumAdapter(DungeonType::class))
// Параллакс
registerTypeAdapterFactory(ParallaxPrototype.ADAPTER)
registerTypeAdapterFactory(ParallaxPrototypeLayer.ADAPTER)
registerTypeAdapter(ParallaxPrototypeLayer.LAYER_PARALLAX_ADAPTER)
// Предметы
registerTypeAdapterFactory(ItemPrototype.ADAPTER)
registerTypeAdapterFactory(CurrencyItemPrototype.ADAPTER)
registerTypeAdapterFactory(ArmorItemPrototype.ADAPTER)
registerTypeAdapterFactory(MaterialItemPrototype.ADAPTER)
registerTypeAdapterFactory(LiquidItemPrototype.ADAPTER)
registerTypeAdapterFactory(IItemDefinition.InventoryIcon.ADAPTER)
registerTypeAdapterFactory(IFossilItemDefinition.FossilSetDescription.ADAPTER)
registerTypeAdapterFactory(IArmorItemDefinition.ArmorFrames.ADAPTER)
// Функции
registerTypeAdapter(JsonFunction.CONSTRAINT_ADAPTER)
registerTypeAdapter(JsonFunction.INTERPOLATION_ADAPTER)
registerTypeAdapter(JsonFunction.Companion)
// Тайлы и жидкости
registerTypeAdapterFactory(MaterialModifier.ADAPTER)
registerTypeAdapterFactory(RenderParameters.ADAPTER)
registerTypeAdapterFactory(RenderPiece.ADAPTER)
registerTypeAdapterFactory(RenderRuleList.ADAPTER)
registerTypeAdapterFactory(RenderMatch.ADAPTER)
registerTypeAdapterFactory(RenderMatch.PIECE_ADAPTER)
registerTypeAdapterFactory(RenderMatch.MATCHER_ADAPTER)
registerTypeAdapterFactory(RenderMatchList.ADAPTER)
registerTypeAdapterFactory(RenderRuleList.Entry.ADAPTER)
registerTypeAdapterFactory(RenderTemplate.ADAPTER)
registerTypeAdapter(EnumAdapter(RenderRuleList.Combination::class))
registerTypeAdapterFactory(TileDefinition.ADAPTER)
registerTypeAdapterFactory(LiquidDefinition.ADAPTER)
registerTypeAdapterFactory(LiquidDefinition.INTERACTION_ADAPTER)
// Общее
registerTypeAdapter(SpriteReference.Companion)
registerTypeAdapter(AtlasConfiguration.ADAPTER)
registerTypeAdapterFactory(LeveledStatusEffect.ADAPTER)
registerTypeAdapter(MaterialReference.Companion)
registerTypeAdapter(ThingDescription.Companion)
registerTypeAdapter(EnumAdapter(DamageType::class, default = DamageType.NORMAL))
registerTypeAdapter(EnumAdapter(ItemRarity::class))
registerTypeAdapter(EnumAdapter(ItemTooltipKind::class))
}
}

View File

@ -190,6 +190,7 @@ class TileRenderer(val state: GLStateTracker, val def: IRenderableTile) {
val equalityTester: EqualityRuleTester = when (def) {
is TileDefinition -> TileEqualityTester(def)
is MaterialModifier -> ModifierEqualityTester(def)
else -> throw IllegalStateException()
}
val bakedProgramState = state.tileRenderers.foreground(texture)

View File

@ -1,7 +1,7 @@
package ru.dbotthepony.kstarbound.defs
import com.google.common.collect.ImmutableList
import com.google.gson.GsonBuilder
import com.google.gson.JsonSyntaxException
import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
@ -27,10 +27,6 @@ enum class JsonFunctionInterpolation(vararg aliases: String) : IStringSerializab
override fun write(out: JsonWriter) {
out.value(this.name)
}
companion object {
val ADAPTER: TypeAdapter<JsonFunctionInterpolation> = EnumAdapter(JsonFunctionInterpolation::class).neverNull()
}
}
enum class JsonFunctionConstraint(vararg aliases: String) : IStringSerializable {
@ -49,10 +45,6 @@ enum class JsonFunctionConstraint(vararg aliases: String) : IStringSerializable
override fun write(out: JsonWriter) {
out.value(this.name)
}
companion object {
val ADAPTER: TypeAdapter<JsonFunctionConstraint> = EnumAdapter(JsonFunctionConstraint::class).neverNull()
}
}
class JsonFunction(
@ -61,6 +53,9 @@ class JsonFunction(
val ranges: List<Vector2d>
) {
companion object : TypeAdapter<JsonFunction>() {
val CONSTRAINT_ADAPTER = EnumAdapter(JsonFunctionConstraint::class)
val INTERPOLATION_ADAPTER = EnumAdapter(JsonFunctionInterpolation::class)
override fun write(out: JsonWriter, value: JsonFunction) {
TODO("Not yet implemented")
}
@ -68,8 +63,8 @@ class JsonFunction(
override fun read(reader: JsonReader): JsonFunction {
reader.beginArray()
val interpolation = JsonFunctionInterpolation.ADAPTER.read(reader)
val constraint = JsonFunctionConstraint.ADAPTER.read(reader)
val interpolation = INTERPOLATION_ADAPTER.read(reader) ?: throw JsonSyntaxException("Missing interpolation")
val constraint = CONSTRAINT_ADAPTER.read(reader) ?: throw JsonSyntaxException("Missing constraint")
val ranges = ArrayList<Vector2d>()
@ -81,11 +76,5 @@ class JsonFunction(
return JsonFunction(interpolation, constraint, ImmutableList.copyOf(ranges))
}
fun registerGson(gsonBuilder: GsonBuilder) {
gsonBuilder.registerTypeAdapter(JsonFunctionConstraint::class.java, JsonFunctionConstraint.ADAPTER)
gsonBuilder.registerTypeAdapter(JsonFunctionInterpolation::class.java, JsonFunctionInterpolation.ADAPTER)
gsonBuilder.registerTypeAdapter(JsonFunction::class.java, this)
}
}
}

View File

@ -305,9 +305,5 @@ class AtlasConfiguration private constructor(
}
val ADAPTER: TypeAdapter<AtlasConfiguration?> = Starbound.NULLABLE_STRING_ADAPTER.transform(read = read@{ get(it ?: return@read it as AtlasConfiguration?) }, write = write@{ it?.name })
fun registerGson(gsonBuilder: GsonBuilder) {
gsonBuilder.registerTypeAdapter(ADAPTER)
}
}
}

View File

@ -32,9 +32,5 @@ data class SpriteReference(
override fun read(`in`: JsonReader): SpriteReference {
return parse(Starbound.assetFolder(`in`.nextString()))
}
fun registerGson(gsonBuilder: GsonBuilder) {
gsonBuilder.registerTypeAdapter(this)
}
}
}

View File

@ -42,9 +42,7 @@ interface IArmorItemDefinition : ILeveledItemDefinition, IScriptableItemDefiniti
ArmorFrames::body,
ArmorFrames::backSleeve,
ArmorFrames::frontSleeve,
)
.build()
.ifString { ArmorFrames(Starbound.assetFolder(it), null, null) }
).ifString { ArmorFrames(Starbound.assetFolder(it), null, null) }
}
}
}

View File

@ -64,7 +64,7 @@ interface IFossilItemDefinition : IItemDefinition {
FossilSetDescription::class,
FossilSetDescription::price,
FossilSetDescription::shortdescription,
FossilSetDescription::description).build()
FossilSetDescription::description)
}
}
}

View File

@ -44,7 +44,7 @@ interface IItemDefinition : IThingWithDescription {
override val image: SpriteReference
) : IInventoryIcon {
companion object {
val ADAPTER = FactoryAdapter.Builder(InventoryIcon::class, InventoryIcon::image).build().ifString { InventoryIcon(SpriteReference.parse(Starbound.assetFolder(it))) }
val ADAPTER = FactoryAdapter.Builder(InventoryIcon::class, InventoryIcon::image).ifString { InventoryIcon(SpriteReference.parse(Starbound.assetFolder(it))) }
}
}

View File

@ -45,8 +45,7 @@ data class LiquidDefinition(
.auto(Interaction::materialResult)
fun registerGson(gsonBuilder: GsonBuilder) {
gsonBuilder.registerTypeAdapterFactory(ADAPTER)
gsonBuilder.registerTypeAdapterFactory(INTERACTION_ADAPTER)
}
}
}

View File

@ -14,14 +14,5 @@ class ParallaxPrototype {
ParallaxPrototype::verticalOrigin,
ParallaxPrototype::layers,
)
fun registerGson(gsonBuilder: GsonBuilder) {
gsonBuilder.registerTypeAdapter(ParallaxPrototype::class.java, ADAPTER)
gsonBuilder.registerTypeAdapter(ParallaxPrototypeLayer::class.java, ParallaxPrototypeLayer.ADAPTER)
gsonBuilder.registerTypeAdapter(
ParallaxPrototypeLayer.Parallax::class.java,
ParallaxPrototypeLayer.LAYER_PARALLAX_ADAPTER
)
}
}
}

View File

@ -120,16 +120,6 @@ class ConfigurableProjectile : RawPrototype<ConfigurableProjectile, ConfiguredPr
ConfigurableProjectile::speed,
ConfigurableProjectile::power,
)
fun registerGson(gson: GsonBuilder) {
gson.registerTypeAdapterFactory(ADAPTER)
gson.registerTypeAdapter(EnumAdapter(ProjectilePhysics::class).neverNull())
gson.registerTypeAdapter(ActionConfig.ADAPTER)
gson.registerTypeAdapter(ActionProjectile.ADAPTER)
gson.registerTypeAdapter(ActionSound.ADAPTER)
gson.registerTypeAdapter(ActionLoop.ADAPTER)
gson.registerTypeAdapter(ActionActions.ADAPTER)
}
}
}
@ -185,7 +175,7 @@ class ActionConfig : IConfigurableAction {
}
companion object {
val ADAPTER = BuilderAdapter.Builder(::ActionConfig, ActionConfig::file).ignoreKey("action").build()
val ADAPTER = BuilderAdapter.Builder(::ActionConfig, ActionConfig::file).ignoreKey("action")
private val cache = ConcurrentHashMap<String, CActionConfig>()
}
@ -204,11 +194,7 @@ class ActionProjectile : IConfigurableAction {
}
companion object {
val ADAPTER = BuilderAdapter.Builder(::ActionProjectile,
ActionProjectile::type,
ActionProjectile::angle,
ActionProjectile::inheritDamageFactor,
).ignoreKey("action").build()
val ADAPTER = BuilderAdapter.Builder(::ActionProjectile, ActionProjectile::type, ActionProjectile::angle, ActionProjectile::inheritDamageFactor).ignoreKey("action")
}
}
@ -223,9 +209,7 @@ class ActionSound : IConfigurableAction {
}
companion object {
val ADAPTER = BuilderAdapter.Builder(::ActionSound,
ActionSound::options,
).ignoreKey("action").build()
val ADAPTER = BuilderAdapter.Builder(::ActionSound, ActionSound::options).ignoreKey("action")
}
}
@ -241,10 +225,7 @@ class ActionLoop : IConfigurableAction {
}
companion object {
val ADAPTER = BuilderAdapter.Builder(::ActionLoop,
ActionLoop::count,
ActionLoop::body,
).ignoreKey("action").build()
val ADAPTER = BuilderAdapter.Builder(::ActionLoop, ActionLoop::count, ActionLoop::body).ignoreKey("action")
}
}
@ -259,8 +240,6 @@ class ActionActions : IConfigurableAction {
}
companion object {
val ADAPTER = BuilderAdapter.Builder(::ActionActions,
ActionActions::list,
).ignoreKey("action").build()
val ADAPTER = BuilderAdapter.Builder(::ActionActions, ActionActions::list).ignoreKey("action")
}
}

View File

@ -36,9 +36,5 @@ data class MaterialModifier(
.auto(MaterialModifier::miningParticle)
.auto(MaterialModifier::renderTemplate)
.auto(MaterialModifier::renderParameters)
fun registerGson(gsonBuilder: GsonBuilder) {
gsonBuilder.registerTypeAdapterFactory(ADAPTER)
}
}
}

View File

@ -34,9 +34,5 @@ data class RenderParameters(
.auto(RenderParameters::occludesBelow)
.auto(RenderParameters::lightTransparent)
.auto(RenderParameters::zLevel)
fun registerGson(gsonBuilder: GsonBuilder) {
gsonBuilder.registerTypeAdapterFactory(ADAPTER)
}
}
}

View File

@ -86,7 +86,6 @@ data class RenderRuleList(
.auto(Entry::type)
.auto(Entry::matchHue)
.auto(Entry::inverse)
.build()
}
}
@ -118,7 +117,6 @@ data class RenderRuleList(
val ADAPTER = FactoryAdapter.Builder(RenderRuleList::class)
.auto(RenderRuleList::entries)
.auto(RenderRuleList::join)
.build()
}
}
@ -261,20 +259,5 @@ data class RenderTemplate(
.auto(RenderTemplate::matches)
.auto(RenderTemplate::rules)
.asReference()
fun registerGson(gsonBuilder: GsonBuilder) {
gsonBuilder.registerTypeAdapterFactory(RenderPiece.ADAPTER)
gsonBuilder.registerTypeAdapter(RenderRuleList.ADAPTER)
gsonBuilder.registerTypeAdapterFactory(RenderMatch.ADAPTER)
gsonBuilder.registerTypeAdapterFactory(RenderMatch.PIECE_ADAPTER)
gsonBuilder.registerTypeAdapterFactory(RenderMatch.MATCHER_ADAPTER)
gsonBuilder.registerTypeAdapterFactory(RenderMatchList.ADAPTER)
gsonBuilder.registerTypeAdapter(RenderRuleList.Entry.ADAPTER)
gsonBuilder.registerTypeAdapterFactory(ADAPTER)
gsonBuilder.registerTypeAdapter(EnumAdapter(RenderRuleList.Combination::class.java))
}
}
}

View File

@ -38,9 +38,5 @@ data class TileDefinition(
.auto(TileDefinition::category)
.auto(TileDefinition::renderTemplate)
.auto(TileDefinition::renderParameters)
fun registerGson(gsonBuilder: GsonBuilder) {
gsonBuilder.registerTypeAdapterFactory(ADAPTER)
}
}
}

View File

@ -8,6 +8,7 @@ import org.apache.logging.log4j.LogManager
import ru.dbotthepony.kstarbound.io.ColorTypeAdapter
import ru.dbotthepony.kstarbound.io.json.builder.BuilderAdapter
import ru.dbotthepony.kstarbound.io.json.builder.EnumAdapter
import ru.dbotthepony.kstarbound.registerTypeAdapter
import ru.dbotthepony.kvector.vector.Color
import ru.dbotthepony.kvector.vector.ndouble.Vector2d
import kotlin.properties.ReadWriteProperty
@ -38,14 +39,6 @@ class SkyParameters {
SkyParameters::surfaceLevel,
SkyParameters::seed,
)
fun registerGson(gsonBuilder: GsonBuilder) {
gsonBuilder.registerTypeAdapter(SkyParameters::class.java, ADAPTER)
gsonBuilder.registerTypeAdapter(SkyColoringManifold::class.java, SkyColoringManifold.ADAPTER)
gsonBuilder.registerTypeAdapter(SkyColoring::class.java, SkyColoring.ADAPTER)
gsonBuilder.registerTypeAdapter(SkyType::class.java, EnumAdapter(SkyType::class))
SkySatellite.registerGson(gsonBuilder)
}
}
}
@ -55,7 +48,7 @@ class SkyColoringManifold {
companion object {
val ADAPTER = object : TypeAdapter<SkyColoringManifold>() {
override fun write(out: JsonWriter?, value: SkyColoringManifold?) {
override fun write(out: JsonWriter, value: SkyColoringManifold) {
TODO("Not yet implemented")
}
@ -151,10 +144,5 @@ class SkySatellite {
)
private val LOGGER = LogManager.getLogger(SkySatellite::class.java)
fun registerGson(gsonBuilder: GsonBuilder) {
gsonBuilder.registerTypeAdapter(SkySatellite::class.java, ADAPTER)
gsonBuilder.registerTypeAdapter(Layer::class.java, LAYER_ADAPTER)
}
}
}

View File

@ -48,11 +48,5 @@ class DungeonWorldDef {
DungeonWorldDef::planetType,
DungeonWorldDef::planetSize,
)
fun registerGson(gsonBuilder: GsonBuilder) {
gsonBuilder.registerTypeAdapter(DungeonWorldDef::class.java, ADAPTER)
gsonBuilder.registerTypeAdapter(EnumAdapter(BeamUpRule::class))
gsonBuilder.registerTypeAdapter(EnumAdapter(DungeonType::class))
}
}
}

View File

@ -0,0 +1,47 @@
package ru.dbotthepony.kstarbound.io.json
import com.google.gson.Gson
import com.google.gson.JsonElement
import com.google.gson.TypeAdapter
import com.google.gson.TypeAdapterFactory
import com.google.gson.internal.bind.JsonTreeReader
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter
import java.io.StringReader
interface ContextualizedTypeAdapter<T> {
fun write(writer: JsonWriter, value: T, gson: Gson)
fun read(reader: JsonReader, gson: Gson): T
fun read(value: String, gson: Gson): T {
return read(JsonReader(StringReader(value)), gson)
}
fun read(value: JsonElement, gson: Gson): T {
return read(JsonTreeReader(value), gson)
}
}
inline fun <reified T> ContextualizedTypeAdapter<T>.factory() = ContextualizedTypeAdapterWrapper(T::class.java, this)
class ContextualizedTypeAdapterWrapper<T>(val type: Class<T>, val adapter: ContextualizedTypeAdapter<T>) : TypeAdapterFactory {
override fun <T : Any?> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
if (this.type.isAssignableFrom(type.rawType)) {
return Adapter(gson) as TypeAdapter<T>
}
return null
}
private inner class Adapter(private val gson: Gson) : TypeAdapter<T>() {
override fun write(out: JsonWriter, value: T) {
return adapter.write(out, value, gson)
}
override fun read(`in`: JsonReader): T {
return adapter.read(`in`, gson)
}
}
}

View File

@ -1,7 +1,10 @@
package ru.dbotthepony.kstarbound.io.json
import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
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.JsonToken
import com.google.gson.stream.JsonWriter
@ -72,3 +75,12 @@ fun <T> TypeAdapter<T?>.neverNull(): TypeAdapter<T> {
}
fun <T> TypeAdapter<T>.allowNull(): TypeAdapter<T?> = nullSafe()
@Deprecated("Небезопасно, надо использовать другой путь")
fun TypeAdapterFactory.ifString(reader: (String) -> Any): TypeAdapterFactory {
return object : TypeAdapterFactory {
override fun <T : Any?> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
return this@ifString.create(gson, type)?.ifString(reader as (String) -> T)
}
}
}

View File

@ -51,14 +51,14 @@ interface INativeJsonHolder : IJsonHolder {
}
@Suppress("FunctionName")
fun <T : Any> BuilderAdapter(factory: () -> T, vararg fields: KMutableProperty1<T, *>): BuilderAdapter<T> {
fun <T : Any> BuilderAdapter(factory: () -> T, vararg fields: KMutableProperty1<T, *>): TypeAdapterFactory {
val builder = BuilderAdapter.Builder(factory)
for (field in fields) {
builder.auto(field)
}
return builder.build()
return builder
}
/**
@ -214,11 +214,11 @@ class BuilderAdapter<T : Any> private constructor(
private val ignoreKeys = ObjectArraySet<String>()
var extraPropertiesAreFatal = false
var logMisses: Boolean? = null
private val factoryReturnType by lazy { factory::invoke.returnType }
private val factoryReturnType by lazy { factory.invoke()::class.java }
@OptIn(ExperimentalStdlibApi::class)
override fun <T : Any?> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
if (type.isAssignableFrom(factoryReturnType.javaType)) {
if (type.isAssignableFrom(factoryReturnType)) {
return build(gson) as TypeAdapter<T>
}

View File

@ -23,6 +23,7 @@ import ru.dbotthepony.kstarbound.defs.util.enrollList
import ru.dbotthepony.kstarbound.defs.util.enrollMap
import ru.dbotthepony.kstarbound.defs.util.flattenJsonElement
import ru.dbotthepony.kstarbound.getValue
import ru.dbotthepony.kstarbound.io.json.ifString
import ru.dbotthepony.kstarbound.io.json.util.LazyTypeProvider
import ru.dbotthepony.kstarbound.io.json.util.ListAdapter
import ru.dbotthepony.kstarbound.io.json.util.MapAdapter
@ -384,6 +385,12 @@ class FactoryAdapter<T : Any> private constructor(
}
private val types = ArrayList<IResolvableProperty<T, *>>()
private var stringTransformer: ((String) -> T)? = null
fun ifString(transformer: (String) -> T): Builder<T> {
stringTransformer = transformer
return this
}
override fun <T : Any?> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
if (type.rawType == clazz.java) {
@ -396,7 +403,7 @@ class FactoryAdapter<T : Any> private constructor(
/**
* Собирает этот [FactoryAdapter] с указанным GSON объектом
*/
fun build(gson: Gson): FactoryAdapter<T> {
fun build(gson: Gson): TypeAdapter<T> {
check(!asList || types.none { it.isFlat }) { "Can't have both flat properties and json data array layout" }
return FactoryAdapter(
@ -405,7 +412,12 @@ class FactoryAdapter<T : Any> private constructor(
asJsonArray = asList,
storesJson = storesJson,
logMisses = logMisses,
)
).let {
if (stringTransformer != null)
it.ifString(stringTransformer!!)
else
it
}
}
/**
@ -416,7 +428,7 @@ class FactoryAdapter<T : Any> private constructor(
* Несмотря на @Deprecated, данный вариант метода удалён не будет
*/
@Deprecated("Используйте как TypeAdapterFactory")
fun build(): FactoryAdapter<T> {
fun build(): TypeAdapter<T> {
check(!asList || types.none { it.isFlat }) { "Can't have both flat properties and json data array layout" }
return FactoryAdapter(
@ -425,7 +437,12 @@ class FactoryAdapter<T : Any> private constructor(
asJsonArray = asList,
storesJson = storesJson,
logMisses = logMisses,
)
).let {
if (stringTransformer != null)
it.ifString(stringTransformer!!)
else
it
}
}
/**