2022-09-22 08:56:07 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace HybridCLR
|
|
|
|
|
{
|
|
|
|
|
public static class RuntimeApi
|
|
|
|
|
{
|
|
|
|
|
#if UNITY_STANDALONE_WIN
|
|
|
|
|
private const string dllName = "GameAssembly";
|
|
|
|
|
#elif UNITY_IOS || UNITY_STANDALONE_OSX || UNITY_WEBGL
|
|
|
|
|
private const string dllName = "__Internal";
|
|
|
|
|
#else
|
|
|
|
|
private const string dllName = "il2cpp";
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-10-08 12:35:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 加载补充元数据assembly
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dllBytes"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="NotSupportedException"></exception>
|
2022-10-26 11:28:49 +08:00
|
|
|
|
public static unsafe LoadImageErrorCode LoadMetadataForAOTAssembly(byte[] dllBytes, HomologousImageMode mode)
|
2022-09-28 21:48:09 +08:00
|
|
|
|
{
|
|
|
|
|
#if UNITY_EDITOR
|
2022-10-08 12:35:34 +08:00
|
|
|
|
return LoadImageErrorCode.OK;
|
2022-09-28 21:48:09 +08:00
|
|
|
|
#else
|
|
|
|
|
fixed(byte* data = dllBytes)
|
|
|
|
|
{
|
2022-11-05 22:22:39 +08:00
|
|
|
|
return (LoadImageErrorCode)LoadMetadataForAOTAssembly(data, dllBytes.Length, (int)mode);
|
2022-09-28 21:48:09 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-08 12:35:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 加载补充元数据assembly
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dllBytes"></param>
|
|
|
|
|
/// <param name="dllSize"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-09-22 08:56:07 +08:00
|
|
|
|
[DllImport(dllName, EntryPoint = "RuntimeApi_LoadMetadataForAOTAssembly")]
|
2022-11-05 22:22:39 +08:00
|
|
|
|
public static extern unsafe int LoadMetadataForAOTAssembly(byte* dllBytes, int dllSize, int mode);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//[DllImport(dllName, EntryPoint = "RuntimeApi_UseDifferentialHybridAOTAssembly")]
|
|
|
|
|
//public static unsafe LoadImageErrorCode UseDifferentialHybridAOTAssembly(string assemblyName)
|
|
|
|
|
//{
|
|
|
|
|
// byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(assemblyName);
|
|
|
|
|
// fixed(byte* namePtr = nameBytes)
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
2022-11-06 20:16:38 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 指示混合执行assembly使用原始的AOT代码。当assembly没有发生变化时必须调用此接口。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="assemblyName"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
public static LoadImageErrorCode UseDifferentialHybridAOTAssembly(string assemblyName)
|
|
|
|
|
{
|
|
|
|
|
return LoadImageErrorCode.OK;
|
|
|
|
|
}
|
|
|
|
|
#else
|
2022-11-05 22:22:39 +08:00
|
|
|
|
[DllImport(dllName, EntryPoint = "RuntimeApi_UseDifferentialHybridAOTAssembly")]
|
|
|
|
|
public static extern LoadImageErrorCode UseDifferentialHybridAOTAssembly(string assemblyName);
|
2022-11-06 20:16:38 +08:00
|
|
|
|
#endif
|
2022-11-05 22:22:39 +08:00
|
|
|
|
|
|
|
|
|
|
2022-11-06 20:16:38 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 加载差分混合执行 assembly
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dllBytes"></param>
|
|
|
|
|
/// <param name="optionBytes"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-11-05 22:22:39 +08:00
|
|
|
|
public static unsafe LoadImageErrorCode LoadDifferentialHybridAssembly(byte[] dllBytes, byte[] optionBytes)
|
|
|
|
|
{
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
return LoadImageErrorCode.OK;
|
|
|
|
|
#else
|
|
|
|
|
fixed(byte* dllBytesPtr = dllBytes)
|
|
|
|
|
{
|
2022-11-06 20:16:38 +08:00
|
|
|
|
fixed(byte* optionBytesPtr = optionBytes)
|
2022-11-05 22:22:39 +08:00
|
|
|
|
{
|
2022-11-06 20:16:38 +08:00
|
|
|
|
return (LoadImageErrorCode)LoadDifferentialHybridAssembly(dllBytesPtr, dllBytes.Length, optionBytesPtr, optionBytes.Length);
|
2022-11-05 22:22:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DllImport(dllName, EntryPoint = "RuntimeApi_LoadDifferentialHybridAssembly")]
|
2022-11-06 20:16:38 +08:00
|
|
|
|
public static extern unsafe int LoadDifferentialHybridAssembly(byte* dllBytes, int dllSize, byte* optionBytesPtr, int optionBytesLength);
|
2022-09-22 08:56:07 +08:00
|
|
|
|
|
2022-10-08 12:35:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取解释器线程栈的最大StackObject个数(size*8 为最终占用的内存大小)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2022-09-22 08:56:07 +08:00
|
|
|
|
[DllImport(dllName, EntryPoint = "RuntimeApi_GetInterpreterThreadObjectStackSize")]
|
|
|
|
|
public static extern int GetInterpreterThreadObjectStackSize();
|
|
|
|
|
|
2022-10-08 12:35:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置解释器线程栈的最大StackObject个数(size*8 为最终占用的内存大小)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="size"></param>
|
2022-09-22 08:56:07 +08:00
|
|
|
|
[DllImport(dllName, EntryPoint = "RuntimeApi_SetInterpreterThreadObjectStackSize")]
|
|
|
|
|
public static extern void SetInterpreterThreadObjectStackSize(int size);
|
|
|
|
|
|
2022-10-08 12:35:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取解释器线程函数帧数量(sizeof(InterpreterFrame)*size 为最终占用的内存大小)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2022-09-22 08:56:07 +08:00
|
|
|
|
[DllImport(dllName, EntryPoint = "RuntimeApi_GetInterpreterThreadFrameStackSize")]
|
|
|
|
|
public static extern int GetInterpreterThreadFrameStackSize();
|
|
|
|
|
|
2022-10-08 12:35:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置解释器线程函数帧数量(sizeof(InterpreterFrame)*size 为最终占用的内存大小)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="size"></param>
|
2022-09-22 08:56:07 +08:00
|
|
|
|
[DllImport(dllName, EntryPoint = "RuntimeApi_SetInterpreterThreadFrameStackSize")]
|
|
|
|
|
public static extern void SetInterpreterThreadFrameStackSize(int size);
|
|
|
|
|
}
|
|
|
|
|
}
|