From ca76523410496359901bd1897cc377404ba5c9a1 Mon Sep 17 00:00:00 2001 From: Joshua Castle <26531652+Kas-tle@users.noreply.github.com> Date: Sat, 20 Apr 2024 21:59:35 -0700 Subject: [PATCH] Define static serializers Signed-off-by: Joshua Castle <26531652+Kas-tle@users.noreply.github.com> --- .../geysermc/geyser/network/GameProtocol.java | 249 +++++++++++------- 1 file changed, 152 insertions(+), 97 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/network/GameProtocol.java b/core/src/main/java/org/geysermc/geyser/network/GameProtocol.java index c7bbae7a2..a593bd3e4 100644 --- a/core/src/main/java/org/geysermc/geyser/network/GameProtocol.java +++ b/core/src/main/java/org/geysermc/geyser/network/GameProtocol.java @@ -221,91 +221,173 @@ public final class GameProtocol { return joiner.toString(); } + @SuppressWarnings("unchecked") private static BedrockCodec processCodec(BedrockCodec codec) { return codec.toBuilder() // Illegal unused serverbound EDU packets - .updateSerializer(PhotoTransferPacket.class, setIllegalSerializer()) - .updateSerializer(LabTablePacket.class, setIllegalSerializer()) - .updateSerializer(CreatePhotoPacket.class, setIllegalSerializer()) - .updateSerializer(NpcRequestPacket.class, setIllegalSerializer()) - .updateSerializer(PhotoInfoRequestPacket.class, setIllegalSerializer()) + .updateSerializer(PhotoTransferPacket.class, IllegalSerializer.getInstance()) + .updateSerializer(LabTablePacket.class, IllegalSerializer.getInstance()) + .updateSerializer(CreatePhotoPacket.class, IllegalSerializer.getInstance()) + .updateSerializer(NpcRequestPacket.class, IllegalSerializer.getInstance()) + .updateSerializer(PhotoInfoRequestPacket.class, IllegalSerializer.getInstance()) // Illegal unused serverbound packets for featured servers - .updateSerializer(PurchaseReceiptPacket.class, setIllegalSerializer()) + .updateSerializer(PurchaseReceiptPacket.class, IllegalSerializer.getInstance()) // Illegal unused serverbound packets that are deprecated - .updateSerializer(ClientCheatAbilityPacket.class, setIllegalSerializer()) + .updateSerializer(ClientCheatAbilityPacket.class, IllegalSerializer.getInstance()) // Illegal unusued serverbound packets that relate to unused features - .updateSerializer(PlayerAuthInputPacket.class, setIllegalSerializer()) - .updateSerializer(ClientCacheBlobStatusPacket.class, setIllegalSerializer()) - .updateSerializer(SubClientLoginPacket.class, setIllegalSerializer()) - .updateSerializer(SubChunkRequestPacket.class, setIllegalSerializer()) - .updateSerializer(GameTestRequestPacket.class, setIllegalSerializer()) + .updateSerializer(PlayerAuthInputPacket.class, IllegalSerializer.getInstance()) + .updateSerializer(ClientCacheBlobStatusPacket.class, IllegalSerializer.getInstance()) + .updateSerializer(SubClientLoginPacket.class, IllegalSerializer.getInstance()) + .updateSerializer(SubChunkRequestPacket.class, IllegalSerializer.getInstance()) + .updateSerializer(GameTestRequestPacket.class, IllegalSerializer.getInstance()) // Ignored serverbound packets - .updateSerializer(CraftingEventPacket.class, setIgnoredSerializer()) // Make illegal when 1.20.40 is removed - .updateSerializer(ClientToServerHandshakePacket.class, setIgnoredSerializer()) - .updateSerializer(EntityFallPacket.class, setIgnoredSerializer()) - .updateSerializer(MapCreateLockedCopyPacket.class, setIgnoredSerializer()) - .updateSerializer(MapInfoRequestPacket.class, setIgnoredSerializer()) - .updateSerializer(SettingsCommandPacket.class, setIgnoredSerializer()) - .updateSerializer(AnvilDamagePacket.class, setIgnoredSerializer()) + .updateSerializer(CraftingEventPacket.class, IgnoredSerializer.getInstance()) // Make illegal when 1.20.40 is removed + .updateSerializer(ClientToServerHandshakePacket.class, IgnoredSerializer.getInstance()) + .updateSerializer(EntityFallPacket.class, IgnoredSerializer.getInstance()) + .updateSerializer(MapCreateLockedCopyPacket.class, IgnoredSerializer.getInstance()) + .updateSerializer(MapInfoRequestPacket.class, IgnoredSerializer.getInstance()) + .updateSerializer(SettingsCommandPacket.class, IgnoredSerializer.getInstance()) + .updateSerializer(AnvilDamagePacket.class, IgnoredSerializer.getInstance()) // Illegal when serverbound due to Geyser specific setup - .updateSerializer(InventoryContentPacket.class, new InventoryContentSerializer_v407() { - @Override - public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, InventoryContentPacket packet) { - throw new IllegalArgumentException("Client cannot send InventoryContentPacket in server-auth inventory environment!"); - } - }) - .updateSerializer(InventorySlotPacket.class, new InventorySlotSerializer_v407() { - @Override - public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, InventorySlotPacket packet) { - throw new IllegalArgumentException("Client cannot send InventorySlotPacket in server-auth inventory environment!"); - } - }) + .updateSerializer(InventoryContentPacket.class, InventoryContentSerializer.getInstance()) + .updateSerializer(InventorySlotPacket.class, InventorySlotSerializer.getInstance()) // Ignored only when serverbound - .updateSerializer(BossEventPacket.class, new BossEventSerializer_v486() { - @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, BossEventPacket packet) {} - }) - .updateSerializer(MobArmorEquipmentPacket.class, new MobArmorEquipmentSerializer_v291() { - @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, MobArmorEquipmentPacket packet) {} - }) - .updateSerializer(PlayerHotbarPacket.class, new PlayerHotbarSerializer_v291() { - @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, PlayerHotbarPacket packet) {} - }) - .updateSerializer(PlayerSkinPacket.class, new PlayerSkinSerializer_v390() { - @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, PlayerSkinPacket packet) {} - }) - .updateSerializer(SetEntityDataPacket.class, new SetEntityDataSerializer_v557() { - @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, SetEntityDataPacket packet) {} - }) - .updateSerializer(SetEntityMotionPacket.class, new SetEntityMotionSerializer_v662() { - @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, SetEntityMotionPacket packet) {} - }) - .updateSerializer(SetEntityLinkPacket.class, new SetEntityLinkSerializer_v291() { - @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, SetEntityLinkPacket packet) {} - }) + .updateSerializer(BossEventPacket.class, BossEventSerializer.getInstance()) + .updateSerializer(MobArmorEquipmentPacket.class, MobArmorEquipmentSerializer.getInstance()) + .updateSerializer(PlayerHotbarPacket.class, PlayerHotbarSerializer.getInstance()) + .updateSerializer(PlayerSkinPacket.class, PlayerSkinSerializer.getInstance()) + .updateSerializer(SetEntityDataPacket.class, SetEntityDataSerializer.getInstance()) + .updateSerializer(SetEntityMotionPacket.class, SetEntityMotionSerializer.getInstance()) + .updateSerializer(SetEntityLinkPacket.class, SetEntityLinkSerializer.getInstance()) // Valid serverbound packets where reading of some fields can be skipped - .updateSerializer(MobEquipmentPacket.class, new MobEquipmentSerializer_v291() { - @Override - public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, MobEquipmentPacket packet) { - packet.setRuntimeEntityId(VarInts.readUnsignedLong(buffer)); - fakeItemRead(buffer); - packet.setInventorySlot(buffer.readUnsignedByte()); - packet.setHotbarSlot(buffer.readUnsignedByte()); - packet.setContainerId(buffer.readByte()); - } - }) + .updateSerializer(MobEquipmentPacket.class, MobEquipmentSerializer.getInstance()) // // Illegal bidirectional packets - .updateSerializer(DebugInfoPacket.class, setIllegalSerializer()) - .updateSerializer(EditorNetworkPacket.class, setIllegalSerializer()) - .updateSerializer(ScriptMessagePacket.class, setIllegalSerializer()) + .updateSerializer(DebugInfoPacket.class, IllegalSerializer.getInstance()) + .updateSerializer(EditorNetworkPacket.class, IllegalSerializer.getInstance()) + .updateSerializer(ScriptMessagePacket.class, IllegalSerializer.getInstance()) // // Ignored bidirectional packets - .updateSerializer(ClientCacheStatusPacket.class, setIgnoredSerializer()) - .updateSerializer(DisconnectPacket.class, setIgnoredSerializer()) - .updateSerializer(SimpleEventPacket.class, setIgnoredSerializer()) - .updateSerializer(TickSyncPacket.class, setIgnoredSerializer()) - .updateSerializer(MultiplayerSettingsPacket.class, setIgnoredSerializer()) + .updateSerializer(ClientCacheStatusPacket.class, IgnoredSerializer.getInstance()) + .updateSerializer(DisconnectPacket.class, IgnoredSerializer.getInstance()) + .updateSerializer(SimpleEventPacket.class, IgnoredSerializer.getInstance()) + .updateSerializer(TickSyncPacket.class, IgnoredSerializer.getInstance()) + .updateSerializer(MultiplayerSettingsPacket.class, IgnoredSerializer.getInstance()) .build(); } + @SuppressWarnings("rawtypes") + private static class IllegalSerializer implements BedrockPacketSerializer { + private static final IllegalSerializer INSTANCE = new IllegalSerializer(); + private IllegalSerializer() {} + public static IllegalSerializer getInstance() { return INSTANCE; } + + @Override public void serialize(ByteBuf buffer, BedrockCodecHelper helper, T packet) { + throw new IllegalArgumentException("Server tried to send unused packet " + packet.getClass().getSimpleName() + "!"); + } + @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, T packet) { + throw new IllegalArgumentException("Client tried to send unused packet " + packet.getClass().getSimpleName() + "!"); + } + } + + @SuppressWarnings("rawtypes") + private static class IgnoredSerializer implements BedrockPacketSerializer { + private static final IgnoredSerializer INSTANCE = new IgnoredSerializer(); + private IgnoredSerializer() {} + public static IgnoredSerializer getInstance() { return INSTANCE; } + + @Override public void serialize(ByteBuf buffer, BedrockCodecHelper helper, T packet) {} + @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, T packet) {} + } + + private static class InventoryContentSerializer extends InventoryContentSerializer_v407 { + private static final InventoryContentSerializer INSTANCE = new InventoryContentSerializer(); + private InventoryContentSerializer() {} + public static InventoryContentSerializer getInstance() { return INSTANCE; } + + @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, InventoryContentPacket packet) { + throw new IllegalArgumentException("Client cannot send InventoryContentPacket in server-auth inventory environment!"); + } + } + + private static class InventorySlotSerializer extends InventorySlotSerializer_v407 { + private static final InventorySlotSerializer INSTANCE = new InventorySlotSerializer(); + private InventorySlotSerializer() {} + public static InventorySlotSerializer getInstance() { return INSTANCE; } + + @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, InventorySlotPacket packet) { + throw new IllegalArgumentException("Client cannot send InventorySlotPacket in server-auth inventory environment!"); + } + } + + private static class BossEventSerializer extends BossEventSerializer_v486 { + private static final BossEventSerializer INSTANCE = new BossEventSerializer(); + private BossEventSerializer() {} + public static BossEventSerializer getInstance() { return INSTANCE; } + + @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, BossEventPacket packet) {} + } + + private static class MobArmorEquipmentSerializer extends MobArmorEquipmentSerializer_v291 { + private static final MobArmorEquipmentSerializer INSTANCE = new MobArmorEquipmentSerializer(); + private MobArmorEquipmentSerializer() {} + public static MobArmorEquipmentSerializer getInstance() { return INSTANCE; } + + @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, MobArmorEquipmentPacket packet) {} + } + + private static class PlayerHotbarSerializer extends PlayerHotbarSerializer_v291 { + private static final PlayerHotbarSerializer INSTANCE = new PlayerHotbarSerializer(); + private PlayerHotbarSerializer() {} + public static PlayerHotbarSerializer getInstance() { return INSTANCE; } + + @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, PlayerHotbarPacket packet) {} + } + + private static class PlayerSkinSerializer extends PlayerSkinSerializer_v390 { + private static final PlayerSkinSerializer INSTANCE = new PlayerSkinSerializer(); + private PlayerSkinSerializer() {} + public static PlayerSkinSerializer getInstance() { return INSTANCE; } + + @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, PlayerSkinPacket packet) {} + } + + private static class SetEntityDataSerializer extends SetEntityDataSerializer_v557 { + private static final SetEntityDataSerializer INSTANCE = new SetEntityDataSerializer(); + private SetEntityDataSerializer() {} + public static SetEntityDataSerializer getInstance() { return INSTANCE; } + + @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, SetEntityDataPacket packet) {} + } + + private static class SetEntityMotionSerializer extends SetEntityMotionSerializer_v662 { + private static final SetEntityMotionSerializer INSTANCE = new SetEntityMotionSerializer(); + private SetEntityMotionSerializer() {} + public static SetEntityMotionSerializer getInstance() { return INSTANCE; } + + @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, SetEntityMotionPacket packet) {} + } + + private static class SetEntityLinkSerializer extends SetEntityLinkSerializer_v291 { + private static final SetEntityLinkSerializer INSTANCE = new SetEntityLinkSerializer(); + private SetEntityLinkSerializer() {} + public static SetEntityLinkSerializer getInstance() { return INSTANCE; } + + @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, SetEntityLinkPacket packet) {} + } + + private static class MobEquipmentSerializer extends MobEquipmentSerializer_v291 { + private static final MobEquipmentSerializer INSTANCE = new MobEquipmentSerializer(); + private MobEquipmentSerializer() {} + public static MobEquipmentSerializer getInstance() { return INSTANCE; } + + @Override public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, MobEquipmentPacket packet) { + packet.setRuntimeEntityId(VarInts.readUnsignedLong(buffer)); + fakeItemRead(buffer); + packet.setInventorySlot(buffer.readUnsignedByte()); + packet.setHotbarSlot(buffer.readUnsignedByte()); + packet.setContainerId(buffer.readByte()); + } + } + /** * Fake reading an item from the buffer to improve performance. * @@ -328,33 +410,6 @@ public final class GameProtocol { buffer.skipBytes(streamSize); } - private static BedrockPacketSerializer setIllegalSerializer() { - return new BedrockPacketSerializer() { - @Override - public void serialize(ByteBuf buffer, BedrockCodecHelper helper, T packet) { - throw new IllegalArgumentException("Server tried to send unused packet " + packet.getClass().getSimpleName() + "!"); - } - - @Override - public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, T packet) { - throw new IllegalArgumentException("Client tried to send unused packet " + packet.getClass().getSimpleName() + "!"); - } - }; - } - - private static BedrockPacketSerializer setIgnoredSerializer() { - return new BedrockPacketSerializer() { - @Override - public void serialize(ByteBuf buffer, BedrockCodecHelper helper, BedrockPacket packet) { - } - - @Override - public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, BedrockPacket packet) { - } - }; - } - - private GameProtocol() { } }