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