Update DownloadSystem

This commit is contained in:
hevinci
2022-03-29 19:45:20 +08:00
parent 32d422aeaa
commit 2d9cccae6f
5 changed files with 33 additions and 36 deletions
@@ -12,7 +12,7 @@ namespace YooAsset
/// </summary>
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 Dictionary<string, string> _cachedHashList = new Dictionary<string, string>(1000);
@@ -43,7 +43,7 @@ namespace YooAsset
/// 开始下载资源文件
/// 注意:只有第一次请求的参数才是有效的
/// </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))
@@ -63,7 +63,7 @@ namespace YooAsset
{
YooLogger.Log($"Beginning to download file : {bundleInfo.BundleName} URL : {bundleInfo.RemoteMainURL}");
FileUtility.CreateFileDirectory(bundleInfo.LocalPath);
var newDownloader = new FileDownloader(bundleInfo);
var newDownloader = new HttpDownloader(bundleInfo);
newDownloader.SendRequest(failedTryAgain, timeout);
_downloaderDic.Add(bundleInfo.Hash, newDownloader);
return newDownloader;
@@ -33,7 +33,7 @@ namespace YooAsset
/// <summary>
/// 下载结果(成功或失败)
/// </summary>
public bool Result = false;
public bool Result = true;
/// <summary>
/// 错误日志
@@ -93,19 +93,18 @@ namespace YooAsset
{
// 创建文件流
fileStream = new FileStream(_savePath, FileMode.OpenOrCreate, FileAccess.Write);
long fileLength = fileStream.Length;
long fileLength = fileStream.Length - 1;
// 创建HTTP下载请求
HttpWebRequest fileRequest = WebRequest.Create(_url) as HttpWebRequest;
fileRequest.Timeout = _timeout;
fileRequest.ReadWriteTimeout = _timeout;
fileRequest.Timeout = _timeout * 1000;
fileRequest.ProtocolVersion = HttpVersion.Version10;
if (fileLength > 0)
{
// 注意:设置远端请求文件的起始位置
fileRequest.AddRange(fileLength);
// 注意:设置本地文件流的起始位置
fileStream.Seek(fileLength, SeekOrigin.Begin);
fileStream.Seek(-1, SeekOrigin.End);
}
// 读取下载数据并保存到文件
@@ -127,18 +126,6 @@ namespace YooAsset
DownloadProgress = progress;
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)
{
@@ -160,8 +147,21 @@ namespace YooAsset
if (fileStream != null)
{
fileStream.Flush();
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;
@@ -213,9 +213,6 @@ namespace YooAsset
_lastError = _threadDownloader.Error;
ReportError();
if (File.Exists(_bundleInfo.LocalPath))
File.Delete(_bundleInfo.LocalPath);
// 失败后重新尝试
if (_failedTryAgain > 0)
_steps = ESteps.TryAgain;