mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2024-11-27 13:15:52 +01:00
Move getPacketDescription from the command to HexDumper as a public API
Related to filoghost/HolographicDisplays#385
This commit is contained in:
parent
b54dd49426
commit
30fe81d366
@ -76,11 +76,6 @@ class CommandPacket extends CommandBase {
|
|||||||
*/
|
*/
|
||||||
public static final int PAGE_LINE_COUNT = 9;
|
public static final int PAGE_LINE_COUNT = 9;
|
||||||
|
|
||||||
/**
|
|
||||||
* Number of bytes before we do a hex dump.
|
|
||||||
*/
|
|
||||||
private static final int HEX_DUMP_THRESHOLD = 256;
|
|
||||||
|
|
||||||
private Plugin plugin;
|
private Plugin plugin;
|
||||||
private Logger logger;
|
private Logger logger;
|
||||||
private ProtocolManager manager;
|
private ProtocolManager manager;
|
||||||
@ -370,7 +365,7 @@ class CommandPacket extends CommandBase {
|
|||||||
logger.info("Initial packet:\n" + original + " -> ");
|
logger.info("Initial packet:\n" + original + " -> ");
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(shortDescription + ":\n" + getPacketDescription(
|
logger.info(shortDescription + ":\n" + HexDumper.getPacketDescription(
|
||||||
event.getPacket())
|
event.getPacket())
|
||||||
);
|
);
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
@ -427,7 +422,7 @@ class CommandPacket extends CommandBase {
|
|||||||
*/
|
*/
|
||||||
private void savePacketState(PacketEvent event) {
|
private void savePacketState(PacketEvent event) {
|
||||||
try {
|
try {
|
||||||
originalPackets.put(event, getPacketDescription(event.getPacket()));
|
originalPackets.put(event, HexDumper.getPacketDescription(event.getPacket()));
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
throw new RuntimeException("Cannot read packet.", e);
|
throw new RuntimeException("Cannot read packet.", e);
|
||||||
}
|
}
|
||||||
@ -449,68 +444,6 @@ class CommandPacket extends CommandBase {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve a detailed string representation of the given packet.
|
|
||||||
* @param packetContainer - the packet to describe.
|
|
||||||
* @return The detailed description.
|
|
||||||
* @throws IllegalAccessException An error occured.
|
|
||||||
*/
|
|
||||||
public String getPacketDescription(PacketContainer packetContainer) throws IllegalAccessException {
|
|
||||||
Object packet = packetContainer.getHandle();
|
|
||||||
Class<?> clazz = packet.getClass();
|
|
||||||
|
|
||||||
// Get the first Minecraft super class
|
|
||||||
while (clazz != null && clazz != Object.class &&
|
|
||||||
(!MinecraftReflection.isMinecraftClass(clazz) ||
|
|
||||||
ByteBuddyGenerated.class.isAssignableFrom(clazz))) {
|
|
||||||
clazz = clazz.getSuperclass();
|
|
||||||
}
|
|
||||||
|
|
||||||
return PrettyPrinter.printObject(packet, clazz, MinecraftReflection.getPacketClass(), PrettyPrinter.RECURSE_DEPTH, new ObjectPrinter() {
|
|
||||||
@Override
|
|
||||||
public boolean print(StringBuilder output, Object value) {
|
|
||||||
// Special case
|
|
||||||
if (value instanceof byte[]) {
|
|
||||||
byte[] data = (byte[]) value;
|
|
||||||
|
|
||||||
if (data.length > HEX_DUMP_THRESHOLD) {
|
|
||||||
output.append("[");
|
|
||||||
HexDumper.defaultDumper().appendTo(output, data);
|
|
||||||
output.append("]");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} else if (value != null) {
|
|
||||||
EquivalentConverter<Object> converter = findConverter(value.getClass());
|
|
||||||
|
|
||||||
if (converter != null) {
|
|
||||||
output.append(converter.getSpecific(value));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the closest equivalent converter to a specific class.
|
|
||||||
* @param clazz - the class.
|
|
||||||
* @return The closest converter, or NULL if not found,
|
|
||||||
*/
|
|
||||||
private EquivalentConverter<Object> findConverter(Class<?> clazz) {
|
|
||||||
Map<Class<?>, EquivalentConverter<Object>> converters = BukkitConverters.getConvertersForGeneric();
|
|
||||||
|
|
||||||
while (clazz != null) {
|
|
||||||
EquivalentConverter<Object> result = converters.get(clazz);
|
|
||||||
|
|
||||||
if (result != null)
|
|
||||||
return result;
|
|
||||||
else
|
|
||||||
clazz = clazz.getSuperclass();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PacketListener updatePacketListener() {
|
public PacketListener updatePacketListener() {
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
package com.comphenix.protocol.utility;
|
package com.comphenix.protocol.utility;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.comphenix.protocol.events.PacketContainer;
|
||||||
|
import com.comphenix.protocol.reflect.EquivalentConverter;
|
||||||
|
import com.comphenix.protocol.reflect.PrettyPrinter;
|
||||||
|
import com.comphenix.protocol.wrappers.BukkitConverters;
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -12,6 +17,11 @@ import com.google.common.base.Preconditions;
|
|||||||
public class HexDumper {
|
public class HexDumper {
|
||||||
private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
|
private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Number of bytes before we do a hex dump.
|
||||||
|
*/
|
||||||
|
private static final int HEX_DUMP_THRESHOLD = 256;
|
||||||
|
|
||||||
// Default values
|
// Default values
|
||||||
private int positionLength = 6;
|
private int positionLength = 6;
|
||||||
private char[] positionSuffix = ": ".toCharArray();
|
private char[] positionSuffix = ": ".toCharArray();
|
||||||
@ -228,4 +238,64 @@ public class HexDumper {
|
|||||||
// Total expected length of each line
|
// Total expected length of each line
|
||||||
return constant + delimiter.length * (groups - 1) + groupLength * groups;
|
return constant + delimiter.length * (groups - 1) + groupLength * groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the closest equivalent converter to a specific class.
|
||||||
|
* @param clazz - the class.
|
||||||
|
* @return The closest converter, or NULL if not found,
|
||||||
|
*/
|
||||||
|
public static EquivalentConverter<Object> findConverter(Class<?> clazz) {
|
||||||
|
Map<Class<?>, EquivalentConverter<Object>> converters = BukkitConverters.getConvertersForGeneric();
|
||||||
|
|
||||||
|
while (clazz != null) {
|
||||||
|
EquivalentConverter<Object> result = converters.get(clazz);
|
||||||
|
|
||||||
|
if (result != null)
|
||||||
|
return result;
|
||||||
|
else
|
||||||
|
clazz = clazz.getSuperclass();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a detailed string representation of the given packet.
|
||||||
|
* @param packetContainer - the packet to describe.
|
||||||
|
* @return The detailed description.
|
||||||
|
* @throws IllegalAccessException An error occured.
|
||||||
|
*/
|
||||||
|
public static String getPacketDescription(PacketContainer packetContainer) throws IllegalAccessException {
|
||||||
|
Object packet = packetContainer.getHandle();
|
||||||
|
Class<?> clazz = packet.getClass();
|
||||||
|
|
||||||
|
// Get the first Minecraft super class
|
||||||
|
while (clazz != null && clazz != Object.class &&
|
||||||
|
(!MinecraftReflection.isMinecraftClass(clazz) ||
|
||||||
|
ByteBuddyGenerated.class.isAssignableFrom(clazz))) {
|
||||||
|
clazz = clazz.getSuperclass();
|
||||||
|
}
|
||||||
|
|
||||||
|
return PrettyPrinter.printObject(packet, clazz, MinecraftReflection.getPacketClass(),
|
||||||
|
PrettyPrinter.RECURSE_DEPTH, (output, value) -> {
|
||||||
|
// Special case
|
||||||
|
if (value instanceof byte[]) {
|
||||||
|
byte[] data = (byte[]) value;
|
||||||
|
|
||||||
|
if (data.length > HEX_DUMP_THRESHOLD) {
|
||||||
|
output.append("[");
|
||||||
|
HexDumper.defaultDumper().appendTo(output, data);
|
||||||
|
output.append("]");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else if (value != null) {
|
||||||
|
EquivalentConverter<Object> converter = findConverter(value.getClass());
|
||||||
|
|
||||||
|
if (converter != null) {
|
||||||
|
output.append(converter.getSpecific(value));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user