mirror of
https://github.com/tuyoogame/YooAsset.git
synced 2026-05-14 19:40:47 +00:00
refactor : 重构异步操作模块
This commit is contained in:
@@ -47,7 +47,7 @@ namespace YooAsset
|
||||
string bundleGUID = _allBundleGUIDs[i];
|
||||
_fileSystem.DeleteCacheBundleFile(bundleGUID);
|
||||
_allBundleGUIDs.RemoveAt(i);
|
||||
if (OperationSystem.IsBusy)
|
||||
if (IsBusy)
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ namespace YooAsset
|
||||
string bundleGUID = _clearBundleGUIDs[i];
|
||||
_fileSystem.DeleteCacheBundleFile(bundleGUID);
|
||||
_clearBundleGUIDs.RemoveAt(i);
|
||||
if (OperationSystem.IsBusy)
|
||||
if (IsBusy)
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -99,7 +99,7 @@ namespace YooAsset
|
||||
string bundleGUID = _clearBundleGUIDs[i];
|
||||
_fileSystem.DeleteCacheBundleFile(bundleGUID);
|
||||
_clearBundleGUIDs.RemoveAt(i);
|
||||
if (OperationSystem.IsBusy)
|
||||
if (IsBusy)
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ namespace YooAsset
|
||||
string bundleGUID = _unusedBundleGUIDs[i];
|
||||
_fileSystem.DeleteCacheBundleFile(bundleGUID);
|
||||
_unusedBundleGUIDs.RemoveAt(i);
|
||||
if (OperationSystem.IsBusy)
|
||||
if (IsBusy)
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ namespace YooAsset
|
||||
Result.Add(element);
|
||||
}
|
||||
|
||||
if (OperationSystem.IsBusy)
|
||||
if (IsBusy)
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -90,7 +90,7 @@ namespace YooAsset
|
||||
|
||||
for (int i = _waitingList.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (OperationSystem.IsBusy)
|
||||
if (IsBusy)
|
||||
break;
|
||||
|
||||
if (_verifyingList.Count >= _verifyMaxNum)
|
||||
|
||||
@@ -8,14 +8,10 @@ namespace YooAsset
|
||||
{
|
||||
public abstract class AsyncOperationBase : IEnumerator, IComparable<AsyncOperationBase>
|
||||
{
|
||||
private List<AsyncOperationBase> _childs;
|
||||
private Action<AsyncOperationBase> _callback;
|
||||
private int _whileFrame = 1000;
|
||||
|
||||
/// <summary>
|
||||
/// 所有子任务
|
||||
/// </summary>
|
||||
internal readonly List<AsyncOperationBase> Childs = new List<AsyncOperationBase>(10);
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步执行完成
|
||||
/// </summary>
|
||||
@@ -26,6 +22,19 @@ namespace YooAsset
|
||||
/// </summary>
|
||||
internal bool IsFinish { private set; get; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 异步系统是否繁忙
|
||||
/// </summary>
|
||||
internal bool IsBusy
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
return false;
|
||||
return OperationSystem.IsBusy;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 任务优先级
|
||||
/// </summary>
|
||||
@@ -125,12 +134,15 @@ namespace YooAsset
|
||||
/// </summary>
|
||||
internal void AddChildOperation(AsyncOperationBase child)
|
||||
{
|
||||
if (_childs == null)
|
||||
_childs = new List<AsyncOperationBase>(10);
|
||||
|
||||
#if UNITY_EDITOR
|
||||
if (Childs.Contains(child))
|
||||
if (_childs.Contains(child))
|
||||
throw new YooInternalException($"The child node {child.GetType().Name} already exists !");
|
||||
#endif
|
||||
|
||||
Childs.Add(child);
|
||||
_childs.Add(child);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -138,12 +150,15 @@ namespace YooAsset
|
||||
/// </summary>
|
||||
internal void RemoveChildOperation(AsyncOperationBase child)
|
||||
{
|
||||
if (_childs == null)
|
||||
return;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
if (Childs.Contains(child) == false)
|
||||
if (_childs.Contains(child) == false)
|
||||
throw new YooInternalException($"The child node {child.GetType().Name} not exists !");
|
||||
#endif
|
||||
|
||||
Childs.Remove(child);
|
||||
_childs.Remove(child);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -187,25 +202,7 @@ namespace YooAsset
|
||||
|
||||
if (IsDone && IsFinish == false)
|
||||
{
|
||||
IsFinish = true;
|
||||
Progress = 1f;
|
||||
|
||||
// 结束记录
|
||||
DebugEndRecording();
|
||||
|
||||
try
|
||||
{
|
||||
_callback?.Invoke(this);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
YooLogger.Error($"Exception in completion callback: {ex}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (_taskCompletionSource != null)
|
||||
_taskCompletionSource.TrySetResult(null);
|
||||
}
|
||||
FinishOperation();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,9 +211,12 @@ namespace YooAsset
|
||||
/// </summary>
|
||||
internal void AbortOperation()
|
||||
{
|
||||
foreach (var child in Childs)
|
||||
if (_childs != null)
|
||||
{
|
||||
child.AbortOperation();
|
||||
foreach (var child in _childs)
|
||||
{
|
||||
child.AbortOperation();
|
||||
}
|
||||
}
|
||||
|
||||
if (IsDone == false)
|
||||
@@ -241,8 +241,20 @@ namespace YooAsset
|
||||
// 结束记录
|
||||
DebugEndRecording();
|
||||
|
||||
if (_taskCompletionSource != null)
|
||||
_taskCompletionSource.TrySetResult(null);
|
||||
try
|
||||
{
|
||||
_callback?.Invoke(this);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
YooLogger.Error($"Exception in completion callback: {ex}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_callback = null;
|
||||
if (_taskCompletionSource != null)
|
||||
_taskCompletionSource.TrySetResult(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,14 +280,6 @@ namespace YooAsset
|
||||
return IsDone;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空完成回调
|
||||
/// </summary>
|
||||
protected void ClearCompletedCallback()
|
||||
{
|
||||
_callback = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步执行完毕
|
||||
/// </summary>
|
||||
@@ -295,6 +299,11 @@ namespace YooAsset
|
||||
{
|
||||
IsWaitForAsyncComplete = true;
|
||||
InternalWaitForAsyncComplete();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
if (IsDone == false)
|
||||
throw new YooInternalException($"WaitForAsyncComplete() must complete operation: {this.GetType().Name}");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -359,12 +368,21 @@ namespace YooAsset
|
||||
operationInfo.BeginTime = BeginTime;
|
||||
operationInfo.ProcessTime = ProcessTime;
|
||||
operationInfo.Status = Status.ToString();
|
||||
operationInfo.Childs = new List<DebugOperationInfo>(Childs.Count);
|
||||
foreach (var child in Childs)
|
||||
|
||||
if (_childs == null)
|
||||
{
|
||||
var childInfo = child.GetDebugOperationInfo();
|
||||
operationInfo.Childs.Add(childInfo);
|
||||
operationInfo.Childs = new List<DebugOperationInfo>();
|
||||
}
|
||||
else
|
||||
{
|
||||
operationInfo.Childs = new List<DebugOperationInfo>(_childs.Count);
|
||||
foreach (var child in _childs)
|
||||
{
|
||||
var childInfo = child.GetDebugOperationInfo();
|
||||
operationInfo.Childs.Add(childInfo);
|
||||
}
|
||||
}
|
||||
|
||||
return operationInfo;
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -11,14 +11,6 @@ namespace YooAsset
|
||||
{
|
||||
OnUpdate();
|
||||
}
|
||||
internal override void InternalAbort()
|
||||
{
|
||||
OnAbort();
|
||||
}
|
||||
internal override void InternalWaitForAsyncComplete()
|
||||
{
|
||||
OnWaitForAsyncComplete();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步操作开始
|
||||
@@ -29,31 +21,5 @@ namespace YooAsset
|
||||
/// 异步操作更新
|
||||
/// </summary>
|
||||
protected abstract void OnUpdate();
|
||||
|
||||
/// <summary>
|
||||
/// 异步操作终止
|
||||
/// </summary>
|
||||
protected abstract void OnAbort();
|
||||
|
||||
/// <summary>
|
||||
/// 异步等待完成
|
||||
/// </summary>
|
||||
protected virtual void OnWaitForAsyncComplete() { }
|
||||
|
||||
/// <summary>
|
||||
/// 异步操作系统是否繁忙
|
||||
/// </summary>
|
||||
protected bool IsBusy()
|
||||
{
|
||||
return OperationSystem.IsBusy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 终止异步操作
|
||||
/// </summary>
|
||||
protected void Abort()
|
||||
{
|
||||
AbortOperation();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,11 +40,8 @@ namespace YooAsset
|
||||
_loopCounter--;
|
||||
LoopUnloadUnused();
|
||||
|
||||
if (IsWaitForAsyncComplete == false)
|
||||
{
|
||||
if (OperationSystem.IsBusy)
|
||||
break;
|
||||
}
|
||||
if (IsBusy)
|
||||
break;
|
||||
}
|
||||
|
||||
if (_loopCounter <= 0)
|
||||
|
||||
@@ -159,11 +159,8 @@ namespace YooAsset
|
||||
|
||||
_packageAssetCount--;
|
||||
Progress = 1f - _packageAssetCount / _progressTotalValue;
|
||||
if (IsWaitForAsyncComplete == false)
|
||||
{
|
||||
if (OperationSystem.IsBusy)
|
||||
break;
|
||||
}
|
||||
if (IsBusy)
|
||||
break;
|
||||
}
|
||||
|
||||
if (_packageAssetCount <= 0)
|
||||
@@ -196,11 +193,8 @@ namespace YooAsset
|
||||
|
||||
_packageBundleCount--;
|
||||
Progress = 1f - _packageBundleCount / _progressTotalValue;
|
||||
if (IsWaitForAsyncComplete == false)
|
||||
{
|
||||
if (OperationSystem.IsBusy)
|
||||
break;
|
||||
}
|
||||
if (IsBusy)
|
||||
break;
|
||||
}
|
||||
|
||||
if (_packageBundleCount <= 0)
|
||||
|
||||
Reference in New Issue
Block a user