mirror of
https://github.com/ViaVersion/ViaFabric.git
synced 2024-12-19 15:57:37 +01:00
Update codec handlers
This commit is contained in:
parent
14ccf796d5
commit
3051b32d07
@ -24,62 +24,7 @@
|
||||
|
||||
package com.github.creeper123123321.viafabric.handler;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import us.myles.ViaVersion.api.PacketWrapper;
|
||||
import us.myles.ViaVersion.api.data.UserConnection;
|
||||
import us.myles.ViaVersion.api.type.Type;
|
||||
import us.myles.ViaVersion.exception.CancelException;
|
||||
import us.myles.ViaVersion.packets.Direction;
|
||||
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
// TODO delete this when https://github.com/ViaVersion/ViaVersion/pull/1505 is merged
|
||||
public class CommonTransformer {
|
||||
public static final String HANDLER_DECODER_NAME = "via-decoder";
|
||||
public static final String HANDLER_ENCODER_NAME = "via-encoder";
|
||||
|
||||
public static boolean preServerboundCheck(UserConnection user) {
|
||||
// Ignore if pending disconnect
|
||||
if (user.isPendingDisconnect()) return true;
|
||||
// Increment received + Check PPS
|
||||
return user.incrementReceived() && user.handlePPS();
|
||||
}
|
||||
|
||||
public static void preClientbound(UserConnection user) {
|
||||
user.incrementSent();
|
||||
}
|
||||
|
||||
public static boolean willTransformPacket(UserConnection user) {
|
||||
return user.isActive();
|
||||
}
|
||||
|
||||
public static void transformClientbound(ByteBuf draft, UserConnection user, Function<Throwable, Exception> cancelSupplier) throws Exception {
|
||||
if (!draft.isReadable()) return;
|
||||
transform(draft, user, Direction.OUTGOING, cancelSupplier);
|
||||
}
|
||||
|
||||
public static void transformServerbound(ByteBuf draft, UserConnection user, Function<Throwable, Exception> cancelSupplier) throws Exception {
|
||||
if (!draft.isReadable()) return;
|
||||
transform(draft, user, Direction.INCOMING, cancelSupplier);
|
||||
}
|
||||
|
||||
private static void transform(ByteBuf draft, UserConnection user, Direction direction, Function<Throwable, Exception> cancelSupplier) throws Exception {
|
||||
int id = Type.VAR_INT.read(draft);
|
||||
if (id == PacketWrapper.PASSTHROUGH_ID) return;
|
||||
PacketWrapper wrapper = new PacketWrapper(id, draft, user);
|
||||
ProtocolInfo protInfo = user.get(ProtocolInfo.class);
|
||||
try {
|
||||
protInfo.getPipeline().transform(direction, protInfo.getState(), wrapper);
|
||||
} catch (CancelException ex) {
|
||||
throw cancelSupplier.apply(ex);
|
||||
}
|
||||
ByteBuf transformed = draft.alloc().buffer();
|
||||
try {
|
||||
wrapper.writeToBuffer(transformed);
|
||||
draft.clear().writeBytes(transformed);
|
||||
} finally {
|
||||
transformed.release();
|
||||
}
|
||||
}
|
||||
}
|
@ -24,43 +24,42 @@
|
||||
|
||||
package com.github.creeper123123321.viafabric.handler.clientside;
|
||||
|
||||
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToMessageDecoder;
|
||||
import us.myles.ViaVersion.api.Via;
|
||||
import us.myles.ViaVersion.api.data.UserConnection;
|
||||
import us.myles.ViaVersion.exception.CancelException;
|
||||
import us.myles.ViaVersion.util.PipelineUtil;
|
||||
import us.myles.ViaVersion.exception.CancelCodecException;
|
||||
import us.myles.ViaVersion.exception.CancelDecoderException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class VRDecodeHandler extends MessageToMessageDecoder<ByteBuf> {
|
||||
private final UserConnection user;
|
||||
private final UserConnection info;
|
||||
|
||||
public VRDecodeHandler(UserConnection user) {
|
||||
this.user = user;
|
||||
public VRDecodeHandler(UserConnection info) {
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
|
||||
CommonTransformer.preClientbound(user);
|
||||
if (!CommonTransformer.willTransformPacket(user)) {
|
||||
out.add(msg.retain());
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf bytebuf, List<Object> out) throws Exception {
|
||||
info.checkOutgoingPacket();
|
||||
if (!info.shouldTransformPacket()) {
|
||||
out.add(bytebuf.retain());
|
||||
return;
|
||||
}
|
||||
ByteBuf draft = msg.alloc().buffer().writeBytes(msg);
|
||||
|
||||
ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf);
|
||||
try {
|
||||
CommonTransformer.transformClientbound(draft, user, ignored -> CancelException.CACHED);
|
||||
out.add(draft.retain());
|
||||
info.transformOutgoing(transformedBuf, CancelDecoderException::generate);
|
||||
out.add(transformedBuf.retain());
|
||||
} finally {
|
||||
draft.release();
|
||||
transformedBuf.release();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
if (PipelineUtil.containsCause(cause, CancelException.class)) return;
|
||||
if (cause instanceof CancelCodecException) return;
|
||||
super.exceptionCaught(ctx, cause);
|
||||
}
|
||||
}
|
||||
|
@ -24,44 +24,42 @@
|
||||
|
||||
package com.github.creeper123123321.viafabric.handler.clientside;
|
||||
|
||||
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToMessageEncoder;
|
||||
import us.myles.ViaVersion.api.data.UserConnection;
|
||||
import us.myles.ViaVersion.exception.CancelException;
|
||||
import us.myles.ViaVersion.util.PipelineUtil;
|
||||
import us.myles.ViaVersion.exception.CancelCodecException;
|
||||
import us.myles.ViaVersion.exception.CancelEncoderException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class VREncodeHandler extends MessageToMessageEncoder<ByteBuf> {
|
||||
private final UserConnection user;
|
||||
private final UserConnection info;
|
||||
|
||||
public VREncodeHandler(UserConnection user) {
|
||||
this.user = user;
|
||||
public VREncodeHandler(UserConnection info) {
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
|
||||
if (CommonTransformer.preServerboundCheck(user)) {
|
||||
throw CancelException.CACHED; // M2ME expects at least one message
|
||||
}
|
||||
if (!CommonTransformer.willTransformPacket(user)) {
|
||||
out.add(msg.retain());
|
||||
protected void encode(ChannelHandlerContext ctx, ByteBuf bytebuf, List<Object> out) throws Exception {
|
||||
if (!info.checkIncomingPacket()) throw CancelEncoderException.generate(null);
|
||||
if (!info.shouldTransformPacket()) {
|
||||
out.add(bytebuf.retain());
|
||||
return;
|
||||
}
|
||||
ByteBuf draft = ctx.alloc().buffer().writeBytes(msg);
|
||||
|
||||
ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf);
|
||||
try {
|
||||
CommonTransformer.transformServerbound(draft, user, ignored -> CancelException.CACHED);
|
||||
out.add(draft.retain());
|
||||
info.transformIncoming(transformedBuf, CancelEncoderException::generate);
|
||||
out.add(transformedBuf.retain());
|
||||
} finally {
|
||||
draft.release();
|
||||
transformedBuf.release();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
if (PipelineUtil.containsCause(cause, CancelException.class)) return;
|
||||
if (cause instanceof CancelCodecException) return;
|
||||
super.exceptionCaught(ctx, cause);
|
||||
}
|
||||
}
|
||||
|
@ -24,46 +24,43 @@
|
||||
|
||||
package com.github.creeper123123321.viafabric.handler.serverside;
|
||||
|
||||
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToMessageDecoder;
|
||||
import us.myles.ViaVersion.api.Via;
|
||||
import us.myles.ViaVersion.api.data.UserConnection;
|
||||
import us.myles.ViaVersion.exception.CancelException;
|
||||
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
|
||||
import us.myles.ViaVersion.exception.CancelCodecException;
|
||||
import us.myles.ViaVersion.exception.CancelDecoderException;
|
||||
import us.myles.ViaVersion.util.PipelineUtil;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class FabricDecodeHandler extends MessageToMessageDecoder<ByteBuf> {
|
||||
private final UserConnection user;
|
||||
private final UserConnection info;
|
||||
|
||||
public FabricDecodeHandler(UserConnection user) {
|
||||
this.user = user;
|
||||
public FabricDecodeHandler(UserConnection info) {
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
|
||||
if (CommonTransformer.preServerboundCheck(user)) {
|
||||
throw CancelException.CACHED;
|
||||
}
|
||||
if (!CommonTransformer.willTransformPacket(user)) {
|
||||
out.add(msg.retain());
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf bytebuf, List<Object> out) throws Exception {
|
||||
if (!info.checkIncomingPacket()) throw CancelDecoderException.generate(null);
|
||||
if (!info.shouldTransformPacket()) {
|
||||
out.add(bytebuf.retain());
|
||||
return;
|
||||
}
|
||||
ByteBuf draft = ctx.alloc().buffer().writeBytes(msg);
|
||||
|
||||
ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf);
|
||||
try {
|
||||
CommonTransformer.transformServerbound(draft, user, ignored -> CancelException.CACHED);
|
||||
out.add(draft.retain());
|
||||
info.transformIncoming(transformedBuf, CancelDecoderException::generate);
|
||||
out.add(transformedBuf.retain());
|
||||
} finally {
|
||||
draft.release();
|
||||
transformedBuf.release();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
if (PipelineUtil.containsCause(cause, CancelException.class)) return;
|
||||
if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return;
|
||||
super.exceptionCaught(ctx, cause);
|
||||
}
|
||||
}
|
@ -24,42 +24,43 @@
|
||||
|
||||
package com.github.creeper123123321.viafabric.handler.serverside;
|
||||
|
||||
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToMessageEncoder;
|
||||
import us.myles.ViaVersion.api.data.UserConnection;
|
||||
import us.myles.ViaVersion.exception.CancelException;
|
||||
import us.myles.ViaVersion.exception.CancelCodecException;
|
||||
import us.myles.ViaVersion.exception.CancelEncoderException;
|
||||
import us.myles.ViaVersion.util.PipelineUtil;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class FabricEncodeHandler extends MessageToMessageEncoder<ByteBuf> {
|
||||
private final UserConnection user;
|
||||
private final UserConnection info;
|
||||
|
||||
public FabricEncodeHandler(UserConnection user) {
|
||||
this.user = user;
|
||||
public FabricEncodeHandler(UserConnection info) {
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void encode(final ChannelHandlerContext ctx, ByteBuf msg, final List<Object> out) throws Exception {
|
||||
CommonTransformer.preClientbound(user);
|
||||
if (!CommonTransformer.willTransformPacket(user)) {
|
||||
out.add(msg.readRetainedSlice(msg.readableBytes()));
|
||||
protected void encode(final ChannelHandlerContext ctx, ByteBuf bytebuf, final List<Object> out) throws Exception {
|
||||
info.checkOutgoingPacket();
|
||||
if (!info.shouldTransformPacket()) {
|
||||
out.add(bytebuf.retain());
|
||||
return;
|
||||
}
|
||||
ByteBuf draft = ctx.alloc().buffer().writeBytes(msg);
|
||||
|
||||
ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf);
|
||||
try {
|
||||
CommonTransformer.transformClientbound(draft, user, ignored -> CancelException.CACHED);
|
||||
out.add(draft.retain());
|
||||
info.transformOutgoing(transformedBuf, CancelEncoderException::generate);
|
||||
out.add(transformedBuf.retain());
|
||||
} finally {
|
||||
draft.release();
|
||||
transformedBuf.release();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
if (PipelineUtil.containsCause(cause, CancelException.class)) return;
|
||||
if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return;
|
||||
super.exceptionCaught(ctx, cause);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user