Update DownloadSystem

pull/4/head
hevinci 2022-03-29 19:45:20 +08:00
parent 32d422aeaa
commit 2d9cccae6f
5 changed files with 33 additions and 36 deletions

View File

@ -41,7 +41,7 @@ namespace YooAsset
private readonly List<ProviderBase> _providers = new List<ProviderBase>(100); private readonly List<ProviderBase> _providers = new List<ProviderBase>(100);
private bool _isWaitForAsyncComplete = false; private bool _isWaitForAsyncComplete = false;
private bool _isShowWaitForAsyncError = false; private bool _isShowWaitForAsyncError = false;
private FileDownloader _fileDownloader; private DownloaderBase _downloader;
private AssetBundleCreateRequest _cacheRequest; private AssetBundleCreateRequest _cacheRequest;
internal AssetBundle CacheBundle { private set; get; } internal AssetBundle CacheBundle { private set; get; }
@ -106,19 +106,19 @@ namespace YooAsset
if (Status == EStatus.Download) if (Status == EStatus.Download)
{ {
int failedTryAgain = int.MaxValue; int failedTryAgain = int.MaxValue;
_fileDownloader = DownloadSystem.BeginDownload(BundleFileInfo, failedTryAgain); _downloader = DownloadSystem.BeginDownload(BundleFileInfo, failedTryAgain);
Status = EStatus.CheckDownload; Status = EStatus.CheckDownload;
} }
// 2. 检测服务器下载结果 // 2. 检测服务器下载结果
if (Status == EStatus.CheckDownload) if (Status == EStatus.CheckDownload)
{ {
if (_fileDownloader.IsDone() == false) if (_downloader.IsDone() == false)
return; return;
if (_fileDownloader.HasError()) if (_downloader.HasError())
{ {
_fileDownloader.ReportError(); _downloader.ReportError();
Status = EStatus.Fail; Status = EStatus.Fail;
} }
else else

View File

@ -19,7 +19,7 @@ namespace YooAsset
private readonly BundleInfo _bundleInfo; private readonly BundleInfo _bundleInfo;
private readonly string _savePath; private readonly string _savePath;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
private FileDownloader _fileDownloader; private DownloaderBase _downloader;
private UnityWebFileRequester _fileRequester; private UnityWebFileRequester _fileRequester;
/// <summary> /// <summary>
@ -67,21 +67,21 @@ namespace YooAsset
if (_steps == ESteps.DownloadFromWeb) if (_steps == ESteps.DownloadFromWeb)
{ {
int failedTryAgain = int.MaxValue; int failedTryAgain = int.MaxValue;
_fileDownloader = DownloadSystem.BeginDownload(_bundleInfo, failedTryAgain); _downloader = DownloadSystem.BeginDownload(_bundleInfo, failedTryAgain);
_steps = ESteps.CheckDownloadFromWeb; _steps = ESteps.CheckDownloadFromWeb;
} }
// 3. 检测服务器下载结果 // 3. 检测服务器下载结果
if (_steps == ESteps.CheckDownloadFromWeb) if (_steps == ESteps.CheckDownloadFromWeb)
{ {
if (_fileDownloader.IsDone() == false) if (_downloader.IsDone() == false)
return; return;
if (_fileDownloader.HasError()) if (_downloader.HasError())
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = _fileDownloader.GetLastError(); Error = _downloader.GetLastError();
} }
else else
{ {

View File

@ -12,7 +12,7 @@ namespace YooAsset
/// </summary> /// </summary>
internal static class DownloadSystem internal static class DownloadSystem
{ {
private static readonly Dictionary<string, FileDownloader> _downloaderDic = new Dictionary<string, FileDownloader>(); private static readonly Dictionary<string, DownloaderBase> _downloaderDic = new Dictionary<string, DownloaderBase>();
private static readonly List<string> _removeList = new List<string>(100); private static readonly List<string> _removeList = new List<string>(100);
private static readonly Dictionary<string, string> _cachedHashList = new Dictionary<string, string>(1000); private static readonly Dictionary<string, string> _cachedHashList = new Dictionary<string, string>(1000);
@ -43,7 +43,7 @@ namespace YooAsset
/// 开始下载资源文件 /// 开始下载资源文件
/// 注意:只有第一次请求的参数才是有效的 /// 注意:只有第一次请求的参数才是有效的
/// </summary> /// </summary>
public static FileDownloader BeginDownload(BundleInfo bundleInfo, int failedTryAgain, int timeout = 60) public static DownloaderBase BeginDownload(BundleInfo bundleInfo, int failedTryAgain, int timeout = 60)
{ {
// 查询存在的下载器 // 查询存在的下载器
if (_downloaderDic.TryGetValue(bundleInfo.Hash, out var downloader)) if (_downloaderDic.TryGetValue(bundleInfo.Hash, out var downloader))
@ -63,7 +63,7 @@ namespace YooAsset
{ {
YooLogger.Log($"Beginning to download file : {bundleInfo.BundleName} URL : {bundleInfo.RemoteMainURL}"); YooLogger.Log($"Beginning to download file : {bundleInfo.BundleName} URL : {bundleInfo.RemoteMainURL}");
FileUtility.CreateFileDirectory(bundleInfo.LocalPath); FileUtility.CreateFileDirectory(bundleInfo.LocalPath);
var newDownloader = new FileDownloader(bundleInfo); var newDownloader = new HttpDownloader(bundleInfo);
newDownloader.SendRequest(failedTryAgain, timeout); newDownloader.SendRequest(failedTryAgain, timeout);
_downloaderDic.Add(bundleInfo.Hash, newDownloader); _downloaderDic.Add(bundleInfo.Hash, newDownloader);
return newDownloader; return newDownloader;

View File

@ -33,7 +33,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 下载结果(成功或失败) /// 下载结果(成功或失败)
/// </summary> /// </summary>
public bool Result = false; public bool Result = true;
/// <summary> /// <summary>
/// 错误日志 /// 错误日志
@ -93,19 +93,18 @@ namespace YooAsset
{ {
// 创建文件流 // 创建文件流
fileStream = new FileStream(_savePath, FileMode.OpenOrCreate, FileAccess.Write); fileStream = new FileStream(_savePath, FileMode.OpenOrCreate, FileAccess.Write);
long fileLength = fileStream.Length; long fileLength = fileStream.Length - 1;
// 创建HTTP下载请求 // 创建HTTP下载请求
HttpWebRequest fileRequest = WebRequest.Create(_url) as HttpWebRequest; HttpWebRequest fileRequest = WebRequest.Create(_url) as HttpWebRequest;
fileRequest.Timeout = _timeout; fileRequest.Timeout = _timeout * 1000;
fileRequest.ReadWriteTimeout = _timeout;
fileRequest.ProtocolVersion = HttpVersion.Version10; fileRequest.ProtocolVersion = HttpVersion.Version10;
if (fileLength > 0) if (fileLength > 0)
{ {
// 注意:设置远端请求文件的起始位置 // 注意:设置远端请求文件的起始位置
fileRequest.AddRange(fileLength); fileRequest.AddRange(fileLength);
// 注意:设置本地文件流的起始位置 // 注意:设置本地文件流的起始位置
fileStream.Seek(fileLength, SeekOrigin.Begin); fileStream.Seek(-1, SeekOrigin.End);
} }
// 读取下载数据并保存到文件 // 读取下载数据并保存到文件
@ -127,18 +126,6 @@ namespace YooAsset
DownloadProgress = progress; DownloadProgress = progress;
DownloadedBytes = (ulong)fileLength; DownloadedBytes = (ulong)fileLength;
} }
// 验证下载文件完整性
bool verfiyResult = DownloadSystem.CheckContentIntegrity(_savePath, _fileSize, _fileCRC);
if (verfiyResult)
{
Result = true;
}
else
{
Result = false;
Error = $"Verify file content failed : {_fileHash}";
}
} }
catch (Exception e) catch (Exception e)
{ {
@ -160,8 +147,21 @@ namespace YooAsset
if (fileStream != null) if (fileStream != null)
{ {
fileStream.Flush();
fileStream.Close(); fileStream.Close();
fileStream.Dispose(); }
// 验证下载文件完整性
if (Result)
{
bool verfiyResult = DownloadSystem.CheckContentIntegrity(_savePath, _fileSize, _fileCRC);
if (verfiyResult == false)
{
Result = false;
Error = $"Verify file content failed : {_fileHash}";
if (File.Exists(_savePath))
File.Delete(_savePath);
}
} }
IsDone = true; IsDone = true;
@ -213,9 +213,6 @@ namespace YooAsset
_lastError = _threadDownloader.Error; _lastError = _threadDownloader.Error;
ReportError(); ReportError();
if (File.Exists(_bundleInfo.LocalPath))
File.Delete(_bundleInfo.LocalPath);
// 失败后重新尝试 // 失败后重新尝试
if (_failedTryAgain > 0) if (_failedTryAgain > 0)
_steps = ESteps.TryAgain; _steps = ESteps.TryAgain;

View File

@ -22,8 +22,8 @@ namespace YooAsset
private readonly int _failedTryAgain; private readonly int _failedTryAgain;
private readonly List<BundleInfo> _downloadList; private readonly List<BundleInfo> _downloadList;
private readonly List<BundleInfo> _loadFailedList = new List<BundleInfo>(); private readonly List<BundleInfo> _loadFailedList = new List<BundleInfo>();
private readonly List<FileDownloader> _downloaders = new List<FileDownloader>(); private readonly List<DownloaderBase> _downloaders = new List<DownloaderBase>();
private readonly List<FileDownloader> _removeList = new List<FileDownloader>(MAX_LOADER_COUNT); private readonly List<DownloaderBase> _removeList = new List<DownloaderBase>(MAX_LOADER_COUNT);
// 数据相关 // 数据相关
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;