From ccffb121b37ea225434175b857db60e673664ba2 Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Sun, 5 Apr 2026 15:18:29 +0800
Subject: [PATCH] =?UTF-8?q?fix(coroutine):=20=E4=BF=AE=E5=A4=8D=E5=8D=8F?=
=?UTF-8?q?=E7=A8=8B=E5=AE=8C=E6=88=90=E7=8A=B6=E6=80=81=E5=A4=84=E7=90=86?=
=?UTF-8?q?=E4=B8=AD=E7=9A=84=E5=BC=82=E5=B8=B8=E6=83=85=E5=86=B5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 为 CoroutineCompletionStatus 枚举添加默认分支处理
- 抛出 ArgumentOutOfRangeException 以处理不支持的协程完成状态
- 防止因未知状态值导致的运行时错误
- 提高协程调度器的健壮性和错误处理能力
---
.../Coroutine/CoroutineScheduler.cs | 30 ++++++++++---------
1 file changed, 16 insertions(+), 14 deletions(-)
diff --git a/GFramework.Core/Coroutine/CoroutineScheduler.cs b/GFramework.Core/Coroutine/CoroutineScheduler.cs
index 0aeb31f2..05fa1f06 100644
--- a/GFramework.Core/Coroutine/CoroutineScheduler.cs
+++ b/GFramework.Core/Coroutine/CoroutineScheduler.cs
@@ -140,20 +140,16 @@ public sealed class CoroutineScheduler(
/// 包含所有活跃协程的快照列表。
public IReadOnlyList GetActiveSnapshots()
{
- var snapshots = new List(_metadata.Count);
-
- foreach (var pair in _metadata)
- {
- var slot = _slots[pair.Value.SlotIndex];
- if (slot == null)
+ return _metadata
+ .Select(pair => pair.Value)
+ .Select(meta => new
{
- continue;
- }
-
- snapshots.Add(CreateSnapshot(pair.Value, slot));
- }
-
- return snapshots;
+ Metadata = meta,
+ Slot = _slots[meta.SlotIndex]
+ })
+ .Where(item => item.Slot is not null)
+ .Select(item => CreateSnapshot(item.Metadata, item.Slot!))
+ .ToArray();
}
///
@@ -689,6 +685,12 @@ public sealed class CoroutineScheduler(
case CoroutineCompletionStatus.Cancelled:
meta.State = CoroutineState.Cancelled;
break;
+
+ default:
+ throw new ArgumentOutOfRangeException(
+ nameof(completionStatus),
+ completionStatus,
+ "Unsupported coroutine completion status.");
}
}
@@ -855,7 +857,7 @@ public sealed class CoroutineScheduler(
/// 协程元数据。
/// 协程槽位。
/// 与当前槽位一致的只读快照。
- private CoroutineSnapshot CreateSnapshot(CoroutineMetadata metadata, CoroutineSlot slot)
+ private static CoroutineSnapshot CreateSnapshot(CoroutineMetadata metadata, CoroutineSlot slot)
{
return new CoroutineSnapshot(
slot.Handle,