Skip to content

Commit

Permalink
Add reserialize packet option
Browse files Browse the repository at this point in the history
  • Loading branch information
OroArmor committed Apr 3, 2024
1 parent 30da53b commit 7b04bbf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public final class NetworkingImpl {
*/
public static final Identifier EARLY_REGISTRATION_CHANNEL_FABRIC = new Identifier("fabric-networking-api-v1", "early_registration");

/**
* Forces reserialization of packets.
*/
// TODO: Remove for 1.20.5. This is done there already.
public static final boolean RESERIALIZE_CUSTOM_PAYLOADS = Boolean.parseBoolean(System.getProperty("quilt.networking.reserialize_custom_payloads"));

public static void init(ModContainer mod) {
// Login setup
ServerLoginConnectionEvents.QUERY_START.register((handler, server, sender, synchronizer) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

Expand All @@ -34,6 +35,7 @@
import net.minecraft.util.Identifier;

import org.quiltmc.qsl.networking.api.PacketByteBufs;
import org.quiltmc.qsl.networking.impl.NetworkingImpl;
import org.quiltmc.qsl.networking.impl.payload.PacketByteBufPayload;

@Mixin(CustomPayloadC2SPacket.class)
Expand All @@ -54,4 +56,15 @@ private static void inject(Identifier id, PacketByteBuf buf, CallbackInfoReturna
cir.setReturnValue(new PacketByteBufPayload(id, copied));
buf.skipBytes(buf.readableBytes());
}

@ModifyArg(method = "apply(Lnet/minecraft/network/listener/ServerCommonPacketListener;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/listener/ServerCommonPacketListener;onCustomPayload(Lnet/minecraft/network/packet/c2s/common/CustomPayloadC2SPacket;)V"))
public CustomPayloadC2SPacket reserialize(CustomPayloadC2SPacket packet) {
if (NetworkingImpl.RESERIALIZE_CUSTOM_PAYLOADS) {
PacketByteBuf buf = PacketByteBufs.create();
packet.write(buf);
return new CustomPayloadC2SPacket(buf);
}

return packet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@
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.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket;
import net.minecraft.network.packet.payload.CustomPayload;
import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket;
import net.minecraft.util.Identifier;

import org.quiltmc.qsl.networking.api.PacketByteBufs;
import org.quiltmc.qsl.networking.impl.NetworkingImpl;
import org.quiltmc.qsl.networking.impl.payload.PacketByteBufPayload;

@Mixin(CustomPayloadS2CPacket.class)
Expand All @@ -54,4 +57,15 @@ private static void inject(Identifier id, PacketByteBuf buf, CallbackInfoReturna
cir.setReturnValue(new PacketByteBufPayload(id, copied));
buf.skipBytes(buf.readableBytes());
}

@ModifyArg(method = "apply(Lnet/minecraft/network/listener/ClientCommonPacketListener;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/listener/ClientCommonPacketListener;onCustomPayload(Lnet/minecraft/network/packet/s2c/common/CustomPayloadS2CPacket;)V"))
public CustomPayloadS2CPacket reserialize(CustomPayloadS2CPacket packet) {
if (NetworkingImpl.RESERIALIZE_CUSTOM_PAYLOADS) {
PacketByteBuf buf = PacketByteBufs.create();
packet.write(buf);
return new CustomPayloadS2CPacket(buf);
}

return packet;
}
}

0 comments on commit 7b04bbf

Please sign in to comment.