From 734e45cf9f7da4b51664b9ae0c75425c2dce9d78 Mon Sep 17 00:00:00 2001 From: Sinai <49360850+sinai-dev@users.noreply.github.com> Date: Wed, 21 Jul 2021 04:13:42 +1000 Subject: [PATCH] Remove patching in mod loader classes --- src/Loader/BIE/ExplorerBepInPlugin.cs | 49 +--------------- src/Loader/IExplorerLoader.cs | 2 - src/Loader/ML/ExplorerMelonMod.cs | 43 -------------- src/Loader/STANDALONE/ExplorerStandalone.cs | 65 +++++---------------- 4 files changed, 16 insertions(+), 143 deletions(-) diff --git a/src/Loader/BIE/ExplorerBepInPlugin.cs b/src/Loader/BIE/ExplorerBepInPlugin.cs index cdd1103..36295ad 100644 --- a/src/Loader/BIE/ExplorerBepInPlugin.cs +++ b/src/Loader/BIE/ExplorerBepInPlugin.cs @@ -51,8 +51,7 @@ namespace UnityExplorer public Action OnLogWarning => LogSource.LogWarning; public Action OnLogError => LogSource.LogError; - // Init common to Mono and Il2Cpp - internal void UniversalInit() + private void Init() { Instance = this; _configHandler = new BepInExConfigHandler(); @@ -62,57 +61,15 @@ namespace UnityExplorer #if MONO // Mono internal void Awake() { - UniversalInit(); + Init(); } #else // Il2Cpp public override void Load() { - UniversalInit(); + Init(); } #endif - - public void SetupCursorPatches() - { - try - { - this.HarmonyInstance.PatchAll(); - } - catch (Exception ex) - { - ExplorerCore.Log($"Exception setting up Harmony patches:\r\n{ex.ReflectionExToString()}"); - } - } - - [HarmonyPatch(typeof(EventSystem), "current", MethodType.Setter)] - public class PATCH_EventSystem_current - { - [HarmonyPrefix] - public static void Prefix_EventSystem_set_current(ref EventSystem value) - { - CursorUnlocker.Prefix_EventSystem_set_current(ref value); - } - } - - [HarmonyPatch(typeof(Cursor), "lockState", MethodType.Setter)] - public class PATCH_Cursor_lockState - { - [HarmonyPrefix] - public static void Prefix_set_lockState(ref CursorLockMode value) - { - CursorUnlocker.Prefix_set_lockState(ref value); - } - } - - [HarmonyPatch(typeof(Cursor), "visible", MethodType.Setter)] - public class PATCH_Cursor_visible - { - [HarmonyPrefix] - public static void Prefix_set_visible(ref bool value) - { - CursorUnlocker.Prefix_set_visible(ref value); - } - } } } #endif \ No newline at end of file diff --git a/src/Loader/IExplorerLoader.cs b/src/Loader/IExplorerLoader.cs index 9aa8ea6..5a4d074 100644 --- a/src/Loader/IExplorerLoader.cs +++ b/src/Loader/IExplorerLoader.cs @@ -15,7 +15,5 @@ namespace UnityExplorer Action OnLogMessage { get; } Action OnLogWarning { get; } Action OnLogError { get; } - - void SetupCursorPatches(); } } diff --git a/src/Loader/ML/ExplorerMelonMod.cs b/src/Loader/ML/ExplorerMelonMod.cs index 25df105..61d82c1 100644 --- a/src/Loader/ML/ExplorerMelonMod.cs +++ b/src/Loader/ML/ExplorerMelonMod.cs @@ -9,12 +9,8 @@ using UnityExplorer.Core; using UnityExplorer.Core.Config; using UnityExplorer.Core.Input; using UnityExplorer.Loader.ML; -#if ML_LEGACY -using Harmony; -#else using HarmonyLib; [assembly: MelonPlatformDomain(MelonPlatformDomainAttribute.CompatibleDomains.UNIVERSAL)] -#endif [assembly: MelonInfo(typeof(ExplorerMelonMod), ExplorerCore.NAME, ExplorerCore.VERSION, ExplorerCore.AUTHOR)] [assembly: MelonGame(null, null)] @@ -42,45 +38,6 @@ namespace UnityExplorer ExplorerCore.Init(this); } - - public void SetupCursorPatches() - { - try - { - PrefixProperty(typeof(Cursor), - "lockState", - new HarmonyMethod(typeof(CursorUnlocker).GetMethod(nameof(CursorUnlocker.Prefix_set_lockState)))); - - PrefixProperty(typeof(Cursor), - "visible", - new HarmonyMethod(typeof(CursorUnlocker).GetMethod(nameof(CursorUnlocker.Prefix_set_visible)))); - - PrefixProperty(typeof(EventSystem), - "current", - new HarmonyMethod(typeof(CursorUnlocker).GetMethod(nameof(CursorUnlocker.Prefix_EventSystem_set_current)))); - } - catch (Exception ex) - { - ExplorerCore.Log($"Exception setting up Harmony patches:\r\n{ex.ReflectionExToString()}"); - } - } - - private void PrefixProperty(Type type, string property, HarmonyMethod prefix) - { - try - { - var prop = type.GetProperty(property); -#if ML_LEGACY - this.Harmony.Patch(prop.GetSetMethod(), prefix: prefix); -#else - HarmonyInstance.Patch(prop.GetSetMethod(), prefix: prefix); -#endif - } - catch (Exception e) - { - ExplorerCore.Log($"Unable to patch {type.Name}.set_{property}: {e.Message}"); - } - } } } #endif \ No newline at end of file diff --git a/src/Loader/STANDALONE/ExplorerStandalone.cs b/src/Loader/STANDALONE/ExplorerStandalone.cs index fb29c67..a2d8387 100644 --- a/src/Loader/STANDALONE/ExplorerStandalone.cs +++ b/src/Loader/STANDALONE/ExplorerStandalone.cs @@ -21,8 +21,7 @@ namespace UnityExplorer /// Call this to initialize UnityExplorer without adding a log listener. /// /// The new (or active, if one exists) instance of ExplorerStandalone. - public static ExplorerStandalone CreateInstance() - => CreateInstance(null); + public static ExplorerStandalone CreateInstance() => CreateInstance(null); /// /// Call this to initialize UnityExplorer and add a listener for UnityExplorer's log messages. @@ -34,7 +33,8 @@ namespace UnityExplorer if (Instance != null) return Instance; - OnLog += logListener; + if (logListener != null) + OnLog += logListener; var instance = new ExplorerStandalone(); instance.Init(); @@ -58,19 +58,7 @@ namespace UnityExplorer { get { - if (s_explorerFolder == null) - { - s_explorerFolder = - Path.Combine( - Path.GetDirectoryName( - Uri.UnescapeDataString(new Uri(Assembly.GetExecutingAssembly().CodeBase) - .AbsolutePath)), - "UnityExplorer"); - - if (!Directory.Exists(s_explorerFolder)) - Directory.CreateDirectory(s_explorerFolder); - } - + CheckExplorerFolder(); return s_explorerFolder; } } @@ -88,45 +76,18 @@ namespace UnityExplorer ExplorerCore.Init(this); } - public void SetupCursorPatches() + private void CheckExplorerFolder() { - try + if (s_explorerFolder == null) { - this.HarmonyInstance.PatchAll(); - } - catch (Exception ex) - { - ExplorerCore.Log($"Exception setting up Harmony patches:\r\n{ex.ReflectionExToString()}"); - } - } + s_explorerFolder = + Path.Combine( + Path.GetDirectoryName( + Uri.UnescapeDataString(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath)), + "UnityExplorer"); - [HarmonyPatch(typeof(EventSystem), "current", MethodType.Setter)] - public class PATCH_EventSystem_current - { - [HarmonyPrefix] - public static void Prefix_EventSystem_set_current(ref EventSystem value) - { - CursorUnlocker.Prefix_EventSystem_set_current(ref value); - } - } - - [HarmonyPatch(typeof(Cursor), "lockState", MethodType.Setter)] - public class PATCH_Cursor_lockState - { - [HarmonyPrefix] - public static void Prefix_set_lockState(ref CursorLockMode value) - { - CursorUnlocker.Prefix_set_lockState(ref value); - } - } - - [HarmonyPatch(typeof(Cursor), "visible", MethodType.Setter)] - public class PATCH_Cursor_visible - { - [HarmonyPrefix] - public static void Prefix_set_visible(ref bool value) - { - CursorUnlocker.Prefix_set_visible(ref value); + if (!Directory.Exists(s_explorerFolder)) + Directory.CreateDirectory(s_explorerFolder); } } }