com.alicizax.unity.cysharp..../Runtime/External/YooAsset/AsyncOperationBaseExtensions.cs

124 lines
4.2 KiB
C#
Raw Permalink Normal View History

2025-02-28 16:08:26 +08:00
using System;
using YooAsset;
using static Cysharp.Threading.Tasks.Internal.Error;
namespace Cysharp.Threading.Tasks
{
public static class AsyncOperationBaseExtensions
{
public static UniTask.Awaiter GetAwaiter(this AsyncOperationBase handle)
{
return ToUniTask(handle).GetAwaiter();
}
2025-09-10 16:04:08 +08:00
public static UniTask ToUniTask(this AsyncOperationBase handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update)
2025-02-28 16:08:26 +08:00
{
ThrowArgumentNullException(handle, nameof(handle));
2025-09-10 16:04:08 +08:00
if (handle.IsDone)
2025-02-28 16:08:26 +08:00
{
return UniTask.CompletedTask;
}
return new UniTask(
2025-09-10 16:04:08 +08:00
AsyncOperationBaserConfiguredSource.Create(handle, timing, progress, out var token),
2025-02-28 16:08:26 +08:00
token
);
}
2025-09-10 16:04:08 +08:00
sealed class AsyncOperationBaserConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<AsyncOperationBaserConfiguredSource>
2025-02-28 16:08:26 +08:00
{
2025-09-10 16:04:08 +08:00
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;
2025-02-28 16:08:26 +08:00
2025-09-10 16:04:08 +08:00
public ref AsyncOperationBaserConfiguredSource NextNode => ref _nextNode;
2025-02-28 16:08:26 +08:00
static AsyncOperationBaserConfiguredSource()
{
2025-09-10 16:04:08 +08:00
TaskPool.RegisterSizeGetter(typeof(AsyncOperationBaserConfiguredSource), () => _pool.Size);
2025-02-28 16:08:26 +08:00
}
2025-09-10 16:04:08 +08:00
AsyncOperationBaserConfiguredSource() { _continuationAction = Continuation; }
2025-02-28 16:08:26 +08:00
2025-09-10 16:04:08 +08:00
public static IUniTaskSource Create(AsyncOperationBase handle, PlayerLoopTiming timing, IProgress<float> progress, out short token)
2025-02-28 16:08:26 +08:00
{
2025-09-10 16:04:08 +08:00
if (!_pool.TryPop(out var result))
2025-02-28 16:08:26 +08:00
{
result = new AsyncOperationBaserConfiguredSource();
}
2025-09-10 16:04:08 +08:00
result._handle = handle;
result._progress = progress;
result._completed = false;
2025-02-28 16:08:26 +08:00
TaskTracker.TrackActiveTask(result, 3);
2025-09-10 16:04:08 +08:00
if (progress != null)
2025-02-28 16:08:26 +08:00
{
PlayerLoopHelper.AddAction(timing, result);
}
2025-09-10 16:04:08 +08:00
handle.Completed += result._continuationAction;
token = result._core.Version;
2025-02-28 16:08:26 +08:00
return result;
}
private void Continuation(AsyncOperationBase _)
{
2025-09-10 16:04:08 +08:00
_handle.Completed -= _continuationAction;
2025-02-28 16:08:26 +08:00
2025-09-10 16:04:08 +08:00
if (_completed)
2025-02-28 16:08:26 +08:00
{
TryReturn();
}
else
{
2025-09-10 16:04:08 +08:00
_completed = true;
if (_handle.Status == EOperationStatus.Failed)
2025-02-28 16:08:26 +08:00
{
2025-09-10 16:04:08 +08:00
_core.TrySetException(new Exception(_handle.Error));
2025-02-28 16:08:26 +08:00
}
else
{
2025-09-10 16:04:08 +08:00
_core.TrySetResult(AsyncUnit.Default);
2025-02-28 16:08:26 +08:00
}
}
}
2025-09-10 16:04:08 +08:00
private bool TryReturn()
2025-02-28 16:08:26 +08:00
{
TaskTracker.RemoveTracking(this);
2025-09-10 16:04:08 +08:00
_core.Reset();
_handle = default;
_progress = default;
return _pool.TryPush(this);
2025-02-28 16:08:26 +08:00
}
2025-09-10 16:04:08 +08:00
public UniTaskStatus GetStatus(short token) => _core.GetStatus(token);
2025-02-28 16:08:26 +08:00
public void OnCompleted(Action<object> continuation, object state, short token)
{
2025-09-10 16:04:08 +08:00
_core.OnCompleted(continuation, state, token);
2025-02-28 16:08:26 +08:00
}
2025-09-10 16:04:08 +08:00
public void GetResult(short token) { _core.GetResult(token); }
public UniTaskStatus UnsafeGetStatus() => _core.UnsafeGetStatus();
2025-02-28 16:08:26 +08:00
public bool MoveNext()
{
2025-09-10 16:04:08 +08:00
if (_completed)
2025-02-28 16:08:26 +08:00
{
TryReturn();
return false;
}
2025-09-10 16:04:08 +08:00
if (!_handle.IsDone)
2025-02-28 16:08:26 +08:00
{
2025-09-10 16:04:08 +08:00
_progress?.Report(_handle.Progress);
2025-02-28 16:08:26 +08:00
}
return true;
}
}
}
2025-09-10 16:04:08 +08:00
}