diff --git a/build.gradle b/build.gradle index 6a863a2..0a14e1c 100644 --- a/build.gradle +++ b/build.gradle @@ -29,6 +29,7 @@ loom { sourceSet sourceSets.client } } + accessWidenerPath = file("src/main/resources/.accesswidener") } diff --git a/src/client/java/de/cscherr/mcpht/MCPHTClient.java b/src/client/java/de/cscherr/mcpht/MCPHTClient.java new file mode 100644 index 0000000..2138b7e --- /dev/null +++ b/src/client/java/de/cscherr/mcpht/MCPHTClient.java @@ -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!"); + } +} \ No newline at end of file diff --git a/src/client/java/de/cscherr/mcpht/McphtClient.java b/src/client/java/de/cscherr/mcpht/McphtClient.java deleted file mode 100644 index b5acb2f..0000000 --- a/src/client/java/de/cscherr/mcpht/McphtClient.java +++ /dev/null @@ -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!"); - } -} \ 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 1be71ba..404009c 100644 --- a/src/client/java/de/cscherr/mcpht/config/MCPHTConfig.java +++ b/src/client/java/de/cscherr/mcpht/config/MCPHTConfig.java @@ -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; } } diff --git a/src/client/java/de/cscherr/mcpht/config/ModMenuImpl.java b/src/client/java/de/cscherr/mcpht/config/ModMenuImpl.java index db896cc..f333db5 100644 --- a/src/client/java/de/cscherr/mcpht/config/ModMenuImpl.java +++ b/src/client/java/de/cscherr/mcpht/config/ModMenuImpl.java @@ -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; + }; } } diff --git a/src/client/java/de/cscherr/mcpht/mixin/client/ExampleClientMixin.java b/src/client/java/de/cscherr/mcpht/mixin/client/ExampleClientMixin.java deleted file mode 100644 index 214fc9f..0000000 --- a/src/client/java/de/cscherr/mcpht/mixin/client/ExampleClientMixin.java +++ /dev/null @@ -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 - } -} \ No newline at end of file diff --git a/src/client/java/de/cscherr/mcpht/mixin/client/NetworkPacketLogMixin.java b/src/client/java/de/cscherr/mcpht/mixin/client/NetworkPacketLogMixin.java new file mode 100644 index 0000000..80a0f6a --- /dev/null +++ b/src/client/java/de/cscherr/mcpht/mixin/client/NetworkPacketLogMixin.java @@ -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() + )); + } + } + +} \ No newline at end of file diff --git a/src/client/resources/mcpht.client.mixins.json b/src/client/resources/mcpht.client.mixins.json index 089bfed..b3096c7 100644 --- a/src/client/resources/mcpht.client.mixins.json +++ b/src/client/resources/mcpht.client.mixins.json @@ -3,7 +3,7 @@ "package": "de.cscherr.mcpht.mixin.client", "compatibilityLevel": "JAVA_17", "client": [ - "ExampleClientMixin" + "NetworkPacketLogMixin" ], "injectors": { "defaultRequire": 1 diff --git a/src/main/java/de/cscherr/mcpht/Mcpht.java b/src/main/java/de/cscherr/mcpht/MCPHT.java similarity index 94% rename from src/main/java/de/cscherr/mcpht/Mcpht.java rename to src/main/java/de/cscherr/mcpht/MCPHT.java index 5ad8349..ddd2e7c 100644 --- a/src/main/java/de/cscherr/mcpht/Mcpht.java +++ b/src/main/java/de/cscherr/mcpht/MCPHT.java @@ -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. diff --git a/src/main/java/de/cscherr/mcpht/mixin/ExampleMixin.java b/src/main/java/de/cscherr/mcpht/mixin/ExampleMixin.java deleted file mode 100644 index 8bd918e..0000000 --- a/src/main/java/de/cscherr/mcpht/mixin/ExampleMixin.java +++ /dev/null @@ -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 - } -} \ No newline at end of file diff --git a/src/main/resources/.accesswidener b/src/main/resources/.accesswidener new file mode 100644 index 0000000..3072332 --- /dev/null +++ b/src/main/resources/.accesswidener @@ -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 \ No newline at end of file diff --git a/src/main/resources/assets/mcpht/lang/en_us.json b/src/main/resources/assets/mcpht/lang/en_us.json index 1295472..46866a5 100644 --- a/src/main/resources/assets/mcpht/lang/en_us.json +++ b/src/main/resources/assets/mcpht/lang/en_us.json @@ -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" } \ No newline at end of file diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index c62ca56..55cf5f9 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -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" } diff --git a/src/main/resources/mcpht.mixins.json b/src/main/resources/mcpht.mixins.json index dc47cfa..d6bf134 100644 --- a/src/main/resources/mcpht.mixins.json +++ b/src/main/resources/mcpht.mixins.json @@ -3,7 +3,6 @@ "package": "de.cscherr.mcpht.mixin", "compatibilityLevel": "JAVA_17", "mixins": [ - "ExampleMixin" ], "injectors": { "defaultRequire": 1