CodeLoader.cs 3.6 KB

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