CodeLoader.cs 3.3 KB

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