diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/json/BinaryJsonWriter.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/json/BinaryJsonWriter.kt index bafc753a..9cd50eb5 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/json/BinaryJsonWriter.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/json/BinaryJsonWriter.kt @@ -21,7 +21,7 @@ private enum class DeflateType { NONE } -private fun T.callWrite(deflate: DeflateType, callable: DataOutputStream.(T) -> Unit): ByteArray { +private fun T.callWrite(deflate: DeflateType, zstdCompressionLevel: Int = 6, callable: DataOutputStream.(T) -> Unit): ByteArray { val stream = FastByteArrayOutputStream() when (deflate) { @@ -36,7 +36,7 @@ private fun T.callWrite(deflate: DeflateType, callable: DataOutputStream.(T) DeflateType.ZSTD -> { val s = ZstdOutputStreamNoFinalizer(stream) - s.setLevel(6) + s.setLevel(zstdCompressionLevel) DataOutputStream(BufferedOutputStream(s, 0x10000)).use { callable(it, this) @@ -57,9 +57,9 @@ fun JsonElement.writeJsonElementDeflated(): ByteArray = callWrite(DeflateType.ZL fun JsonObject.writeJsonObjectDeflated(): ByteArray = callWrite(DeflateType.ZLIB) { writeJsonObject(it) } fun JsonArray.writeJsonArrayDeflated(): ByteArray = callWrite(DeflateType.ZLIB) { writeJsonArray(it) } -fun JsonElement.writeJsonElementZstd(): ByteArray = callWrite(DeflateType.ZSTD) { writeJsonElement(it) } -fun JsonObject.writeJsonObjectZstd(): ByteArray = callWrite(DeflateType.ZSTD) { writeJsonObject(it) } -fun JsonArray.writeJsonArrayZstd(): ByteArray = callWrite(DeflateType.ZSTD) { writeJsonArray(it) } +fun JsonElement.writeJsonElementZstd(level: Int = 6): ByteArray = callWrite(DeflateType.ZSTD, zstdCompressionLevel = level) { writeJsonElement(it) } +fun JsonObject.writeJsonObjectZstd(level: Int = 6): ByteArray = callWrite(DeflateType.ZSTD, zstdCompressionLevel = level) { writeJsonObject(it) } +fun JsonArray.writeJsonArrayZstd(level: Int = 6): ByteArray = callWrite(DeflateType.ZSTD, zstdCompressionLevel = level) { writeJsonArray(it) } fun DataOutputStream.writeJsonElement(value: JsonElement) { when (value) { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/server/ServerConnection.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/server/ServerConnection.kt index 07fe5d1b..28f879ec 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/server/ServerConnection.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/server/ServerConnection.kt @@ -742,13 +742,13 @@ class ServerConnection(val server: StarboundServer, type: ConnectionType) : Conn val startingLocation = findStartingSystem() ?: return scope.launch { shipFlightEventLoop(startingLocation.location, SystemWorldLocation.Celestial(startingLocation)) } } else { - /*if (context.returnWarp != null) { + if (context.returnWarp != null) { enqueueWarp(context.returnWarp, ifFailed = WarpAlias.OwnShip) } else { enqueueWarp(WarpAlias.OwnShip) - }*/ + } - enqueueWarp(WarpAction.World(WorldID.Instance("outpost"))) + //enqueueWarp(WarpAction.World(WorldID.Instance("outpost"))) scope.launch { shipFlightEventLoop(context.shipCoordinate, context.systemLocation) } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/ServerUniverse.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/ServerUniverse.kt index ebdf9bca..efa9a7eb 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/ServerUniverse.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/ServerUniverse.kt @@ -8,19 +8,15 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.SupervisorJob -import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.async import kotlinx.coroutines.cancel -import kotlinx.coroutines.delay import kotlinx.coroutines.future.asCompletableFuture import kotlinx.coroutines.future.await import ru.dbotthepony.kommons.gson.JsonArrayCollector import ru.dbotthepony.kommons.gson.contains import ru.dbotthepony.kommons.gson.get import ru.dbotthepony.kommons.gson.set -import ru.dbotthepony.kstarbound.math.AABBi import ru.dbotthepony.kommons.util.KOptional -import ru.dbotthepony.kstarbound.math.vector.Vector2i import ru.dbotthepony.kstarbound.Globals import ru.dbotthepony.kstarbound.Starbound import ru.dbotthepony.kstarbound.defs.world.CelestialBaseInformation @@ -30,11 +26,13 @@ import ru.dbotthepony.kstarbound.fromJson import ru.dbotthepony.kstarbound.io.BTreeDB5 import ru.dbotthepony.kstarbound.json.jsonArrayOf import ru.dbotthepony.kstarbound.json.mergeJson -import ru.dbotthepony.kstarbound.json.readJsonArrayInflated -import ru.dbotthepony.kstarbound.json.readJsonElementInflated -import ru.dbotthepony.kstarbound.json.writeJsonArrayDeflated -import ru.dbotthepony.kstarbound.json.writeJsonElementDeflated +import ru.dbotthepony.kstarbound.json.readJsonArrayZstd +import ru.dbotthepony.kstarbound.json.readJsonElementZstd +import ru.dbotthepony.kstarbound.json.writeJsonArrayZstd +import ru.dbotthepony.kstarbound.json.writeJsonElementZstd +import ru.dbotthepony.kstarbound.math.AABBi import ru.dbotthepony.kstarbound.math.Line2d +import ru.dbotthepony.kstarbound.math.vector.Vector2i import ru.dbotthepony.kstarbound.math.vector.Vector3i import ru.dbotthepony.kstarbound.util.CarriedExecutor import ru.dbotthepony.kstarbound.util.ScheduledCoroutineExecutor @@ -57,11 +55,9 @@ import java.sql.ResultSet import java.util.concurrent.CompletableFuture import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.TimeUnit -import java.util.function.Consumer import java.util.function.Function import java.util.function.Supplier import java.util.random.RandomGenerator -import kotlin.collections.ArrayList class ServerUniverse(folder: File? = null) : Universe(), Closeable { override val baseInformation: CelestialBaseInformation @@ -148,8 +144,8 @@ class ServerUniverse(folder: File? = null) : Universe(), Closeable { private data class Chunk(val x: Int, val y: Int, val systems: Set, val constellations: Set>) { constructor(x: Int, y: Int, data: ResultSet) : this( x, y, - data.getBytes(1).readJsonArrayInflated().map { Vector3i(it.asJsonArray[0].asInt, it.asJsonArray[1].asInt, it.asJsonArray[2].asInt) }.toSet(), - data.getBytes(2).readJsonArrayInflated().map { + data.getBytes(1).readJsonArrayZstd().map { Vector3i(it.asJsonArray[0].asInt, it.asJsonArray[1].asInt, it.asJsonArray[2].asInt) }.toSet(), + data.getBytes(2).readJsonArrayZstd().map { val a = it.asJsonArray[0].asJsonArray val b = it.asJsonArray[1].asJsonArray Vector2i(a[0].asInt, a[1].asInt) to Vector2i(b[0].asInt, b[1].asInt) @@ -162,10 +158,10 @@ class ServerUniverse(folder: File? = null) : Universe(), Closeable { systems.stream() .map { jsonArrayOf(it.x, it.y, it.z) } .collect(JsonArrayCollector) - .writeJsonArrayDeflated(), + .writeJsonArrayZstd(4), constellations.stream().map { jsonArrayOf(jsonArrayOf(it.first.x, it.first.y), jsonArrayOf(it.second.x, it.second.y)) - }.collect(JsonArrayCollector).writeJsonArrayDeflated() + }.collect(JsonArrayCollector).writeJsonArrayZstd(4) ) } @@ -198,10 +194,10 @@ class ServerUniverse(folder: File? = null) : Universe(), Closeable { fun serialize(): SerializedSystem { return SerializedSystem( x, y, z, - Starbound.gson.toJsonTree(parameters).writeJsonElementDeflated(), + Starbound.gson.toJsonTree(parameters).writeJsonElementZstd(8), planets.entries.stream() .map { jsonArrayOf(it.key.first, it.key.second, it.value) } - .collect(JsonArrayCollector).writeJsonArrayDeflated() + .collect(JsonArrayCollector).writeJsonArrayZstd(8) ) } @@ -270,9 +266,9 @@ class ServerUniverse(folder: File? = null) : Universe(), Closeable { // deserialize in off-thread since it involves big json structures Starbound.EXECUTOR.supplyAsync { - val parameters: CelestialParameters = Starbound.gson.fromJson(parametersBytes.readJsonElementInflated())!! + val parameters: CelestialParameters = Starbound.gson.fromJson(parametersBytes.readJsonElementZstd())!! - val planets: Map, CelestialParameters> = planetsBytes.readJsonArrayInflated().associate { + val planets: Map, CelestialParameters> = planetsBytes.readJsonArrayZstd().associate { it as JsonArray (it[0].asInt to it[1].asInt) to Starbound.gson.fromJson(it[2])!! }