Move JsonUtils to json package

This commit is contained in:
DBotThePony 2024-02-02 12:30:22 +07:00
parent 4d58f0ab71
commit cb38ecfde7
Signed by: DBot
GPG Key ID: DCC23B5715498507
13 changed files with 186 additions and 202 deletions

View File

@ -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

View File

@ -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

View File

@ -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"),

View File

@ -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

View File

@ -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 {

View File

@ -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(

View File

@ -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

View File

@ -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 <T> TypeAdapter<T>.transformRead(transformer: (T) -> T): TypeAdapter<T> {
return object : TypeAdapter<T>() {
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 <T> TypeAdapter<T>.transformWrite(transformer: (T) -> T): TypeAdapter<T> {
return object : TypeAdapter<T>() {
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 <In, Out> TypeAdapter<In>.transform(read: (In) -> Out, write: (Out) -> In): TypeAdapter<Out> {
return object : TypeAdapter<Out>() {
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 <T> TypeAdapter<T>.ifString(reader: (String) -> T): TypeAdapter<T> {
return object : TypeAdapter<T>() {
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 <T> TypeAdapter<T?>.neverNull(): TypeAdapter<T> {
return object : TypeAdapter<T>() {
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 <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)
}
}
}
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 C : Collection<E>, reified E> Gson.collectionAdapter(): TypeAdapter<C> {
return getAdapter(TypeToken.getParameterized(C::class.java, E::class.java)) as TypeAdapter<C>
}
inline fun <reified E> Gson.listAdapter(): TypeAdapter<ImmutableList<E>> {
return collectionAdapter()
}
inline fun <reified E> Gson.mutableListAdapter(): TypeAdapter<ArrayList<E>> {
return collectionAdapter()
}
inline fun <reified E> Gson.setAdapter(): TypeAdapter<ImmutableSet<E>> {
return collectionAdapter()
}
inline fun <reified E> Gson.mutableSetAdapter(): TypeAdapter<ObjectOpenHashSet<E>> {
return collectionAdapter()
}
fun JsonArray(elements: Collection<JsonElement>): JsonArray {
return JsonArray(elements.size).also { elements.forEach(it::add) }
}
fun JsonArray.clear() {
while (size() > 0) {
remove(size() - 1)
}
}

View File

@ -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 <T> TypeAdapter<T>.transformRead(transformer: (T) -> T): TypeAdapter<T> {
return object : TypeAdapter<T>() {
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 <T> TypeAdapter<T>.transformWrite(transformer: (T) -> T): TypeAdapter<T> {
return object : TypeAdapter<T>() {
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 <In, Out> TypeAdapter<In>.transform(read: (In) -> Out, write: (Out) -> In): TypeAdapter<Out> {
return object : TypeAdapter<Out>() {
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 <T> TypeAdapter<T>.ifString(reader: (String) -> T): TypeAdapter<T> {
return object : TypeAdapter<T>() {
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 <T> TypeAdapter<T?>.neverNull(): TypeAdapter<T> {
return object : TypeAdapter<T>() {
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 <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)
}
}
}
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 C : Collection<E>, reified E> Gson.collectionAdapter(): TypeAdapter<C> {
return getAdapter(TypeToken.getParameterized(C::class.java, E::class.java)) as TypeAdapter<C>
}
inline fun <reified E> Gson.listAdapter(): TypeAdapter<ImmutableList<E>> {
return collectionAdapter()
}
inline fun <reified E> Gson.mutableListAdapter(): TypeAdapter<ArrayList<E>> {
return collectionAdapter()
}
inline fun <reified E> Gson.setAdapter(): TypeAdapter<ImmutableSet<E>> {
return collectionAdapter()
}
inline fun <reified E> Gson.mutableSetAdapter(): TypeAdapter<ObjectOpenHashSet<E>> {
return collectionAdapter()
}
fun JsonArray(elements: Collection<JsonElement>): 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)) }

View File

@ -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(

View File

@ -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

View File

@ -206,7 +206,7 @@ abstract class Chunk<WorldType : World<WorldType, This>, This : Chunk<WorldType,
}
override fun toString(): String {
return "Chunk(pos=$pos, entityCount=${entities.size}, world=$world)"
return "${this::class.simpleName}(pos=$pos, entityCount=${entities.size}, world=$world)"
}
fun addObject(obj: WorldObject) {

View File

@ -14,18 +14,16 @@ import ru.dbotthepony.kstarbound.defs.JsonDriven
import ru.dbotthepony.kstarbound.defs.image.SpriteReference
import ru.dbotthepony.kstarbound.defs.`object`.ObjectDefinition
import ru.dbotthepony.kstarbound.defs.`object`.ObjectOrientation
import ru.dbotthepony.kstarbound.server.world.LegacyChunkSource
import ru.dbotthepony.kstarbound.server.world.ServerWorld
import ru.dbotthepony.kstarbound.util.MailboxExecutorService
import ru.dbotthepony.kstarbound.util.get
import ru.dbotthepony.kstarbound.util.set
import ru.dbotthepony.kstarbound.json.get
import ru.dbotthepony.kstarbound.json.set
import ru.dbotthepony.kstarbound.world.Side
import ru.dbotthepony.kstarbound.world.LightCalculator
import ru.dbotthepony.kstarbound.world.World
import ru.dbotthepony.kstarbound.world.api.TileColor
import ru.dbotthepony.kvector.vector.RGBAColor
import ru.dbotthepony.kvector.vector.Vector2i
import kotlin.concurrent.withLock
import kotlin.properties.Delegates
open class WorldObject(