CodeLoader.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using HybridCLR;
  6. using UnityEngine;
  7. namespace ET
  8. {
  9. public class CodeLoader : Singleton<CodeLoader>, ISingletonAwake
  10. {
  11. private Assembly assembly;
  12. private Dictionary<string, TextAsset> dlls;
  13. private Dictionary<string, TextAsset> aotDlls;
  14. public void Awake()
  15. {
  16. }
  17. public async ETTask DownloadAsync()
  18. {
  19. if (!Define.IsEditor)
  20. {
  21. this.dlls = await ResourcesComponent.Instance.LoadAllAssetsAsync<TextAsset>($"Assets/Bundles/Code/Model.dll.bytes");
  22. this.aotDlls = await ResourcesComponent.Instance.LoadAllAssetsAsync<TextAsset>($"Assets/Bundles/AotDlls/mscorlib.dll.bytes");
  23. }
  24. }
  25. public void Start()
  26. {
  27. if (!Define.EnableDll)
  28. {
  29. GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
  30. if (globalConfig.CodeMode != CodeMode.ClientServer)
  31. {
  32. throw new Exception("!ENABLE_DLL mode must use ClientServer code mode!");
  33. }
  34. Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  35. foreach (Assembly ass in assemblies)
  36. {
  37. string name = ass.GetName().Name;
  38. if (name == "Unity.Model")
  39. {
  40. this.assembly = ass;
  41. break;
  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. // 如果需要测试,可替换成下面注释的代码直接加载Assets/Bundles/Code/Model.dll.bytes,但真正打包时必须使用上面的代码
  55. //assBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Model.dll.bytes"));
  56. //pdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Model.pdb.bytes"));
  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.CodeDir, "Model.dll.bytes"));
  69. pdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Model.pdb.bytes"));
  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. // 如果需要测试,可替换成下面注释的代码直接加载Assets/Bundles/Code/Hotfix.dll.bytes,但真正打包时必须使用上面的代码
  87. //assBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Hotfix.dll.bytes"));
  88. //pdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Hotfix.pdb.bytes"));
  89. }
  90. else
  91. {
  92. assBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Hotfix.dll.bytes"));
  93. pdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Hotfix.pdb.bytes"));
  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. }