Fix issues with logging custom payload packets

This commit is contained in:
Dan Mulloy 2017-03-20 21:42:46 -04:00
parent 2205ce639e
commit 5ec87c9d76
2 changed files with 37 additions and 11 deletions

View File

@ -150,29 +150,52 @@ public class WirePacket {
*/
public static WirePacket fromPacket(PacketContainer packet) {
int id = packet.getType().getCurrentId();
return new WirePacket(id, getBytes(bufferFromPacket(packet)));
return new WirePacket(id, bytesFromPacket(packet));
}
/**
* Creates a ByteBuf from an existing PacketContainer containing all the
* Creates a byte array from an existing PacketContainer containing all the
* bytes from that packet
*
* @param packet Existing packet
* @return The ByteBuf
*/
public static ByteBuf bufferFromPacket(PacketContainer packet) {
public static byte[] bytesFromPacket(PacketContainer packet) {
checkNotNull(packet, "packet cannot be null!");
ByteBuf buffer = PacketContainer.createPacketBuffer();
ByteBuf store = PacketContainer.createPacketBuffer();
// Read the bytes once
Method write = MinecraftMethods.getPacketWriteByteBufMethod();
try {
write.invoke(packet.getHandle(), buffer);
} catch (ReflectiveOperationException ex) {
throw new RuntimeException("Failed to serialize packet contents.", ex);
throw new RuntimeException("Failed to read packet contents.", ex);
}
return buffer;
byte[] bytes = getBytes(buffer);
// Rewrite them to the packet to avoid issues with certain packets
if (packet.getType() == PacketType.Play.Server.CUSTOM_PAYLOAD
|| packet.getType() == PacketType.Play.Client.CUSTOM_PAYLOAD) {
// Make a copy of the array before writing
byte[] ret = Arrays.copyOf(bytes, bytes.length);
store.writeBytes(bytes);
Method read = MinecraftMethods.getPacketReadByteBufMethod();
try {
read.invoke(packet.getHandle(), store);
} catch (ReflectiveOperationException ex) {
throw new RuntimeException("Failed to rewrite packet contents.", ex);
}
return ret;
}
return bytes;
}
/**

View File

@ -94,10 +94,10 @@ public class PacketLogging implements CommandExecutor, PacketListener {
}
try {
try {
try { // Try IDs first
int id = Integer.parseInt(args[2]);
type = PacketType.findCurrent(protocol, pSender, id);
} catch (NumberFormatException ex) {
} catch (NumberFormatException ex) { // Check packet names
String name = args[2];
outer: for (PacketType packet : PacketType.values()) {
if (packet.getProtocol() == protocol && packet.getSender() == pSender) {
@ -114,7 +114,7 @@ public class PacketLogging implements CommandExecutor, PacketListener {
}
}
}
} catch (IllegalArgumentException ex) {
} catch (IllegalArgumentException ex) { // RIP
type = null;
}
@ -168,6 +168,7 @@ public class PacketLogging implements CommandExecutor, PacketListener {
this.sendingWhitelist = ListeningWhitelist.newBuilder().types(sendingTypes).build();
this.receivingWhitelist = ListeningWhitelist.newBuilder().types(receivingTypes).build();
// Setup the file logger if it hasn't been already
if (location == LogLocation.FILE && fileLogger == null) {
fileLogger = Logger.getLogger("ProtocolLib-FileLogging");
@ -199,6 +200,8 @@ public class PacketLogging implements CommandExecutor, PacketListener {
log(event);
}
// Here's where the magic happens
private static String hexDump(byte[] bytes) throws IOException {
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
HexDump.dump(bytes, 0, output, 0);
@ -208,8 +211,8 @@ public class PacketLogging implements CommandExecutor, PacketListener {
private void log(PacketEvent event) {
try {
WirePacket packet = WirePacket.fromPacket(event.getPacket());
String hexDump = hexDump(packet.getBytes());
byte[] bytes = WirePacket.bytesFromPacket(event.getPacket());
String hexDump = hexDump(bytes);
if (location == LogLocation.FILE) {
fileLogger.log(Level.INFO, event.getPacketType() + ":");