Skip to content

Commit 679035c

Browse files
committed
Fix an error with wire packets
Addresses #224
1 parent bd4352f commit 679035c

File tree

2 files changed

+120
-12
lines changed

2 files changed

+120
-12
lines changed

modules/API/src/main/java/com/comphenix/protocol/injector/netty/WirePacket.java

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121

2222
import java.lang.reflect.Method;
23+
import java.util.Arrays;
2324

2425
import com.comphenix.protocol.PacketType;
2526
import com.comphenix.protocol.events.PacketContainer;
@@ -78,16 +79,7 @@ public byte[] getBytes() {
7879
* @param output Output to write to
7980
*/
8081
public void writeId(ByteBuf output) {
81-
checkNotNull(output, "output cannot be null!");
82-
// From PacketDataSerializer#d(int)
83-
84-
int i = id;
85-
while ((i & -128) != 0) {
86-
output.writeByte(i & 127 | 128);
87-
i >>>= 7;
88-
}
89-
90-
output.writeByte(i);
82+
writeVarInt(output, id);
9183
}
9284

9385
/**
@@ -118,6 +110,39 @@ public ByteBuf serialize() {
118110
return buffer;
119111
}
120112

113+
@Override
114+
public boolean equals(Object obj) {
115+
if (this == obj) return true;
116+
117+
if (obj instanceof WirePacket) {
118+
WirePacket that = (WirePacket) obj;
119+
return this.id == that.id &&
120+
Arrays.equals(this.bytes, that.bytes);
121+
}
122+
123+
return false;
124+
}
125+
126+
@Override
127+
public int hashCode() {
128+
final int prime = 31;
129+
int result = 1;
130+
result = prime * result + Arrays.hashCode(bytes);
131+
result = prime * result + id;
132+
return result;
133+
}
134+
135+
@Override
136+
public String toString() {
137+
return "WirePacket[id=" + id + ", bytes=" + Arrays.toString(bytes) + "]";
138+
}
139+
140+
private static byte[] getBytes(ByteBuf buffer) {
141+
byte[] array = new byte[buffer.readableBytes()];
142+
buffer.readBytes(array);
143+
return array;
144+
}
145+
121146
/**
122147
* Creates a WirePacket from an existing PacketContainer
123148
* @param packet Existing packet
@@ -137,7 +162,7 @@ public static WirePacket fromPacket(PacketContainer packet) {
137162
throw new RuntimeException("Failed to serialize packet contents.", ex);
138163
}
139164

140-
return new WirePacket(id, buffer.array());
165+
return new WirePacket(id, getBytes(buffer));
141166
}
142167

143168
/**
@@ -162,6 +187,36 @@ public static WirePacket fromPacket(Object packet) {
162187
throw new RuntimeException("Failed to serialize packet contents.", ex);
163188
}
164189

165-
return new WirePacket(id, buffer.array());
190+
return new WirePacket(id, getBytes(buffer));
191+
}
192+
193+
public static void writeVarInt(ByteBuf output, int i) {
194+
checkNotNull(output, "output cannot be null!");
195+
196+
while ((i & -128) != 0) {
197+
output.writeByte(i & 127 | 128);
198+
i >>>= 7;
199+
}
200+
201+
output.writeByte(i);
202+
}
203+
204+
public static int readVarInt(ByteBuf input) {
205+
checkNotNull(input, "input cannot be null!");
206+
207+
int i = 0;
208+
int j = 0;
209+
210+
byte b0;
211+
212+
do {
213+
b0 = input.readByte();
214+
i |= (b0 & 127) << j++ * 7;
215+
if (j > 5) {
216+
throw new RuntimeException("VarInt too big");
217+
}
218+
} while ((b0 & 128) == 128);
219+
220+
return i;
166221
}
167222
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* (c) 2016 dmulloy2
3+
*/
4+
package com.comphenix.protocol.injector;
5+
6+
import static org.junit.Assert.assertArrayEquals;
7+
import static org.junit.Assert.assertEquals;
8+
9+
import org.junit.BeforeClass;
10+
import org.junit.Test;
11+
12+
import com.comphenix.protocol.BukkitInitialization;
13+
import com.comphenix.protocol.PacketType;
14+
import com.comphenix.protocol.events.PacketContainer;
15+
import com.comphenix.protocol.injector.netty.WirePacket;
16+
17+
import io.netty.buffer.ByteBuf;
18+
19+
/**
20+
* @author dmulloy2
21+
*/
22+
public class WirePacketTest {
23+
24+
@BeforeClass
25+
public static void beforeClass() {
26+
BukkitInitialization.initializePackage();
27+
}
28+
29+
@Test
30+
public void testPackets() {
31+
PacketContainer packet = new PacketContainer(PacketType.Play.Server.CHAT);
32+
WirePacket wire = WirePacket.fromPacket(packet);
33+
WirePacket handle = WirePacket.fromPacket(packet.getHandle());
34+
assertEquals(wire, handle);
35+
}
36+
37+
@Test
38+
public void testSerialization() {
39+
int id = 42;
40+
byte[] array = { 1, 3, 7, 21, 88, 67, 8 };
41+
42+
WirePacket packet = new WirePacket(id, array);
43+
44+
ByteBuf buf = packet.serialize();
45+
46+
int backId = WirePacket.readVarInt(buf);
47+
byte[] backArray = new byte[buf.readableBytes()];
48+
buf.readBytes(backArray);
49+
50+
assertEquals(id, backId);
51+
assertArrayEquals(array, backArray);
52+
}
53+
}

0 commit comments

Comments
 (0)