From cb38ecfde7d86cf1722159e1401a8151ae2eb825 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 2 Feb 2024 12:30:22 +0700 Subject: [PATCH] Move JsonUtils to json package --- .../dbotthepony/kstarbound/defs/Drawable.kt | 3 +- .../dbotthepony/kstarbound/defs/JsonDriven.kt | 2 +- .../kstarbound/defs/StatModifier.kt | 4 +- .../kstarbound/defs/image/Image.kt | 6 +- .../kstarbound/defs/item/ItemDescriptor.kt | 2 +- .../defs/object/ObjectDefinition.kt | 8 +- .../defs/object/ObjectOrientation.kt | 6 +- .../ru/dbotthepony/kstarbound/json/Ext.kt | 175 ------------------ .../kstarbound/{util => json}/JsonUtils.kt | 170 ++++++++++++++++- .../kstarbound/player/QuestInstance.kt | 2 +- .../server/world/LegacyChunkSource.kt | 2 - .../ru/dbotthepony/kstarbound/world/Chunk.kt | 2 +- .../kstarbound/world/entities/WorldObject.kt | 6 +- 13 files changed, 186 insertions(+), 202 deletions(-) delete mode 100644 src/main/kotlin/ru/dbotthepony/kstarbound/json/Ext.kt rename src/main/kotlin/ru/dbotthepony/kstarbound/{util => json}/JsonUtils.kt (59%) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/Drawable.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/Drawable.kt index adb99308..b2c52827 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/Drawable.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/Drawable.kt @@ -17,12 +17,11 @@ import ru.dbotthepony.kstarbound.json.builder.JsonFactory import ru.dbotthepony.kstarbound.json.consumeNull import ru.dbotthepony.kstarbound.math.LineF import ru.dbotthepony.kstarbound.util.Either -import ru.dbotthepony.kstarbound.util.contains +import ru.dbotthepony.kstarbound.json.contains import ru.dbotthepony.kvector.arrays.Matrix3f import ru.dbotthepony.kvector.vector.RGBAColor import ru.dbotthepony.kvector.vector.Vector2f import ru.dbotthepony.kvector.vector.Vector3f -import kotlin.math.sin sealed class Drawable(val position: Vector2f, val color: RGBAColor, val fullbright: Boolean) { @JsonFactory diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/JsonDriven.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/JsonDriven.kt index c46ef10d..3bf72e82 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/JsonDriven.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/JsonDriven.kt @@ -8,7 +8,7 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap import ru.dbotthepony.kstarbound.Starbound import ru.dbotthepony.kstarbound.util.AssetPathStack import ru.dbotthepony.kstarbound.util.Either -import ru.dbotthepony.kstarbound.util.set +import ru.dbotthepony.kstarbound.json.set import java.util.function.Consumer import java.util.function.Function import java.util.function.Supplier diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/StatModifier.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/StatModifier.kt index 6e5ba5df..cc9ceb86 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/StatModifier.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/StatModifier.kt @@ -8,8 +8,8 @@ import com.google.gson.TypeAdapter import com.google.gson.stream.JsonReader import com.google.gson.stream.JsonWriter import ru.dbotthepony.kstarbound.json.consumeNull -import ru.dbotthepony.kstarbound.util.contains -import ru.dbotthepony.kstarbound.util.get +import ru.dbotthepony.kstarbound.json.contains +import ru.dbotthepony.kstarbound.json.get enum class StatModifierType(vararg names: String) { BASE_ADDITION("value", "baseAddition"), diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/image/Image.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/image/Image.kt index 202b079e..6f7cd3ae 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/image/Image.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/image/Image.kt @@ -30,9 +30,9 @@ import ru.dbotthepony.kstarbound.IStarboundFile import ru.dbotthepony.kstarbound.client.StarboundClient import ru.dbotthepony.kstarbound.client.gl.GLTexture2D import ru.dbotthepony.kstarbound.json.consumeNull -import ru.dbotthepony.kstarbound.util.contains -import ru.dbotthepony.kstarbound.util.get -import ru.dbotthepony.kstarbound.util.getObject +import ru.dbotthepony.kstarbound.json.contains +import ru.dbotthepony.kstarbound.json.get +import ru.dbotthepony.kstarbound.json.getObject import ru.dbotthepony.kvector.vector.Vector2i import ru.dbotthepony.kvector.vector.Vector4i import java.io.BufferedInputStream diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ItemDescriptor.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ItemDescriptor.kt index 3b2c5602..f6908a9d 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ItemDescriptor.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ItemDescriptor.kt @@ -14,7 +14,7 @@ import ru.dbotthepony.kstarbound.lua.StateMachine import ru.dbotthepony.kstarbound.lua.from import ru.dbotthepony.kstarbound.lua.toJsonObject import ru.dbotthepony.kstarbound.util.KOptional -import ru.dbotthepony.kstarbound.util.get +import ru.dbotthepony.kstarbound.json.get import java.util.function.Supplier fun ItemDescriptor(data: JsonElement): ItemDescriptor { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/object/ObjectDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/object/ObjectDefinition.kt index 46ebb27c..9f326c71 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/object/ObjectDefinition.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/object/ObjectDefinition.kt @@ -24,10 +24,10 @@ import ru.dbotthepony.kstarbound.json.listAdapter import ru.dbotthepony.kstarbound.json.stream import ru.dbotthepony.kstarbound.math.PeriodicFunction import ru.dbotthepony.kstarbound.util.Either -import ru.dbotthepony.kstarbound.util.contains -import ru.dbotthepony.kstarbound.util.get -import ru.dbotthepony.kstarbound.util.getArray -import ru.dbotthepony.kstarbound.util.set +import ru.dbotthepony.kstarbound.json.contains +import ru.dbotthepony.kstarbound.json.get +import ru.dbotthepony.kstarbound.json.getArray +import ru.dbotthepony.kstarbound.json.set import ru.dbotthepony.kvector.vector.RGBAColor data class ObjectDefinition( diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/object/ObjectOrientation.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/object/ObjectOrientation.kt index a2e55220..40150278 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/object/ObjectOrientation.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/object/ObjectOrientation.kt @@ -19,9 +19,9 @@ import ru.dbotthepony.kstarbound.json.clear import ru.dbotthepony.kstarbound.json.consumeNull import ru.dbotthepony.kstarbound.json.listAdapter import ru.dbotthepony.kstarbound.json.setAdapter -import ru.dbotthepony.kstarbound.util.contains -import ru.dbotthepony.kstarbound.util.get -import ru.dbotthepony.kstarbound.util.set +import ru.dbotthepony.kstarbound.json.contains +import ru.dbotthepony.kstarbound.json.get +import ru.dbotthepony.kstarbound.json.set import ru.dbotthepony.kstarbound.world.Side import ru.dbotthepony.kvector.util2d.AABB import ru.dbotthepony.kvector.util2d.AABBi diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/json/Ext.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/json/Ext.kt deleted file mode 100644 index 4b02519d..00000000 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/json/Ext.kt +++ /dev/null @@ -1,175 +0,0 @@ -package ru.dbotthepony.kstarbound.json - -import com.google.common.collect.ImmutableList -import com.google.common.collect.ImmutableSet -import com.google.gson.Gson -import com.google.gson.JsonArray -import com.google.gson.JsonElement -import com.google.gson.JsonNull -import com.google.gson.JsonObject -import com.google.gson.JsonPrimitive -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.ObjectOpenHashSet - -fun TypeAdapter.transformRead(transformer: (T) -> T): TypeAdapter { - return object : TypeAdapter() { - override fun write(out: JsonWriter, value: T) { - return this@transformRead.write(out, value) - } - - override fun read(`in`: JsonReader): T { - return transformer(this@transformRead.read(`in`)) - } - } -} - -fun TypeAdapter.transformWrite(transformer: (T) -> T): TypeAdapter { - return object : TypeAdapter() { - override fun write(out: JsonWriter, value: T) { - return this@transformWrite.write(out, transformer(value)) - } - - override fun read(`in`: JsonReader): T { - return this@transformWrite.read(`in`) - } - } -} - -fun TypeAdapter.transform(read: (In) -> Out, write: (Out) -> In): TypeAdapter { - return object : TypeAdapter() { - override fun write(out: JsonWriter, value: Out) { - return this@transform.write(out, write(value)) - } - - override fun read(`in`: JsonReader): Out { - return read(this@transform.read(`in`)) - } - } -} - -fun TypeAdapter.ifString(reader: (String) -> T): TypeAdapter { - return object : TypeAdapter() { - override fun write(out: JsonWriter, value: T) { - return this@ifString.write(out, value) - } - - override fun read(`in`: JsonReader): T { - if (`in`.peek() == JsonToken.STRING) { - return reader(`in`.nextString()) - } - - return this@ifString.read(`in`) - } - } -} - -fun TypeAdapter.neverNull(): TypeAdapter { - return object : TypeAdapter() { - override fun write(out: JsonWriter, value: T) { - this@neverNull.write(out, value) - } - - override fun read(`in`: JsonReader): T { - val path = `in`.path - return this@neverNull.read(`in`) ?: throw JsonSyntaxException("Value was null near $path") - } - } -} - -fun TypeAdapter.allowNull(): TypeAdapter = nullSafe() - -@Deprecated("Небезопасно, надо использовать другой путь") -fun TypeAdapterFactory.ifString(reader: (String) -> Any): TypeAdapterFactory { - return object : TypeAdapterFactory { - override fun create(gson: Gson, type: TypeToken): TypeAdapter? { - return this@ifString.create(gson, type)?.ifString(reader as (String) -> T) - } - } -} - -fun JsonReader.consumeNull(): Boolean { - if (peek() == JsonToken.NULL) { - nextNull() - return true - } - - return false -} - -fun JsonWriter.value(element: JsonPrimitive) { - if (element.isBoolean) { - value(element.asBoolean) - } else if (element.isNumber) { - value(element.asNumber) - } else if (element.isString) { - value(element.asString) - } else { - throw IllegalArgumentException(element.toString()) - } -} - -fun JsonWriter.value(element: JsonNull) { - nullValue() -} - -fun JsonWriter.value(element: JsonArray) { - beginArray() - for (v in element) value(v) - endArray() -} - -fun JsonWriter.value(element: JsonObject) { - beginObject() - for ((k, v) in element.entrySet()) { - name(k) - value(v) - } - endObject() -} - -fun JsonWriter.value(element: JsonElement?) { - when (element) { - is JsonPrimitive -> value(element) - is JsonNull -> value(element) - is JsonArray -> value(element) - is JsonObject -> value(element) - null -> nullValue() - else -> throw IllegalArgumentException(element.toString()) - } -} - -inline fun , reified E> Gson.collectionAdapter(): TypeAdapter { - return getAdapter(TypeToken.getParameterized(C::class.java, E::class.java)) as TypeAdapter -} - -inline fun Gson.listAdapter(): TypeAdapter> { - return collectionAdapter() -} - -inline fun Gson.mutableListAdapter(): TypeAdapter> { - return collectionAdapter() -} - -inline fun Gson.setAdapter(): TypeAdapter> { - return collectionAdapter() -} - -inline fun Gson.mutableSetAdapter(): TypeAdapter> { - return collectionAdapter() -} - -fun JsonArray(elements: Collection): JsonArray { - return JsonArray(elements.size).also { elements.forEach(it::add) } -} - -fun JsonArray.clear() { - while (size() > 0) { - remove(size() - 1) - } -} diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/util/JsonUtils.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/json/JsonUtils.kt similarity index 59% rename from src/main/kotlin/ru/dbotthepony/kstarbound/util/JsonUtils.kt rename to src/main/kotlin/ru/dbotthepony/kstarbound/json/JsonUtils.kt index 225e245e..94d157c0 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/util/JsonUtils.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/json/JsonUtils.kt @@ -1,5 +1,8 @@ -package ru.dbotthepony.kstarbound.util +package ru.dbotthepony.kstarbound.json +import com.google.common.collect.ImmutableList +import com.google.common.collect.ImmutableSet +import com.google.gson.Gson import com.google.gson.JsonArray import com.google.gson.JsonElement import com.google.gson.JsonNull @@ -7,8 +10,169 @@ import com.google.gson.JsonObject import com.google.gson.JsonPrimitive import com.google.gson.JsonSyntaxException import com.google.gson.TypeAdapter -import ru.dbotthepony.kstarbound.json.InternedJsonElementAdapter -import java.lang.ref.Reference +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.ObjectOpenHashSet + +fun TypeAdapter.transformRead(transformer: (T) -> T): TypeAdapter { + return object : TypeAdapter() { + override fun write(out: JsonWriter, value: T) { + return this@transformRead.write(out, value) + } + + override fun read(`in`: JsonReader): T { + return transformer(this@transformRead.read(`in`)) + } + } +} + +fun TypeAdapter.transformWrite(transformer: (T) -> T): TypeAdapter { + return object : TypeAdapter() { + override fun write(out: JsonWriter, value: T) { + return this@transformWrite.write(out, transformer(value)) + } + + override fun read(`in`: JsonReader): T { + return this@transformWrite.read(`in`) + } + } +} + +fun TypeAdapter.transform(read: (In) -> Out, write: (Out) -> In): TypeAdapter { + return object : TypeAdapter() { + override fun write(out: JsonWriter, value: Out) { + return this@transform.write(out, write(value)) + } + + override fun read(`in`: JsonReader): Out { + return read(this@transform.read(`in`)) + } + } +} + +fun TypeAdapter.ifString(reader: (String) -> T): TypeAdapter { + return object : TypeAdapter() { + override fun write(out: JsonWriter, value: T) { + return this@ifString.write(out, value) + } + + override fun read(`in`: JsonReader): T { + if (`in`.peek() == JsonToken.STRING) { + return reader(`in`.nextString()) + } + + return this@ifString.read(`in`) + } + } +} + +fun TypeAdapter.neverNull(): TypeAdapter { + return object : TypeAdapter() { + override fun write(out: JsonWriter, value: T) { + this@neverNull.write(out, value) + } + + override fun read(`in`: JsonReader): T { + val path = `in`.path + return this@neverNull.read(`in`) ?: throw JsonSyntaxException("Value was null near $path") + } + } +} + +fun TypeAdapter.allowNull(): TypeAdapter = nullSafe() + +@Deprecated("Небезопасно, надо использовать другой путь") +fun TypeAdapterFactory.ifString(reader: (String) -> Any): TypeAdapterFactory { + return object : TypeAdapterFactory { + override fun create(gson: Gson, type: TypeToken): TypeAdapter? { + return this@ifString.create(gson, type)?.ifString(reader as (String) -> T) + } + } +} + +fun JsonReader.consumeNull(): Boolean { + if (peek() == JsonToken.NULL) { + nextNull() + return true + } + + return false +} + +fun JsonWriter.value(element: JsonPrimitive) { + if (element.isBoolean) { + value(element.asBoolean) + } else if (element.isNumber) { + value(element.asNumber) + } else if (element.isString) { + value(element.asString) + } else { + throw IllegalArgumentException(element.toString()) + } +} + +fun JsonWriter.value(element: JsonNull) { + nullValue() +} + +fun JsonWriter.value(element: JsonArray) { + beginArray() + for (v in element) value(v) + endArray() +} + +fun JsonWriter.value(element: JsonObject) { + beginObject() + for ((k, v) in element.entrySet()) { + name(k) + value(v) + } + endObject() +} + +fun JsonWriter.value(element: JsonElement?) { + when (element) { + is JsonPrimitive -> value(element) + is JsonNull -> value(element) + is JsonArray -> value(element) + is JsonObject -> value(element) + null -> nullValue() + else -> throw IllegalArgumentException(element.toString()) + } +} + +inline fun , reified E> Gson.collectionAdapter(): TypeAdapter { + return getAdapter(TypeToken.getParameterized(C::class.java, E::class.java)) as TypeAdapter +} + +inline fun Gson.listAdapter(): TypeAdapter> { + return collectionAdapter() +} + +inline fun Gson.mutableListAdapter(): TypeAdapter> { + return collectionAdapter() +} + +inline fun Gson.setAdapter(): TypeAdapter> { + return collectionAdapter() +} + +inline fun Gson.mutableSetAdapter(): TypeAdapter> { + return collectionAdapter() +} + +fun JsonArray(elements: Collection): JsonArray { + return JsonArray(elements.size).also { elements.forEach(it::add) } +} + +fun JsonArray.clear() { + while (size() > 0) { + remove(size() - 1) + } +} operator fun JsonObject.set(key: String, value: JsonElement?) { add(key, value) } operator fun JsonObject.set(key: String, value: String) { add(key, JsonPrimitive(value)) } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/player/QuestInstance.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/player/QuestInstance.kt index a54ca1de..7598fc56 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/player/QuestInstance.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/player/QuestInstance.kt @@ -10,7 +10,7 @@ import ru.dbotthepony.kstarbound.Starbound import ru.dbotthepony.kstarbound.defs.quest.QuestTemplate import ru.dbotthepony.kstarbound.lua.NewLuaState import ru.dbotthepony.kstarbound.util.ItemStack -import ru.dbotthepony.kstarbound.util.set +import ru.dbotthepony.kstarbound.json.set import java.util.UUID class QuestInstance( diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/LegacyChunkSource.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/LegacyChunkSource.kt index 1a458518..38fb3285 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/LegacyChunkSource.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/LegacyChunkSource.kt @@ -1,13 +1,11 @@ package ru.dbotthepony.kstarbound.server.world import org.apache.logging.log4j.LogManager -import ru.dbotthepony.kstarbound.Registries import ru.dbotthepony.kstarbound.Starbound import ru.dbotthepony.kstarbound.io.BTreeDB import ru.dbotthepony.kstarbound.io.readVarInt import ru.dbotthepony.kstarbound.json.VersionedJson import ru.dbotthepony.kstarbound.util.KOptional -import ru.dbotthepony.kstarbound.util.get import ru.dbotthepony.kstarbound.world.CHUNK_SIZE import ru.dbotthepony.kstarbound.world.ChunkPos import ru.dbotthepony.kstarbound.world.api.AbstractCell diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/Chunk.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/Chunk.kt index 26f75e55..af6ccb29 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/Chunk.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/Chunk.kt @@ -206,7 +206,7 @@ abstract class Chunk, This : Chunk