mirror of
https://github.com/ViaVersion/VIAaaS.git
synced 2025-01-24 22:01:49 +01:00
use ByteBufHolder for UnknownPacket
This commit is contained in:
parent
76fdf2a8a7
commit
7f8a91997c
@ -1,8 +1,8 @@
|
||||
package com.viaversion.aas.codec
|
||||
|
||||
import com.viaversion.aas.handler.MinecraftHandler
|
||||
import com.viaversion.aas.codec.packet.Packet
|
||||
import com.viaversion.aas.codec.packet.PacketRegistry
|
||||
import com.viaversion.aas.handler.MinecraftHandler
|
||||
import com.viaversion.aas.util.StacklessException
|
||||
import com.viaversion.viaversion.api.protocol.packet.Direction
|
||||
import com.viaversion.viaversion.exception.CancelEncoderException
|
||||
|
@ -23,6 +23,8 @@ import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPacke
|
||||
import com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8
|
||||
import com.viaversion.viaversion.protocols.protocol1_9to1_8.ClientboundPackets1_9
|
||||
import io.netty.buffer.ByteBuf
|
||||
import io.netty.buffer.ByteBufAllocator
|
||||
import io.netty.util.ReferenceCountUtil
|
||||
import java.util.function.Supplier
|
||||
|
||||
object PacketRegistry {
|
||||
@ -130,11 +132,14 @@ object PacketRegistry {
|
||||
|
||||
fun decode(byteBuf: ByteBuf, protocolVersion: Int, state: State, direction: Direction): Packet {
|
||||
val packetId = Type.VAR_INT.readPrimitive(byteBuf)
|
||||
val packet =
|
||||
getPacketConstructor(protocolVersion, state, packetId, direction)?.get() ?: UnknownPacket(packetId)
|
||||
packet.decode(byteBuf, protocolVersion)
|
||||
if (byteBuf.isReadable) throw StacklessException("Remaining bytes!")
|
||||
return packet
|
||||
val packet = getPacketConstructor(protocolVersion, state, packetId, direction)?.get()
|
||||
?: UnknownPacket(packetId, ByteBufAllocator.DEFAULT.buffer())
|
||||
try {
|
||||
packet.decode(byteBuf, protocolVersion)
|
||||
return ReferenceCountUtil.retain(packet)
|
||||
} finally {
|
||||
ReferenceCountUtil.release(packet)
|
||||
}
|
||||
}
|
||||
|
||||
fun encode(packet: Packet, byteBuf: ByteBuf, protocolVersion: Int, direction: Direction) {
|
||||
|
@ -1,16 +1,43 @@
|
||||
package com.viaversion.aas.codec.packet
|
||||
|
||||
import com.viaversion.aas.readRemainingBytes
|
||||
import io.netty.buffer.ByteBuf
|
||||
import io.netty.buffer.ByteBufHolder
|
||||
|
||||
class UnknownPacket(val id: Int) : Packet {
|
||||
lateinit var content: ByteArray
|
||||
|
||||
class UnknownPacket(val id: Int, val content: ByteBuf) : Packet, ByteBufHolder {
|
||||
override fun decode(byteBuf: ByteBuf, protocolVersion: Int) {
|
||||
content = readRemainingBytes(byteBuf)
|
||||
content.writeBytes(byteBuf)
|
||||
}
|
||||
|
||||
override fun encode(byteBuf: ByteBuf, protocolVersion: Int) {
|
||||
byteBuf.writeBytes(content)
|
||||
}
|
||||
|
||||
override fun retain(): UnknownPacket {
|
||||
content.retain()
|
||||
return this
|
||||
}
|
||||
|
||||
override fun retain(increment: Int): UnknownPacket {
|
||||
content.retain(increment)
|
||||
return this
|
||||
}
|
||||
|
||||
override fun touch(): UnknownPacket {
|
||||
content.touch()
|
||||
return this
|
||||
}
|
||||
|
||||
override fun touch(hint: Any): UnknownPacket {
|
||||
content.touch(hint)
|
||||
return this
|
||||
}
|
||||
|
||||
override fun refCnt() = content.refCnt()
|
||||
override fun release() = content.release()
|
||||
override fun release(decrement: Int) = content.release(decrement)
|
||||
override fun content(): ByteBuf = content
|
||||
override fun copy() = UnknownPacket(id, content.copy())
|
||||
override fun duplicate() = UnknownPacket(id, content.duplicate())
|
||||
override fun retainedDuplicate() = UnknownPacket(id, content.retainedDuplicate())
|
||||
override fun replace(content: ByteBuf) = UnknownPacket(id, content)
|
||||
}
|
@ -12,6 +12,7 @@ import com.viaversion.aas.util.StacklessException
|
||||
import com.viaversion.aas.writeFlushClose
|
||||
import com.viaversion.viaversion.api.protocol.packet.State
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import io.netty.util.ReferenceCountUtil
|
||||
|
||||
object PlayState : MinecraftConnectionState {
|
||||
override val state: State
|
||||
@ -24,7 +25,7 @@ object PlayState : MinecraftConnectionState {
|
||||
packet is UnknownPacket && (packet.id !in 0..127) -> throw StacklessException("Invalid packet id!")
|
||||
packet is PluginMessage && !handler.frontEnd -> modifyPluginMessage(handler, packet)
|
||||
}
|
||||
forward(handler, packet)
|
||||
forward(handler, ReferenceCountUtil.retain(packet))
|
||||
}
|
||||
|
||||
private fun modifyPluginMessage(handler: MinecraftHandler, pluginMessage: PluginMessage) {
|
||||
|
@ -13,7 +13,6 @@ import com.viaversion.aas.handler.forward
|
||||
import com.viaversion.aas.parseProtocol
|
||||
import com.viaversion.aas.util.StacklessException
|
||||
import com.viaversion.aas.writeFlushClose
|
||||
import com.viaversion.viaversion.api.Via
|
||||
import com.viaversion.viaversion.api.protocol.packet.State
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import java.util.*
|
||||
|
Loading…
Reference in New Issue
Block a user