Skip to content

Commit

Permalink
fix: minor multichannel issue on 1.20.6+
Browse files Browse the repository at this point in the history
  • Loading branch information
ishland committed Dec 1, 2024
1 parent f3eee0b commit 2f73221
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
import com.ishland.raknetify.common.connection.RakNetSimpleMultiChannelCodec;
import com.ishland.raknetify.common.connection.multichannel.CustomPayloadChannel;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket;
import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket;

@ChannelHandler.Sharable
public class MultiChannellingPacketCapture extends ChannelOutboundHandlerAdapter {

private Class<?> packetClass = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@

import com.ishland.raknetify.common.connection.RakNetSimpleMultiChannelCodec;
import com.ishland.raknetify.common.connection.SynchronizationLayer;
import com.ishland.raknetify.fabric.mixin.RaknetifyFabricMixinPlugin;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import net.minecraft.network.handler.NetworkStateTransitions;
import net.minecraft.network.packet.c2s.play.AcknowledgeReconfigurationC2SPacket;
import net.minecraft.network.packet.s2c.play.CommandTreeS2CPacket;
import net.minecraft.network.packet.s2c.play.EnterReconfigurationS2CPacket;
Expand Down Expand Up @@ -65,6 +67,12 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
if (msg instanceof CommandTreeS2CPacket) {
ctx.write(RakNetSimpleMultiChannelCodec.SIGNAL_START_MULTICHANNEL);
}
if (RaknetifyFabricMixinPlugin.AFTER_1_20_5) {
if (msg instanceof NetworkStateTransitions.DecoderTransitioner || msg instanceof NetworkStateTransitions.EncoderTransitioner) {
ctx.write(RakNetNetworkTransitionUtil.handleTransition(msg), promise);
return;
}
}
super.write(ctx, msg, promise);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@
import com.ishland.raknetify.fabric.common.compat.viafabric.ViaFabricCompatInjector;
import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import network.ycc.raknet.RakNet;

public class RakNetFabricConnectionUtil {

public static final String NAME_RAKNETIFY_MULTI_CHANNEL_PACKET_CATURE = "raknetify-multi-channel-packet-cature";

private RakNetFabricConnectionUtil() {
}

Expand All @@ -52,11 +57,8 @@ public static void postInitChannel(Channel channel, boolean isClientSide) {
channel.pipeline().replace("splitter", "splitter", new ChannelDuplexHandler()); // no-op
channel.pipeline().replace("prepender", "prepender", new ChannelDuplexHandler()); // no-op
final MultiChannellingPacketCapture handler = new MultiChannellingPacketCapture();
if (channel.pipeline().names().contains("unbundler")) {
channel.pipeline().addBefore("unbundler", "raknetify-multi-channel-packet-cature", handler);
} else {
channel.pipeline().addLast("raknetify-multi-channel-packet-cature", handler);
}
channel.pipeline().addLast(NAME_RAKNETIFY_MULTI_CHANNEL_PACKET_CATURE, handler);
onPipelineReorder(channel.pipeline());
channel.pipeline().get(RakNetSimpleMultiChannelCodec.class)
.addHandler(handler.getCustomPayloadHandler())
.addHandler(handler.getCaptureBasedHandler());
Expand All @@ -65,4 +67,16 @@ public static void postInitChannel(Channel channel, boolean isClientSide) {
}
}

static void onPipelineReorder(ChannelPipeline pipeline) {
if (pipeline.get("encoder") == null) {
// System.out.println("Reordering failed: no encoder");
return;
}
// System.out.println("Reordering");
ChannelHandler handler = pipeline.remove(RakNetFabricConnectionUtil.NAME_RAKNETIFY_MULTI_CHANNEL_PACKET_CATURE);
if (handler != null) {
pipeline.addAfter("encoder", RakNetFabricConnectionUtil.NAME_RAKNETIFY_MULTI_CHANNEL_PACKET_CATURE, handler);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is a part of the Raknetify project, licensed under MIT.
*
* Copyright (c) 2022-2023 ishland
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.ishland.raknetify.fabric.common.connection;

import net.minecraft.network.handler.NetworkStateTransitions;

public class RakNetNetworkTransitionUtil {

static Object handleTransition(Object msg) {
if (msg instanceof NetworkStateTransitions.DecoderTransitioner transitioner) {
return transitioner.andThen(context -> RakNetFabricConnectionUtil.onPipelineReorder(context.pipeline()));
} else if (msg instanceof NetworkStateTransitions.EncoderTransitioner transitioner) {
return transitioner.andThen(context -> RakNetFabricConnectionUtil.onPipelineReorder(context.pipeline()));
}
return msg;
}

}

0 comments on commit 2f73221

Please sign in to comment.