1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2025-12-09 01:33:33 +08:00
Files
DSP_Mods/UXAssist/Common/PatchImpl.cs
2024-09-18 19:36:31 +08:00

39 lines
1.0 KiB
C#

using System;
using System.Reflection;
using HarmonyLib;
namespace UXAssist.Common;
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class PatchImplGuidAttribute(string guid) : Attribute
{
public string Guid { get; } = guid;
}
public class PatchImpl<T> where T : PatchImpl<T>, new()
{
private static T Instance { get; } = new();
private Harmony _patch;
public static void Enable(bool enable)
{
var thisInstance = Instance;
if (enable)
{
var guid = typeof(T).GetCustomAttribute<PatchImplGuidAttribute>()?.Guid ?? $"PatchImpl.{typeof(T).FullName ?? typeof(T).ToString()}";
thisInstance._patch ??= Harmony.CreateAndPatchAll(typeof(T), guid);
thisInstance.OnEnable();
return;
}
thisInstance.OnDisable();
thisInstance._patch?.UnpatchSelf();
thisInstance._patch = null;
}
public static Harmony GetHarmony() => Instance._patch;
protected virtual void OnEnable() { }
protected virtual void OnDisable() { }
}