diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/DownloadHandlerFileRange.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/DownloadHandlerFileRange.cs new file mode 100644 index 0000000..d8ee959 --- /dev/null +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/DownloadHandlerFileRange.cs @@ -0,0 +1,87 @@ +using System; +using System.IO; +using UnityEngine; +using UnityEngine.Networking; + +namespace YooAsset +{ + /// + /// 支持Unity2018版本的断点续传下载器 + /// + public class DownloadHandlerFileRange : DownloadHandlerScript + { + private string _fileSavePath; + private long _fileTotalSize; + private UnityWebRequest _webRequest; + private FileStream _fileStream; + + private long _localFileSize = 0; + private long _curFileSize = 0; + + + public DownloadHandlerFileRange(string fileSavePath, long fileTotalSize, UnityWebRequest webRequest) : base(new byte[1024 * 1024]) + { + _fileSavePath = fileSavePath; + _fileTotalSize = fileTotalSize; + _webRequest = webRequest; + + if (File.Exists(fileSavePath)) + { + FileInfo fileInfo = new FileInfo(fileSavePath); + _localFileSize = fileInfo.Length; + } + + _fileStream = new FileStream(_fileSavePath, FileMode.Append, FileAccess.Write); + _curFileSize = _localFileSize; + } + protected override bool ReceiveData(byte[] data, int dataLength) + { + if (data == null || dataLength == 0 || _webRequest.responseCode >= 400) + return false; + + if (_fileStream == null) + return false; + + _fileStream.Write(data, 0, dataLength); + _curFileSize += dataLength; + return true; + } + + /// + /// UnityWebRequest.downloadHandler.data + /// + protected override byte[] GetData() + { + return null; + } + + /// + /// UnityWebRequest.downloadHandler.text + /// + protected override string GetText() + { + return null; + } + + /// + /// UnityWebRequest.downloadProgress + /// + protected override float GetProgress() + { + return _fileTotalSize == 0 ? 0 : ((float)_curFileSize) / _fileTotalSize; + } + + /// + /// 释放下载句柄 + /// + public void Cleanup() + { + if (_fileStream != null) + { + _fileStream.Flush(); + _fileStream.Dispose(); + _fileStream = null; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/DownloadHandlerFileRange.cs.meta b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/DownloadHandlerFileRange.cs.meta new file mode 100644 index 0000000..ff557b1 --- /dev/null +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/DownloadHandlerFileRange.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 94254ab8e4496214884c11a891c131c6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs index c3c2d55..b8253cd 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs @@ -9,8 +9,9 @@ namespace YooAsset { internal sealed class FileDownloader : DownloaderBase { - private UnityWebRequest _webRequest; - private bool _breakResume; + private readonly bool _breakResume; + private UnityWebRequest _webRequest = null; + private DownloadHandlerFileRange _downloadHandle = null; // 重置变量 private bool _isAbort = false; @@ -40,7 +41,7 @@ namespace YooAsset } else { - if(verifyResult == EVerifyResult.FileOverflow) + if (verifyResult == EVerifyResult.FileOverflow) { string cacheFilePath = _bundleInfo.Bundle.CachedFilePath; if (File.Exists(cacheFilePath)) @@ -75,8 +76,15 @@ namespace YooAsset _requestURL = GetRequestURL(); _webRequest = UnityWebRequest.Get(_requestURL); - DownloadHandlerFile handler = new DownloadHandlerFile(fileSavePath, true); + +#if UNITY_2019_4_OR_NEWER + var handler = new DownloadHandlerFile(fileSavePath, true); handler.removeFileOnAbort = false; +#else + var handler = new DownloadHandlerFileRange(fileSavePath, _bundleInfo.Bundle.FileSize, _webRequest); + _downloadHandle = handler; +#endif + _webRequest.downloadHandler = handler; _webRequest.disposeDownloadHandlerOnDispose = true; if (fileLength > 0) @@ -218,6 +226,12 @@ namespace YooAsset } private void DisposeWebRequest() { + if(_downloadHandle != null) + { + _downloadHandle.Cleanup(); + _downloadHandle = null; + } + if (_webRequest != null) { _webRequest.Dispose();