refactor : 重构代码
This commit is contained in:
@@ -10,17 +10,22 @@ namespace YooAsset
|
||||
{
|
||||
private List<AsyncOperationBase> _childs;
|
||||
private Action<AsyncOperationBase> _callback;
|
||||
private uint _priority = 0;
|
||||
private uint _priority;
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步执行完成
|
||||
/// </summary>
|
||||
internal bool IsWaitForAsyncComplete { private set; get; } = false;
|
||||
internal bool IsWaitForAsyncComplete { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 标记脏(用于调度器检测并重排)
|
||||
/// </summary>
|
||||
internal bool IsDirty { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经完成
|
||||
/// </summary>
|
||||
internal bool IsFinish { private set; get; } = false;
|
||||
internal bool IsFinish { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 异步系统是否繁忙
|
||||
@@ -35,11 +40,6 @@ namespace YooAsset
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标记脏(用于调度器检测并重排)
|
||||
/// </summary>
|
||||
internal bool IsDirty { set; get; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 任务优先级
|
||||
/// </summary>
|
||||
@@ -98,6 +98,7 @@ namespace YooAsset
|
||||
{
|
||||
try
|
||||
{
|
||||
//注意:任务已完成,立即调用回调
|
||||
value.Invoke(this);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -295,21 +296,25 @@ namespace YooAsset
|
||||
// 结束记录
|
||||
DebugEndRecording();
|
||||
|
||||
try
|
||||
if (_callback != null)
|
||||
{
|
||||
//TODO 单个回调异常会阻断后续订阅者
|
||||
_callback?.Invoke(this);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
YooLogger.Error($"Exception in completion callback: {ex}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_callback = null;
|
||||
if (_taskCompletionSource != null)
|
||||
_taskCompletionSource.TrySetResult(null);
|
||||
var invocationList = _callback.GetInvocationList();
|
||||
foreach (var handler in invocationList)
|
||||
{
|
||||
try
|
||||
{
|
||||
((Action<AsyncOperationBase>)handler).Invoke(this);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
YooLogger.Error($"Exception in completion callback: {ex}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_callback = null;
|
||||
if (_taskCompletionSource != null)
|
||||
_taskCompletionSource.TrySetResult(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,7 +332,10 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 批量执行一定次数的更新逻辑
|
||||
/// </summary>
|
||||
/// <param name="count">次数</param>
|
||||
/// <param name="count">最大执行次数,默认1000次</param>
|
||||
/// <remarks>
|
||||
/// 用于需要快速完成但又不想完全阻塞主线程的场景。
|
||||
/// </remarks>
|
||||
protected void RunBatchExecution(int count = 1000)
|
||||
{
|
||||
if (IsDone)
|
||||
@@ -374,7 +382,7 @@ namespace YooAsset
|
||||
/// </summary>
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
//TODO 防止异步操作被挂起陷入无限死循环!
|
||||
//注意:防止异步操作被挂起陷入无限死循环!
|
||||
if (Status == EOperationStatus.None)
|
||||
{
|
||||
StartOperation();
|
||||
@@ -400,15 +408,17 @@ namespace YooAsset
|
||||
}
|
||||
|
||||
#region 调试信息
|
||||
private const int MaxCycleCheckDepth = 4096; // 循环检测最大深度
|
||||
|
||||
/// <summary>
|
||||
/// 开始的时间
|
||||
/// </summary>
|
||||
public string BeginTime { protected set; get; }
|
||||
public string BeginTime { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// 处理耗时(单位:毫秒)
|
||||
/// </summary>
|
||||
public long ProcessTime { protected set; get; }
|
||||
public long ProcessTime { get; protected set; }
|
||||
|
||||
// 加载耗时统计
|
||||
private Stopwatch _watch = null;
|
||||
@@ -449,9 +459,13 @@ namespace YooAsset
|
||||
double s = System.Math.Floor(spawnTime - m * 60 - h * 3600);
|
||||
return h.ToString("00") + ":" + m.ToString("00") + ":" + s.ToString("00");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检测添加子任务是否会形成循环依赖
|
||||
/// 使用深度优先搜索(DFS)遍历子任务图
|
||||
/// </summary>
|
||||
private bool WouldCreateCycle(AsyncOperationBase child)
|
||||
{
|
||||
const int maxVisited = 4096;
|
||||
var stack = new Stack<AsyncOperationBase>();
|
||||
var visited = new HashSet<AsyncOperationBase>();
|
||||
stack.Push(child);
|
||||
@@ -462,18 +476,22 @@ namespace YooAsset
|
||||
if (node == null)
|
||||
continue;
|
||||
|
||||
// 防止重复访问
|
||||
if (visited.Add(node) == false)
|
||||
continue;
|
||||
|
||||
if (visited.Count > maxVisited)
|
||||
// 防止无限循环(图过大)
|
||||
if (visited.Count > MaxCycleCheckDepth)
|
||||
throw new YooInternalException("Child operation graph is too large, cycle check aborted !");
|
||||
|
||||
// 检测循环:如果遍历到自己,说明形成循环
|
||||
if (ReferenceEquals(node, this))
|
||||
return true;
|
||||
|
||||
if (node._childs == null)
|
||||
continue;
|
||||
|
||||
// 将子节点加入栈
|
||||
for (int i = 0; i < node._childs.Count; i++)
|
||||
{
|
||||
stack.Push(node._childs[i]);
|
||||
|
||||
Reference in New Issue
Block a user