Полностью перешли на TypeAdapterFactory, наверное
This commit is contained in:
parent
c80372e0fa
commit
6ea4d7a6ab
@ -52,14 +52,18 @@ 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.ArrayListAdapterFactory
|
||||
import ru.dbotthepony.kstarbound.io.json.factory.ImmutableCollectionAdapterFactory
|
||||
import ru.dbotthepony.kstarbound.math.PolyTypeAdapter
|
||||
|
||||
fun addStarboundJsonAdapters(builder: GsonBuilder) {
|
||||
with(builder) {
|
||||
// ImmutableList, и прочее
|
||||
// ImmutableList, ImmutableSet, ImmutableMap
|
||||
registerTypeAdapterFactory(ImmutableCollectionAdapterFactory)
|
||||
|
||||
// ArrayList
|
||||
registerTypeAdapterFactory(ArrayListAdapterFactory)
|
||||
|
||||
registerTypeAdapter(ColorTypeAdapter.nullSafe())
|
||||
|
||||
// математические классы
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ru.dbotthepony.kstarbound.defs.item
|
||||
|
||||
import com.google.common.collect.ImmutableList
|
||||
import ru.dbotthepony.kstarbound.Starbound
|
||||
import ru.dbotthepony.kstarbound.defs.util.enrollMap
|
||||
import ru.dbotthepony.kstarbound.io.json.builder.BuilderAdapter
|
||||
@ -9,13 +10,13 @@ import ru.dbotthepony.kstarbound.io.json.neverNull
|
||||
import ru.dbotthepony.kstarbound.util.NotNullVar
|
||||
|
||||
class ArmorItemPrototype : ItemPrototype(), IArmorItemDefinition {
|
||||
override var colorOptions: List<Map<String, String>> = listOf()
|
||||
override var colorOptions: ImmutableList<Map<String, String>> = ImmutableList.of()
|
||||
override var maleFrames: IArmorItemDefinition.ArmorFrames by NotNullVar()
|
||||
override var femaleFrames: IArmorItemDefinition.ArmorFrames by NotNullVar()
|
||||
override var level: Double = 1.0
|
||||
override var leveledStatusEffects: List<LeveledStatusEffect> = listOf()
|
||||
override var leveledStatusEffects: ImmutableList<LeveledStatusEffect> = ImmutableList.of()
|
||||
|
||||
override var scripts: List<String> = listOf()
|
||||
override var scripts: ImmutableList<String> = ImmutableList.of()
|
||||
override var scriptDelta: Int = 1
|
||||
|
||||
override var armorType: ArmorPieceType by NotNullVar()
|
||||
@ -60,7 +61,7 @@ class ArmorItemPrototype : ItemPrototype(), IArmorItemDefinition {
|
||||
companion object {
|
||||
val ADAPTER = BuilderAdapter.Builder(::ArmorItemPrototype)
|
||||
.also { addFields(it as BuilderAdapter.Builder<ItemPrototype>) } // безопасность: свойства родительского класса объявлены как final
|
||||
.add(ArmorItemPrototype::colorOptions, Starbound.NULLABLE_STRING_ADAPTER.neverNull().asJsonObject().asList())
|
||||
.auto(ArmorItemPrototype::colorOptions)
|
||||
.auto(ArmorItemPrototype::maleFrames)
|
||||
.auto(ArmorItemPrototype::femaleFrames)
|
||||
.auto(ArmorItemPrototype::level)
|
||||
|
@ -1,13 +1,14 @@
|
||||
package ru.dbotthepony.kstarbound.defs.item
|
||||
|
||||
import com.google.common.collect.ImmutableList
|
||||
import ru.dbotthepony.kstarbound.defs.util.enrollMap
|
||||
import ru.dbotthepony.kstarbound.io.json.builder.BuilderAdapter
|
||||
import ru.dbotthepony.kstarbound.util.NotNullVar
|
||||
|
||||
class CurrencyItemPrototype : ItemPrototype(), ICurrencyItemDefinition {
|
||||
override var pickupSoundsSmall: List<String> = listOf()
|
||||
override var pickupSoundsMedium: List<String> = listOf()
|
||||
override var pickupSoundsLarge: List<String> = listOf()
|
||||
override var pickupSoundsSmall: ImmutableList<String> = ImmutableList.of()
|
||||
override var pickupSoundsMedium: ImmutableList<String> = ImmutableList.of()
|
||||
override var pickupSoundsLarge: ImmutableList<String> = ImmutableList.of()
|
||||
override var smallStackLimit: Long by NotNullVar()
|
||||
override var mediumStackLimit: Long by NotNullVar()
|
||||
override var currency: String by NotNullVar()
|
||||
|
@ -14,16 +14,16 @@ open class ItemPrototype : IItemDefinition, INativeJsonHolder {
|
||||
final override var price: Long = 0L
|
||||
final override var rarity: ItemRarity = ItemRarity.COMMON
|
||||
final override var category: String? = null
|
||||
final override var inventoryIcon: ArrayList<IItemDefinition.InventoryIcon>? = null
|
||||
final override var itemTags: ArrayList<String> = ArrayList()
|
||||
final override var learnBlueprintsOnPickup: ArrayList<String> = ArrayList()
|
||||
final override var inventoryIcon: ImmutableList<IItemDefinition.InventoryIcon>? = null
|
||||
final override var itemTags: ImmutableList<String> = ImmutableList.of()
|
||||
final override var learnBlueprintsOnPickup: ImmutableList<String> = ImmutableList.of()
|
||||
final override var maxStack: Long = 9999L
|
||||
final override var eventCategory: String? = null
|
||||
final override var consumeOnPickup: Boolean = false
|
||||
final override var pickupQuestTemplates: ArrayList<String> = ArrayList()
|
||||
final override var pickupQuestTemplates: ImmutableList<String> = ImmutableList.of()
|
||||
final override var tooltipKind: ItemTooltipKind = ItemTooltipKind.NORMAL
|
||||
final override var twoHanded: Boolean = false
|
||||
final override var radioMessagesOnPickup: ArrayList<String> = ArrayList()
|
||||
final override var radioMessagesOnPickup: ImmutableList<String> = ImmutableList.of()
|
||||
final override var fuelAmount: Long? = null
|
||||
|
||||
var descriptionData: ThingDescription by NotNullVar()
|
||||
|
@ -218,7 +218,7 @@ class BuilderAdapter<T : Any> private constructor(
|
||||
|
||||
@OptIn(ExperimentalStdlibApi::class)
|
||||
override fun <T : Any?> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
|
||||
if (type.isAssignableFrom(factoryReturnType)) {
|
||||
if (type.rawType == factoryReturnType) {
|
||||
return build(gson) as TypeAdapter<T>
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,17 @@
|
||||
package ru.dbotthepony.kstarbound.io.json.factory
|
||||
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.TypeAdapterFactory
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import java.lang.reflect.ParameterizedType
|
||||
|
||||
object ArrayListAdapterFactory : TypeAdapterFactory {
|
||||
override fun <T : Any?> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
|
||||
if (ArrayList::class.java.isAssignableFrom(type.rawType) && type.type is ParameterizedType) {
|
||||
return ArrayListTypeAdapter(gson.getAdapter(TypeToken.get((type.type as ParameterizedType).actualTypeArguments[0]))).nullSafe() as TypeAdapter<T>
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package ru.dbotthepony.kstarbound.io.json.factory
|
||||
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonToken
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import java.util.ArrayList
|
||||
|
||||
class ArrayListTypeAdapter<E>(val elementAdapter: TypeAdapter<E>) : TypeAdapter<java.util.ArrayList<E>>() {
|
||||
override fun write(out: JsonWriter, value: ArrayList<E>) {
|
||||
if (value.size == 1) {
|
||||
elementAdapter.write(out, value[0])
|
||||
return
|
||||
}
|
||||
|
||||
out.beginArray()
|
||||
|
||||
for (v in value) {
|
||||
elementAdapter.write(out, v)
|
||||
}
|
||||
|
||||
out.endArray()
|
||||
}
|
||||
|
||||
override fun read(reader: JsonReader): ArrayList<E> {
|
||||
if (reader.peek() != JsonToken.BEGIN_ARRAY) {
|
||||
// не массив, возможно упрощение структуры "a": [value] -> "a": value
|
||||
val list = ArrayList<E>(1)
|
||||
list.add(elementAdapter.read(reader))
|
||||
return list
|
||||
}
|
||||
|
||||
reader.beginArray()
|
||||
|
||||
val list = ArrayList<E>()
|
||||
|
||||
while (reader.peek() != JsonToken.END_ARRAY) {
|
||||
list.add(elementAdapter.read(reader))
|
||||
}
|
||||
|
||||
reader.endArray()
|
||||
|
||||
return list
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user