diff --git a/gradle.properties b/gradle.properties index 3663089..3d875a2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ # rip my ram -org.gradle.jvmargs=-Xmx512M \ No newline at end of file +org.gradle.jvmargs=-Xms32M -Xmx512M diff --git a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/handler/clientside/ProtocolDetectionHandler.java b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/handler/clientside/ProtocolDetectionHandler.java deleted file mode 100644 index d7d3d3c..0000000 --- a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/handler/clientside/ProtocolDetectionHandler.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.viaversion.fabric.mc114.handler.clientside; - -import com.viaversion.fabric.mc114.ViaFabric; -import com.viaversion.fabric.mc114.service.ProtocolAutoDetector; -import com.viaversion.viaversion.util.Pair; -import io.netty.channel.ChannelDuplexHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelPromise; - -import java.net.InetSocketAddress; -import java.util.ArrayDeque; -import java.util.Queue; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; - -public class ProtocolDetectionHandler extends ChannelDuplexHandler { - private final Queue> queuedMessages = new ArrayDeque<>(); - private boolean hold = true; - private boolean pendentFlush; - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - super.channelActive(ctx); - if (ctx.channel().remoteAddress() instanceof InetSocketAddress) { - ScheduledFuture timeoutRun = ctx.executor().schedule(() -> { - ViaFabric.JLOGGER.warning("Timeout for protocol auto-detection in " - + ctx.channel().remoteAddress() + " server"); - hold = false; - drainQueue(ctx); - ctx.pipeline().remove(this); - }, 10, TimeUnit.SECONDS); - ProtocolAutoDetector.detectVersion(((InetSocketAddress) ctx.channel().remoteAddress())) - .whenComplete((obj, ex) -> { - ctx.pipeline().remove(this); - timeoutRun.cancel(false); - }); - // Let's cache it before we need it - } - } - - @Override - public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { - if (!hold) { - drainQueue(ctx); - super.write(ctx, msg, promise); - } else { - queuedMessages.add(new Pair<>(msg, promise)); - } - } - - @Override - public void flush(ChannelHandlerContext ctx) throws Exception { - if (!hold) { - drainQueue(ctx); - super.flush(ctx); - } else { - pendentFlush = true; - } - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - drainQueue(ctx); - super.channelInactive(ctx); - } - - private void drainQueue(ChannelHandlerContext ctx) { - queuedMessages.forEach(it -> ctx.write(it.getKey(), it.getValue())); - queuedMessages.clear(); - if (pendentFlush) ctx.flush(); - pendentFlush = false; - } - - @Override - public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { - drainQueue(ctx); - super.handlerRemoved(ctx); - } -} diff --git a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/client/MixinClientConnection.java b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/client/MixinClientConnection.java new file mode 100644 index 0000000..4795eb1 --- /dev/null +++ b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/client/MixinClientConnection.java @@ -0,0 +1,27 @@ +package com.viaversion.fabric.mc114.mixin.pipeline.client; + +import com.viaversion.fabric.mc114.ViaFabric; +import com.viaversion.fabric.mc114.service.ProtocolAutoDetector; +import net.minecraft.network.ClientConnection; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; + +@Mixin(ClientConnection.class) +public class MixinClientConnection { + @Inject(method = "connect", at = @At("HEAD")) + private static void onConnect(InetAddress address, int port, boolean shouldUseNativeTransport, CallbackInfoReturnable cir) { + try { + if (!ViaFabric.config.isClientSideEnabled()) return; + ProtocolAutoDetector.detectVersion(new InetSocketAddress(address, port)).get(10, TimeUnit.SECONDS); + } catch (Exception e) { + ViaFabric.JLOGGER.log(Level.WARNING, "Could not auto-detect protocol for " + address + " " + e); + } + } +} diff --git a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/client/MixinClientConnectionChInit.java b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/client/MixinClientConnectionChInit.java index 9b8aa27..a5cb569 100644 --- a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/client/MixinClientConnectionChInit.java +++ b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/mixin/pipeline/client/MixinClientConnectionChInit.java @@ -2,9 +2,7 @@ package com.viaversion.fabric.mc114.mixin.pipeline.client; import com.viaversion.fabric.common.handler.FabricDecodeHandler; import com.viaversion.fabric.common.handler.FabricEncodeHandler; -import com.viaversion.fabric.mc114.ViaFabric; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc114.handler.clientside.ProtocolDetectionHandler; import com.viaversion.fabric.common.protocol.HostnameParserProtocol; import com.viaversion.viaversion.connection.UserConnectionImpl; import com.viaversion.viaversion.protocol.ProtocolPipelineImpl; @@ -27,9 +25,6 @@ public class MixinClientConnectionChInit { channel.pipeline() .addBefore("encoder", CommonTransformer.HANDLER_ENCODER_NAME, new FabricEncodeHandler(user)) .addBefore("decoder", CommonTransformer.HANDLER_DECODER_NAME, new FabricDecodeHandler(user)); - if (ViaFabric.config.isClientSideEnabled()) { - channel.pipeline().addAfter(CommonTransformer.HANDLER_ENCODER_NAME, "via-autoprotocol", new ProtocolDetectionHandler()); - } } } } diff --git a/viafabric-mc114/src/main/resources/mixins.viafabric114.pipeline.json b/viafabric-mc114/src/main/resources/mixins.viafabric114.pipeline.json index 26e4eda..7580fda 100644 --- a/viafabric-mc114/src/main/resources/mixins.viafabric114.pipeline.json +++ b/viafabric-mc114/src/main/resources/mixins.viafabric114.pipeline.json @@ -7,6 +7,7 @@ "MixinServerNetworkIoChInit" ], "client": [ + "client.MixinClientConnection", "client.MixinClientConnectionChInit" ], "injectors": { diff --git a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/handler/clientside/ProtocolDetectionHandler.java b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/handler/clientside/ProtocolDetectionHandler.java deleted file mode 100644 index a04a8ff..0000000 --- a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/handler/clientside/ProtocolDetectionHandler.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.viaversion.fabric.mc115.handler.clientside; - -import com.viaversion.fabric.mc115.ViaFabric; -import com.viaversion.fabric.mc115.service.ProtocolAutoDetector; -import com.viaversion.viaversion.util.Pair; -import io.netty.channel.ChannelDuplexHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelPromise; - -import java.net.InetSocketAddress; -import java.util.ArrayDeque; -import java.util.Queue; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; - -public class ProtocolDetectionHandler extends ChannelDuplexHandler { - private final Queue> queuedMessages = new ArrayDeque<>(); - private boolean hold = true; - private boolean pendentFlush; - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - super.channelActive(ctx); - if (ctx.channel().remoteAddress() instanceof InetSocketAddress) { - ScheduledFuture timeoutRun = ctx.executor().schedule(() -> { - ViaFabric.JLOGGER.warning("Timeout for protocol auto-detection in " - + ctx.channel().remoteAddress() + " server"); - hold = false; - drainQueue(ctx); - ctx.pipeline().remove(this); - }, 10, TimeUnit.SECONDS); - ProtocolAutoDetector.detectVersion(((InetSocketAddress) ctx.channel().remoteAddress())) - .whenComplete((obj, ex) -> { - ctx.pipeline().remove(this); - timeoutRun.cancel(false); - }); - // Let's cache it before we need it - } - } - - @Override - public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { - if (!hold) { - drainQueue(ctx); - super.write(ctx, msg, promise); - } else { - queuedMessages.add(new Pair<>(msg, promise)); - } - } - - @Override - public void flush(ChannelHandlerContext ctx) throws Exception { - if (!hold) { - drainQueue(ctx); - super.flush(ctx); - } else { - pendentFlush = true; - } - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - drainQueue(ctx); - super.channelInactive(ctx); - } - - private void drainQueue(ChannelHandlerContext ctx) { - queuedMessages.forEach(it -> ctx.write(it.getKey(), it.getValue())); - queuedMessages.clear(); - if (pendentFlush) ctx.flush(); - pendentFlush = false; - } - - @Override - public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { - drainQueue(ctx); - super.handlerRemoved(ctx); - } -} diff --git a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/client/MixinClientConnection.java b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/client/MixinClientConnection.java new file mode 100644 index 0000000..6c37a8a --- /dev/null +++ b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/client/MixinClientConnection.java @@ -0,0 +1,27 @@ +package com.viaversion.fabric.mc115.mixin.pipeline.client; + +import com.viaversion.fabric.mc115.ViaFabric; +import com.viaversion.fabric.mc115.service.ProtocolAutoDetector; +import net.minecraft.network.ClientConnection; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; + +@Mixin(ClientConnection.class) +public class MixinClientConnection { + @Inject(method = "connect", at = @At("HEAD")) + private static void onConnect(InetAddress address, int port, boolean shouldUseNativeTransport, CallbackInfoReturnable cir) { + try { + if (!ViaFabric.config.isClientSideEnabled()) return; + ProtocolAutoDetector.detectVersion(new InetSocketAddress(address, port)).get(10, TimeUnit.SECONDS); + } catch (Exception e) { + ViaFabric.JLOGGER.log(Level.WARNING, "Could not auto-detect protocol for " + address + " " + e); + } + } +} diff --git a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/client/MixinClientConnectionChInit.java b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/client/MixinClientConnectionChInit.java index 58356df..c0172f2 100644 --- a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/client/MixinClientConnectionChInit.java +++ b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/mixin/pipeline/client/MixinClientConnectionChInit.java @@ -2,9 +2,7 @@ package com.viaversion.fabric.mc115.mixin.pipeline.client; import com.viaversion.fabric.common.handler.FabricDecodeHandler; import com.viaversion.fabric.common.handler.FabricEncodeHandler; -import com.viaversion.fabric.mc115.ViaFabric; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc115.handler.clientside.ProtocolDetectionHandler; import com.viaversion.fabric.common.protocol.HostnameParserProtocol; import com.viaversion.viaversion.connection.UserConnectionImpl; import com.viaversion.viaversion.protocol.ProtocolPipelineImpl; @@ -27,9 +25,6 @@ public class MixinClientConnectionChInit { channel.pipeline() .addBefore("encoder", CommonTransformer.HANDLER_ENCODER_NAME, new FabricEncodeHandler(user)) .addBefore("decoder", CommonTransformer.HANDLER_DECODER_NAME, new FabricDecodeHandler(user)); - if (ViaFabric.config.isClientSideEnabled()) { - channel.pipeline().addAfter(CommonTransformer.HANDLER_ENCODER_NAME, "via-autoprotocol", new ProtocolDetectionHandler()); - } } } } diff --git a/viafabric-mc115/src/main/resources/mixins.viafabric115.pipeline.json b/viafabric-mc115/src/main/resources/mixins.viafabric115.pipeline.json index 9fa6aa7..a949147 100644 --- a/viafabric-mc115/src/main/resources/mixins.viafabric115.pipeline.json +++ b/viafabric-mc115/src/main/resources/mixins.viafabric115.pipeline.json @@ -7,6 +7,7 @@ "MixinServerNetworkIoChInit" ], "client": [ + "client.MixinClientConnection", "client.MixinClientConnectionChInit" ], "injectors": { diff --git a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/handler/clientside/ProtocolDetectionHandler.java b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/handler/clientside/ProtocolDetectionHandler.java deleted file mode 100644 index a875d03..0000000 --- a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/handler/clientside/ProtocolDetectionHandler.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.viaversion.fabric.mc116.handler.clientside; - -import com.viaversion.fabric.mc116.ViaFabric; -import com.viaversion.fabric.mc116.service.ProtocolAutoDetector; -import com.viaversion.viaversion.util.Pair; -import io.netty.channel.ChannelDuplexHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelPromise; - -import java.net.InetSocketAddress; -import java.util.ArrayDeque; -import java.util.Queue; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; - -public class ProtocolDetectionHandler extends ChannelDuplexHandler { - private final Queue> queuedMessages = new ArrayDeque<>(); - private boolean hold = true; - private boolean pendentFlush; - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - super.channelActive(ctx); - if (ctx.channel().remoteAddress() instanceof InetSocketAddress) { - ScheduledFuture timeoutRun = ctx.executor().schedule(() -> { - ViaFabric.JLOGGER.warning("Timeout for protocol auto-detection in " - + ctx.channel().remoteAddress() + " server"); - hold = false; - drainQueue(ctx); - ctx.pipeline().remove(this); - }, 10, TimeUnit.SECONDS); - ProtocolAutoDetector.detectVersion(((InetSocketAddress) ctx.channel().remoteAddress())) - .whenComplete((obj, ex) -> { - ctx.pipeline().remove(this); - timeoutRun.cancel(false); - }); - // Let's cache it before we need it - } - } - - @Override - public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { - if (!hold) { - drainQueue(ctx); - super.write(ctx, msg, promise); - } else { - queuedMessages.add(new Pair<>(msg, promise)); - } - } - - @Override - public void flush(ChannelHandlerContext ctx) throws Exception { - if (!hold) { - drainQueue(ctx); - super.flush(ctx); - } else { - pendentFlush = true; - } - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - drainQueue(ctx); - super.channelInactive(ctx); - } - - private void drainQueue(ChannelHandlerContext ctx) { - queuedMessages.forEach(it -> ctx.write(it.getKey(), it.getValue())); - queuedMessages.clear(); - if (pendentFlush) ctx.flush(); - pendentFlush = false; - } - - @Override - public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { - drainQueue(ctx); - super.handlerRemoved(ctx); - } -} diff --git a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/client/MixinClientConnection.java b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/client/MixinClientConnection.java new file mode 100644 index 0000000..8d841d9 --- /dev/null +++ b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/client/MixinClientConnection.java @@ -0,0 +1,27 @@ +package com.viaversion.fabric.mc116.mixin.pipeline.client; + +import com.viaversion.fabric.mc116.ViaFabric; +import com.viaversion.fabric.mc116.service.ProtocolAutoDetector; +import net.minecraft.network.ClientConnection; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; + +@Mixin(ClientConnection.class) +public class MixinClientConnection { + @Inject(method = "connect", at = @At("HEAD")) + private static void onConnect(InetAddress address, int port, boolean shouldUseNativeTransport, CallbackInfoReturnable cir) { + try { + if (!ViaFabric.config.isClientSideEnabled()) return; + ProtocolAutoDetector.detectVersion(new InetSocketAddress(address, port)).get(10, TimeUnit.SECONDS); + } catch (Exception e) { + ViaFabric.JLOGGER.log(Level.WARNING, "Could not auto-detect protocol for " + address + " " + e); + } + } +} diff --git a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/client/MixinClientConnectionChInit.java b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/client/MixinClientConnectionChInit.java index 1c019e3..6de9dd6 100644 --- a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/client/MixinClientConnectionChInit.java +++ b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/mixin/pipeline/client/MixinClientConnectionChInit.java @@ -2,9 +2,7 @@ package com.viaversion.fabric.mc116.mixin.pipeline.client; import com.viaversion.fabric.common.handler.FabricDecodeHandler; import com.viaversion.fabric.common.handler.FabricEncodeHandler; -import com.viaversion.fabric.mc116.ViaFabric; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc116.handler.clientside.ProtocolDetectionHandler; import com.viaversion.fabric.common.protocol.HostnameParserProtocol; import com.viaversion.viaversion.connection.UserConnectionImpl; import com.viaversion.viaversion.protocol.ProtocolPipelineImpl; @@ -27,9 +25,6 @@ public class MixinClientConnectionChInit { channel.pipeline() .addBefore("encoder", CommonTransformer.HANDLER_ENCODER_NAME, new FabricEncodeHandler(user)) .addBefore("decoder", CommonTransformer.HANDLER_DECODER_NAME, new FabricDecodeHandler(user)); - if (ViaFabric.config.isClientSideEnabled()) { - channel.pipeline().addAfter(CommonTransformer.HANDLER_ENCODER_NAME, "via-autoprotocol", new ProtocolDetectionHandler()); - } } } } diff --git a/viafabric-mc116/src/main/resources/mixins.viafabric116.pipeline.json b/viafabric-mc116/src/main/resources/mixins.viafabric116.pipeline.json index a1d47a2..e395193 100644 --- a/viafabric-mc116/src/main/resources/mixins.viafabric116.pipeline.json +++ b/viafabric-mc116/src/main/resources/mixins.viafabric116.pipeline.json @@ -7,6 +7,7 @@ "MixinServerNetworkIoChInit" ], "client": [ + "client.MixinClientConnection", "client.MixinClientConnectionChInit" ], "injectors": { diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/handler/clientside/ProtocolDetectionHandler.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/handler/clientside/ProtocolDetectionHandler.java deleted file mode 100644 index a9c6663..0000000 --- a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/handler/clientside/ProtocolDetectionHandler.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.viaversion.fabric.mc117.handler.clientside; - -import com.viaversion.fabric.mc117.ViaFabric; -import com.viaversion.fabric.mc117.service.ProtocolAutoDetector; -import com.viaversion.viaversion.util.Pair; -import io.netty.channel.ChannelDuplexHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelPromise; - -import java.net.InetSocketAddress; -import java.util.ArrayDeque; -import java.util.Queue; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; - -public class ProtocolDetectionHandler extends ChannelDuplexHandler { - private final Queue> queuedMessages = new ArrayDeque<>(); - private boolean hold = true; - private boolean pendentFlush; - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - super.channelActive(ctx); - if (ctx.channel().remoteAddress() instanceof InetSocketAddress) { - ScheduledFuture timeoutRun = ctx.executor().schedule(() -> { - ViaFabric.JLOGGER.warning("Timeout for protocol auto-detection in " - + ctx.channel().remoteAddress() + " server"); - hold = false; - drainQueue(ctx); - ctx.pipeline().remove(this); - }, 10, TimeUnit.SECONDS); - ProtocolAutoDetector.detectVersion(((InetSocketAddress) ctx.channel().remoteAddress())) - .whenComplete((obj, ex) -> { - ctx.pipeline().remove(this); - timeoutRun.cancel(false); - }); - // Let's cache it before we need it - } - } - - @Override - public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { - if (!hold) { - drainQueue(ctx); - super.write(ctx, msg, promise); - } else { - queuedMessages.add(new Pair<>(msg, promise)); - } - } - - @Override - public void flush(ChannelHandlerContext ctx) throws Exception { - if (!hold) { - drainQueue(ctx); - super.flush(ctx); - } else { - pendentFlush = true; - } - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - drainQueue(ctx); - super.channelInactive(ctx); - } - - private void drainQueue(ChannelHandlerContext ctx) { - queuedMessages.forEach(it -> ctx.write(it.getKey(), it.getValue())); - queuedMessages.clear(); - if (pendentFlush) ctx.flush(); - pendentFlush = false; - } - - @Override - public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { - drainQueue(ctx); - super.handlerRemoved(ctx); - } -} diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/client/MixinClientConnection.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/client/MixinClientConnection.java new file mode 100644 index 0000000..c0ee77a --- /dev/null +++ b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/client/MixinClientConnection.java @@ -0,0 +1,26 @@ +package com.viaversion.fabric.mc117.mixin.pipeline.client; + +import com.viaversion.fabric.mc117.ViaFabric; +import com.viaversion.fabric.mc117.service.ProtocolAutoDetector; +import net.minecraft.network.ClientConnection; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import java.net.InetSocketAddress; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; + +@Mixin(ClientConnection.class) +public class MixinClientConnection { + @Inject(method = "connect", at = @At("HEAD")) + private static void onConnect(InetSocketAddress address, boolean useEpoll, CallbackInfoReturnable cir) { + try { + if (!ViaFabric.config.isClientSideEnabled()) return; + ProtocolAutoDetector.detectVersion(address).get(10, TimeUnit.SECONDS); + } catch (Exception e) { + ViaFabric.JLOGGER.log(Level.WARNING, "Could not auto-detect protocol for " + address + " " + e); + } + } +} diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/client/MixinClientConnectionChInit.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/client/MixinClientConnectionChInit.java index 715b9d3..faa3cca 100644 --- a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/client/MixinClientConnectionChInit.java +++ b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/mixin/pipeline/client/MixinClientConnectionChInit.java @@ -2,9 +2,7 @@ package com.viaversion.fabric.mc117.mixin.pipeline.client; import com.viaversion.fabric.common.handler.FabricDecodeHandler; import com.viaversion.fabric.common.handler.FabricEncodeHandler; -import com.viaversion.fabric.mc117.ViaFabric; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc117.handler.clientside.ProtocolDetectionHandler; import com.viaversion.fabric.common.protocol.HostnameParserProtocol; import com.viaversion.viaversion.connection.UserConnectionImpl; import com.viaversion.viaversion.protocol.ProtocolPipelineImpl; @@ -27,9 +25,6 @@ public class MixinClientConnectionChInit { channel.pipeline() .addBefore("encoder", CommonTransformer.HANDLER_ENCODER_NAME, new FabricEncodeHandler(user)) .addBefore("decoder", CommonTransformer.HANDLER_DECODER_NAME, new FabricDecodeHandler(user)); - if (ViaFabric.config.isClientSideEnabled()) { - channel.pipeline().addAfter(CommonTransformer.HANDLER_ENCODER_NAME, "via-autoprotocol", new ProtocolDetectionHandler()); - } } } } diff --git a/viafabric-mc117/src/main/resources/mixins.viafabric117.pipeline.json b/viafabric-mc117/src/main/resources/mixins.viafabric117.pipeline.json index ee1b68a..54ccd08 100644 --- a/viafabric-mc117/src/main/resources/mixins.viafabric117.pipeline.json +++ b/viafabric-mc117/src/main/resources/mixins.viafabric117.pipeline.json @@ -7,6 +7,7 @@ "MixinServerNetworkIoChInit" ], "client": [ + "client.MixinClientConnection", "client.MixinClientConnectionChInit" ], "injectors": { diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/handler/clientside/ProtocolDetectionHandler.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/handler/clientside/ProtocolDetectionHandler.java deleted file mode 100644 index d86840f..0000000 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/handler/clientside/ProtocolDetectionHandler.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.viaversion.fabric.mc18.handler.clientside; - -import com.viaversion.fabric.mc18.ViaFabric; -import com.viaversion.fabric.mc18.service.ProtocolAutoDetector; -import com.viaversion.viaversion.util.Pair; -import io.netty.channel.ChannelDuplexHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelPromise; - -import java.net.InetSocketAddress; -import java.util.ArrayDeque; -import java.util.Queue; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; - -public class ProtocolDetectionHandler extends ChannelDuplexHandler { - private final Queue> queuedMessages = new ArrayDeque<>(); - private boolean hold = true; - private boolean pendentFlush; - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - super.channelActive(ctx); - if (ctx.channel().remoteAddress() instanceof InetSocketAddress) { - ScheduledFuture timeoutRun = ctx.executor().schedule(() -> { - ViaFabric.JLOGGER.warning("Timeout for protocol auto-detection in " - + ctx.channel().remoteAddress() + " server"); - hold = false; - drainQueue(ctx); - ctx.pipeline().remove(this); - }, 10, TimeUnit.SECONDS); - ProtocolAutoDetector.detectVersion(((InetSocketAddress) ctx.channel().remoteAddress())) - .whenComplete((obj, ex) -> { - ctx.pipeline().remove(this); - timeoutRun.cancel(false); - }); - // Let's cache it before we need it - } - } - - @Override - public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { - if (!hold) { - drainQueue(ctx); - super.write(ctx, msg, promise); - } else { - queuedMessages.add(new Pair<>(msg, promise)); - } - } - - @Override - public void flush(ChannelHandlerContext ctx) throws Exception { - if (!hold) { - drainQueue(ctx); - super.flush(ctx); - } else { - pendentFlush = true; - } - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - drainQueue(ctx); - super.channelInactive(ctx); - } - - private void drainQueue(ChannelHandlerContext ctx) { - queuedMessages.forEach(it -> ctx.write(it.getKey(), it.getValue())); - queuedMessages.clear(); - if (pendentFlush) ctx.flush(); - pendentFlush = false; - } - - @Override - public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { - drainQueue(ctx); - super.handlerRemoved(ctx); - } -} diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/client/MixinClientConnection.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/client/MixinClientConnection.java new file mode 100644 index 0000000..46f3040 --- /dev/null +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/client/MixinClientConnection.java @@ -0,0 +1,27 @@ +package com.viaversion.fabric.mc18.mixin.pipeline.client; + +import com.viaversion.fabric.mc18.ViaFabric; +import com.viaversion.fabric.mc18.service.ProtocolAutoDetector; +import net.minecraft.network.ClientConnection; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; + +@Mixin(ClientConnection.class) +public class MixinClientConnection { + @Inject(method = "connect", at = @At("HEAD")) + private static void onConnect(InetAddress address, int port, boolean shouldUseNativeTransport, CallbackInfoReturnable cir) { + try { + if (!ViaFabric.config.isClientSideEnabled()) return; + ProtocolAutoDetector.detectVersion(new InetSocketAddress(address, port)).get(10, TimeUnit.SECONDS); + } catch (Exception e) { + ViaFabric.JLOGGER.log(Level.WARNING, "Could not auto-detect protocol for " + address + " " + e); + } + } +} diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/client/MixinClientConnectionChInit.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/client/MixinClientConnectionChInit.java index f6dd177..693a39d 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/client/MixinClientConnectionChInit.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/pipeline/client/MixinClientConnectionChInit.java @@ -2,9 +2,7 @@ package com.viaversion.fabric.mc18.mixin.pipeline.client; import com.viaversion.fabric.common.handler.FabricDecodeHandler; import com.viaversion.fabric.common.handler.FabricEncodeHandler; -import com.viaversion.fabric.mc18.ViaFabric; import com.viaversion.fabric.common.handler.CommonTransformer; -import com.viaversion.fabric.mc18.handler.clientside.ProtocolDetectionHandler; import com.viaversion.fabric.common.protocol.HostnameParserProtocol; import com.viaversion.viaversion.connection.UserConnectionImpl; import com.viaversion.viaversion.protocol.ProtocolPipelineImpl; @@ -27,9 +25,6 @@ public class MixinClientConnectionChInit { channel.pipeline() .addBefore("encoder", CommonTransformer.HANDLER_ENCODER_NAME, new FabricEncodeHandler(user)) .addBefore("decoder", CommonTransformer.HANDLER_DECODER_NAME, new FabricDecodeHandler(user)); - if (ViaFabric.config.isClientSideEnabled()) { - channel.pipeline().addAfter(CommonTransformer.HANDLER_ENCODER_NAME, "via-autoprotocol", new ProtocolDetectionHandler()); - } } } } diff --git a/viafabric-mc18/src/main/resources/mixins.viafabric18.pipeline.json b/viafabric-mc18/src/main/resources/mixins.viafabric18.pipeline.json index 98d56c5..df4b31a 100644 --- a/viafabric-mc18/src/main/resources/mixins.viafabric18.pipeline.json +++ b/viafabric-mc18/src/main/resources/mixins.viafabric18.pipeline.json @@ -7,6 +7,7 @@ "MixinServerNetworkIoChInit" ], "client": [ + "client.MixinClientConnection", "client.MixinClientConnectionChInit" ], "injectors": {