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

136 lines
3.9 KiB
C#
Raw Normal View History

using HarmonyLib;
using OWML.Common;
2021-11-25 15:32:34 +00:00
using QSB.Anglerfish.Patches;
2021-04-29 17:30:45 +00:00
using QSB.Animation.NPC.Patches;
2021-04-23 23:10:29 +00:00
using QSB.Animation.Patches;
2021-03-29 22:36:51 +00:00
using QSB.CampfireSync.Patches;
using QSB.ConversationSync.Patches;
using QSB.DeathSync.Patches;
2021-11-09 19:39:56 +00:00
using QSB.EchoesOfTheEye.LightSensorSync.Patches;
using QSB.ElevatorSync.Patches;
2021-03-25 21:10:20 +00:00
using QSB.GeyserSync.Patches;
2021-06-11 13:34:27 +00:00
using QSB.Inputs.Patches;
2021-02-23 14:42:18 +00:00
using QSB.ItemSync.Patches;
using QSB.LogSync.Patches;
2021-11-25 15:32:34 +00:00
using QSB.MeteorSync.Patches;
using QSB.OrbSync.Patches;
2021-04-18 08:29:12 +00:00
using QSB.Player.Patches;
using QSB.PoolSync.Patches;
using QSB.QuantumSync.Patches;
2021-03-30 16:28:05 +00:00
using QSB.RoastingSync.Patches;
2021-11-03 09:18:52 +00:00
using QSB.SatelliteSync.Patches;
2021-04-12 10:31:21 +00:00
using QSB.ShipSync.Patches;
2021-02-28 14:43:05 +00:00
using QSB.StatueSync.Patches;
2021-06-12 14:58:34 +00:00
using QSB.TimeSync.Patches;
using QSB.Tools.ProbeLauncherTool.Patches;
2021-11-25 15:32:34 +00:00
using QSB.Tools.SignalscopeTool.FrequencySync.Patches;
2021-11-25 15:25:17 +00:00
using QSB.Tools.TranslatorTool.TranslationSync.Patches;
2020-11-03 21:11:10 +00:00
using QSB.Utility;
2021-11-29 22:57:13 +00:00
using QSB.ZeroGCaveSync.Patches;
2020-11-03 21:11:10 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
namespace QSB.Patches
{
2020-12-02 21:29:53 +00:00
public static class QSBPatchManager
{
2021-02-06 22:03:10 +00:00
public static event Action<QSBPatchTypes> OnPatchType;
2021-02-09 17:18:01 +00:00
public static event Action<QSBPatchTypes> OnUnpatchType;
2020-11-04 20:01:28 +00:00
2021-11-20 19:49:50 +00:00
private static List<QSBPatch> _patchList = new();
private static List<QSBPatchTypes> _patchedTypes = new();
2020-12-14 20:41:56 +00:00
2021-11-20 19:49:50 +00:00
public static Dictionary<QSBPatchTypes, Harmony> TypeToInstance = new();
2020-12-14 21:20:53 +00:00
public static void Init()
2020-12-02 21:29:53 +00:00
{
_patchList = new List<QSBPatch>
{
new ConversationPatches(),
new DeathPatches(),
new ElevatorPatches(),
new OrbPatches(),
2020-12-21 19:41:53 +00:00
new LogPatches(),
2020-12-22 21:39:53 +00:00
new QuantumVisibilityPatches(),
2021-01-18 12:33:07 +00:00
new ServerQuantumPatches(),
new ClientQuantumPatches(),
new FrequencyPatches(),
2021-01-18 12:33:07 +00:00
new SpiralPatches(),
2021-02-23 14:42:18 +00:00
new QuantumPatches(),
2021-02-28 14:43:05 +00:00
new ItemPatches(),
2021-03-25 21:10:20 +00:00
new StatuePatches(),
new GeyserPatches(),
2021-03-29 22:36:51 +00:00
new PoolPatches(),
2021-03-30 16:28:05 +00:00
new CampfirePatches(),
2021-04-12 10:31:21 +00:00
new RoastingPatches(),
2021-04-23 23:10:29 +00:00
new PlayerPatches(),
2021-04-26 13:30:21 +00:00
new PlayerAnimationPatches(),
2021-05-10 13:30:38 +00:00
new CharacterAnimationPatches(),
2021-06-11 13:34:27 +00:00
new ShipPatches(),
2021-06-12 14:58:34 +00:00
new InputPatches(),
2021-06-22 20:02:09 +00:00
new TimePatches(),
2021-06-23 11:06:08 +00:00
new MapPatches(),
new LauncherPatches(),
2021-11-03 09:18:52 +00:00
new SolanumPatches(),
2021-11-08 20:55:53 +00:00
new SatelliteProjectorPatches(),
2021-11-10 01:56:45 +00:00
new LightSensorPatches(),
2021-11-11 02:40:02 +00:00
new AnglerPatches(),
2021-11-11 05:13:49 +00:00
new MeteorClientPatches(),
2021-11-23 08:41:05 +00:00
new MeteorServerPatches(),
2021-11-29 22:57:13 +00:00
new TravelerControllerPatches(),
new ZeroGCavePatches()
2020-12-02 21:29:53 +00:00
};
2020-11-03 21:11:10 +00:00
TypeToInstance = new Dictionary<QSBPatchTypes, Harmony>
{
2021-10-16 13:15:25 +00:00
{ QSBPatchTypes.OnClientConnect, new Harmony("QSB.Client") },
{ QSBPatchTypes.OnServerClientConnect, new Harmony("QSB.Server") },
{ QSBPatchTypes.OnNonServerClientConnect, new Harmony("QSB.NonServer") },
{ QSBPatchTypes.RespawnTime, new Harmony("QSB.Death") }
};
2020-12-02 21:29:53 +00:00
DebugLog.DebugWrite("Patch Manager ready.", MessageType.Success);
}
2020-11-03 21:11:10 +00:00
2020-12-02 21:29:53 +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;
}
2021-02-06 22:03:10 +00:00
OnPatchType?.SafeInvoke(type);
2021-10-16 22:19:49 +00:00
//DebugLog.DebugWrite($"Patch block {Enum.GetName(typeof(QSBPatchTypes), type)}", MessageType.Info);
2020-12-02 21:29:53 +00:00
foreach (var patch in _patchList.Where(x => x.Type == type))
{
2021-10-16 22:19:49 +00:00
//DebugLog.DebugWrite($" - Patching in {patch.GetType().Name}", MessageType.Info);
try
{
patch.DoPatches(TypeToInstance[type]);
_patchedTypes.Add(type);
}
catch (Exception ex)
{
2021-10-24 09:47:25 +00:00
DebugLog.ToConsole($"Error while patching {patch.GetType().Name} :\r\n{ex}", MessageType.Error);
}
2020-12-02 21:29:53 +00:00
}
}
2021-02-09 17:18:01 +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;
}
2021-02-09 17:18:01 +00:00
OnUnpatchType?.SafeInvoke(type);
2021-10-16 22:19:49 +00:00
//DebugLog.DebugWrite($"Unpatch block {Enum.GetName(typeof(QSBPatchTypes), type)}", MessageType.Info);
TypeToInstance[type].UnpatchSelf();
_patchedTypes.Remove(type);
2021-02-09 17:18:01 +00:00
}
2020-12-02 21:29:53 +00:00
}
2021-11-10 01:56:45 +00:00
}