LoadBundleFileOperation.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace YooAsset
  5. {
  6. internal class LoadBundleFileOperation : AsyncOperationBase
  7. {
  8. private enum ESteps
  9. {
  10. None,
  11. LoadFile,
  12. Done,
  13. }
  14. private readonly ResourceManager _resourceManager;
  15. private readonly List<ProviderOperation> _providers = new List<ProviderOperation>(100);
  16. private readonly List<ProviderOperation> _removeList = new List<ProviderOperation>(100);
  17. private FSLoadBundleOperation _loadBundleOp;
  18. private ESteps _steps = ESteps.None;
  19. /// <summary>
  20. /// 资源包文件信息
  21. /// </summary>
  22. public BundleInfo LoadBundleInfo { private set; get; }
  23. /// <summary>
  24. /// 是否已经销毁
  25. /// </summary>
  26. public bool IsDestroyed { private set; get; } = false;
  27. /// <summary>
  28. /// 引用计数
  29. /// </summary>
  30. public int RefCount { private set; get; } = 0;
  31. /// <summary>
  32. /// 下载进度
  33. /// </summary>
  34. public float DownloadProgress { set; get; } = 0;
  35. /// <summary>
  36. /// 下载大小
  37. /// </summary>
  38. public long DownloadedBytes { set; get; } = 0;
  39. /// <summary>
  40. /// 加载结果
  41. /// </summary>
  42. public BundleResult Result { set; get; }
  43. internal LoadBundleFileOperation(ResourceManager resourceManager, BundleInfo bundleInfo)
  44. {
  45. _resourceManager = resourceManager;
  46. LoadBundleInfo = bundleInfo;
  47. }
  48. internal override void InternalStart()
  49. {
  50. _steps = ESteps.LoadFile;
  51. }
  52. internal override void InternalUpdate()
  53. {
  54. if (_steps == ESteps.None || _steps == ESteps.Done)
  55. return;
  56. if (_steps == ESteps.LoadFile)
  57. {
  58. if (_loadBundleOp == null)
  59. {
  60. _loadBundleOp = LoadBundleInfo.LoadBundleFile();
  61. _loadBundleOp.StartOperation();
  62. AddChildOperation(_loadBundleOp);
  63. }
  64. if (IsWaitForAsyncComplete)
  65. _loadBundleOp.WaitForAsyncComplete();
  66. _loadBundleOp.UpdateOperation();
  67. DownloadProgress = _loadBundleOp.DownloadProgress;
  68. DownloadedBytes = _loadBundleOp.DownloadedBytes;
  69. if (_loadBundleOp.IsDone == false)
  70. return;
  71. if (_loadBundleOp.Status == EOperationStatus.Succeed)
  72. {
  73. if (_loadBundleOp.Result == null)
  74. {
  75. _steps = ESteps.Done;
  76. Status = EOperationStatus.Failed;
  77. Error = $"The bundle loader result is null ! {LoadBundleInfo.Bundle.BundleName}";
  78. }
  79. else
  80. {
  81. _steps = ESteps.Done;
  82. Result = _loadBundleOp.Result;
  83. Status = EOperationStatus.Succeed;
  84. }
  85. }
  86. else
  87. {
  88. _steps = ESteps.Done;
  89. Status = EOperationStatus.Failed;
  90. Error = _loadBundleOp.Error;
  91. }
  92. }
  93. }
  94. internal override void InternalWaitForAsyncComplete()
  95. {
  96. while (true)
  97. {
  98. if (ExecuteWhileDone())
  99. {
  100. _steps = ESteps.Done;
  101. break;
  102. }
  103. }
  104. }
  105. internal override string InternalGetDesc()
  106. {
  107. return $"BundleName : {LoadBundleInfo.Bundle.BundleName}";
  108. }
  109. /// <summary>
  110. /// 引用(引用计数递加)
  111. /// </summary>
  112. public void Reference()
  113. {
  114. RefCount++;
  115. }
  116. /// <summary>
  117. /// 释放(引用计数递减)
  118. /// </summary>
  119. public void Release()
  120. {
  121. RefCount--;
  122. }
  123. /// <summary>
  124. /// 销毁
  125. /// </summary>
  126. public void DestroyLoader()
  127. {
  128. IsDestroyed = true;
  129. // Check fatal
  130. if (RefCount > 0)
  131. throw new Exception($"Bundle file loader ref is not zero : {LoadBundleInfo.Bundle.BundleName}");
  132. if (IsDone == false)
  133. throw new Exception($"Bundle file loader is not done : {LoadBundleInfo.Bundle.BundleName}");
  134. if (Result != null)
  135. Result.UnloadBundleFile();
  136. }
  137. /// <summary>
  138. /// 是否可以销毁
  139. /// </summary>
  140. public bool CanDestroyLoader()
  141. {
  142. if (IsDone == false)
  143. return false;
  144. if (RefCount > 0)
  145. return false;
  146. // YOOASSET_LEGACY_DEPENDENCY
  147. // 检查引用链上的资源包是否已经全部销毁
  148. // 注意:互相引用的资源包无法卸载!
  149. if (LoadBundleInfo.Bundle.ReferenceBundleIDs.Count > 0)
  150. {
  151. foreach (var bundleID in LoadBundleInfo.Bundle.ReferenceBundleIDs)
  152. {
  153. if (_resourceManager.CheckBundleDestroyed(bundleID) == false)
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. /// <summary>
  160. /// 添加附属的资源提供者
  161. /// </summary>
  162. public void AddProvider(ProviderOperation provider)
  163. {
  164. if (_providers.Contains(provider) == false)
  165. _providers.Add(provider);
  166. }
  167. /// <summary>
  168. /// 尝试销毁资源提供者
  169. /// </summary>
  170. public void TryDestroyProviders()
  171. {
  172. // 获取移除列表
  173. _removeList.Clear();
  174. foreach (var provider in _providers)
  175. {
  176. if (provider.CanDestroyProvider())
  177. {
  178. _removeList.Add(provider);
  179. }
  180. }
  181. // 销毁资源提供者
  182. foreach (var provider in _removeList)
  183. {
  184. _providers.Remove(provider);
  185. provider.DestroyProvider();
  186. }
  187. // 移除资源提供者
  188. if (_removeList.Count > 0)
  189. {
  190. _resourceManager.RemoveBundleProviders(_removeList);
  191. _removeList.Clear();
  192. }
  193. }
  194. }
  195. }