Init.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. AssemblyManager.Instance.Add("Model", typeof(Model.Init).Assembly);
  25. this.RegisterAssembly();
  26. this.RegisterILAdapter();
  27. this.RegisterDelegate();
  28. this.RegisterRedirection();
  29. IType hotfixInitType = AppDomain.LoadedTypes["Hotfix.Init"];
  30. start = hotfixInitType.GetMethod("Start", 0);
  31. update = hotfixInitType.GetMethod("Update", 0);
  32. onApplicationQuit = hotfixInitType.GetMethod("OnApplicationQuit", 0);
  33. // 进入热更新层
  34. this.AppDomain.Invoke(this.start, null, param0);
  35. }
  36. catch (Exception e)
  37. {
  38. Log.Error(e.ToString());
  39. }
  40. }
  41. private void Update()
  42. {
  43. this.AppDomain.Invoke(this.update, null, this.param0);
  44. ObjectEvents.Instance.Update();
  45. }
  46. private void OnApplicationQuit()
  47. {
  48. this.AppDomain.Invoke(this.onApplicationQuit, null, this.param0);
  49. }
  50. public void RegisterAssembly()
  51. {
  52. GameObject code = (GameObject)Resources.Load("Code");
  53. byte[] assBytes = code.GetComponent<ReferenceCollector>().Get<TextAsset>("Hotfix.dll").bytes;
  54. byte[] mdbBytes = code.GetComponent<ReferenceCollector>().Get<TextAsset>("Hotfix.pdb").bytes;
  55. using (MemoryStream fs = new MemoryStream(assBytes))
  56. using (MemoryStream p = new MemoryStream(mdbBytes))
  57. {
  58. AppDomain.LoadAssembly(fs, p, new Mono.Cecil.Pdb.PdbReaderProvider());
  59. }
  60. }
  61. public unsafe void RegisterRedirection()
  62. {
  63. MethodInfo mi = typeof(Log).GetMethod("Debug", new Type[] { typeof(string) });
  64. this.AppDomain.RegisterCLRMethodRedirection(mi, ILRedirection.LogDebug);
  65. }
  66. public void RegisterDelegate()
  67. {
  68. AppDomain.DelegateManager.RegisterMethodDelegate<AChannel, System.Net.Sockets.SocketError>();
  69. AppDomain.DelegateManager.RegisterMethodDelegate<byte[], int, int>();
  70. }
  71. public void RegisterILAdapter()
  72. {
  73. Assembly assembly = typeof(Init).Assembly;
  74. foreach (Type type in assembly.GetTypes())
  75. {
  76. object[] attrs = type.GetCustomAttributes(typeof(ILAdapterAttribute), false);
  77. if (attrs.Length == 0)
  78. {
  79. continue;
  80. }
  81. object obj = Activator.CreateInstance(type);
  82. CrossBindingAdaptor adaptor = obj as CrossBindingAdaptor;
  83. if (adaptor == null)
  84. {
  85. continue;
  86. }
  87. AppDomain.RegisterCrossBindingAdaptor(adaptor);
  88. }
  89. }
  90. }
  91. }