From 4edbdd1c1c9fec8e9b363708471ceb63d00e8475 Mon Sep 17 00:00:00 2001 From: FlorianMichael <60033407+FlorianMichael@users.noreply.github.com> Date: Wed, 17 May 2023 17:22:15 +0200 Subject: [PATCH 1/2] Cleaned ViaBedrock implementation --- .../definition/v1_12_2/FontCacheFix.java | 14 ++++---- .../mixin/base/MixinClientConnection.java | 4 +-- .../MixinMultiplayerServerListPinger.java | 9 ++++-- .../MixinClientPlayNetworkHandler.java | 4 ++- .../screen/MixinConnectScreen_1.java | 4 +-- .../protocolhack/ProtocolHack.java | 32 ++++++++----------- .../netty/ViaFabricPlusVLBPipeline.java | 8 ++--- .../viabedrock/RakNetClientConnection.java | 10 +++++- 8 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/main/java/de/florianmichael/viafabricplus/definition/v1_12_2/FontCacheFix.java b/src/main/java/de/florianmichael/viafabricplus/definition/v1_12_2/FontCacheFix.java index 8210a1c0..2883b79c 100644 --- a/src/main/java/de/florianmichael/viafabricplus/definition/v1_12_2/FontCacheFix.java +++ b/src/main/java/de/florianmichael/viafabricplus/definition/v1_12_2/FontCacheFix.java @@ -57,10 +57,7 @@ public class FontCacheFix { public enum BuiltinEmptyGlyph1_12_2 implements Glyph { - VERY_MISSING(() -> BuiltinEmptyGlyph1_12_2.createRectImage((x, y) -> { - boolean bl = x == 0 || x + 1 == 5 || y == 0 || y + 1 == 8; - return bl ? -1 : 0; - })); + VERY_MISSING(() -> BuiltinEmptyGlyph1_12_2.createRectImage((x, y) -> x == 0 || x + 1 == 5 || y == 0 || y + 1 == 8 ? -1 : 0)); final NativeImage image; @@ -71,9 +68,9 @@ public class FontCacheFix { private static NativeImage createRectImage(BuiltinEmptyGlyph1_12_2.ColorSupplier colorSupplier) { final NativeImage nativeImage = new NativeImage(NativeImage.Format.RGBA, 5, 8, false); - for (int i = 0; i < 8; ++i) { - for (int j = 0; j < 5; ++j) { - nativeImage.setColor(j, i, colorSupplier.getColor(j, i)); + for (int y = 0; y < 8; ++y) { + for (int x = 0; x < 5; ++x) { + nativeImage.setColor(x, y, colorSupplier.getColor(x, y)); } } nativeImage.untrack(); @@ -88,6 +85,7 @@ public class FontCacheFix { @Override public GlyphRenderer bake(Function function) { return function.apply(new RenderableGlyph() { + @Override public int getWidth() { return BuiltinEmptyGlyph1_12_2.this.image.getWidth(); @@ -118,7 +116,7 @@ public class FontCacheFix { @FunctionalInterface interface ColorSupplier { - int getColor(int var1, int var2); + int getColor(int x, int y); } } } diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/MixinClientConnection.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/MixinClientConnection.java index dd8be508..f1a90551 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/MixinClientConnection.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/MixinClientConnection.java @@ -99,9 +99,9 @@ public abstract class MixinClientConnection extends SimpleChannelInboundHandler< public void channelRegistered(ChannelHandlerContext ctx) throws Exception { super.channelRegistered(ctx); - if (ProtocolHack.getRakNetPingSessions().contains(viafabricplus_capturedAddress)) { + if (RakNetClientConnection.getRakNetPingSessions().contains(viafabricplus_capturedAddress)) { channelActive(ctx); - ProtocolHack.getRakNetPingSessions().remove(viafabricplus_capturedAddress); + RakNetClientConnection.getRakNetPingSessions().remove(viafabricplus_capturedAddress); } } diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/MixinMultiplayerServerListPinger.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/MixinMultiplayerServerListPinger.java index 5b54614a..93f9377b 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/MixinMultiplayerServerListPinger.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/MixinMultiplayerServerListPinger.java @@ -19,6 +19,7 @@ package de.florianmichael.viafabricplus.injection.mixin.base; import de.florianmichael.viafabricplus.injection.access.IServerInfo; import de.florianmichael.viafabricplus.protocolhack.ProtocolHack; +import de.florianmichael.viafabricplus.protocolhack.netty.viabedrock.RakNetClientConnection; import de.florianmichael.vialoadingbase.model.ComparableProtocolVersion; import net.minecraft.client.network.MultiplayerServerListPinger; import net.minecraft.client.network.ServerAddress; @@ -40,7 +41,11 @@ public class MixinMultiplayerServerListPinger { public void trackSessions(ServerInfo entry, Runnable saver, CallbackInfo ci, ServerAddress serverAddress, Optional optional, InetSocketAddress inetSocketAddress) { final ComparableProtocolVersion version = ((IServerInfo) entry).viafabricplus_forcedVersion(); - if (version != null) ProtocolHack.getForcedVersions().put(inetSocketAddress, version); - if (ProtocolHack.isEqualToOrForced(inetSocketAddress, BedrockProtocolVersion.bedrockLatest)) ProtocolHack.getRakNetPingSessions().add(inetSocketAddress); + if (version != null) { + ProtocolHack.getForcedVersions().put(inetSocketAddress, version); + } + if (ProtocolHack.getTargetVersion(inetSocketAddress).isEqualTo(BedrockProtocolVersion.bedrockLatest)) { + RakNetClientConnection.getRakNetPingSessions().add(inetSocketAddress); + } } } diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/MixinClientPlayNetworkHandler.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/MixinClientPlayNetworkHandler.java index 0620837b..af1c0297 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/MixinClientPlayNetworkHandler.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/MixinClientPlayNetworkHandler.java @@ -70,6 +70,8 @@ public abstract class MixinClientPlayNetworkHandler { @Shadow public abstract void sendPacket(Packet packet); + @Shadow @Final private ClientConnection connection; + @Inject(method = "", at = @At("RETURN")) public void fixPlayerListOrdering(MinecraftClient client, Screen screen, ClientConnection connection, ServerInfo serverInfo, GameProfile profile, WorldSession worldSession, CallbackInfo ci) { if (ProtocolHack.getTargetVersion().isOlderThanOrEqualTo(ProtocolVersion.v1_19_1)) { @@ -152,7 +154,7 @@ public abstract class MixinClientPlayNetworkHandler { @Inject(method = "onSetTradeOffers", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/NetworkThreadUtils;forceMainThread(Lnet/minecraft/network/packet/Packet;Lnet/minecraft/network/listener/PacketListener;Lnet/minecraft/util/thread/ThreadExecutor;)V", shift = At.Shift.AFTER), cancellable = true) public void checkLoginPacket(SetTradeOffersS2CPacket packet, CallbackInfo ci) { - if (ProtocolHack.getTargetVersion().isOlderThanOrEqualTo(ProtocolVersion.v1_13_2) && this.client.player == null) { + if (ProtocolHack.getTargetVersion(connection.channel).isOlderThanOrEqualTo(ProtocolVersion.v1_13_2) && this.client.player == null) { ViaFabricPlus.LOGGER.error("Server tried to send Play packet in Login process, dropping \"SetTradeOffers\""); ci.cancel(); } diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/screen/MixinConnectScreen_1.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/screen/MixinConnectScreen_1.java index bd08d0b5..ff72c80b 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/screen/MixinConnectScreen_1.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/screen/MixinConnectScreen_1.java @@ -66,7 +66,7 @@ public class MixinConnectScreen_1 { @Redirect(method = "run", at = @At(value = "INVOKE", target = "Ljava/net/InetSocketAddress;getHostName()Ljava/lang/String;", ordinal = 0)) public String replaceAddress(InetSocketAddress instance) { - if (ProtocolHack.isOlderThanOrEqualToOrForced(instance, ProtocolVersion.v1_17) || ProtocolHack.isEqualToOrForced(instance, BedrockProtocolVersion.bedrockLatest)) { + if (ProtocolHack.getTargetVersion(instance).isOlderThanOrEqualTo(ProtocolVersion.v1_17) || ProtocolHack.getTargetVersion(instance).isEqualTo(BedrockProtocolVersion.bedrockLatest)) { return field_33737.getAddress(); } return instance.getHostName(); @@ -74,7 +74,7 @@ public class MixinConnectScreen_1 { @Redirect(method = "run", at = @At(value = "INVOKE", target = "Ljava/net/InetSocketAddress;getPort()I")) public int replacePort(InetSocketAddress instance) { - if (ProtocolHack.isOlderThanOrEqualToOrForced(instance, ProtocolVersion.v1_17) || ProtocolHack.isEqualToOrForced(instance, BedrockProtocolVersion.bedrockLatest)) { + if (ProtocolHack.getTargetVersion(instance).isOlderThanOrEqualTo(ProtocolVersion.v1_17) || ProtocolHack.getTargetVersion(instance).isEqualTo(BedrockProtocolVersion.bedrockLatest)) { return field_33737.getPort(); } return instance.getPort(); diff --git a/src/main/java/de/florianmichael/viafabricplus/protocolhack/ProtocolHack.java b/src/main/java/de/florianmichael/viafabricplus/protocolhack/ProtocolHack.java index e9eba770..c327995b 100644 --- a/src/main/java/de/florianmichael/viafabricplus/protocolhack/ProtocolHack.java +++ b/src/main/java/de/florianmichael/viafabricplus/protocolhack/ProtocolHack.java @@ -82,40 +82,34 @@ public class ProtocolHack { public final static AttributeKey FORCED_VERSION = AttributeKey.newInstance("viafabricplus-forced-version"); private final static Map forcedVersions = new HashMap<>(); - private final static List rakNetPingSessions = new ArrayList<>(); public static ComparableProtocolVersion getTargetVersion() { - if (MinecraftClient.getInstance() == null || MinecraftClient.getInstance().getNetworkHandler() == null) return getTargetVersion(null); + if (MinecraftClient.getInstance() == null || MinecraftClient.getInstance().getNetworkHandler() == null) { + return getTargetVersion((Channel) null); + } return getTargetVersion(MinecraftClient.getInstance().getNetworkHandler().getConnection().channel); } public static ComparableProtocolVersion getTargetVersion(final Channel channel) { - if (channel != null && channel.hasAttr(FORCED_VERSION)) return channel.attr(FORCED_VERSION).get(); + if (channel != null && channel.hasAttr(FORCED_VERSION)) { + return channel.attr(FORCED_VERSION).get(); + } return ViaLoadingBase.getInstance().getTargetVersion(); } + public static ComparableProtocolVersion getTargetVersion(final InetSocketAddress socketAddress) { + if (forcedVersions.containsKey(socketAddress)) { + return forcedVersions.get(socketAddress); + } + return getTargetVersion(); + } + public static Map getForcedVersions() { return forcedVersions; } - public static List getRakNetPingSessions() { - return rakNetPingSessions; - } - - public static boolean isEqualToOrForced(final InetSocketAddress socketAddress, final ProtocolVersion version) { - if (forcedVersions.containsKey(socketAddress)) return forcedVersions.get(socketAddress).isEqualTo(version); - - return ProtocolHack.getTargetVersion().isEqualTo(version); - } - - public static boolean isOlderThanOrEqualToOrForced(final InetSocketAddress socketAddress, final ProtocolVersion version) { - if (forcedVersions.containsKey(socketAddress)) return forcedVersions.get(socketAddress).isOlderThanOrEqualTo(version); - - return ProtocolHack.getTargetVersion().isOlderThanOrEqualTo(version); - } - public static void injectVLBPipeline(final ClientConnection connection, final Channel channel, final InetSocketAddress address) { if (ProtocolHack.getForcedVersions().containsKey(address)) { channel.attr(ProtocolHack.FORCED_VERSION).set(ProtocolHack.getForcedVersions().get(address)); diff --git a/src/main/java/de/florianmichael/viafabricplus/protocolhack/netty/ViaFabricPlusVLBPipeline.java b/src/main/java/de/florianmichael/viafabricplus/protocolhack/netty/ViaFabricPlusVLBPipeline.java index d965c593..eb4b6ecc 100644 --- a/src/main/java/de/florianmichael/viafabricplus/protocolhack/netty/ViaFabricPlusVLBPipeline.java +++ b/src/main/java/de/florianmichael/viafabricplus/protocolhack/netty/ViaFabricPlusVLBPipeline.java @@ -19,8 +19,8 @@ package de.florianmichael.viafabricplus.protocolhack.netty; import com.viaversion.viaversion.api.connection.UserConnection; import de.florianmichael.viafabricplus.ViaFabricPlus; -import de.florianmichael.viafabricplus.protocolhack.ProtocolHack; import de.florianmichael.viafabricplus.protocolhack.netty.viabedrock.DisconnectAdapter; +import de.florianmichael.viafabricplus.protocolhack.netty.viabedrock.RakNetClientConnection; import de.florianmichael.viafabricplus.protocolhack.netty.viabedrock.codec.PingEncapsulationCodec; import de.florianmichael.viafabricplus.protocolhack.netty.viabedrock.codec.RakMessageEncapsulationCodec; import de.florianmichael.vialoadingbase.model.ComparableProtocolVersion; @@ -50,8 +50,8 @@ public class ViaFabricPlusVLBPipeline extends VLBPipeline { public final static String VIA_BEDROCK_ENCRYPTION_HANDLER_NAME = "via-bedrock-encryption"; // ViaLegacy (pre Netty) - public final static String VIA_LEGACY_PRE_NETTY_LENGTH_PREPENDER_HANDLER_NAME = "via-legacy-decoder"; - public final static String VIA_LEGACY_PRE_NETTY_LENGTH_REMOVER_HANDLER_NAME = "via-legacy-encoder"; + public final static String VIA_LEGACY_PRE_NETTY_LENGTH_PREPENDER_HANDLER_NAME = "via-legacy-pre-netty-length-prepender"; + public final static String VIA_LEGACY_PRE_NETTY_LENGTH_REMOVER_HANDLER_NAME = "via-legacy-pre-netty-length-remover"; private final InetSocketAddress address; private final ComparableProtocolVersion version; @@ -93,7 +93,7 @@ public class ViaFabricPlusVLBPipeline extends VLBPipeline { pipeline.remove("timeout"); // Pinging in RakNet is something different - if (ProtocolHack.getRakNetPingSessions().contains(address)) { + if (RakNetClientConnection.getRakNetPingSessions().contains(address)) { pipeline.replace(VIA_BEDROCK_FRAME_ENCAPSULATION_HANDLER_NAME, VIA_BEDROCK_PING_ENCAPSULATION_HANDLER_NAME, new PingEncapsulationCodec(address)); pipeline.remove(VIA_BEDROCK_PACKET_ENCAPSULATION_HANDLER_NAME); diff --git a/src/main/java/de/florianmichael/viafabricplus/protocolhack/netty/viabedrock/RakNetClientConnection.java b/src/main/java/de/florianmichael/viafabricplus/protocolhack/netty/viabedrock/RakNetClientConnection.java index 428f3886..fb32a2af 100644 --- a/src/main/java/de/florianmichael/viafabricplus/protocolhack/netty/viabedrock/RakNetClientConnection.java +++ b/src/main/java/de/florianmichael/viafabricplus/protocolhack/netty/viabedrock/RakNetClientConnection.java @@ -32,9 +32,13 @@ import org.cloudburstmc.netty.channel.raknet.config.RakChannelOption; import org.jetbrains.annotations.NotNull; import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.ThreadLocalRandom; public class RakNetClientConnection { + private final static List rakNetPingSessions = new ArrayList<>(); + public static void connectRakNet(final ClientConnection clientConnection, final InetSocketAddress address, final Lazy lazy, final Class channelType) { final Bootstrap nettyBoostrap = new Bootstrap().group((EventLoopGroup) lazy.get()).handler(new ChannelInitializer<>() { @Override @@ -55,10 +59,14 @@ public class RakNetClientConnection { } }).channelFactory(channelType == EpollSocketChannel.class ? RakChannelFactory.client(EpollDatagramChannel.class) : RakChannelFactory.client(NioDatagramChannel.class)); - if (ProtocolHack.getRakNetPingSessions().contains(address)) { + if (getRakNetPingSessions().contains(address)) { nettyBoostrap.bind(new InetSocketAddress(0)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE).syncUninterruptibly(); } else { nettyBoostrap.connect(address.getAddress(), address.getPort()).syncUninterruptibly(); } } + + public static List getRakNetPingSessions() { + return rakNetPingSessions; + } } From fcff14dc44facd13807997cd7ce848acc1f8ea71 Mon Sep 17 00:00:00 2001 From: FlorianMichael <60033407+FlorianMichael@users.noreply.github.com> Date: Thu, 18 May 2023 23:10:42 +0200 Subject: [PATCH 2/2] Bump libraries --- gradle.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index a33f4f44..5867751b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -19,7 +19,7 @@ raknet_transport_version=1.0.0.CR1-SNAPSHOT classic4j_version=1.2.0 # viaversion (and required) libs -viaversion_version=4.7.0-1.20-pre1-SNAPSHOT +viaversion_version=4.7.0-1.20-pre2-SNAPSHOT viabackwards_version=4.7.0-1.20-pre1-SNAPSHOT snake_yml_version=2.0 @@ -27,7 +27,7 @@ snake_yml_version=2.0 vialegacy_version=2.2.16 viaaprilfools_version=2.0.7-SNAPSHOT viabedrock_version=0.0.1-SNAPSHOT -minecraftauth_version=2.1.1 +minecraftauth_version=2.1.2 # lenni0451 libs mcstructs_text_version=2.2.5