refactor : 重构异步操作模块

This commit is contained in:
何冠峰
2026-01-12 16:10:24 +08:00
parent 294fa18fec
commit 7198e639d9
4 changed files with 57 additions and 20 deletions

View File

@@ -155,9 +155,19 @@ namespace YooAsset
if (_childs == null) if (_childs == null)
_childs = new List<AsyncOperationBase>(10); _childs = new List<AsyncOperationBase>(10);
#if UNITY_EDITOR #if UNITY_EDITOR || DEBUG
if (child == null)
throw new YooInternalException("The child node is null !");
if (ReferenceEquals(child, this))
throw new YooInternalException("The child node cannot be itself !");
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 !");
// 禁止形成环依赖
if (WouldCreateCycle(child))
throw new YooInternalException($"AddChildOperation would create a cycle : {this.GetType().Name} -> {child.GetType().Name}");
#endif #endif
_childs.Add(child); _childs.Add(child);
@@ -171,7 +181,10 @@ namespace YooAsset
if (_childs == null) if (_childs == null)
return; return;
#if UNITY_EDITOR #if UNITY_EDITOR || DEBUG
if (child == null)
throw new YooInternalException("The child node is null !");
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
@@ -349,10 +362,10 @@ namespace YooAsset
if (IsWaitForAsyncComplete == false) if (IsWaitForAsyncComplete == false)
{ {
IsWaitForAsyncComplete = true; IsWaitForAsyncComplete = true;
if (IsDone == false) if (IsDone == false)
InternalWaitForAsyncComplete(); InternalWaitForAsyncComplete();
if (IsDone == false) if (IsDone == false)
{ {
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
@@ -415,6 +428,39 @@ namespace YooAsset
float s = UnityEngine.Mathf.FloorToInt(spawnTime - m * 60f - h * 3600f); float s = UnityEngine.Mathf.FloorToInt(spawnTime - m * 60f - h * 3600f);
return h.ToString("00") + ":" + m.ToString("00") + ":" + s.ToString("00"); return h.ToString("00") + ":" + m.ToString("00") + ":" + s.ToString("00");
} }
private bool WouldCreateCycle(AsyncOperationBase child)
{
const int maxVisited = 4096;
var stack = new Stack<AsyncOperationBase>();
var visited = new HashSet<AsyncOperationBase>();
stack.Push(child);
while (stack.Count > 0)
{
var node = stack.Pop();
if (node == null)
continue;
if (visited.Add(node) == false)
continue;
if (visited.Count > maxVisited)
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]);
}
}
return false;
}
internal DebugOperationInfo GetDebugOperationInfo() internal DebugOperationInfo GetDebugOperationInfo()
{ {

View File

@@ -97,6 +97,8 @@ namespace YooAsset
/// </summary> /// </summary>
public static void DestroyAll() public static void DestroyAll()
{ {
YooLogger.Log("Operation system destroy all !");
// 清空所有调度器 // 清空所有调度器
foreach (var scheduler in _schedulerList) foreach (var scheduler in _schedulerList)
{ {
@@ -177,7 +179,7 @@ namespace YooAsset
} }
// 严格模式:非默认包裹必须先创建调度器 // 严格模式:非默认包裹必须先创建调度器
throw new YooInternalException($"Package scheduler not found: {packageName}. Please call YooAssets.CreatePackage() first!"); throw new YooInternalException($"Operation scheduler not found: {packageName}. Please call YooAssets.CreatePackage() first!");
} }
#region #region

View File

@@ -59,6 +59,7 @@ namespace YooAsset
_driver.AddComponent<RemoteDebuggerInRuntime>(); _driver.AddComponent<RemoteDebuggerInRuntime>();
#endif #endif
// 初始化异步操作系统
OperationSystem.Initialize(); OperationSystem.Initialize();
} }
} }
@@ -75,8 +76,8 @@ namespace YooAsset
if (_driver != null) if (_driver != null)
GameObject.Destroy(_driver); GameObject.Destroy(_driver);
// 终止并清空所有包裹的异步操作 // 销毁异步操作系统
ClearAllPackageOperation(); OperationSystem.DestroyAll();
// 卸载所有AssetBundle // 卸载所有AssetBundle
AssetBundle.UnloadAllAssetBundles(true); AssetBundle.UnloadAllAssetBundles(true);
@@ -97,18 +98,6 @@ namespace YooAsset
} }
} }
/// <summary>
/// 终止并清空所有包裹的异步操作
/// </summary>
internal static void ClearAllPackageOperation()
{
foreach (var package in _packages)
{
OperationSystem.ClearPackageOperation(package.PackageName);
}
OperationSystem.DestroyAll();
}
/// <summary> /// <summary>
/// 创建资源包裹 /// 创建资源包裹
/// </summary> /// </summary>

View File

@@ -25,7 +25,7 @@ namespace YooAsset
void OnApplicationQuit() void OnApplicationQuit()
{ {
// 说明在编辑器下确保播放被停止时IO类操作被终止。 // 说明在编辑器下确保播放被停止时IO类操作被终止。
YooAssets.ClearAllPackageOperation(); YooAssets.Destroy();
} }
#endif #endif