2021-12-01 01:10:38 -08:00
|
|
|
using HarmonyLib;
|
2021-10-15 21:06:51 +01:00
|
|
|
using OWML.Common;
|
2020-11-03 21:11:10 +00:00
|
|
|
using QSB.Utility;
|
2021-12-23 17:07:29 -08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-11-03 21:11:10 +00:00
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
namespace QSB.Patches;
|
|
|
|
|
|
|
|
public static class QSBPatchManager
|
2020-11-03 21:11:10 +00:00
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
public static event Action<QSBPatchTypes> OnPatchType;
|
|
|
|
public static event Action<QSBPatchTypes> OnUnpatchType;
|
|
|
|
|
|
|
|
private static readonly List<QSBPatch> _patchList = new();
|
|
|
|
private static readonly List<QSBPatchTypes> _patchedTypes = new();
|
2020-11-04 20:01:28 +00:00
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
public static Dictionary<QSBPatchTypes, Harmony> TypeToInstance = new();
|
2020-12-14 21:41:56 +01:00
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
public static void Init()
|
|
|
|
{
|
|
|
|
foreach (var type in typeof(QSBPatch).GetDerivedTypes())
|
|
|
|
{
|
|
|
|
_patchList.Add((QSBPatch)Activator.CreateInstance(type));
|
|
|
|
}
|
2021-10-15 21:06:51 +01:00
|
|
|
|
2022-03-11 10:21:27 +00:00
|
|
|
foreach (QSBPatchTypes type in Enum.GetValues(typeof(QSBPatchTypes)))
|
2020-12-02 21:29:53 +00:00
|
|
|
{
|
2022-03-11 10:21:27 +00:00
|
|
|
TypeToInstance.Add(type, new Harmony(type.ToString()));
|
|
|
|
}
|
2020-11-03 21:11:10 +00:00
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
DebugLog.DebugWrite("Patch Manager ready.", MessageType.Success);
|
|
|
|
}
|
2021-10-15 21:06:51 +01:00
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
public static void DoPatchType(QSBPatchTypes type)
|
|
|
|
{
|
|
|
|
if (_patchedTypes.Contains(type))
|
|
|
|
{
|
|
|
|
DebugLog.ToConsole($"Warning - Tried to patch type {type}, when it has already been patched!", MessageType.Warning);
|
|
|
|
return;
|
2020-12-02 21:29:53 +00:00
|
|
|
}
|
2020-11-03 21:11:10 +00:00
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
OnPatchType?.SafeInvoke(type);
|
|
|
|
//DebugLog.DebugWrite($"Patch block {Enum.GetName(typeof(QSBPatchTypes), type)}", MessageType.Info);
|
|
|
|
foreach (var patch in _patchList.Where(x => x.Type == type))
|
2020-12-02 21:29:53 +00:00
|
|
|
{
|
2022-03-11 00:58:14 -08:00
|
|
|
//DebugLog.DebugWrite($" - Patching in {patch.GetType().Name}", MessageType.Info);
|
2022-03-02 19:46:33 -08:00
|
|
|
try
|
2021-11-18 16:00:51 +00:00
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
patch.DoPatches(TypeToInstance[type]);
|
2021-11-18 16:00:51 +00:00
|
|
|
}
|
2022-03-02 19:46:33 -08:00
|
|
|
catch (Exception ex)
|
2020-12-02 21:29:53 +00:00
|
|
|
{
|
2022-03-11 00:58:14 -08:00
|
|
|
DebugLog.ToConsole($"Error while patching {patch.GetType().Name} :\r\n{ex}", MessageType.Error);
|
2020-12-02 21:29:53 +00:00
|
|
|
}
|
2022-02-27 04:40:44 -08:00
|
|
|
}
|
2021-11-18 16:00:51 +00:00
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
_patchedTypes.Add(type);
|
|
|
|
}
|
2022-02-24 22:04:54 -08:00
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
public static void DoUnpatchType(QSBPatchTypes type)
|
|
|
|
{
|
|
|
|
if (!_patchedTypes.Contains(type))
|
|
|
|
{
|
|
|
|
DebugLog.ToConsole($"Warning - Tried to unpatch type {type}, when it is either unpatched or was never patched.", MessageType.Warning);
|
|
|
|
return;
|
2022-02-27 04:40:44 -08:00
|
|
|
}
|
2022-03-02 19:46:33 -08:00
|
|
|
|
|
|
|
OnUnpatchType?.SafeInvoke(type);
|
|
|
|
//DebugLog.DebugWrite($"Unpatch block {Enum.GetName(typeof(QSBPatchTypes), type)}", MessageType.Info);
|
|
|
|
TypeToInstance[type].UnpatchSelf();
|
|
|
|
_patchedTypes.Remove(type);
|
2020-12-02 21:29:53 +00:00
|
|
|
}
|
2022-02-24 22:04:54 -08:00
|
|
|
}
|