UnityExplorer/src/Helpers/ICallHelper.cs

36 lines
1.2 KiB
C#
Raw Normal View History

2020-10-16 19:40:01 +11:00
#if CPP
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
2020-10-16 19:40:01 +11:00
namespace ExplorerBeta.Helpers
2020-10-16 19:40:01 +11:00
{
[SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "External methods")]
2020-10-16 19:40:01 +11:00
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];
IntPtr ptr = il2cpp_resolve_icall(iCallName);
2020-10-16 19:40:01 +11:00
if (ptr == IntPtr.Zero)
{
throw new MissingMethodException($"Could not resolve internal call by name '{iCallName}'!");
}
Delegate iCall = Marshal.GetDelegateForFunctionPointer(ptr, typeof(T));
2020-10-16 19:40:01 +11:00
iCallCache.Add(iCallName, iCall);
return (T)iCall;
}
[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr il2cpp_resolve_icall([MarshalAs(UnmanagedType.LPStr)] string name);
}
}
#endif