Skip to content

Commit

Permalink
Fix Fabric serverside
Browse files Browse the repository at this point in the history
  • Loading branch information
You committed Oct 14, 2024
1 parent 2099930 commit 57f9189
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import io.github.retrooper.packetevents.impl.netty.manager.player.PlayerManagerAbstract;
import io.github.retrooper.packetevents.impl.netty.manager.protocol.ProtocolManagerAbstract;
import io.github.retrooper.packetevents.impl.netty.manager.server.ServerManagerAbstract;
import io.github.retrooper.packetevents.injector.CustomPipelineUtil;
import io.netty.channel.Channel;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minecraft.SharedConstants;
Expand Down Expand Up @@ -199,11 +198,11 @@ public void load() {
PacketEvents.CONNECTION_HANDLER_NAME = "pe-connection-handler-" + id;
PacketEvents.SERVER_CHANNEL_HANDLER_NAME = "pe-connection-initializer-" + id;

try {
CustomPipelineUtil.init();
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
// try {
// CustomPipelineUtil.init();
// } catch (Exception ex) {
// throw new IllegalStateException(ex);
// }

injector.inject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.util.PacketEventsImplHelper;
import io.github.retrooper.packetevents.injector.CustomPipelineUtil;
import io.github.retrooper.packetevents.mixin.CompressionDecoderMixin;
import io.github.retrooper.packetevents.mixin.CompressionEncoderMixin;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import net.minecraft.client.player.LocalPlayer;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import net.minecraft.network.CompressionEncoder;

@ChannelHandler.Sharable
public class PacketEventsClientDecoder extends MessageToMessageDecoder<ByteBuf> {
Expand Down Expand Up @@ -50,39 +52,31 @@ private boolean handleCompression(ChannelHandlerContext ctx, ByteBuf buffer) {
// Need to decompress this packet due to bad order
ChannelHandler decompressor = ctx.pipeline().get("decompress");
//CompressionDecoder
try {
List<?> list = CustomPipelineUtil.callDecode(decompressor, ctx, buffer);
ByteBuf decompressed = (ByteBuf) list.get(0);
if (buffer != decompressed) {
try {
buffer.clear().writeBytes(decompressed);
} finally {
decompressed.release();
}
}
//Relocate handlers
PacketEventsClientDecoder decoder = (PacketEventsClientDecoder) ctx.pipeline().remove(PacketEvents.DECODER_NAME);
ctx.pipeline().addAfter("decompress", PacketEvents.DECODER_NAME, decoder);
PacketEventsClientEncoder encoder = (PacketEventsClientEncoder) ctx.pipeline().remove(PacketEvents.ENCODER_NAME);
ctx.pipeline().addAfter("compress", PacketEvents.ENCODER_NAME, encoder);
checkedCompression = true;
return true;
} catch (InvocationTargetException e) {
e.printStackTrace();
}
List<?> list = CustomPipelineUtil.callDecode((CompressionDecoderMixin) decompressor, ctx, buffer);
ByteBuf decompressed = (ByteBuf) list.get(0);
if (buffer != decompressed) {
try {
buffer.clear().writeBytes(decompressed);
} finally {
decompressed.release();
}
}
//Relocate handlers
PacketEventsClientDecoder decoder = (PacketEventsClientDecoder) ctx.pipeline().remove(PacketEvents.DECODER_NAME);
ctx.pipeline().addAfter("decompress", PacketEvents.DECODER_NAME, decoder);
PacketEventsClientEncoder encoder = (PacketEventsClientEncoder) ctx.pipeline().remove(PacketEvents.ENCODER_NAME);
ctx.pipeline().addAfter("compress", PacketEvents.ENCODER_NAME, encoder);
checkedCompression = true;
return true;
}
return false;
}

private void recompress(ChannelHandlerContext ctx, ByteBuf buffer) {
ByteBuf compressed = ctx.alloc().buffer();
try {
ChannelHandler compressor = ctx.pipeline().get("compress");
CustomPipelineUtil.callEncode(compressor, ctx, buffer, compressed);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
try {
ChannelHandler compressor = ctx.pipeline().get("compress");
CustomPipelineUtil.callEncode((CompressionEncoderMixin) compressor, ctx, buffer, compressed);
try {
buffer.clear().writeBytes(compressed);
PacketEvents.getAPI().getLogManager().debug("Recompressed packet!");
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerDisconnect;
import io.github.retrooper.packetevents.injector.CustomPipelineUtil;
import io.github.retrooper.packetevents.injector.connection.ServerConnectionInitializer;
import io.github.retrooper.packetevents.mixin.CompressionDecoderMixin;
import io.github.retrooper.packetevents.mixin.CompressionEncoderMixin;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
Expand Down Expand Up @@ -143,7 +145,7 @@ private void compress(ChannelHandlerContext ctx, ByteBuf input) throws Invocatio
ByteBuf temp = ctx.alloc().buffer();
try {
if (compressor != null) {
CustomPipelineUtil.callEncode(compressor, ctx, input, temp);
CustomPipelineUtil.callEncode((CompressionEncoderMixin) compressor, ctx, input, temp);
}
} finally {
input.clear().writeBytes(temp);
Expand All @@ -154,7 +156,8 @@ private void compress(ChannelHandlerContext ctx, ByteBuf input) throws Invocatio
private void decompress(ChannelHandlerContext ctx, ByteBuf input, ByteBuf output) throws InvocationTargetException {
ChannelHandler decompressor = ctx.pipeline().get("decompress");
if (decompressor != null) {
ByteBuf temp = (ByteBuf) CustomPipelineUtil.callDecode(decompressor, ctx, input).get(0);
ByteBuf temp = (ByteBuf) CustomPipelineUtil.callDecode(
(CompressionDecoderMixin) decompressor, ctx, input).get(0);
try {
output.clear().writeBytes(temp);
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,86 +1,35 @@
package io.github.retrooper.packetevents.injector;

import com.github.retrooper.packetevents.util.reflection.Reflection;
import io.github.retrooper.packetevents.mixin.CompressionDecoderMixin;
import io.github.retrooper.packetevents.mixin.CompressionEncoderMixin;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class CustomPipelineUtil {
private static Method DECODE_METHOD;
private static Method ENCODE_METHOD;
private static Method MTM_DECODE;
private static Method MTM_ENCODE;

public static void init() {
try {
Class<?> compressionDecoderClass = Reflection.getClassByNameWithoutException("net.minecraft.network.CompressionDecoder");
DECODE_METHOD = compressionDecoderClass.getDeclaredMethod("decode", ChannelHandlerContext.class, ByteBuf.class, List.class);
DECODE_METHOD.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
Class<?> compressionDecoderClass = Reflection.getClassByNameWithoutException("net.minecraft.network.CompressionEncoder");
ENCODE_METHOD = compressionDecoderClass.getDeclaredMethod("encode", ChannelHandlerContext.class, ByteBuf.class, ByteBuf.class);
ENCODE_METHOD.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
Class<?> messageToMessageDecoderClass = Reflection.getClassByNameWithoutException("io.netty.handler.codec.MessageToMessageDecoder");
MTM_DECODE = messageToMessageDecoderClass.getDeclaredMethod("decode", ChannelHandlerContext.class, Object.class, List.class);
MTM_DECODE.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
Class<?> messageToMessageDecoderClass = Reflection.getClassByNameWithoutException("io.netty.handler.codec.MessageToMessageEncoder");
MTM_ENCODE = messageToMessageDecoderClass.getDeclaredMethod("encode", ChannelHandlerContext.class, Object.class, List.class);
MTM_ENCODE.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}

public static List<Object> callDecode(Object decoder, Object ctx, Object input) throws InvocationTargetException {
public static List<Object> callDecode(CompressionDecoderMixin decoder, ChannelHandlerContext ctx, ByteBuf input) {
List<Object> output = new ArrayList<>();
try {
CustomPipelineUtil.DECODE_METHOD.invoke(decoder, ctx, input, output);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
decoder.decode(ctx, input, output);
return output;
}

public static void callEncode(Object encoder, Object ctx, Object msg, Object output) throws InvocationTargetException {
try {
CustomPipelineUtil.ENCODE_METHOD.invoke(encoder, ctx, msg, output);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

public static List<Object> callMTMEncode(Object encoder, Object ctx, Object msg) {
List<Object> output = new ArrayList<>();
try {
MTM_ENCODE.invoke(encoder, ctx, msg, output);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return output;
public static void callEncode(CompressionEncoderMixin encoder, ChannelHandlerContext ctx, ByteBuf msg, ByteBuf output) {
encoder.encode(ctx, msg, output);
}

public static List<Object> callMTMDecode(Object decoder, Object ctx, Object msg) throws InvocationTargetException {
List<Object> output = new ArrayList<>();
try {
MTM_DECODE.invoke(decoder, ctx, msg, output);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return output;
}
}
// public static List<Object> callMTMEncode(MessageToMessageEncoder<?> encoder, ChannelHandlerContext ctx, Object msg) {
// List<Object> output = new ArrayList<>();
// encoder.encode(ctx, msg, output);
// return output;
// }
//
// public static List<Object> callMTMDecode(MessageToMessageDecoder<?> decoder, ChannelHandlerContext ctx, Object msg) {
// List<Object> output = new ArrayList<>();
// decoder.decode(ctx, msg, output);
// return output;
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.retrooper.packetevents.mixin;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import java.util.List;
import net.minecraft.network.CompressionDecoder;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(CompressionDecoder.class)
public interface CompressionDecoderMixin {
@Invoker("decode") void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.retrooper.packetevents.mixin;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import net.minecraft.network.CompressionEncoder;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(CompressionEncoder.class)
public interface CompressionEncoderMixin {
@Invoker("encode") void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.retrooper.packetevents.mixin;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import java.util.List;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(MessageToMessageDecoder.class)
public interface MessageToMessageDecoderMixin {
@Invoker("decode") void decode(ChannelHandlerContext ctx, Object msg, List<Object> out);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.retrooper.packetevents.mixin;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import java.util.List;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(MessageToMessageEncoder.class)
public interface MessageToMessageEncoderMixin {
@Invoker("encode") void encode(ChannelHandlerContext ctx, Object msg, List<Object> out);
}
1 change: 1 addition & 0 deletions fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"mixins": [
"packetevents.mixins.json"
],
"accessWidener": "packetevents.accesswidener",
"depends": {
"fabricloader": ">=0.15.11",
"fabric-api": "*"
Expand Down
4 changes: 4 additions & 0 deletions fabric/src/main/resources/packetevents.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"package": "io.github.retrooper.packetevents.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"CompressionDecoderMixin",
"CompressionEncoderMixin",
"MessageToMessageDecoderMixin",
"MessageToMessageEncoderMixin",
"PacketEventsInjectorMixin"
],
"client": [
Expand Down

0 comments on commit 57f9189

Please sign in to comment.