ILRuntimeLauncher.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using ILRuntime.Runtime.Enviorment;
  2. using System.Collections;
  3. using System.IO;
  4. using UnityEngine;
  5. using FairyGUI;
  6. using System.Reflection;
  7. using GFGGame.Launcher;
  8. //using ILRuntime.Generated.CrossBinding;
  9. using System;
  10. using VEngine;
  11. //下面这行为了取消使用WWW的警告,Unity2018以后推荐使用UnityWebRequest,处于兼容性考虑Demo依然使用WWW
  12. #pragma warning disable CS0618
  13. namespace GFGGame
  14. {
  15. class ILRuntimeLauncher : SingletonMonoBase<ILRuntimeLauncher>
  16. {
  17. ILRuntime.Runtime.Enviorment.AppDomain appdomain;
  18. System.IO.MemoryStream dllStream;
  19. System.IO.MemoryStream pdbStream;
  20. public void LoadAssembly(byte[] assBytes, byte[] pdbBytes)
  21. {
  22. //首先实例化ILRuntime的AppDomain,AppDomain是一个应用程序域,每个AppDomain都是一个独立的沙盒
  23. appdomain = new ILRuntime.Runtime.Enviorment.AppDomain();
  24. dllStream = new MemoryStream(assBytes);
  25. pdbStream = new MemoryStream(pdbBytes);
  26. try
  27. {
  28. appdomain.LoadAssembly(dllStream, pdbStream, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
  29. }
  30. catch
  31. {
  32. Debug.LogError("加载热更DLL失败,请确保已经编译过热更DLL");
  33. }
  34. InitializeILRuntime();
  35. OnILHotFixLoaded();
  36. }
  37. void InitializeILRuntime()
  38. {
  39. #if DEBUG && (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE)
  40. //由于Unity的Profiler接口只允许在主线程使用,为了避免出异常,需要告诉ILRuntime主线程的线程ID才能正确将函数运行耗时报告给Profiler
  41. appdomain.UnityMainThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId;
  42. appdomain.DebugService.StartDebugService(56000);
  43. #endif
  44. //在使用LitJson前,需要对LitJson进行注册
  45. ILRuntimeHelper.RegisterILRuntimeCLRRedirection(appdomain);
  46. //这里做一些ILRuntime的注册,如委托适配器,值类型绑定等等
  47. appdomain.DelegateManager.RegisterMethodDelegate<EventContext>();
  48. appdomain.DelegateManager.RegisterMethodDelegate<System.String>();
  49. appdomain.DelegateManager.RegisterMethodDelegate<System.String, System.String, UnityEngine.LogType>();
  50. appdomain.DelegateManager.RegisterMethodDelegate<System.Object>();
  51. appdomain.DelegateManager.RegisterMethodDelegate<System.Int32, FairyGUI.GObject>();
  52. appdomain.DelegateManager.RegisterMethodDelegate<FairyGUI.GTweener>();
  53. appdomain.DelegateManager.RegisterFunctionDelegate<System.Int32, System.Int32, System.Int32>();
  54. appdomain.DelegateManager.RegisterFunctionDelegate<ILRuntime.Runtime.Intepreter.ILTypeInstance, ILRuntime.Runtime.Intepreter.ILTypeInstance, System.Int32>();
  55. appdomain.DelegateManager.RegisterMethodDelegate<System.Object, System.Timers.ElapsedEventArgs>();
  56. appdomain.DelegateManager.RegisterDelegateConvertor<EventCallback0>((action) =>
  57. {
  58. return new EventCallback0(() =>
  59. {
  60. ((System.Action)action)();
  61. });
  62. });
  63. appdomain.DelegateManager.RegisterDelegateConvertor<EventCallback1>((action) =>
  64. {
  65. return new EventCallback1((e) =>
  66. {
  67. ((System.Action<EventContext>)action)(e);
  68. });
  69. });
  70. appdomain.DelegateManager.RegisterDelegateConvertor<UnityEngine.Application.LogCallback>((act) =>
  71. {
  72. return new UnityEngine.Application.LogCallback((condition, stackTrace, type) =>
  73. {
  74. ((Action<System.String, System.String, UnityEngine.LogType>)act)(condition, stackTrace, type);
  75. });
  76. });
  77. appdomain.DelegateManager.RegisterDelegateConvertor<FairyGUI.TimerCallback>((act) =>
  78. {
  79. return new FairyGUI.TimerCallback((param) =>
  80. {
  81. ((Action<System.Object>)act)(param);
  82. });
  83. });
  84. appdomain.DelegateManager.RegisterDelegateConvertor<FairyGUI.ListItemRenderer>((act) =>
  85. {
  86. return new FairyGUI.ListItemRenderer((index, item) =>
  87. {
  88. ((Action<System.Int32, FairyGUI.GObject>)act)(index, item);
  89. });
  90. });
  91. appdomain.DelegateManager.RegisterDelegateConvertor<FairyGUI.GTweenCallback1>((act) =>
  92. {
  93. return new FairyGUI.GTweenCallback1((tweener) =>
  94. {
  95. ((Action<FairyGUI.GTweener>)act)(tweener);
  96. });
  97. });
  98. appdomain.DelegateManager.RegisterDelegateConvertor<System.Comparison<System.Int32>>((act) =>
  99. {
  100. return new System.Comparison<System.Int32>((x, y) =>
  101. {
  102. return ((Func<System.Int32, System.Int32, System.Int32>)act)(x, y);
  103. });
  104. });
  105. appdomain.DelegateManager.RegisterDelegateConvertor<FairyGUI.GTweenCallback>((act) =>
  106. {
  107. return new FairyGUI.GTweenCallback(() =>
  108. {
  109. ((Action)act)();
  110. });
  111. });
  112. appdomain.DelegateManager.RegisterDelegateConvertor<FairyGUI.PlayCompleteCallback>((act) =>
  113. {
  114. return new FairyGUI.PlayCompleteCallback(() =>
  115. {
  116. ((Action)act)();
  117. });
  118. });
  119. appdomain.DelegateManager.RegisterDelegateConvertor<FairyGUI.TransitionHook>((act) =>
  120. {
  121. return new FairyGUI.TransitionHook(() =>
  122. {
  123. ((Action)act)();
  124. });
  125. });
  126. appdomain.DelegateManager.RegisterDelegateConvertor<System.Comparison<ILRuntime.Runtime.Intepreter.ILTypeInstance>>((act) =>
  127. {
  128. return new System.Comparison<ILRuntime.Runtime.Intepreter.ILTypeInstance>((x, y) =>
  129. {
  130. return ((Func<ILRuntime.Runtime.Intepreter.ILTypeInstance, ILRuntime.Runtime.Intepreter.ILTypeInstance, System.Int32>)act)(x, y);
  131. });
  132. });
  133. appdomain.DelegateManager.RegisterDelegateConvertor<System.Timers.ElapsedEventHandler>((act) =>
  134. {
  135. return new System.Timers.ElapsedEventHandler((sender, e) =>
  136. {
  137. ((Action<System.Object, System.Timers.ElapsedEventArgs>)act)(sender, e);
  138. });
  139. });
  140. //appdomain.RegisterCrossBindingAdaptor(new ScriptableObjectAdapter());
  141. //初始化CLR绑定请放在初始化的最后一步!!
  142. ILRuntime.Runtime.Generated.CLRBindings.Initialize(appdomain);
  143. }
  144. void OnILHotFixLoaded()
  145. {
  146. //第一次方法调用
  147. appdomain.Invoke("GFGGame.HotUpdate.HotUpdateEntry", "Start", null, null);
  148. }
  149. }
  150. }