|
16 | 16 | */
|
17 | 17 | package com.comphenix.protocol;
|
18 | 18 |
|
| 19 | +import java.io.ByteArrayOutputStream; |
19 | 20 | import java.io.File;
|
20 | 21 | import java.io.IOException;
|
21 | 22 | import java.text.MessageFormat;
|
|
29 | 30 | import java.util.logging.LogRecord;
|
30 | 31 | import java.util.logging.Logger;
|
31 | 32 |
|
| 33 | +import org.apache.commons.io.HexDump; |
32 | 34 | import org.bukkit.ChatColor;
|
33 | 35 | import org.bukkit.command.Command;
|
34 | 36 | import org.bukkit.command.CommandExecutor;
|
|
41 | 43 | import com.comphenix.protocol.events.PacketEvent;
|
42 | 44 | import com.comphenix.protocol.events.PacketListener;
|
43 | 45 | 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; |
47 | 47 |
|
48 | 48 | /**
|
49 | 49 | * Logs packets to a given stream
|
@@ -97,7 +97,22 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
|
97 | 97 | int id = Integer.parseInt(args[2]);
|
98 | 98 | type = PacketType.findCurrent(protocol, pSender, id);
|
99 | 99 | } 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 | + } |
101 | 116 | }
|
102 | 117 | } catch (IllegalArgumentException ex) {
|
103 | 118 | sender.sendMessage(ChatColor.RED + "Unknown packet: " + PacketType.format(protocol, pSender, args[2]));
|
@@ -176,9 +191,23 @@ public void onPacketReceiving(PacketEvent event) {
|
176 | 191 | log(event);
|
177 | 192 | }
|
178 | 193 |
|
| 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 | + |
179 | 201 | 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 | + } |
182 | 211 |
|
183 | 212 | if (location == LogLocation.FILE) {
|
184 | 213 | fileLogger.log(Level.INFO, event.getPacketType() + ":");
|
|
0 commit comments