CodeLoader.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using Cysharp.Threading.Tasks;
  2. using HybridCLR;
  3. using Sirenix.Utilities;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Reflection;
  9. using UnityEngine;
  10. using YooAsset;
  11. namespace ET
  12. {
  13. public class CodeLoader: Singleton<CodeLoader>
  14. {
  15. public void Start()
  16. {
  17. AsyncLoadResAndCode().Forget();
  18. }
  19. private async UniTaskVoid AsyncLoadResAndCode()
  20. {
  21. YooAssets.EPlayMode playMode;
  22. if (Define.EnableCodes)
  23. {
  24. playMode = YooAssets.EPlayMode.EditorSimulateMode;
  25. }
  26. else
  27. {
  28. GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
  29. playMode = globalConfig.PlayMode;
  30. }
  31. Log.Info($"start mode: {playMode}");
  32. // 启动YooAsset引擎
  33. await YooAssetProxy.StartYooAssetEngine(playMode);
  34. // Shader Warm Up
  35. ShaderVariantCollection shaderVariantCollection =
  36. (await YooAssetProxy.LoadAssetAsync<ShaderVariantCollection>(
  37. YooAssetProxy.GetYooAssetFormatResPath("ProjectSShaderVariant",
  38. YooAssetProxy.YooAssetResType.Shader)))
  39. .GetAssetObject<ShaderVariantCollection>();
  40. Stopwatch stopwatch = Stopwatch.StartNew();
  41. shaderVariantCollection.WarmUp();
  42. stopwatch.Stop();
  43. Log.Info($"Shader Warm Up完成, 耗时: {stopwatch.ElapsedMilliseconds}ms,shaderCount: {shaderVariantCollection.shaderCount} variantCount: {shaderVariantCollection.variantCount}");
  44. //加载代码入口
  45. if (Define.EnableCodes)
  46. {
  47. LoadLocalCode();
  48. }
  49. else
  50. {
  51. await LoadMetadataForAOT();
  52. await LoadHotfixCode();
  53. }
  54. }
  55. // 加载assembly对应的dll,会自动为它hook。一旦aot泛型函数的native函数不存在,用解释器版本代码
  56. private async UniTask LoadMetadataForAOT()
  57. {
  58. //AOT dll列表的配置也可以通过热更新下下来,这样增加了新的泛型就不怕了
  59. ConfigStringList hotUpdateAOTDlls =
  60. (await YooAssetProxy.LoadAssetAsync<ConfigStringList>("HotUpdateAOTDlls"))
  61. .GetAssetObject<ConfigStringList>();
  62. List<UniTask<RawFileOperation>> tasks = new List<UniTask<RawFileOperation>>();
  63. foreach (var aotDll in hotUpdateAOTDlls.List)
  64. {
  65. tasks.Add(YooAssetProxy.GetRawFileAsync(aotDll));
  66. }
  67. RawFileOperation[] rawFileOperations = await UniTask.WhenAll(tasks);
  68. foreach (var task in rawFileOperations)
  69. {
  70. var ret = RuntimeApi.LoadMetadataForAOTAssembly(task.GetRawBytes(), HomologousImageMode.SuperSet);
  71. if (ret != LoadImageErrorCode.OK)
  72. {
  73. Log.Error($"Error occurs when loadMetadataForAOT: {ret}");
  74. }
  75. }
  76. }
  77. //热更加载 model.dll & hotfix.dll
  78. private async UniTask LoadHotfixCode()
  79. {
  80. Dictionary<string, Type> typesInAssembly = new Dictionary<string, Type>();
  81. Assembly modelAss = null;
  82. #if ! UNITY_EDITOR
  83. var battleDlls = (await YooAssetProxy.LoadAssetAsync<ConfigStringList>("HotupdateBattleDlls")).GetAssetObject<ConfigStringList>();
  84. foreach (var dll in battleDlls.List)
  85. {
  86. Log.Info($"to load: {dll}");
  87. Assembly.Load((await YooAssetProxy.GetRawFileAsync(dll)).GetRawBytes());
  88. }
  89. #endif
  90. var hotDlls = (await YooAssetProxy.LoadAssetAsync<ConfigKeyValueMap>("HotupdateDlls")).GetAssetObject<ConfigKeyValueMap>();
  91. foreach(var kv in hotDlls.keyValueMaps)
  92. {
  93. Assembly ass;
  94. if(kv.Value.IsNullOrWhitespace())
  95. {
  96. Log.Info($"to load: {kv.Key}");
  97. ass = Assembly.Load((await YooAssetProxy.GetRawFileAsync(kv.Key)).GetRawBytes());
  98. }
  99. else
  100. {
  101. Log.Info($"to load: {kv.Key}");
  102. ass = Assembly.Load((await YooAssetProxy.GetRawFileAsync(kv.Key)).GetRawBytes(),
  103. (await YooAssetProxy.GetRawFileAsync(kv.Value)).GetRawBytes());
  104. }
  105. foreach (Type type in ass.GetTypes())
  106. {
  107. typesInAssembly[type.FullName] = type;
  108. }
  109. if(kv.Key.Equals("Model.dll"))
  110. {
  111. modelAss = ass;
  112. }
  113. }
  114. EventSystem.Instance.Add(typesInAssembly);
  115. if(modelAss == null)
  116. {
  117. Log.Error("Not Found 'Model.dll'! ");
  118. return;
  119. }
  120. Log.Info("to call Client.Entry.");
  121. IStaticMethod start = new MonoStaticMethod(modelAss, "ET.Entry", "Start");
  122. start.Run();
  123. }
  124. //不使用dll,打开了EnableCode,使用Editor编辑模式
  125. private void LoadLocalCode()
  126. {
  127. //GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
  128. //if (globalConfig.CodeMode != CodeMode.ClientServer)
  129. //{
  130. // throw new Exception("ENABLE_CODES mode must use ClientServer code mode!");
  131. //}
  132. Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  133. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(assemblies);
  134. EventSystem.Instance.Add(types);
  135. bool bStart = false;
  136. foreach (Assembly ass in assemblies)
  137. {
  138. string name = ass.GetName().Name;
  139. if (name == "Unity.Model.Codes")
  140. {
  141. bStart = true;
  142. IStaticMethod start = new StaticMethod(ass, "ET.Entry", "Start");
  143. start.Run();
  144. break;
  145. }
  146. }
  147. if (!bStart)
  148. {
  149. throw new Exception("not found Assembly: 'Unity.Model.Codes'");
  150. }
  151. }
  152. //编译时会检查这个方法,需保留
  153. public void LoadHotfix()
  154. {
  155. }
  156. }
  157. }