Files
AquaDX/AquaMai/AquaMai.Core/Helpers/EnableConditionHelper.cs
Menci 37044dae01 [RF] AquaMai configuration refactor (#82)
更新了配置文件格式,原有的配置文件将被自动无缝迁移,详情请见新的配置文件中的注释(例外:`SlideJudgeTweak` 不再默认启用)
旧配置文件将被重命名备份,如果更新到此版本遇到 Bug 请联系我们

Updated configuration file schema. The old config file will be migrated automatically and seamlessly. See the comments in the new configuration file for details. (Except for `SlideJudgeTweak` is no longer enabled by default)
Your old configuration file will be renamed as a backup. If you encounter any bug with this version, please contact us.
2024-11-25 01:25:19 +08:00

73 lines
2.3 KiB
C#

using System;
using System.Collections;
using System.Reflection;
using AquaMai.Core.Attributes;
using AquaMai.Core.Resources;
using HarmonyLib;
using MelonLoader;
namespace AquaMai.Core.Helpers;
public class EnableConditionHelper
{
[HarmonyPostfix]
[HarmonyPatch("HarmonyLib.PatchTools", "GetPatchMethod")]
public static void PostGetPatchMethod(ref MethodInfo __result)
{
if (__result != null)
{
if (ShouldSkipMethodOrClass(__result.GetCustomAttribute, __result.ReflectedType, __result.Name))
{
__result = null;
}
}
}
[HarmonyPostfix]
[HarmonyPatch("HarmonyLib.PatchTools", "GetPatchMethods")]
public static void PostGetPatchMethods(ref IList __result)
{
for (int i = 0; i < __result.Count; i++)
{
var harmonyMethod = Traverse.Create(__result[i]).Field("info").GetValue() as HarmonyMethod;
var method = harmonyMethod.method;
if (ShouldSkipMethodOrClass(method.GetCustomAttribute, method.ReflectedType, method.Name))
{
__result.RemoveAt(i);
i--;
}
}
}
public static bool ShouldSkipClass(Type type)
{
return ShouldSkipMethodOrClass(type.GetCustomAttribute, type);
}
private static bool ShouldSkipMethodOrClass(Func<Type, object> getCustomAttribute, Type type, string methodName = "")
{
var displayName = type.FullName + (string.IsNullOrEmpty(methodName) ? "" : $".{methodName}");
var enableIf = (EnableIfAttribute)getCustomAttribute(typeof(EnableIfAttribute));
if (enableIf != null && !enableIf.ShouldEnable(type))
{
# if DEBUG
MelonLogger.Msg($"Skipping {displayName} due to EnableIf condition");
# endif
return true;
}
var enableGameVersion = (EnableGameVersionAttribute)getCustomAttribute(typeof(EnableGameVersionAttribute));
if (enableGameVersion != null && !enableGameVersion.ShouldEnable(GameInfo.GameVersion))
{
# if DEBUG
MelonLogger.Msg($"Skipping {displayName} due to EnableGameVersion condition");
# endif
if (!enableGameVersion.NoWarn)
{
MelonLogger.Warning(string.Format(Locale.SkipIncompatiblePatch, type));
}
return true;
}
return false;
}
}