packet logging with field info

This commit is contained in:
Christoph J. Scherr 2023-11-10 23:07:48 +01:00
parent 38fa6b52de
commit 21eca17e31
2 changed files with 40 additions and 25 deletions

View File

@ -29,8 +29,14 @@ public class MCPHTConfig implements ConfigData {
} }
private Pattern compileFilter() { private Pattern compileFilter() {
try {
return Pattern.compile(this.networkLogging.regex); 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() { public void logSettings() {
MCPHTClient.LOGGER.info(String.format("CONFIG: \n" + MCPHTClient.LOGGER.info(String.format("CONFIG: \n" +

View File

@ -3,6 +3,7 @@ package de.cscherr.mcpht.mixin.client;
import de.cscherr.mcpht.MCPHTClient; import de.cscherr.mcpht.MCPHTClient;
import de.cscherr.mcpht.config.MCPHTConfig; import de.cscherr.mcpht.config.MCPHTConfig;
import net.minecraft.network.ClientConnection; import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket;
import org.slf4j.Logger; import org.slf4j.Logger;
import net.minecraft.network.PacketCallbacks; import net.minecraft.network.PacketCallbacks;
import net.minecraft.network.listener.PacketListener; import net.minecraft.network.listener.PacketListener;
@ -21,6 +22,36 @@ public class NetworkPacketLogMixin {
private static boolean filterPacket(Packet<?> packet) { private static boolean filterPacket(Packet<?> packet) {
return MCPHTClient.networkPacketFilter.matcher(packet.getClass().getSimpleName()).find(); 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 = "<None>";
}
}
return fieldInfo;
}
@Unique @Unique
private static final Logger LOGGER = MCPHTClient.LOGGER; private static final Logger LOGGER = MCPHTClient.LOGGER;
@ -35,18 +66,7 @@ public class NetworkPacketLogMixin {
@Inject(method = "sendImmediately", at = @At("HEAD")) @Inject(method = "sendImmediately", at = @At("HEAD"))
public void logTXPacket(Packet<?> packet, @Nullable PacketCallbacks callbacks, CallbackInfo ci) { public void logTXPacket(Packet<?> packet, @Nullable PacketCallbacks callbacks, CallbackInfo ci) {
if (MCPHTClient.CONFIG.networkLogging.TX && filterPacket(packet)) { if (MCPHTClient.CONFIG.networkLogging.TX && filterPacket(packet)) {
String information = null; LOGGER.info(String.format("TX Package: %s", getPacketInfo(packet)));
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));
} }
} }
@ -61,18 +81,7 @@ public class NetworkPacketLogMixin {
@Inject(method = "handlePacket", at = @At("HEAD")) @Inject(method = "handlePacket", at = @At("HEAD"))
private static void logRXPacket(Packet<?> packet, PacketListener listener, CallbackInfo ci) { private static void logRXPacket(Packet<?> packet, PacketListener listener, CallbackInfo ci) {
if (MCPHTClient.CONFIG.networkLogging.RX && filterPacket(packet)) { if (MCPHTClient.CONFIG.networkLogging.RX && filterPacket(packet)) {
String information = null; LOGGER.info(String.format("RX Package: %s", getPacketInfo(packet)));
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));
} }
} }