From 7518f8b98bcebd9a19ecb80a03bdb2f129fe02fb Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Tue, 24 Jul 2018 13:06:35 -0300 Subject: [PATCH 1/2] Replace Unpooled.buffer() (cherry picked from commit e9159cb2def9088cfefa0da5593c0456c5a830dc) --- .../us/myles/ViaVersion/api/PacketWrapper.java | 7 +++---- .../types/Chunk1_13Type.java | 3 +-- .../chunks/ChunkSection1_9_3_4.java | 16 +++++++++------- .../types/Chunk1_9_3_4Type.java | 3 +-- .../chunks/ChunkSection1_9_1_2.java | 16 +++++++++------- .../types/Chunk1_9_1_2Type.java | 3 +-- .../chunks/ChunkSection1_9to1_8.java | 16 +++++++++------- .../protocol1_9to1_8/packets/WorldPackets.java | 3 +-- .../protocol1_9to1_8/types/ChunkType.java | 3 +-- 9 files changed, 35 insertions(+), 35 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java b/common/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java index 6421495e8..763167c10 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java +++ b/common/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java @@ -2,7 +2,6 @@ package us.myles.ViaVersion.api; import com.google.common.base.Preconditions; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import lombok.Getter; import lombok.Setter; @@ -331,7 +330,7 @@ public class PacketWrapper { // Apply other protocols apply(Direction.OUTGOING, user().get(ProtocolInfo.class).getState(), index, protocols); // Send - ByteBuf output = inputBuffer == null ? Unpooled.buffer() : inputBuffer.alloc().buffer(); + ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer(); writeToBuffer(output); return output; @@ -379,7 +378,7 @@ public class PacketWrapper { public void send() throws Exception { if (!isCancelled()) { // Send - ByteBuf output = inputBuffer == null ? Unpooled.buffer() : inputBuffer.alloc().buffer(); + ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer(); writeToBuffer(output); user().sendRawPacket(output); } @@ -474,7 +473,7 @@ public class PacketWrapper { */ public void sendToServer() throws Exception { if (!isCancelled()) { - ByteBuf output = inputBuffer == null ? Unpooled.buffer() : inputBuffer.alloc().buffer(); + ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer(); writeToBuffer(output); user().getChannel().pipeline().context(Via.getManager().getInjector().getDecoderName()).fireChannelRead(output); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/types/Chunk1_13Type.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/types/Chunk1_13Type.java index ba1247c84..380200cb4 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/types/Chunk1_13Type.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/types/Chunk1_13Type.java @@ -2,7 +2,6 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.types; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.minecraft.Environment; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; @@ -83,7 +82,7 @@ public class Chunk1_13Type extends PartialType { output.writeBoolean(chunk.isGroundUp()); Type.VAR_INT.write(output, chunk.getBitmask()); - ByteBuf buf = Unpooled.buffer(); + ByteBuf buf = output.alloc().buffer(); for (int i = 0; i < 16; i++) { ChunkSection section = chunk.getSections()[i]; if (section == null) continue; // Section not set diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/chunks/ChunkSection1_9_3_4.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/chunks/ChunkSection1_9_3_4.java index 8539f0f14..d822c8475 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/chunks/ChunkSection1_9_3_4.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/chunks/ChunkSection1_9_3_4.java @@ -2,7 +2,6 @@ package us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.chunks; import com.google.common.collect.Lists; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.minecraft.chunks.NibbleArray; import us.myles.ViaVersion.api.type.Type; @@ -351,12 +350,15 @@ public class ChunkSection1_9_3_4 implements ChunkSection { private int countBytes(int value) throws Exception { // Count amount of bytes that would be sent if the value were sent as a VarInt - ByteBuf buf = Unpooled.buffer(); - Type.VAR_INT.write(buf, value); - buf.readerIndex(0); - int bitCount = buf.readableBytes(); - buf.release(); - return bitCount; + if ((value & (~0 << 7)) == 0) + return 1; + if ((value & (~0 << 14)) == 0) + return 2; + if ((value & (~0 << 21)) == 0) + return 3; + if ((value & (~0 << 28)) == 0) + return 4; + return 5; } @Override diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/types/Chunk1_9_3_4Type.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/types/Chunk1_9_3_4Type.java index 194356017..58f563f05 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/types/Chunk1_9_3_4Type.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/types/Chunk1_9_3_4Type.java @@ -2,7 +2,6 @@ package us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.types; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.minecraft.Environment; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; @@ -81,7 +80,7 @@ public class Chunk1_9_3_4Type extends PartialType { output.writeBoolean(chunk.isGroundUp()); Type.VAR_INT.write(output, chunk.getBitmask()); - ByteBuf buf = Unpooled.buffer(); + ByteBuf buf = output.alloc().buffer(); for (int i = 0; i < 16; i++) { ChunkSection section = chunk.getSections()[i]; if (section == null) continue; // Section not set diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/chunks/ChunkSection1_9_1_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/chunks/ChunkSection1_9_1_2.java index fff7980bd..c0c383b4f 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/chunks/ChunkSection1_9_1_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/chunks/ChunkSection1_9_1_2.java @@ -2,7 +2,6 @@ package us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.chunks; import com.google.common.collect.Lists; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.minecraft.chunks.NibbleArray; import us.myles.ViaVersion.api.type.Type; @@ -348,11 +347,14 @@ public class ChunkSection1_9_1_2 implements ChunkSection { private int countBytes(int value) throws Exception { // Count amount of bytes that would be sent if the value were sent as a VarInt - ByteBuf buf = Unpooled.buffer(); - Type.VAR_INT.write(buf, value); - buf.readerIndex(0); - int bitCount = buf.readableBytes(); - buf.release(); - return bitCount; + if ((value & (~0 << 7)) == 0) + return 1; + if ((value & (~0 << 14)) == 0) + return 2; + if ((value & (~0 << 21)) == 0) + return 3; + if ((value & (~0 << 28)) == 0) + return 4; + return 5; } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/types/Chunk1_9_1_2Type.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/types/Chunk1_9_1_2Type.java index 6fbfafbf8..621c303ac 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/types/Chunk1_9_1_2Type.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/types/Chunk1_9_1_2Type.java @@ -2,7 +2,6 @@ package us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.types; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.minecraft.Environment; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; @@ -79,7 +78,7 @@ public class Chunk1_9_1_2Type extends PartialType { output.writeBoolean(chunk.isGroundUp()); Type.VAR_INT.write(output, chunk.getBitmask()); - ByteBuf buf = Unpooled.buffer(); + ByteBuf buf = output.alloc().buffer(); for (int i = 0; i < 16; i++) { ChunkSection section = chunk.getSections()[i]; if (section == null) continue; // Section not set diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/chunks/ChunkSection1_9to1_8.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/chunks/ChunkSection1_9to1_8.java index 72a48578e..e77518363 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/chunks/ChunkSection1_9to1_8.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/chunks/ChunkSection1_9to1_8.java @@ -2,7 +2,6 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.chunks; import com.google.common.collect.Lists; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.minecraft.chunks.NibbleArray; import us.myles.ViaVersion.api.type.Type; @@ -251,11 +250,14 @@ public class ChunkSection1_9to1_8 implements ChunkSection { private int countBytes(int value) throws Exception { // Count amount of bytes that would be sent if the value were sent as a VarInt - ByteBuf buf = Unpooled.buffer(); - Type.VAR_INT.write(buf, value); - buf.readerIndex(0); - int bitCount = buf.readableBytes(); - buf.release(); - return bitCount; + if ((value & (~0 << 7)) == 0) + return 1; + if ((value & (~0 << 14)) == 0) + return 2; + if ((value & (~0 << 21)) == 0) + return 3; + if ((value & (~0 << 28)) == 0) + return 4; + return 5; } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java index 37670ed9b..48595f269 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java @@ -4,7 +4,6 @@ import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.google.common.base.Optional; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.minecraft.Position; @@ -158,7 +157,7 @@ public class WorldPackets { throw new IOException("transformMapChunkBulk returned the wrong object type"); PacketWrapper output = (PacketWrapper) obj; - ByteBuf buffer = Unpooled.buffer(); + ByteBuf buffer = wrapper.user().getChannel().alloc().buffer(); output.setId(-1); // -1 for no writing of id output.writeToBuffer(buffer); PacketWrapper chunkPacket = new PacketWrapper(0x21, buffer, wrapper.user()); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/ChunkType.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/ChunkType.java index fe6934e40..eda3efd5b 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/ChunkType.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/ChunkType.java @@ -2,7 +2,6 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.types; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.type.PartialType; @@ -154,7 +153,7 @@ public class ChunkType extends PartialType { output.writeByte(chunk.isGroundUp() ? 0x01 : 0x00); Type.VAR_INT.write(output, chunk.getPrimaryBitmask()); - ByteBuf buf = Unpooled.buffer(); + ByteBuf buf = output.alloc().buffer(); for (int i = 0; i < SECTION_COUNT; i++) { ChunkSection1_9to1_8 section = chunk.getSections()[i]; if (section == null) continue; // Section not set From e9aa18fd6b72e2d2b0bbca99fd898a04d964ae7d Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Sun, 29 Jul 2018 09:34:27 -0300 Subject: [PATCH 2/2] Fix memory leak --- .../protocols/protocol1_9to1_8/packets/WorldPackets.java | 1 + 1 file changed, 1 insertion(+) diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java index 48595f269..8a60e7b57 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java @@ -162,6 +162,7 @@ public class WorldPackets { output.writeToBuffer(buffer); PacketWrapper chunkPacket = new PacketWrapper(0x21, buffer, wrapper.user()); chunkPacket.send(Protocol1_9TO1_8.class, false, true); + buffer.release(); } } });