From ab8b736f7e8e4b0418a289853d518413884a1046 Mon Sep 17 00:00:00 2001 From: Sinai Date: Sat, 1 May 2021 20:55:14 +1000 Subject: [PATCH] Improve Il2Cpp Coroutine support, make universal ExplorerBehaviour class --- src/Core/ExplorerBehaviour.cs | 62 +++++++++++++++++++++ src/Core/Input/CursorUnlocker.cs | 5 +- src/Core/Runtime/Il2Cpp/Il2CppCoroutine.cs | 7 ++- src/Core/Runtime/Il2Cpp/Il2CppProvider.cs | 5 ++ src/Core/Runtime/Mono/DummyBehaviour.cs | 29 ---------- src/Core/Runtime/Mono/MonoProvider.cs | 4 +- src/Core/Runtime/RuntimeProvider.cs | 8 +++ src/ExplorerCore.cs | 13 ++++- src/Loader/BIE/ExplorerBepInPlugin.cs | 38 ++----------- src/Loader/IExplorerLoader.cs | 2 +- src/Loader/ML/ExplorerMelonMod.cs | 10 ++-- src/Loader/STANDALONE/ExplorerStandalone.cs | 22 +------- src/UnityExplorer.csproj | 5 +- 13 files changed, 112 insertions(+), 98 deletions(-) create mode 100644 src/Core/ExplorerBehaviour.cs delete mode 100644 src/Core/Runtime/Mono/DummyBehaviour.cs diff --git a/src/Core/ExplorerBehaviour.cs b/src/Core/ExplorerBehaviour.cs new file mode 100644 index 0000000..31e260a --- /dev/null +++ b/src/Core/ExplorerBehaviour.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using UnityEngine; +#if CPP +using UnhollowerRuntimeLib; +#endif + +namespace UnityExplorer +{ + // Handles all Behaviour update calls for UnityExplorer (Update, FixedUpdate, OnPostRender). + // Basically just a wrapper which calls the corresponding methods in ExplorerCore. + + public class ExplorerBehaviour : MonoBehaviour + { + internal static ExplorerBehaviour Instance { get; private set; } + + internal static void Setup() + { +#if CPP + ClassInjector.RegisterTypeInIl2Cpp(); +#endif + + var obj = new GameObject("ExplorerBehaviour"); + GameObject.DontDestroyOnLoad(obj); + obj.hideFlags |= HideFlags.HideAndDontSave; + Instance = obj.AddComponent(); + } + +#if CPP + public ExplorerBehaviour(IntPtr ptr) : base(ptr) { } +#endif + + internal void Awake() + { +#if CPP + Camera.onPostRender = Camera.onPostRender == null + ? new Action(OnPostRender) + : Il2CppSystem.Delegate.Combine(Camera.onPostRender, (Camera.CameraCallback)new Action(OnPostRender)).Cast(); + +#else + Camera.onPostRender += OnPostRender; +#endif + } + + internal void Update() + { + ExplorerCore.Update(); + } + + internal void FixedUpdate() + { + ExplorerCore.FixedUpdate(); + } + + internal static void OnPostRender(Camera camera) + { + ExplorerCore.OnPostRender(); + } + } +} diff --git a/src/Core/Input/CursorUnlocker.cs b/src/Core/Input/CursorUnlocker.cs index 01caa29..c1e8f95 100644 --- a/src/Core/Input/CursorUnlocker.cs +++ b/src/Core/Input/CursorUnlocker.cs @@ -143,6 +143,9 @@ namespace UnityExplorer.Core.Input public static void ReleaseEventSystem() { + if (EventSystem.current != UIManager.EventSys) + return; + if (InputManager.CurrentType == InputType.InputSystem) return; @@ -173,7 +176,7 @@ namespace UnityExplorer.Core.Input m_lastVisibleState = (bool?)CursorType.GetProperty("visible", BF.Public | BF.Static)?.GetValue(null, null) ?? false; - ExplorerCore.Loader.SetupPatches(); + ExplorerCore.Loader.SetupCursorPatches(); } catch (Exception e) { diff --git a/src/Core/Runtime/Il2Cpp/Il2CppCoroutine.cs b/src/Core/Runtime/Il2Cpp/Il2CppCoroutine.cs index 8f33ba7..c2ac01f 100644 --- a/src/Core/Runtime/Il2Cpp/Il2CppCoroutine.cs +++ b/src/Core/Runtime/Il2Cpp/Il2CppCoroutine.cs @@ -124,8 +124,11 @@ namespace UnityExplorer.Core.Runtime.Il2Cpp if (nextAsEnumerator != null) // il2cpp IEnumerator also handles CustomYieldInstruction next = new Il2CppEnumeratorWrapper(nextAsEnumerator); else - ExplorerCore.LogWarning($"Unknown coroutine yield object of type {il2CppObjectBase} for coroutine {enumerator}"); - break; + ExplorerCore.LogWarning($"Unknown coroutine yield object of type '{il2CppObjectBase}' for coroutine '{enumerator}'"); + return; + default: + ExplorerCore.LogWarning($"Unknown coroutine yield object of type '{next}' for coroutine '{enumerator}'"); + return; } ourCoroutinesStore.Add(new CoroTuple { WaitCondition = next, Coroutine = enumerator }); diff --git a/src/Core/Runtime/Il2Cpp/Il2CppProvider.cs b/src/Core/Runtime/Il2Cpp/Il2CppProvider.cs index 945703a..06e9bea 100644 --- a/src/Core/Runtime/Il2Cpp/Il2CppProvider.cs +++ b/src/Core/Runtime/Il2Cpp/Il2CppProvider.cs @@ -56,6 +56,11 @@ namespace UnityExplorer.Core.Runtime.Il2Cpp Il2CppCoroutine.Start(routine); } + internal override void ProcessOnPostRender() + { + Il2CppCoroutine.ProcessWaitForEndOfFrame(); + } + public override void Update() { Il2CppCoroutine.Process(); diff --git a/src/Core/Runtime/Mono/DummyBehaviour.cs b/src/Core/Runtime/Mono/DummyBehaviour.cs deleted file mode 100644 index 3a4888f..0000000 --- a/src/Core/Runtime/Mono/DummyBehaviour.cs +++ /dev/null @@ -1,29 +0,0 @@ -#if MONO -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using UnityEngine; - -namespace UnityExplorer.Core.Runtime.Mono -{ - public class DummyBehaviour : MonoBehaviour - { - public static DummyBehaviour Instance; - - public static void Setup() - { - var obj = new GameObject("Explorer_DummyBehaviour"); - DontDestroyOnLoad(obj); - obj.hideFlags |= HideFlags.HideAndDontSave; - - obj.AddComponent(); - } - - internal void Awake() - { - Instance = this; - } - } -} -#endif \ No newline at end of file diff --git a/src/Core/Runtime/Mono/MonoProvider.cs b/src/Core/Runtime/Mono/MonoProvider.cs index 994cdd3..a861dd0 100644 --- a/src/Core/Runtime/Mono/MonoProvider.cs +++ b/src/Core/Runtime/Mono/MonoProvider.cs @@ -23,8 +23,6 @@ namespace UnityExplorer.Core.Runtime.Mono ExplorerCore.Context = RuntimeContext.Mono; Reflection = new MonoReflection(); TextureUtil = new MonoTextureUtil(); - - DummyBehaviour.Setup(); } public override void SetupEvents() @@ -39,7 +37,7 @@ namespace UnityExplorer.Core.Runtime.Mono public override void StartCoroutine(IEnumerator routine) { - DummyBehaviour.Instance.StartCoroutine(routine); + ExplorerBehaviour.Instance.StartCoroutine(routine); } public override void Update() diff --git a/src/Core/Runtime/RuntimeProvider.cs b/src/Core/Runtime/RuntimeProvider.cs index 60cb6f9..655dd14 100644 --- a/src/Core/Runtime/RuntimeProvider.cs +++ b/src/Core/Runtime/RuntimeProvider.cs @@ -65,5 +65,13 @@ namespace UnityExplorer public abstract void SetColorBlock(Selectable selectable, Color? normal = null, Color? highlighted = null, Color? pressed = null, Color? disabled = null); + + internal virtual void ProcessOnPostRender() + { + } + + internal virtual void ProcessFixedUpdate() + { + } } } diff --git a/src/ExplorerCore.cs b/src/ExplorerCore.cs index 55d52fd..76fc128 100644 --- a/src/ExplorerCore.cs +++ b/src/ExplorerCore.cs @@ -34,9 +34,10 @@ namespace UnityExplorer LogWarning("UnityExplorer is already loaded!"); return; } - Loader = loader; + ExplorerBehaviour.Setup(); + if (!Directory.Exists(Loader.ExplorerFolder)) Directory.CreateDirectory(Loader.ExplorerFolder); @@ -96,6 +97,16 @@ namespace UnityExplorer UIManager.Update(); } + public static void FixedUpdate() + { + RuntimeProvider.Instance.ProcessFixedUpdate(); + } + + public static void OnPostRender() + { + RuntimeProvider.Instance.ProcessOnPostRender(); + } + #region LOGGING public static void Log(object message) diff --git a/src/Loader/BIE/ExplorerBepInPlugin.cs b/src/Loader/BIE/ExplorerBepInPlugin.cs index 82082b2..79a866d 100644 --- a/src/Loader/BIE/ExplorerBepInPlugin.cs +++ b/src/Loader/BIE/ExplorerBepInPlugin.cs @@ -57,53 +57,23 @@ namespace UnityExplorer { Instance = this; _configHandler = new BepInExConfigHandler(); + ExplorerCore.Init(this); } -#if MONO // Mono-specific +#if MONO // Mono internal void Awake() { UniversalInit(); - ExplorerCore.Init(this); } - internal void Update() - { - ExplorerCore.Update(); - } - -#else // Il2Cpp-specific +#else // Il2Cpp public override void Load() { UniversalInit(); - - ClassInjector.RegisterTypeInIl2Cpp(); - - var obj = new GameObject("ExplorerBehaviour"); - obj.AddComponent(); - obj.hideFlags = HideFlags.HideAndDontSave; - GameObject.DontDestroyOnLoad(obj); - - ExplorerCore.Init(this); - } - - // BepInEx Il2Cpp mod class doesn't have monobehaviour methods yet, so wrap them in a dummy. - public class ExplorerBehaviour : MonoBehaviour - { - public ExplorerBehaviour(IntPtr ptr) : base(ptr) { } - - internal void Awake() - { - Instance.LogSource.LogMessage("ExplorerBehaviour.Awake"); - } - - internal void Update() - { - ExplorerCore.Update(); - } } #endif - public void SetupPatches() + public void SetupCursorPatches() { try { diff --git a/src/Loader/IExplorerLoader.cs b/src/Loader/IExplorerLoader.cs index 78a17b4..ac7520b 100644 --- a/src/Loader/IExplorerLoader.cs +++ b/src/Loader/IExplorerLoader.cs @@ -17,6 +17,6 @@ namespace UnityExplorer Action OnLogWarning { get; } Action OnLogError { get; } - void SetupPatches(); + void SetupCursorPatches(); } } diff --git a/src/Loader/ML/ExplorerMelonMod.cs b/src/Loader/ML/ExplorerMelonMod.cs index f01094c..5f0fdfc 100644 --- a/src/Loader/ML/ExplorerMelonMod.cs +++ b/src/Loader/ML/ExplorerMelonMod.cs @@ -41,12 +41,12 @@ namespace UnityExplorer ExplorerCore.Init(this); } - public override void OnUpdate() - { - ExplorerCore.Update(); - } + //public override void OnUpdate() + //{ + // ExplorerCore.Update(); + //} - public void SetupPatches() + public void SetupCursorPatches() { try { diff --git a/src/Loader/STANDALONE/ExplorerStandalone.cs b/src/Loader/STANDALONE/ExplorerStandalone.cs index 9a2ab59..27a3387 100644 --- a/src/Loader/STANDALONE/ExplorerStandalone.cs +++ b/src/Loader/STANDALONE/ExplorerStandalone.cs @@ -87,30 +87,10 @@ namespace UnityExplorer Instance = this; _configHandler = new StandaloneConfigHandler(); -#if CPP - ClassInjector.RegisterTypeInIl2Cpp(); -#endif - var obj = new GameObject("ExplorerBehaviour"); - obj.AddComponent(); - - GameObject.DontDestroyOnLoad(obj); - obj.hideFlags = HideFlags.HideAndDontSave; - ExplorerCore.Init(this); } - public class ExplorerBehaviour : MonoBehaviour - { -#if CPP - public ExplorerBehaviour(IntPtr ptr) : base(ptr) { } -#endif - internal void Update() - { - ExplorerCore.Update(); - } - } - - public void SetupPatches() + public void SetupCursorPatches() { try { diff --git a/src/UnityExplorer.csproj b/src/UnityExplorer.csproj index ab7c893..7c7c055 100644 --- a/src/UnityExplorer.csproj +++ b/src/UnityExplorer.csproj @@ -219,6 +219,7 @@ + @@ -233,16 +234,19 @@ + + + @@ -267,7 +271,6 @@ -