CodeLoader.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.dll"));
  50. pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.pdb"));
  51. }
  52. this.model = Assembly.Load(assBytes, pdbBytes);
  53. if (SerializeHelper.UseMemoryPack)
  54. {
  55. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (Game).Assembly, typeof(Init).Assembly, this.model);
  56. EventSystem.Instance.Add(types);
  57. }
  58. else
  59. {
  60. this.LoadHotfix();
  61. }
  62. }
  63. IStaticMethod start = new StaticMethod(this.model, "ET.Entry", "Start");
  64. start.Run();
  65. }
  66. // 热重载调用该方法
  67. public void LoadHotfix()
  68. {
  69. byte[] assBytes;
  70. byte[] pdbBytes;
  71. if (!Define.IsEditor)
  72. {
  73. Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  74. assBytes = ((TextAsset)dictionary["Hotfix.dll"]).bytes;
  75. pdbBytes = ((TextAsset)dictionary["Hotfix.pdb"]).bytes;
  76. }
  77. else
  78. {
  79. // 傻屌Unity在这里搞了个傻逼优化,认为同一个路径的dll,返回的程序集就一样。所以这里每次编译都要随机名字
  80. string[] logicFiles = Directory.GetFiles(Define.BuildOutputDir, "Hotfix_*.dll");
  81. if (logicFiles.Length != 1)
  82. {
  83. throw new Exception("Logic dll count != 1");
  84. }
  85. string logicName = Path.GetFileNameWithoutExtension(logicFiles[0]);
  86. assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, $"{logicName}.dll"));
  87. pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, $"{logicName}.pdb"));
  88. //assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Hotfix.dll"));
  89. //pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Hotfix.pdb"));
  90. }
  91. Assembly hotfixAssembly = Assembly.Load(assBytes, pdbBytes);
  92. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (Game).Assembly, typeof(Init).Assembly, this.model, hotfixAssembly);
  93. EventSystem.Instance.Add(types);
  94. }
  95. }
  96. }