From 90feac8c8301907650d73a9bb96def0019531192 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Fri, 20 May 2022 13:53:36 +0200 Subject: [PATCH] Update DebugHandler --- .../viaversion/api/debug/DebugHandler.java | 24 +++++++++++++++++-- .../api/protocol/AbstractProtocol.java | 2 +- .../viaversion/debug/DebugHandlerImpl.java | 19 ++++++++++++--- .../protocol/ProtocolPipelineImpl.java | 16 ++++++++----- .../protocol/packet/PacketWrapperImpl.java | 6 ++--- .../types/Chunk1_18Type.java | 4 ++-- 6 files changed, 54 insertions(+), 17 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/debug/DebugHandler.java b/api/src/main/java/com/viaversion/viaversion/api/debug/DebugHandler.java index 88cee2b13..46d700b3d 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/debug/DebugHandler.java +++ b/api/src/main/java/com/viaversion/viaversion/api/debug/DebugHandler.java @@ -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); + } + } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java index 87f181079..8cb745213 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java @@ -351,7 +351,7 @@ public abstract class AbstractProtocol 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()); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPipelineImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPipelineImpl.java index f7a598e1f..890c02464 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPipelineImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPipelineImpl.java @@ -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 }); diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java index 64f8a0698..5eb371e58 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java @@ -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 + '}'; } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/types/Chunk1_18Type.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/types/Chunk1_18Type.java index 3334daaba..cbedc7fa7 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/types/Chunk1_18Type.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/types/Chunk1_18Type.java @@ -58,9 +58,9 @@ public final class Chunk1_18Type extends Type { 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(); }