From 38fa6b52dec7035794c30faa50eb0ea32e4c753d Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Fri, 10 Nov 2023 20:20:12 +0100 Subject: [PATCH] packet logging works --- .../java/de/cscherr/mcpht/MCPHTClient.java | 13 +-- .../de/cscherr/mcpht/config/MCPHTConfig.java | 43 +++++--- .../mixin/client/NetworkPacketLogMixin.java | 104 ++++++++++-------- 3 files changed, 89 insertions(+), 71 deletions(-) diff --git a/src/client/java/de/cscherr/mcpht/MCPHTClient.java b/src/client/java/de/cscherr/mcpht/MCPHTClient.java index 2138b7e..04b1655 100644 --- a/src/client/java/de/cscherr/mcpht/MCPHTClient.java +++ b/src/client/java/de/cscherr/mcpht/MCPHTClient.java @@ -4,16 +4,17 @@ import de.cscherr.mcpht.config.MCPHTConfig; import me.shedaniel.autoconfig.AutoConfig; import net.fabricmc.api.ClientModInitializer; import me.shedaniel.autoconfig.serializer.JanksonConfigSerializer; -import net.minecraft.client.gui.screen.Screen; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.regex.Pattern; public class MCPHTClient implements ClientModInitializer { public static final String MOD_ID = "mcpht"; public static final String VERSION_NUMBER = "0.0.1"; public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); public static MCPHTConfig CONFIG; + public static Pattern networkPacketFilter; @Override public void onInitializeClient() { @@ -30,15 +31,7 @@ public class MCPHTClient implements ClientModInitializer { conf.onLoad(); return null; }); - LOGGER.info(String.format("CONFIG: \n" + - "NetworkLogging\n" + - " RX: %b\n" + - " TX: %b\n" + - " verbosity: %s\n" + - " regex: '%s'", CONFIG.networkLogging.RX, - CONFIG.networkLogging.TX, - CONFIG.networkLogging.verbosity, - CONFIG.networkLogging.regex)); + CONFIG.onLoad(); LOGGER.info("Init client!"); } } \ No newline at end of file diff --git a/src/client/java/de/cscherr/mcpht/config/MCPHTConfig.java b/src/client/java/de/cscherr/mcpht/config/MCPHTConfig.java index 09c26a6..1136ce2 100644 --- a/src/client/java/de/cscherr/mcpht/config/MCPHTConfig.java +++ b/src/client/java/de/cscherr/mcpht/config/MCPHTConfig.java @@ -5,6 +5,7 @@ import de.cscherr.mcpht.mixin.client.NetworkPacketLogMixin; import me.shedaniel.autoconfig.ConfigData; import me.shedaniel.autoconfig.annotation.Config; import me.shedaniel.autoconfig.annotation.ConfigEntry; + import java.util.regex.Pattern; @Config(name = "mcphtconf") @@ -15,34 +16,46 @@ public class MCPHTConfig implements ConfigData { public void onSave() { MCPHTClient.LOGGER.info("saving and processing configs"); - compileFilter(); + MCPHTClient.networkPacketFilter = compileFilter(); + + logSettings(); } public void onLoad() { MCPHTClient.LOGGER.info("loading configs"); - compileFilter(); + MCPHTClient.networkPacketFilter = compileFilter(); + + logSettings(); } - public void compileFilter() { - try { - this.networkLogging.pattern = Pattern.compile(".*"); + + private Pattern compileFilter() { + return Pattern.compile(this.networkLogging.regex); } - catch (Exception e) { - MCPHTClient.LOGGER.error(e.getMessage()); - } + + public void logSettings() { + MCPHTClient.LOGGER.info(String.format("CONFIG: \n" + + "NetworkLogging\n" + + " RX: %b\n" + + " TX: %b\n" + + " verbosity: %s\n" + + " regex: '%s'\n" + + " filter: %s", + MCPHTClient.CONFIG.networkLogging.RX, + MCPHTClient.CONFIG.networkLogging.TX, + MCPHTClient.CONFIG.networkLogging.verbosity, + MCPHTClient.CONFIG.networkLogging.regex, + MCPHTClient.networkPacketFilter + )); } public static class NetworkLoggingScheme { public boolean RX = false; public boolean TX = false; public NetworkPacketVerbosity verbosity = NetworkPacketVerbosity.NAME; - public String regex = ""; + public String regex = ".*"; + public enum NetworkPacketVerbosity { - ALL, - NAME + ALL, NAME } - @ConfigEntry.Gui.Excluded - // FIXME: This field cannot be saved to a file, which causes a runtime error on saving. - // TODO: Add serialization and deserialization methods for this somehow. - public Pattern pattern; } } 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 91d5332..05b6635 100644 --- a/src/client/java/de/cscherr/mcpht/mixin/client/NetworkPacketLogMixin.java +++ b/src/client/java/de/cscherr/mcpht/mixin/client/NetworkPacketLogMixin.java @@ -14,54 +14,66 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.jetbrains.annotations.Nullable; +import java.util.regex.Pattern; + @Mixin(ClientConnection.class) public class NetworkPacketLogMixin { - @Unique - private static final Logger LOGGER = MCPHTClient.LOGGER; - /** - * Inject to the method that sends packets. Must not be static for some reason - * - * @param packet the Network packet to be sent - * @param callbacks magical fabric mixin stuff - * @param ci magical fabric mixin stuff - */ - @Inject(method = "sendImmediately", at = @At("HEAD")) - public void logSentPacket(Packet packet, @Nullable PacketCallbacks callbacks, CallbackInfo ci) { - if (MCPHTClient.CONFIG.networkLogging.TX) { - String information = null; - switch (MCPHTClient.CONFIG.networkLogging.verbosity) { - case NAME -> { - information = packet.getClass().getSimpleName(); - break; - } - case ALL -> { - information = String.format("%s:\n" + - " %s\n" + - "ALL INFO", packet.getClass().getSimpleName(), packet.toString()); - break; - } - } - LOGGER.info(String.format("Sent Package: %s", - information - )); - } - } + private static boolean filterPacket(Packet packet) { + return MCPHTClient.networkPacketFilter.matcher(packet.getClass().getSimpleName()).find(); + } - /** - * Inject to the method that receives and handles packets. - * Must be static for some reason - * - * @param packet the Network packet to be sent - * @param listener i dont know what this does - * @param ci magical fabric mixin stuff - */ - @Inject(method = "handlePacket", at = @At("HEAD")) - private static void logReceivedPacket(Packet packet, PacketListener listener, CallbackInfo ci) { - if (MCPHTClient.CONFIG.networkLogging.RX) { - LOGGER.info(String.format("Received Package: %s", - packet.getClass().getSimpleName() - )); - } - } + @Unique + private static final Logger LOGGER = MCPHTClient.LOGGER; + + /** + * Inject to the method that sends packets. Must not be static for some reason + * + * @param packet the Network packet to be sent + * @param callbacks magical fabric mixin stuff + * @param ci magical fabric mixin stuff + */ + @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)); + } + } + + /** + * Inject to the method that receives and handles packets. + * Must be static for some reason + * + * @param packet the Network packet to be sent + * @param listener i dont know what this does + * @param ci magical fabric mixin stuff + */ + @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)); + } + } }