RuntimeApi.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using UnityEngine.Scripting;
  10. namespace HybridCLR
  11. {
  12. [Preserve]
  13. public static class RuntimeApi
  14. {
  15. /// <summary>
  16. /// 加载补充元数据assembly
  17. /// </summary>
  18. /// <param name="dllBytes"></param>
  19. /// <returns></returns>
  20. /// <exception cref="NotSupportedException"></exception>
  21. #if UNITY_EDITOR
  22. public static unsafe LoadImageErrorCode LoadMetadataForAOTAssembly(byte[] dllBytes, HomologousImageMode mode)
  23. {
  24. return LoadImageErrorCode.OK;
  25. }
  26. #else
  27. [MethodImpl(MethodImplOptions.InternalCall)]
  28. public static extern LoadImageErrorCode LoadMetadataForAOTAssembly(byte[] dllBytes, HomologousImageMode mode);
  29. #endif
  30. /// <summary>
  31. /// 获取解释器线程栈的最大StackObject个数(size*8 为最终占用的内存大小)
  32. /// </summary>
  33. /// <returns></returns>
  34. public static int GetInterpreterThreadObjectStackSize()
  35. {
  36. return GetRuntimeOption(RuntimeOptionId.InterpreterThreadObjectStackSize);
  37. }
  38. /// <summary>
  39. /// 设置解释器线程栈的最大StackObject个数(size*8 为最终占用的内存大小)
  40. /// </summary>
  41. /// <param name="size"></param>
  42. public static void SetInterpreterThreadObjectStackSize(int size)
  43. {
  44. SetRuntimeOption(RuntimeOptionId.InterpreterThreadObjectStackSize, size);
  45. }
  46. /// <summary>
  47. /// 获取解释器线程函数帧数量(sizeof(InterpreterFrame)*size 为最终占用的内存大小)
  48. /// </summary>
  49. /// <returns></returns>
  50. public static int GetInterpreterThreadFrameStackSize()
  51. {
  52. return GetRuntimeOption(RuntimeOptionId.InterpreterThreadFrameStackSize);
  53. }
  54. /// <summary>
  55. /// 设置解释器线程函数帧数量(sizeof(InterpreterFrame)*size 为最终占用的内存大小)
  56. /// </summary>
  57. /// <param name="size"></param>
  58. public static void SetInterpreterThreadFrameStackSize(int size)
  59. {
  60. SetRuntimeOption(RuntimeOptionId.InterpreterThreadFrameStackSize, size);
  61. }
  62. #if UNITY_EDITOR
  63. private static readonly Dictionary<RuntimeOptionId, int> s_runtimeOptions = new Dictionary<RuntimeOptionId, int>();
  64. public static void SetRuntimeOption(RuntimeOptionId optionId, int value)
  65. {
  66. s_runtimeOptions[optionId] = value;
  67. }
  68. #else
  69. [MethodImpl(MethodImplOptions.InternalCall)]
  70. public static extern void SetRuntimeOption(RuntimeOptionId optionId, int value);
  71. #endif
  72. #if UNITY_EDITOR
  73. public static int GetRuntimeOption(RuntimeOptionId optionId)
  74. {
  75. if (s_runtimeOptions.TryGetValue(optionId, out var value))
  76. {
  77. return value;
  78. }
  79. return 0;
  80. }
  81. #else
  82. [MethodImpl(MethodImplOptions.InternalCall)]
  83. public static extern int GetRuntimeOption(RuntimeOptionId optionId);
  84. #endif
  85. }
  86. }