3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-09-17 01:23:43 +02:00

Update DebugHandler

Dieser Commit ist enthalten in:
Nassim Jahnke 2022-05-20 13:53:36 +02:00
Ursprung 80a807e366
Commit 90feac8c83
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
6 geänderte Dateien mit 54 neuen und 17 gelöschten Zeilen

Datei anzeigen

@ -22,8 +22,12 @@
*/ */
package com.viaversion.viaversion.api.debug; 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; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
@Beta
public interface DebugHandler { public interface DebugHandler {
/** /**
@ -47,6 +51,14 @@ public interface DebugHandler {
*/ */
void addPacketTypeNameToLog(String packetTypeName); 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. * 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. * Returns whether the given packet should be logged.
* If no specific packet type has been added, all packet types will 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 * @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);
}
}
} }

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

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