Injector code cleanup, fix connection closing

This commit is contained in:
creeper123123321 2019-10-27 12:20:24 -03:00
parent ff4c2d108e
commit 8abe965958
12 changed files with 150 additions and 264 deletions

View File

@ -0,0 +1,70 @@
/*
* MIT License
*
* Copyright (c) 2018 creeper123123321 and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
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.packets.Direction;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
public class CommonTransformer {
public static final String HANDLER_DECODER_NAME = "viafabric_decoder_handler";
public static final String HANDLER_ENCODER_NAME = "viafabric_encoder_handler";
public static void transformClientbound(ByteBuf draft, UserConnection user) throws Exception {
if (!draft.isReadable()) return;
// Increment sent
user.incrementSent();
transform(draft, user, Direction.OUTGOING);
}
public static void transformServerbound(ByteBuf draft, UserConnection user) throws Exception {
if (!draft.isReadable()) return;
// Ignore if pending disconnect
if (user.isPendingDisconnect()) return;
// Increment received + Check PPS
if (user.incrementReceived() && user.handlePPS()) return;
transform(draft, user, Direction.INCOMING);
}
private static void transform(ByteBuf draft, UserConnection user, Direction direction) throws Exception {
if (!user.isActive()) return;
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);
protInfo.getPipeline().transform(direction, protInfo.getState(), wrapper);
ByteBuf transformed = draft.alloc().buffer();
try {
wrapper.writeToBuffer(transformed);
draft.clear().writeBytes(transformed);
} finally {
transformed.release();
}
}
}

View File

@ -24,15 +24,13 @@
package com.github.creeper123123321.viafabric.handler.clientside; package com.github.creeper123123321.viafabric.handler.clientside;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.ByteToMessageDecoder;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.packets.Direction;
import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.util.PipelineUtil; import us.myles.ViaVersion.util.PipelineUtil;
@ -40,7 +38,6 @@ import java.util.List;
public class VRDecodeHandler extends ByteToMessageDecoder { public class VRDecodeHandler extends ByteToMessageDecoder {
private UserConnection user; private UserConnection user;
public static final String NAME = "viafabric_decoder_handler";
public VRDecodeHandler(UserConnection user) { public VRDecodeHandler(UserConnection user) {
this.user = user; this.user = user;
@ -48,37 +45,12 @@ public class VRDecodeHandler extends ByteToMessageDecoder {
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
// Based on ViaVersion Sponge encoder code ByteBuf draft = msg.alloc().buffer().writeBytes(msg);
ByteBuf outBuf = msg.alloc().buffer().writeBytes(msg);
try { try {
// Increment sent CommonTransformer.transformClientbound(draft, user);
user.incrementSent(); out.add(draft.retain());
if (user.isActive()) {
// Handle ID
int id = Type.VAR_INT.read(outBuf);
if (id != PacketWrapper.PASSTHROUGH_ID) {
// Transform
PacketWrapper wrapper = new PacketWrapper(id, outBuf, user);
ProtocolInfo protInfo = user.get(ProtocolInfo.class);
protInfo.getPipeline().transform(Direction.OUTGOING, protInfo.getState(), wrapper);
ByteBuf newPacket = msg.alloc().buffer();
try {
wrapper.writeToBuffer(newPacket);
outBuf.clear();
outBuf.writeBytes(newPacket);
} finally {
newPacket.release();
}
}
}
// pass to minecraft encoder
out.add(outBuf.retain());
} finally { } finally {
outBuf.release(); draft.release();
} }
} }
@ -89,7 +61,8 @@ public class VRDecodeHandler extends ByteToMessageDecoder {
} }
@Override @Override
public void channelInactive(ChannelHandlerContext ctx) { public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
ProtocolInfo info = user.get(ProtocolInfo.class); ProtocolInfo info = user.get(ProtocolInfo.class);
if (info.getUuid() != null) { if (info.getUuid() != null) {
Via.getManager().removePortedClient(info.getUuid()); Via.getManager().removePortedClient(info.getUuid());

View File

@ -24,21 +24,17 @@
package com.github.creeper123123321.viafabric.handler.clientside; package com.github.creeper123123321.viafabric.handler.clientside;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.EncoderException; import io.netty.handler.codec.EncoderException;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToByteEncoder;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.packets.Direction;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.util.PipelineUtil; import us.myles.ViaVersion.util.PipelineUtil;
public class VREncodeHandler extends MessageToByteEncoder { public class VREncodeHandler extends MessageToByteEncoder {
private UserConnection user; private UserConnection user;
public static String NAME = "viafabric_encoder_handler";
public VREncodeHandler(UserConnection user) { public VREncodeHandler(UserConnection user) {
this.user = user; this.user = user;
@ -49,44 +45,12 @@ public class VREncodeHandler extends MessageToByteEncoder {
// Based on Sponge ViaVersion decoder code // Based on Sponge ViaVersion decoder code
if (!(msg instanceof ByteBuf)) throw new EncoderException("Received msg isn't ByteBuf"); if (!(msg instanceof ByteBuf)) throw new EncoderException("Received msg isn't ByteBuf");
ByteBuf outBuffer = ctx.alloc().buffer().writeBytes((ByteBuf) msg); ByteBuf draft = ctx.alloc().buffer().writeBytes((ByteBuf) msg);
try { try {
// use transformers CommonTransformer.transformServerbound(draft, user);
if (outBuffer.readableBytes() > 0) { out.writeBytes(draft);
// Ignore if pending disconnect
if (user.isPendingDisconnect()) {
return;
}
// Increment received
boolean second = user.incrementReceived();
// Check PPS
if (second && user.handlePPS())
return;
if (user.isActive()) {
// Handle ID
int id = Type.VAR_INT.read(outBuffer);
// Transform
if (id != PacketWrapper.PASSTHROUGH_ID) {
PacketWrapper wrapper = new PacketWrapper(id, outBuffer, user);
ProtocolInfo protInfo = user.get(ProtocolInfo.class);
protInfo.getPipeline().transform(Direction.INCOMING, protInfo.getState(), wrapper);
ByteBuf newPacket = outBuffer.alloc().buffer();
try {
wrapper.writeToBuffer(newPacket);
outBuffer.clear();
outBuffer.writeBytes(newPacket);
} finally {
newPacket.release();
}
}
}
out.writeBytes(outBuffer);
}
} finally { } finally {
outBuffer.release(); draft.release();
} }
} }

View File

@ -24,85 +24,33 @@
package com.github.creeper123123321.viafabric.handler.serverside; package com.github.creeper123123321.viafabric.handler.serverside;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.ByteToMessageDecoder;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.packets.Direction;
import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.util.PipelineUtil; import us.myles.ViaVersion.util.PipelineUtil;
import java.lang.reflect.InvocationTargetException;
import java.util.List; import java.util.List;
public class FabricDecodeHandler extends ByteToMessageDecoder { public class FabricDecodeHandler extends ByteToMessageDecoder {
// https://github.com/MylesIsCool/ViaVersion/blob/1abc3ebea66878192b08b577353dff7d619ed51e/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/SpongeDecodeHandler.java private final UserConnection user;
private final ByteToMessageDecoder minecraftDecoder;
private final UserConnection info;
public FabricDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) { public FabricDecodeHandler(UserConnection user) {
this.info = info; this.user = user;
this.minecraftDecoder = minecraftDecoder;
} }
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf bytebuf, List<Object> list) throws Exception { protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
// use transformers ByteBuf draft = ctx.alloc().buffer().writeBytes(in);
if (bytebuf.readableBytes() > 0) { try {
// Ignore if pending disconnect CommonTransformer.transformServerbound(draft, user);
if (info.isPendingDisconnect()) { out.add(draft.retain());
return; } finally {
} draft.release();
// Increment received
boolean second = info.incrementReceived();
// Check PPS
if (second) {
if (info.handlePPS())
return;
}
if (info.isActive()) {
// Handle ID
int id = Type.VAR_INT.read(bytebuf);
// Transform
ByteBuf newPacket = ctx.alloc().buffer();
try {
if (id == PacketWrapper.PASSTHROUGH_ID) {
newPacket.writeBytes(bytebuf);
} else {
PacketWrapper wrapper = new PacketWrapper(id, bytebuf, info);
ProtocolInfo protInfo = info.get(ProtocolInfo.class);
protInfo.getPipeline().transform(Direction.INCOMING, protInfo.getState(), wrapper);
wrapper.writeToBuffer(newPacket);
}
bytebuf.clear();
bytebuf = newPacket;
} catch (Exception e) {
// Clear Buffer
bytebuf.clear();
// Release Packet, be free!
newPacket.release();
throw e;
}
}
// call minecraft decoder
try {
list.addAll(PipelineUtil.callDecode(this.minecraftDecoder, ctx, bytebuf));
} catch (InvocationTargetException e) {
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause();
}
} finally {
if (info.isActive()) {
bytebuf.release();
}
}
} }
} }
@ -113,8 +61,9 @@ public class FabricDecodeHandler extends ByteToMessageDecoder {
} }
@Override @Override
public void channelInactive(ChannelHandlerContext ctx) { public void channelInactive(ChannelHandlerContext ctx) throws Exception {
ProtocolInfo pi = info.get(ProtocolInfo.class); super.channelInactive(ctx); // May call decode
ProtocolInfo pi = user.get(ProtocolInfo.class);
if (pi.getUuid() != null) { if (pi.getUuid() != null) {
Via.getManager().removePortedClient(pi.getUuid()); Via.getManager().removePortedClient(pi.getUuid());
} }

View File

@ -24,66 +24,32 @@
package com.github.creeper123123321.viafabric.handler.serverside; package com.github.creeper123123321.viafabric.handler.serverside;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.EncoderException;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToByteEncoder;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.packets.Direction;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.util.PipelineUtil; import us.myles.ViaVersion.util.PipelineUtil;
import java.lang.reflect.InvocationTargetException;
public class FabricEncodeHandler extends MessageToByteEncoder { public class FabricEncodeHandler extends MessageToByteEncoder {
// https://github.com/MylesIsCool/ViaVersion/blob/master/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/SpongeEncodeHandler.java private final UserConnection user;
private final UserConnection info;
private final MessageToByteEncoder minecraftEncoder;
public FabricEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) { public FabricEncodeHandler(UserConnection user) {
this.info = info; this.user = user;
this.minecraftEncoder = minecraftEncoder;
} }
@Override @Override
protected void encode(final ChannelHandlerContext ctx, Object o, final ByteBuf bytebuf) throws Exception { protected void encode(final ChannelHandlerContext ctx, Object in, final ByteBuf out) throws Exception {
// handle the packet type if (!(in instanceof ByteBuf)) throw new EncoderException("Received object isn't ByteBuf");
if (!(o instanceof ByteBuf)) {
// call minecraft encoder
try {
PipelineUtil.callEncode(this.minecraftEncoder, ctx, o, bytebuf);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause();
}
}
}
if (bytebuf.readableBytes() == 0) {
throw new CancelException();
}
// Increment sent
info.incrementSent();
if (info.isActive()) {
// Handle ID
int id = Type.VAR_INT.read(bytebuf);
// Transform
ByteBuf oldPacket = bytebuf.copy();
bytebuf.clear();
try { ByteBuf draft = ctx.alloc().buffer().writeBytes((ByteBuf) in);
PacketWrapper wrapper = new PacketWrapper(id, oldPacket, info); try {
ProtocolInfo protInfo = info.get(ProtocolInfo.class); CommonTransformer.transformClientbound(draft, user);
protInfo.getPipeline().transform(Direction.OUTGOING, protInfo.getState(), wrapper); out.writeBytes(draft);
wrapper.writeToBuffer(bytebuf); } finally {
} catch (Exception e) { draft.release();
bytebuf.clear();
throw e;
} finally {
oldPacket.release();
}
} }
} }

View File

@ -24,6 +24,9 @@
package com.github.creeper123123321.viafabric.mixin; package com.github.creeper123123321.viafabric.mixin;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelPipeline;
import net.minecraft.network.ClientConnection; import net.minecraft.network.ClientConnection;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
@ -40,11 +43,33 @@ public class MixinClientConnection {
value = "INVOKE", value = "INVOKE",
target = "Lorg/apache/logging/log4j/Logger;debug(Ljava/lang/String;Ljava/lang/Throwable;)V" target = "Lorg/apache/logging/log4j/Logger;debug(Ljava/lang/String;Ljava/lang/Throwable;)V"
)) ))
public void redirectDebug(Logger logger, String message, Throwable t) { private void redirectDebug(Logger logger, String message, Throwable t) {
if ("Failed to sent packet".equals(message)) { if ("Failed to sent packet".equals(message)) {
logger.info(message, t); logger.info(message, t);
} else { } else {
logger.debug(message, t); logger.debug(message, t);
} }
} }
@Redirect(method = "setMinCompressedSize", at = @At(
value = "INVOKE",
remap = false,
target = "Lio/netty/channel/ChannelPipeline;addBefore(Ljava/lang/String;Ljava/lang/String;Lio/netty/channel/ChannelHandler;)Lio/netty/channel/ChannelPipeline;"
))
private ChannelPipeline decodeEncodePlacement(ChannelPipeline instance, String base, String newHandler, ChannelHandler handler) {
// Fixes the handler order
switch (base) {
case "decoder": {
if (instance.get(CommonTransformer.HANDLER_DECODER_NAME) != null)
base = CommonTransformer.HANDLER_DECODER_NAME;
break;
}
case "encoder": {
if (instance.get(CommonTransformer.HANDLER_ENCODER_NAME) != null)
base = CommonTransformer.HANDLER_ENCODER_NAME;
break;
}
}
return instance.addBefore(base, newHandler, handler);
}
} }

View File

@ -24,12 +24,11 @@
package com.github.creeper123123321.viafabric.mixin; package com.github.creeper123123321.viafabric.mixin;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import com.github.creeper123123321.viafabric.handler.serverside.FabricDecodeHandler; import com.github.creeper123123321.viafabric.handler.serverside.FabricDecodeHandler;
import com.github.creeper123123321.viafabric.handler.serverside.FabricEncodeHandler; import com.github.creeper123123321.viafabric.handler.serverside.FabricEncodeHandler;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToByteEncoder;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
@ -45,11 +44,8 @@ public class MixinServerNetworkIoChInit {
UserConnection user = new UserConnection(channel); UserConnection user = new UserConnection(channel);
new ProtocolPipeline(user); new ProtocolPipeline(user);
MessageToByteEncoder oldEncoder = (MessageToByteEncoder) channel.pipeline().get("encoder"); channel.pipeline().addBefore("encoder", CommonTransformer.HANDLER_ENCODER_NAME, new FabricEncodeHandler(user));
ByteToMessageDecoder oldDecoder = (ByteToMessageDecoder) channel.pipeline().get("decoder"); channel.pipeline().addBefore("decoder", CommonTransformer.HANDLER_DECODER_NAME, new FabricDecodeHandler(user));
channel.pipeline().replace("encoder", "encoder", new FabricEncodeHandler(user, oldEncoder));
channel.pipeline().replace("decoder", "decoder", new FabricDecodeHandler(user, oldDecoder));
} }
} }
} }

View File

@ -1,57 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2018 creeper123123321 and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.creeper123123321.viafabric.mixin.client;
import com.github.creeper123123321.viafabric.handler.clientside.VRDecodeHandler;
import com.github.creeper123123321.viafabric.handler.clientside.VREncodeHandler;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelPipeline;
import net.minecraft.network.ClientConnection;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(ClientConnection.class)
public class MixinClientConnection {
@Redirect(method = "setMinCompressedSize", at = @At(
value = "INVOKE",
remap = false,
target = "Lio/netty/channel/ChannelPipeline;addBefore(Ljava/lang/String;Ljava/lang/String;Lio/netty/channel/ChannelHandler;)Lio/netty/channel/ChannelPipeline;"
))
private ChannelPipeline decodeEncodePlacement(ChannelPipeline instance, String base, String newHandler, ChannelHandler handler) {
// Fixes the handler order
switch (base) {
case "decoder": {
if (instance.get(VRDecodeHandler.class) != null) base = VRDecodeHandler.NAME;
break;
}
case "encoder": {
if (instance.get(VREncodeHandler.class) != null) base = VREncodeHandler.NAME;
break;
}
}
return instance.addBefore(base, newHandler, handler);
}
}

View File

@ -24,6 +24,7 @@
package com.github.creeper123123321.viafabric.mixin.client; package com.github.creeper123123321.viafabric.mixin.client;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import com.github.creeper123123321.viafabric.handler.clientside.VRDecodeHandler; import com.github.creeper123123321.viafabric.handler.clientside.VRDecodeHandler;
import com.github.creeper123123321.viafabric.handler.clientside.VREncodeHandler; import com.github.creeper123123321.viafabric.handler.clientside.VREncodeHandler;
import com.github.creeper123123321.viafabric.platform.VRClientSideUserConnection; import com.github.creeper123123321.viafabric.platform.VRClientSideUserConnection;
@ -44,8 +45,8 @@ public class MixinClientConnectionChInit {
UserConnection user = new VRClientSideUserConnection(channel); UserConnection user = new VRClientSideUserConnection(channel);
new ProtocolPipeline(user); new ProtocolPipeline(user);
channel.pipeline().addBefore("encoder", VREncodeHandler.NAME, new VREncodeHandler(user)); channel.pipeline().addBefore("encoder", CommonTransformer.HANDLER_ENCODER_NAME, new VREncodeHandler(user));
channel.pipeline().addBefore("decoder", VRDecodeHandler.NAME, new VRDecodeHandler(user)); channel.pipeline().addBefore("decoder", CommonTransformer.HANDLER_DECODER_NAME, new VRDecodeHandler(user));
} }
} }
} }

View File

@ -24,8 +24,7 @@
package com.github.creeper123123321.viafabric.platform; package com.github.creeper123123321.viafabric.platform;
import com.github.creeper123123321.viafabric.handler.clientside.VRDecodeHandler; import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import com.github.creeper123123321.viafabric.handler.clientside.VREncodeHandler;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
@ -53,10 +52,10 @@ public class VRClientSideUserConnection extends UserConnection {
packet.release(); packet.release();
final Channel channel = this.getChannel(); final Channel channel = this.getChannel();
if (currentThread) { if (currentThread) {
PipelineUtil.getPreviousContext(VRDecodeHandler.NAME, channel.pipeline()).fireChannelRead(copy); PipelineUtil.getPreviousContext(CommonTransformer.HANDLER_DECODER_NAME, channel.pipeline()).fireChannelRead(copy);
} else { } else {
channel.eventLoop().submit(() -> { channel.eventLoop().submit(() -> {
PipelineUtil.getPreviousContext(VRDecodeHandler.NAME, channel.pipeline()).fireChannelRead(copy); PipelineUtil.getPreviousContext(CommonTransformer.HANDLER_DECODER_NAME, channel.pipeline()).fireChannelRead(copy);
}); });
} }
} }
@ -72,17 +71,17 @@ public class VRClientSideUserConnection extends UserConnection {
copy.writeBytes(packet); copy.writeBytes(packet);
packet.release(); packet.release();
final Channel channel = this.getChannel(); final Channel channel = this.getChannel();
PipelineUtil.getPreviousContext(VRDecodeHandler.NAME, channel.pipeline()).fireChannelRead(copy); PipelineUtil.getPreviousContext(CommonTransformer.HANDLER_DECODER_NAME, channel.pipeline()).fireChannelRead(copy);
return channel.newSucceededFuture(); return channel.newSucceededFuture();
} }
@Override @Override
public void sendRawPacketToServer(ByteBuf packet, boolean currentThread) { public void sendRawPacketToServer(ByteBuf packet, boolean currentThread) {
if (currentThread) { if (currentThread) {
getChannel().pipeline().context(VREncodeHandler.NAME).writeAndFlush(packet); getChannel().pipeline().context(CommonTransformer.HANDLER_ENCODER_NAME).writeAndFlush(packet);
} else { } else {
getChannel().eventLoop().submit(() -> { getChannel().eventLoop().submit(() -> {
getChannel().pipeline().context(VREncodeHandler.NAME).writeAndFlush(packet); getChannel().pipeline().context(CommonTransformer.HANDLER_ENCODER_NAME).writeAndFlush(packet);
}); });
} }
} }

View File

@ -24,6 +24,7 @@
package com.github.creeper123123321.viafabric.platform; package com.github.creeper123123321.viafabric.platform;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.SharedConstants; import net.minecraft.SharedConstants;
@ -52,12 +53,12 @@ public class VRInjector implements ViaInjector {
@Override @Override
public String getEncoderName() { public String getEncoderName() {
return "encoder"; return CommonTransformer.HANDLER_ENCODER_NAME;
} }
@Override @Override
public String getDecoderName() { public String getDecoderName() {
return "decoder"; return CommonTransformer.HANDLER_DECODER_NAME;
} }
@Override @Override

View File

@ -7,7 +7,6 @@
"MixinServerNetworkIoChInit" "MixinServerNetworkIoChInit"
], ],
"client": [ "client": [
"client.MixinClientConnection",
"client.MixinClientConnectionChInit", "client.MixinClientConnectionChInit",
"client.MixinMultiplayerScreen" "client.MixinMultiplayerScreen"
], ],