Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit bc0ad5e

Browse files
committed
refactored icalls using icall helper
1 parent bdf86a7 commit bc0ad5e

27 files changed

+115
-61
lines changed

src/CacheObject/CacheFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Reflection;
33
using Explorer.CacheObject;
44
using UnityEngine;
5+
using Explorer.Helpers;
56

67
namespace Explorer
78
{

src/CacheObject/CacheField.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text;
55
using System.Reflection;
66
using Explorer.UI;
7+
using Explorer.Helpers;
78

89
namespace Explorer.CacheObject
910
{

src/CacheObject/CacheMethod.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reflection;
55
using UnityEngine;
66
using Explorer.UI.Shared;
7+
using Explorer.Helpers;
78

89
namespace Explorer.CacheObject
910
{

src/CacheObject/CacheObjectBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using UnityEngine;
66
using Explorer.UI;
77
using Explorer.UI.Shared;
8+
using Explorer.Helpers;
89

910
namespace Explorer.CacheObject
1011
{

src/CacheObject/CacheProperty.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text;
55
using System.Reflection;
66
using Explorer.UI;
7+
using Explorer.Helpers;
78

89
namespace Explorer.CacheObject
910
{

src/Explorer.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@
264264
<Compile Include="Unstrip\ImageConversion\ImageConversionUnstrip.cs" />
265265
<Compile Include="Unstrip\IMGUI\Internal_GUIUtility.cs" />
266266
<Compile Include="Unstrip\IMGUI\Internal_TextEditor.cs" />
267+
<Compile Include="Helpers\ICallHelper.cs" />
267268
<Compile Include="Unstrip\LayerMask\LayerMaskUnstrip.cs" />
268269
<Compile Include="Unstrip\Scene\SceneUnstrip.cs" />
269270
<Compile Include="Unstrip\IMGUI\GUIUnstrip.cs" />

src/Extensions/ReflectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
5+
using Explorer.Helpers;
56

67
namespace Explorer
78
{

src/Helpers/ICallHelper.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#if CPP
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Runtime.InteropServices;
6+
using System.Text;
7+
using System.Reflection;
8+
9+
namespace Explorer.Helpers
10+
{
11+
public static class ICallHelper
12+
{
13+
private static readonly Dictionary<string, Delegate> iCallCache = new Dictionary<string, Delegate>();
14+
15+
public static T GetICall<T>(string iCallName) where T : Delegate
16+
{
17+
if (iCallCache.ContainsKey(iCallName))
18+
{
19+
return (T)iCallCache[iCallName];
20+
}
21+
22+
var ptr = il2cpp_resolve_icall(iCallName);
23+
24+
if (ptr == IntPtr.Zero)
25+
{
26+
throw new MissingMethodException($"Could not resolve internal call by name '{iCallName}'!");
27+
}
28+
29+
var iCall = Marshal.GetDelegateForFunctionPointer(ptr, typeof(T));
30+
iCallCache.Add(iCallName, iCall);
31+
32+
return (T)iCall;
33+
}
34+
35+
#region External
36+
#pragma warning disable IDE1006 // Naming Styles
37+
38+
[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
39+
public static extern IntPtr il2cpp_resolve_icall([MarshalAs(UnmanagedType.LPStr)] string name);
40+
41+
#pragma warning restore IDE1006
42+
#endregion
43+
}
44+
}
45+
#endif

src/Helpers/ReflectionHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
using UnhollowerRuntimeLib;
1313
#endif
1414

15-
namespace Explorer
15+
namespace Explorer.Helpers
1616
{
1717
public class ReflectionHelpers
1818
{

src/Helpers/UnityHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using UnityEngine;
22

3-
namespace Explorer
3+
namespace Explorer.Helpers
44
{
55
public class UnityHelpers
66
{

0 commit comments

Comments
 (0)