DownloadManager.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine.Networking;
  6. namespace YooAsset
  7. {
  8. /// <summary>
  9. /// 1. 保证每一时刻资源文件只存在一个下载器
  10. /// 2. 保证下载器下载完成后立刻验证并缓存
  11. /// 3. 保证资源文件不会被重复下载
  12. /// </summary>
  13. internal class DownloadManager
  14. {
  15. private readonly Dictionary<string, DownloaderBase> _downloaders = new Dictionary<string, DownloaderBase>(1000);
  16. private readonly List<string> _removeList = new List<string>(1000);
  17. private uint _breakpointResumeFileSize;
  18. /// <summary>
  19. /// 所属包裹
  20. /// </summary>
  21. public readonly string PackageName;
  22. public DownloadManager(string packageName)
  23. {
  24. PackageName = packageName;
  25. }
  26. /// <summary>
  27. /// 初始化
  28. /// </summary>
  29. public void Initialize(uint breakpointResumeFileSize)
  30. {
  31. _breakpointResumeFileSize = breakpointResumeFileSize;
  32. }
  33. /// <summary>
  34. /// 更新下载器
  35. /// </summary>
  36. public void Update()
  37. {
  38. // 更新下载器
  39. _removeList.Clear();
  40. foreach (var valuePair in _downloaders)
  41. {
  42. var downloader = valuePair.Value;
  43. downloader.Update();
  44. if (downloader.IsDone())
  45. {
  46. _removeList.Add(valuePair.Key);
  47. }
  48. }
  49. // 移除下载器
  50. foreach (var key in _removeList)
  51. {
  52. _downloaders.Remove(key);
  53. }
  54. }
  55. /// <summary>
  56. /// 销毁所有下载器
  57. /// </summary>
  58. public void DestroyAll()
  59. {
  60. foreach (var valuePair in _downloaders)
  61. {
  62. var downloader = valuePair.Value;
  63. downloader.Abort();
  64. }
  65. _downloaders.Clear();
  66. _removeList.Clear();
  67. }
  68. /// <summary>
  69. /// 创建下载器
  70. /// 注意:只有第一次请求的参数才有效
  71. /// </summary>
  72. public DownloaderBase CreateDownload(BundleInfo bundleInfo, int failedTryAgain, int timeout = 60)
  73. {
  74. // 查询存在的下载器
  75. if (_downloaders.TryGetValue(bundleInfo.CachedDataFilePath, out var downloader))
  76. {
  77. downloader.Reference();
  78. return downloader;
  79. }
  80. // 如果资源已经缓存
  81. if (bundleInfo.IsCached())
  82. {
  83. var completedDownloader = new CompletedDownloader(bundleInfo);
  84. return completedDownloader;
  85. }
  86. // 创建新的下载器
  87. DownloaderBase newDownloader = null;
  88. YooLogger.Log($"Beginning to download bundle : {bundleInfo.Bundle.BundleName} URL : {bundleInfo.RemoteMainURL}");
  89. #if UNITY_WEBGL
  90. if (bundleInfo.Bundle.Buildpipeline == EDefaultBuildPipeline.RawFileBuildPipeline.ToString())
  91. {
  92. FileUtility.CreateFileDirectory(bundleInfo.CachedDataFilePath);
  93. System.Type requesterType = typeof(FileGeneralRequest);
  94. newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
  95. }
  96. else
  97. {
  98. System.Type requesterType = typeof(AssetBundleWebRequest);
  99. newDownloader = new WebDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
  100. }
  101. #else
  102. FileUtility.CreateFileDirectory(bundleInfo.CachedDataFilePath);
  103. bool resumeDownload = bundleInfo.Bundle.FileSize >= _breakpointResumeFileSize;
  104. if (resumeDownload)
  105. {
  106. System.Type requesterType = typeof(FileResumeRequest);
  107. newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
  108. }
  109. else
  110. {
  111. System.Type requesterType = typeof(FileGeneralRequest);
  112. newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
  113. }
  114. #endif
  115. // 返回新创建的下载器
  116. _downloaders.Add(bundleInfo.CachedDataFilePath, newDownloader);
  117. newDownloader.Reference();
  118. return newDownloader;
  119. }
  120. /// <summary>
  121. /// 停止不再使用的下载器
  122. /// </summary>
  123. public void AbortUnusedDownloader()
  124. {
  125. foreach (var valuePair in _downloaders)
  126. {
  127. var downloader = valuePair.Value;
  128. if (downloader.RefCount <= 0)
  129. {
  130. downloader.Abort();
  131. }
  132. }
  133. }
  134. }
  135. }