CodeLoader.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using UnityEngine;
  6. namespace ET
  7. {
  8. public class CodeLoader: IDisposable
  9. {
  10. public static CodeLoader Instance = new CodeLoader();
  11. public Action Update;
  12. public Action LateUpdate;
  13. public Action OnApplicationQuit;
  14. private Assembly assembly;
  15. public GlobalConfig GlobalConfig;
  16. private CodeLoader()
  17. {
  18. }
  19. public void Dispose()
  20. {
  21. }
  22. public void Start()
  23. {
  24. if (Define.EnableCodes)
  25. {
  26. this.GlobalConfig.LoadMode = LoadMode.Codes;
  27. }
  28. switch (this.GlobalConfig.LoadMode)
  29. {
  30. case LoadMode.Mono:
  31. {
  32. if (Define.EnableCodes)
  33. {
  34. throw new Exception("LoadMode.Mono must remove ENABLE_CODE define, please use ET/ChangeDefine/Remove ENABLE_CODE to Remove define");
  35. }
  36. Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  37. byte[] assBytes = ((TextAsset)dictionary["Code.dll"]).bytes;
  38. byte[] pdbBytes = ((TextAsset)dictionary["Code.pdb"]).bytes;
  39. assembly = Assembly.Load(assBytes, pdbBytes);
  40. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (Game).Assembly, this.assembly);
  41. Game.EventSystem.Add(types);
  42. IStaticMethod start = new StaticMethod(assembly, "ET.Client.Entry", "Start");
  43. start.Run();
  44. break;
  45. }
  46. case LoadMode.Reload:
  47. {
  48. if (Define.EnableCodes)
  49. {
  50. throw new Exception("LoadMode.Reload must remove ENABLE_CODE define, please use ET/ChangeDefine/Remove ENABLE_CODE to Remove define");
  51. }
  52. byte[] assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.dll"));
  53. byte[] pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.pdb"));
  54. assembly = Assembly.Load(assBytes, pdbBytes);
  55. this.LoadHotfix();
  56. IStaticMethod start = new StaticMethod(assembly, "ET.Client.Entry", "Start");
  57. start.Run();
  58. break;
  59. }
  60. case LoadMode.Codes:
  61. {
  62. if (!Define.EnableCodes)
  63. {
  64. throw new Exception("LoadMode.Codes must add ENABLE_CODE define, please use ET/ChangeDefine/Add ENABLE_CODE to add define");
  65. }
  66. Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  67. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(assemblies);
  68. Game.EventSystem.Add(types);
  69. foreach (Assembly ass in assemblies)
  70. {
  71. string name = ass.GetName().Name;
  72. if (name == "Unity.Codes")
  73. {
  74. this.assembly = ass;
  75. }
  76. }
  77. IStaticMethod start = new StaticMethod(assembly, "ET.Client.Entry", "Start");
  78. start.Run();
  79. break;
  80. }
  81. }
  82. }
  83. // 热重载调用下面两个方法
  84. // CodeLoader.Instance.LoadLogic();
  85. // Game.EventSystem.Load();
  86. public void LoadHotfix()
  87. {
  88. if (this.GlobalConfig.LoadMode != LoadMode.Reload)
  89. {
  90. throw new Exception("CodeMode != Reload!");
  91. }
  92. // 傻屌Unity在这里搞了个傻逼优化,认为同一个路径的dll,返回的程序集就一样。所以这里每次编译都要随机名字
  93. string[] logicFiles = Directory.GetFiles(Define.BuildOutputDir, "Hotfix_*.dll");
  94. if (logicFiles.Length != 1)
  95. {
  96. throw new Exception("Logic dll count != 1");
  97. }
  98. string logicName = Path.GetFileNameWithoutExtension(logicFiles[0]);
  99. byte[] assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, $"{logicName}.dll"));
  100. byte[] pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, $"{logicName}.pdb"));
  101. Assembly hotfixAssembly = Assembly.Load(assBytes, pdbBytes);
  102. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (Game).Assembly, this.assembly, hotfixAssembly);
  103. Game.EventSystem.Add(types);
  104. }
  105. }
  106. }