[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 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<Config>(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));