refactored icalls using icall helper

This commit is contained in:
sinaioutlander 2020-10-16 19:40:01 +11:00
parent bdf86a7448
commit bc0ad5eab6
27 changed files with 115 additions and 61 deletions

View File

@ -2,6 +2,7 @@
using System.Reflection; using System.Reflection;
using Explorer.CacheObject; using Explorer.CacheObject;
using UnityEngine; using UnityEngine;
using Explorer.Helpers;
namespace Explorer namespace Explorer
{ {

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Reflection; using System.Reflection;
using Explorer.UI; using Explorer.UI;
using Explorer.Helpers;
namespace Explorer.CacheObject namespace Explorer.CacheObject
{ {

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using UnityEngine; using UnityEngine;
using Explorer.UI.Shared; using Explorer.UI.Shared;
using Explorer.Helpers;
namespace Explorer.CacheObject namespace Explorer.CacheObject
{ {

View File

@ -5,6 +5,7 @@ using System.Reflection;
using UnityEngine; using UnityEngine;
using Explorer.UI; using Explorer.UI;
using Explorer.UI.Shared; using Explorer.UI.Shared;
using Explorer.Helpers;
namespace Explorer.CacheObject namespace Explorer.CacheObject
{ {

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Reflection; using System.Reflection;
using Explorer.UI; using Explorer.UI;
using Explorer.Helpers;
namespace Explorer.CacheObject namespace Explorer.CacheObject
{ {

View File

@ -264,6 +264,7 @@
<Compile Include="Unstrip\ImageConversion\ImageConversionUnstrip.cs" /> <Compile Include="Unstrip\ImageConversion\ImageConversionUnstrip.cs" />
<Compile Include="Unstrip\IMGUI\Internal_GUIUtility.cs" /> <Compile Include="Unstrip\IMGUI\Internal_GUIUtility.cs" />
<Compile Include="Unstrip\IMGUI\Internal_TextEditor.cs" /> <Compile Include="Unstrip\IMGUI\Internal_TextEditor.cs" />
<Compile Include="Helpers\ICallHelper.cs" />
<Compile Include="Unstrip\LayerMask\LayerMaskUnstrip.cs" /> <Compile Include="Unstrip\LayerMask\LayerMaskUnstrip.cs" />
<Compile Include="Unstrip\Scene\SceneUnstrip.cs" /> <Compile Include="Unstrip\Scene\SceneUnstrip.cs" />
<Compile Include="Unstrip\IMGUI\GUIUnstrip.cs" /> <Compile Include="Unstrip\IMGUI\GUIUnstrip.cs" />

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using Explorer.Helpers;
namespace Explorer namespace Explorer
{ {

View File

@ -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<string, Delegate> iCallCache = new Dictionary<string, Delegate>();
public static T GetICall<T>(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

View File

@ -12,7 +12,7 @@ using UnhollowerBaseLib;
using UnhollowerRuntimeLib; using UnhollowerRuntimeLib;
#endif #endif
namespace Explorer namespace Explorer.Helpers
{ {
public class ReflectionHelpers public class ReflectionHelpers
{ {

View File

@ -1,6 +1,6 @@
using UnityEngine; using UnityEngine;
namespace Explorer namespace Explorer.Helpers
{ {
public class UnityHelpers public class UnityHelpers
{ {

View File

@ -2,6 +2,7 @@
using System.Reflection; using System.Reflection;
using UnityEngine; using UnityEngine;
using Explorer.Input; using Explorer.Input;
using Explorer.Helpers;
#if CPP #if CPP
using UnhollowerBaseLib; using UnhollowerBaseLib;
#endif #endif
@ -42,10 +43,7 @@ namespace Explorer
#if CPP #if CPP
internal delegate void d_ResetInputAxes(); internal delegate void d_ResetInputAxes();
internal static d_ResetInputAxes ResetInputAxes_iCall = public static void ResetInputAxes() => ICallHelper.GetICall<d_ResetInputAxes>("UnityEngine.Input::ResetInputAxes").Invoke();
IL2CPP.ResolveICall<d_ResetInputAxes>("UnityEngine.Input::ResetInputAxes");
public static void ResetInputAxes() => ResetInputAxes_iCall();
#else #else
public static void ResetInputAxes() => UnityEngine.Input.ResetInputAxes(); public static void ResetInputAxes() => UnityEngine.Input.ResetInputAxes();
#endif #endif
@ -55,29 +53,34 @@ namespace Explorer
// public extern static string compositionString { get; } // public extern static string compositionString { get; }
internal delegate IntPtr d_get_compositionString(); internal delegate IntPtr d_get_compositionString();
internal static d_get_compositionString get_compositionString_iCall =
IL2CPP.ResolveICall<d_get_compositionString>("UnityEngine.Input::get_compositionString");
public static string compositionString => IL2CPP.Il2CppStringToManaged(get_compositionString_iCall()); public static string compositionString
{
get
{
var iCall = ICallHelper.GetICall<d_get_compositionString>("UnityEngine.Input::get_compositionString");
return IL2CPP.Il2CppStringToManaged(iCall.Invoke());
}
}
// public extern static Vector2 compositionCursorPos { get; set; } // public extern static Vector2 compositionCursorPos { get; set; }
internal delegate void d_get_compositionCursorPos(out Vector2 ret); internal delegate void d_get_compositionCursorPos(out Vector2 ret);
internal static d_get_compositionCursorPos get_compositionCursorPos_iCall = internal delegate void d_set_compositionCursorPos(ref Vector2 value);
IL2CPP.ResolveICall<d_get_compositionCursorPos>("UnityEngine.Input::get_compositionCursorPos_Injected");
internal delegate void set_compositionCursorPos_delegate(ref Vector2 value);
internal static set_compositionCursorPos_delegate set_compositionCursorPos_iCall =
IL2CPP.ResolveICall<set_compositionCursorPos_delegate>("UnityEngine.Input::set_compositionCursorPos_Injected");
public static Vector2 compositionCursorPos public static Vector2 compositionCursorPos
{ {
get get
{ {
get_compositionCursorPos_iCall(out Vector2 ret); var iCall = ICallHelper.GetICall<d_get_compositionCursorPos>("UnityEngine.Input::get_compositionCursorPos_Injected");
iCall.Invoke(out Vector2 ret);
return ret; return ret;
} }
set => set_compositionCursorPos_iCall(ref value); set
{
var iCall = ICallHelper.GetICall<d_set_compositionCursorPos>("UnityEngine.Input::set_compositionCursorPos_Injected");
iCall.Invoke(ref value);
}
} }
#pragma warning restore IDE1006 #pragma warning restore IDE1006

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using UnityEngine; using UnityEngine;
using Explorer.Helpers;
namespace Explorer.Input namespace Explorer.Input
{ {

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using UnityEngine; using UnityEngine;
using Explorer.Helpers;
namespace Explorer.Input namespace Explorer.Input
{ {

View File

@ -46,14 +46,6 @@ namespace Explorer.Tests
public static int StaticField = 5; public static int StaticField = 5;
public int NonStaticField; 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 #if CPP
public static Il2CppSystem.Collections.Generic.HashSet<string> ILHashSetTest; public static Il2CppSystem.Collections.Generic.HashSet<string> ILHashSetTest;
#endif #endif

View File

@ -1,5 +1,6 @@
using System; using System;
using UnityEngine; using UnityEngine;
using Explorer.Helpers;
#if ML #if ML
using Harmony; using Harmony;
#else #else

View File

@ -4,6 +4,7 @@ using UnityEngine;
using Explorer.UI.Shared; using Explorer.UI.Shared;
using Explorer.UI.Main; using Explorer.UI.Main;
using Explorer.Unstrip.LayerMasks; using Explorer.Unstrip.LayerMasks;
using Explorer.Helpers;
#if CPP #if CPP
using UnhollowerRuntimeLib; using UnhollowerRuntimeLib;
#endif #endif

View File

@ -1,4 +1,5 @@
using UnityEngine; using UnityEngine;
using Explorer.Helpers;
namespace Explorer.UI.Inspectors namespace Explorer.UI.Inspectors
{ {

View File

@ -7,6 +7,7 @@ using Explorer.UI;
using Explorer.UI.Shared; using Explorer.UI.Shared;
using Explorer.CacheObject; using Explorer.CacheObject;
using Explorer.UI.Inspectors; using Explorer.UI.Inspectors;
using Explorer.Helpers;
#if CPP #if CPP
using UnhollowerBaseLib; using UnhollowerBaseLib;
#endif #endif

View File

@ -5,6 +5,7 @@ using System.Reflection;
using UnityEngine; using UnityEngine;
using Explorer.UI.Shared; using Explorer.UI.Shared;
using Explorer.CacheObject; using Explorer.CacheObject;
using Explorer.Helpers;
namespace Explorer.UI namespace Explorer.UI
{ {

View File

@ -2,11 +2,12 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using Explorer.UI.Shared;
using Explorer.CacheObject;
using Explorer.Helpers;
#if CPP #if CPP
using UnhollowerBaseLib; using UnhollowerBaseLib;
#endif #endif
using Explorer.UI.Shared;
using Explorer.CacheObject;
namespace Explorer.UI namespace Explorer.UI
{ {

View File

@ -7,6 +7,7 @@ using UnityEngine;
using Explorer.UI.Shared; using Explorer.UI.Shared;
using Explorer.CacheObject; using Explorer.CacheObject;
using System.Linq; using System.Linq;
using Explorer.Helpers;
#if CPP #if CPP
using UnhollowerBaseLib; using UnhollowerBaseLib;
#endif #endif

View File

@ -5,6 +5,7 @@ using UnityEngine;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
using Explorer.UI.Shared; using Explorer.UI.Shared;
using Explorer.CacheObject; using Explorer.CacheObject;
using Explorer.Helpers;
namespace Explorer.UI.Main namespace Explorer.UI.Main
{ {

View File

@ -6,7 +6,7 @@ using System.Reflection;
using UnityEngine; using UnityEngine;
using Explorer.UI.Shared; using Explorer.UI.Shared;
using Explorer.CacheObject; using Explorer.CacheObject;
using System.Threading; using Explorer.Helpers;
namespace Explorer.UI.Main namespace Explorer.UI.Main
{ {

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using UnityEngine; using UnityEngine;
using Explorer.Helpers;
namespace Explorer.Unstrip.IMGUI namespace Explorer.Unstrip.IMGUI
{ {

View File

@ -6,6 +6,7 @@ using System.Text;
using UnhollowerBaseLib; using UnhollowerBaseLib;
using UnityEngine; using UnityEngine;
using System.IO; using System.IO;
using Explorer.Helpers;
namespace Explorer.Unstrip.ImageConversion namespace Explorer.Unstrip.ImageConversion
{ {
@ -13,29 +14,27 @@ namespace Explorer.Unstrip.ImageConversion
{ {
// byte[] ImageConversion.EncodeToPNG(this Texture2D image); // byte[] ImageConversion.EncodeToPNG(this Texture2D image);
internal delegate byte[] d_EncodeToPNG(IntPtr tex);
public static byte[] EncodeToPNG(this Texture2D tex) public static byte[] EncodeToPNG(this Texture2D tex)
{ {
return EncodeToPNG_iCall(tex.Pointer); return ICallHelper.GetICall<d_EncodeToPNG>("UnityEngine.ImageConversion::EncodeToPNG")
.Invoke(tex.Pointer);
} }
internal delegate byte[] EncodeToPNG_delegate(IntPtr tex);
internal static EncodeToPNG_delegate EncodeToPNG_iCall =
IL2CPP.ResolveICall<EncodeToPNG_delegate>("UnityEngine.ImageConversion::EncodeToPNG");
// bool ImageConversion.LoadImage(this Texture2D tex, byte[] data, bool markNonReadable); // 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) public static bool LoadImage(this Texture2D tex, byte[] data, bool markNonReadable)
{ {
return LoadImage_iCall(tex.Pointer, data, markNonReadable); return ICallHelper.GetICall<d_LoadImage>("UnityEngine.ImageConversion::LoadImage")
.Invoke(tex.Pointer, data, markNonReadable);
} }
internal delegate bool LoadImage_delegate(IntPtr tex, byte[] data, bool markNonReadable); // Helper for LoadImage from filepath
internal static LoadImage_delegate LoadImage_iCall =
IL2CPP.ResolveICall<LoadImage_delegate>("UnityEngine.ImageConversion::LoadImage");
// Helper for LoadImage public static bool LoadImage(Texture2D tex, string filePath, bool markNonReadable)
public static bool LoadImage(this Texture2D tex, string filePath, bool markNonReadable)
{ {
if (!File.Exists(filePath)) if (!File.Exists(filePath))
{ {

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using UnityEngine; using UnityEngine;
using Explorer.Helpers;
#if CPP #if CPP
using UnhollowerBaseLib; using UnhollowerBaseLib;
#endif #endif
@ -13,20 +14,14 @@ namespace Explorer.Unstrip.LayerMasks
{ {
#if CPP #if CPP
internal delegate IntPtr d_LayerToName(int layer); internal delegate IntPtr d_LayerToName(int layer);
internal static d_LayerToName LayerToName_iCall =
IL2CPP.ResolveICall<d_LayerToName>("UnityEngine.LayerMask::LayerToName");
public static string LayerToName(int layer) public static string LayerToName(int layer)
{ {
var ptr = LayerToName_iCall(layer); var iCall = ICallHelper.GetICall<d_LayerToName>("UnityEngine.LayerMask::LayerToName");
return IL2CPP.Il2CppStringToManaged(iCall.Invoke(layer));
return IL2CPP.Il2CppStringToManaged(ptr);
} }
#else #else
public static string LayerToName(int layer) public static string LayerToName(int layer) => LayerMask.LayerToName(layer);
{
return LayerMask.LayerToName(layer);
}
#endif #endif
} }
} }

View File

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Explorer.Helpers;
using UnhollowerBaseLib; using UnhollowerBaseLib;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
@ -12,28 +13,29 @@ namespace Explorer.Unstrip.Scenes
public class SceneUnstrip public class SceneUnstrip
{ {
//Scene.GetRootGameObjects(); //Scene.GetRootGameObjects();
internal delegate void d_GetRootGameObjects(int handle, IntPtr list);
public static GameObject[] GetRootGameObjects(Scene scene) public static GameObject[] GetRootGameObjects(Scene scene)
{ {
var list = new Il2CppSystem.Collections.Generic.List<GameObject>(GetRootCount_Internal(scene)); var list = new Il2CppSystem.Collections.Generic.List<GameObject>(GetRootCount(scene));
GetRootGameObjectsInternal_iCall(scene.handle, list.Pointer); var iCall = ICallHelper.GetICall<d_GetRootGameObjects>("UnityEngine.SceneManagement.Scene::GetRootGameObjectsInternal");
iCall.Invoke(scene.handle, list.Pointer);
return list.ToArray(); return list.ToArray();
} }
internal delegate void GetRootGameObjectsInternal_delegate(int handle, IntPtr list);
internal static GetRootGameObjectsInternal_delegate GetRootGameObjectsInternal_iCall =
IL2CPP.ResolveICall<GetRootGameObjectsInternal_delegate>("UnityEngine.SceneManagement.Scene::GetRootGameObjectsInternal");
//Scene.rootCount; //Scene.rootCount;
public static int GetRootCount_Internal(Scene scene)
{
return GetRootCountInternal_iCall(scene.handle);
}
internal delegate int GetRootCountInternal_delegate(int handle); internal delegate int GetRootCountInternal_delegate(int handle);
internal static GetRootCountInternal_delegate GetRootCountInternal_iCall =
IL2CPP.ResolveICall<GetRootCountInternal_delegate>("UnityEngine.SceneManagement.Scene::GetRootCountInternal"); public static int GetRootCount(Scene scene)
{
var iCall = ICallHelper.GetICall<GetRootCountInternal_delegate>("UnityEngine.SceneManagement.Scene::GetRootCountInternal");
return iCall.Invoke(scene.handle);
}
} }
} }
#endif #endif