From 1c9405199c7f726a26cf2677a18ef3525240d127 Mon Sep 17 00:00:00 2001 From: creeper123123321 <7974274+creeper123123321@users.noreply.github.com> Date: Wed, 29 Jun 2022 08:34:51 -0300 Subject: [PATCH] optimize PacketRegistry, update deps --- build.gradle.kts | 8 +-- .../aas/codec/packet/PacketRegistry.kt | 53 +++++++++++-------- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index ce998a2..439f723 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -57,8 +57,8 @@ dependencies { implementation(kotlin("stdlib-jdk8")) implementation(kotlin("reflect")) - val vvVer = "4.3.2-SNAPSHOT" - val vbVer = "4.3.1-SNAPSHOT" + val vvVer = "4.3.2-1.19.1-rc1-SNAPSHOT" + val vbVer = "4.3.1-1.19.1-pre1-SNAPSHOT" val vrVer = "d189537" implementation("com.viaversion:viaversion:$vvVer") { isTransitive = false } implementation("com.viaversion:viabackwards:$vbVer") { isTransitive = false } @@ -84,7 +84,7 @@ dependencies { implementation("org.jline:jline-terminal-jansi:3.21.0") implementation("org.slf4j:slf4j-api:$slf4jVer") - val ktorVersion = "2.0.2" + val ktorVersion = "2.0.3" implementation("io.ktor:ktor-network-tls-certificates-jvm:$ktorVersion") implementation("io.ktor:ktor-server-websockets:$ktorVersion") implementation("io.ktor:ktor-server-netty-jvm:$ktorVersion") @@ -101,7 +101,7 @@ dependencies { implementation("io.ktor:ktor-client-logging-jvm:$ktorVersion") testImplementation("io.ktor:ktor-server-test-host-jvm:$ktorVersion") - implementation("com.auth0:java-jwt:3.19.2") + implementation("com.auth0:java-jwt:4.0.0") } val run: JavaExec by tasks diff --git a/src/main/kotlin/com/viaversion/aas/codec/packet/PacketRegistry.kt b/src/main/kotlin/com/viaversion/aas/codec/packet/PacketRegistry.kt index 46af288..c4b6008 100644 --- a/src/main/kotlin/com/viaversion/aas/codec/packet/PacketRegistry.kt +++ b/src/main/kotlin/com/viaversion/aas/codec/packet/PacketRegistry.kt @@ -1,6 +1,8 @@ package com.viaversion.aas.codec.packet import com.google.common.collect.Range +import com.google.common.collect.RangeMap +import com.google.common.collect.TreeRangeMap import com.viaversion.aas.codec.packet.handshake.Handshake import com.viaversion.aas.codec.packet.login.* import com.viaversion.aas.codec.packet.play.Kick @@ -31,7 +33,10 @@ import io.netty.util.ReferenceCountUtil import java.util.function.Supplier object PacketRegistry { - val entries = mutableListOf() + // state, direction, packet id, protocol version -> entry + val entriesDecoding = hashMapOf, RangeMap>() + // direction, type, protocol version -> entry + val entriesEncoding = hashMapOf>, RangeMap>() init { // Obviously stolen from https://github.com/VelocityPowered/Velocity/blob/dev/1.1.0/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -63,7 +68,6 @@ object PacketRegistry { register(State.STATUS, Direction.CLIENTBOUND, ::StatusResponse, Range.all(), 0) register(State.STATUS, Direction.CLIENTBOUND, ::StatusPong, Range.all(), 1) - // Play register( State.PLAY, Direction.CLIENTBOUND, ::Kick, mapOf( ProtocolVersion.v1_7_1..ProtocolVersion.v1_8 to ClientboundPackets1_8.DISCONNECT.id, @@ -75,10 +79,9 @@ object PacketRegistry { ProtocolVersion.v1_16_2..ProtocolVersion.v1_16_4 to ClientboundPackets1_16_2.DISCONNECT.id, ProtocolVersion.v1_17..ProtocolVersion.v1_17_1 to ClientboundPackets1_17.DISCONNECT.id, ProtocolVersion.v1_18..ProtocolVersion.v1_18_2 to ClientboundPackets1_18.DISCONNECT.id, - ProtocolVersion.v1_19.singleton to ClientboundPackets1_19.DISCONNECT.id + ProtocolVersion.v1_19..ProtocolVersion.v1_19_1 to ClientboundPackets1_19.DISCONNECT.id ) ) - register( State.PLAY, Direction.CLIENTBOUND, ::PluginMessage, mapOf( ProtocolVersion.v1_7_1..ProtocolVersion.v1_8 to ClientboundPackets1_8.PLUGIN_MESSAGE.id, @@ -90,10 +93,9 @@ object PacketRegistry { ProtocolVersion.v1_16_2..ProtocolVersion.v1_16_4 to ClientboundPackets1_16_2.PLUGIN_MESSAGE.id, ProtocolVersion.v1_17..ProtocolVersion.v1_17_1 to ClientboundPackets1_17.PLUGIN_MESSAGE.id, ProtocolVersion.v1_18..ProtocolVersion.v1_18_2 to ClientboundPackets1_18.PLUGIN_MESSAGE.id, - ProtocolVersion.v1_19.singleton to ClientboundPackets1_19.PLUGIN_MESSAGE.id + ProtocolVersion.v1_19..ProtocolVersion.v1_19_1 to ClientboundPackets1_19.PLUGIN_MESSAGE.id ) ) - register( State.PLAY, Direction.CLIENTBOUND, @@ -116,7 +118,25 @@ object PacketRegistry { idByProtocol: Map, Int>, klass: Class

= P::class.java, ) { - entries.add(RegistryEntry(idByProtocol, state, direction, constructor, klass)) + idByProtocol.forEach { (protocolRange, packetId) -> + entriesDecoding.computeIfAbsent(Triple(state, direction, packetId)) { TreeRangeMap.create() } + .also { rangeMap -> + if (rangeMap.subRangeMap(protocolRange).asMapOfRanges().isNotEmpty()) + throw IllegalStateException("entry already exists") + rangeMap.put(protocolRange, DecodingInfo(constructor)) + } + } + + val protocolRangeToId = TreeRangeMap.create() + idByProtocol.forEach { (range, id) -> protocolRangeToId.put(range, id) } + + entriesEncoding.computeIfAbsent(direction to klass) { TreeRangeMap.create() }.also { rangeMap -> + idByProtocol.forEach { (protocolRange, packetId) -> + if (rangeMap.subRangeMap(protocolRange).asMapOfRanges().isNotEmpty()) + throw IllegalStateException("entry already exists") + rangeMap.put(protocolRange, EncodingInfo(packetId)) + } + } } inline fun register( @@ -129,13 +149,8 @@ object PacketRegistry { register(constructor = constructor, direction = direction, state = state, idByProtocol = mapOf(protocol to id)) } - data class RegistryEntry( - val idByVersion: Map, Int>, - val state: State, - val direction: Direction, - val constructor: Supplier, - val packetClass: Class - ) + data class DecodingInfo(val constructor: Supplier) + data class EncodingInfo(val packetId: Int) fun getPacketConstructor( protocolVersion: Int, @@ -143,17 +158,11 @@ object PacketRegistry { id: Int, direction: Direction ): Supplier? { - return entries.firstOrNull { - it.direction == direction - && it.state == state - && it.idByVersion.entries.firstOrNull { it.key.contains(protocolVersion) }?.value == id - }?.constructor + return entriesDecoding.get(Triple(state, direction, id))?.get(protocolVersion)?.constructor } fun getPacketId(packetClass: Class, protocolVersion: Int, direction: Direction): Int? { - return entries.firstOrNull { - it.packetClass == packetClass && it.direction == direction - }?.idByVersion?.entries?.firstOrNull { it.key.contains(protocolVersion) }?.value + return entriesEncoding.get(direction to packetClass)?.get(protocolVersion)?.packetId } fun decode(byteBuf: ByteBuf, protocolVersion: Int, state: State, direction: Direction): Packet {