EnumAdapter для всех enum'ов
This commit is contained in:
parent
6ea4d7a6ab
commit
38d341913a
@ -46,6 +46,7 @@ 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.factory.ArrayListAdapterFactory
|
||||
import ru.dbotthepony.kstarbound.io.json.factory.ImmutableCollectionAdapterFactory
|
||||
import ru.dbotthepony.kstarbound.math.*
|
||||
import java.io.*
|
||||
@ -149,6 +150,15 @@ object Starbound {
|
||||
// чтоб строки всегда intern'ились
|
||||
.registerTypeAdapter(NULLABLE_STRING_ADAPTER)
|
||||
|
||||
// ImmutableList, ImmutableSet, ImmutableMap
|
||||
.registerTypeAdapterFactory(ImmutableCollectionAdapterFactory)
|
||||
|
||||
// ArrayList
|
||||
.registerTypeAdapterFactory(ArrayListAdapterFactory)
|
||||
|
||||
// все enum'ы без особых настроек
|
||||
.registerTypeAdapterFactory(EnumAdapter.Companion)
|
||||
|
||||
.also(::addStarboundJsonAdapters)
|
||||
|
||||
.create()
|
||||
|
@ -58,12 +58,6 @@ import ru.dbotthepony.kstarbound.math.PolyTypeAdapter
|
||||
|
||||
fun addStarboundJsonAdapters(builder: GsonBuilder) {
|
||||
with(builder) {
|
||||
// ImmutableList, ImmutableSet, ImmutableMap
|
||||
registerTypeAdapterFactory(ImmutableCollectionAdapterFactory)
|
||||
|
||||
// ArrayList
|
||||
registerTypeAdapterFactory(ArrayListAdapterFactory)
|
||||
|
||||
registerTypeAdapter(ColorTypeAdapter.nullSafe())
|
||||
|
||||
// математические классы
|
||||
@ -77,7 +71,6 @@ fun addStarboundJsonAdapters(builder: GsonBuilder) {
|
||||
|
||||
// Снаряды
|
||||
registerTypeAdapterFactory(ConfigurableProjectile.ADAPTER)
|
||||
registerTypeAdapter(EnumAdapter(ProjectilePhysics::class).neverNull())
|
||||
registerTypeAdapterFactory(ActionConfig.ADAPTER)
|
||||
registerTypeAdapterFactory(ActionProjectile.ADAPTER)
|
||||
registerTypeAdapterFactory(ActionSound.ADAPTER)
|
||||
@ -88,14 +81,11 @@ fun addStarboundJsonAdapters(builder: GsonBuilder) {
|
||||
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)
|
||||
@ -128,7 +118,6 @@ fun addStarboundJsonAdapters(builder: GsonBuilder) {
|
||||
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)
|
||||
@ -142,7 +131,5 @@ fun addStarboundJsonAdapters(builder: GsonBuilder) {
|
||||
registerTypeAdapter(ThingDescription.Companion)
|
||||
|
||||
registerTypeAdapter(EnumAdapter(DamageType::class, default = DamageType.NORMAL))
|
||||
registerTypeAdapter(EnumAdapter(ItemRarity::class))
|
||||
registerTypeAdapter(EnumAdapter(ItemTooltipKind::class))
|
||||
}
|
||||
}
|
||||
|
@ -3,12 +3,17 @@ package ru.dbotthepony.kstarbound.io.json.builder
|
||||
import com.google.common.collect.ImmutableList
|
||||
import com.google.common.collect.ImmutableMap
|
||||
import com.google.common.collect.Streams
|
||||
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
|
||||
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.stream.Stream
|
||||
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 mapping: ImmutableMap<String, T>
|
||||
private val areCustom = IStringSerializable::class.isSuperclassOf(enum)
|
||||
private val misses = ObjectOpenHashSet<String>()
|
||||
|
||||
init {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user