mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
更新demo
更新demo
This commit is contained in:
@@ -1,23 +1,28 @@
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class CompletedDownloader : DownloaderBase
|
||||
{
|
||||
public CompletedDownloader(BundleInfo bundleInfo) : base(bundleInfo, 0, 0)
|
||||
{
|
||||
_downloadProgress = 1f;
|
||||
_downloadedBytes = (ulong)bundleInfo.Bundle.FileSize;
|
||||
_status = EStatus.Succeed;
|
||||
}
|
||||
internal sealed class CompletedDownloader : DownloaderBase
|
||||
{
|
||||
public CompletedDownloader(BundleInfo bundleInfo) : base(bundleInfo, null, 0, 0)
|
||||
{
|
||||
DownloadProgress = 1f;
|
||||
DownloadedBytes = (ulong)bundleInfo.Bundle.FileSize;
|
||||
_status = EStatus.Succeed;
|
||||
}
|
||||
|
||||
public override void SendRequest(params object[] param)
|
||||
{
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
}
|
||||
public override void Abort()
|
||||
{
|
||||
}
|
||||
}
|
||||
public override void SendRequest(params object[] param)
|
||||
{
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
}
|
||||
public override void Abort()
|
||||
{
|
||||
}
|
||||
public override AssetBundle GetAssetBundle()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,87 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 支持Unity2018版本的断点续传下载器
|
||||
/// </summary>
|
||||
internal 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// UnityWebRequest.downloadHandler.data
|
||||
/// </summary>
|
||||
protected override byte[] GetData()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// UnityWebRequest.downloadHandler.text
|
||||
/// </summary>
|
||||
protected override string GetText()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// UnityWebRequest.downloadProgress
|
||||
/// </summary>
|
||||
protected override float GetProgress()
|
||||
{
|
||||
return _fileTotalSize == 0 ? 0 : ((float)_curFileSize) / _fileTotalSize;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放下载句柄
|
||||
/// </summary>
|
||||
public void Cleanup()
|
||||
{
|
||||
if (_fileStream != null)
|
||||
{
|
||||
_fileStream.Flush();
|
||||
_fileStream.Dispose();
|
||||
_fileStream = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 94254ab8e4496214884c11a891c131c6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -1,193 +1,182 @@
|
||||
using System.IO;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal abstract class DownloaderBase
|
||||
{
|
||||
public enum EStatus
|
||||
{
|
||||
None = 0,
|
||||
Succeed,
|
||||
Failed
|
||||
}
|
||||
internal abstract class DownloaderBase
|
||||
{
|
||||
public enum EStatus
|
||||
{
|
||||
None = 0,
|
||||
Succeed,
|
||||
Failed
|
||||
}
|
||||
|
||||
protected readonly BundleInfo _bundleInfo;
|
||||
protected readonly int _timeout;
|
||||
protected int _failedTryAgain;
|
||||
protected readonly BundleInfo _bundleInfo;
|
||||
protected readonly System.Type _requesterType;
|
||||
protected readonly int _timeout;
|
||||
protected int _failedTryAgain;
|
||||
|
||||
protected UnityWebRequest _webRequest;
|
||||
protected EStatus _status = EStatus.None;
|
||||
protected string _lastError = string.Empty;
|
||||
protected long _lastCode = 0;
|
||||
protected IWebRequester _requester;
|
||||
protected EStatus _status = EStatus.None;
|
||||
protected string _lastestNetError = string.Empty;
|
||||
protected long _lastestHttpCode = 0;
|
||||
|
||||
// 请求次数
|
||||
protected int _requestCount = 0;
|
||||
protected string _requestURL;
|
||||
// 请求次数
|
||||
protected int _requestCount = 0;
|
||||
protected string _requestURL;
|
||||
|
||||
// 下载进度
|
||||
protected float _downloadProgress = 0f;
|
||||
protected ulong _downloadedBytes = 0;
|
||||
// 超时相关
|
||||
protected bool _isAbort = false;
|
||||
protected ulong _latestDownloadBytes;
|
||||
protected float _latestDownloadRealtime;
|
||||
protected float _tryAgainTimer;
|
||||
|
||||
// 超时相关
|
||||
protected bool _isAbort = false;
|
||||
protected ulong _latestDownloadBytes;
|
||||
protected float _latestDownloadRealtime;
|
||||
protected float _tryAgainTimer;
|
||||
/// <summary>
|
||||
/// 是否等待异步结束
|
||||
/// 警告:只能用于解压APP内部资源
|
||||
/// </summary>
|
||||
public bool WaitForAsyncComplete = false;
|
||||
|
||||
/// <summary>
|
||||
/// 是否等待异步结束
|
||||
/// 警告:只能用于解压APP内部资源
|
||||
/// </summary>
|
||||
public bool WaitForAsyncComplete = false;
|
||||
/// <summary>
|
||||
/// 下载进度(0f~1f)
|
||||
/// </summary>
|
||||
public float DownloadProgress { protected set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 下载进度(0f~1f)
|
||||
/// </summary>
|
||||
public float DownloadProgress
|
||||
{
|
||||
get { return _downloadProgress; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 已经下载的总字节数
|
||||
/// </summary>
|
||||
public ulong DownloadedBytes { protected set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 已经下载的总字节数
|
||||
/// </summary>
|
||||
public ulong DownloadedBytes
|
||||
{
|
||||
get { return _downloadedBytes; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 引用计数
|
||||
/// </summary>
|
||||
public int RefCount { private set; get; }
|
||||
|
||||
|
||||
public DownloaderBase(BundleInfo bundleInfo, int failedTryAgain, int timeout)
|
||||
{
|
||||
_bundleInfo = bundleInfo;
|
||||
_failedTryAgain = failedTryAgain;
|
||||
_timeout = timeout;
|
||||
}
|
||||
public abstract void SendRequest(params object[] param);
|
||||
public abstract void Update();
|
||||
public abstract void Abort();
|
||||
public DownloaderBase(BundleInfo bundleInfo, System.Type requesterType, int failedTryAgain, int timeout)
|
||||
{
|
||||
_bundleInfo = bundleInfo;
|
||||
_requesterType = requesterType;
|
||||
_failedTryAgain = failedTryAgain;
|
||||
_timeout = timeout;
|
||||
}
|
||||
public abstract void SendRequest(params object[] args);
|
||||
public abstract void Update();
|
||||
public abstract void Abort();
|
||||
public abstract AssetBundle GetAssetBundle();
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载文件的大小
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public long GetDownloadFileSize()
|
||||
{
|
||||
return _bundleInfo.Bundle.FileSize;
|
||||
}
|
||||
/// <summary>
|
||||
/// 引用(引用计数递加)
|
||||
/// </summary>
|
||||
public void Reference()
|
||||
{
|
||||
RefCount++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载文件的资源包名
|
||||
/// </summary>
|
||||
public string GetDownloadBundleName()
|
||||
{
|
||||
return _bundleInfo.Bundle.BundleName;
|
||||
}
|
||||
/// <summary>
|
||||
/// 释放(引用计数递减)
|
||||
/// </summary>
|
||||
public void Release()
|
||||
{
|
||||
RefCount--;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检测下载器是否已经完成(无论成功或失败)
|
||||
/// </summary>
|
||||
public bool IsDone()
|
||||
{
|
||||
return _status == EStatus.Succeed || _status == EStatus.Failed;
|
||||
}
|
||||
/// <summary>
|
||||
/// 检测下载器是否已经完成(无论成功或失败)
|
||||
/// </summary>
|
||||
public bool IsDone()
|
||||
{
|
||||
return _status == EStatus.Succeed || _status == EStatus.Failed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载过程是否发生错误
|
||||
/// </summary>
|
||||
public bool HasError()
|
||||
{
|
||||
return _status == EStatus.Failed;
|
||||
}
|
||||
/// <summary>
|
||||
/// 下载过程是否发生错误
|
||||
/// </summary>
|
||||
public bool HasError()
|
||||
{
|
||||
return _status == EStatus.Failed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按照错误级别打印错误
|
||||
/// </summary>
|
||||
public void ReportError()
|
||||
{
|
||||
YooLogger.Error(GetLastError());
|
||||
}
|
||||
/// <summary>
|
||||
/// 按照错误级别打印错误
|
||||
/// </summary>
|
||||
public void ReportError()
|
||||
{
|
||||
YooLogger.Error(GetLastError());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按照警告级别打印错误
|
||||
/// </summary>
|
||||
public void ReportWarning()
|
||||
{
|
||||
YooLogger.Warning(GetLastError());
|
||||
}
|
||||
/// <summary>
|
||||
/// 按照警告级别打印错误
|
||||
/// </summary>
|
||||
public void ReportWarning()
|
||||
{
|
||||
YooLogger.Warning(GetLastError());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取最近发生的错误信息
|
||||
/// </summary>
|
||||
public string GetLastError()
|
||||
{
|
||||
return $"Failed to download : {_requestURL} Error : {_lastError} Code : {_lastCode}";
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取最近发生的错误信息
|
||||
/// </summary>
|
||||
public string GetLastError()
|
||||
{
|
||||
return $"Failed to download : {_requestURL} Error : {_lastestNetError} Code : {_lastestHttpCode}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载文件的大小
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public long GetDownloadFileSize()
|
||||
{
|
||||
return _bundleInfo.Bundle.FileSize;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载的资源包名称
|
||||
/// </summary>
|
||||
public string GetDownloadBundleName()
|
||||
{
|
||||
return _bundleInfo.Bundle.BundleName;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取网络请求地址
|
||||
/// </summary>
|
||||
protected string GetRequestURL()
|
||||
{
|
||||
// 轮流返回请求地址
|
||||
_requestCount++;
|
||||
if (_requestCount % 2 == 0)
|
||||
return _bundleInfo.RemoteFallbackURL;
|
||||
else
|
||||
return _bundleInfo.RemoteMainURL;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取网络请求地址
|
||||
/// </summary>
|
||||
protected string GetRequestURL()
|
||||
{
|
||||
// 轮流返回请求地址
|
||||
_requestCount++;
|
||||
if (_requestCount % 2 == 0)
|
||||
return _bundleInfo.RemoteFallbackURL;
|
||||
else
|
||||
return _bundleInfo.RemoteMainURL;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 超时判定方法
|
||||
/// </summary>
|
||||
protected void CheckTimeout()
|
||||
{
|
||||
// 注意:在连续时间段内无新增下载数据及判定为超时
|
||||
if (_isAbort == false)
|
||||
{
|
||||
if (_latestDownloadBytes != DownloadedBytes)
|
||||
{
|
||||
_latestDownloadBytes = DownloadedBytes;
|
||||
_latestDownloadRealtime = Time.realtimeSinceStartup;
|
||||
}
|
||||
/// <summary>
|
||||
/// 超时判定方法
|
||||
/// </summary>
|
||||
protected void CheckTimeout()
|
||||
{
|
||||
// 注意:在连续时间段内无新增下载数据及判定为超时
|
||||
if (_isAbort == false)
|
||||
{
|
||||
if (_latestDownloadBytes != DownloadedBytes)
|
||||
{
|
||||
_latestDownloadBytes = DownloadedBytes;
|
||||
_latestDownloadRealtime = Time.realtimeSinceStartup;
|
||||
}
|
||||
|
||||
float offset = Time.realtimeSinceStartup - _latestDownloadRealtime;
|
||||
if (offset > _timeout)
|
||||
{
|
||||
YooLogger.Warning($"Web file request timeout : {_requestURL}");
|
||||
_webRequest.Abort();
|
||||
_isAbort = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 缓存下载文件
|
||||
/// </summary>
|
||||
protected void CachingFile(string tempFilePath)
|
||||
{
|
||||
string infoFilePath = _bundleInfo.Bundle.CachedInfoFilePath;
|
||||
string dataFilePath = _bundleInfo.Bundle.CachedDataFilePath;
|
||||
string dataFileCRC = _bundleInfo.Bundle.FileCRC;
|
||||
long dataFileSize = _bundleInfo.Bundle.FileSize;
|
||||
|
||||
if (File.Exists(infoFilePath))
|
||||
File.Delete(infoFilePath);
|
||||
if (File.Exists(dataFilePath))
|
||||
File.Delete(dataFilePath);
|
||||
|
||||
FileInfo fileInfo = new FileInfo(tempFilePath);
|
||||
fileInfo.MoveTo(dataFilePath);
|
||||
|
||||
// 写入信息文件记录验证数据
|
||||
CacheFileInfo.WriteInfoToFile(infoFilePath, dataFileCRC, dataFileSize);
|
||||
|
||||
// 记录缓存文件
|
||||
var wrapper = new PackageCache.RecordWrapper(infoFilePath, dataFilePath, dataFileCRC, dataFileSize);
|
||||
CacheSystem.RecordFile(_bundleInfo.Bundle.PackageName, _bundleInfo.Bundle.CacheGUID, wrapper);
|
||||
}
|
||||
}
|
||||
float offset = Time.realtimeSinceStartup - _latestDownloadRealtime;
|
||||
if (offset > _timeout)
|
||||
{
|
||||
YooLogger.Warning($"Web file request timeout : {_requestURL}");
|
||||
if (_requester != null)
|
||||
_requester.Abort();
|
||||
_isAbort = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,216 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件下载器
|
||||
/// </summary>
|
||||
internal sealed class FileDownloader : DownloaderBase
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
PrepareDownload,
|
||||
CreateDownloader,
|
||||
CheckDownload,
|
||||
VerifyTempFile,
|
||||
WaitingVerifyTempFile,
|
||||
CachingFile,
|
||||
TryAgain,
|
||||
Done,
|
||||
}
|
||||
|
||||
private VerifyTempFileOperation _verifyFileOp = null;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
public FileDownloader(BundleInfo bundleInfo, System.Type requesterType, int failedTryAgain, int timeout) : base(bundleInfo, requesterType, failedTryAgain, timeout)
|
||||
{
|
||||
}
|
||||
public override void SendRequest(params object[] args)
|
||||
{
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
_steps = ESteps.PrepareDownload;
|
||||
}
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
if (_steps == ESteps.None)
|
||||
return;
|
||||
if (IsDone())
|
||||
return;
|
||||
|
||||
// 准备下载
|
||||
if (_steps == ESteps.PrepareDownload)
|
||||
{
|
||||
// 获取请求地址
|
||||
_requestURL = GetRequestURL();
|
||||
|
||||
// 重置变量
|
||||
DownloadProgress = 0f;
|
||||
DownloadedBytes = 0;
|
||||
|
||||
// 重置变量
|
||||
_isAbort = false;
|
||||
_latestDownloadBytes = 0;
|
||||
_latestDownloadRealtime = Time.realtimeSinceStartup;
|
||||
|
||||
// 重置计时器
|
||||
if (_tryAgainTimer > 0f)
|
||||
YooLogger.Warning($"Try again download : {_requestURL}");
|
||||
_tryAgainTimer = 0f;
|
||||
|
||||
_steps = ESteps.CreateDownloader;
|
||||
}
|
||||
|
||||
// 创建下载器
|
||||
if (_steps == ESteps.CreateDownloader)
|
||||
{
|
||||
_requester = (IWebRequester)Activator.CreateInstance(_requesterType);
|
||||
_requester.Create(_requestURL, _bundleInfo);
|
||||
_steps = ESteps.CheckDownload;
|
||||
}
|
||||
|
||||
// 检测下载结果
|
||||
if (_steps == ESteps.CheckDownload)
|
||||
{
|
||||
_requester.Update();
|
||||
DownloadedBytes = _requester.DownloadedBytes;
|
||||
DownloadProgress = _requester.DownloadProgress;
|
||||
if (_requester.IsDone() == false)
|
||||
{
|
||||
CheckTimeout();
|
||||
return;
|
||||
}
|
||||
|
||||
_lastestNetError = _requester.RequestNetError;
|
||||
_lastestHttpCode = _requester.RequestHttpCode;
|
||||
if (_requester.Status != ERequestStatus.Success)
|
||||
{
|
||||
_steps = ESteps.TryAgain;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.VerifyTempFile;
|
||||
}
|
||||
}
|
||||
|
||||
// 验证下载文件
|
||||
if (_steps == ESteps.VerifyTempFile)
|
||||
{
|
||||
VerifyTempFileElement element = new VerifyTempFileElement(_bundleInfo.TempDataFilePath, _bundleInfo.Bundle.FileCRC, _bundleInfo.Bundle.FileSize);
|
||||
_verifyFileOp = VerifyTempFileOperation.CreateOperation(element);
|
||||
OperationSystem.StartOperation(_bundleInfo.Bundle.PackageName, _verifyFileOp);
|
||||
_steps = ESteps.WaitingVerifyTempFile;
|
||||
}
|
||||
|
||||
// 等待验证完成
|
||||
if (_steps == ESteps.WaitingVerifyTempFile)
|
||||
{
|
||||
if (WaitForAsyncComplete)
|
||||
_verifyFileOp.InternalOnUpdate();
|
||||
|
||||
if (_verifyFileOp.IsDone == false)
|
||||
return;
|
||||
|
||||
if (_verifyFileOp.Status == EOperationStatus.Succeed)
|
||||
{
|
||||
_steps = ESteps.CachingFile;
|
||||
}
|
||||
else
|
||||
{
|
||||
string tempFilePath = _bundleInfo.TempDataFilePath;
|
||||
if (File.Exists(tempFilePath))
|
||||
File.Delete(tempFilePath);
|
||||
|
||||
_lastestNetError = _verifyFileOp.Error;
|
||||
_steps = ESteps.TryAgain;
|
||||
}
|
||||
}
|
||||
|
||||
// 缓存下载文件
|
||||
if (_steps == ESteps.CachingFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
CachingFile();
|
||||
_status = EStatus.Succeed;
|
||||
_steps = ESteps.Done;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_lastestNetError = e.Message;
|
||||
_steps = ESteps.TryAgain;
|
||||
}
|
||||
}
|
||||
|
||||
// 重新尝试下载
|
||||
if (_steps == ESteps.TryAgain)
|
||||
{
|
||||
if (_failedTryAgain <= 0)
|
||||
{
|
||||
ReportError();
|
||||
_status = EStatus.Failed;
|
||||
_steps = ESteps.Done;
|
||||
return;
|
||||
}
|
||||
|
||||
_tryAgainTimer += Time.unscaledDeltaTime;
|
||||
if (_tryAgainTimer > 1f)
|
||||
{
|
||||
_failedTryAgain--;
|
||||
_steps = ESteps.PrepareDownload;
|
||||
ReportWarning();
|
||||
}
|
||||
}
|
||||
}
|
||||
public override void Abort()
|
||||
{
|
||||
if (_requester != null)
|
||||
_requester.Abort();
|
||||
|
||||
if (IsDone() == false)
|
||||
{
|
||||
_status = EStatus.Failed;
|
||||
_steps = ESteps.Done;
|
||||
_lastestNetError = "user abort";
|
||||
_lastestHttpCode = 0;
|
||||
}
|
||||
}
|
||||
public override AssetBundle GetAssetBundle()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 缓存下载文件
|
||||
/// </summary>
|
||||
private void CachingFile()
|
||||
{
|
||||
string tempFilePath = _bundleInfo.TempDataFilePath;
|
||||
string infoFilePath = _bundleInfo.CachedInfoFilePath;
|
||||
string dataFilePath = _bundleInfo.CachedDataFilePath;
|
||||
string dataFileCRC = _bundleInfo.Bundle.FileCRC;
|
||||
long dataFileSize = _bundleInfo.Bundle.FileSize;
|
||||
|
||||
if (File.Exists(infoFilePath))
|
||||
File.Delete(infoFilePath);
|
||||
if (File.Exists(dataFilePath))
|
||||
File.Delete(dataFilePath);
|
||||
|
||||
// 移动临时文件路径
|
||||
FileInfo fileInfo = new FileInfo(tempFilePath);
|
||||
fileInfo.MoveTo(dataFilePath);
|
||||
|
||||
// 写入信息文件记录验证数据
|
||||
CacheHelper.WriteInfoToFile(infoFilePath, dataFileCRC, dataFileSize);
|
||||
|
||||
// 记录缓存文件
|
||||
_bundleInfo.CacheRecord();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,229 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 普通的下载器
|
||||
/// </summary>
|
||||
internal sealed class FileGeneralDownloader : DownloaderBase
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
PrepareDownload,
|
||||
CreateDownloader,
|
||||
CheckDownload,
|
||||
VerifyTempFile,
|
||||
WaitingVerifyTempFile,
|
||||
CachingFile,
|
||||
TryAgain,
|
||||
Done,
|
||||
}
|
||||
|
||||
private readonly string _tempFilePath;
|
||||
private VerifyTempFileOperation _verifyFileOp = null;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
|
||||
public FileGeneralDownloader(BundleInfo bundleInfo, int failedTryAgain, int timeout) : base(bundleInfo, failedTryAgain, timeout)
|
||||
{
|
||||
_tempFilePath = bundleInfo.Bundle.TempDataFilePath;
|
||||
}
|
||||
public override void SendRequest(params object[] param)
|
||||
{
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
_steps = ESteps.PrepareDownload;
|
||||
}
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
if (_steps == ESteps.None)
|
||||
return;
|
||||
if (IsDone())
|
||||
return;
|
||||
|
||||
// 准备下载
|
||||
if (_steps == ESteps.PrepareDownload)
|
||||
{
|
||||
// 获取请求地址
|
||||
_requestURL = GetRequestURL();
|
||||
|
||||
// 重置变量
|
||||
_downloadProgress = 0f;
|
||||
_downloadedBytes = 0;
|
||||
|
||||
// 重置变量
|
||||
_isAbort = false;
|
||||
_latestDownloadBytes = 0;
|
||||
_latestDownloadRealtime = Time.realtimeSinceStartup;
|
||||
|
||||
// 重置计时器
|
||||
if(_tryAgainTimer > 0f)
|
||||
YooLogger.Warning($"Try again download : {_requestURL}");
|
||||
_tryAgainTimer = 0f;
|
||||
|
||||
// 删除临时文件
|
||||
if (File.Exists(_tempFilePath))
|
||||
File.Delete(_tempFilePath);
|
||||
|
||||
_steps = ESteps.CreateDownloader;
|
||||
}
|
||||
|
||||
// 创建下载器
|
||||
if (_steps == ESteps.CreateDownloader)
|
||||
{
|
||||
_webRequest = DownloadSystem.NewRequest(_requestURL);
|
||||
DownloadHandlerFile handler = new DownloadHandlerFile(_tempFilePath);
|
||||
handler.removeFileOnAbort = true;
|
||||
_webRequest.downloadHandler = handler;
|
||||
_webRequest.disposeDownloadHandlerOnDispose = true;
|
||||
_webRequest.SendWebRequest();
|
||||
_steps = ESteps.CheckDownload;
|
||||
}
|
||||
|
||||
// 检测下载结果
|
||||
if (_steps == ESteps.CheckDownload)
|
||||
{
|
||||
_downloadProgress = _webRequest.downloadProgress;
|
||||
_downloadedBytes = _webRequest.downloadedBytes;
|
||||
if (_webRequest.isDone == false)
|
||||
{
|
||||
CheckTimeout();
|
||||
return;
|
||||
}
|
||||
|
||||
bool hasError = false;
|
||||
|
||||
// 检查网络错误
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
if (_webRequest.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
hasError = true;
|
||||
_lastError = _webRequest.error;
|
||||
_lastCode = _webRequest.responseCode;
|
||||
}
|
||||
#else
|
||||
if (_webRequest.isNetworkError || _webRequest.isHttpError)
|
||||
{
|
||||
hasError = true;
|
||||
_lastError = _webRequest.error;
|
||||
_lastCode = _webRequest.responseCode;
|
||||
}
|
||||
#endif
|
||||
|
||||
// 如果网络异常
|
||||
if (hasError)
|
||||
{
|
||||
// 下载失败之后删除文件
|
||||
if (File.Exists(_tempFilePath))
|
||||
File.Delete(_tempFilePath);
|
||||
|
||||
_steps = ESteps.TryAgain;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.VerifyTempFile;
|
||||
}
|
||||
|
||||
// 最终释放下载器
|
||||
DisposeWebRequest();
|
||||
}
|
||||
|
||||
// 验证下载文件
|
||||
if (_steps == ESteps.VerifyTempFile)
|
||||
{
|
||||
VerifyTempFileElement element = new VerifyTempFileElement(_bundleInfo.Bundle.TempDataFilePath, _bundleInfo.Bundle.FileCRC, _bundleInfo.Bundle.FileSize);
|
||||
_verifyFileOp = VerifyTempFileOperation.CreateOperation(element);
|
||||
OperationSystem.StartOperation(_verifyFileOp);
|
||||
_steps = ESteps.WaitingVerifyTempFile;
|
||||
}
|
||||
|
||||
// 等待验证完成
|
||||
if (_steps == ESteps.WaitingVerifyTempFile)
|
||||
{
|
||||
if (WaitForAsyncComplete)
|
||||
_verifyFileOp.Update();
|
||||
|
||||
if (_verifyFileOp.IsDone == false)
|
||||
return;
|
||||
|
||||
if (_verifyFileOp.Status == EOperationStatus.Succeed)
|
||||
{
|
||||
_steps = ESteps.CachingFile;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (File.Exists(_tempFilePath))
|
||||
File.Delete(_tempFilePath);
|
||||
|
||||
_lastError = _verifyFileOp.Error;
|
||||
_steps = ESteps.TryAgain;
|
||||
}
|
||||
}
|
||||
|
||||
// 缓存下载文件
|
||||
if (_steps == ESteps.CachingFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
CachingFile(_tempFilePath);
|
||||
_status = EStatus.Succeed;
|
||||
_steps = ESteps.Done;
|
||||
_lastError = string.Empty;
|
||||
_lastCode = 0;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_lastError = e.Message;
|
||||
_steps = ESteps.TryAgain;
|
||||
}
|
||||
}
|
||||
|
||||
// 重新尝试下载
|
||||
if (_steps == ESteps.TryAgain)
|
||||
{
|
||||
if (_failedTryAgain <= 0)
|
||||
{
|
||||
ReportError();
|
||||
_status = EStatus.Failed;
|
||||
_steps = ESteps.Done;
|
||||
return;
|
||||
}
|
||||
|
||||
_tryAgainTimer += Time.unscaledDeltaTime;
|
||||
if (_tryAgainTimer > 1f)
|
||||
{
|
||||
_failedTryAgain--;
|
||||
_steps = ESteps.PrepareDownload;
|
||||
ReportWarning();
|
||||
}
|
||||
}
|
||||
}
|
||||
public override void Abort()
|
||||
{
|
||||
if (IsDone() == false)
|
||||
{
|
||||
_status = EStatus.Failed;
|
||||
_steps = ESteps.Done;
|
||||
_lastError = "user abort";
|
||||
_lastCode = 0;
|
||||
DisposeWebRequest();
|
||||
}
|
||||
}
|
||||
|
||||
private void DisposeWebRequest()
|
||||
{
|
||||
if (_webRequest != null)
|
||||
{
|
||||
_webRequest.Dispose();
|
||||
_webRequest = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,295 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 断点续传下载器
|
||||
/// </summary>
|
||||
internal sealed class FileResumeDownloader : DownloaderBase
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
CheckTempFile,
|
||||
WaitingCheckTempFile,
|
||||
PrepareDownload,
|
||||
CreateDownloader,
|
||||
CheckDownload,
|
||||
VerifyTempFile,
|
||||
WaitingVerifyTempFile,
|
||||
CachingFile,
|
||||
TryAgain,
|
||||
Done,
|
||||
}
|
||||
|
||||
private readonly string _tempFilePath;
|
||||
private DownloadHandlerFileRange _downloadHandle = null;
|
||||
private VerifyTempFileOperation _checkFileOp = null;
|
||||
private VerifyTempFileOperation _verifyFileOp = null;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
// 重置变量
|
||||
private ulong _fileOriginLength;
|
||||
|
||||
|
||||
public FileResumeDownloader(BundleInfo bundleInfo, int failedTryAgain, int timeout) : base(bundleInfo, failedTryAgain, timeout)
|
||||
{
|
||||
_tempFilePath = bundleInfo.Bundle.TempDataFilePath;
|
||||
}
|
||||
public override void SendRequest(params object[] param)
|
||||
{
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
_steps = ESteps.CheckTempFile;
|
||||
}
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
if (_steps == ESteps.None)
|
||||
return;
|
||||
if (IsDone())
|
||||
return;
|
||||
|
||||
// 检测临时文件
|
||||
if (_steps == ESteps.CheckTempFile)
|
||||
{
|
||||
VerifyTempFileElement element = new VerifyTempFileElement(_bundleInfo.Bundle.TempDataFilePath, _bundleInfo.Bundle.FileCRC, _bundleInfo.Bundle.FileSize);
|
||||
_checkFileOp = VerifyTempFileOperation.CreateOperation(element);
|
||||
OperationSystem.StartOperation(_checkFileOp);
|
||||
_steps = ESteps.WaitingCheckTempFile;
|
||||
}
|
||||
|
||||
// 等待检测结果
|
||||
if (_steps == ESteps.WaitingCheckTempFile)
|
||||
{
|
||||
if (WaitForAsyncComplete)
|
||||
_checkFileOp.Update();
|
||||
|
||||
if (_checkFileOp.IsDone == false)
|
||||
return;
|
||||
|
||||
if (_checkFileOp.Status == EOperationStatus.Succeed)
|
||||
{
|
||||
_steps = ESteps.CachingFile;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_checkFileOp.VerifyResult == EVerifyResult.FileOverflow)
|
||||
{
|
||||
if (File.Exists(_tempFilePath))
|
||||
File.Delete(_tempFilePath);
|
||||
}
|
||||
_steps = ESteps.PrepareDownload;
|
||||
}
|
||||
}
|
||||
|
||||
// 创建下载器
|
||||
if (_steps == ESteps.PrepareDownload)
|
||||
{
|
||||
// 获取请求地址
|
||||
_requestURL = GetRequestURL();
|
||||
|
||||
// 重置变量
|
||||
_downloadProgress = 0f;
|
||||
_downloadedBytes = 0;
|
||||
|
||||
// 重置变量
|
||||
_isAbort = false;
|
||||
_latestDownloadBytes = 0;
|
||||
_latestDownloadRealtime = Time.realtimeSinceStartup;
|
||||
_fileOriginLength = 0;
|
||||
|
||||
// 重置计时器
|
||||
if (_tryAgainTimer > 0f)
|
||||
YooLogger.Warning($"Try again download : {_requestURL}");
|
||||
_tryAgainTimer = 0f;
|
||||
|
||||
_steps = ESteps.CreateDownloader;
|
||||
}
|
||||
|
||||
// 创建下载器
|
||||
if (_steps == ESteps.CreateDownloader)
|
||||
{
|
||||
long fileLength = -1;
|
||||
if (File.Exists(_tempFilePath))
|
||||
{
|
||||
FileInfo fileInfo = new FileInfo(_tempFilePath);
|
||||
fileLength = fileInfo.Length;
|
||||
_fileOriginLength = (ulong)fileLength;
|
||||
_downloadedBytes = _fileOriginLength;
|
||||
}
|
||||
|
||||
#if UNITY_2019_4_OR_NEWER
|
||||
_webRequest = DownloadSystem.NewRequest(_requestURL);
|
||||
var handler = new DownloadHandlerFile(_tempFilePath, true);
|
||||
handler.removeFileOnAbort = false;
|
||||
#else
|
||||
_webRequest = DownloadSystem.NewRequest(_requestURL);
|
||||
var handler = new DownloadHandlerFileRange(_tempFilePath, _bundleInfo.Bundle.FileSize, _webRequest);
|
||||
_downloadHandle = handler;
|
||||
#endif
|
||||
_webRequest.downloadHandler = handler;
|
||||
_webRequest.disposeDownloadHandlerOnDispose = true;
|
||||
if (fileLength > 0)
|
||||
_webRequest.SetRequestHeader("Range", $"bytes={fileLength}-");
|
||||
_webRequest.SendWebRequest();
|
||||
_steps = ESteps.CheckDownload;
|
||||
}
|
||||
|
||||
// 检测下载结果
|
||||
if (_steps == ESteps.CheckDownload)
|
||||
{
|
||||
_downloadProgress = _webRequest.downloadProgress;
|
||||
_downloadedBytes = _fileOriginLength + _webRequest.downloadedBytes;
|
||||
if (_webRequest.isDone == false)
|
||||
{
|
||||
CheckTimeout();
|
||||
return;
|
||||
}
|
||||
|
||||
bool hasError = false;
|
||||
|
||||
// 检查网络错误
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
if (_webRequest.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
hasError = true;
|
||||
_lastError = _webRequest.error;
|
||||
_lastCode = _webRequest.responseCode;
|
||||
}
|
||||
#else
|
||||
if (_webRequest.isNetworkError || _webRequest.isHttpError)
|
||||
{
|
||||
hasError = true;
|
||||
_lastError = _webRequest.error;
|
||||
_lastCode = _webRequest.responseCode;
|
||||
}
|
||||
#endif
|
||||
|
||||
// 如果网络异常
|
||||
if (hasError)
|
||||
{
|
||||
// 注意:下载断点续传文件发生特殊错误码之后删除文件
|
||||
if (DownloadSystem.ClearFileResponseCodes != null)
|
||||
{
|
||||
if (DownloadSystem.ClearFileResponseCodes.Contains(_webRequest.responseCode))
|
||||
{
|
||||
if (File.Exists(_tempFilePath))
|
||||
File.Delete(_tempFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
_steps = ESteps.TryAgain;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.VerifyTempFile;
|
||||
}
|
||||
|
||||
// 最终释放下载器
|
||||
DisposeWebRequest();
|
||||
}
|
||||
|
||||
// 验证下载文件
|
||||
if (_steps == ESteps.VerifyTempFile)
|
||||
{
|
||||
VerifyTempFileElement element = new VerifyTempFileElement(_bundleInfo.Bundle.TempDataFilePath, _bundleInfo.Bundle.FileCRC, _bundleInfo.Bundle.FileSize);
|
||||
_verifyFileOp = VerifyTempFileOperation.CreateOperation(element);
|
||||
OperationSystem.StartOperation(_verifyFileOp);
|
||||
_steps = ESteps.WaitingVerifyTempFile;
|
||||
}
|
||||
|
||||
// 等待验证完成
|
||||
if (_steps == ESteps.WaitingVerifyTempFile)
|
||||
{
|
||||
if (WaitForAsyncComplete)
|
||||
_verifyFileOp.Update();
|
||||
|
||||
if (_verifyFileOp.IsDone == false)
|
||||
return;
|
||||
|
||||
if (_verifyFileOp.Status == EOperationStatus.Succeed)
|
||||
{
|
||||
_steps = ESteps.CachingFile;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (File.Exists(_tempFilePath))
|
||||
File.Delete(_tempFilePath);
|
||||
|
||||
_lastError = _verifyFileOp.Error;
|
||||
_steps = ESteps.TryAgain;
|
||||
}
|
||||
}
|
||||
|
||||
// 缓存下载文件
|
||||
if (_steps == ESteps.CachingFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
CachingFile(_tempFilePath);
|
||||
_status = EStatus.Succeed;
|
||||
_steps = ESteps.Done;
|
||||
_lastError = string.Empty;
|
||||
_lastCode = 0;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_lastError = e.Message;
|
||||
_steps = ESteps.TryAgain;
|
||||
}
|
||||
}
|
||||
|
||||
// 重新尝试下载
|
||||
if (_steps == ESteps.TryAgain)
|
||||
{
|
||||
if (_failedTryAgain <= 0)
|
||||
{
|
||||
ReportError();
|
||||
_status = EStatus.Failed;
|
||||
_steps = ESteps.Done;
|
||||
return;
|
||||
}
|
||||
|
||||
_tryAgainTimer += Time.unscaledDeltaTime;
|
||||
if (_tryAgainTimer > 1f)
|
||||
{
|
||||
_failedTryAgain--;
|
||||
_steps = ESteps.PrepareDownload;
|
||||
ReportWarning();
|
||||
}
|
||||
}
|
||||
}
|
||||
public override void Abort()
|
||||
{
|
||||
if (IsDone() == false)
|
||||
{
|
||||
_status = EStatus.Failed;
|
||||
_steps = ESteps.Done;
|
||||
_lastError = "user abort";
|
||||
_lastCode = 0;
|
||||
DisposeWebRequest();
|
||||
}
|
||||
}
|
||||
|
||||
private void DisposeWebRequest()
|
||||
{
|
||||
if (_downloadHandle != null)
|
||||
{
|
||||
_downloadHandle.Cleanup();
|
||||
_downloadHandle = null;
|
||||
}
|
||||
|
||||
if (_webRequest != null)
|
||||
{
|
||||
_webRequest.Dispose();
|
||||
_webRequest = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a439c9a8f36dcc942b92a8e8af927237
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -3,209 +3,138 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class WebDownloader : DownloaderBase
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
PrepareDownload,
|
||||
CreateDownloader,
|
||||
CheckDownload,
|
||||
VerifyTempFile,
|
||||
WaitingVerifyTempFile,
|
||||
CachingFile,
|
||||
TryAgain,
|
||||
Done,
|
||||
}
|
||||
internal sealed class WebDownloader : DownloaderBase
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
PrepareDownload,
|
||||
CreateDownloader,
|
||||
CheckDownload,
|
||||
TryAgain,
|
||||
Done,
|
||||
}
|
||||
|
||||
private DownloadHandlerAssetBundle _downloadhandler;
|
||||
private ESteps _steps = ESteps.None;
|
||||
private bool _getAssetBundle = false;
|
||||
private AssetBundle _cacheAssetBundle;
|
||||
private ESteps _steps = ESteps.None;
|
||||
private bool _getAssetBundle = false;
|
||||
|
||||
public WebDownloader(BundleInfo bundleInfo, int failedTryAgain, int timeout) : base(bundleInfo, failedTryAgain, timeout)
|
||||
{
|
||||
}
|
||||
public override void SendRequest(params object[] param)
|
||||
{
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
if (param.Length > 0)
|
||||
{
|
||||
_getAssetBundle = (bool)param[0];
|
||||
}
|
||||
_steps = ESteps.PrepareDownload;
|
||||
}
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
if (_steps == ESteps.None)
|
||||
return;
|
||||
if (IsDone())
|
||||
return;
|
||||
public WebDownloader(BundleInfo bundleInfo, System.Type requesterType, int failedTryAgain, int timeout) : base(bundleInfo, requesterType, failedTryAgain, timeout)
|
||||
{
|
||||
}
|
||||
public override void SendRequest(params object[] args)
|
||||
{
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
if (args.Length > 0)
|
||||
{
|
||||
_getAssetBundle = (bool)args[0];
|
||||
}
|
||||
_steps = ESteps.PrepareDownload;
|
||||
}
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
if (_steps == ESteps.None)
|
||||
return;
|
||||
if (IsDone())
|
||||
return;
|
||||
|
||||
// 创建下载器
|
||||
if (_steps == ESteps.PrepareDownload)
|
||||
{
|
||||
// 重置变量
|
||||
_downloadProgress = 0f;
|
||||
_downloadedBytes = 0;
|
||||
// 创建下载器
|
||||
if (_steps == ESteps.PrepareDownload)
|
||||
{
|
||||
// 获取请求地址
|
||||
_requestURL = GetRequestURL();
|
||||
|
||||
// 重置变量
|
||||
_isAbort = false;
|
||||
_latestDownloadBytes = 0;
|
||||
_latestDownloadRealtime = Time.realtimeSinceStartup;
|
||||
_tryAgainTimer = 0f;
|
||||
// 重置变量
|
||||
DownloadProgress = 0f;
|
||||
DownloadedBytes = 0;
|
||||
|
||||
// 获取请求地址
|
||||
_requestURL = GetRequestURL();
|
||||
_steps = ESteps.CreateDownloader;
|
||||
}
|
||||
// 重置变量
|
||||
_isAbort = false;
|
||||
_latestDownloadBytes = 0;
|
||||
_latestDownloadRealtime = Time.realtimeSinceStartup;
|
||||
|
||||
// 创建下载器
|
||||
if (_steps == ESteps.CreateDownloader)
|
||||
{
|
||||
_webRequest = DownloadSystem.NewRequest(_requestURL);
|
||||
// 重置计时器
|
||||
if (_tryAgainTimer > 0f)
|
||||
YooLogger.Warning($"Try again download : {_requestURL}");
|
||||
_tryAgainTimer = 0f;
|
||||
|
||||
if (CacheSystem.DisableUnityCacheOnWebGL)
|
||||
{
|
||||
uint crc = _bundleInfo.Bundle.UnityCRC;
|
||||
_downloadhandler = new DownloadHandlerAssetBundle(_requestURL, crc);
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
_downloadhandler.autoLoadAssetBundle = false;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
uint crc = _bundleInfo.Bundle.UnityCRC;
|
||||
var hash = Hash128.Parse(_bundleInfo.Bundle.FileHash);
|
||||
_downloadhandler = new DownloadHandlerAssetBundle(_requestURL, hash, crc);
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
_downloadhandler.autoLoadAssetBundle = false;
|
||||
#endif
|
||||
}
|
||||
_steps = ESteps.CreateDownloader;
|
||||
}
|
||||
|
||||
_webRequest.downloadHandler = _downloadhandler;
|
||||
_webRequest.disposeDownloadHandlerOnDispose = false;
|
||||
_webRequest.SendWebRequest();
|
||||
_steps = ESteps.CheckDownload;
|
||||
}
|
||||
// 创建下载器
|
||||
if (_steps == ESteps.CreateDownloader)
|
||||
{
|
||||
_requester = (IWebRequester)Activator.CreateInstance(_requesterType);
|
||||
_requester.Create(_requestURL, _bundleInfo, _getAssetBundle);
|
||||
_steps = ESteps.CheckDownload;
|
||||
}
|
||||
|
||||
// 检测下载结果
|
||||
if (_steps == ESteps.CheckDownload)
|
||||
{
|
||||
_downloadProgress = _webRequest.downloadProgress;
|
||||
_downloadedBytes = _webRequest.downloadedBytes;
|
||||
if (_webRequest.isDone == false)
|
||||
{
|
||||
CheckTimeout();
|
||||
return;
|
||||
}
|
||||
// 检测下载结果
|
||||
if (_steps == ESteps.CheckDownload)
|
||||
{
|
||||
_requester.Update();
|
||||
DownloadedBytes = _requester.DownloadedBytes;
|
||||
DownloadProgress = _requester.DownloadProgress;
|
||||
if (_requester.IsDone() == false)
|
||||
{
|
||||
CheckTimeout();
|
||||
return;
|
||||
}
|
||||
|
||||
bool hasError = false;
|
||||
_lastestNetError = _requester.RequestNetError;
|
||||
_lastestHttpCode = _requester.RequestHttpCode;
|
||||
if (_requester.Status != ERequestStatus.Success)
|
||||
{
|
||||
_steps = ESteps.TryAgain;
|
||||
}
|
||||
else
|
||||
{
|
||||
_status = EStatus.Succeed;
|
||||
_steps = ESteps.Done;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查网络错误
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
if (_webRequest.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
hasError = true;
|
||||
_lastError = _webRequest.error;
|
||||
_lastCode = _webRequest.responseCode;
|
||||
}
|
||||
#else
|
||||
if (_webRequest.isNetworkError || _webRequest.isHttpError)
|
||||
{
|
||||
hasError = true;
|
||||
_lastError = _webRequest.error;
|
||||
_lastCode = _webRequest.responseCode;
|
||||
}
|
||||
#endif
|
||||
// 重新尝试下载
|
||||
if (_steps == ESteps.TryAgain)
|
||||
{
|
||||
if (_failedTryAgain <= 0)
|
||||
{
|
||||
ReportError();
|
||||
_status = EStatus.Failed;
|
||||
_steps = ESteps.Done;
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果网络异常
|
||||
if (hasError)
|
||||
{
|
||||
_steps = ESteps.TryAgain;
|
||||
}
|
||||
else
|
||||
{
|
||||
_status = EStatus.Succeed;
|
||||
_steps = ESteps.Done;
|
||||
_lastError = string.Empty;
|
||||
_lastCode = 0;
|
||||
}
|
||||
_tryAgainTimer += Time.unscaledDeltaTime;
|
||||
if (_tryAgainTimer > 1f)
|
||||
{
|
||||
_failedTryAgain--;
|
||||
_steps = ESteps.PrepareDownload;
|
||||
ReportWarning();
|
||||
YooLogger.Warning($"Try again download : {_requestURL}");
|
||||
}
|
||||
}
|
||||
}
|
||||
public override void Abort()
|
||||
{
|
||||
if (_requester != null)
|
||||
_requester.Abort();
|
||||
|
||||
if (_getAssetBundle)
|
||||
{
|
||||
_cacheAssetBundle = _downloadhandler.assetBundle;
|
||||
if (_cacheAssetBundle == null)
|
||||
{
|
||||
_lastError = "assetBundle is null";
|
||||
_steps = ESteps.TryAgain;
|
||||
}
|
||||
}
|
||||
|
||||
// 最终释放请求
|
||||
DisposeRequest();
|
||||
}
|
||||
|
||||
// 重新尝试下载
|
||||
if (_steps == ESteps.TryAgain)
|
||||
{
|
||||
if (_failedTryAgain <= 0)
|
||||
{
|
||||
DisposeRequest();
|
||||
ReportError();
|
||||
_status = EStatus.Failed;
|
||||
_steps = ESteps.Done;
|
||||
return;
|
||||
}
|
||||
|
||||
_tryAgainTimer += Time.unscaledDeltaTime;
|
||||
if (_tryAgainTimer > 1f)
|
||||
{
|
||||
_failedTryAgain--;
|
||||
_steps = ESteps.PrepareDownload;
|
||||
ReportWarning();
|
||||
YooLogger.Warning($"Try again download : {_requestURL}");
|
||||
}
|
||||
}
|
||||
}
|
||||
public override void Abort()
|
||||
{
|
||||
if (IsDone() == false)
|
||||
{
|
||||
_status = EStatus.Failed;
|
||||
_steps = ESteps.Done;
|
||||
_lastError = "user abort";
|
||||
_lastCode = 0;
|
||||
|
||||
DisposeRequest();
|
||||
}
|
||||
}
|
||||
private void DisposeRequest()
|
||||
{
|
||||
if (_webRequest != null)
|
||||
{
|
||||
_webRequest.Dispose();
|
||||
_webRequest = null;
|
||||
}
|
||||
if (_downloadhandler != null)
|
||||
{
|
||||
_downloadhandler.Dispose();
|
||||
_downloadhandler = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源包
|
||||
/// </summary>
|
||||
public AssetBundle GetAssetBundle()
|
||||
{
|
||||
return _cacheAssetBundle;
|
||||
}
|
||||
}
|
||||
if (IsDone() == false)
|
||||
{
|
||||
_status = EStatus.Failed;
|
||||
_steps = ESteps.Done;
|
||||
_lastestNetError = "user abort";
|
||||
_lastestHttpCode = 0;
|
||||
}
|
||||
}
|
||||
public override AssetBundle GetAssetBundle()
|
||||
{
|
||||
return (AssetBundle)_requester.GetRequestObject();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user