damn i just need regex

This commit is contained in:
Christoph J. Scherr 2023-11-09 01:28:06 +01:00
parent 5dde143ddd
commit 0d1ecb5341
14 changed files with 169 additions and 64 deletions

View File

@ -29,6 +29,7 @@ loom {
sourceSet sourceSets.client
}
}
accessWidenerPath = file("src/main/resources/.accesswidener")
}

View File

@ -0,0 +1,44 @@
package de.cscherr.mcpht;
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;
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;
@Override
public void onInitializeClient() {
AutoConfig.register(MCPHTConfig.class, JanksonConfigSerializer::new);
// This entrypoint is suitable for setting up client-specific logic, such as rendering.
CONFIG = AutoConfig.getConfigHolder(MCPHTConfig.class).getConfig();
AutoConfig.getConfigHolder(MCPHTConfig.class).registerSaveListener((configHolder, mcphtConfig) -> {
MCPHTConfig conf = AutoConfig.getConfigHolder(MCPHTConfig.class).getConfig();
conf.onSave();
return null;
});
AutoConfig.getConfigHolder(MCPHTConfig.class).registerLoadListener((configHolder, mcphtConfig) -> {
MCPHTConfig conf = AutoConfig.getConfigHolder(MCPHTConfig.class).getConfig();
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));
LOGGER.info("Init client!");
}
}

View File

@ -1,21 +0,0 @@
package de.cscherr.mcpht;
import de.cscherr.mcpht.config.MCPHTConfig;
import me.shedaniel.autoconfig.AutoConfig;
import net.fabricmc.api.ClientModInitializer;
import me.shedaniel.autoconfig.serializer.JanksonConfigSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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);
@Override
public void onInitializeClient() {
AutoConfig.register(MCPHTConfig.class, JanksonConfigSerializer::new);
// This entrypoint is suitable for setting up client-specific logic, such as rendering.
LOGGER.info("Init client!");
}
}

View File

@ -1,17 +1,43 @@
package de.cscherr.mcpht.config;
import de.cscherr.mcpht.MCPHTClient;
import joptsimple.util.RegexMatcher;
import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;
import me.shedaniel.autoconfig.annotation.ConfigEntry;
import me.shedaniel.cloth.clothconfig.shadowed.blue.endless.jankson.Comment;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
@Config(name = "mcphtconf")
public class MCPHTConfig implements ConfigData {
@ConfigEntry.Category("Networking")
@ConfigEntry.Gui.CollapsibleObject
NetworkLoggingScheme networkLogging = new NetworkLoggingScheme();
static class NetworkLoggingScheme {
boolean RX = false;
boolean TX = false;
public NetworkLoggingScheme networkLogging = new NetworkLoggingScheme();
public void onSave() {
MCPHTClient.LOGGER.info("saving and processing configs");
compileFilter();
}
public void onLoad() {
MCPHTClient.LOGGER.info("loading configs");
compileFilter();
}
private void compileFilter() {
this.networkLogging.filter = Pattern.compile(this.networkLogging.regex);
}
public static class NetworkLoggingScheme {
public boolean RX = false;
public boolean TX = false;
public NetworkPacketVerbosity verbosity = NetworkPacketVerbosity.NAME;
public String regex = "";
public enum NetworkPacketVerbosity {
ALL,
NAME
}
@ConfigEntry.Gui.Excluded
public Pattern filter;
}
}

View File

@ -5,11 +5,15 @@ import com.terraformersmc.modmenu.api.ModMenuApi;
import me.shedaniel.autoconfig.AutoConfig;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.screen.Screen;
@Environment(EnvType.CLIENT)
public class ModMenuImpl implements ModMenuApi {
@Override
public ConfigScreenFactory<?> getModConfigScreenFactory() {
return parent -> AutoConfig.getConfigScreen(MCPHTConfig.class, parent).get();
return parent -> {
Screen screen = AutoConfig.getConfigScreen(MCPHTConfig.class, parent).get();
return screen;
};
}
}

View File

@ -1,15 +0,0 @@
package de.cscherr.mcpht.mixin.client;
import net.minecraft.client.MinecraftClient;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(MinecraftClient.class)
public class ExampleClientMixin {
@Inject(at = @At("HEAD"), method = "run")
private void run(CallbackInfo info) {
// This code is injected into the start of MinecraftClient.run()V
}
}

View File

@ -0,0 +1,71 @@
package de.cscherr.mcpht.mixin.client;
import de.cscherr.mcpht.MCPHTClient;
import de.cscherr.mcpht.config.MCPHTConfig;
import net.minecraft.network.ClientConnection;
import org.slf4j.Logger;
import net.minecraft.network.PacketCallbacks;
import net.minecraft.network.listener.PacketListener;
import net.minecraft.network.packet.Packet;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.jetbrains.annotations.Nullable;
import java.util.regex.Matcher;
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"))
private void logSentPacket(Packet<?> packet, @Nullable PacketCallbacks callbacks, CallbackInfo ci) {
if (MCPHTClient.CONFIG.networkLogging.TX &&
MCPHTClient.CONFIG.networkLogging.filter.matcher(packet.getClass().getSimpleName()).matches()) {
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(), packet.toString());
break;
}
}
LOGGER.info(String.format("Sent 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 logReceivedPacket(Packet<?> packet, PacketListener listener, CallbackInfo ci) {
if (MCPHTClient.CONFIG.networkLogging.RX) {
LOGGER.info(String.format("Received Package: %s",
packet.getClass().getSimpleName()
));
}
}
}

View File

@ -3,7 +3,7 @@
"package": "de.cscherr.mcpht.mixin.client",
"compatibilityLevel": "JAVA_17",
"client": [
"ExampleClientMixin"
"NetworkPacketLogMixin"
],
"injectors": {
"defaultRequire": 1

View File

@ -5,7 +5,7 @@ import net.fabricmc.api.ModInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Mcpht implements ModInitializer {
public class MCPHT implements ModInitializer {
public static final String MOD_ID = "mcpht";
public static final String VERSION_NUMBER = "0.0.1";
// This logger is used to write text to the console and the log file.

View File

@ -1,15 +0,0 @@
package de.cscherr.mcpht.mixin;
import net.minecraft.server.MinecraftServer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(MinecraftServer.class)
public class ExampleMixin {
@Inject(at = @At("HEAD"), method = "loadWorld")
private void init(CallbackInfo info) {
// This code is injected into the start of MinecraftServer.loadWorld()V
}
}

View File

@ -0,0 +1,4 @@
accessWidener v2 named
# I just need some damn regex, come on
accessible class java/utils/regex/Pattern
accessible class java/utils/regex/Matcher

View File

@ -1,5 +1,7 @@
{
"text.autoconfig.mcphtconf.option.networkLogging": "Network Packet Logging",
"text.autoconfig.mcphtconf.option.networkLogging.RX": "RX",
"text.autoconfig.mcphtconf.option.networkLogging.TX": "TX"
"text.autoconfig.mcphtconf.option.networkLogging.TX": "TX",
"text.autoconfig.mcphtconf.option.networkLogging.regex": "Regex Filter",
"text.autoconfig.mcphtconf.option.networkLogging.verbosity": "Log verbosity"
}

View File

@ -20,8 +20,12 @@
"icon": "assets/mcpht/icon.png",
"environment": "*",
"entrypoints": {
"main": ["de.cscherr.mcpht.Mcpht"],
"client": ["de.cscherr.mcpht.McphtClient"],
"main": [
"de.cscherr.mcpht.MCPHT"
],
"client": [
"de.cscherr.mcpht.MCPHTClient"
],
"modmenu": [
"de.cscherr.mcpht.config.ModMenuImpl"
]
@ -47,5 +51,6 @@
"modmenu": {
"update_checker": true
}
}
},
"accessWidener": "mcpht.accesswidener"
}

View File

@ -3,7 +3,6 @@
"package": "de.cscherr.mcpht.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"ExampleMixin"
],
"injectors": {
"defaultRequire": 1