optimize PacketRegistry, update deps

This commit is contained in:
creeper123123321 2022-06-29 08:34:51 -03:00
parent 475cc0d1f5
commit 1c9405199c
2 changed files with 35 additions and 26 deletions

View File

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

View File

@ -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<RegistryEntry>()
// state, direction, packet id, protocol version -> entry
val entriesDecoding = hashMapOf<Triple<State, Direction, Int>, RangeMap<Int, DecodingInfo>>()
// direction, type, protocol version -> entry
val entriesEncoding = hashMapOf<Pair<Direction, Class<out Packet>>, RangeMap<Int, EncodingInfo>>()
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<Range<Int>, Int>,
klass: Class<P> = 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<Int, Int>()
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 <reified P : Packet> 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<Range<Int>, Int>,
val state: State,
val direction: Direction,
val constructor: Supplier<out Packet>,
val packetClass: Class<out Packet>
)
data class DecodingInfo(val constructor: Supplier<out Packet>)
data class EncodingInfo(val packetId: Int)
fun getPacketConstructor(
protocolVersion: Int,
@ -143,17 +158,11 @@ object PacketRegistry {
id: Int,
direction: Direction
): Supplier<out Packet>? {
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<out Packet>, 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 {