[refactor] RuntimeApi相关函数由PInvoke改为InternalCall

main
walon 2023-10-09 22:24:35 +08:00
parent 69de3d733d
commit b573e61504
1 changed files with 16 additions and 37 deletions

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -10,12 +11,9 @@ namespace HybridCLR
{ {
public static class RuntimeApi public static class RuntimeApi
{ {
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX #if UNITY_EDITOR
private const string dllName = "GameAssembly"; private static int s_interpreterThreadObjectStackSize = 128 * 1024;
#elif UNITY_ANDROID private static int s_interpreterThreadFrameStackSize = 2 * 1024;
private const string dllName = "il2cpp";
#else
private const string dllName = "__Internal";
#endif #endif
/// <summary> /// <summary>
@ -24,35 +22,16 @@ namespace HybridCLR
/// <param name="dllBytes"></param> /// <param name="dllBytes"></param>
/// <returns></returns> /// <returns></returns>
/// <exception cref="NotSupportedException"></exception> /// <exception cref="NotSupportedException"></exception>
#if UNITY_EDITOR
public static unsafe LoadImageErrorCode LoadMetadataForAOTAssembly(byte[] dllBytes, HomologousImageMode mode) public static unsafe LoadImageErrorCode LoadMetadataForAOTAssembly(byte[] dllBytes, HomologousImageMode mode)
{ {
#if UNITY_EDITOR
return LoadImageErrorCode.OK; return LoadImageErrorCode.OK;
#else
fixed(byte* data = dllBytes)
{
return (LoadImageErrorCode)LoadMetadataForAOTAssembly(data, dllBytes.Length, (int)mode);
}
#endif
}
/// <summary>
/// 加载补充元数据assembly
/// </summary>
/// <param name="dllBytes"></param>
/// <param name="dllSize"></param>
/// <returns></returns>
#if UNITY_EDITOR
public static unsafe int LoadMetadataForAOTAssembly(byte* dllBytes, int dllSize, int mode)
{
return 0;
} }
#else #else
[DllImport(dllName, EntryPoint = "RuntimeApi_LoadMetadataForAOTAssembly")] [MethodImpl(MethodImplOptions.InternalCall)]
public static extern unsafe int LoadMetadataForAOTAssembly(byte* dllBytes, int dllSize, int mode); public static extern LoadImageErrorCode LoadMetadataForAOTAssembly(byte[] dllBytes, HomologousImageMode mode);
#endif #endif
/// <summary> /// <summary>
/// 获取解释器线程栈的最大StackObject个数(size*8 为最终占用的内存大小) /// 获取解释器线程栈的最大StackObject个数(size*8 为最终占用的内存大小)
/// </summary> /// </summary>
@ -60,10 +39,10 @@ namespace HybridCLR
#if UNITY_EDITOR #if UNITY_EDITOR
public static int GetInterpreterThreadObjectStackSize() public static int GetInterpreterThreadObjectStackSize()
{ {
return 0; return s_interpreterThreadObjectStackSize;
} }
#else #else
[DllImport(dllName, EntryPoint = "RuntimeApi_GetInterpreterThreadObjectStackSize")] [MethodImpl(MethodImplOptions.InternalCall)]
public static extern int GetInterpreterThreadObjectStackSize(); public static extern int GetInterpreterThreadObjectStackSize();
#endif #endif
@ -74,10 +53,10 @@ namespace HybridCLR
#if UNITY_EDITOR #if UNITY_EDITOR
public static void SetInterpreterThreadObjectStackSize(int size) public static void SetInterpreterThreadObjectStackSize(int size)
{ {
s_interpreterThreadObjectStackSize = size;
} }
#else #else
[DllImport(dllName, EntryPoint = "RuntimeApi_SetInterpreterThreadObjectStackSize")] [MethodImpl(MethodImplOptions.InternalCall)]
public static extern void SetInterpreterThreadObjectStackSize(int size); public static extern void SetInterpreterThreadObjectStackSize(int size);
#endif #endif
/// <summary> /// <summary>
@ -87,10 +66,10 @@ namespace HybridCLR
#if UNITY_EDITOR #if UNITY_EDITOR
public static int GetInterpreterThreadFrameStackSize() public static int GetInterpreterThreadFrameStackSize()
{ {
return 0; return s_interpreterThreadFrameStackSize;
} }
#else #else
[DllImport(dllName, EntryPoint = "RuntimeApi_GetInterpreterThreadFrameStackSize")] [MethodImpl(MethodImplOptions.InternalCall)]
public static extern int GetInterpreterThreadFrameStackSize(); public static extern int GetInterpreterThreadFrameStackSize();
#endif #endif
@ -101,10 +80,10 @@ namespace HybridCLR
#if UNITY_EDITOR #if UNITY_EDITOR
public static void SetInterpreterThreadFrameStackSize(int size) public static void SetInterpreterThreadFrameStackSize(int size)
{ {
s_interpreterThreadFrameStackSize = size;
} }
#else #else
[DllImport(dllName, EntryPoint = "RuntimeApi_SetInterpreterThreadFrameStackSize")] [MethodImpl(MethodImplOptions.InternalCall)]
public static extern void SetInterpreterThreadFrameStackSize(int size); public static extern void SetInterpreterThreadFrameStackSize(int size);
#endif #endif
} }