From 21eca17e310faab98cff36bd97c1424530437b67 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Fri, 10 Nov 2023 23:07:48 +0100 Subject: [PATCH] packet logging with field info --- .../de/cscherr/mcpht/config/MCPHTConfig.java | 8 ++- .../mixin/client/NetworkPacketLogMixin.java | 57 +++++++++++-------- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/client/java/de/cscherr/mcpht/config/MCPHTConfig.java b/src/client/java/de/cscherr/mcpht/config/MCPHTConfig.java index 1136ce2..2920c3c 100644 --- a/src/client/java/de/cscherr/mcpht/config/MCPHTConfig.java +++ b/src/client/java/de/cscherr/mcpht/config/MCPHTConfig.java @@ -29,7 +29,13 @@ public class MCPHTConfig implements ConfigData { } private Pattern compileFilter() { - return Pattern.compile(this.networkLogging.regex); + try { + return Pattern.compile(this.networkLogging.regex); + } + catch (Exception e) { + MCPHTClient.LOGGER.warn(String.format("Invalid regex: %s", this.networkLogging.regex)); + return Pattern.compile(".*"); // just accept all on error + } } public void logSettings() { diff --git a/src/client/java/de/cscherr/mcpht/mixin/client/NetworkPacketLogMixin.java b/src/client/java/de/cscherr/mcpht/mixin/client/NetworkPacketLogMixin.java index 05b6635..53af2a2 100644 --- a/src/client/java/de/cscherr/mcpht/mixin/client/NetworkPacketLogMixin.java +++ b/src/client/java/de/cscherr/mcpht/mixin/client/NetworkPacketLogMixin.java @@ -3,6 +3,7 @@ package de.cscherr.mcpht.mixin.client; import de.cscherr.mcpht.MCPHTClient; import de.cscherr.mcpht.config.MCPHTConfig; import net.minecraft.network.ClientConnection; +import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket; import org.slf4j.Logger; import net.minecraft.network.PacketCallbacks; import net.minecraft.network.listener.PacketListener; @@ -21,6 +22,36 @@ public class NetworkPacketLogMixin { private static boolean filterPacket(Packet packet) { return MCPHTClient.networkPacketFilter.matcher(packet.getClass().getSimpleName()).find(); } + private static String getPacketInfo(Packet packet) { + String information = null; + switch (MCPHTClient.CONFIG.networkLogging.verbosity) { + case NAME -> { + information = packet.getClass().getSimpleName(); + break; + } + case ALL -> { + information = String.format("%s:\n%s", packet.getClass().getSimpleName(), getPacketFields(packet)); + break; + } + default -> + throw new IllegalStateException("Unexpected value: " + MCPHTClient.CONFIG.networkLogging.verbosity); + } + return information; + } + private static String getPacketFields(Packet packet) { + String fieldInfo = ""; + switch (packet.getClass().getSimpleName()) { + case "PositionAndOnGround" -> { + PlayerMoveC2SPacket ppacket = (PlayerMoveC2SPacket)packet; + fieldInfo = String.format("X: %f | Y: %f | Z: %f\n" + + "Ground: %b", ppacket.getX(0), ppacket.getY(0), ppacket.getZ(0), ppacket.isOnGround()); + } + default -> { + fieldInfo = ""; + } + } + return fieldInfo; + } @Unique private static final Logger LOGGER = MCPHTClient.LOGGER; @@ -35,18 +66,7 @@ public class NetworkPacketLogMixin { @Inject(method = "sendImmediately", at = @At("HEAD")) public void logTXPacket(Packet packet, @Nullable PacketCallbacks callbacks, CallbackInfo ci) { if (MCPHTClient.CONFIG.networkLogging.TX && filterPacket(packet)) { - String information = null; - switch (MCPHTClient.CONFIG.networkLogging.verbosity) { - case NAME -> { - information = packet.getClass().getSimpleName(); - break; - } - case ALL -> { - information = String.format("%s:\n" + " %s\n", packet.getClass().getSimpleName(), packet.toString()); - break; - } - } - LOGGER.info(String.format("TX Package: %s", information)); + LOGGER.info(String.format("TX Package: %s", getPacketInfo(packet))); } } @@ -61,18 +81,7 @@ public class NetworkPacketLogMixin { @Inject(method = "handlePacket", at = @At("HEAD")) private static void logRXPacket(Packet packet, PacketListener listener, CallbackInfo ci) { if (MCPHTClient.CONFIG.networkLogging.RX && filterPacket(packet)) { - String information = null; - switch (MCPHTClient.CONFIG.networkLogging.verbosity) { - case NAME -> { - information = packet.getClass().getSimpleName(); - break; - } - case ALL -> { - information = String.format("%s:\n" + " %s\n", packet.getClass().getSimpleName(), packet.toString()); - break; - } - } - LOGGER.info(String.format("RX Package: %s", information)); + LOGGER.info(String.format("RX Package: %s", getPacketInfo(packet))); } }