@@ -43,7 +43,7 @@ public class Plugin : BaseUnityPlugin
4343 internal static new ConfigFile Config ;
4444 internal static ModDataGlobals Globals ;
4545 internal static InternalData GlobalsInternal ;
46- private readonly Harmony HarmonyManager = new ( MyPluginInfo . PLUGIN_GUID ) ;
46+ private Harmony HarmonyInstance ;
4747
4848 /*
4949 * @brief
@@ -57,6 +57,12 @@ private void Awake()
5757 string ServerName , PlayerName , GameMode ;
5858 bool ToggleMod , VoiceChat ;
5959
60+ /*
61+ * Start patching the game assembly with our code
62+ * and cache a harmony instance of this mod.
63+ */
64+ HarmonyInstance = Harmony . CreateAndPatchAll ( Assembly . GetExecutingAssembly ( ) ) ;
65+
6066 /* Cache the plugin class methods so that the COTL MP mod can use them across different modules */
6167 Logger = base . Logger ;
6268 Config = base . Config ;
@@ -80,7 +86,7 @@ private void Awake()
8086 if ( SettingData == null )
8187 {
8288 Logger . LogFatal ( "Failed to set default or load the \" Mod Toggle\" setting!" ) ;
83- Harmony . UnpatchAll ( ) ;
89+ HarmonyInstance . UnpatchSelf ( ) ;
8490 return ;
8591 }
8692
@@ -95,7 +101,7 @@ private void Awake()
95101 if ( SettingData == null )
96102 {
97103 Logger . LogFatal ( "Failed to set default or load the \" Game Mode\" setting!" ) ;
98- Harmony . UnpatchAll ( ) ;
104+ HarmonyInstance . UnpatchSelf ( ) ;
99105 return ;
100106 }
101107
@@ -110,7 +116,7 @@ private void Awake()
110116 if ( SettingData == null )
111117 {
112118 Logger . LogFatal ( "Failed to set default or load the \" Player Name\" setting!" ) ;
113- Harmony . UnpatchAll ( ) ;
119+ HarmonyInstance . UnpatchSelf ( ) ;
114120 return ;
115121 }
116122
@@ -125,7 +131,7 @@ private void Awake()
125131 if ( SettingData == null )
126132 {
127133 Logger . LogFatal ( "Failed to set default or load the \" Server Name\" setting!" ) ;
128- Harmony . UnpatchAll ( ) ;
134+ HarmonyInstance . UnpatchSelf ( ) ;
129135 return ;
130136 }
131137
@@ -140,7 +146,7 @@ private void Awake()
140146 if ( SettingData == null )
141147 {
142148 Logger . LogFatal ( "Failed to set default or load the \" Max Players\" setting!" ) ;
143- Harmony . UnpatchAll ( ) ;
149+ HarmonyInstance . UnpatchSelf ( ) ;
144150 return ;
145151 }
146152
@@ -155,7 +161,7 @@ private void Awake()
155161 if ( SettingData == null )
156162 {
157163 Logger . LogFatal ( "Failed to set default or load the \" Toggle Voice Chat\" setting!" ) ;
158- Harmony . UnpatchAll ( ) ;
164+ HarmonyInstance . UnpatchSelf ( ) ;
159165 return ;
160166 }
161167
@@ -179,13 +185,12 @@ private void Awake()
179185
180186 /*
181187 * @brief
182- * Patches the game assembly with Harmony patches set up
183- * by the mod.
188+ * Performs additional tasks after the successful initialization
189+ * of the mod.
184190 */
185191 private void OnEnable ( )
186192 {
187- HarmonyManager . PatchAll ( Assembly . GetExecutingAssembly ( ) ) ;
188- Logger . LogMessage ( $ "{ HarmonyManager . GetPatchedMethods ( ) . Count ( ) } patches have been applied!") ;
193+ Logger . LogMessage ( $ "{ HarmonyInstance . GetPatchedMethods ( ) . Count ( ) } patches have been applied!") ;
189194 }
190195
191196 /*
@@ -196,7 +201,7 @@ private void OnEnable()
196201 private void OnDisable ( )
197202 {
198203 Logger . LogMessage ( $ "Unloading { MyPluginInfo . PLUGIN_GUID } ") ;
199- HarmonyManager . UnpatchSelf ( ) ;
204+ HarmonyInstance . UnpatchSelf ( ) ;
200205 Logger . LogMessage ( "Mod has been unloaded!" ) ;
201206 }
202207}
0 commit comments