CodeLoader.cs 3.5 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. private Assembly assembly;
  11. private static CodeLoader instance;
  12. public static CodeLoader Instance
  13. {
  14. get
  15. {
  16. return instance ??= new CodeLoader();
  17. }
  18. }
  19. private CodeLoader()
  20. {
  21. }
  22. public void Dispose()
  23. {
  24. instance = null;
  25. }
  26. public void Start()
  27. {
  28. if (Define.EnableCodes)
  29. {
  30. Init.Instance.GlobalConfig.LoadMode = LoadMode.Codes;
  31. }
  32. switch (Init.Instance.GlobalConfig.LoadMode)
  33. {
  34. case LoadMode.Mono:
  35. {
  36. if (Define.EnableCodes)
  37. {
  38. throw new Exception("LoadMode.Mono must remove ENABLE_CODE define, please use ET/ChangeDefine/Remove ENABLE_CODE to Remove define");
  39. }
  40. Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  41. byte[] assBytes = ((TextAsset)dictionary["Code.dll"]).bytes;
  42. byte[] pdbBytes = ((TextAsset)dictionary["Code.pdb"]).bytes;
  43. assembly = Assembly.Load(assBytes, pdbBytes);
  44. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (Game).Assembly, this.assembly);
  45. Game.EventSystem.Add(types);
  46. break;
  47. }
  48. case LoadMode.Reload:
  49. {
  50. if (Define.EnableCodes)
  51. {
  52. throw new Exception("LoadMode.Reload must remove ENABLE_CODE define, please use ET/ChangeDefine/Remove ENABLE_CODE to Remove define");
  53. }
  54. byte[] assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.dll"));
  55. byte[] pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.pdb"));
  56. assembly = Assembly.Load(assBytes, pdbBytes);
  57. this.LoadHotfix();
  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. break;
  78. }
  79. }
  80. IStaticMethod start = new StaticMethod(assembly, "ET.Entry", "Start");
  81. start.Run();
  82. }
  83. // 热重载调用下面两个方法
  84. // CodeLoader.Instance.LoadLogic();
  85. // Game.EventSystem.Load();
  86. public void LoadHotfix()
  87. {
  88. if (Init.Instance.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. }