Skip to content

Commit 5914e49

Browse files
committed
[COTLMP] Move the patching procedure on appropriate methods
Execute `PatchAll` in OnEnable after the mod has fully initialized its core stuff and unload it in OnDisable. Count the number of patched methods as well.
1 parent cbf8a34 commit 5914e49

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

COTLMP/Plugin.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using HarmonyLib;
1717
using System;
1818
using System.IO;
19+
using System.Linq;
1920
using System.Reflection;
2021

2122
/* NAMESPACES *****************************************************************/
@@ -42,6 +43,7 @@ public class Plugin : BaseUnityPlugin
4243
internal static new ConfigFile Config;
4344
internal static ModDataGlobals Globals;
4445
internal static InternalData GlobalsInternal;
46+
private readonly Harmony HarmonyManager = new(MyPluginInfo.PLUGIN_GUID);
4547

4648
/*
4749
* @brief
@@ -55,9 +57,6 @@ private void Awake()
5557
string ServerName, PlayerName, GameMode;
5658
bool ToggleMod, VoiceChat;
5759

58-
/* Start patching the game assembly with our code */
59-
Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly());
60-
6160
/* Cache the plugin class methods so that the COTL MP mod can use them across different modules */
6261
Logger = base.Logger;
6362
Config = base.Config;
@@ -177,6 +176,29 @@ private void Awake()
177176
/* Log to the debugger that our mod is loaded */
178177
Logger.LogInfo($"Plugin {MyPluginInfo.PLUGIN_GUID} is loaded!");
179178
}
179+
180+
/*
181+
* @brief
182+
* Patches the game assembly with Harmony patches set up
183+
* by the mod.
184+
*/
185+
private void OnEnable()
186+
{
187+
HarmonyManager.PatchAll(Assembly.GetExecutingAssembly());
188+
Logger.LogMessage($"{HarmonyManager.GetPatchedMethods().Count()} patches have been applied!");
189+
}
190+
191+
/*
192+
* @brief
193+
* Unloads all the patches hooked into game on quit event
194+
* of the game.
195+
*/
196+
private void OnDisable()
197+
{
198+
Logger.LogMessage($"Unloading {MyPluginInfo.PLUGIN_GUID}");
199+
HarmonyManager.UnpatchSelf();
200+
Logger.LogMessage("Mod has been unloaded!");
201+
}
180202
}
181203

182204
/* EOF */

0 commit comments

Comments
 (0)