Add progress of async operation

异步操作类增加进度查询。
pull/9/head
hevinci 2022-05-07 20:30:16 +08:00
parent ecd4973948
commit bd87e982ef
15 changed files with 61 additions and 21 deletions

View File

@ -195,6 +195,7 @@ namespace YooAsset
// 3. 检测APK拷贝文件结果
if (_steps == ESteps.CheckDownloadFromApk)
{
Progress = _fileRequester.Progress();
if (_fileRequester.IsDone() == false)
return;
@ -336,6 +337,7 @@ namespace YooAsset
// 3. 检测服务器下载结果
if (_steps == ESteps.CheckDownloadFromWeb)
{
Progress = _downloader.DownloadProgress;
if (_downloader.IsDone() == false)
return;
@ -363,6 +365,7 @@ namespace YooAsset
// 5. 检测APK拷贝文件结果
if (_steps == ESteps.CheckDownloadFromApk)
{
Progress = _fileRequester.Progress();
if (_fileRequester.IsDone() == false)
return;

View File

@ -26,19 +26,6 @@ namespace YooAsset
private Scene _scene;
private AsyncOperation _asyncOp;
/// <summary>
/// 场景卸载进度
/// </summary>
public float Progress
{
get
{
if (_asyncOp == null)
return 0;
return _asyncOp.progress;
}
}
internal UnloadSceneOperation(string error)
{
_flag = EFlag.Error;
@ -87,6 +74,7 @@ namespace YooAsset
if (_steps == ESteps.Checking)
{
Progress = _asyncOp.progress;
if (_asyncOp.isDone == false)
return;

View File

@ -11,7 +11,7 @@ namespace YooAsset
get
{
if (IsDone)
return 100f;
return 1f;
else
return 0;
}

View File

@ -11,7 +11,7 @@ namespace YooAsset
get
{
if (IsDone)
return 100f;
return 1f;
else
return 0;
}

View File

@ -28,7 +28,7 @@ namespace YooAsset
/// <summary>
/// 下载进度0-100f
/// 下载进度
/// </summary>
public float DownloadProgress
{

View File

@ -53,7 +53,7 @@ namespace YooAsset
// 检测下载结果
if (_steps == ESteps.CheckDownload)
{
_downloadProgress = _webRequest.downloadProgress * 100f;
_downloadProgress = _webRequest.downloadProgress;
_downloadedBytes = _webRequest.downloadedBytes;
if (_operationHandle.isDone == false)
{

View File

@ -203,7 +203,7 @@ namespace YooAsset
if (_steps == ESteps.CheckDownload)
{
_downloadProgress = _threadDownloader.DownloadProgress * 100f;
_downloadProgress = _threadDownloader.DownloadProgress;
_downloadedBytes = _threadDownloader.DownloadedBytes;
if (_threadDownloader.IsDone == false)
return;

View File

@ -83,6 +83,16 @@ namespace YooAsset
return _operationHandle.isDone;
}
/// <summary>
/// 下载进度
/// </summary>
public float Progress()
{
if (_operationHandle == null)
return 0;
return _operationHandle.progress;
}
/// <summary>
/// 下载是否发生错误
/// </summary>

View File

@ -60,6 +60,16 @@ namespace YooAsset
return _operationHandle.isDone;
}
/// <summary>
/// 下载进度
/// </summary>
public float Progress()
{
if (_operationHandle == null)
return 0;
return _operationHandle.progress;
}
/// <summary>
/// 下载是否发生错误
/// </summary>

View File

@ -18,7 +18,12 @@ namespace YooAsset
/// <summary>
/// 错误信息
/// </summary>
public string Error { get; protected set; } = string.Empty;
public string Error { get; protected set; }
/// <summary>
/// 处理进度
/// </summary>
public float Progress { get; protected set; }
/// <summary>
/// 是否已经完成
@ -70,8 +75,8 @@ namespace YooAsset
internal abstract void Update();
internal void Finish()
{
Progress = 1f;
_callback?.Invoke(this);
if (_taskCompletionSource != null)
_taskCompletionSource.TrySetResult(null);
}

View File

@ -145,6 +145,7 @@ namespace YooAsset
{
_lastDownloadBytes = downloadBytes;
_lastDownloadCount = CurrentDownloadCount;
Progress = (float)_lastDownloadBytes / TotalDownloadBytes;
OnDownloadProgressCallback?.Invoke(TotalDownloadCount, _lastDownloadCount, TotalDownloadBytes, _lastDownloadBytes);
}

View File

@ -108,6 +108,7 @@ namespace YooAsset
if (_steps == ESteps.Update)
{
_appManifestLoader.Update();
Progress = _appManifestLoader.Progress();
if (_appManifestLoader.IsDone() == false)
return;
@ -181,6 +182,7 @@ namespace YooAsset
if (_steps == ESteps.Update)
{
_appManifestLoader.Update();
Progress = _appManifestLoader.Progress();
if (_appManifestLoader.IsDone() == false)
return;
@ -243,6 +245,16 @@ namespace YooAsset
return false;
}
/// <summary>
/// 加载进度
/// </summary>
public float Progress()
{
if (_downloader2 == null)
return 0;
return _downloader2.Progress();
}
public void Update()
{
if (IsDone())

View File

@ -172,6 +172,7 @@ namespace YooAsset
if (_steps == ESteps.UpdateVerifyingCache)
{
Progress = GetVerifyProgress();
if (UpdateVerifyingCache())
{
_steps = ESteps.Done;
@ -258,6 +259,7 @@ namespace YooAsset
private readonly List<PatchBundle> _verifyingList = new List<PatchBundle>(100);
private readonly ThreadSyncContext _syncContext = new ThreadSyncContext();
private int _verifyMaxNum = 32;
private int _verifyTotalCount = 0;
private int _verifySuccessCount = 0;
private int _verifyFailCount = 0;
@ -290,6 +292,7 @@ namespace YooAsset
ThreadPool.GetMaxThreads(out int workerThreads, out int ioThreads);
YooLogger.Log($"Work threads : {workerThreads}, IO threads : {ioThreads}");
_verifyMaxNum = Math.Min(workerThreads, ioThreads);
_verifyTotalCount = _waitingList.Count;
}
private bool UpdateVerifyingCache()
{
@ -358,6 +361,12 @@ namespace YooAsset
}
_verifyingList.Remove(info.Bundle);
}
private float GetVerifyProgress()
{
if (_verifyTotalCount == 0)
return 1f;
return (float)(_verifySuccessCount + _verifyFailCount) / _verifyTotalCount;
}
#endregion
}
}

View File

@ -111,6 +111,7 @@ namespace YooAsset
if (_steps == ESteps.CheckWebManifest)
{
Progress = _downloaderManifest.Progress();
if (_downloaderManifest.IsDone() == false)
return;

View File

@ -87,6 +87,7 @@ namespace YooAsset
if (_steps == ESteps.CheckStaticVersion)
{
Progress = _downloader.Progress();
if (_downloader.IsDone() == false)
return;
@ -105,7 +106,7 @@ namespace YooAsset
Status = EOperationStatus.Succeed;
}
else
{
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"URL : {_downloader.URL} Error : static version content is invalid.";