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 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!");
}
}

View File

@ -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(".*");
}
catch (Exception e) {
MCPHTClient.LOGGER.error(e.getMessage());
private Pattern compileFilter() {
return Pattern.compile(this.networkLogging.regex);
}
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;
}
}

View File

@ -14,10 +14,17 @@ 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 {
private static boolean filterPacket(Packet<?> packet) {
return MCPHTClient.networkPacketFilter.matcher(packet.getClass().getSimpleName()).find();
}
@Unique
private static final Logger LOGGER = MCPHTClient.LOGGER;
/**
* 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
*/
@Inject(method = "sendImmediately", at = @At("HEAD"))
public void logSentPacket(Packet<?> packet, @Nullable PacketCallbacks callbacks, CallbackInfo ci) {
if (MCPHTClient.CONFIG.networkLogging.TX) {
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 -> {
@ -35,15 +42,11 @@ public class NetworkPacketLogMixin {
break;
}
case ALL -> {
information = String.format("%s:\n" +
" %s\n" +
"ALL INFO", packet.getClass().getSimpleName(), packet.toString());
information = String.format("%s:\n" + " %s\n", packet.getClass().getSimpleName(), packet.toString());
break;
}
}
LOGGER.info(String.format("Sent Package: %s",
information
));
LOGGER.info(String.format("TX Package: %s", information));
}
}
@ -56,11 +59,20 @@ public class NetworkPacketLogMixin {
* @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()
));
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));
}
}