1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2025-12-09 02:53:29 +08:00
This commit is contained in:
2024-09-18 19:36:31 +08:00
parent bd512a6b78
commit f5b98c1c48
12 changed files with 250 additions and 214 deletions

View File

@@ -10,22 +10,18 @@ public class PatchImplGuidAttribute(string guid) : Attribute
public string Guid { get; } = guid;
}
public class PatchImpl<T> where T : new()
public class PatchImpl<T> where T : PatchImpl<T>, new()
{
private static T Instance { get; } = new();
private Harmony _patch;
public static void Enable(bool enable)
{
if (Instance is not PatchImpl<T> thisInstance)
{
UXAssist.Logger.LogError($"PatchImpl<{typeof(T).Name}> is not inherited correctly");
return;
}
var thisInstance = Instance;
if (enable)
{
var guid = typeof(T).GetCustomAttribute<PatchImplGuidAttribute>()?.Guid;
var guid = typeof(T).GetCustomAttribute<PatchImplGuidAttribute>()?.Guid ?? $"PatchImpl.{typeof(T).FullName ?? typeof(T).ToString()}";
thisInstance._patch ??= Harmony.CreateAndPatchAll(typeof(T), guid);
thisInstance.OnEnable();
return;
@@ -35,7 +31,7 @@ public class PatchImpl<T> where T : new()
thisInstance._patch = null;
}
public static Harmony GetHarmony() => (Instance as PatchImpl<T>)?._patch;
public static Harmony GetHarmony() => Instance._patch;
protected virtual void OnEnable() { }
protected virtual void OnDisable() { }