Init.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. ObjectEvents.Instance.Update();
  43. this.AppDomain.Invoke(this.update, null, this.param0);
  44. }
  45. private void OnApplicationQuit()
  46. {
  47. this.AppDomain.Invoke(this.onApplicationQuit, null, this.param0);
  48. }
  49. public void RegisterAssembly()
  50. {
  51. GameObject code = (GameObject)Resources.Load("Code");
  52. byte[] assBytes = code.GetComponent<ReferenceCollector>().Get<TextAsset>("Hotfix.dll").bytes;
  53. byte[] mdbBytes = code.GetComponent<ReferenceCollector>().Get<TextAsset>("Hotfix.pdb").bytes;
  54. using (MemoryStream fs = new MemoryStream(assBytes))
  55. using (MemoryStream p = new MemoryStream(mdbBytes))
  56. {
  57. AppDomain.LoadAssembly(fs, p, new Mono.Cecil.Pdb.PdbReaderProvider());
  58. }
  59. }
  60. public unsafe void RegisterRedirection()
  61. {
  62. var mi = typeof(Log).GetMethod("Debug", new System.Type[] { typeof(string) });
  63. this.AppDomain.RegisterCLRMethodRedirection(mi, ILRedirection.LogDebug);
  64. }
  65. public void RegisterDelegate()
  66. {
  67. AppDomain.DelegateManager.RegisterMethodDelegate<AChannel, System.Net.Sockets.SocketError>();
  68. AppDomain.DelegateManager.RegisterMethodDelegate<System.Byte[], System.Int32, System.Int32>();
  69. }
  70. public void RegisterILAdapter()
  71. {
  72. Assembly assembly = typeof(Init).Assembly;
  73. foreach (Type type in assembly.GetTypes())
  74. {
  75. object[] attrs = type.GetCustomAttributes(typeof(ILAdapterAttribute), false);
  76. if (attrs.Length == 0)
  77. {
  78. continue;
  79. }
  80. object obj = Activator.CreateInstance(type);
  81. CrossBindingAdaptor adaptor = obj as CrossBindingAdaptor;
  82. if (adaptor == null)
  83. {
  84. continue;
  85. }
  86. AppDomain.RegisterCrossBindingAdaptor(adaptor);
  87. }
  88. }
  89. }
  90. }