mirror of
https://github.com/sinai-dev/UnityExplorer.git
synced 2025-09-20 21:46:02 +08:00
refactored icalls using icall helper
This commit is contained in:
45
src/Helpers/ICallHelper.cs
Normal file
45
src/Helpers/ICallHelper.cs
Normal 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
|
@ -12,7 +12,7 @@ using UnhollowerBaseLib;
|
||||
using UnhollowerRuntimeLib;
|
||||
#endif
|
||||
|
||||
namespace Explorer
|
||||
namespace Explorer.Helpers
|
||||
{
|
||||
public class ReflectionHelpers
|
||||
{
|
||||
|
@ -1,6 +1,6 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Explorer
|
||||
namespace Explorer.Helpers
|
||||
{
|
||||
public class UnityHelpers
|
||||
{
|
||||
|
Reference in New Issue
Block a user