Skip to content

Commit b30628f

Browse files
committed
Make hex dump more readable, match more packet names
1 parent 9289825 commit b30628f

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

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

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.comphenix.protocol;
1818

19+
import java.io.ByteArrayOutputStream;
1920
import java.io.File;
2021
import java.io.IOException;
2122
import java.text.MessageFormat;
@@ -29,6 +30,7 @@
2930
import java.util.logging.LogRecord;
3031
import java.util.logging.Logger;
3132

33+
import org.apache.commons.io.HexDump;
3234
import org.bukkit.ChatColor;
3335
import org.bukkit.command.Command;
3436
import org.bukkit.command.CommandExecutor;
@@ -41,9 +43,7 @@
4143
import com.comphenix.protocol.events.PacketEvent;
4244
import com.comphenix.protocol.events.PacketListener;
4345
import com.comphenix.protocol.injector.netty.WirePacket;
44-
45-
import io.netty.buffer.ByteBuf;
46-
import io.netty.buffer.ByteBufUtil;
46+
import com.google.common.base.Charsets;
4747

4848
/**
4949
* Logs packets to a given stream
@@ -97,7 +97,22 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
9797
int id = Integer.parseInt(args[2]);
9898
type = PacketType.findCurrent(protocol, pSender, id);
9999
} catch (NumberFormatException ex) {
100-
type = PacketType.findCurrent(protocol, pSender, args[2]);
100+
String name = args[2];
101+
outer: for (PacketType packet : PacketType.values()) {
102+
if (packet.getProtocol() == protocol &&
103+
packet.getSender() == pSender) {
104+
if (packet.name().equalsIgnoreCase(name)) {
105+
type = packet;
106+
break outer;
107+
}
108+
for (String className : packet.getClassNames()) {
109+
if (className.equalsIgnoreCase(name)) {
110+
type = packet;
111+
break outer;
112+
}
113+
}
114+
}
115+
}
101116
}
102117
} catch (IllegalArgumentException ex) {
103118
sender.sendMessage(ChatColor.RED + "Unknown packet: " + PacketType.format(protocol, pSender, args[2]));
@@ -176,9 +191,23 @@ public void onPacketReceiving(PacketEvent event) {
176191
log(event);
177192
}
178193

194+
private static String hexDump(byte[] bytes) throws IOException {
195+
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
196+
HexDump.dump(bytes, 0, output, 0);
197+
return new String(output.toByteArray(), Charsets.UTF_8);
198+
}
199+
}
200+
179201
private void log(PacketEvent event) {
180-
ByteBuf buffer = WirePacket.bufferFromPacket(event.getPacket());
181-
String hexDump = ByteBufUtil.hexDump(buffer);
202+
String hexDump;
203+
204+
try {
205+
WirePacket packet = WirePacket.fromPacket(event.getPacket());
206+
hexDump = hexDump(packet.getBytes());
207+
} catch (Throwable ex) {
208+
fileLogger.log(Level.WARNING, "Failed to dump packet: " + ex.toString());
209+
return;
210+
}
182211

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

0 commit comments

Comments
 (0)