DownloadSystem.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using UnityEngine.Networking;
  7. namespace YooAsset
  8. {
  9. /// <summary>
  10. /// 自定义下载器的请求委托
  11. /// </summary>
  12. public delegate UnityWebRequest DownloadRequestDelegate(string url);
  13. /// <summary>
  14. /// 1. 保证每一时刻资源文件只存在一个下载器
  15. /// 2. 保证下载器下载完成后立刻验证并缓存
  16. /// 3. 保证资源文件不会被重复下载
  17. /// </summary>
  18. internal static class DownloadSystem
  19. {
  20. private static readonly Dictionary<string, DownloaderBase> _downloaderDic = new Dictionary<string, DownloaderBase>();
  21. private static readonly List<string> _removeList = new List<string>(100);
  22. /// <summary>
  23. /// 自定义下载器的请求委托
  24. /// </summary>
  25. public static DownloadRequestDelegate RequestDelegate = null;
  26. /// <summary>
  27. /// 自定义的证书认证实例
  28. /// </summary>
  29. public static CertificateHandler CertificateHandlerInstance = null;
  30. /// <summary>
  31. /// 网络重定向次数
  32. /// </summary>
  33. public static int RedirectLimit { set; get; } = -1;
  34. /// <summary>
  35. /// 启用断点续传功能文件的最小字节数
  36. /// </summary>
  37. public static int BreakpointResumeFileSize { set; get; } = int.MaxValue;
  38. /// <summary>
  39. /// 下载失败后清理文件的HTTP错误码
  40. /// </summary>
  41. public static List<long> ClearFileResponseCodes { set; get; }
  42. /// <summary>
  43. /// 初始化下载器
  44. /// </summary>
  45. public static void Initialize()
  46. {
  47. }
  48. /// <summary>
  49. /// 更新下载器
  50. /// </summary>
  51. public static void Update()
  52. {
  53. // 更新下载器
  54. _removeList.Clear();
  55. foreach (var valuePair in _downloaderDic)
  56. {
  57. var downloader = valuePair.Value;
  58. downloader.Update();
  59. if (downloader.IsDone())
  60. _removeList.Add(valuePair.Key);
  61. }
  62. // 移除下载器
  63. foreach (var key in _removeList)
  64. {
  65. _downloaderDic.Remove(key);
  66. }
  67. }
  68. /// <summary>
  69. /// 销毁所有下载器
  70. /// </summary>
  71. public static void DestroyAll()
  72. {
  73. foreach (var valuePair in _downloaderDic)
  74. {
  75. var downloader = valuePair.Value;
  76. downloader.Abort();
  77. }
  78. _downloaderDic.Clear();
  79. _removeList.Clear();
  80. RequestDelegate = null;
  81. CertificateHandlerInstance = null;
  82. BreakpointResumeFileSize = int.MaxValue;
  83. ClearFileResponseCodes = null;
  84. }
  85. /// <summary>
  86. /// 开始下载资源文件
  87. /// 注意:只有第一次请求的参数才是有效的
  88. /// </summary>
  89. public static DownloaderBase BeginDownload(BundleInfo bundleInfo, int failedTryAgain, int timeout = 60)
  90. {
  91. // 查询存在的下载器
  92. if (_downloaderDic.TryGetValue(bundleInfo.Bundle.CachedDataFilePath, out var downloader))
  93. {
  94. return downloader;
  95. }
  96. // 如果资源已经缓存
  97. if (CacheSystem.IsCached(bundleInfo.Bundle.PackageName, bundleInfo.Bundle.CacheGUID))
  98. {
  99. var tempDownloader = new TempDownloader(bundleInfo);
  100. return tempDownloader;
  101. }
  102. // 创建新的下载器
  103. {
  104. YooLogger.Log($"Beginning to download file : {bundleInfo.Bundle.FileName} URL : {bundleInfo.RemoteMainURL}");
  105. FileUtility.CreateFileDirectory(bundleInfo.Bundle.CachedDataFilePath);
  106. bool breakDownload = bundleInfo.Bundle.FileSize >= BreakpointResumeFileSize;
  107. DownloaderBase newDownloader = new FileDownloader(bundleInfo, breakDownload);
  108. newDownloader.SendRequest(failedTryAgain, timeout);
  109. _downloaderDic.Add(bundleInfo.Bundle.CachedDataFilePath, newDownloader);
  110. return newDownloader;
  111. }
  112. }
  113. /// <summary>
  114. /// 创建一个新的网络请求
  115. /// </summary>
  116. public static UnityWebRequest NewRequest(string requestURL)
  117. {
  118. UnityWebRequest webRequest;
  119. if (RequestDelegate != null)
  120. webRequest = RequestDelegate.Invoke(requestURL);
  121. else
  122. webRequest = new UnityWebRequest(requestURL, UnityWebRequest.kHttpVerbGET);
  123. SetUnityWebRequest(webRequest);
  124. return webRequest;
  125. }
  126. /// <summary>
  127. /// 设置网络请求的自定义参数
  128. /// </summary>
  129. public static void SetUnityWebRequest(UnityWebRequest webRequest)
  130. {
  131. if (RedirectLimit >= 0)
  132. webRequest.redirectLimit = RedirectLimit;
  133. if (CertificateHandlerInstance != null)
  134. {
  135. webRequest.certificateHandler = CertificateHandlerInstance;
  136. webRequest.disposeCertificateHandlerOnDispose = false;
  137. }
  138. }
  139. /// <summary>
  140. /// 获取下载器的总数
  141. /// </summary>
  142. public static int GetDownloaderTotalCount()
  143. {
  144. return _downloaderDic.Count;
  145. }
  146. }
  147. }