CodeLoader.cs 3.1 KB

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