diff --git a/Assets/UniTask.YooAsset~/AsyncOperationBaseExtensions.cs b/Assets/UniTask.YooAsset~/AsyncOperationBaseExtensions.cs new file mode 100644 index 0000000..b9536ba --- /dev/null +++ b/Assets/UniTask.YooAsset~/AsyncOperationBaseExtensions.cs @@ -0,0 +1,145 @@ +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(); + } + + public static UniTask ToUniTask(this AsyncOperationBase handle, + IProgress progress = null, + PlayerLoopTiming timing = PlayerLoopTiming.Update) + { + ThrowArgumentNullException(handle, nameof(handle)); + + if(handle.IsDone) + { + return UniTask.CompletedTask; + } + + return new UniTask( + AsyncOperationBaserConfiguredSource.Create( + handle, + timing, + progress, + out var token + ), + token + ); + } + + sealed class AsyncOperationBaserConfiguredSource : IUniTaskSource, + IPlayerLoopItem, + ITaskPoolNode + { + private static TaskPool pool; + + private AsyncOperationBaserConfiguredSource nextNode; + + public ref AsyncOperationBaserConfiguredSource NextNode => ref nextNode; + + static AsyncOperationBaserConfiguredSource() + { + TaskPool.RegisterSizeGetter(typeof(AsyncOperationBaserConfiguredSource), () => pool.Size); + } + + private readonly Action continuationAction; + private AsyncOperationBase handle; + private IProgress progress; + private bool completed; + private UniTaskCompletionSourceCore core; + + AsyncOperationBaserConfiguredSource() { continuationAction = Continuation; } + + public static IUniTaskSource Create(AsyncOperationBase handle, + PlayerLoopTiming timing, + IProgress progress, + out short token) + { + if(!pool.TryPop(out var result)) + { + result = new AsyncOperationBaserConfiguredSource(); + } + + result.handle = handle; + result.progress = progress; + result.completed = false; + TaskTracker.TrackActiveTask(result, 3); + + if(progress is not null) + { + PlayerLoopHelper.AddAction(timing, result); + } + + handle.Completed += result.continuationAction; + + token = result.core.Version; + + return result; + } + + private void Continuation(AsyncOperationBase _) + { + handle.Completed -= continuationAction; + + if(completed) + { + TryReturn(); + } + else + { + completed = true; + if(handle.Status == EOperationStatus.Failed) + { + core.TrySetException(new Exception(handle.Error)); + } + else + { + core.TrySetResult(AsyncUnit.Default); + } + } + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + handle = default; + progress = default; + return pool.TryPush(this); + } + + public UniTaskStatus GetStatus(short token) => core.GetStatus(token); + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public void GetResult(short token) { core.GetResult(token); } + + public UniTaskStatus UnsafeGetStatus() => core.UnsafeGetStatus(); + + public bool MoveNext() + { + if(completed) + { + TryReturn(); + return false; + } + + if(!handle.IsDone) + { + progress?.Report(handle.Progress); + } + + return true; + } + } + } +} \ No newline at end of file diff --git a/Assets/UniTask.YooAsset~/AsyncOperationBaseExtensions.cs.meta b/Assets/UniTask.YooAsset~/AsyncOperationBaseExtensions.cs.meta new file mode 100644 index 0000000..db5f045 --- /dev/null +++ b/Assets/UniTask.YooAsset~/AsyncOperationBaseExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2cceff5ec1f84bd0a6a9b4ed7719527c +timeCreated: 1651978895 \ No newline at end of file diff --git a/Assets/UniTask.YooAsset~/README.md.meta b/Assets/UniTask.YooAsset~/README.md.meta new file mode 100644 index 0000000..6a40dae --- /dev/null +++ b/Assets/UniTask.YooAsset~/README.md.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 6375cc739b170490fbc6c38181b2c600 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: