RuntimeApi.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 UnityEditor;
  10. using UnityEngine.Scripting;
  11. namespace HybridCLR
  12. {
  13. [Preserve]
  14. public static class RuntimeApi
  15. {
  16. /// <summary>
  17. /// load supplementary metadata assembly
  18. /// </summary>
  19. /// <param name="dllBytes"></param>
  20. /// <returns></returns>
  21. /// <exception cref="NotSupportedException"></exception>
  22. #if UNITY_EDITOR
  23. public static unsafe LoadImageErrorCode LoadMetadataForAOTAssembly(byte[] dllBytes, HomologousImageMode mode)
  24. {
  25. return LoadImageErrorCode.OK;
  26. }
  27. #else
  28. [MethodImpl(MethodImplOptions.InternalCall)]
  29. public static extern LoadImageErrorCode LoadMetadataForAOTAssembly(byte[] dllBytes, HomologousImageMode mode);
  30. #endif
  31. /// <summary>
  32. /// prejit method to avoid the jit cost of first time running
  33. /// </summary>
  34. /// <param name="method"></param>
  35. /// <returns>return true if method is jited, return false if method can't be jited </returns>
  36. ///
  37. #if UNITY_EDITOR
  38. public static bool PreJitMethod(MethodInfo method)
  39. {
  40. return false;
  41. }
  42. #else
  43. [MethodImpl(MethodImplOptions.InternalCall)]
  44. public static extern bool PreJitMethod(MethodInfo method);
  45. #endif
  46. /// <summary>
  47. /// prejit all methods of class to avoid the jit cost of first time running
  48. /// </summary>
  49. /// <param name="type"></param>
  50. /// <returns>return true if class is jited, return false if class can't be jited </returns>
  51. #if UNITY_EDITOR
  52. public static bool PreJitClass(Type type)
  53. {
  54. return false;
  55. }
  56. #else
  57. [MethodImpl(MethodImplOptions.InternalCall)]
  58. public static extern bool PreJitClass(Type type);
  59. #endif
  60. /// <summary>
  61. /// get the maximum number of StackObjects in the interpreter thread stack (size*8 represents the final memory size occupied
  62. /// </summary>
  63. /// <returns></returns>
  64. public static int GetInterpreterThreadObjectStackSize()
  65. {
  66. return GetRuntimeOption(RuntimeOptionId.InterpreterThreadObjectStackSize);
  67. }
  68. /// <summary>
  69. /// set the maximum number of StackObjects for the interpreter thread stack (size*8 represents the final memory size occupied)
  70. /// </summary>
  71. /// <param name="size"></param>
  72. public static void SetInterpreterThreadObjectStackSize(int size)
  73. {
  74. SetRuntimeOption(RuntimeOptionId.InterpreterThreadObjectStackSize, size);
  75. }
  76. /// <summary>
  77. /// get the number of interpreter thread function frames (sizeof(InterpreterFrame)*size represents the final memory size occupied)
  78. /// </summary>
  79. /// <returns></returns>
  80. public static int GetInterpreterThreadFrameStackSize()
  81. {
  82. return GetRuntimeOption(RuntimeOptionId.InterpreterThreadFrameStackSize);
  83. }
  84. /// <summary>
  85. /// set the number of interpreter thread function frames (sizeof(InterpreterFrame)*size represents the final memory size occupied)
  86. /// </summary>
  87. /// <param name="size"></param>
  88. public static void SetInterpreterThreadFrameStackSize(int size)
  89. {
  90. SetRuntimeOption(RuntimeOptionId.InterpreterThreadFrameStackSize, size);
  91. }
  92. #if UNITY_EDITOR
  93. private static readonly Dictionary<RuntimeOptionId, int> s_runtimeOptions = new Dictionary<RuntimeOptionId, int>();
  94. /// <summary>
  95. /// set runtime option value
  96. /// </summary>
  97. /// <param name="optionId"></param>
  98. /// <param name="value"></param>
  99. public static void SetRuntimeOption(RuntimeOptionId optionId, int value)
  100. {
  101. s_runtimeOptions[optionId] = value;
  102. }
  103. #else
  104. [MethodImpl(MethodImplOptions.InternalCall)]
  105. public static extern void SetRuntimeOption(RuntimeOptionId optionId, int value);
  106. #endif
  107. /// <summary>
  108. /// get runtime option value
  109. /// </summary>
  110. /// <param name="optionId"></param>
  111. /// <returns></returns>
  112. #if UNITY_EDITOR
  113. public static int GetRuntimeOption(RuntimeOptionId optionId)
  114. {
  115. if (s_runtimeOptions.TryGetValue(optionId, out var value))
  116. {
  117. return value;
  118. }
  119. return 0;
  120. }
  121. #else
  122. [MethodImpl(MethodImplOptions.InternalCall)]
  123. public static extern int GetRuntimeOption(RuntimeOptionId optionId);
  124. #endif
  125. }
  126. }