using System; using System.IO; using System.Collections; using System.Collections.Generic; using System.Threading; using UnityEngine.Networking; namespace YooAsset { /// /// 自定义下载器的请求委托 /// public delegate UnityWebRequest DownloadRequestDelegate(string url); /// /// 1. 保证每一时刻资源文件只存在一个下载器 /// 2. 保证下载器下载完成后立刻验证并缓存 /// 3. 保证资源文件不会被重复下载 /// internal static class DownloadSystem { private static readonly Dictionary _downloaderDic = new Dictionary(); private static readonly List _removeList = new List(100); /// /// 自定义下载器的请求委托 /// public static DownloadRequestDelegate RequestDelegate = null; /// /// 自定义的证书认证实例 /// public static CertificateHandler CertificateHandlerInstance = null; /// /// 网络重定向次数 /// public static int RedirectLimit { set; get; } = -1; /// /// 启用断点续传功能文件的最小字节数 /// public static int BreakpointResumeFileSize { set; get; } = int.MaxValue; /// /// 下载失败后清理文件的HTTP错误码 /// public static List ClearFileResponseCodes { set; get; } /// /// 初始化下载器 /// public static void Initialize() { } /// /// 更新下载器 /// public static void Update() { // 更新下载器 _removeList.Clear(); foreach (var valuePair in _downloaderDic) { var downloader = valuePair.Value; downloader.Update(); if (downloader.IsDone()) _removeList.Add(valuePair.Key); } // 移除下载器 foreach (var key in _removeList) { _downloaderDic.Remove(key); } } /// /// 销毁所有下载器 /// public static void DestroyAll() { foreach (var valuePair in _downloaderDic) { var downloader = valuePair.Value; downloader.Abort(); } _downloaderDic.Clear(); _removeList.Clear(); RequestDelegate = null; CertificateHandlerInstance = null; BreakpointResumeFileSize = int.MaxValue; ClearFileResponseCodes = null; } /// /// 创建下载器 /// 注意:只有第一次请求的参数才有效 /// public static DownloaderBase CreateDownload(BundleInfo bundleInfo, int failedTryAgain, int timeout = 60) { // 查询存在的下载器 if (_downloaderDic.TryGetValue(bundleInfo.Bundle.CachedDataFilePath, out var downloader)) return downloader; // 如果资源已经缓存 if (CacheSystem.IsCached(bundleInfo.Bundle.PackageName, bundleInfo.Bundle.CacheGUID)) { var completedDownloader = new CompletedDownloader(bundleInfo); return completedDownloader; } // 创建新的下载器 YooLogger.Log($"Beginning to download bundle : {bundleInfo.Bundle.BundleName} URL : {bundleInfo.RemoteMainURL}"); #if UNITY_WEBGL if (bundleInfo.Bundle.IsRawFile) { FileUtility.CreateFileDirectory(bundleInfo.Bundle.CachedDataFilePath); DownloaderBase newDownloader = new FileGeneralDownloader(bundleInfo, failedTryAgain, timeout); _downloaderDic.Add(bundleInfo.Bundle.CachedDataFilePath, newDownloader); return newDownloader; } else { WebDownloader newDownloader = new WebDownloader(bundleInfo, failedTryAgain, timeout); _downloaderDic.Add(bundleInfo.Bundle.CachedDataFilePath, newDownloader); return newDownloader; } #else FileUtility.CreateFileDirectory(bundleInfo.Bundle.CachedDataFilePath); bool resumeDownload = bundleInfo.Bundle.FileSize >= BreakpointResumeFileSize; DownloaderBase newDownloader; if (resumeDownload) newDownloader = new FileResumeDownloader(bundleInfo, failedTryAgain, timeout); else newDownloader = new FileGeneralDownloader(bundleInfo, failedTryAgain, timeout); _downloaderDic.Add(bundleInfo.Bundle.CachedDataFilePath, newDownloader); return newDownloader; #endif } /// /// 创建一个新的网络请求 /// public static UnityWebRequest NewRequest(string requestURL) { UnityWebRequest webRequest; if (RequestDelegate != null) webRequest = RequestDelegate.Invoke(requestURL); else webRequest = new UnityWebRequest(requestURL, UnityWebRequest.kHttpVerbGET); SetUnityWebRequestParam(webRequest); return webRequest; } /// /// 设置网络请求的自定义参数 /// private static void SetUnityWebRequestParam(UnityWebRequest webRequest) { if (RedirectLimit >= 0) webRequest.redirectLimit = RedirectLimit; if (CertificateHandlerInstance != null) { webRequest.certificateHandler = CertificateHandlerInstance; webRequest.disposeCertificateHandlerOnDispose = false; } } /// /// 获取下载器的总数 /// public static int GetDownloaderTotalCount() { return _downloaderDic.Count; } } }