1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2026-02-04 19:42:17 +08:00

UXAssist 1.2.4

This commit is contained in:
2024-09-24 00:33:41 +08:00
parent daaf7b4901
commit 237830fc91
13 changed files with 149 additions and 73 deletions

View File

@@ -1,15 +1,30 @@
using System;
using System.Linq;
using System.Reflection;
using HarmonyLib;
namespace UXAssist.Common;
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class PatchImplGuidAttribute(string guid) : Attribute
public class PatchGuidAttribute(string guid) : Attribute
{
public string Guid { get; } = guid;
}
public enum PatchCallbackFlag
{
// OnEnable() is called After patch is applied by default, set this flag to call it before patch is applied
CallOnEnableBeforePatch,
// OnDisable() is called Before patch is removed by default, set this flag to call it after patch is removed
CallOnDisableAfterUnpatch,
}
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
public class PatchSetCallbackFlagAttribute(PatchCallbackFlag flag) : Attribute
{
public PatchCallbackFlag Flag { get; } = flag;
}
public class PatchImpl<T> where T : PatchImpl<T>, new()
{
private static T Instance { get; } = new();
@@ -21,14 +36,18 @@ public class PatchImpl<T> where T : PatchImpl<T>, new()
var thisInstance = Instance;
if (enable)
{
var guid = typeof(T).GetCustomAttribute<PatchImplGuidAttribute>()?.Guid ?? $"PatchImpl.{typeof(T).FullName ?? typeof(T).ToString()}";
var guid = typeof(T).GetCustomAttribute<PatchGuidAttribute>()?.Guid ?? $"PatchImpl.{typeof(T).FullName ?? typeof(T).ToString()}";
var callOnEnableBefore = typeof(T).GetCustomAttributes<PatchSetCallbackFlagAttribute>().Any(n => n.Flag == PatchCallbackFlag.CallOnEnableBeforePatch);
if (callOnEnableBefore) thisInstance.OnEnable();
thisInstance._patch ??= Harmony.CreateAndPatchAll(typeof(T), guid);
thisInstance.OnEnable();
if (!callOnEnableBefore) thisInstance.OnEnable();
return;
}
thisInstance.OnDisable();
var callOnDisableAfter = typeof(T).GetCustomAttributes<PatchSetCallbackFlagAttribute>().Any(n => n.Flag == PatchCallbackFlag.CallOnDisableAfterUnpatch);
if (!callOnDisableAfter) thisInstance.OnDisable();
thisInstance._patch?.UnpatchSelf();
thisInstance._patch = null;
if (callOnDisableAfter) thisInstance.OnDisable();
}
public static Harmony GetHarmony() => Instance._patch;