DownloaderOperation.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. namespace YooAsset
  4. {
  5. public abstract class DownloaderOperation : AsyncOperationBase
  6. {
  7. private enum ESteps
  8. {
  9. None,
  10. Check,
  11. Loading,
  12. Done,
  13. }
  14. private const int MAX_LOADER_COUNT = 64;
  15. public delegate void OnDownloadOver(bool isSucceed);
  16. public delegate void OnDownloadProgress(int totalDownloadCount, int currentDownloadCount, long totalDownloadBytes, long currentDownloadBytes);
  17. public delegate void OnDownloadError(string fileName, string error);
  18. public delegate void OnStartDownloadFile(string fileName, long sizeBytes);
  19. private readonly int _downloadingMaxNumber;
  20. private readonly int _failedTryAgain;
  21. private readonly int _timeout;
  22. private readonly List<BundleInfo> _downloadList;
  23. private readonly List<DownloaderBase> _downloaders = new List<DownloaderBase>(MAX_LOADER_COUNT);
  24. private readonly List<DownloaderBase> _removeList = new List<DownloaderBase>(MAX_LOADER_COUNT);
  25. private readonly List<DownloaderBase> _failedList = new List<DownloaderBase>(MAX_LOADER_COUNT);
  26. // 数据相关
  27. private bool _isPause = false;
  28. private long _lastDownloadBytes = 0;
  29. private int _lastDownloadCount = 0;
  30. private long _cachedDownloadBytes = 0;
  31. private int _cachedDownloadCount = 0;
  32. private ESteps _steps = ESteps.None;
  33. /// <summary>
  34. /// 统计的下载文件总数量
  35. /// </summary>
  36. public int TotalDownloadCount { private set; get; }
  37. /// <summary>
  38. /// 统计的下载文件的总大小
  39. /// </summary>
  40. public long TotalDownloadBytes { private set; get; }
  41. /// <summary>
  42. /// 当前已经完成的下载总数量
  43. /// </summary>
  44. public int CurrentDownloadCount
  45. {
  46. get { return _lastDownloadCount; }
  47. }
  48. /// <summary>
  49. /// 当前已经完成的下载总大小
  50. /// </summary>
  51. public long CurrentDownloadBytes
  52. {
  53. get { return _lastDownloadBytes; }
  54. }
  55. /// <summary>
  56. /// 当下载器结束(无论成功或失败)
  57. /// </summary>
  58. public OnDownloadOver OnDownloadOverCallback { set; get; }
  59. /// <summary>
  60. /// 当下载进度发生变化
  61. /// </summary>
  62. public OnDownloadProgress OnDownloadProgressCallback { set; get; }
  63. /// <summary>
  64. /// 当某个文件下载失败
  65. /// </summary>
  66. public OnDownloadError OnDownloadErrorCallback { set; get; }
  67. /// <summary>
  68. /// 当开始下载某个文件
  69. /// </summary>
  70. public OnStartDownloadFile OnStartDownloadFileCallback { set; get; }
  71. internal DownloaderOperation(List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
  72. {
  73. _downloadList = downloadList;
  74. _downloadingMaxNumber = UnityEngine.Mathf.Clamp(downloadingMaxNumber, 1, MAX_LOADER_COUNT); ;
  75. _failedTryAgain = failedTryAgain;
  76. _timeout = timeout;
  77. if (downloadList != null)
  78. {
  79. TotalDownloadCount = downloadList.Count;
  80. foreach (var packageBundle in downloadList)
  81. {
  82. TotalDownloadBytes += packageBundle.Bundle.FileSize;
  83. }
  84. }
  85. }
  86. internal override void Start()
  87. {
  88. YooLogger.Log($"Begine to download : {TotalDownloadCount} files and {TotalDownloadBytes} bytes");
  89. _steps = ESteps.Check;
  90. }
  91. internal override void Update()
  92. {
  93. if (_steps == ESteps.None || _steps == ESteps.Done)
  94. return;
  95. if (_steps == ESteps.Check)
  96. {
  97. if (_downloadList == null)
  98. {
  99. _steps = ESteps.Done;
  100. Status = EOperationStatus.Failed;
  101. Error = "Download list is null.";
  102. }
  103. else
  104. {
  105. _steps = ESteps.Loading;
  106. }
  107. }
  108. if (_steps == ESteps.Loading)
  109. {
  110. // 检测下载器结果
  111. _removeList.Clear();
  112. long downloadBytes = _cachedDownloadBytes;
  113. foreach (var downloader in _downloaders)
  114. {
  115. downloadBytes += (long)downloader.DownloadedBytes;
  116. if (downloader.IsDone() == false)
  117. continue;
  118. BundleInfo bundleInfo = downloader.GetBundleInfo();
  119. // 检测是否下载失败
  120. if (downloader.HasError())
  121. {
  122. _removeList.Add(downloader);
  123. _failedList.Add(downloader);
  124. continue;
  125. }
  126. // 下载成功
  127. _removeList.Add(downloader);
  128. _cachedDownloadCount++;
  129. _cachedDownloadBytes += bundleInfo.Bundle.FileSize;
  130. }
  131. // 移除已经完成的下载器(无论成功或失败)
  132. foreach (var loader in _removeList)
  133. {
  134. _downloaders.Remove(loader);
  135. }
  136. // 如果下载进度发生变化
  137. if (_lastDownloadBytes != downloadBytes || _lastDownloadCount != _cachedDownloadCount)
  138. {
  139. _lastDownloadBytes = downloadBytes;
  140. _lastDownloadCount = _cachedDownloadCount;
  141. Progress = (float)_lastDownloadBytes / TotalDownloadBytes;
  142. OnDownloadProgressCallback?.Invoke(TotalDownloadCount, _lastDownloadCount, TotalDownloadBytes, _lastDownloadBytes);
  143. }
  144. // 动态创建新的下载器到最大数量限制
  145. // 注意:如果期间有下载失败的文件,暂停动态创建下载器
  146. if (_downloadList.Count > 0 && _failedList.Count == 0)
  147. {
  148. if (_isPause)
  149. return;
  150. if (_downloaders.Count < _downloadingMaxNumber)
  151. {
  152. int index = _downloadList.Count - 1;
  153. var bundleInfo = _downloadList[index];
  154. var operation = DownloadSystem.BeginDownload(bundleInfo, _failedTryAgain, _timeout);
  155. _downloaders.Add(operation);
  156. _downloadList.RemoveAt(index);
  157. OnStartDownloadFileCallback?.Invoke(bundleInfo.Bundle.BundleName, bundleInfo.Bundle.FileSize);
  158. }
  159. }
  160. // 下载结算
  161. if (_downloaders.Count == 0)
  162. {
  163. if (_failedList.Count > 0)
  164. {
  165. var failedDownloader = _failedList[0];
  166. string fileName = failedDownloader.GetBundleInfo().Bundle.BundleName;
  167. _steps = ESteps.Done;
  168. Status = EOperationStatus.Failed;
  169. Error = $"Failed to download file : {fileName}";
  170. OnDownloadErrorCallback?.Invoke(fileName, failedDownloader.GetLastError());
  171. OnDownloadOverCallback?.Invoke(false);
  172. }
  173. else
  174. {
  175. // 结算成功
  176. _steps = ESteps.Done;
  177. Status = EOperationStatus.Succeed;
  178. OnDownloadOverCallback?.Invoke(true);
  179. }
  180. }
  181. }
  182. }
  183. /// <summary>
  184. /// 开始下载
  185. /// </summary>
  186. public void BeginDownload()
  187. {
  188. if (_steps == ESteps.None)
  189. {
  190. OperationSystem.StartOperation(this);
  191. }
  192. }
  193. /// <summary>
  194. /// 暂停下载
  195. /// </summary>
  196. public void PauseDownload()
  197. {
  198. _isPause = true;
  199. }
  200. /// <summary>
  201. /// 恢复下载
  202. /// </summary>
  203. public void ResumeDownload()
  204. {
  205. _isPause = false;
  206. }
  207. /// <summary>
  208. /// 取消下载
  209. /// </summary>
  210. public void CancelDownload()
  211. {
  212. if (_steps != ESteps.Done)
  213. {
  214. _steps = ESteps.Done;
  215. Status = EOperationStatus.Failed;
  216. Error = "User cancel.";
  217. }
  218. }
  219. }
  220. public sealed class ResourceDownloaderOperation : DownloaderOperation
  221. {
  222. internal ResourceDownloaderOperation(List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
  223. : base(downloadList, downloadingMaxNumber, failedTryAgain, timeout)
  224. {
  225. }
  226. /// <summary>
  227. /// 创建空的下载器
  228. /// </summary>
  229. internal static ResourceDownloaderOperation CreateEmptyDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout)
  230. {
  231. List<BundleInfo> downloadList = new List<BundleInfo>();
  232. var operation = new ResourceDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  233. return operation;
  234. }
  235. }
  236. public sealed class ResourceUnpackerOperation : DownloaderOperation
  237. {
  238. internal ResourceUnpackerOperation(List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
  239. : base(downloadList, downloadingMaxNumber, failedTryAgain, timeout)
  240. {
  241. }
  242. /// <summary>
  243. /// 创建空的解压器
  244. /// </summary>
  245. internal static ResourceUnpackerOperation CreateEmptyUnpacker(int upackingMaxNumber, int failedTryAgain, int timeout)
  246. {
  247. List<BundleInfo> downloadList = new List<BundleInfo>();
  248. var operation = new ResourceUnpackerOperation(downloadList, upackingMaxNumber, failedTryAgain, int.MaxValue);
  249. return operation;
  250. }
  251. }
  252. }