EnumAdapter для всех enum'ов

This commit is contained in:
DBotThePony 2023-01-22 23:39:44 +07:00
parent 6ea4d7a6ab
commit 38d341913a
Signed by: DBot
GPG Key ID: DCC23B5715498507
3 changed files with 38 additions and 14 deletions

View File

@ -46,6 +46,7 @@ import ru.dbotthepony.kstarbound.io.json.Vector2dTypeAdapter
import ru.dbotthepony.kstarbound.io.json.Vector2fTypeAdapter import ru.dbotthepony.kstarbound.io.json.Vector2fTypeAdapter
import ru.dbotthepony.kstarbound.io.json.Vector2iTypeAdapter import ru.dbotthepony.kstarbound.io.json.Vector2iTypeAdapter
import ru.dbotthepony.kstarbound.io.json.Vector4iTypeAdapter import ru.dbotthepony.kstarbound.io.json.Vector4iTypeAdapter
import ru.dbotthepony.kstarbound.io.json.factory.ArrayListAdapterFactory
import ru.dbotthepony.kstarbound.io.json.factory.ImmutableCollectionAdapterFactory import ru.dbotthepony.kstarbound.io.json.factory.ImmutableCollectionAdapterFactory
import ru.dbotthepony.kstarbound.math.* import ru.dbotthepony.kstarbound.math.*
import java.io.* import java.io.*
@ -149,6 +150,15 @@ object Starbound {
// чтоб строки всегда intern'ились // чтоб строки всегда intern'ились
.registerTypeAdapter(NULLABLE_STRING_ADAPTER) .registerTypeAdapter(NULLABLE_STRING_ADAPTER)
// ImmutableList, ImmutableSet, ImmutableMap
.registerTypeAdapterFactory(ImmutableCollectionAdapterFactory)
// ArrayList
.registerTypeAdapterFactory(ArrayListAdapterFactory)
// все enum'ы без особых настроек
.registerTypeAdapterFactory(EnumAdapter.Companion)
.also(::addStarboundJsonAdapters) .also(::addStarboundJsonAdapters)
.create() .create()

View File

@ -58,12 +58,6 @@ import ru.dbotthepony.kstarbound.math.PolyTypeAdapter
fun addStarboundJsonAdapters(builder: GsonBuilder) { fun addStarboundJsonAdapters(builder: GsonBuilder) {
with(builder) { with(builder) {
// ImmutableList, ImmutableSet, ImmutableMap
registerTypeAdapterFactory(ImmutableCollectionAdapterFactory)
// ArrayList
registerTypeAdapterFactory(ArrayListAdapterFactory)
registerTypeAdapter(ColorTypeAdapter.nullSafe()) registerTypeAdapter(ColorTypeAdapter.nullSafe())
// математические классы // математические классы
@ -77,7 +71,6 @@ fun addStarboundJsonAdapters(builder: GsonBuilder) {
// Снаряды // Снаряды
registerTypeAdapterFactory(ConfigurableProjectile.ADAPTER) registerTypeAdapterFactory(ConfigurableProjectile.ADAPTER)
registerTypeAdapter(EnumAdapter(ProjectilePhysics::class).neverNull())
registerTypeAdapterFactory(ActionConfig.ADAPTER) registerTypeAdapterFactory(ActionConfig.ADAPTER)
registerTypeAdapterFactory(ActionProjectile.ADAPTER) registerTypeAdapterFactory(ActionProjectile.ADAPTER)
registerTypeAdapterFactory(ActionSound.ADAPTER) registerTypeAdapterFactory(ActionSound.ADAPTER)
@ -88,14 +81,11 @@ fun addStarboundJsonAdapters(builder: GsonBuilder) {
registerTypeAdapterFactory(SkyParameters.ADAPTER) registerTypeAdapterFactory(SkyParameters.ADAPTER)
registerTypeAdapter(SkyColoringManifold.ADAPTER) registerTypeAdapter(SkyColoringManifold.ADAPTER)
registerTypeAdapterFactory(SkyColoring.ADAPTER) registerTypeAdapterFactory(SkyColoring.ADAPTER)
registerTypeAdapter(EnumAdapter(SkyType::class))
registerTypeAdapterFactory(SkySatellite.ADAPTER) registerTypeAdapterFactory(SkySatellite.ADAPTER)
registerTypeAdapterFactory(SkySatellite.LAYER_ADAPTER) registerTypeAdapterFactory(SkySatellite.LAYER_ADAPTER)
// Данные о данжах // Данные о данжах
registerTypeAdapterFactory(DungeonWorldDef.ADAPTER) registerTypeAdapterFactory(DungeonWorldDef.ADAPTER)
registerTypeAdapter(EnumAdapter(BeamUpRule::class))
registerTypeAdapter(EnumAdapter(DungeonType::class))
// Параллакс // Параллакс
registerTypeAdapterFactory(ParallaxPrototype.ADAPTER) registerTypeAdapterFactory(ParallaxPrototype.ADAPTER)
@ -128,7 +118,6 @@ fun addStarboundJsonAdapters(builder: GsonBuilder) {
registerTypeAdapterFactory(RenderMatchList.ADAPTER) registerTypeAdapterFactory(RenderMatchList.ADAPTER)
registerTypeAdapterFactory(RenderRuleList.Entry.ADAPTER) registerTypeAdapterFactory(RenderRuleList.Entry.ADAPTER)
registerTypeAdapterFactory(RenderTemplate.ADAPTER) registerTypeAdapterFactory(RenderTemplate.ADAPTER)
registerTypeAdapter(EnumAdapter(RenderRuleList.Combination::class))
registerTypeAdapterFactory(TileDefinition.ADAPTER) registerTypeAdapterFactory(TileDefinition.ADAPTER)
registerTypeAdapterFactory(LiquidDefinition.ADAPTER) registerTypeAdapterFactory(LiquidDefinition.ADAPTER)
registerTypeAdapterFactory(LiquidDefinition.INTERACTION_ADAPTER) registerTypeAdapterFactory(LiquidDefinition.INTERACTION_ADAPTER)
@ -142,7 +131,5 @@ fun addStarboundJsonAdapters(builder: GsonBuilder) {
registerTypeAdapter(ThingDescription.Companion) registerTypeAdapter(ThingDescription.Companion)
registerTypeAdapter(EnumAdapter(DamageType::class, default = DamageType.NORMAL)) registerTypeAdapter(EnumAdapter(DamageType::class, default = DamageType.NORMAL))
registerTypeAdapter(EnumAdapter(ItemRarity::class))
registerTypeAdapter(EnumAdapter(ItemTooltipKind::class))
} }
} }

View File

@ -3,12 +3,17 @@ package ru.dbotthepony.kstarbound.io.json.builder
import com.google.common.collect.ImmutableList import com.google.common.collect.ImmutableList
import com.google.common.collect.ImmutableMap import com.google.common.collect.ImmutableMap
import com.google.common.collect.Streams import com.google.common.collect.Streams
import com.google.gson.Gson
import com.google.gson.JsonSyntaxException import com.google.gson.JsonSyntaxException
import com.google.gson.TypeAdapter 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.JsonReader
import com.google.gson.stream.JsonToken import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter import com.google.gson.stream.JsonWriter
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
import org.apache.logging.log4j.LogManager
import java.util.Arrays import java.util.Arrays
import java.util.stream.Stream import java.util.stream.Stream
import kotlin.reflect.KClass import kotlin.reflect.KClass
@ -54,6 +59,7 @@ class EnumAdapter<T : Enum<T>>(private val enum: KClass<T>, values: Stream<T> =
private val values = values.collect(ImmutableList.toImmutableList()) private val values = values.collect(ImmutableList.toImmutableList())
private val mapping: ImmutableMap<String, T> private val mapping: ImmutableMap<String, T>
private val areCustom = IStringSerializable::class.isSuperclassOf(enum) private val areCustom = IStringSerializable::class.isSuperclassOf(enum)
private val misses = ObjectOpenHashSet<String>()
init { init {
val builder = Object2ObjectArrayMap<String, T>() val builder = Object2ObjectArrayMap<String, T>()
@ -102,7 +108,13 @@ class EnumAdapter<T : Enum<T>>(private val enum: KClass<T>, values: Stream<T> =
} }
} }
return mapping[key] ?: default val existing = mapping[key]
if (existing == null && misses.add(key)) {
LOGGER.error("$key is not a valid ${enum.qualifiedName} value")
}
return existing ?: default
} }
/** /**
@ -129,4 +141,19 @@ class EnumAdapter<T : Enum<T>>(private val enum: KClass<T>, values: Stream<T> =
} }
} }
} }
private enum class Never
companion object : TypeAdapterFactory {
private val LOGGER = LogManager.getLogger()
override fun <T : Any?> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
if (type.rawType.isEnum) {
val clazz = type.rawType as Class<Never>
return EnumAdapter(clazz, clazz.enumConstants) as TypeAdapter<T>
}
return null
}
}
} }