From 770e0ff29cb3bea439c625d76d7c2b67650ca8ee Mon Sep 17 00:00:00 2001 From: FlorianMichael <60033407+FlorianMichael@users.noreply.github.com> Date: Tue, 28 Nov 2023 17:25:59 +0100 Subject: [PATCH] Cleaned connecting code by removing useless mixins/fields and fixed a few issues --- .../injection/access/IServerInfo.java | 8 ++-- .../integration/MixinConnectScreen_1.java | 8 ++-- .../integration/MixinMultiplayerScreen.java | 3 +- .../MixinMultiplayerServerListPinger_1.java | 22 ++++++---- ...ltiplayerServerListWidget_ServerEntry.java | 8 ++-- ...een_1.java => MixinMultiplayerScreen.java} | 24 ++++------- .../MixinMultiplayerServerListPinger.java | 3 +- .../perserverversion/MixinServerInfo.java | 41 ++++++++++--------- src/main/resources/viafabricplus.mixins.json | 4 +- 9 files changed, 61 insertions(+), 60 deletions(-) rename src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/{MixinConnectScreen_1.java => MixinMultiplayerScreen.java} (56%) diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/access/IServerInfo.java b/src/main/java/de/florianmichael/viafabricplus/injection/access/IServerInfo.java index b0eefe7d..2c5f60ba 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/access/IServerInfo.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/access/IServerInfo.java @@ -26,10 +26,10 @@ public interface IServerInfo { VersionEnum viaFabricPlus$forcedVersion(); void viaFabricPlus$forceVersion(final VersionEnum version); - boolean viaFabricPlus$enabled(); - void viaFabricPlus$enable(); + boolean viaFabricPlus$passedDirectConnectScreen(); + void viaFabricPlus$passDirectConnectScreen(); - int viaFabricPlus$translatingVersion(); - void viaFabricPlus$setTranslatingVersion(final int version); + VersionEnum viaFabricPlus$translatingVersion(); + void viaFabricPlus$setTranslatingVersion(final VersionEnum version); } diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinConnectScreen_1.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinConnectScreen_1.java index 0692735e..f2509e4f 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinConnectScreen_1.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinConnectScreen_1.java @@ -69,9 +69,11 @@ public abstract class MixinConnectScreen_1 { @SuppressWarnings("InvalidInjectorMethodSignature") @Inject(method = "run", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;connect(Ljava/net/InetSocketAddress;ZLnet/minecraft/network/ClientConnection;)Lio/netty/channel/ChannelFuture;", shift = At.Shift.BEFORE), locals = LocalCapture.CAPTURE_FAILHARD) private void setServerInfo(CallbackInfo ci, InetSocketAddress inetSocketAddress) { - final VersionEnum serverVersion = ((IServerInfo) this.field_40415).viaFabricPlus$forcedVersion(); - if (serverVersion != null) { - ProtocolHack.setTargetVersion(serverVersion); + final IServerInfo mixinServerInfo = (IServerInfo) this.field_40415; + final VersionEnum targetVersion = mixinServerInfo.viaFabricPlus$forcedVersion(); + + if (targetVersion != null && !mixinServerInfo.viaFabricPlus$passedDirectConnectScreen()) { + ProtocolHack.setTargetVersion(targetVersion); } else if (GeneralSettings.global().autoDetectVersion.getValue()) { this.field_2416.setStatus(Text.translatable("base.viafabricplus.detecting_server_version")); MCPing diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinMultiplayerScreen.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinMultiplayerScreen.java index 612821a7..ab548e8b 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinMultiplayerScreen.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinMultiplayerScreen.java @@ -40,8 +40,7 @@ public abstract class MixinMultiplayerScreen extends Screen { @Inject(method = "init", at = @At("RETURN")) private void addProtocolSelectionButton(CallbackInfo ci) { // Create the button - var builder = ButtonWidget.builder(Text.literal("ViaFabricPlus"), button -> - MainScreen.INSTANCE.open(this)).size(98, 20); + var builder = ButtonWidget.builder(Text.literal("ViaFabricPlus"), button -> MainScreen.INSTANCE.open(this)).size(98, 20); // Set the button's position according to the configured orientation builder = GeneralSettings.withOrientation(builder, GeneralSettings.global().multiplayerScreenButtonOrientation.getIndex(), width, height); diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinMultiplayerServerListPinger_1.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinMultiplayerServerListPinger_1.java index bda8e633..df42c2cd 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinMultiplayerServerListPinger_1.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinMultiplayerServerListPinger_1.java @@ -51,29 +51,33 @@ public abstract class MixinMultiplayerServerListPinger_1 implements ClientQueryP @Inject(method = "onResponse(Lnet/minecraft/network/packet/s2c/query/QueryResponseS2CPacket;)V", at = @At("HEAD")) private void trackTranslatingState(QueryResponseS2CPacket packet, CallbackInfo ci) { - final UserConnection userConnection = ((IClientConnection) field_3774).viaFabricPlus$getUserConnection(); - // If ViaVersion is translating the current connection, we track the target version, and it's state in the server info // So we can later draw this information when hovering over the ping bar in the server list - if (userConnection != null) { - ((IServerInfo) field_3776).viaFabricPlus$enable(); - ((IServerInfo) field_3776).viaFabricPlus$setTranslatingVersion(userConnection.getProtocolInfo().getServerProtocolVersion()); + if (field_3774 instanceof IClientConnection mixinClientConnection) { + ((IServerInfo) field_3776).viaFabricPlus$setTranslatingVersion(mixinClientConnection.viaFabricPlus$getTargetVersion()); } } - @Inject(method = "onResponse", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/ServerMetadata;version()Ljava/util/Optional;", shift = At.Shift.AFTER)) + @Inject(method = "onResponse", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;send(Lnet/minecraft/network/packet/Packet;)V", shift = At.Shift.AFTER)) private void setProtocolVersion(CallbackInfo ci) { + final VersionEnum version = ((IClientConnection) this.field_3774).viaFabricPlus$getTargetVersion(); + + // ViaVersion is not translating the current connection, so we don't need to do anything + if (version == null) { + return; + } + final boolean isCompatible; - if (ProtocolHack.getTargetVersion().isOlderThanOrEqualTo(VersionEnum.r1_6_4)) { + if (version.isOlderThanOrEqualTo(VersionEnum.r1_6_4)) { // Because of ViaVersion not supporting legacy minecraft versions where protocol ids are overlapping, ViaLegacy // has its own protocol id offset, where realVersion = -(ViaLegacyVersion >> 2). Normally ViaVersion sends the client // version to the client so its detection doesn't break when checking for serverVersion == clientVersion, but since // ViaLegacy doesn't do that, we have to do it ourselves - isCompatible = LegacyProtocolVersion.getRealProtocolVersion(((IClientConnection) this.field_3774).viaFabricPlus$getTargetVersion().getVersion()) == this.field_3776.protocolVersion; + isCompatible = LegacyProtocolVersion.getRealProtocolVersion(version.getVersion()) == this.field_3776.protocolVersion; } else if (ProtocolHack.getTargetVersion().equals(VersionEnum.bedrockLatest)) { // Bedrock edition doesn't have a protocol id like the Java edition, ViaBedrock also has its own protocol id offset // Which we need to remove to get the real protocol id - isCompatible = ((IClientConnection) this.field_3774).viaFabricPlus$getTargetVersion().getVersion() - BedrockProtocolVersion.PROTOCOL_ID_OVERLAP_PREVENTION_OFFSET == this.field_3776.protocolVersion; + isCompatible = version.getVersion() - BedrockProtocolVersion.PROTOCOL_ID_OVERLAP_PREVENTION_OFFSET == this.field_3776.protocolVersion; } else { return; } diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinMultiplayerServerListWidget_ServerEntry.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinMultiplayerServerListWidget_ServerEntry.java index 081b98bd..76b5ae30 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinMultiplayerServerListWidget_ServerEntry.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinMultiplayerServerListWidget_ServerEntry.java @@ -47,12 +47,10 @@ public abstract class MixinMultiplayerServerListWidget_ServerEntry { private void drawTranslatingState(MultiplayerScreen instance, List tooltip, Operation original) { final List tooltipCopy = new ArrayList<>(tooltip); if (GeneralSettings.global().showAdvertisedServerVersion.getValue()) { - final IServerInfo mixinServerInfo = ((IServerInfo) server); + final VersionEnum versionEnum = ((IServerInfo) server).viaFabricPlus$translatingVersion(); - if (mixinServerInfo.viaFabricPlus$enabled()) { - final var versionEnum = VersionEnum.fromProtocolId(mixinServerInfo.viaFabricPlus$translatingVersion()); - - tooltipCopy.add(Text.translatable("base.viafabricplus.via_translates_to", versionEnum != VersionEnum.UNKNOWN ? versionEnum.getName() + " (" + versionEnum.getVersion() + ")" : mixinServerInfo.viaFabricPlus$translatingVersion())); + if (versionEnum != null) { + tooltipCopy.add(Text.translatable("base.viafabricplus.via_translates_to", versionEnum.getName() + " (" + versionEnum.getOriginalVersion() + ")")); tooltipCopy.add(Text.translatable("base.viafabricplus.server_version", server.version.getString() + " (" + server.protocolVersion + ")")); } } diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinConnectScreen_1.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinMultiplayerScreen.java similarity index 56% rename from src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinConnectScreen_1.java rename to src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinMultiplayerScreen.java index fc600b89..b8b83847 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinConnectScreen_1.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinMultiplayerScreen.java @@ -20,28 +20,22 @@ package de.florianmichael.viafabricplus.injection.mixin.base.perserverversion; import de.florianmichael.viafabricplus.injection.access.IServerInfo; -import de.florianmichael.viafabricplus.protocolhack.ProtocolHack; +import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen; import net.minecraft.client.network.ServerInfo; -import org.spongepowered.asm.mixin.Final; 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.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.Redirect; -@Mixin(targets = "net.minecraft.client.gui.screen.ConnectScreen$1") -public abstract class MixinConnectScreen_1 { +@Mixin(MultiplayerScreen.class) +public abstract class MixinMultiplayerScreen { - @Final - @Shadow - ServerInfo field_40415; + @Shadow protected abstract void connect(ServerInfo entry); - @Inject(method = "run", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;connect(Ljava/net/InetSocketAddress;ZLnet/minecraft/network/ClientConnection;)Lio/netty/channel/ChannelFuture;")) - private void setForcedTargetVersion(CallbackInfo ci) { - if (field_40415 != null) { - // Set the target version to the forced version when connecting to a server - ProtocolHack.setTargetVersion(((IServerInfo) field_40415).viaFabricPlus$forcedVersion()); - } + @Redirect(method = "directConnect", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/multiplayer/MultiplayerScreen;connect(Lnet/minecraft/client/network/ServerInfo;)V")) + private void storeDirectConnectionPhase(MultiplayerScreen instance, ServerInfo entry) { + ((IServerInfo) entry).viaFabricPlus$passDirectConnectScreen(); + connect(entry); } } diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinMultiplayerServerListPinger.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinMultiplayerServerListPinger.java index dbd9208b..e7758b8e 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinMultiplayerServerListPinger.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinMultiplayerServerListPinger.java @@ -39,7 +39,8 @@ public abstract class MixinMultiplayerServerListPinger { @Redirect(method = "add", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;connect(Ljava/net/InetSocketAddress;ZLnet/minecraft/util/profiler/PerformanceLog;)Lnet/minecraft/network/ClientConnection;")) private ClientConnection setForcedVersion(InetSocketAddress address, boolean useEpoll, PerformanceLog packetSizeLog, @Local ServerInfo serverInfo) { final VersionEnum forcedVersion = ((IServerInfo) serverInfo).viaFabricPlus$forcedVersion(); - if (forcedVersion != null) { + + if (forcedVersion != null && !((IServerInfo) serverInfo).viaFabricPlus$passedDirectConnectScreen()) { // We use the PerformanceLog field to store the forced version since it's always null when pinging a server // So we can create a dummy instance, store the forced version in it and later destroy the instance again // To avoid any side effects, we also support cases where a mod is also creating a PerformanceLog instance diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinServerInfo.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinServerInfo.java index 7078caf3..22bfea8e 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinServerInfo.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinServerInfo.java @@ -41,15 +41,11 @@ public abstract class MixinServerInfo implements IServerInfo { @Unique private VersionEnum viaFabricPlus$forcedVersion = null; - @Override - public VersionEnum viaFabricPlus$forcedVersion() { - return viaFabricPlus$forcedVersion; - } + @Unique + private boolean viaFabricPlus$passedDirectConnectScreen; - @Override - public void viaFabricPlus$forceVersion(VersionEnum version) { - viaFabricPlus$forcedVersion = version; - } + @Unique + private VersionEnum viaFabricPlus$translatingVersion; @Inject(method = "toNbt", at = @At("TAIL"), locals = LocalCapture.CAPTURE_FAILHARD) private void saveForcedVersion(CallbackInfoReturnable cir, NbtCompound nbtCompound) { @@ -77,29 +73,36 @@ public abstract class MixinServerInfo implements IServerInfo { viaFabricPlus$forceVersion(((IServerInfo) serverInfo).viaFabricPlus$forcedVersion()); } - @Unique - private boolean viaFabricPlus$enabled; - @Override - public boolean viaFabricPlus$enabled() { - return viaFabricPlus$enabled; + public VersionEnum viaFabricPlus$forcedVersion() { + return viaFabricPlus$forcedVersion; } @Override - public void viaFabricPlus$enable() { - viaFabricPlus$enabled = true; + public void viaFabricPlus$forceVersion(VersionEnum version) { + viaFabricPlus$forcedVersion = version; } - @Unique - private int viaFabricPlus$translatingVersion; + @Override + public boolean viaFabricPlus$passedDirectConnectScreen() { + final boolean previous = viaFabricPlus$passedDirectConnectScreen; + viaFabricPlus$passedDirectConnectScreen = false; + + return previous; + } @Override - public int viaFabricPlus$translatingVersion() { + public void viaFabricPlus$passDirectConnectScreen() { + viaFabricPlus$passedDirectConnectScreen = true; + } + + @Override + public VersionEnum viaFabricPlus$translatingVersion() { return viaFabricPlus$translatingVersion; } @Override - public void viaFabricPlus$setTranslatingVersion(int version) { + public void viaFabricPlus$setTranslatingVersion(VersionEnum version) { viaFabricPlus$translatingVersion = version; } } diff --git a/src/main/resources/viafabricplus.mixins.json b/src/main/resources/viafabricplus.mixins.json index 03f501cb..79eadaa9 100644 --- a/src/main/resources/viafabricplus.mixins.json +++ b/src/main/resources/viafabricplus.mixins.json @@ -19,7 +19,7 @@ "base.integration.MixinMultiplayerServerListPinger_1", "base.integration.MixinMultiplayerServerListWidget_ServerEntry", "base.integration.MixinRakSessionCodec", - "base.perserverversion.MixinConnectScreen_1", + "base.perserverversion.MixinMultiplayerScreen", "base.perserverversion.MixinMultiplayerServerListPinger", "base.perserverversion.MixinPerformanceLog", "base.perserverversion.MixinServerInfo", @@ -162,7 +162,6 @@ "fixes.minecraft.screen.screenhandler.MixinScreenHandler", "fixes.vialegacy.MixinClassicProtocolExtension", "fixes.vialegacy.MixinClientboundPacketsc0_30cpe", - "vialegacy.MixinExtensionProtocolMetadataStorage", "fixes.vialegacy.MixinProtocol1_8to1_7_6_10", "fixes.vialegacy.MixinProtocolc0_30toc0_30cpe", "fixes.viaversion.MixinChatItemRewriter", @@ -193,6 +192,7 @@ "viabedrock.MixinBedrockProtocol", "viabedrock.MixinBlobCache", "viabedrock.MixinJoinPackets", + "vialegacy.MixinExtensionProtocolMetadataStorage", "viaversion.MixinAbstractFenceConnectionHandler", "viaversion.MixinGlassConnectionHandler", "viaversion.MixinProtocolVersion"