CodeLoader.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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>, ISingletonAwake
  10. {
  11. private Assembly assembly;
  12. public void Awake()
  13. {
  14. }
  15. public void Start()
  16. {
  17. if (!Define.EnableDll)
  18. {
  19. GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
  20. if (globalConfig.CodeMode != CodeMode.ClientServer)
  21. {
  22. throw new Exception("!ENABLE_CODES mode must use ClientServer code mode!");
  23. }
  24. Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  25. foreach (Assembly ass in assemblies)
  26. {
  27. string name = ass.GetName().Name;
  28. if (name == "Unity.Model")
  29. {
  30. this.assembly = ass;
  31. }
  32. }
  33. World.Instance.AddSingleton<CodeTypes, Assembly[]>(assemblies);
  34. }
  35. else
  36. {
  37. byte[] assBytes;
  38. byte[] pdbBytes;
  39. if (!Define.IsEditor)
  40. {
  41. assBytes = (ResourcesComponent.Instance.GetAssets($"Assets/Bundles/Config/Model.dll.bytes") as TextAsset).bytes;
  42. pdbBytes = (ResourcesComponent.Instance.GetAssets($"Assets/Bundles/Config/Model.pdb.bytes") as TextAsset).bytes;
  43. // 这里为了方便做测试,直接加载了Unity/Temp/Bin/Debug/Model.dll,真正打包要还原使用上面注释的代码
  44. //assBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Model.dll"));
  45. //pdbBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Model.pdb"));
  46. if (Define.EnableIL2CPP)
  47. {
  48. HybridCLRHelper.Load();
  49. }
  50. }
  51. else
  52. {
  53. assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.dll"));
  54. pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.pdb"));
  55. }
  56. this.assembly = Assembly.Load(assBytes, pdbBytes);
  57. Assembly hotfixAssembly = this.LoadHotfix();
  58. World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly});
  59. }
  60. IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
  61. start.Run();
  62. }
  63. private Assembly LoadHotfix()
  64. {
  65. byte[] assBytes;
  66. byte[] pdbBytes;
  67. if (!Define.IsEditor)
  68. {
  69. assBytes = (ResourcesComponent.Instance.GetAssets($"Assets/Bundles/Config/Hotfix.dll.bytes") as TextAsset).bytes;
  70. pdbBytes = (ResourcesComponent.Instance.GetAssets($"Assets/Bundles/Config/Hotfix.pdb.bytes") as TextAsset).bytes;
  71. // 这里为了方便做测试,直接加载了Unity/Temp/Bin/Debug/Hotfix.dll,真正打包要还原使用上面注释的代码
  72. //assBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Hotfix.dll"));
  73. //pdbBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Hotfix.pdb"));
  74. }
  75. else
  76. {
  77. assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Hotfix.dll"));
  78. pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Hotfix.pdb"));
  79. }
  80. Assembly hotfixAssembly = Assembly.Load(assBytes, pdbBytes);
  81. return hotfixAssembly;
  82. }
  83. public void Reload()
  84. {
  85. Assembly hotfixAssembly = this.LoadHotfix();
  86. CodeTypes codeTypes = World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly});
  87. codeTypes.CreateCode();
  88. Log.Debug($"reload dll finish!");
  89. }
  90. }
  91. }