From 50bfc8670697ed96f1db34a731ef70032b6e12e5 Mon Sep 17 00:00:00 2001 From: Myles Date: Tue, 9 Feb 2021 16:32:31 +0000 Subject: [PATCH] Revert "The bytebuf doesn't need to be rewritten" This reverts commit ac877d0b1a1541c8447789ab237f616d1bcd8abf. --- .../bukkit/handlers/BukkitDecodeHandler.java | 28 ++++++++++++------- .../sponge/handlers/SpongeDecodeHandler.java | 28 ++++++++++++------- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/handlers/BukkitDecodeHandler.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/handlers/BukkitDecodeHandler.java index 2650e5f77..3930581f9 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/handlers/BukkitDecodeHandler.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/handlers/BukkitDecodeHandler.java @@ -31,17 +31,25 @@ public class BukkitDecodeHandler extends ByteToMessageDecoder { throw CancelDecoderException.generate(null); } - if (info.shouldTransformPacket()) { - info.transformIncoming(bytebuf, CancelDecoderException::generate); - } - + ByteBuf transformedBuf = null; try { - list.addAll(PipelineUtil.callDecode(this.minecraftDecoder, ctx, bytebuf)); - } catch (InvocationTargetException e) { - if (e.getCause() instanceof Exception) { - throw (Exception) e.getCause(); - } else if (e.getCause() instanceof Error) { - throw (Error) e.getCause(); + if (info.shouldTransformPacket()) { + transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); + info.transformIncoming(transformedBuf, CancelDecoderException::generate); + } + + try { + list.addAll(PipelineUtil.callDecode(this.minecraftDecoder, ctx, transformedBuf == null ? bytebuf : transformedBuf)); + } catch (InvocationTargetException e) { + if (e.getCause() instanceof Exception) { + throw (Exception) e.getCause(); + } else if (e.getCause() instanceof Error) { + throw (Error) e.getCause(); + } + } + } finally { + if (transformedBuf != null) { + transformedBuf.release(); } } } diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/SpongeDecodeHandler.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/SpongeDecodeHandler.java index 387954b46..975a62896 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/SpongeDecodeHandler.java +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/SpongeDecodeHandler.java @@ -28,17 +28,25 @@ public class SpongeDecodeHandler extends ByteToMessageDecoder { throw CancelDecoderException.generate(null); } - if (info.shouldTransformPacket()) { - info.transformIncoming(bytebuf, CancelDecoderException::generate); - } - + ByteBuf transformedBuf = null; try { - list.addAll(PipelineUtil.callDecode(this.minecraftDecoder, ctx, bytebuf)); - } catch (InvocationTargetException e) { - if (e.getCause() instanceof Exception) { - throw (Exception) e.getCause(); - } else if (e.getCause() instanceof Error) { - throw (Error) e.getCause(); + if (info.shouldTransformPacket()) { + transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); + info.transformIncoming(transformedBuf, CancelDecoderException::generate); + } + + try { + list.addAll(PipelineUtil.callDecode(this.minecraftDecoder, ctx, transformedBuf == null ? bytebuf : transformedBuf)); + } catch (InvocationTargetException e) { + if (e.getCause() instanceof Exception) { + throw (Exception) e.getCause(); + } else if (e.getCause() instanceof Error) { + throw (Error) e.getCause(); + } + } + } finally { + if (transformedBuf != null) { + transformedBuf.release(); } } }