Skip to content
This repository was archived by the owner on May 21, 2024. It is now read-only.

Commit 5ec87c9

Browse files
committed
Fix issues with logging custom payload packets
1 parent 2205ce6 commit 5ec87c9

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,29 +150,52 @@ private static byte[] getBytes(ByteBuf buffer) {
150150
*/
151151
public static WirePacket fromPacket(PacketContainer packet) {
152152
int id = packet.getType().getCurrentId();
153-
return new WirePacket(id, getBytes(bufferFromPacket(packet)));
153+
return new WirePacket(id, bytesFromPacket(packet));
154154
}
155155

156156
/**
157-
* Creates a ByteBuf from an existing PacketContainer containing all the
157+
* Creates a byte array from an existing PacketContainer containing all the
158158
* bytes from that packet
159159
*
160160
* @param packet Existing packet
161161
* @return The ByteBuf
162162
*/
163-
public static ByteBuf bufferFromPacket(PacketContainer packet) {
163+
public static byte[] bytesFromPacket(PacketContainer packet) {
164164
checkNotNull(packet, "packet cannot be null!");
165-
165+
166166
ByteBuf buffer = PacketContainer.createPacketBuffer();
167+
ByteBuf store = PacketContainer.createPacketBuffer();
168+
169+
// Read the bytes once
167170
Method write = MinecraftMethods.getPacketWriteByteBufMethod();
168171

169172
try {
170173
write.invoke(packet.getHandle(), buffer);
171174
} catch (ReflectiveOperationException ex) {
172-
throw new RuntimeException("Failed to serialize packet contents.", ex);
175+
throw new RuntimeException("Failed to read packet contents.", ex);
173176
}
174177

175-
return buffer;
178+
byte[] bytes = getBytes(buffer);
179+
180+
// Rewrite them to the packet to avoid issues with certain packets
181+
if (packet.getType() == PacketType.Play.Server.CUSTOM_PAYLOAD
182+
|| packet.getType() == PacketType.Play.Client.CUSTOM_PAYLOAD) {
183+
// Make a copy of the array before writing
184+
byte[] ret = Arrays.copyOf(bytes, bytes.length);
185+
store.writeBytes(bytes);
186+
187+
Method read = MinecraftMethods.getPacketReadByteBufMethod();
188+
189+
try {
190+
read.invoke(packet.getHandle(), store);
191+
} catch (ReflectiveOperationException ex) {
192+
throw new RuntimeException("Failed to rewrite packet contents.", ex);
193+
}
194+
195+
return ret;
196+
}
197+
198+
return bytes;
176199
}
177200

178201
/**

modules/ProtocolLib/src/main/java/com/comphenix/protocol/PacketLogging.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
9494
}
9595

9696
try {
97-
try {
97+
try { // Try IDs first
9898
int id = Integer.parseInt(args[2]);
9999
type = PacketType.findCurrent(protocol, pSender, id);
100-
} catch (NumberFormatException ex) {
100+
} catch (NumberFormatException ex) { // Check packet names
101101
String name = args[2];
102102
outer: for (PacketType packet : PacketType.values()) {
103103
if (packet.getProtocol() == protocol && packet.getSender() == pSender) {
@@ -114,7 +114,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
114114
}
115115
}
116116
}
117-
} catch (IllegalArgumentException ex) {
117+
} catch (IllegalArgumentException ex) { // RIP
118118
type = null;
119119
}
120120

@@ -168,6 +168,7 @@ private void startLogging() {
168168
this.sendingWhitelist = ListeningWhitelist.newBuilder().types(sendingTypes).build();
169169
this.receivingWhitelist = ListeningWhitelist.newBuilder().types(receivingTypes).build();
170170

171+
// Setup the file logger if it hasn't been already
171172
if (location == LogLocation.FILE && fileLogger == null) {
172173
fileLogger = Logger.getLogger("ProtocolLib-FileLogging");
173174

@@ -199,6 +200,8 @@ public void onPacketReceiving(PacketEvent event) {
199200
log(event);
200201
}
201202

203+
// Here's where the magic happens
204+
202205
private static String hexDump(byte[] bytes) throws IOException {
203206
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
204207
HexDump.dump(bytes, 0, output, 0);
@@ -208,8 +211,8 @@ private static String hexDump(byte[] bytes) throws IOException {
208211

209212
private void log(PacketEvent event) {
210213
try {
211-
WirePacket packet = WirePacket.fromPacket(event.getPacket());
212-
String hexDump = hexDump(packet.getBytes());
214+
byte[] bytes = WirePacket.bytesFromPacket(event.getPacket());
215+
String hexDump = hexDump(bytes);
213216

214217
if (location == LogLocation.FILE) {
215218
fileLogger.log(Level.INFO, event.getPacketType() + ":");

0 commit comments

Comments
 (0)