mirror of
https://github.com/tuyoogame/YooAsset.git
synced 2026-05-27 11:10:11 +00:00
refactor the runtime code
重构了运行时代码,支持全新的文件系统。
This commit is contained in:
@@ -20,15 +20,14 @@ namespace YooAsset
|
||||
public delegate void OnDownloadError(string fileName, string error);
|
||||
public delegate void OnStartDownloadFile(string fileName, long sizeBytes);
|
||||
|
||||
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;
|
||||
@@ -86,9 +85,8 @@ namespace YooAsset
|
||||
public OnStartDownloadFile OnStartDownloadFileCallback { 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); ;
|
||||
@@ -103,7 +101,7 @@ namespace YooAsset
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
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()
|
||||
@@ -133,11 +131,11 @@ namespace YooAsset
|
||||
foreach (var downloader in _downloaders)
|
||||
{
|
||||
downloadBytes += (long)downloader.DownloadedBytes;
|
||||
if (downloader.IsDone() == false)
|
||||
if (downloader.IsDone == false)
|
||||
continue;
|
||||
|
||||
// 检测是否下载失败
|
||||
if (downloader.HasError())
|
||||
if (downloader.Status != EOperationStatus.Succeed)
|
||||
{
|
||||
_removeList.Add(downloader);
|
||||
_failedList.Add(downloader);
|
||||
@@ -147,13 +145,13 @@ namespace YooAsset
|
||||
// 下载成功
|
||||
_removeList.Add(downloader);
|
||||
_cachedDownloadCount++;
|
||||
_cachedDownloadBytes += downloader.GetDownloadFileSize();
|
||||
_cachedDownloadBytes += (long)downloader.DownloadedBytes;
|
||||
}
|
||||
|
||||
// 移除已经完成的下载器(无论成功或失败)
|
||||
foreach (var loader in _removeList)
|
||||
foreach (var downloader in _removeList)
|
||||
{
|
||||
_downloaders.Remove(loader);
|
||||
_downloaders.Remove(downloader);
|
||||
}
|
||||
|
||||
// 如果下载进度发生变化
|
||||
@@ -177,7 +175,6 @@ namespace YooAsset
|
||||
int index = _bundleInfoList.Count - 1;
|
||||
var bundleInfo = _bundleInfoList[index];
|
||||
var downloader = bundleInfo.CreateDownloader(_failedTryAgain, _timeout);
|
||||
downloader.SendRequest();
|
||||
_downloaders.Add(downloader);
|
||||
_bundleInfoList.RemoveAt(index);
|
||||
OnStartDownloadFileCallback?.Invoke(bundleInfo.Bundle.BundleName, bundleInfo.Bundle.FileSize);
|
||||
@@ -190,11 +187,11 @@ 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());
|
||||
OnDownloadErrorCallback?.Invoke(bundleName, failedDownloader.Error);
|
||||
OnDownloadOverCallback?.Invoke(false);
|
||||
}
|
||||
else
|
||||
@@ -246,16 +243,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 +271,7 @@ namespace YooAsset
|
||||
{
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
OperationSystem.StartOperation(this);
|
||||
OperationSystem.StartOperation(_packageName, this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,69 +301,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