Update DebugHandler

This commit is contained in:
Nassim Jahnke 2022-05-20 13:53:36 +02:00
parent 80a807e366
commit 90feac8c83
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
6 changed files with 54 additions and 17 deletions

View File

@ -22,8 +22,12 @@
*/
package com.viaversion.viaversion.api.debug;
import com.google.common.annotations.Beta;
import com.viaversion.viaversion.api.protocol.packet.Direction;
import com.viaversion.viaversion.api.protocol.packet.PacketType;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
@Beta
public interface DebugHandler {
/**
@ -47,6 +51,14 @@ public interface DebugHandler {
*/
void addPacketTypeNameToLog(String packetTypeName);
/**
* Adds a packet id to the list of packet types to log.
* Packets will be checked on each protocol transformer, so this is best used on single protocol pipes.
*
* @param packetType packet type
*/
void addPacketTypeToLog(PacketType packetType);
/**
* Removes a packet type name from the list of packet types to log.
*
@ -78,8 +90,16 @@ public interface DebugHandler {
* Returns whether the given packet should be logged.
* If no specific packet type has been added, all packet types will be logged.
*
* @param wrapper packet wrapper
* @param wrapper packet wrapper
* @param direction packet direction
* @return whether the packet should be logged
*/
boolean shouldLog(PacketWrapper wrapper);
boolean shouldLog(PacketWrapper wrapper, Direction direction);
default void enableAndLogIds(final PacketType... packetTypes) {
setEnabled(true);
for (final PacketType packetType : packetTypes) {
addPacketTypeToLog(packetType);
}
}
}

View File

@ -351,7 +351,7 @@ public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 exte
throw e;
}
private String toNiceHex(int id) {
public static String toNiceHex(int id) {
String hex = Integer.toHexString(id).toUpperCase();
return (hex.length() == 1 ? "0x0" : "0x") + hex;
}

View File

@ -18,7 +18,11 @@
package com.viaversion.viaversion.debug;
import com.viaversion.viaversion.api.debug.DebugHandler;
import com.viaversion.viaversion.api.protocol.packet.Direction;
import com.viaversion.viaversion.api.protocol.packet.PacketType;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import java.util.HashSet;
import java.util.Set;
@ -26,7 +30,9 @@ import java.util.Set;
public final class DebugHandlerImpl implements DebugHandler {
private final Set<String> packetTypesToLog = new HashSet<>();
private boolean logPostPacketTransform = true;
private final IntSet clientboundPacketIdsToLog = new IntOpenHashSet();
private final IntSet serverboundPacketIdsToLog = new IntOpenHashSet();
private boolean logPostPacketTransform;
private boolean enabled;
@Override
@ -44,6 +50,11 @@ public final class DebugHandlerImpl implements DebugHandler {
packetTypesToLog.add(packetTypeName);
}
@Override
public void addPacketTypeToLog(PacketType packetType) {
(packetType.direction() == Direction.SERVERBOUND ? serverboundPacketIdsToLog : clientboundPacketIdsToLog).add(packetType.getId());
}
@Override
public boolean removePacketTypeNameToLog(final String packetTypeName) {
return packetTypesToLog.remove(packetTypeName);
@ -65,7 +76,9 @@ public final class DebugHandlerImpl implements DebugHandler {
}
@Override
public boolean shouldLog(final PacketWrapper wrapper) {
return packetTypesToLog.isEmpty() || (wrapper.getPacketType() != null && packetTypesToLog.contains(wrapper.getPacketType().getName()));
public boolean shouldLog(final PacketWrapper wrapper, final Direction direction) {
return packetTypesToLog.isEmpty() && serverboundPacketIdsToLog.isEmpty() && clientboundPacketIdsToLog.isEmpty()
|| (wrapper.getPacketType() != null && packetTypesToLog.contains(wrapper.getPacketType().getName()))
|| (direction == Direction.SERVERBOUND ? serverboundPacketIdsToLog : clientboundPacketIdsToLog).contains(wrapper.getId());
}
}

View File

@ -114,12 +114,16 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot
public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception {
int originalID = packetWrapper.getId();
DebugHandler debugHandler = Via.getManager().debugHandler();
if (debugHandler.enabled() && !debugHandler.logPostPacketTransform() && debugHandler.shouldLog(packetWrapper, direction)) {
logPacket(direction, state, packetWrapper, originalID);
}
// Apply protocols
packetWrapper.apply(direction, state, 0, protocolList, direction == Direction.CLIENTBOUND);
super.transform(direction, state, packetWrapper);
DebugHandler debugHandler = Via.getManager().debugHandler();
if (debugHandler.enabled() && debugHandler.logPostPacketTransform() && debugHandler.shouldLog(packetWrapper)) {
if (debugHandler.enabled() && debugHandler.logPostPacketTransform() && debugHandler.shouldLog(packetWrapper, direction)) {
logPacket(direction, state, packetWrapper, originalID);
}
}
@ -127,20 +131,20 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot
private void logPacket(Direction direction, State state, PacketWrapper packetWrapper, int originalID) {
// Debug packet
int clientProtocol = userConnection.getProtocolInfo().getProtocolVersion();
ViaPlatform platform = Via.getPlatform();
ViaPlatform<?> platform = Via.getPlatform();
String actualUsername = packetWrapper.user().getProtocolInfo().getUsername();
String username = actualUsername != null ? actualUsername + " " : "";
platform.getLogger().log(Level.INFO, "{0}{1} {2}: {3} (0x{4}) -> {5} (0x{6}) [{7}] {8}",
platform.getLogger().log(Level.INFO, "{0}{1} {2}: {3} ({4}) -> {5} ({6}) [{7}] {8}",
new Object[]{
username,
direction,
state,
originalID,
Integer.toHexString(originalID),
AbstractSimpleProtocol.toNiceHex(originalID),
packetWrapper.getId(),
Integer.toHexString(packetWrapper.getId()),
AbstractSimpleProtocol.toNiceHex(packetWrapper.getId()),
Integer.toString(clientProtocol),
packetWrapper
});

View File

@ -488,10 +488,10 @@ public class PacketWrapperImpl implements PacketWrapper {
@Override
public String toString() {
return "PacketWrapper{" +
"packetValues=" + packetValues +
", readableObjects=" + readableObjects +
"packetType=" + packetType +
", id=" + id +
", packetType=" + packetType +
", packetValues=" + packetValues +
", readableObjects=" + readableObjects +
'}';
}
}

View File

@ -58,9 +58,9 @@ public final class Chunk1_18Type extends Type<Chunk> {
sections[i] = sectionType.read(sectionsBuf);
}
} finally {
if (sectionsBuf.readableBytes() > 0 && Via.getManager().isDebug()) {
/*if (sectionsBuf.readableBytes() > 0 && Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("Found " + sectionsBuf.readableBytes() + " more bytes than expected while reading the chunk: " + chunkX + "/" + chunkZ);
}
}*/
sectionsBuf.release();
}