AquaDX/AquaMai/Main.cs

119 lines
3.9 KiB
C#
Raw Normal View History

2024-02-07 18:16:07 +08:00
using System;
using AquaMai.Fix;
using AquaMai.Helpers;
2024-04-08 14:59:54 +08:00
using AquaMai.UX;
2024-02-07 15:38:36 +08:00
using MelonLoader;
2024-02-07 17:06:58 +08:00
using Tomlet;
2024-02-07 13:45:47 +08:00
namespace AquaMai
{
public static class BuildInfo
{
public const string Name = "AquaMai";
public const string Description = "Mod for Sinmai";
public const string Author = "Aza";
public const string Company = null;
2024-09-12 21:56:23 +08:00
public const string Version = "1.0.5";
2024-02-07 13:45:47 +08:00
public const string DownloadLink = null;
}
public class AquaMai : MelonMod
{
2024-02-07 15:38:36 +08:00
public static Config AppConfig { get; private set; }
private void Patch(Type type)
2024-02-07 18:16:07 +08:00
{
MelonLogger.Msg($"> Patching {type}");
try
{
HarmonyInstance.PatchAll(type);
foreach (var nested in type.GetNestedTypes())
{
Patch(nested);
}
}
catch (Exception e)
{
MelonLogger.Error($"Failed to patch {type}: {e}");
}
2024-02-07 18:16:07 +08:00
}
2024-02-07 18:40:13 +08:00
/**
* Apply patches using reflection, based on the settings
*/
private void ApplyPatches()
2024-02-07 18:40:13 +08:00
{
// 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;
2024-02-07 18:40:13 +08:00
// Check if the boolean value is true
if (!(bool)settingProp.GetValue(categoryValue)) continue;
2024-02-07 18:40:13 +08:00
// Get the Type from the config directive name
var directiveType = Type.GetType($"AquaMai.{categoryProp.Name}.{settingProp.Name}");
2024-02-07 18:40:13 +08:00
// If the type is found, call the Patch method
if (directiveType != null)
{
Patch(directiveType);
}
2024-02-07 18:40:13 +08:00
else MelonLogger.Error($"Type not found for {categoryProp.Name}.{settingProp.Name}");
}
}
}
public override void OnInitializeMelon()
2024-02-07 13:45:47 +08:00
{
2024-02-07 16:53:13 +08:00
MelonLogger.Msg("Loading mod settings...");
2024-02-07 17:06:58 +08:00
// Check if AquaMai.toml exists
if (!System.IO.File.Exists("AquaMai.toml"))
{
MelonLogger.Error("AquaMai.toml not found! Please create it.");
return;
}
2024-02-07 17:06:58 +08:00
// Read AquaMai.toml to load settings
AppConfig = TomletMain.To<Config>(System.IO.File.ReadAllText("AquaMai.toml"));
2024-09-06 23:15:53 +08:00
// Migrate old settings
AppConfig.UX.LoadAssetsPng = AppConfig.UX.LoadAssetsPng || AppConfig.UX.LoadJacketPng;
AppConfig.UX.LoadJacketPng = false;
2024-02-07 18:16:07 +08:00
// Fixes that does not have side effects
// These don't need to be configurable
// Helpers
Patch(typeof(MessageHelper));
Patch(typeof(MusicDirHelper));
Patch(typeof(SharedInstances));
// Fixes
2024-02-07 18:16:07 +08:00
Patch(typeof(FixCharaCrash));
Patch(typeof(BasicFix));
Patch(typeof(DisableReboot));
Patch(typeof(ExtendNotesPool));
Patch(typeof(FixCheckAuth));
// UX
2024-04-08 14:59:54 +08:00
Patch(typeof(CustomVersionString));
2024-09-02 23:02:47 +08:00
Patch(typeof(CustomPlaceName));
Patch(typeof(RunCommandOnEvents));
// Apply patches based on the settings
ApplyPatches();
2024-02-07 16:53:13 +08:00
MelonLogger.Msg("Loaded!");
2024-02-07 13:45:47 +08:00
}
}
}