mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
TEngine 6
This commit is contained in:
@@ -15,20 +15,36 @@ namespace YooAsset
|
||||
|
||||
private const int MAX_LOADER_COUNT = 64;
|
||||
|
||||
public delegate void OnDownloadOver(bool isSucceed);
|
||||
public delegate void OnDownloadProgress(int totalDownloadCount, int currentDownloadCount, long totalDownloadBytes, long currentDownloadBytes);
|
||||
public delegate void OnDownloadError(string fileName, string error);
|
||||
public delegate void OnStartDownloadFile(string fileName, long sizeBytes);
|
||||
#region 委托定义
|
||||
/// <summary>
|
||||
/// 下载器结束
|
||||
/// </summary>
|
||||
public delegate void DownloaderFinish(DownloaderFinishData data);
|
||||
|
||||
/// <summary>
|
||||
/// 下载进度更新
|
||||
/// </summary>
|
||||
public delegate void DownloadUpdate(DownloadUpdateData data);
|
||||
|
||||
/// <summary>
|
||||
/// 下载发生错误
|
||||
/// </summary>
|
||||
public delegate void DownloadError(DownloadErrorData data);
|
||||
|
||||
/// <summary>
|
||||
/// 开始下载某个文件
|
||||
/// </summary>
|
||||
public delegate void DownloadFileBegin(DownloadFileData data);
|
||||
#endregion
|
||||
|
||||
private readonly DownloadManager _downloadMgr;
|
||||
private readonly string _packageName;
|
||||
private readonly int _downloadingMaxNumber;
|
||||
private readonly int _failedTryAgain;
|
||||
private readonly int _timeout;
|
||||
private readonly List<BundleInfo> _bundleInfoList;
|
||||
private readonly List<DownloaderBase> _downloaders = new List<DownloaderBase>(MAX_LOADER_COUNT);
|
||||
private readonly List<DownloaderBase> _removeList = new List<DownloaderBase>(MAX_LOADER_COUNT);
|
||||
private readonly List<DownloaderBase> _failedList = new List<DownloaderBase>(MAX_LOADER_COUNT);
|
||||
private readonly List<FSDownloadFileOperation> _downloaders = new List<FSDownloadFileOperation>(MAX_LOADER_COUNT);
|
||||
private readonly List<FSDownloadFileOperation> _removeList = new List<FSDownloadFileOperation>(MAX_LOADER_COUNT);
|
||||
private readonly List<FSDownloadFileOperation> _failedList = new List<FSDownloadFileOperation>(MAX_LOADER_COUNT);
|
||||
|
||||
// 数据相关
|
||||
private bool _isPause = false;
|
||||
@@ -68,27 +84,26 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 当下载器结束(无论成功或失败)
|
||||
/// </summary>
|
||||
public OnDownloadOver OnDownloadOverCallback { set; get; }
|
||||
public DownloaderFinish DownloadFinishCallback { set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 当下载进度发生变化
|
||||
/// </summary>
|
||||
public OnDownloadProgress OnDownloadProgressCallback { set; get; }
|
||||
public DownloadUpdate DownloadUpdateCallback { set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 当某个文件下载失败
|
||||
/// 当下载器发生错误
|
||||
/// </summary>
|
||||
public OnDownloadError OnDownloadErrorCallback { set; get; }
|
||||
public DownloadError DownloadErrorCallback { set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 当开始下载某个文件
|
||||
/// </summary>
|
||||
public OnStartDownloadFile OnStartDownloadFileCallback { set; get; }
|
||||
public DownloadFileBegin DownloadFileBeginCallback { set; get; }
|
||||
|
||||
|
||||
internal DownloaderOperation(DownloadManager downloadMgr, string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
|
||||
internal DownloaderOperation(string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
|
||||
{
|
||||
_downloadMgr = downloadMgr;
|
||||
_packageName = packageName;
|
||||
_bundleInfoList = downloadList;
|
||||
_downloadingMaxNumber = UnityEngine.Mathf.Clamp(downloadingMaxNumber, 1, MAX_LOADER_COUNT); ;
|
||||
@@ -101,12 +116,12 @@ namespace YooAsset
|
||||
// 统计下载信息
|
||||
CalculatDownloaderInfo();
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
internal override void InternalStart()
|
||||
{
|
||||
YooLogger.Log($"Begine to download : {TotalDownloadCount} files and {TotalDownloadBytes} bytes");
|
||||
YooLogger.Log($"Begine to download {TotalDownloadCount} files and {TotalDownloadBytes} bytes");
|
||||
_steps = ESteps.Check;
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
internal override void InternalUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
@@ -132,12 +147,13 @@ namespace YooAsset
|
||||
long downloadBytes = _cachedDownloadBytes;
|
||||
foreach (var downloader in _downloaders)
|
||||
{
|
||||
downloadBytes += (long)downloader.DownloadedBytes;
|
||||
if (downloader.IsDone() == false)
|
||||
downloader.UpdateOperation();
|
||||
downloadBytes += downloader.DownloadedBytes;
|
||||
if (downloader.IsDone == false)
|
||||
continue;
|
||||
|
||||
// 检测是否下载失败
|
||||
if (downloader.HasError())
|
||||
if (downloader.Status != EOperationStatus.Succeed)
|
||||
{
|
||||
_removeList.Add(downloader);
|
||||
_failedList.Add(downloader);
|
||||
@@ -147,13 +163,13 @@ namespace YooAsset
|
||||
// 下载成功
|
||||
_removeList.Add(downloader);
|
||||
_cachedDownloadCount++;
|
||||
_cachedDownloadBytes += downloader.GetDownloadFileSize();
|
||||
_cachedDownloadBytes += downloader.DownloadedBytes;
|
||||
}
|
||||
|
||||
// 移除已经完成的下载器(无论成功或失败)
|
||||
foreach (var loader in _removeList)
|
||||
foreach (var downloader in _removeList)
|
||||
{
|
||||
_downloaders.Remove(loader);
|
||||
_downloaders.Remove(downloader);
|
||||
}
|
||||
|
||||
// 如果下载进度发生变化
|
||||
@@ -162,7 +178,18 @@ namespace YooAsset
|
||||
_lastDownloadBytes = downloadBytes;
|
||||
_lastDownloadCount = _cachedDownloadCount;
|
||||
Progress = (float)_lastDownloadBytes / TotalDownloadBytes;
|
||||
OnDownloadProgressCallback?.Invoke(TotalDownloadCount, _lastDownloadCount, TotalDownloadBytes, _lastDownloadBytes);
|
||||
|
||||
if (DownloadUpdateCallback != null)
|
||||
{
|
||||
var data = new DownloadUpdateData();
|
||||
data.PackageName = _packageName;
|
||||
data.Progress = Progress;
|
||||
data.TotalDownloadCount = TotalDownloadCount;
|
||||
data.CurrentDownloadCount = _lastDownloadCount;
|
||||
data.TotalDownloadBytes = TotalDownloadBytes;
|
||||
data.CurrentDownloadBytes = _lastDownloadBytes;
|
||||
DownloadUpdateCallback.Invoke(data);
|
||||
}
|
||||
}
|
||||
|
||||
// 动态创建新的下载器到最大数量限制
|
||||
@@ -177,10 +204,20 @@ namespace YooAsset
|
||||
int index = _bundleInfoList.Count - 1;
|
||||
var bundleInfo = _bundleInfoList[index];
|
||||
var downloader = bundleInfo.CreateDownloader(_failedTryAgain, _timeout);
|
||||
downloader.SendRequest();
|
||||
downloader.StartOperation();
|
||||
this.AddChildOperation(downloader);
|
||||
|
||||
_downloaders.Add(downloader);
|
||||
_bundleInfoList.RemoveAt(index);
|
||||
OnStartDownloadFileCallback?.Invoke(bundleInfo.Bundle.BundleName, bundleInfo.Bundle.FileSize);
|
||||
|
||||
if (DownloadFileBeginCallback != null)
|
||||
{
|
||||
var data = new DownloadFileData();
|
||||
data.PackageName = _packageName;
|
||||
data.FileName = bundleInfo.Bundle.BundleName;
|
||||
data.FileSize = bundleInfo.Bundle.FileSize;
|
||||
DownloadFileBeginCallback.Invoke(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,19 +227,41 @@ namespace YooAsset
|
||||
if (_failedList.Count > 0)
|
||||
{
|
||||
var failedDownloader = _failedList[0];
|
||||
string bundleName = failedDownloader.GetDownloadBundleName();
|
||||
string bundleName = failedDownloader.Bundle.BundleName;
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = $"Failed to download file : {bundleName}";
|
||||
OnDownloadErrorCallback?.Invoke(bundleName, failedDownloader.GetLastError());
|
||||
OnDownloadOverCallback?.Invoke(false);
|
||||
|
||||
if (DownloadErrorCallback != null)
|
||||
{
|
||||
var data = new DownloadErrorData();
|
||||
data.PackageName = _packageName;
|
||||
data.FileName = bundleName;
|
||||
data.ErrorInfo = failedDownloader.Error;
|
||||
DownloadErrorCallback.Invoke(data);
|
||||
}
|
||||
|
||||
if (DownloadFinishCallback != null)
|
||||
{
|
||||
var data = new DownloaderFinishData();
|
||||
data.PackageName = _packageName;
|
||||
data.Succeed = false;
|
||||
DownloadFinishCallback.Invoke(data);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 结算成功
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
OnDownloadOverCallback?.Invoke(true);
|
||||
|
||||
if (DownloadFinishCallback != null)
|
||||
{
|
||||
var data = new DownloaderFinishData();
|
||||
data.PackageName = _packageName;
|
||||
data.Succeed = true;
|
||||
DownloadFinishCallback.Invoke(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -246,16 +305,18 @@ namespace YooAsset
|
||||
HashSet<string> temper = new HashSet<string>();
|
||||
foreach (var bundleInfo in _bundleInfoList)
|
||||
{
|
||||
if (temper.Contains(bundleInfo.CachedDataFilePath) == false)
|
||||
string combineGUID = bundleInfo.GetDownloadCombineGUID();
|
||||
if (temper.Contains(combineGUID) == false)
|
||||
{
|
||||
temper.Add(bundleInfo.CachedDataFilePath);
|
||||
temper.Add(combineGUID);
|
||||
}
|
||||
}
|
||||
|
||||
// 合并下载列表
|
||||
foreach (var bundleInfo in downloader._bundleInfoList)
|
||||
{
|
||||
if (temper.Contains(bundleInfo.CachedDataFilePath) == false)
|
||||
string combineGUID = bundleInfo.GetDownloadCombineGUID();
|
||||
if (temper.Contains(combineGUID) == false)
|
||||
{
|
||||
_bundleInfoList.Add(bundleInfo);
|
||||
}
|
||||
@@ -272,7 +333,7 @@ namespace YooAsset
|
||||
{
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
OperationSystem.StartOperation(this);
|
||||
OperationSystem.StartOperation(_packageName, this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,69 +363,63 @@ namespace YooAsset
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = "User cancel.";
|
||||
ReleaseAllDownloader();
|
||||
}
|
||||
}
|
||||
private void ReleaseAllDownloader()
|
||||
{
|
||||
foreach (var downloader in _downloaders)
|
||||
{
|
||||
downloader.Release();
|
||||
}
|
||||
|
||||
// 注意:停止不再使用的下载器
|
||||
_downloadMgr.AbortUnusedDownloader();
|
||||
foreach (var downloader in _downloaders)
|
||||
{
|
||||
downloader.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ResourceDownloaderOperation : DownloaderOperation
|
||||
{
|
||||
internal ResourceDownloaderOperation(DownloadManager downloadMgr, string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
|
||||
: base(downloadMgr, packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout)
|
||||
internal ResourceDownloaderOperation(string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
|
||||
: base(packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建空的下载器
|
||||
/// </summary>
|
||||
internal static ResourceDownloaderOperation CreateEmptyDownloader(DownloadManager downloadMgr, string packageName, int downloadingMaxNumber, int failedTryAgain, int timeout)
|
||||
internal static ResourceDownloaderOperation CreateEmptyDownloader(string packageName, int downloadingMaxNumber, int failedTryAgain, int timeout)
|
||||
{
|
||||
List<BundleInfo> downloadList = new List<BundleInfo>();
|
||||
var operation = new ResourceDownloaderOperation(downloadMgr, packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
|
||||
var operation = new ResourceDownloaderOperation(packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
|
||||
return operation;
|
||||
}
|
||||
}
|
||||
public sealed class ResourceUnpackerOperation : DownloaderOperation
|
||||
{
|
||||
internal ResourceUnpackerOperation(DownloadManager downloadMgr, string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
|
||||
: base(downloadMgr, packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout)
|
||||
internal ResourceUnpackerOperation(string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
|
||||
: base(packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建空的解压器
|
||||
/// </summary>
|
||||
internal static ResourceUnpackerOperation CreateEmptyUnpacker(DownloadManager downloadMgr, string packageName, int upackingMaxNumber, int failedTryAgain, int timeout)
|
||||
internal static ResourceUnpackerOperation CreateEmptyUnpacker(string packageName, int upackingMaxNumber, int failedTryAgain, int timeout)
|
||||
{
|
||||
List<BundleInfo> downloadList = new List<BundleInfo>();
|
||||
var operation = new ResourceUnpackerOperation(downloadMgr, packageName, downloadList, upackingMaxNumber, failedTryAgain, int.MaxValue);
|
||||
var operation = new ResourceUnpackerOperation(packageName, downloadList, upackingMaxNumber, failedTryAgain, int.MaxValue);
|
||||
return operation;
|
||||
}
|
||||
}
|
||||
public sealed class ResourceImporterOperation : DownloaderOperation
|
||||
{
|
||||
internal ResourceImporterOperation(DownloadManager downloadMgr, string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
|
||||
: base(downloadMgr, packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout)
|
||||
internal ResourceImporterOperation(string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
|
||||
: base(packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建空的导入器
|
||||
/// </summary>
|
||||
internal static ResourceImporterOperation CreateEmptyImporter(DownloadManager downloadMgr, string packageName, int upackingMaxNumber, int failedTryAgain, int timeout)
|
||||
internal static ResourceImporterOperation CreateEmptyImporter(string packageName, int upackingMaxNumber, int failedTryAgain, int timeout)
|
||||
{
|
||||
List<BundleInfo> downloadList = new List<BundleInfo>();
|
||||
var operation = new ResourceImporterOperation(downloadMgr, packageName, downloadList, upackingMaxNumber, failedTryAgain, int.MaxValue);
|
||||
var operation = new ResourceImporterOperation(packageName, downloadList, upackingMaxNumber, failedTryAgain, int.MaxValue);
|
||||
return operation;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user