From 5f1ca64d65b50f3a0fde43672cee96a74b7834be Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Wed, 7 Feb 2024 05:40:13 -0500 Subject: [PATCH] [O] Apply patches using reflection --- AquaMai/Main.cs | 59 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/AquaMai/Main.cs b/AquaMai/Main.cs index 472533f0..a69e69aa 100644 --- a/AquaMai/Main.cs +++ b/AquaMai/Main.cs @@ -1,7 +1,5 @@ using System; -using AquaMai.Cheat; using AquaMai.Fix; -using AquaMai.UX; using MelonLoader; using Tomlet; @@ -20,14 +18,46 @@ namespace AquaMai public class AquaMai : MelonMod { public static Config AppConfig { get; private set; } - - private void Patch(Type type) + + private static void Patch(Type type) { MelonLogger.Msg($"> Patching {type}"); HarmonyLib.Harmony.CreateAndPatchAll(type); } + + /** + * Apply patches using reflection, based on the settings + */ + private static void ApplyPatches() + { + // Iterate over all properties of AppConfig + foreach (var categoryProp in AppConfig.GetType().GetProperties()) + { + // Get the value of the category property (e.g., UX, Cheat) + var categoryValue = categoryProp.GetValue(AppConfig); + if (categoryValue == null) continue; + var categoryType = categoryValue.GetType(); - public override void OnInitializeMelon() + // Iterate over properties in the category (e.g., SkipWarningScreen, SinglePlayer) + foreach (var settingProp in categoryType.GetProperties()) + { + // The property should be a boolean + if (settingProp.PropertyType != typeof(bool)) continue; + + // Check if the boolean value is true + if (!(bool) settingProp.GetValue(categoryValue)) continue; + + // Get the Type from the config directive name + var directiveType = Type.GetType($"AquaMai.{categoryProp.Name}.{settingProp.Name}"); + + // If the type is found, call the Patch method + if (directiveType != null) Patch(directiveType); + else MelonLogger.Error($"Type not found for {categoryProp.Name}.{settingProp.Name}"); + } + } + } + + public override void OnInitializeMelon() { MelonLogger.Msg("Loading mod settings..."); @@ -40,21 +70,10 @@ namespace AquaMai // Read AquaMai.toml to load settings AppConfig = TomletMain.To(System.IO.File.ReadAllText("AquaMai.toml")); - - if (AppConfig.UX.SkipWarningScreen) - Patch(typeof(SkipWarningScreen)); - - if (AppConfig.UX.SinglePlayer) - Patch(typeof(SinglePlayer)); - - if (AppConfig.Cheat.TicketUnlock) - Patch(typeof(TicketUnlock)); - - if (AppConfig.UX.SkipToMusicSelection) - { - Patch(typeof(SkipToMusicSelection)); - } - + + // Apply patches based on the settings + ApplyPatches(); + // Fixes that does not have side effects // These don't need to be configurable Patch(typeof(FixCharaCrash));