ILRuntimeLauncher.cs 7.1 KB

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