PreDownloadContentOperation.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 abstract ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60);
  15. /// <summary>
  16. /// 创建资源下载器,用于下载指定的资源标签关联的资源包文件
  17. /// </summary>
  18. /// <param name="tag">资源标签</param>
  19. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  20. /// <param name="failedTryAgain">下载失败的重试次数</param>
  21. /// <param name="timeout">超时时间</param>
  22. public abstract ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60);
  23. /// <summary>
  24. /// 创建资源下载器,用于下载指定的资源标签列表关联的资源包文件
  25. /// </summary>
  26. /// <param name="tags">资源标签列表</param>
  27. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  28. /// <param name="failedTryAgain">下载失败的重试次数</param>
  29. /// <param name="timeout">超时时间</param>
  30. public abstract ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60);
  31. /// <summary>
  32. /// 创建资源下载器,用于下载指定的资源依赖的资源包文件
  33. /// </summary>
  34. /// <param name="location">资源定位地址</param>
  35. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  36. /// <param name="failedTryAgain">下载失败的重试次数</param>
  37. /// <param name="timeout">超时时间</param>
  38. public abstract ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60);
  39. /// <summary>
  40. /// 创建资源下载器,用于下载指定的资源列表依赖的资源包文件
  41. /// </summary>
  42. /// <param name="locations">资源定位地址列表</param>
  43. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  44. /// <param name="failedTryAgain">下载失败的重试次数</param>
  45. /// <param name="timeout">超时时间</param>
  46. public abstract ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60);
  47. }
  48. internal class EditorPlayModePreDownloadContentOperation : PreDownloadContentOperation
  49. {
  50. private readonly EditorSimulateModeImpl _impl;
  51. public EditorPlayModePreDownloadContentOperation(EditorSimulateModeImpl impl)
  52. {
  53. _impl = impl;
  54. }
  55. internal override void InternalOnStart()
  56. {
  57. Status = EOperationStatus.Succeed;
  58. }
  59. internal override void InternalOnUpdate()
  60. {
  61. }
  62. public override ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  63. {
  64. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  65. }
  66. public override ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  67. {
  68. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  69. }
  70. public override ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  71. {
  72. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  73. }
  74. public override ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  75. {
  76. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  77. }
  78. public override ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  79. {
  80. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  81. }
  82. }
  83. internal class OfflinePlayModePreDownloadContentOperation : PreDownloadContentOperation
  84. {
  85. private readonly OfflinePlayModeImpl _impl;
  86. public OfflinePlayModePreDownloadContentOperation(OfflinePlayModeImpl impl)
  87. {
  88. _impl = impl;
  89. }
  90. internal override void InternalOnStart()
  91. {
  92. Status = EOperationStatus.Succeed;
  93. }
  94. internal override void InternalOnUpdate()
  95. {
  96. }
  97. public override ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  98. {
  99. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  100. }
  101. public override ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  102. {
  103. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  104. }
  105. public override ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  106. {
  107. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  108. }
  109. public override ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  110. {
  111. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  112. }
  113. public override ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  114. {
  115. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  116. }
  117. }
  118. internal class HostPlayModePreDownloadContentOperation : PreDownloadContentOperation
  119. {
  120. private enum ESteps
  121. {
  122. None,
  123. CheckActiveManifest,
  124. TryLoadCacheManifest,
  125. DownloadManifest,
  126. LoadCacheManifest,
  127. CheckDeserializeManifest,
  128. Done,
  129. }
  130. private readonly HostPlayModeImpl _impl;
  131. private readonly string _packageVersion;
  132. private readonly int _timeout;
  133. private LoadCacheManifestOperation _tryLoadCacheManifestOp;
  134. private LoadCacheManifestOperation _loadCacheManifestOp;
  135. private DownloadManifestOperation _downloadManifestOp;
  136. private PackageManifest _manifest;
  137. private ESteps _steps = ESteps.None;
  138. internal HostPlayModePreDownloadContentOperation(HostPlayModeImpl impl, string packageVersion, int timeout)
  139. {
  140. _impl = impl;
  141. _packageVersion = packageVersion;
  142. _timeout = timeout;
  143. }
  144. internal override void InternalOnStart()
  145. {
  146. _steps = ESteps.CheckActiveManifest;
  147. }
  148. internal override void InternalOnUpdate()
  149. {
  150. if (_steps == ESteps.None || _steps == ESteps.Done)
  151. return;
  152. if (_steps == ESteps.CheckActiveManifest)
  153. {
  154. // 检测当前激活的清单对象
  155. if (_impl.ActiveManifest != null)
  156. {
  157. if (_impl.ActiveManifest.PackageVersion == _packageVersion)
  158. {
  159. _manifest = _impl.ActiveManifest;
  160. _steps = ESteps.Done;
  161. Status = EOperationStatus.Succeed;
  162. return;
  163. }
  164. }
  165. _steps = ESteps.TryLoadCacheManifest;
  166. }
  167. if (_steps == ESteps.TryLoadCacheManifest)
  168. {
  169. if (_tryLoadCacheManifestOp == null)
  170. {
  171. _tryLoadCacheManifestOp = new LoadCacheManifestOperation(_impl.Persistent, _packageVersion);
  172. OperationSystem.StartOperation(_impl.PackageName, _tryLoadCacheManifestOp);
  173. }
  174. if (_tryLoadCacheManifestOp.IsDone == false)
  175. return;
  176. if (_tryLoadCacheManifestOp.Status == EOperationStatus.Succeed)
  177. {
  178. _manifest = _tryLoadCacheManifestOp.Manifest;
  179. _steps = ESteps.Done;
  180. Status = EOperationStatus.Succeed;
  181. }
  182. else
  183. {
  184. _steps = ESteps.DownloadManifest;
  185. }
  186. }
  187. if (_steps == ESteps.DownloadManifest)
  188. {
  189. if (_downloadManifestOp == null)
  190. {
  191. _downloadManifestOp = new DownloadManifestOperation(_impl.Persistent, _impl.RemoteServices, _packageVersion, _timeout);
  192. OperationSystem.StartOperation(_impl.PackageName, _downloadManifestOp);
  193. }
  194. if (_downloadManifestOp.IsDone == false)
  195. return;
  196. if (_downloadManifestOp.Status == EOperationStatus.Succeed)
  197. {
  198. _steps = ESteps.LoadCacheManifest;
  199. }
  200. else
  201. {
  202. _steps = ESteps.Done;
  203. Status = EOperationStatus.Failed;
  204. Error = _downloadManifestOp.Error;
  205. }
  206. }
  207. if (_steps == ESteps.LoadCacheManifest)
  208. {
  209. if (_loadCacheManifestOp == null)
  210. {
  211. _loadCacheManifestOp = new LoadCacheManifestOperation(_impl.Persistent, _packageVersion);
  212. OperationSystem.StartOperation(_impl.PackageName, _loadCacheManifestOp);
  213. }
  214. if (_loadCacheManifestOp.IsDone == false)
  215. return;
  216. if (_loadCacheManifestOp.Status == EOperationStatus.Succeed)
  217. {
  218. _manifest = _loadCacheManifestOp.Manifest;
  219. _steps = ESteps.Done;
  220. Status = EOperationStatus.Succeed;
  221. }
  222. else
  223. {
  224. _steps = ESteps.Done;
  225. Status = EOperationStatus.Failed;
  226. Error = _loadCacheManifestOp.Error;
  227. }
  228. }
  229. }
  230. public override ResourceDownloaderOperation CreateResourceDownloader(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(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  236. }
  237. List<BundleInfo> downloadList = _impl.GetDownloadListByAll(_manifest);
  238. var operation = new ResourceDownloaderOperation(_impl.Download, _impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  239. return operation;
  240. }
  241. public override ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  242. {
  243. if (Status != EOperationStatus.Succeed)
  244. {
  245. YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !");
  246. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  247. }
  248. List<BundleInfo> downloadList = _impl.GetDownloadListByTags(_manifest, new string[] { tag });
  249. var operation = new ResourceDownloaderOperation(_impl.Download, _impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  250. return operation;
  251. }
  252. public override ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  253. {
  254. if (Status != EOperationStatus.Succeed)
  255. {
  256. YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !");
  257. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  258. }
  259. List<BundleInfo> downloadList = _impl.GetDownloadListByTags(_manifest, tags);
  260. var operation = new ResourceDownloaderOperation(_impl.Download, _impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  261. return operation;
  262. }
  263. public override ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  264. {
  265. if (Status != EOperationStatus.Succeed)
  266. {
  267. YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !");
  268. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  269. }
  270. List<AssetInfo> assetInfos = new List<AssetInfo>();
  271. var assetInfo = _manifest.ConvertLocationToAssetInfo(location, null);
  272. assetInfos.Add(assetInfo);
  273. List<BundleInfo> downloadList = _impl.GetDownloadListByPaths(_manifest, assetInfos.ToArray());
  274. var operation = new ResourceDownloaderOperation(_impl.Download, _impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  275. return operation;
  276. }
  277. public override ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  278. {
  279. if (Status != EOperationStatus.Succeed)
  280. {
  281. YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !");
  282. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  283. }
  284. List<AssetInfo> assetInfos = new List<AssetInfo>(locations.Length);
  285. foreach (var location in locations)
  286. {
  287. var assetInfo = _manifest.ConvertLocationToAssetInfo(location, null);
  288. assetInfos.Add(assetInfo);
  289. }
  290. List<BundleInfo> downloadList = _impl.GetDownloadListByPaths(_manifest, assetInfos.ToArray());
  291. var operation = new ResourceDownloaderOperation(_impl.Download, _impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  292. return operation;
  293. }
  294. }
  295. internal class WebPlayModePreDownloadContentOperation : PreDownloadContentOperation
  296. {
  297. private readonly WebPlayModeImpl _impl;
  298. public WebPlayModePreDownloadContentOperation(WebPlayModeImpl impl)
  299. {
  300. _impl = impl;
  301. }
  302. internal override void InternalOnStart()
  303. {
  304. Status = EOperationStatus.Succeed;
  305. }
  306. internal override void InternalOnUpdate()
  307. {
  308. }
  309. public override ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  310. {
  311. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  312. }
  313. public override ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  314. {
  315. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  316. }
  317. public override ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  318. {
  319. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  320. }
  321. public override ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  322. {
  323. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  324. }
  325. public override ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  326. {
  327. return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
  328. }
  329. }
  330. }