From bc0ad5eab66a0c4f12db940ee7a58dd77a28d0c5 Mon Sep 17 00:00:00 2001 From: sinaioutlander <49360850+sinaioutlander@users.noreply.github.com> Date: Fri, 16 Oct 2020 19:40:01 +1100 Subject: [PATCH] refactored icalls using icall helper --- src/CacheObject/CacheFactory.cs | 1 + src/CacheObject/CacheField.cs | 1 + src/CacheObject/CacheMethod.cs | 1 + src/CacheObject/CacheObjectBase.cs | 1 + src/CacheObject/CacheProperty.cs | 1 + src/Explorer.csproj | 1 + src/Extensions/ReflectionExtensions.cs | 1 + src/Helpers/ICallHelper.cs | 45 +++++++++++++++++++ src/Helpers/ReflectionHelpers.cs | 2 +- src/Helpers/UnityHelpers.cs | 2 +- src/Input/InputManager.cs | 33 +++++++------- src/Input/InputSystem.cs | 1 + src/Input/LegacyInput.cs | 1 + src/Tests/TestClass.cs | 8 ---- src/UI/ForceUnlockCursor.cs | 1 + src/UI/Inspectors/GameObjectInspector.cs | 1 + src/UI/Inspectors/InspectUnderMouse.cs | 1 + src/UI/Inspectors/ReflectionInspector.cs | 1 + src/UI/InteractiveValue/InteractiveValue.cs | 1 + .../Object/InteractiveDictionary.cs | 5 ++- .../Object/InteractiveEnumerable.cs | 1 + src/UI/Main/ScenePage.cs | 1 + src/UI/Main/SearchPage.cs | 2 +- src/Unstrip/IMGUI/Internal_GUIUtility.cs | 1 + .../ImageConversion/ImageConversionUnstrip.cs | 23 +++++----- src/Unstrip/LayerMask/LayerMaskUnstrip.cs | 13 ++---- src/Unstrip/Scene/SceneUnstrip.cs | 26 ++++++----- 27 files changed, 115 insertions(+), 61 deletions(-) create mode 100644 src/Helpers/ICallHelper.cs diff --git a/src/CacheObject/CacheFactory.cs b/src/CacheObject/CacheFactory.cs index 4c1bfaa..3a881b5 100644 --- a/src/CacheObject/CacheFactory.cs +++ b/src/CacheObject/CacheFactory.cs @@ -2,6 +2,7 @@ using System.Reflection; using Explorer.CacheObject; using UnityEngine; +using Explorer.Helpers; namespace Explorer { diff --git a/src/CacheObject/CacheField.cs b/src/CacheObject/CacheField.cs index ce003d0..2aaa748 100644 --- a/src/CacheObject/CacheField.cs +++ b/src/CacheObject/CacheField.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Reflection; using Explorer.UI; +using Explorer.Helpers; namespace Explorer.CacheObject { diff --git a/src/CacheObject/CacheMethod.cs b/src/CacheObject/CacheMethod.cs index b989321..f115c22 100644 --- a/src/CacheObject/CacheMethod.cs +++ b/src/CacheObject/CacheMethod.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Reflection; using UnityEngine; using Explorer.UI.Shared; +using Explorer.Helpers; namespace Explorer.CacheObject { diff --git a/src/CacheObject/CacheObjectBase.cs b/src/CacheObject/CacheObjectBase.cs index 82d84eb..efba856 100644 --- a/src/CacheObject/CacheObjectBase.cs +++ b/src/CacheObject/CacheObjectBase.cs @@ -5,6 +5,7 @@ using System.Reflection; using UnityEngine; using Explorer.UI; using Explorer.UI.Shared; +using Explorer.Helpers; namespace Explorer.CacheObject { diff --git a/src/CacheObject/CacheProperty.cs b/src/CacheObject/CacheProperty.cs index 1e2cea8..2380d59 100644 --- a/src/CacheObject/CacheProperty.cs +++ b/src/CacheObject/CacheProperty.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Reflection; using Explorer.UI; +using Explorer.Helpers; namespace Explorer.CacheObject { diff --git a/src/Explorer.csproj b/src/Explorer.csproj index 738e97f..b945f13 100644 --- a/src/Explorer.csproj +++ b/src/Explorer.csproj @@ -264,6 +264,7 @@ + diff --git a/src/Extensions/ReflectionExtensions.cs b/src/Extensions/ReflectionExtensions.cs index 14d9ffc..d8422f5 100644 --- a/src/Extensions/ReflectionExtensions.cs +++ b/src/Extensions/ReflectionExtensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using Explorer.Helpers; namespace Explorer { diff --git a/src/Helpers/ICallHelper.cs b/src/Helpers/ICallHelper.cs new file mode 100644 index 0000000..a2de5bd --- /dev/null +++ b/src/Helpers/ICallHelper.cs @@ -0,0 +1,45 @@ +#if CPP +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Reflection; + +namespace Explorer.Helpers +{ + public static class ICallHelper + { + private static readonly Dictionary iCallCache = new Dictionary(); + + public static T GetICall(string iCallName) where T : Delegate + { + if (iCallCache.ContainsKey(iCallName)) + { + return (T)iCallCache[iCallName]; + } + + var ptr = il2cpp_resolve_icall(iCallName); + + if (ptr == IntPtr.Zero) + { + throw new MissingMethodException($"Could not resolve internal call by name '{iCallName}'!"); + } + + var iCall = Marshal.GetDelegateForFunctionPointer(ptr, typeof(T)); + iCallCache.Add(iCallName, iCall); + + return (T)iCall; + } + + #region External + #pragma warning disable IDE1006 // Naming Styles + + [DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr il2cpp_resolve_icall([MarshalAs(UnmanagedType.LPStr)] string name); + + #pragma warning restore IDE1006 + #endregion + } +} +#endif \ No newline at end of file diff --git a/src/Helpers/ReflectionHelpers.cs b/src/Helpers/ReflectionHelpers.cs index 74f0c4d..8fa1c96 100644 --- a/src/Helpers/ReflectionHelpers.cs +++ b/src/Helpers/ReflectionHelpers.cs @@ -12,7 +12,7 @@ using UnhollowerBaseLib; using UnhollowerRuntimeLib; #endif -namespace Explorer +namespace Explorer.Helpers { public class ReflectionHelpers { diff --git a/src/Helpers/UnityHelpers.cs b/src/Helpers/UnityHelpers.cs index b9a42ab..5bad857 100644 --- a/src/Helpers/UnityHelpers.cs +++ b/src/Helpers/UnityHelpers.cs @@ -1,6 +1,6 @@ using UnityEngine; -namespace Explorer +namespace Explorer.Helpers { public class UnityHelpers { diff --git a/src/Input/InputManager.cs b/src/Input/InputManager.cs index 358b264..e2ed087 100644 --- a/src/Input/InputManager.cs +++ b/src/Input/InputManager.cs @@ -2,6 +2,7 @@ using System.Reflection; using UnityEngine; using Explorer.Input; +using Explorer.Helpers; #if CPP using UnhollowerBaseLib; #endif @@ -42,10 +43,7 @@ namespace Explorer #if CPP internal delegate void d_ResetInputAxes(); - internal static d_ResetInputAxes ResetInputAxes_iCall = - IL2CPP.ResolveICall("UnityEngine.Input::ResetInputAxes"); - - public static void ResetInputAxes() => ResetInputAxes_iCall(); + public static void ResetInputAxes() => ICallHelper.GetICall("UnityEngine.Input::ResetInputAxes").Invoke(); #else public static void ResetInputAxes() => UnityEngine.Input.ResetInputAxes(); #endif @@ -55,29 +53,34 @@ namespace Explorer // public extern static string compositionString { get; } internal delegate IntPtr d_get_compositionString(); - internal static d_get_compositionString get_compositionString_iCall = - IL2CPP.ResolveICall("UnityEngine.Input::get_compositionString"); - public static string compositionString => IL2CPP.Il2CppStringToManaged(get_compositionString_iCall()); + public static string compositionString + { + get + { + var iCall = ICallHelper.GetICall("UnityEngine.Input::get_compositionString"); + return IL2CPP.Il2CppStringToManaged(iCall.Invoke()); + } + } // public extern static Vector2 compositionCursorPos { get; set; } internal delegate void d_get_compositionCursorPos(out Vector2 ret); - internal static d_get_compositionCursorPos get_compositionCursorPos_iCall = - IL2CPP.ResolveICall("UnityEngine.Input::get_compositionCursorPos_Injected"); - - internal delegate void set_compositionCursorPos_delegate(ref Vector2 value); - internal static set_compositionCursorPos_delegate set_compositionCursorPos_iCall = - IL2CPP.ResolveICall("UnityEngine.Input::set_compositionCursorPos_Injected"); + internal delegate void d_set_compositionCursorPos(ref Vector2 value); public static Vector2 compositionCursorPos { get { - get_compositionCursorPos_iCall(out Vector2 ret); + var iCall = ICallHelper.GetICall("UnityEngine.Input::get_compositionCursorPos_Injected"); + iCall.Invoke(out Vector2 ret); return ret; } - set => set_compositionCursorPos_iCall(ref value); + set + { + var iCall = ICallHelper.GetICall("UnityEngine.Input::set_compositionCursorPos_Injected"); + iCall.Invoke(ref value); + } } #pragma warning restore IDE1006 diff --git a/src/Input/InputSystem.cs b/src/Input/InputSystem.cs index 0f23629..c0004fc 100644 --- a/src/Input/InputSystem.cs +++ b/src/Input/InputSystem.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using UnityEngine; +using Explorer.Helpers; namespace Explorer.Input { diff --git a/src/Input/LegacyInput.cs b/src/Input/LegacyInput.cs index 183426f..8b3e996 100644 --- a/src/Input/LegacyInput.cs +++ b/src/Input/LegacyInput.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using UnityEngine; +using Explorer.Helpers; namespace Explorer.Input { diff --git a/src/Tests/TestClass.cs b/src/Tests/TestClass.cs index 237a4fe..e5644b4 100644 --- a/src/Tests/TestClass.cs +++ b/src/Tests/TestClass.cs @@ -46,14 +46,6 @@ namespace Explorer.Tests public static int StaticField = 5; public int NonStaticField; -#if CPP - public static IntPtr FindICall(string name) => il2cpp_resolve_icall(name); - - [DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - private static extern IntPtr il2cpp_resolve_icall([MarshalAs(UnmanagedType.LPStr)] string name); - -#endif - #if CPP public static Il2CppSystem.Collections.Generic.HashSet ILHashSetTest; #endif diff --git a/src/UI/ForceUnlockCursor.cs b/src/UI/ForceUnlockCursor.cs index 239dad7..ff06f02 100644 --- a/src/UI/ForceUnlockCursor.cs +++ b/src/UI/ForceUnlockCursor.cs @@ -1,5 +1,6 @@ using System; using UnityEngine; +using Explorer.Helpers; #if ML using Harmony; #else diff --git a/src/UI/Inspectors/GameObjectInspector.cs b/src/UI/Inspectors/GameObjectInspector.cs index 607b83b..35f766b 100644 --- a/src/UI/Inspectors/GameObjectInspector.cs +++ b/src/UI/Inspectors/GameObjectInspector.cs @@ -4,6 +4,7 @@ using UnityEngine; using Explorer.UI.Shared; using Explorer.UI.Main; using Explorer.Unstrip.LayerMasks; +using Explorer.Helpers; #if CPP using UnhollowerRuntimeLib; #endif diff --git a/src/UI/Inspectors/InspectUnderMouse.cs b/src/UI/Inspectors/InspectUnderMouse.cs index 96fc08d..6144c42 100644 --- a/src/UI/Inspectors/InspectUnderMouse.cs +++ b/src/UI/Inspectors/InspectUnderMouse.cs @@ -1,4 +1,5 @@ using UnityEngine; +using Explorer.Helpers; namespace Explorer.UI.Inspectors { diff --git a/src/UI/Inspectors/ReflectionInspector.cs b/src/UI/Inspectors/ReflectionInspector.cs index 189e22c..4f7a2a2 100644 --- a/src/UI/Inspectors/ReflectionInspector.cs +++ b/src/UI/Inspectors/ReflectionInspector.cs @@ -7,6 +7,7 @@ using Explorer.UI; using Explorer.UI.Shared; using Explorer.CacheObject; using Explorer.UI.Inspectors; +using Explorer.Helpers; #if CPP using UnhollowerBaseLib; #endif diff --git a/src/UI/InteractiveValue/InteractiveValue.cs b/src/UI/InteractiveValue/InteractiveValue.cs index c7d218f..8247150 100644 --- a/src/UI/InteractiveValue/InteractiveValue.cs +++ b/src/UI/InteractiveValue/InteractiveValue.cs @@ -5,6 +5,7 @@ using System.Reflection; using UnityEngine; using Explorer.UI.Shared; using Explorer.CacheObject; +using Explorer.Helpers; namespace Explorer.UI { diff --git a/src/UI/InteractiveValue/Object/InteractiveDictionary.cs b/src/UI/InteractiveValue/Object/InteractiveDictionary.cs index 2d56859..233df5d 100644 --- a/src/UI/InteractiveValue/Object/InteractiveDictionary.cs +++ b/src/UI/InteractiveValue/Object/InteractiveDictionary.cs @@ -2,11 +2,12 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +using Explorer.UI.Shared; +using Explorer.CacheObject; +using Explorer.Helpers; #if CPP using UnhollowerBaseLib; #endif -using Explorer.UI.Shared; -using Explorer.CacheObject; namespace Explorer.UI { diff --git a/src/UI/InteractiveValue/Object/InteractiveEnumerable.cs b/src/UI/InteractiveValue/Object/InteractiveEnumerable.cs index 32fc71d..a1d97c4 100644 --- a/src/UI/InteractiveValue/Object/InteractiveEnumerable.cs +++ b/src/UI/InteractiveValue/Object/InteractiveEnumerable.cs @@ -7,6 +7,7 @@ using UnityEngine; using Explorer.UI.Shared; using Explorer.CacheObject; using System.Linq; +using Explorer.Helpers; #if CPP using UnhollowerBaseLib; #endif diff --git a/src/UI/Main/ScenePage.cs b/src/UI/Main/ScenePage.cs index 2b2c706..52e0efc 100644 --- a/src/UI/Main/ScenePage.cs +++ b/src/UI/Main/ScenePage.cs @@ -5,6 +5,7 @@ using UnityEngine; using UnityEngine.SceneManagement; using Explorer.UI.Shared; using Explorer.CacheObject; +using Explorer.Helpers; namespace Explorer.UI.Main { diff --git a/src/UI/Main/SearchPage.cs b/src/UI/Main/SearchPage.cs index f4b50ad..bfa91bd 100644 --- a/src/UI/Main/SearchPage.cs +++ b/src/UI/Main/SearchPage.cs @@ -6,7 +6,7 @@ using System.Reflection; using UnityEngine; using Explorer.UI.Shared; using Explorer.CacheObject; -using System.Threading; +using Explorer.Helpers; namespace Explorer.UI.Main { diff --git a/src/Unstrip/IMGUI/Internal_GUIUtility.cs b/src/Unstrip/IMGUI/Internal_GUIUtility.cs index 5f6c36f..53ec2c3 100644 --- a/src/Unstrip/IMGUI/Internal_GUIUtility.cs +++ b/src/Unstrip/IMGUI/Internal_GUIUtility.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using UnityEngine; +using Explorer.Helpers; namespace Explorer.Unstrip.IMGUI { diff --git a/src/Unstrip/ImageConversion/ImageConversionUnstrip.cs b/src/Unstrip/ImageConversion/ImageConversionUnstrip.cs index 97d9986..0be4c7e 100644 --- a/src/Unstrip/ImageConversion/ImageConversionUnstrip.cs +++ b/src/Unstrip/ImageConversion/ImageConversionUnstrip.cs @@ -6,6 +6,7 @@ using System.Text; using UnhollowerBaseLib; using UnityEngine; using System.IO; +using Explorer.Helpers; namespace Explorer.Unstrip.ImageConversion { @@ -13,29 +14,27 @@ namespace Explorer.Unstrip.ImageConversion { // byte[] ImageConversion.EncodeToPNG(this Texture2D image); + internal delegate byte[] d_EncodeToPNG(IntPtr tex); + public static byte[] EncodeToPNG(this Texture2D tex) { - return EncodeToPNG_iCall(tex.Pointer); + return ICallHelper.GetICall("UnityEngine.ImageConversion::EncodeToPNG") + .Invoke(tex.Pointer); } - internal delegate byte[] EncodeToPNG_delegate(IntPtr tex); - internal static EncodeToPNG_delegate EncodeToPNG_iCall = - IL2CPP.ResolveICall("UnityEngine.ImageConversion::EncodeToPNG"); - // bool ImageConversion.LoadImage(this Texture2D tex, byte[] data, bool markNonReadable); + internal delegate bool d_LoadImage(IntPtr tex, byte[] data, bool markNonReadable); + public static bool LoadImage(this Texture2D tex, byte[] data, bool markNonReadable) { - return LoadImage_iCall(tex.Pointer, data, markNonReadable); + return ICallHelper.GetICall("UnityEngine.ImageConversion::LoadImage") + .Invoke(tex.Pointer, data, markNonReadable); } - internal delegate bool LoadImage_delegate(IntPtr tex, byte[] data, bool markNonReadable); - internal static LoadImage_delegate LoadImage_iCall = - IL2CPP.ResolveICall("UnityEngine.ImageConversion::LoadImage"); + // Helper for LoadImage from filepath - // Helper for LoadImage - - public static bool LoadImage(this Texture2D tex, string filePath, bool markNonReadable) + public static bool LoadImage(Texture2D tex, string filePath, bool markNonReadable) { if (!File.Exists(filePath)) { diff --git a/src/Unstrip/LayerMask/LayerMaskUnstrip.cs b/src/Unstrip/LayerMask/LayerMaskUnstrip.cs index 2695602..cb8b6fd 100644 --- a/src/Unstrip/LayerMask/LayerMaskUnstrip.cs +++ b/src/Unstrip/LayerMask/LayerMaskUnstrip.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using UnityEngine; +using Explorer.Helpers; #if CPP using UnhollowerBaseLib; #endif @@ -13,20 +14,14 @@ namespace Explorer.Unstrip.LayerMasks { #if CPP internal delegate IntPtr d_LayerToName(int layer); - internal static d_LayerToName LayerToName_iCall = - IL2CPP.ResolveICall("UnityEngine.LayerMask::LayerToName"); public static string LayerToName(int layer) { - var ptr = LayerToName_iCall(layer); - - return IL2CPP.Il2CppStringToManaged(ptr); + var iCall = ICallHelper.GetICall("UnityEngine.LayerMask::LayerToName"); + return IL2CPP.Il2CppStringToManaged(iCall.Invoke(layer)); } #else - public static string LayerToName(int layer) - { - return LayerMask.LayerToName(layer); - } + public static string LayerToName(int layer) => LayerMask.LayerToName(layer); #endif } } diff --git a/src/Unstrip/Scene/SceneUnstrip.cs b/src/Unstrip/Scene/SceneUnstrip.cs index 6c3ee3e..83187a3 100644 --- a/src/Unstrip/Scene/SceneUnstrip.cs +++ b/src/Unstrip/Scene/SceneUnstrip.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using Explorer.Helpers; using UnhollowerBaseLib; using UnityEngine; using UnityEngine.SceneManagement; @@ -12,28 +13,29 @@ namespace Explorer.Unstrip.Scenes public class SceneUnstrip { //Scene.GetRootGameObjects(); + + internal delegate void d_GetRootGameObjects(int handle, IntPtr list); + public static GameObject[] GetRootGameObjects(Scene scene) { - var list = new Il2CppSystem.Collections.Generic.List(GetRootCount_Internal(scene)); + var list = new Il2CppSystem.Collections.Generic.List(GetRootCount(scene)); - GetRootGameObjectsInternal_iCall(scene.handle, list.Pointer); + var iCall = ICallHelper.GetICall("UnityEngine.SceneManagement.Scene::GetRootGameObjectsInternal"); + + iCall.Invoke(scene.handle, list.Pointer); return list.ToArray(); } - internal delegate void GetRootGameObjectsInternal_delegate(int handle, IntPtr list); - internal static GetRootGameObjectsInternal_delegate GetRootGameObjectsInternal_iCall = - IL2CPP.ResolveICall("UnityEngine.SceneManagement.Scene::GetRootGameObjectsInternal"); - //Scene.rootCount; - public static int GetRootCount_Internal(Scene scene) - { - return GetRootCountInternal_iCall(scene.handle); - } internal delegate int GetRootCountInternal_delegate(int handle); - internal static GetRootCountInternal_delegate GetRootCountInternal_iCall = - IL2CPP.ResolveICall("UnityEngine.SceneManagement.Scene::GetRootCountInternal"); + + public static int GetRootCount(Scene scene) + { + var iCall = ICallHelper.GetICall("UnityEngine.SceneManagement.Scene::GetRootCountInternal"); + return iCall.Invoke(scene.handle); + } } } #endif \ No newline at end of file