update
Some checks failed
Sync Github To Image / sync-gitlink (push) Has been cancelled
Sync Github To Image / sync-gitlab (push) Has been cancelled
Sync Github To Image / sync-gitee (push) Has been cancelled
Sync Github To Image / sync-atomgit (push) Has been cancelled
Sync Github To Image / sync-gitcode (push) Has been cancelled
Sync Github To Image / sync-framagit (push) Has been cancelled

This commit is contained in:
陈思海 2025-09-10 16:04:08 +08:00
parent 5d93024115
commit 5aca0bb9f6
2 changed files with 93 additions and 141 deletions

View File

@ -10,132 +10,110 @@ namespace Cysharp.Threading.Tasks
{ {
return ToUniTask(handle).GetAwaiter(); return ToUniTask(handle).GetAwaiter();
} }
public static UniTask ToUniTask(this AsyncOperationBase handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update)
public static UniTask ToUniTask(this AsyncOperationBase handle,
IProgress<float> progress = null,
PlayerLoopTiming timing = PlayerLoopTiming.Update)
{ {
ThrowArgumentNullException(handle, nameof(handle)); ThrowArgumentNullException(handle, nameof(handle));
if(handle.IsDone) if (handle.IsDone)
{ {
return UniTask.CompletedTask; return UniTask.CompletedTask;
} }
return new UniTask( return new UniTask(
AsyncOperationBaserConfiguredSource.Create( AsyncOperationBaserConfiguredSource.Create(handle, timing, progress, out var token),
handle,
timing,
progress,
out var token
),
token token
); );
} }
sealed class AsyncOperationBaserConfiguredSource : IUniTaskSource, sealed class AsyncOperationBaserConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<AsyncOperationBaserConfiguredSource>
IPlayerLoopItem,
ITaskPoolNode<AsyncOperationBaserConfiguredSource>
{ {
private static TaskPool<AsyncOperationBaserConfiguredSource> pool; private static TaskPool<AsyncOperationBaserConfiguredSource> _pool;
private AsyncOperationBaserConfiguredSource _nextNode;
private readonly Action<AsyncOperationBase> _continuationAction;
private AsyncOperationBase _handle;
private IProgress<float> _progress;
private bool _completed;
private UniTaskCompletionSourceCore<AsyncUnit> _core;
private AsyncOperationBaserConfiguredSource nextNode; public ref AsyncOperationBaserConfiguredSource NextNode => ref _nextNode;
public ref AsyncOperationBaserConfiguredSource NextNode => ref nextNode;
static AsyncOperationBaserConfiguredSource() static AsyncOperationBaserConfiguredSource()
{ {
TaskPool.RegisterSizeGetter(typeof(AsyncOperationBaserConfiguredSource), () => pool.Size); TaskPool.RegisterSizeGetter(typeof(AsyncOperationBaserConfiguredSource), () => _pool.Size);
} }
private readonly Action<AsyncOperationBase> continuationAction; AsyncOperationBaserConfiguredSource() { _continuationAction = Continuation; }
private AsyncOperationBase handle;
private IProgress<float> progress;
private bool completed;
private UniTaskCompletionSourceCore<AsyncUnit> core;
AsyncOperationBaserConfiguredSource() { continuationAction = Continuation; } public static IUniTaskSource Create(AsyncOperationBase handle, PlayerLoopTiming timing, IProgress<float> progress, out short token)
public static IUniTaskSource Create(AsyncOperationBase handle,
PlayerLoopTiming timing,
IProgress<float> progress,
out short token)
{ {
if(!pool.TryPop(out var result)) if (!_pool.TryPop(out var result))
{ {
result = new AsyncOperationBaserConfiguredSource(); result = new AsyncOperationBaserConfiguredSource();
} }
result.handle = handle; result._handle = handle;
result.progress = progress; result._progress = progress;
result.completed = false; result._completed = false;
TaskTracker.TrackActiveTask(result, 3); TaskTracker.TrackActiveTask(result, 3);
if(progress != null) if (progress != null)
{ {
PlayerLoopHelper.AddAction(timing, result); PlayerLoopHelper.AddAction(timing, result);
} }
handle.Completed += result.continuationAction; handle.Completed += result._continuationAction;
token = result._core.Version;
token = result.core.Version;
return result; return result;
} }
private void Continuation(AsyncOperationBase _) private void Continuation(AsyncOperationBase _)
{ {
handle.Completed -= continuationAction; _handle.Completed -= _continuationAction;
if(completed) if (_completed)
{ {
TryReturn(); TryReturn();
} }
else else
{ {
completed = true; _completed = true;
if(handle.Status == EOperationStatus.Failed) if (_handle.Status == EOperationStatus.Failed)
{ {
core.TrySetException(new Exception(handle.Error)); _core.TrySetException(new Exception(_handle.Error));
} }
else else
{ {
core.TrySetResult(AsyncUnit.Default); _core.TrySetResult(AsyncUnit.Default);
} }
} }
} }
private bool TryReturn()
bool TryReturn()
{ {
TaskTracker.RemoveTracking(this); TaskTracker.RemoveTracking(this);
core.Reset(); _core.Reset();
handle = default; _handle = default;
progress = default; _progress = default;
return pool.TryPush(this); return _pool.TryPush(this);
} }
public UniTaskStatus GetStatus(short token) => core.GetStatus(token); public UniTaskStatus GetStatus(short token) => _core.GetStatus(token);
public void OnCompleted(Action<object> continuation, object state, short token) public void OnCompleted(Action<object> continuation, object state, short token)
{ {
core.OnCompleted(continuation, state, token); _core.OnCompleted(continuation, state, token);
} }
public void GetResult(short token) { _core.GetResult(token); }
public void GetResult(short token) { core.GetResult(token); } public UniTaskStatus UnsafeGetStatus() => _core.UnsafeGetStatus();
public UniTaskStatus UnsafeGetStatus() => core.UnsafeGetStatus();
public bool MoveNext() public bool MoveNext()
{ {
if(completed) if (_completed)
{ {
TryReturn(); TryReturn();
return false; return false;
} }
if(!handle.IsDone) if (!_handle.IsDone)
{ {
progress?.Report(handle.Progress); _progress?.Report(_handle.Progress);
} }
return true; return true;

View File

@ -7,77 +7,61 @@ using System.Runtime.CompilerServices;
using YooAsset; using YooAsset;
using static Cysharp.Threading.Tasks.Internal.Error; using static Cysharp.Threading.Tasks.Internal.Error;
namespace Cysharp.Threading.Tasks namespace Cysharp.Threading.Tasks
{ {
public static class OperationHandleBaseExtensions public static class HandleBaseExtensions
{ {
public static UniTask.Awaiter GetAwaiter(this HandleBase handle) public static UniTask.Awaiter GetAwaiter(this HandleBase handle)
{ {
return ToUniTask(handle).GetAwaiter(); return ToUniTask(handle).GetAwaiter();
} }
public static UniTask ToUniTask(this HandleBase handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update)
public static UniTask ToUniTask(this HandleBase handle,
IProgress<float> progress = null,
PlayerLoopTiming timing = PlayerLoopTiming.Update)
{ {
ThrowArgumentNullException(handle, nameof(handle)); ThrowArgumentNullException(handle, nameof(handle));
if(!handle.IsValid) if (!handle.IsValid)
{ {
return UniTask.CompletedTask; return UniTask.CompletedTask;
} }
return new UniTask( return new UniTask(
OperationHandleBaserConfiguredSource.Create( HandleBaserConfiguredSource.Create(handle, timing, progress, out var token),
handle,
timing,
progress,
out var token
),
token token
); );
} }
sealed class OperationHandleBaserConfiguredSource : IUniTaskSource, sealed class HandleBaserConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<HandleBaserConfiguredSource>
IPlayerLoopItem,
ITaskPoolNode<OperationHandleBaserConfiguredSource>
{ {
private static TaskPool<OperationHandleBaserConfiguredSource> pool; private static TaskPool<HandleBaserConfiguredSource> _pool;
private HandleBaserConfiguredSource _nextNode;
private readonly Action<HandleBase> _continuationAction;
private HandleBase _handle;
private IProgress<float> _progress;
private bool _completed;
private UniTaskCompletionSourceCore<AsyncUnit> _core;
private OperationHandleBaserConfiguredSource nextNode; public ref HandleBaserConfiguredSource NextNode => ref _nextNode;
public ref OperationHandleBaserConfiguredSource NextNode => ref nextNode; static HandleBaserConfiguredSource()
static OperationHandleBaserConfiguredSource()
{ {
TaskPool.RegisterSizeGetter(typeof(OperationHandleBaserConfiguredSource), () => pool.Size); TaskPool.RegisterSizeGetter(typeof(HandleBaserConfiguredSource), () => _pool.Size);
} }
private readonly Action<HandleBase> continuationAction; HandleBaserConfiguredSource() { _continuationAction = Continuation; }
private HandleBase handle;
private IProgress<float> progress;
private bool completed;
private UniTaskCompletionSourceCore<AsyncUnit> core;
OperationHandleBaserConfiguredSource() { continuationAction = Continuation; } public static IUniTaskSource Create(HandleBase handle, PlayerLoopTiming timing, IProgress<float> progress, out short token)
public static IUniTaskSource Create(HandleBase handle,
PlayerLoopTiming timing,
IProgress<float> progress,
out short token)
{ {
if(!pool.TryPop(out var result)) if (!_pool.TryPop(out var result))
{ {
result = new OperationHandleBaserConfiguredSource(); result = new HandleBaserConfiguredSource();
} }
result.handle = handle; result._handle = handle;
result.progress = progress; result._progress = progress;
result.completed = false; result._completed = false;
TaskTracker.TrackActiveTask(result, 3); TaskTracker.TrackActiveTask(result, 3);
if(progress != null) if (progress != null)
{ {
PlayerLoopHelper.AddAction(timing, result); PlayerLoopHelper.AddAction(timing, result);
} }
@ -87,7 +71,7 @@ namespace Cysharp.Threading.Tasks
// BUG 也可能报的是 Action '1' Action '1' 的 InvalidCastException // BUG 也可能报的是 Action '1' Action '1' 的 InvalidCastException
// BUG 此处不得不这么修改, 如果后续 Unity 修复了这个问题, 可以恢复之前的写法 // BUG 此处不得不这么修改, 如果后续 Unity 修复了这个问题, 可以恢复之前的写法
#if UNITY_2020_BUG #if UNITY_2020_BUG
switch(handle) switch (handle)
{ {
case AssetHandle asset_handle: case AssetHandle asset_handle:
asset_handle.Completed += result.AssetContinuation; asset_handle.Completed += result.AssetContinuation;
@ -125,37 +109,33 @@ namespace Cysharp.Threading.Tasks
break; break;
} }
#endif #endif
token = result.core.Version; token = result._core.Version;
return result; return result;
} }
#if UNITY_2020_BUG #if UNITY_2020_BUG
private void AssetContinuation(AssetHandle handle) private void AssetContinuation(AssetHandle handle)
{ {
handle.Completed -= AssetContinuation; handle.Completed -= AssetContinuation;
BaseContinuation(); BaseContinuation();
} }
private void SceneContinuation(SceneHandle handle) private void SceneContinuation(SceneHandle handle)
{ {
handle.Completed -= SceneContinuation; handle.Completed -= SceneContinuation;
BaseContinuation(); BaseContinuation();
} }
private void SubContinuation(SubAssetsHandle handle) private void SubContinuation(SubAssetsHandle handle)
{ {
handle.Completed -= SubContinuation; handle.Completed -= SubContinuation;
BaseContinuation(); BaseContinuation();
} }
private void RawFileContinuation(RawFileHandle handle) private void RawFileContinuation(RawFileHandle handle)
{ {
handle.Completed -= RawFileContinuation; handle.Completed -= RawFileContinuation;
BaseContinuation(); BaseContinuation();
} }
private void AllAssetsContinuation(AllAssetsHandle handle) private void AllAssetsContinuation(AllAssetsHandle handle)
{ {
handle.Completed -= AllAssetsContinuation; handle.Completed -= AllAssetsContinuation;
BaseContinuation(); BaseContinuation();
} }
@ -163,79 +143,73 @@ namespace Cysharp.Threading.Tasks
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void BaseContinuation() private void BaseContinuation()
{ {
if(completed) if (_completed)
{ {
TryReturn(); TryReturn();
} }
else else
{ {
completed = true; _completed = true;
if(handle.Status == EOperationStatus.Failed) if (_handle.Status == EOperationStatus.Failed)
{ {
core.TrySetException(new Exception(handle.LastError)); _core.TrySetException(new Exception(_handle.LastError));
} }
else else
{ {
core.TrySetResult(AsyncUnit.Default); _core.TrySetResult(AsyncUnit.Default);
} }
} }
} }
private void Continuation(HandleBase _) private void Continuation(HandleBase _)
{ {
switch(handle) switch (_handle)
{ {
case AssetHandle asset_handle: case AssetHandle asset_handle:
asset_handle.Completed -= continuationAction; asset_handle.Completed -= _continuationAction;
break; break;
case SceneHandle scene_handle: case SceneHandle scene_handle:
scene_handle.Completed -= continuationAction; scene_handle.Completed -= _continuationAction;
break; break;
case SubAssetsHandle sub_asset_handle: case SubAssetsHandle sub_asset_handle:
sub_asset_handle.Completed -= continuationAction; sub_asset_handle.Completed -= _continuationAction;
break; break;
case RawFileHandle raw_file_handle: case RawFileHandle raw_file_handle:
raw_file_handle.Completed -= continuationAction; raw_file_handle.Completed -= _continuationAction;
break; break;
case AllAssetsHandle all_assets_handle: case AllAssetsHandle all_assets_handle:
all_assets_handle.Completed -= continuationAction; all_assets_handle.Completed -= _continuationAction;
break; break;
} }
BaseContinuation(); BaseContinuation();
} }
private bool TryReturn()
bool TryReturn()
{ {
TaskTracker.RemoveTracking(this); TaskTracker.RemoveTracking(this);
core.Reset(); _core.Reset();
handle = default; _handle = default;
progress = default; _progress = default;
return pool.TryPush(this); return _pool.TryPush(this);
} }
public UniTaskStatus GetStatus(short token) => core.GetStatus(token); public UniTaskStatus GetStatus(short token) => _core.GetStatus(token);
public void OnCompleted(Action<object> continuation, object state, short token) public void OnCompleted(Action<object> continuation, object state, short token)
{ {
core.OnCompleted(continuation, state, token); _core.OnCompleted(continuation, state, token);
} }
public void GetResult(short token) { _core.GetResult(token); }
public void GetResult(short token) { core.GetResult(token); } public UniTaskStatus UnsafeGetStatus() => _core.UnsafeGetStatus();
public UniTaskStatus UnsafeGetStatus() => core.UnsafeGetStatus();
public bool MoveNext() public bool MoveNext()
{ {
if(completed) if (_completed)
{ {
TryReturn(); TryReturn();
return false; return false;
} }
if(handle.IsValid) if (_handle.IsValid)
{ {
progress?.Report(handle.Progress); _progress?.Report(_handle.Progress);
} }
return true; return true;