ProviderOperation.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. namespace YooAsset
  6. {
  7. internal abstract class ProviderOperation : AsyncOperationBase
  8. {
  9. protected enum ESteps
  10. {
  11. None = 0,
  12. StartBundleLoader,
  13. WaitBundleLoader,
  14. ProcessBundleResult,
  15. Done,
  16. }
  17. /// <summary>
  18. /// 资源提供者唯一标识符
  19. /// </summary>
  20. public string ProviderGUID { private set; get; }
  21. /// <summary>
  22. /// 资源信息
  23. /// </summary>
  24. public AssetInfo MainAssetInfo { private set; get; }
  25. /// <summary>
  26. /// 获取的资源对象
  27. /// </summary>
  28. public UnityEngine.Object AssetObject { protected set; get; }
  29. /// <summary>
  30. /// 获取的资源对象集合
  31. /// </summary>
  32. public UnityEngine.Object[] AllAssetObjects { protected set; get; }
  33. /// <summary>
  34. /// 获取的资源对象集合
  35. /// </summary>
  36. public UnityEngine.Object[] SubAssetObjects { protected set; get; }
  37. /// <summary>
  38. /// 获取的场景对象
  39. /// </summary>
  40. public UnityEngine.SceneManagement.Scene SceneObject { protected set; get; }
  41. /// <summary>
  42. /// 获取的资源包对象
  43. /// </summary>
  44. public BundleResult BundleResultObject { protected set; get; }
  45. /// <summary>
  46. /// 加载的场景名称
  47. /// </summary>
  48. public string SceneName { protected set; get; }
  49. /// <summary>
  50. /// 引用计数
  51. /// </summary>
  52. public int RefCount { private set; get; } = 0;
  53. /// <summary>
  54. /// 是否已经销毁
  55. /// </summary>
  56. public bool IsDestroyed { private set; get; } = false;
  57. private ESteps _steps = ESteps.None;
  58. private readonly LoadBundleFileOperation _mainBundleLoader;
  59. private readonly List<LoadBundleFileOperation> _bundleLoaders = new List<LoadBundleFileOperation>(10);
  60. private readonly HashSet<HandleBase> _handles = new HashSet<HandleBase>();
  61. public ProviderOperation(ResourceManager manager, string providerGUID, AssetInfo assetInfo)
  62. {
  63. ProviderGUID = providerGUID;
  64. MainAssetInfo = assetInfo;
  65. if (string.IsNullOrEmpty(providerGUID) == false)
  66. {
  67. // 主资源包加载器
  68. _mainBundleLoader = manager.CreateMainBundleFileLoader(assetInfo);
  69. _mainBundleLoader.AddProvider(this);
  70. _bundleLoaders.Add(_mainBundleLoader);
  71. // 依赖资源包加载器集合
  72. var dependLoaders = manager.CreateDependBundleFileLoaders(assetInfo);
  73. if (dependLoaders.Count > 0)
  74. _bundleLoaders.AddRange(dependLoaders);
  75. // 增加引用计数
  76. foreach (var bundleLoader in _bundleLoaders)
  77. {
  78. bundleLoader.Reference();
  79. }
  80. }
  81. }
  82. internal override void InternalStart()
  83. {
  84. _steps = ESteps.StartBundleLoader;
  85. }
  86. internal override void InternalUpdate()
  87. {
  88. if (_steps == ESteps.None || _steps == ESteps.Done)
  89. return;
  90. if (_steps == ESteps.StartBundleLoader)
  91. {
  92. foreach (var bundleLoader in _bundleLoaders)
  93. {
  94. bundleLoader.StartOperation();
  95. AddChildOperation(bundleLoader);
  96. }
  97. _steps = ESteps.WaitBundleLoader;
  98. }
  99. if (_steps == ESteps.WaitBundleLoader)
  100. {
  101. if (IsWaitForAsyncComplete)
  102. {
  103. foreach (var bundleLoader in _bundleLoaders)
  104. {
  105. bundleLoader.WaitForAsyncComplete();
  106. }
  107. }
  108. // 更新资源包加载器
  109. foreach (var bundleLoader in _bundleLoaders)
  110. {
  111. bundleLoader.UpdateOperation();
  112. }
  113. // 检测加载是否完成
  114. foreach (var bundleLoader in _bundleLoaders)
  115. {
  116. if (bundleLoader.IsDone == false)
  117. return;
  118. if (bundleLoader.Status != EOperationStatus.Succeed)
  119. {
  120. InvokeCompletion(bundleLoader.Error, EOperationStatus.Failed);
  121. return;
  122. }
  123. }
  124. // 检测加载结果
  125. BundleResultObject = _mainBundleLoader.Result;
  126. if (BundleResultObject == null)
  127. {
  128. string error = $"Loaded bundle result is null !";
  129. InvokeCompletion(error, EOperationStatus.Failed);
  130. return;
  131. }
  132. _steps = ESteps.ProcessBundleResult;
  133. }
  134. if (_steps == ESteps.ProcessBundleResult)
  135. {
  136. ProcessBundleResult();
  137. }
  138. }
  139. internal override void InternalWaitForAsyncComplete()
  140. {
  141. while (true)
  142. {
  143. if (ExecuteWhileDone())
  144. {
  145. _steps = ESteps.Done;
  146. break;
  147. }
  148. }
  149. }
  150. internal override string InternalGetDesc()
  151. {
  152. return $"AssetPath : {MainAssetInfo.AssetPath}";
  153. }
  154. protected abstract void ProcessBundleResult();
  155. /// <summary>
  156. /// 销毁资源提供者
  157. /// </summary>
  158. public void DestroyProvider()
  159. {
  160. IsDestroyed = true;
  161. // 检测是否为正常销毁
  162. if (IsDone == false)
  163. {
  164. Error = "User abort !";
  165. Status = EOperationStatus.Failed;
  166. }
  167. // 减少引用计数
  168. foreach (var bundleLoader in _bundleLoaders)
  169. {
  170. bundleLoader.Release();
  171. }
  172. }
  173. /// <summary>
  174. /// 是否可以销毁
  175. /// </summary>
  176. public bool CanDestroyProvider()
  177. {
  178. // 注意:在进行资源加载过程时不可以销毁
  179. if (_steps == ESteps.ProcessBundleResult)
  180. return false;
  181. return RefCount <= 0;
  182. }
  183. /// <summary>
  184. /// 创建资源句柄
  185. /// </summary>
  186. public T CreateHandle<T>() where T : HandleBase
  187. {
  188. // 引用计数增加
  189. RefCount++;
  190. HandleBase handle = HandleFactory.CreateHandle(this, typeof(T));
  191. _handles.Add(handle);
  192. return handle as T;
  193. }
  194. /// <summary>
  195. /// 释放资源句柄
  196. /// </summary>
  197. public void ReleaseHandle(HandleBase handle)
  198. {
  199. if (RefCount <= 0)
  200. throw new System.Exception("Should never get here !");
  201. if (_handles.Remove(handle) == false)
  202. throw new System.Exception("Should never get here !");
  203. // 引用计数减少
  204. RefCount--;
  205. }
  206. /// <summary>
  207. /// 释放所有资源句柄
  208. /// </summary>
  209. public void ReleaseAllHandles()
  210. {
  211. List<HandleBase> tempers = _handles.ToList();
  212. foreach (var handle in tempers)
  213. {
  214. handle.Release();
  215. }
  216. }
  217. /// <summary>
  218. /// 结束流程
  219. /// </summary>
  220. protected void InvokeCompletion(string error, EOperationStatus status)
  221. {
  222. _steps = ESteps.Done;
  223. Error = error;
  224. Status = status;
  225. // 注意:创建临时列表是为了防止外部逻辑在回调函数内创建或者释放资源句柄。
  226. // 注意:回调方法如果发生异常,会阻断列表里的后续回调方法!
  227. List<HandleBase> tempers = _handles.ToList();
  228. foreach (var hande in tempers)
  229. {
  230. if (hande.IsValid)
  231. {
  232. hande.InvokeCallback();
  233. }
  234. }
  235. }
  236. /// <summary>
  237. /// 获取下载报告
  238. /// </summary>
  239. public DownloadStatus GetDownloadStatus()
  240. {
  241. DownloadStatus status = new DownloadStatus();
  242. foreach (var bundleLoader in _bundleLoaders)
  243. {
  244. status.TotalBytes += bundleLoader.LoadBundleInfo.Bundle.FileSize;
  245. status.DownloadedBytes += bundleLoader.DownloadedBytes;
  246. }
  247. if (status.TotalBytes == 0)
  248. throw new System.Exception("Should never get here !");
  249. status.IsDone = status.DownloadedBytes == status.TotalBytes;
  250. status.Progress = (float)status.DownloadedBytes / status.TotalBytes;
  251. return status;
  252. }
  253. #region 调试信息
  254. /// <summary>
  255. /// 出生的场景
  256. /// </summary>
  257. public string SpawnScene = string.Empty;
  258. [Conditional("DEBUG")]
  259. public void InitProviderDebugInfo()
  260. {
  261. SpawnScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
  262. }
  263. /// <summary>
  264. /// 获取资源包的调试信息列表
  265. /// </summary>
  266. internal List<string> GetDebugDependBundles()
  267. {
  268. List<string> result = new List<string>(_bundleLoaders.Count);
  269. foreach (var bundleLoader in _bundleLoaders)
  270. {
  271. var packageBundle = bundleLoader.LoadBundleInfo.Bundle;
  272. result.Add(packageBundle.BundleName);
  273. }
  274. return result;
  275. }
  276. #endregion
  277. }
  278. }