CodeLoader.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using UnityEngine;
  6. using System.Linq;
  7. namespace ET
  8. {
  9. public class CodeLoader: IDisposable
  10. {
  11. public static CodeLoader Instance = new CodeLoader();
  12. public Action Update;
  13. public Action LateUpdate;
  14. public Action OnApplicationQuit;
  15. private Assembly assembly;
  16. private ILRuntime.Runtime.Enviorment.AppDomain appDomain;
  17. private Type[] allTypes;
  18. public CodeMode CodeMode { get; set; }
  19. private CodeLoader()
  20. {
  21. }
  22. public void Dispose()
  23. {
  24. this.appDomain?.Dispose();
  25. }
  26. public void Start()
  27. {
  28. switch (this.CodeMode)
  29. {
  30. case CodeMode.Mono:
  31. {
  32. Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  33. byte[] assBytes = ((TextAsset)dictionary["Code.dll"]).bytes;
  34. byte[] pdbBytes = ((TextAsset)dictionary["Code.pdb"]).bytes;
  35. assembly = Assembly.Load(assBytes, pdbBytes);
  36. this.allTypes = assembly.GetTypes();
  37. IStaticMethod start = new MonoStaticMethod(assembly, "ET.Entry", "Start");
  38. start.Run();
  39. break;
  40. }
  41. case CodeMode.ILRuntime:
  42. {
  43. Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  44. byte[] assBytes = ((TextAsset)dictionary["Code.dll"]).bytes;
  45. byte[] pdbBytes = ((TextAsset)dictionary["Code.pdb"]).bytes;
  46. //byte[] assBytes = File.ReadAllBytes(Path.Combine("../Unity/", Define.BuildOutputDir, "Code.dll"));
  47. //byte[] pdbBytes = File.ReadAllBytes(Path.Combine("../Unity/", Define.BuildOutputDir, "Code.pdb"));
  48. appDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
  49. MemoryStream assStream = new MemoryStream(assBytes);
  50. MemoryStream pdbStream = new MemoryStream(pdbBytes);
  51. appDomain.LoadAssembly(assStream, pdbStream, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
  52. ILHelper.InitILRuntime(appDomain);
  53. this.allTypes = appDomain.LoadedTypes.Values.Select(x => x.ReflectionType).ToArray();
  54. IStaticMethod start = new ILStaticMethod(appDomain, "ET.Entry", "Start", 0);
  55. start.Run();
  56. break;
  57. }
  58. case CodeMode.Reload:
  59. {
  60. byte[] assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Data.dll"));
  61. byte[] pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Data.pdb"));
  62. assembly = Assembly.Load(assBytes, pdbBytes);
  63. this.LoadLogic();
  64. IStaticMethod start = new MonoStaticMethod(assembly, "ET.Entry", "Start");
  65. start.Run();
  66. break;
  67. }
  68. }
  69. }
  70. // 热重载调用下面三个方法
  71. // CodeLoader.Instance.LoadLogic();
  72. // Game.EventSystem.Add(CodeLoader.Instance.GetTypes());
  73. // Game.EventSystem.Load();
  74. public void LoadLogic()
  75. {
  76. if (this.CodeMode != CodeMode.Reload)
  77. {
  78. throw new Exception("CodeMode != Reload!");
  79. }
  80. // 傻屌Unity在这里搞了个傻逼优化,认为同一个路径的dll,返回的程序集就一样。所以这里每次编译都要随机名字
  81. string[] logicFiles = Directory.GetFiles(Define.BuildOutputDir, "Logic_*.dll");
  82. if (logicFiles.Length != 1)
  83. {
  84. throw new Exception("Logic dll count != 1");
  85. }
  86. string logicName = Path.GetFileNameWithoutExtension(logicFiles[0]);
  87. byte[] assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, $"{logicName}.dll"));
  88. byte[] pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, $"{logicName}.pdb"));
  89. Assembly hotfixAssembly = Assembly.Load(assBytes, pdbBytes);
  90. List<Type> listType = new List<Type>();
  91. listType.AddRange(this.assembly.GetTypes());
  92. listType.AddRange(hotfixAssembly.GetTypes());
  93. this.allTypes = listType.ToArray();
  94. }
  95. public Type[] GetTypes()
  96. {
  97. return this.allTypes;
  98. }
  99. }
  100. }