mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2024-09-30 08:07:34 +02:00
Fix issues with logging custom payload packets
This commit is contained in:
parent
2205ce639e
commit
5ec87c9d76
@ -150,29 +150,52 @@ public class WirePacket {
|
|||||||
*/
|
*/
|
||||||
public static WirePacket fromPacket(PacketContainer packet) {
|
public static WirePacket fromPacket(PacketContainer packet) {
|
||||||
int id = packet.getType().getCurrentId();
|
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
|
* bytes from that packet
|
||||||
*
|
*
|
||||||
* @param packet Existing packet
|
* @param packet Existing packet
|
||||||
* @return The ByteBuf
|
* @return The ByteBuf
|
||||||
*/
|
*/
|
||||||
public static ByteBuf bufferFromPacket(PacketContainer packet) {
|
public static byte[] bytesFromPacket(PacketContainer packet) {
|
||||||
checkNotNull(packet, "packet cannot be null!");
|
checkNotNull(packet, "packet cannot be null!");
|
||||||
|
|
||||||
ByteBuf buffer = PacketContainer.createPacketBuffer();
|
ByteBuf buffer = PacketContainer.createPacketBuffer();
|
||||||
|
ByteBuf store = PacketContainer.createPacketBuffer();
|
||||||
|
|
||||||
|
// Read the bytes once
|
||||||
Method write = MinecraftMethods.getPacketWriteByteBufMethod();
|
Method write = MinecraftMethods.getPacketWriteByteBufMethod();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
write.invoke(packet.getHandle(), buffer);
|
write.invoke(packet.getHandle(), buffer);
|
||||||
} catch (ReflectiveOperationException ex) {
|
} 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -94,10 +94,10 @@ public class PacketLogging implements CommandExecutor, PacketListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
try {
|
try { // Try IDs first
|
||||||
int id = Integer.parseInt(args[2]);
|
int id = Integer.parseInt(args[2]);
|
||||||
type = PacketType.findCurrent(protocol, pSender, id);
|
type = PacketType.findCurrent(protocol, pSender, id);
|
||||||
} catch (NumberFormatException ex) {
|
} catch (NumberFormatException ex) { // Check packet names
|
||||||
String name = args[2];
|
String name = args[2];
|
||||||
outer: for (PacketType packet : PacketType.values()) {
|
outer: for (PacketType packet : PacketType.values()) {
|
||||||
if (packet.getProtocol() == protocol && packet.getSender() == pSender) {
|
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;
|
type = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,6 +168,7 @@ public class PacketLogging implements CommandExecutor, PacketListener {
|
|||||||
this.sendingWhitelist = ListeningWhitelist.newBuilder().types(sendingTypes).build();
|
this.sendingWhitelist = ListeningWhitelist.newBuilder().types(sendingTypes).build();
|
||||||
this.receivingWhitelist = ListeningWhitelist.newBuilder().types(receivingTypes).build();
|
this.receivingWhitelist = ListeningWhitelist.newBuilder().types(receivingTypes).build();
|
||||||
|
|
||||||
|
// Setup the file logger if it hasn't been already
|
||||||
if (location == LogLocation.FILE && fileLogger == null) {
|
if (location == LogLocation.FILE && fileLogger == null) {
|
||||||
fileLogger = Logger.getLogger("ProtocolLib-FileLogging");
|
fileLogger = Logger.getLogger("ProtocolLib-FileLogging");
|
||||||
|
|
||||||
@ -199,6 +200,8 @@ public class PacketLogging implements CommandExecutor, PacketListener {
|
|||||||
log(event);
|
log(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Here's where the magic happens
|
||||||
|
|
||||||
private static String hexDump(byte[] bytes) throws IOException {
|
private static String hexDump(byte[] bytes) throws IOException {
|
||||||
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
|
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
|
||||||
HexDump.dump(bytes, 0, output, 0);
|
HexDump.dump(bytes, 0, output, 0);
|
||||||
@ -208,8 +211,8 @@ public class PacketLogging implements CommandExecutor, PacketListener {
|
|||||||
|
|
||||||
private void log(PacketEvent event) {
|
private void log(PacketEvent event) {
|
||||||
try {
|
try {
|
||||||
WirePacket packet = WirePacket.fromPacket(event.getPacket());
|
byte[] bytes = WirePacket.bytesFromPacket(event.getPacket());
|
||||||
String hexDump = hexDump(packet.getBytes());
|
String hexDump = hexDump(bytes);
|
||||||
|
|
||||||
if (location == LogLocation.FILE) {
|
if (location == LogLocation.FILE) {
|
||||||
fileLogger.log(Level.INFO, event.getPacketType() + ":");
|
fileLogger.log(Level.INFO, event.getPacketType() + ":");
|
||||||
|
Loading…
Reference in New Issue
Block a user