mirror of https://github.com/ViaVersion/VIAaaS.git
43 lines
1.7 KiB
Kotlin
43 lines
1.7 KiB
Kotlin
package com.viaversion.aas.handler
|
|
|
|
import com.viaversion.viaversion.api.connection.UserConnection
|
|
import com.viaversion.viaversion.exception.CancelDecoderException
|
|
import com.viaversion.viaversion.exception.CancelEncoderException
|
|
import io.netty.buffer.ByteBuf
|
|
import io.netty.channel.ChannelHandlerContext
|
|
import io.netty.handler.codec.MessageToMessageCodec
|
|
|
|
class ViaCodec(val info: UserConnection) : MessageToMessageCodec<ByteBuf, ByteBuf>() {
|
|
override fun decode(ctx: ChannelHandlerContext, bytebuf: ByteBuf, out: MutableList<Any>) {
|
|
if (!ctx.channel().isActive) return
|
|
if (!info.checkClientboundPacket()) throw CancelDecoderException.generate(null)
|
|
if (!info.shouldTransformPacket()) {
|
|
out.add(bytebuf.retain())
|
|
return
|
|
}
|
|
val transformedBuf: ByteBuf = ctx.alloc().buffer().writeBytes(bytebuf)
|
|
try {
|
|
info.transformClientbound(transformedBuf, CancelDecoderException::generate)
|
|
out.add(transformedBuf.retain())
|
|
} finally {
|
|
transformedBuf.release()
|
|
}
|
|
}
|
|
|
|
override fun encode(ctx: ChannelHandlerContext, bytebuf: ByteBuf, out: MutableList<Any>) {
|
|
if (!ctx.channel().isActive) throw CancelEncoderException.CACHED
|
|
if (!info.checkServerboundPacket()) throw CancelEncoderException.generate(null)
|
|
if (!info.shouldTransformPacket()) {
|
|
out.add(bytebuf.retain())
|
|
return
|
|
}
|
|
val transformedBuf: ByteBuf = ctx.alloc().buffer().writeBytes(bytebuf)
|
|
try {
|
|
info.transformServerbound(transformedBuf, CancelEncoderException::generate)
|
|
out.add(transformedBuf.retain())
|
|
} finally {
|
|
transformedBuf.release()
|
|
}
|
|
}
|
|
}
|