From bf91d2667a0044630a61fe14f99c445512d2f37a Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 1 May 2024 19:54:28 +0200 Subject: [PATCH] Add hacky mixin into ModInjector to ensure floodgate-fabric works with direct connections (#122) --- build.gradle.kts | 5 +++- .../inject/fabric/FabricInjector.java | 19 ++++++++------ .../mixin/GeyserModInjectorMixin.java | 25 +++++++++++++++++++ .../floodgate/util/MixinConfigPlugin.java | 3 +++ src/main/resources/floodgate.mixins.json | 3 ++- 5 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/geysermc/floodgate/mixin/GeyserModInjectorMixin.java diff --git a/build.gradle.kts b/build.gradle.kts index 07f46b8..eb3c6bb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -49,6 +49,9 @@ dependencies { include("org.geysermc.event", "events", "1.1-SNAPSHOT") include("org.lanternpowered", "lmbda", "2.0.0") // used in events + // Geyser dependency for the fun injector mixin :))) + modImplementation("org.geysermc.geyser:fabric:2.2.3-SNAPSHOT") + // cloud include("org.incendo:cloud-fabric:2.0.0-SNAPSHOT") modImplementation("org.incendo:cloud-fabric:2.0.0-SNAPSHOT") @@ -134,7 +137,7 @@ modrinth { syncBodyFrom.set(rootProject.file("README.md").readText()) uploadFile.set(tasks.named("remapModrinthJar")) - gameVersions.addAll("1.20.5") + gameVersions.addAll("1.20.5", "1.20.6") loaders.add("fabric") diff --git a/src/main/java/org/geysermc/floodgate/inject/fabric/FabricInjector.java b/src/main/java/org/geysermc/floodgate/inject/fabric/FabricInjector.java index e5a767f..15f0a1a 100644 --- a/src/main/java/org/geysermc/floodgate/inject/fabric/FabricInjector.java +++ b/src/main/java/org/geysermc/floodgate/inject/fabric/FabricInjector.java @@ -1,23 +1,35 @@ package org.geysermc.floodgate.inject.fabric; +import com.google.inject.Inject; import io.netty.channel.*; import lombok.Getter; import lombok.RequiredArgsConstructor; +import lombok.Setter; import org.checkerframework.checker.nullness.qual.NonNull; +import org.geysermc.floodgate.api.logger.FloodgateLogger; import org.geysermc.floodgate.inject.CommonPlatformInjector; @RequiredArgsConstructor public final class FabricInjector extends CommonPlatformInjector { + + @Setter @Getter private static FabricInjector instance; @Getter private final boolean injected = true; + @Inject private FloodgateLogger logger; + @Override public void inject() throws Exception { //no-op } public void injectClient(ChannelFuture future) { + if (future.channel().pipeline().names().contains("floodgate-init")) { + logger.debug("Tried to inject twice!"); + return; + } + future.channel().pipeline().addFirst("floodgate-init", new ChannelInboundHandlerAdapter() { @Override public void channelRead(@NonNull ChannelHandlerContext ctx, @NonNull Object msg) throws Exception { @@ -47,11 +59,4 @@ public void removeInjection() throws Exception { //no-op } - public static FabricInjector getInstance() { - return instance; - } - - public static void setInstance(FabricInjector injector) { - instance = injector; - } } diff --git a/src/main/java/org/geysermc/floodgate/mixin/GeyserModInjectorMixin.java b/src/main/java/org/geysermc/floodgate/mixin/GeyserModInjectorMixin.java new file mode 100644 index 0000000..4b117ad --- /dev/null +++ b/src/main/java/org/geysermc/floodgate/mixin/GeyserModInjectorMixin.java @@ -0,0 +1,25 @@ +package org.geysermc.floodgate.mixin; + +import io.netty.channel.ChannelFuture; +import org.geysermc.floodgate.inject.fabric.FabricInjector; +import org.geysermc.geyser.GeyserBootstrap; +import org.geysermc.geyser.platform.mod.GeyserModInjector; +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 java.util.List; + +@Mixin(value = GeyserModInjector.class, remap = false) +public class GeyserModInjectorMixin { + + @Shadow + private List allServerChannels; + + @Inject(method = "initializeLocalChannel0", at = @At(value = "INVOKE_ASSIGN", target = "Ljava/util/List;add(Ljava/lang/Object;)Z")) + public void onChannelAdd(GeyserBootstrap bootstrap, CallbackInfo ci) { + FabricInjector.getInstance().injectClient(this.allServerChannels.get(this.allServerChannels.size() - 1)); + } +} diff --git a/src/main/java/org/geysermc/floodgate/util/MixinConfigPlugin.java b/src/main/java/org/geysermc/floodgate/util/MixinConfigPlugin.java index dbedf9e..b8ee162 100644 --- a/src/main/java/org/geysermc/floodgate/util/MixinConfigPlugin.java +++ b/src/main/java/org/geysermc/floodgate/util/MixinConfigPlugin.java @@ -25,6 +25,9 @@ public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { //returns true if fabricproxy-lite is present, therefore loading the mixin. If not present, the mixin will not be loaded. return FabricLoader.getInstance().isModLoaded("fabricproxy-lite"); } + if (mixinClassName.equals("org.geysermc.floodgate.mixin.GeyserModInjectorMixin")) { + return FabricLoader.getInstance().isModLoaded("geyser-fabric"); + } return true; } diff --git a/src/main/resources/floodgate.mixins.json b/src/main/resources/floodgate.mixins.json index 4b1347e..ca2c72e 100644 --- a/src/main/resources/floodgate.mixins.json +++ b/src/main/resources/floodgate.mixins.json @@ -5,9 +5,10 @@ "compatibilityLevel": "JAVA_16", "mixins": [ "ChunkMapMixin", - "ClientIntentionPacketMixinInterface", "ClientIntentionPacketMixin", + "ClientIntentionPacketMixinInterface", "ConnectionMixin", + "GeyserModInjectorMixin", "ServerConnectionListenerMixin" ], "plugin": "org.geysermc.floodgate.util.MixinConfigPlugin",