Init.cs 2.2 KB

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