CodeLoader.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 modelAssembly;
  12. private Assembly modelViewAssembly;
  13. private Dictionary<string, TextAsset> dlls;
  14. private Dictionary<string, TextAsset> aotDlls;
  15. public void Awake()
  16. {
  17. }
  18. private async ETTask DownloadAsync()
  19. {
  20. if (!Define.IsEditor)
  21. {
  22. this.dlls = await ResourcesComponent.Instance.LoadAllAssetsAsync<TextAsset>($"Packages/cn.etetet.loader/Code/ET.Model.dll.bytes");
  23. if (Define.EnableIL2CPP)
  24. {
  25. this.aotDlls = await ResourcesComponent.Instance.LoadAllAssetsAsync<TextAsset>($"Packages/cn.etetet.loader/AotDlls/mscorlib.dll.bytes");
  26. }
  27. }
  28. }
  29. public async ETTask Start()
  30. {
  31. await DownloadAsync();
  32. if (!Define.IsEditor)
  33. {
  34. byte[] modelAssBytes = this.dlls["ET.Model.dll"].bytes;
  35. byte[] modelPdbBytes = this.dlls["ET.Model.pdb"].bytes;
  36. byte[] modelViewAssBytes = this.dlls["ET.ModelView.dll"].bytes;
  37. byte[] modelViewPdbBytes = this.dlls["ET.ModelView.pdb"].bytes;
  38. // 如果需要测试,可替换成下面注释的代码直接加载Packages/cn.etetet.loader/Code/ET.Model.dll.bytes,但真正打包时必须使用上面的代码
  39. //modelAssBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "ET.Model.dll.bytes"));
  40. //modelPdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "ET.Model.pdb.bytes"));
  41. //modelViewAssBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "ET.ModelView.dll.bytes"));
  42. //modelViewPdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "ET.ModelView.pdb.bytes"));
  43. if (Define.EnableIL2CPP)
  44. {
  45. foreach (var kv in this.aotDlls)
  46. {
  47. TextAsset textAsset = kv.Value;
  48. RuntimeApi.LoadMetadataForAOTAssembly(textAsset.bytes, HomologousImageMode.SuperSet);
  49. }
  50. }
  51. this.modelAssembly = Assembly.Load(modelAssBytes, modelPdbBytes);
  52. this.modelViewAssembly = Assembly.Load(modelViewAssBytes, modelViewPdbBytes);
  53. }
  54. else
  55. {
  56. byte[] modelAssBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "ET.Model.dll.bytes"));
  57. byte[] modelPdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "ET.Model.pdb.bytes"));
  58. byte[] modelViewAssBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "ET.ModelView.dll.bytes"));
  59. byte[] modelViewPdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "ET.ModelView.pdb.bytes"));
  60. this.modelAssembly = Assembly.Load(modelAssBytes, modelPdbBytes);
  61. this.modelViewAssembly = Assembly.Load(modelViewAssBytes, modelViewPdbBytes);
  62. }
  63. (Assembly hotfixAssembly, Assembly hotfixViewAssembly) = this.LoadHotfix();
  64. World.Instance.AddSingleton<CodeTypes, Assembly[]>(new[]
  65. {
  66. typeof (World).Assembly, typeof (Init).Assembly, this.modelAssembly, this.modelViewAssembly, hotfixAssembly,
  67. hotfixViewAssembly
  68. });
  69. IStaticMethod start = new StaticMethod(this.modelAssembly, "ET.Entry", "Start");
  70. start.Run();
  71. }
  72. private (Assembly, Assembly) LoadHotfix()
  73. {
  74. byte[] hotfixAssBytes;
  75. byte[] hotfixPdbBytes;
  76. byte[] hotfixViewAssBytes;
  77. byte[] hotfixViewPdbBytes;
  78. Assembly hotfixAssembly = null;
  79. Assembly hotfixViewAssembly = null;
  80. if (!Define.IsEditor)
  81. {
  82. hotfixAssBytes = this.dlls["ET.Hotfix.dll"].bytes;
  83. hotfixPdbBytes = this.dlls["ET.Hotfix.pdb"].bytes;
  84. hotfixViewAssBytes = this.dlls["ET.HotfixView.dll"].bytes;
  85. hotfixViewPdbBytes = this.dlls["ET.HotfixView.pdb"].bytes;
  86. // 如果需要测试,可替换成下面注释的代码直接加载Packages/cn.etetet.loader/Code/Hotfix.dll.bytes,但真正打包时必须使用上面的代码
  87. //hotfixAssBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "ET.Hotfix.dll.bytes"));
  88. //hotfixPdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "ET.Hotfix.pdb.bytes"));
  89. //hotfixViewAssBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "ET.HotfixView.dll.bytes"));
  90. //hotfixViewPdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "ET.HotfixView.pdb.bytes"));
  91. hotfixAssembly = Assembly.Load(hotfixAssBytes, hotfixPdbBytes);
  92. hotfixViewAssembly = Assembly.Load(hotfixViewAssBytes, hotfixViewPdbBytes);
  93. }
  94. else
  95. {
  96. hotfixAssBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "ET.Hotfix.dll.bytes"));
  97. hotfixPdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "ET.Hotfix.pdb.bytes"));
  98. hotfixViewAssBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "ET.HotfixView.dll.bytes"));
  99. hotfixViewPdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "ET.HotfixView.pdb.bytes"));
  100. hotfixAssembly = Assembly.Load(hotfixAssBytes, hotfixPdbBytes);
  101. hotfixViewAssembly = Assembly.Load(hotfixViewAssBytes, hotfixViewPdbBytes);
  102. }
  103. return (hotfixAssembly, hotfixViewAssembly);
  104. }
  105. public void Reload()
  106. {
  107. (Assembly hotfixAssembly, Assembly hotfixViewAssembly) = this.LoadHotfix();
  108. CodeTypes codeTypes = World.Instance.AddSingleton<CodeTypes, Assembly[]>(new[]
  109. {
  110. typeof (World).Assembly, typeof (Init).Assembly, this.modelAssembly, this.modelViewAssembly, hotfixAssembly,
  111. hotfixViewAssembly
  112. });
  113. codeTypes.CodeProcess();
  114. Log.Info($"reload dll finish!");
  115. }
  116. }
  117. }