packet logging works

This commit is contained in:
Christoph J. Scherr 2023-11-10 20:20:12 +01:00
parent bd44fab93e
commit 38fa6b52de
3 changed files with 89 additions and 71 deletions

View File

@ -4,16 +4,17 @@ import de.cscherr.mcpht.config.MCPHTConfig;
import me.shedaniel.autoconfig.AutoConfig; import me.shedaniel.autoconfig.AutoConfig;
import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.ClientModInitializer;
import me.shedaniel.autoconfig.serializer.JanksonConfigSerializer; import me.shedaniel.autoconfig.serializer.JanksonConfigSerializer;
import net.minecraft.client.gui.screen.Screen;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.regex.Pattern;
public class MCPHTClient implements ClientModInitializer { public class MCPHTClient implements ClientModInitializer {
public static final String MOD_ID = "mcpht"; public static final String MOD_ID = "mcpht";
public static final String VERSION_NUMBER = "0.0.1"; public static final String VERSION_NUMBER = "0.0.1";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static MCPHTConfig CONFIG; public static MCPHTConfig CONFIG;
public static Pattern networkPacketFilter;
@Override @Override
public void onInitializeClient() { public void onInitializeClient() {
@ -30,15 +31,7 @@ public class MCPHTClient implements ClientModInitializer {
conf.onLoad(); conf.onLoad();
return null; return null;
}); });
LOGGER.info(String.format("CONFIG: \n" + CONFIG.onLoad();
"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));
LOGGER.info("Init client!"); LOGGER.info("Init client!");
} }
} }

View File

@ -5,6 +5,7 @@ import de.cscherr.mcpht.mixin.client.NetworkPacketLogMixin;
import me.shedaniel.autoconfig.ConfigData; import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config; import me.shedaniel.autoconfig.annotation.Config;
import me.shedaniel.autoconfig.annotation.ConfigEntry; import me.shedaniel.autoconfig.annotation.ConfigEntry;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@Config(name = "mcphtconf") @Config(name = "mcphtconf")
@ -15,34 +16,46 @@ public class MCPHTConfig implements ConfigData {
public void onSave() { public void onSave() {
MCPHTClient.LOGGER.info("saving and processing configs"); MCPHTClient.LOGGER.info("saving and processing configs");
compileFilter(); MCPHTClient.networkPacketFilter = compileFilter();
logSettings();
} }
public void onLoad() { public void onLoad() {
MCPHTClient.LOGGER.info("loading configs"); MCPHTClient.LOGGER.info("loading configs");
compileFilter(); MCPHTClient.networkPacketFilter = compileFilter();
logSettings();
} }
public void compileFilter() {
try { private Pattern compileFilter() {
this.networkLogging.pattern = Pattern.compile(".*"); 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 static class NetworkLoggingScheme {
public boolean RX = false; public boolean RX = false;
public boolean TX = false; public boolean TX = false;
public NetworkPacketVerbosity verbosity = NetworkPacketVerbosity.NAME; public NetworkPacketVerbosity verbosity = NetworkPacketVerbosity.NAME;
public String regex = ""; public String regex = ".*";
public enum NetworkPacketVerbosity { public enum NetworkPacketVerbosity {
ALL, ALL, NAME
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;
} }
} }

View File

@ -14,10 +14,17 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.regex.Pattern;
@Mixin(ClientConnection.class) @Mixin(ClientConnection.class)
public class NetworkPacketLogMixin { public class NetworkPacketLogMixin {
private static boolean filterPacket(Packet<?> packet) {
return MCPHTClient.networkPacketFilter.matcher(packet.getClass().getSimpleName()).find();
}
@Unique @Unique
private static final Logger LOGGER = MCPHTClient.LOGGER; private static final Logger LOGGER = MCPHTClient.LOGGER;
/** /**
* Inject to the method that sends packets. Must not be static for some reason * Inject to the method that sends packets. Must not be static for some reason
* *
@ -26,8 +33,8 @@ public class NetworkPacketLogMixin {
* @param ci magical fabric mixin stuff * @param ci magical fabric mixin stuff
*/ */
@Inject(method = "sendImmediately", at = @At("HEAD")) @Inject(method = "sendImmediately", at = @At("HEAD"))
public void logSentPacket(Packet<?> packet, @Nullable PacketCallbacks callbacks, CallbackInfo ci) { public void logTXPacket(Packet<?> packet, @Nullable PacketCallbacks callbacks, CallbackInfo ci) {
if (MCPHTClient.CONFIG.networkLogging.TX) { if (MCPHTClient.CONFIG.networkLogging.TX && filterPacket(packet)) {
String information = null; String information = null;
switch (MCPHTClient.CONFIG.networkLogging.verbosity) { switch (MCPHTClient.CONFIG.networkLogging.verbosity) {
case NAME -> { case NAME -> {
@ -35,15 +42,11 @@ public class NetworkPacketLogMixin {
break; break;
} }
case ALL -> { case ALL -> {
information = String.format("%s:\n" + information = String.format("%s:\n" + " %s\n", packet.getClass().getSimpleName(), packet.toString());
" %s\n" +
"ALL INFO", packet.getClass().getSimpleName(), packet.toString());
break; break;
} }
} }
LOGGER.info(String.format("Sent Package: %s", LOGGER.info(String.format("TX Package: %s", information));
information
));
} }
} }
@ -56,11 +59,20 @@ public class NetworkPacketLogMixin {
* @param ci magical fabric mixin stuff * @param ci magical fabric mixin stuff
*/ */
@Inject(method = "handlePacket", at = @At("HEAD")) @Inject(method = "handlePacket", at = @At("HEAD"))
private static void logReceivedPacket(Packet<?> packet, PacketListener listener, CallbackInfo ci) { private static void logRXPacket(Packet<?> packet, PacketListener listener, CallbackInfo ci) {
if (MCPHTClient.CONFIG.networkLogging.RX) { if (MCPHTClient.CONFIG.networkLogging.RX && filterPacket(packet)) {
LOGGER.info(String.format("Received Package: %s", String information = null;
packet.getClass().getSimpleName() 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));
} }
} }