CodeLoader.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. private CodeLoader()
  18. {
  19. }
  20. public void Start()
  21. {
  22. switch (Init.Instance.CodeMode)
  23. {
  24. case CodeMode.Mono:
  25. {
  26. Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  27. byte[] assBytes = ((TextAsset)dictionary["Code.dll"]).bytes;
  28. byte[] pdbBytes = ((TextAsset)dictionary["Code.pdb"]).bytes;
  29. assembly = Assembly.Load(assBytes, pdbBytes);
  30. this.allTypes = assembly.GetTypes();
  31. IStaticMethod start = new MonoStaticMethod(assembly, "ET.Entry", "Start");
  32. start.Run();
  33. break;
  34. }
  35. case CodeMode.ILRuntime:
  36. {
  37. Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  38. byte[] assBytes = ((TextAsset)dictionary["Code.dll"]).bytes;
  39. byte[] pdbBytes = ((TextAsset)dictionary["Code.pdb"]).bytes;
  40. //byte[] assBytes = File.ReadAllBytes(Path.Combine("../Unity/", Define.BuildOutputDir, "Code.dll"));
  41. //byte[] pdbBytes = File.ReadAllBytes(Path.Combine("../Unity/", Define.BuildOutputDir, "Code.pdb"));
  42. ILRuntime.Runtime.Enviorment.AppDomain appDomain = new ILRuntime.Runtime.Enviorment.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 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[] logicFiles = Directory.GetFiles(Define.BuildOutputDir, "Logic_*.dll");
  72. if (logicFiles.Length != 1)
  73. {
  74. throw new Exception("Logic dll count != 1");
  75. }
  76. string logicName = Path.GetFileNameWithoutExtension(logicFiles[0]);
  77. byte[] assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, $"{logicName}.dll"));
  78. byte[] pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, $"{logicName}.pdb"));
  79. Assembly hotfixAssembly = Assembly.Load(assBytes, pdbBytes);
  80. List<Type> listType = new List<Type>();
  81. listType.AddRange(this.assembly.GetTypes());
  82. listType.AddRange(hotfixAssembly.GetTypes());
  83. this.allTypes = listType.ToArray();
  84. }
  85. public Type[] GetTypes()
  86. {
  87. return this.allTypes;
  88. }
  89. }
  90. }