This commit is contained in:
creeper123123321 2021-01-03 16:36:53 -03:00
parent 7149b5d630
commit d90bb4640a
3 changed files with 25 additions and 18 deletions

View File

@ -26,6 +26,7 @@
package com.github.creeper123123321.viafabric.handler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import us.myles.ViaVersion.api.data.UserConnection;
@ -35,6 +36,7 @@ import us.myles.ViaVersion.util.PipelineUtil;
import java.util.List;
@ChannelHandler.Sharable
public class FabricDecodeHandler extends MessageToMessageDecoder<ByteBuf> {
private final UserConnection info;

View File

@ -26,6 +26,7 @@
package com.github.creeper123123321.viafabric.handler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import us.myles.ViaVersion.api.data.UserConnection;
@ -35,6 +36,7 @@ import us.myles.ViaVersion.util.PipelineUtil;
import java.util.List;
@ChannelHandler.Sharable
public class FabricEncodeHandler extends MessageToMessageEncoder<ByteBuf> {
private final UserConnection info;

View File

@ -26,17 +26,25 @@
package com.github.creeper123123321.viafabric.mixin.pipeline;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import com.github.creeper123123321.viafabric.handler.FabricDecodeHandler;
import com.github.creeper123123321.viafabric.handler.FabricEncodeHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelPipeline;
import net.minecraft.network.ClientConnection;
import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ClientConnection.class)
public class MixinClientConnection {
@Shadow private Channel channel;
@Redirect(
method = "exceptionCaught",
remap = false,
@ -52,25 +60,20 @@ public class MixinClientConnection {
}
}
@Redirect(method = "setCompressionThreshold", 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;"
@Inject(method = "setCompressionThreshold", at = @At(
value = "RETURN",
remap = false
))
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;
}
private void fixCompressionOrder(int compressionThreshold, CallbackInfo ci) {
if (channel.pipeline().get(FabricEncodeHandler.class) == null) return;
if (channel.pipeline().names().indexOf("compress")
< channel.pipeline().names().indexOf(CommonTransformer.HANDLER_ENCODER_NAME)) {
return; // Order is correct or compression is disabled
}
return instance.addBefore(base, newHandler, handler);
// Fixes the handler order
FabricDecodeHandler decode = channel.pipeline().remove(FabricDecodeHandler.class);
FabricEncodeHandler encode = channel.pipeline().remove(FabricEncodeHandler.class);
channel.pipeline().addAfter("decompress", "via-decoder", decode);
channel.pipeline().addAfter("compress", "via-encoder", encode);
}
}