[O] Apply patches using reflection

pull/9/head
Azalea 2024-02-07 05:40:13 -05:00
parent fb58f08e44
commit 5f1ca64d65
1 changed files with 39 additions and 20 deletions

View File

@ -1,7 +1,5 @@
using System; using System;
using AquaMai.Cheat;
using AquaMai.Fix; using AquaMai.Fix;
using AquaMai.UX;
using MelonLoader; using MelonLoader;
using Tomlet; using Tomlet;
@ -21,12 +19,44 @@ namespace AquaMai
{ {
public static Config AppConfig { get; private set; } public static Config AppConfig { get; private set; }
private void Patch(Type type) private static void Patch(Type type)
{ {
MelonLogger.Msg($"> Patching {type}"); MelonLogger.Msg($"> Patching {type}");
HarmonyLib.Harmony.CreateAndPatchAll(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();
// 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() public override void OnInitializeMelon()
{ {
MelonLogger.Msg("Loading mod settings..."); MelonLogger.Msg("Loading mod settings...");
@ -41,19 +71,8 @@ namespace AquaMai
// Read AquaMai.toml to load settings // Read AquaMai.toml to load settings
AppConfig = TomletMain.To<Config>(System.IO.File.ReadAllText("AquaMai.toml")); AppConfig = TomletMain.To<Config>(System.IO.File.ReadAllText("AquaMai.toml"));
if (AppConfig.UX.SkipWarningScreen) // Apply patches based on the settings
Patch(typeof(SkipWarningScreen)); ApplyPatches();
if (AppConfig.UX.SinglePlayer)
Patch(typeof(SinglePlayer));
if (AppConfig.Cheat.TicketUnlock)
Patch(typeof(TicketUnlock));
if (AppConfig.UX.SkipToMusicSelection)
{
Patch(typeof(SkipToMusicSelection));
}
// Fixes that does not have side effects // Fixes that does not have side effects
// These don't need to be configurable // These don't need to be configurable