DownloadSystem.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace YooAsset
  6. {
  7. /// <summary>
  8. /// 1. 保证每一时刻资源文件只存在一个下载器
  9. /// 2. 保证下载器下载完成后立刻验证并缓存
  10. /// 3. 保证资源文件不会被重复下载
  11. /// </summary>
  12. internal static class DownloadSystem
  13. {
  14. private static readonly Dictionary<string, DownloaderBase> _downloaderDic = new Dictionary<string, DownloaderBase>();
  15. private static readonly List<string> _removeList = new List<string>(100);
  16. private static int _breakpointResumeFileSize = int.MaxValue;
  17. /// <summary>
  18. /// 初始化
  19. /// </summary>
  20. public static void Initialize(int breakpointResumeFileSize)
  21. {
  22. _breakpointResumeFileSize = breakpointResumeFileSize;
  23. }
  24. /// <summary>
  25. /// 更新所有下载器
  26. /// </summary>
  27. public static void Update()
  28. {
  29. // 更新下载器
  30. _removeList.Clear();
  31. foreach (var valuePair in _downloaderDic)
  32. {
  33. var downloader = valuePair.Value;
  34. downloader.Update();
  35. if (downloader.IsDone())
  36. _removeList.Add(valuePair.Key);
  37. }
  38. // 移除下载器
  39. foreach (var key in _removeList)
  40. {
  41. _downloaderDic.Remove(key);
  42. }
  43. }
  44. /// <summary>
  45. /// 销毁所有下载器
  46. /// </summary>
  47. public static void DestroyAll()
  48. {
  49. foreach (var valuePair in _downloaderDic)
  50. {
  51. var downloader = valuePair.Value;
  52. downloader.Abort();
  53. }
  54. _downloaderDic.Clear();
  55. _removeList.Clear();
  56. _breakpointResumeFileSize = int.MaxValue;
  57. }
  58. /// <summary>
  59. /// 开始下载资源文件
  60. /// 注意:只有第一次请求的参数才是有效的
  61. /// </summary>
  62. public static DownloaderBase BeginDownload(BundleInfo bundleInfo, int failedTryAgain, int timeout = 60)
  63. {
  64. // 查询存在的下载器
  65. if (_downloaderDic.TryGetValue(bundleInfo.Bundle.CachedFilePath, out var downloader))
  66. {
  67. return downloader;
  68. }
  69. // 如果资源已经缓存
  70. if (CacheSystem.IsCached(bundleInfo.Bundle))
  71. {
  72. var tempDownloader = new TempDownloader(bundleInfo);
  73. return tempDownloader;
  74. }
  75. // 创建新的下载器
  76. {
  77. YooLogger.Log($"Beginning to download file : {bundleInfo.Bundle.FileName} URL : {bundleInfo.RemoteMainURL}");
  78. FileUtility.CreateFileDirectory(bundleInfo.Bundle.CachedFilePath);
  79. bool breakDownload = bundleInfo.Bundle.FileSize >= _breakpointResumeFileSize;
  80. DownloaderBase newDownloader = new FileDownloader(bundleInfo, breakDownload);
  81. newDownloader.SendRequest(failedTryAgain, timeout);
  82. _downloaderDic.Add(bundleInfo.Bundle.CachedFilePath, newDownloader);
  83. return newDownloader;
  84. }
  85. }
  86. /// <summary>
  87. /// 获取下载器的总数
  88. /// </summary>
  89. public static int GetDownloaderTotalCount()
  90. {
  91. return _downloaderDic.Count;
  92. }
  93. }
  94. }