update download system

下载文件验证支持多线程。
This commit is contained in:
hevinci
2023-03-03 16:12:17 +08:00
parent fc3ed28eda
commit ab29069af5
10 changed files with 250 additions and 40 deletions
@@ -2,6 +2,7 @@
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine.Networking;
namespace YooAsset
@@ -21,6 +22,10 @@ namespace YooAsset
private static readonly Dictionary<string, DownloaderBase> _downloaderDic = new Dictionary<string, DownloaderBase>();
private static readonly List<string> _removeList = new List<string>(100);
/// <summary>
/// 线程同步
/// </summary>
public static ThreadSyncContext SyncContext { set; get; }
/// <summary>
/// 自定义下载器的请求委托
@@ -44,10 +49,21 @@ namespace YooAsset
/// <summary>
/// 更新所有下载器
/// 初始化下载器
/// </summary>
public static void Initialize()
{
SyncContext = new ThreadSyncContext();
}
/// <summary>
/// 更新下载器
/// </summary>
public static void Update()
{
if (SyncContext != null)
SyncContext.Update();
// 更新下载器
_removeList.Clear();
foreach (var valuePair in _downloaderDic)
@@ -78,6 +94,7 @@ namespace YooAsset
_downloaderDic.Clear();
_removeList.Clear();
SyncContext = null;
RequestDelegate = null;
CertificateHandlerInstance = null;
BreakpointResumeFileSize = int.MaxValue;
@@ -7,11 +7,13 @@ namespace YooAsset
{
None,
CheckTempFile,
WaitingCheckTempFile,
PrepareDownload,
CreateResumeDownloader,
CreateGeneralDownloader,
CheckDownload,
VerifyingFile,
VerifyTempFile,
WaitingVerifyTempFile,
CachingFile,
TryAgain,
Succeed,
@@ -13,6 +13,8 @@ namespace YooAsset
private readonly string _tempFilePath;
private UnityWebRequest _webRequest = null;
private DownloadHandlerFileRange _downloadHandle = null;
private VerifyTempFileOperation _checkFileOp = null;
private VerifyTempFileOperation _verifyFileOp = null;
// 重置变量
private bool _isAbort = false;
@@ -34,17 +36,28 @@ namespace YooAsset
if (IsDone())
return;
// 检测本地临时文件
// 检测临时文件
if (_steps == ESteps.CheckTempFile)
{
var verifyResult = CacheSystem.VerifyingTempFile(_bundleInfo.Bundle);
if (verifyResult == EVerifyResult.Succeed)
VerifyTempElement element = new VerifyTempElement(_bundleInfo.Bundle.TempDataFilePath, _bundleInfo.Bundle.FileCRC, _bundleInfo.Bundle.FileSize);
_checkFileOp = VerifyTempFileOperation.CreateOperation(element);
OperationSystem.StartOperation(_checkFileOp);
_steps = ESteps.WaitingCheckTempFile;
}
// 等待检测结果
if (_steps == ESteps.WaitingCheckTempFile)
{
if (_checkFileOp.IsDone == false)
return;
if (_checkFileOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.CachingFile;
}
else
{
if (verifyResult == EVerifyResult.FileOverflow)
if (_checkFileOp.VerifyResult == EVerifyResult.FileOverflow)
{
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
@@ -188,7 +201,7 @@ namespace YooAsset
}
else
{
_steps = ESteps.VerifyingFile;
_steps = ESteps.VerifyTempFile;
}
// 释放下载器
@@ -196,21 +209,30 @@ namespace YooAsset
}
// 验证下载文件
if (_steps == ESteps.VerifyingFile)
if (_steps == ESteps.VerifyTempFile)
{
var verifyResult = CacheSystem.VerifyingTempFile(_bundleInfo.Bundle);
if (verifyResult == EVerifyResult.Succeed)
VerifyTempElement element = new VerifyTempElement(_bundleInfo.Bundle.TempDataFilePath, _bundleInfo.Bundle.FileCRC, _bundleInfo.Bundle.FileSize);
_verifyFileOp = VerifyTempFileOperation.CreateOperation(element);
OperationSystem.StartOperation(_verifyFileOp);
_steps = ESteps.WaitingVerifyTempFile;
}
// 等待验证完成
if (_steps == ESteps.WaitingVerifyTempFile)
{
if (_verifyFileOp.IsDone == false)
return;
if (_verifyFileOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.CachingFile;
}
else
{
_lastError = $"Failed to verifying file : {_bundleInfo.Bundle.FileName}, ErrorCode : {verifyResult}";
// 注意:验证失败后删除文件
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
_lastError = _verifyFileOp.Error;
_steps = ESteps.TryAgain;
}
}
@@ -226,7 +248,7 @@ namespace YooAsset
long dataFileSize = _bundleInfo.Bundle.FileSize;
if (File.Exists(infoFilePath))
File.Delete(infoFilePath);
File.Delete(infoFilePath);
if (File.Exists(dataFilePath))
File.Delete(dataFilePath);