Init.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using Model;
  5. using ILRuntime.CLR.Method;
  6. using ILRuntime.CLR.TypeSystem;
  7. using ILRuntime.Runtime.Enviorment;
  8. using UnityEngine;
  9. namespace Model
  10. {
  11. public class Init: MonoBehaviour
  12. {
  13. public static Init Instance;
  14. public ILRuntime.Runtime.Enviorment.AppDomain AppDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
  15. private readonly object[] param0 = new object[0];
  16. private IMethod start;
  17. private IMethod update;
  18. private IMethod onApplicationQuit;
  19. private void Start()
  20. {
  21. try
  22. {
  23. Instance = this;
  24. this.RegisterAssembly();
  25. this.RegisterILAdapter();
  26. this.RegisterDelegate();
  27. this.RegisterRedirection();
  28. IType hotfixInitType = AppDomain.LoadedTypes["Hotfix.HotfixInit"];
  29. start = hotfixInitType.GetMethod("Start", 0);
  30. update = hotfixInitType.GetMethod("Update", 0);
  31. onApplicationQuit = hotfixInitType.GetMethod("OnApplicationQuit", 0);
  32. // 进入热更新层
  33. this.AppDomain.Invoke(this.start, null, param0);
  34. }
  35. catch (Exception e)
  36. {
  37. Log.Error(e.ToString());
  38. }
  39. }
  40. private void Update()
  41. {
  42. this.AppDomain.Invoke(this.update, null, this.param0);
  43. }
  44. private void OnApplicationQuit()
  45. {
  46. this.AppDomain.Invoke(this.onApplicationQuit, null, this.param0);
  47. }
  48. public void RegisterAssembly()
  49. {
  50. GameObject code = (GameObject)Resources.Load("Code");
  51. byte[] assBytes = code.GetComponent<ReferenceCollector>().Get<TextAsset>("Hotfix.dll").bytes;
  52. byte[] mdbBytes = code.GetComponent<ReferenceCollector>().Get<TextAsset>("Hotfix.pdb").bytes;
  53. using (MemoryStream fs = new MemoryStream(assBytes))
  54. using (MemoryStream p = new MemoryStream(mdbBytes))
  55. {
  56. AppDomain.LoadAssembly(fs, p, new Mono.Cecil.Pdb.PdbReaderProvider());
  57. }
  58. }
  59. public unsafe void RegisterRedirection()
  60. {
  61. var mi = typeof(Log).GetMethod("Debug", new System.Type[] { typeof(string) });
  62. this.AppDomain.RegisterCLRMethodRedirection(mi, ILRedirection.LogDebug);
  63. }
  64. public void RegisterDelegate()
  65. {
  66. AppDomain.DelegateManager.RegisterMethodDelegate<AChannel, System.Net.Sockets.SocketError>();
  67. AppDomain.DelegateManager.RegisterMethodDelegate<System.Byte[], System.Int32, System.Int32>();
  68. }
  69. public void RegisterILAdapter()
  70. {
  71. Assembly assembly = typeof(Init).Assembly;
  72. foreach (Type type in assembly.GetTypes())
  73. {
  74. object[] attrs = type.GetCustomAttributes(typeof(ILAdapterAttribute), false);
  75. if (attrs.Length == 0)
  76. {
  77. continue;
  78. }
  79. object obj = Activator.CreateInstance(type);
  80. CrossBindingAdaptor adaptor = obj as CrossBindingAdaptor;
  81. if (adaptor == null)
  82. {
  83. continue;
  84. }
  85. AppDomain.RegisterCrossBindingAdaptor(adaptor);
  86. }
  87. }
  88. }
  89. }