quantum-space-buddies/QSB/Patches/QSBPatchManager.cs

99 lines
2.9 KiB
C#
Raw Normal View History

2021-12-01 09:10:38 +00:00
using HarmonyLib;
using OWML.Common;
2020-11-03 21:11:10 +00:00
using QSB.Utility;
2021-12-24 01:07:29 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2020-11-03 21:11:10 +00:00
2022-03-03 03:46:33 +00:00
namespace QSB.Patches;
public static class QSBPatchManager
2020-11-03 21:11:10 +00:00
{
2022-03-03 03:46:33 +00: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();
2022-07-22 09:50:59 +00:00
private static PatchVendor _vendor = PatchVendor.None;
2020-11-04 20:01:28 +00:00
2022-03-03 03:46:33 +00:00
public static Dictionary<QSBPatchTypes, Harmony> TypeToInstance = new();
2020-12-14 20:41:56 +00:00
2022-03-03 03:46:33 +00:00
public static void Init()
{
foreach (var type in typeof(QSBPatch).GetDerivedTypes())
{
_patchList.Add((QSBPatch)Activator.CreateInstance(type));
}
foreach (QSBPatchTypes type in Enum.GetValues(typeof(QSBPatchTypes)))
2020-12-02 21:29:53 +00:00
{
TypeToInstance.Add(type, new Harmony(type.ToString()));
}
2020-11-03 21:11:10 +00:00
2022-07-22 09:50:59 +00:00
var gameAssemblyTypes = typeof(AstroObject).Assembly.GetTypes();
var isEpic = gameAssemblyTypes.Any(x => x.Name == "EpicEntitlementRetriever");
var isSteam = gameAssemblyTypes.Any(x => x.Name == "SteamEntitlementRetriever");
var isUWP = gameAssemblyTypes.Any(x => x.Name == "MSStoreEntitlementRetriever");
if (isEpic && !isSteam && !isUWP)
{
_vendor = PatchVendor.Epic;
}
else if (!isEpic && isSteam && !isUWP)
{
_vendor = PatchVendor.Steam;
}
else if (!isEpic && !isSteam && isUWP)
{
_vendor = PatchVendor.Gamepass;
}
else
{
// ???
DebugLog.ToConsole($"FATAL - Could not determine game vendor.", MessageType.Fatal);
}
DebugLog.DebugWrite($"Determined game vendor as {_vendor}", MessageType.Info);
2022-03-03 03:46:33 +00:00
DebugLog.DebugWrite("Patch Manager ready.", MessageType.Success);
}
2022-03-03 03:46:33 +00: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-03 03:46:33 +00:00
OnPatchType?.SafeInvoke(type);
//DebugLog.DebugWrite($"Patch block {Enum.GetName(typeof(QSBPatchTypes), type)}", MessageType.Info);
2022-07-22 09:50:59 +00:00
foreach (var patch in _patchList.Where(x => x.Type == type && x.PatchVendor.HasFlag(_vendor)))
2020-12-02 21:29:53 +00:00
{
//DebugLog.DebugWrite($" - Patching in {patch.GetType().Name}", MessageType.Info);
2022-03-03 03:46:33 +00:00
try
{
2022-03-03 03:46:33 +00:00
patch.DoPatches(TypeToInstance[type]);
}
2022-03-03 03:46:33 +00:00
catch (Exception ex)
2020-12-02 21:29:53 +00:00
{
DebugLog.ToConsole($"Error while patching {patch.GetType().Name} :\r\n{ex}", MessageType.Error);
2020-12-02 21:29:53 +00:00
}
}
2022-03-03 03:46:33 +00:00
_patchedTypes.Add(type);
}
2022-03-03 03:46:33 +00: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-03-03 03:46:33 +00: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
}
}