update download system

support to cancel download operation
This commit is contained in:
hevinci
2023-10-07 16:02:11 +08:00
parent 0c7289bbc8
commit 0ceeeb8c5b
8 changed files with 160 additions and 93 deletions

View File

@@ -49,7 +49,9 @@ namespace YooAsset
var downloader = valuePair.Value;
downloader.Update();
if (downloader.IsDone())
{
_removeList.Add(valuePair.Key);
}
}
// 移除下载器
@@ -82,7 +84,10 @@ namespace YooAsset
{
// 查询存在的下载器
if (_downloaders.TryGetValue(bundleInfo.CachedDataFilePath, out var downloader))
{
downloader.Reference();
return downloader;
}
// 如果资源已经缓存
if (bundleInfo.IsCached())
@@ -92,22 +97,19 @@ namespace YooAsset
}
// 创建新的下载器
DownloaderBase newDownloader = null;
YooLogger.Log($"Beginning to download bundle : {bundleInfo.Bundle.BundleName} URL : {bundleInfo.RemoteMainURL}");
#if UNITY_WEBGL
if (bundleInfo.Bundle.Buildpipeline == DefaultBuildPipeline.RawFileBuildPipelineName)
{
FileUtility.CreateFileDirectory(bundleInfo.CachedDataFilePath);
System.Type requesterType = typeof(FileGeneralRequest);
DownloaderBase newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
_downloaders.Add(bundleInfo.CachedDataFilePath, newDownloader);
return newDownloader;
newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
}
else
{
System.Type requesterType = typeof(AssetBundleWebRequest);
WebDownloader newDownloader = new WebDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
_downloaders.Add(bundleInfo.CachedDataFilePath, newDownloader);
return newDownloader;
newDownloader = new WebDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
}
#else
FileUtility.CreateFileDirectory(bundleInfo.CachedDataFilePath);
@@ -115,18 +117,34 @@ namespace YooAsset
if (resumeDownload)
{
System.Type requesterType = typeof(FileResumeRequest);
DownloaderBase newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
_downloaders.Add(bundleInfo.CachedDataFilePath, newDownloader);
return newDownloader;
newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
}
else
{
System.Type requesterType = typeof(FileGeneralRequest);
DownloaderBase newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
_downloaders.Add(bundleInfo.CachedDataFilePath, newDownloader);
return newDownloader;
newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
}
#endif
// 返回新创建的下载器
_downloaders.Add(bundleInfo.CachedDataFilePath, newDownloader);
newDownloader.Reference();
return newDownloader;
}
/// <summary>
/// 停止不再使用的下载器
/// </summary>
public void AbortUnusedDownloader()
{
foreach (var valuePair in _downloaders)
{
var downloader = valuePair.Value;
if (downloader.RefCount <= 0)
{
downloader.Abort();
}
}
}
}
}

View File

@@ -49,6 +49,11 @@ namespace YooAsset
/// </summary>
public ulong DownloadedBytes { protected set; get; }
/// <summary>
/// 引用计数
/// </summary>
public int RefCount { private set; get; }
public DownloaderBase(BundleInfo bundleInfo, System.Type requesterType, int failedTryAgain, int timeout)
{
@@ -63,20 +68,19 @@ namespace YooAsset
public abstract AssetBundle GetAssetBundle();
/// <summary>
/// 获取下载文件的大小
/// 引用(引用计数递加)
/// </summary>
/// <returns></returns>
public long GetDownloadFileSize()
public void Reference()
{
return _bundleInfo.Bundle.FileSize;
RefCount++;
}
/// <summary>
/// 获取下载文件的资源包名
/// 释放(引用计数递减)
/// </summary>
public string GetDownloadBundleName()
public void Release()
{
return _bundleInfo.Bundle.BundleName;
RefCount--;
}
/// <summary>
@@ -119,6 +123,23 @@ namespace YooAsset
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>
/// 获取网络请求地址