CodeLoader.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using HybridCLR;
  6. using UnityEngine;
  7. #pragma warning disable CS0162
  8. namespace ET
  9. {
  10. public class CodeLoader: Singleton<CodeLoader>, ISingletonAwake
  11. {
  12. private Assembly assembly;
  13. private Dictionary<string, TextAsset> dlls;
  14. private Dictionary<string, TextAsset> aotDlls;
  15. public void Awake()
  16. {
  17. }
  18. public async ETTask DownloadAsync()
  19. {
  20. if (!Define.IsEditor)
  21. {
  22. this.dlls = await ResourcesComponent.Instance.LoadAllAssetsAsync<TextAsset>($"Assets/Bundles/Code/Model.dll.bytes");
  23. this.aotDlls = await ResourcesComponent.Instance.LoadAllAssetsAsync<TextAsset>($"Assets/Bundles/AotDlls/mscorlib.dll.bytes");
  24. }
  25. }
  26. public void Start()
  27. {
  28. if (!Define.EnableDll)
  29. {
  30. GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
  31. if (globalConfig.CodeMode != CodeMode.ClientServer)
  32. {
  33. throw new Exception("!ENABLE_CODES mode must use ClientServer code mode!");
  34. }
  35. Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  36. foreach (Assembly ass in assemblies)
  37. {
  38. string name = ass.GetName().Name;
  39. if (name == "Unity.Model")
  40. {
  41. this.assembly = ass;
  42. }
  43. }
  44. World.Instance.AddSingleton<CodeTypes, Assembly[]>(assemblies);
  45. }
  46. else
  47. {
  48. byte[] assBytes;
  49. byte[] pdbBytes;
  50. if (!Define.IsEditor)
  51. {
  52. assBytes = this.dlls["Model.dll"].bytes;
  53. pdbBytes = this.dlls["Model.pdb"].bytes;
  54. // 这里为了方便做测试,直接加载了Unity/Temp/Bin/Debug/Model.dll,真正打包要还原使用上面注释的代码
  55. //assBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Model.dll"));
  56. //pdbBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Model.pdb"));
  57. if (Define.EnableIL2CPP)
  58. {
  59. foreach (var kv in this.aotDlls)
  60. {
  61. TextAsset textAsset = kv.Value;
  62. RuntimeApi.LoadMetadataForAOTAssembly(textAsset.bytes, HomologousImageMode.SuperSet);
  63. }
  64. }
  65. }
  66. else
  67. {
  68. assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.dll"));
  69. pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.pdb"));
  70. }
  71. this.assembly = Assembly.Load(assBytes, pdbBytes);
  72. Assembly hotfixAssembly = this.LoadHotfix();
  73. World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly});
  74. }
  75. IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
  76. start.Run();
  77. }
  78. private Assembly LoadHotfix()
  79. {
  80. byte[] assBytes;
  81. byte[] pdbBytes;
  82. if (!Define.IsEditor)
  83. {
  84. assBytes = this.dlls["Hotfix.dll"].bytes;
  85. pdbBytes = this.dlls["Hotfix.pdb"].bytes;
  86. // 这里为了方便做测试,直接加载了Unity/Temp/Bin/Debug/Hotfix.dll,真正打包要还原使用上面注释的代码
  87. //assBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Hotfix.dll"));
  88. //pdbBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Hotfix.pdb"));
  89. }
  90. else
  91. {
  92. assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Hotfix.dll"));
  93. pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Hotfix.pdb"));
  94. }
  95. Assembly hotfixAssembly = Assembly.Load(assBytes, pdbBytes);
  96. return hotfixAssembly;
  97. }
  98. public void Reload()
  99. {
  100. Assembly hotfixAssembly = this.LoadHotfix();
  101. CodeTypes codeTypes = World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly});
  102. codeTypes.CreateCode();
  103. Log.Debug($"reload dll finish!");
  104. }
  105. }
  106. }