PreDownloadContentOperation.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace YooAsset
  5. {
  6. public abstract class PreDownloadContentOperation : AsyncOperationBase
  7. {
  8. /// <summary>
  9. /// 创建资源下载器,用于下载当前资源版本所有的资源包文件
  10. /// </summary>
  11. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  12. /// <param name="failedTryAgain">下载失败的重试次数</param>
  13. /// <param name="timeout">超时时间</param>
  14. public virtual ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  15. {
  16. return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
  17. }
  18. /// <summary>
  19. /// 创建资源下载器,用于下载指定的资源标签关联的资源包文件
  20. /// </summary>
  21. /// <param name="tag">资源标签</param>
  22. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  23. /// <param name="failedTryAgain">下载失败的重试次数</param>
  24. /// <param name="timeout">超时时间</param>
  25. public virtual ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  26. {
  27. return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
  28. }
  29. /// <summary>
  30. /// 创建资源下载器,用于下载指定的资源标签列表关联的资源包文件
  31. /// </summary>
  32. /// <param name="tags">资源标签列表</param>
  33. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  34. /// <param name="failedTryAgain">下载失败的重试次数</param>
  35. /// <param name="timeout">超时时间</param>
  36. public virtual ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  37. {
  38. return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
  39. }
  40. /// <summary>
  41. /// 创建资源下载器,用于下载指定的资源依赖的资源包文件
  42. /// </summary>
  43. /// <param name="location">资源定位地址</param>
  44. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  45. /// <param name="failedTryAgain">下载失败的重试次数</param>
  46. /// <param name="timeout">超时时间</param>
  47. public virtual ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  48. {
  49. return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
  50. }
  51. /// <summary>
  52. /// 创建资源下载器,用于下载指定的资源列表依赖的资源包文件
  53. /// </summary>
  54. /// <param name="locations">资源定位地址列表</param>
  55. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  56. /// <param name="failedTryAgain">下载失败的重试次数</param>
  57. /// <param name="timeout">超时时间</param>
  58. public virtual ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  59. {
  60. return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
  61. }
  62. }
  63. internal class EditorPlayModePreDownloadContentOperation : PreDownloadContentOperation
  64. {
  65. internal override void Start()
  66. {
  67. Status = EOperationStatus.Succeed;
  68. }
  69. internal override void Update()
  70. {
  71. }
  72. }
  73. internal class OfflinePlayModePreDownloadContentOperation : PreDownloadContentOperation
  74. {
  75. internal override void Start()
  76. {
  77. Status = EOperationStatus.Succeed;
  78. }
  79. internal override void Update()
  80. {
  81. }
  82. }
  83. internal class HostPlayModePreDownloadContentOperation : PreDownloadContentOperation
  84. {
  85. private enum ESteps
  86. {
  87. None,
  88. CheckActiveManifest,
  89. TryLoadCacheManifest,
  90. DownloadManifest,
  91. LoadCacheManifest,
  92. CheckDeserializeManifest,
  93. Done,
  94. }
  95. private readonly HostPlayModeImpl _impl;
  96. private readonly string _packageName;
  97. private readonly string _packageVersion;
  98. private readonly int _timeout;
  99. private LoadCacheManifestOperation _tryLoadCacheManifestOp;
  100. private LoadCacheManifestOperation _loadCacheManifestOp;
  101. private DownloadManifestOperation _downloadManifestOp;
  102. private PackageManifest _manifest;
  103. private ESteps _steps = ESteps.None;
  104. internal HostPlayModePreDownloadContentOperation(HostPlayModeImpl impl, string packageName, string packageVersion, int timeout)
  105. {
  106. _impl = impl;
  107. _packageName = packageName;
  108. _packageVersion = packageVersion;
  109. _timeout = timeout;
  110. }
  111. internal override void Start()
  112. {
  113. _steps = ESteps.CheckActiveManifest;
  114. }
  115. internal override void Update()
  116. {
  117. if (_steps == ESteps.None || _steps == ESteps.Done)
  118. return;
  119. if (_steps == ESteps.CheckActiveManifest)
  120. {
  121. // 检测当前激活的清单对象
  122. if (_impl.ActiveManifest != null)
  123. {
  124. if (_impl.ActiveManifest.PackageVersion == _packageVersion)
  125. {
  126. _manifest = _impl.ActiveManifest;
  127. _steps = ESteps.Done;
  128. Status = EOperationStatus.Succeed;
  129. return;
  130. }
  131. }
  132. _steps = ESteps.TryLoadCacheManifest;
  133. }
  134. if (_steps == ESteps.TryLoadCacheManifest)
  135. {
  136. if (_tryLoadCacheManifestOp == null)
  137. {
  138. _tryLoadCacheManifestOp = new LoadCacheManifestOperation(_packageName, _packageVersion);
  139. OperationSystem.StartOperation(_tryLoadCacheManifestOp);
  140. }
  141. if (_tryLoadCacheManifestOp.IsDone == false)
  142. return;
  143. if (_tryLoadCacheManifestOp.Status == EOperationStatus.Succeed)
  144. {
  145. _manifest = _tryLoadCacheManifestOp.Manifest;
  146. _steps = ESteps.Done;
  147. Status = EOperationStatus.Succeed;
  148. }
  149. else
  150. {
  151. _steps = ESteps.DownloadManifest;
  152. }
  153. }
  154. if (_steps == ESteps.DownloadManifest)
  155. {
  156. if (_downloadManifestOp == null)
  157. {
  158. _downloadManifestOp = new DownloadManifestOperation(_impl.RemoteServices, _packageName, _packageVersion, _timeout);
  159. OperationSystem.StartOperation(_downloadManifestOp);
  160. }
  161. if (_downloadManifestOp.IsDone == false)
  162. return;
  163. if (_downloadManifestOp.Status == EOperationStatus.Succeed)
  164. {
  165. _steps = ESteps.LoadCacheManifest;
  166. }
  167. else
  168. {
  169. _steps = ESteps.Done;
  170. Status = EOperationStatus.Failed;
  171. Error = _downloadManifestOp.Error;
  172. }
  173. }
  174. if (_steps == ESteps.LoadCacheManifest)
  175. {
  176. if (_loadCacheManifestOp == null)
  177. {
  178. _loadCacheManifestOp = new LoadCacheManifestOperation(_packageName, _packageVersion);
  179. OperationSystem.StartOperation(_loadCacheManifestOp);
  180. }
  181. if (_loadCacheManifestOp.IsDone == false)
  182. return;
  183. if (_loadCacheManifestOp.Status == EOperationStatus.Succeed)
  184. {
  185. _manifest = _loadCacheManifestOp.Manifest;
  186. _steps = ESteps.Done;
  187. Status = EOperationStatus.Succeed;
  188. }
  189. else
  190. {
  191. _steps = ESteps.Done;
  192. Status = EOperationStatus.Failed;
  193. Error = _loadCacheManifestOp.Error;
  194. }
  195. }
  196. }
  197. public override ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  198. {
  199. if (Status != EOperationStatus.Succeed)
  200. {
  201. YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !");
  202. return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
  203. }
  204. List<BundleInfo> downloadList = _impl.GetDownloadListByAll(_manifest);
  205. var operation = new ResourceDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  206. return operation;
  207. }
  208. public override ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  209. {
  210. if (Status != EOperationStatus.Succeed)
  211. {
  212. YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !");
  213. return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
  214. }
  215. List<BundleInfo> downloadList = _impl.GetDownloadListByTags(_manifest, new string[] { tag });
  216. var operation = new ResourceDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  217. return operation;
  218. }
  219. public override ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  220. {
  221. if (Status != EOperationStatus.Succeed)
  222. {
  223. YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !");
  224. return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
  225. }
  226. List<BundleInfo> downloadList = _impl.GetDownloadListByTags(_manifest, tags);
  227. var operation = new ResourceDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  228. return operation;
  229. }
  230. public override ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  231. {
  232. if (Status != EOperationStatus.Succeed)
  233. {
  234. YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !");
  235. return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
  236. }
  237. List<AssetInfo> assetInfos = new List<AssetInfo>();
  238. var assetInfo = _manifest.ConvertLocationToAssetInfo(location, null);
  239. assetInfos.Add(assetInfo);
  240. List<BundleInfo> downloadList = _impl.GetDownloadListByPaths(_manifest, assetInfos.ToArray());
  241. var operation = new ResourceDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  242. return operation;
  243. }
  244. public override ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  245. {
  246. if (Status != EOperationStatus.Succeed)
  247. {
  248. YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !");
  249. return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
  250. }
  251. List<AssetInfo> assetInfos = new List<AssetInfo>(locations.Length);
  252. foreach (var location in locations)
  253. {
  254. var assetInfo = _manifest.ConvertLocationToAssetInfo(location, null);
  255. assetInfos.Add(assetInfo);
  256. }
  257. List<BundleInfo> downloadList = _impl.GetDownloadListByPaths(_manifest, assetInfos.ToArray());
  258. var operation = new ResourceDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  259. return operation;
  260. }
  261. }
  262. }