CodeLoader.cs 3.6 KB

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