From 81b28a0407ca239a2ae3413b647396fe6b38d864 Mon Sep 17 00:00:00 2001 From: L <401419353@qq.com> Date: Mon, 1 Aug 2022 14:55:24 +0800 Subject: [PATCH] Update UniTask Sample --- .../Samples~/UniTask Sample/UniTask.meta | 8 + .../UniTask Sample/UniTask/Editor.meta | 8 + .../UniTask/Editor/SplitterGUILayout.cs | 62 + .../UniTask/Editor/SplitterGUILayout.cs.meta | 11 + .../UniTask/Editor/UniTask.Editor.asmdef | 17 + .../UniTask/Editor/UniTask.Editor.asmdef.meta | 7 + .../UniTask/Editor/UniTaskTrackerTreeView.cs | 182 + .../Editor/UniTaskTrackerTreeView.cs.meta | 11 + .../UniTask/Editor/UniTaskTrackerWindow.cs | 209 + .../Editor/UniTaskTrackerWindow.cs.meta | 11 + .../UniTask Sample/UniTask/Runtime.meta | 8 + .../UniTask/Runtime/AsyncLazy.cs | 245 + .../UniTask/Runtime/AsyncLazy.cs.meta | 11 + .../UniTask/Runtime/AsyncReactiveProperty.cs | 644 + .../Runtime/AsyncReactiveProperty.cs.meta | 11 + .../UniTask/Runtime/AsyncUnit.cs | 26 + .../UniTask/Runtime/AsyncUnit.cs.meta | 11 + .../CancellationTokenEqualityComparer.cs | 23 + .../CancellationTokenEqualityComparer.cs.meta | 11 + .../Runtime/CancellationTokenExtensions.cs | 182 + .../CancellationTokenExtensions.cs.meta | 11 + .../CancellationTokenSourceExtensions.cs | 44 + .../CancellationTokenSourceExtensions.cs.meta | 11 + .../UniTask Sample/UniTask/Runtime/Channel.cs | 450 + .../UniTask/Runtime/Channel.cs.meta | 11 + .../UniTask/Runtime/CompilerServices.meta | 8 + .../AsyncMethodBuilderAttribute.cs | 17 + .../AsyncMethodBuilderAttribute.cs.meta | 11 + .../AsyncUniTaskMethodBuilder.cs | 269 + .../AsyncUniTaskMethodBuilder.cs.meta | 11 + .../AsyncUniTaskVoidMethodBuilder.cs | 137 + .../AsyncUniTaskVoidMethodBuilder.cs.meta | 11 + .../CompilerServices/StateMachineRunner.cs | 380 + .../StateMachineRunner.cs.meta | 11 + .../Runtime/EnumerableAsyncExtensions.cs | 34 + .../Runtime/EnumerableAsyncExtensions.cs.meta | 11 + .../Runtime/EnumeratorAsyncExtensions.cs | 287 + .../Runtime/EnumeratorAsyncExtensions.cs.meta | 11 + .../UniTask/Runtime/ExceptionExtensions.cs | 14 + .../Runtime/ExceptionExtensions.cs.meta | 11 + .../UniTask/Runtime/External.meta | 8 + .../Runtime/External/Addressables.meta | 8 + .../AddressablesAsyncExtensions.cs | 401 + .../AddressablesAsyncExtensions.cs.meta | 11 + .../Addressables/UniTask.Addressables.asmdef | 22 + .../UniTask.Addressables.asmdef.meta | 7 + .../UniTask/Runtime/External/DOTween.meta | 8 + .../DOTween/DOTweenAsyncExtensions.cs | 470 + .../DOTween/DOTweenAsyncExtensions.cs.meta | 11 + .../External/DOTween/UniTask.DOTween.asmdef | 22 + .../DOTween/UniTask.DOTween.asmdef.meta | 7 + .../UniTask/Runtime/External/TextMeshPro.meta | 8 + .../TextMeshProAsyncExtensions.InputField.cs | 224 + ...tMeshProAsyncExtensions.InputField.cs.meta | 11 + .../TextMeshProAsyncExtensions.InputField.tt | 66 + ...tMeshProAsyncExtensions.InputField.tt.meta | 7 + .../TextMeshPro/TextMeshProAsyncExtensions.cs | 130 + .../TextMeshProAsyncExtensions.cs.meta | 11 + .../TextMeshPro/UniTask.TextMeshPro.asmdef | 22 + .../UniTask.TextMeshPro.asmdef.meta | 7 + .../Runtime/IUniTaskAsyncEnumerable.cs | 91 + .../Runtime/IUniTaskAsyncEnumerable.cs.meta | 11 + .../UniTask/Runtime/IUniTaskSource.cs | 124 + .../UniTask/Runtime/IUniTaskSource.cs.meta | 11 + .../UniTask/Runtime/Internal.meta | 8 + .../UniTask/Runtime/Internal/ArrayPool.cs | 150 + .../Runtime/Internal/ArrayPool.cs.meta | 12 + .../UniTask/Runtime/Internal/ArrayPoolUtil.cs | 115 + .../Runtime/Internal/ArrayPoolUtil.cs.meta | 12 + .../UniTask/Runtime/Internal/ArrayUtil.cs | 73 + .../Runtime/Internal/ArrayUtil.cs.meta | 12 + .../Runtime/Internal/ContinuationQueue.cs | 225 + .../Internal/ContinuationQueue.cs.meta | 11 + .../Runtime/Internal/DiagnosticsExtensions.cs | 249 + .../Internal/DiagnosticsExtensions.cs.meta | 11 + .../UniTask/Runtime/Internal/Error.cs | 79 + .../UniTask/Runtime/Internal/Error.cs.meta | 12 + .../UniTask/Runtime/Internal/MinimumQueue.cs | 112 + .../Runtime/Internal/MinimumQueue.cs.meta | 11 + .../Runtime/Internal/PlayerLoopRunner.cs | 260 + .../Runtime/Internal/PlayerLoopRunner.cs.meta | 11 + .../Runtime/Internal/PooledDelegate.cs | 50 + .../Runtime/Internal/PooledDelegate.cs.meta | 11 + .../Internal/RuntimeHelpersAbstraction.cs | 64 + .../RuntimeHelpersAbstraction.cs.meta | 12 + .../UniTask/Runtime/Internal/StatePool.cs | 153 + .../Runtime/Internal/StatePool.cs.meta | 11 + .../UniTask/Runtime/Internal/TaskTracker.cs | 178 + .../Runtime/Internal/TaskTracker.cs.meta | 11 + .../Runtime/Internal/UnityEqualityComparer.cs | 267 + .../Internal/UnityEqualityComparer.cs.meta | 11 + .../Internal/UnityWebRequestExtensions.cs | 28 + .../UnityWebRequestExtensions.cs.meta | 11 + .../Runtime/Internal/ValueStopwatch.cs | 37 + .../Runtime/Internal/ValueStopwatch.cs.meta | 11 + .../Runtime/Internal/WeakDictionary.cs | 334 + .../Runtime/Internal/WeakDictionary.cs.meta | 11 + .../UniTask Sample/UniTask/Runtime/Linq.meta | 8 + .../UniTask/Runtime/Linq/Aggregate.cs | 318 + .../UniTask/Runtime/Linq/Aggregate.cs.meta | 11 + .../UniTask/Runtime/Linq/All.cs | 108 + .../UniTask/Runtime/Linq/All.cs.meta | 11 + .../UniTask/Runtime/Linq/Any.cs | 136 + .../UniTask/Runtime/Linq/Any.cs.meta | 11 + .../UniTask/Runtime/Linq/AppendPrepend.cs | 151 + .../Runtime/Linq/AppendPrepend.cs.meta | 11 + .../Runtime/Linq/AsUniTaskAsyncEnumerable.cs | 10 + .../Linq/AsUniTaskAsyncEnumerable.cs.meta | 11 + .../Runtime/Linq/AsyncEnumeratorBase.cs | 356 + .../Runtime/Linq/AsyncEnumeratorBase.cs.meta | 11 + .../UniTask/Runtime/Linq/Average.cs | 1524 +++ .../UniTask/Runtime/Linq/Average.cs.meta | 11 + .../UniTask/Runtime/Linq/Average.tt | 237 + .../UniTask/Runtime/Linq/Average.tt.meta | 7 + .../UniTask/Runtime/Linq/Buffer.cs | 345 + .../UniTask/Runtime/Linq/Buffer.cs.meta | 11 + .../UniTask/Runtime/Linq/Cast.cs | 53 + .../UniTask/Runtime/Linq/Cast.cs.meta | 11 + .../UniTask/Runtime/Linq/CombineLatest.cs | 11372 ++++++++++++++++ .../Runtime/Linq/CombineLatest.cs.meta | 11 + .../UniTask/Runtime/Linq/CombineLatest.tt | 221 + .../Runtime/Linq/CombineLatest.tt.meta | 7 + .../UniTask/Runtime/Linq/Concat.cs | 164 + .../UniTask/Runtime/Linq/Concat.cs.meta | 11 + .../UniTask/Runtime/Linq/Contains.cs | 50 + .../UniTask/Runtime/Linq/Contains.cs.meta | 11 + .../UniTask/Runtime/Linq/Count.cs | 144 + .../UniTask/Runtime/Linq/Count.cs.meta | 11 + .../UniTask/Runtime/Linq/Create.cs | 174 + .../UniTask/Runtime/Linq/Create.cs.meta | 11 + .../UniTask/Runtime/Linq/DefaultIfEmpty.cs | 142 + .../Runtime/Linq/DefaultIfEmpty.cs.meta | 11 + .../UniTask/Runtime/Linq/Distinct.cs | 277 + .../UniTask/Runtime/Linq/Distinct.cs.meta | 11 + .../Runtime/Linq/DistinctUntilChanged.cs | 662 + .../Runtime/Linq/DistinctUntilChanged.cs.meta | 11 + .../UniTask Sample/UniTask/Runtime/Linq/Do.cs | 258 + .../UniTask/Runtime/Linq/Do.cs.meta | 11 + .../UniTask/Runtime/Linq/ElementAt.cs | 58 + .../UniTask/Runtime/Linq/ElementAt.cs.meta | 11 + .../UniTask/Runtime/Linq/Empty.cs | 47 + .../UniTask/Runtime/Linq/Empty.cs.meta | 11 + .../UniTask/Runtime/Linq/Except.cs | 116 + .../UniTask/Runtime/Linq/Except.cs.meta | 11 + .../UniTask/Runtime/Linq/First.cs | 200 + .../UniTask/Runtime/Linq/First.cs.meta | 11 + .../UniTask/Runtime/Linq/ForEach.cs | 193 + .../UniTask/Runtime/Linq/ForEach.cs.meta | 11 + .../UniTask/Runtime/Linq/GroupBy.cs | 923 ++ .../UniTask/Runtime/Linq/GroupBy.cs.meta | 11 + .../UniTask/Runtime/Linq/GroupJoin.cs | 612 + .../UniTask/Runtime/Linq/GroupJoin.cs.meta | 11 + .../UniTask/Runtime/Linq/Intersect.cs | 117 + .../UniTask/Runtime/Linq/Intersect.cs.meta | 11 + .../UniTask/Runtime/Linq/Join.cs | 728 + .../UniTask/Runtime/Linq/Join.cs.meta | 11 + .../UniTask/Runtime/Linq/Last.cs | 240 + .../UniTask/Runtime/Linq/Last.cs.meta | 11 + .../UniTask/Runtime/Linq/LongCount.cs | 144 + .../UniTask/Runtime/Linq/LongCount.cs.meta | 11 + .../UniTask/Runtime/Linq/Max.cs | 200 + .../UniTask/Runtime/Linq/Max.cs.meta | 11 + .../UniTask/Runtime/Linq/Min.cs | 200 + .../UniTask/Runtime/Linq/Min.cs.meta | 11 + .../UniTask/Runtime/Linq/MinMax.cs | 3763 +++++ .../UniTask/Runtime/Linq/MinMax.cs.meta | 11 + .../UniTask/Runtime/Linq/MinMax.tt | 262 + .../UniTask/Runtime/Linq/MinMax.tt.meta | 7 + .../UniTask/Runtime/Linq/Never.cs | 56 + .../UniTask/Runtime/Linq/Never.cs.meta | 11 + .../UniTask/Runtime/Linq/OfType.cs | 61 + .../UniTask/Runtime/Linq/OfType.cs.meta | 11 + .../UniTask/Runtime/Linq/OrderBy.cs | 558 + .../UniTask/Runtime/Linq/OrderBy.cs.meta | 11 + .../UniTask/Runtime/Linq/Pairwise.cs | 128 + .../UniTask/Runtime/Linq/Pairwise.cs.meta | 11 + .../UniTask/Runtime/Linq/Publish.cs | 173 + .../UniTask/Runtime/Linq/Publish.cs.meta | 11 + .../UniTask/Runtime/Linq/Queue.cs | 103 + .../UniTask/Runtime/Linq/Queue.cs.meta | 11 + .../UniTask/Runtime/Linq/Range.cs | 75 + .../UniTask/Runtime/Linq/Range.cs.meta | 11 + .../UniTask/Runtime/Linq/Repeat.cs | 68 + .../UniTask/Runtime/Linq/Repeat.cs.meta | 11 + .../UniTask/Runtime/Linq/Return.cs | 63 + .../UniTask/Runtime/Linq/Return.cs.meta | 11 + .../UniTask/Runtime/Linq/Reverse.cs | 78 + .../UniTask/Runtime/Linq/Reverse.cs.meta | 11 + .../UniTask/Runtime/Linq/Select.cs | 760 ++ .../UniTask/Runtime/Linq/Select.cs.meta | 11 + .../UniTask/Runtime/Linq/SelectMany.cs | 892 ++ .../UniTask/Runtime/Linq/SelectMany.cs.meta | 11 + .../UniTask/Runtime/Linq/SequenceEqual.cs | 87 + .../Runtime/Linq/SequenceEqual.cs.meta | 11 + .../UniTask/Runtime/Linq/Single.cs | 230 + .../UniTask/Runtime/Linq/Single.cs.meta | 11 + .../UniTask/Runtime/Linq/Skip.cs | 69 + .../UniTask/Runtime/Linq/Skip.cs.meta | 11 + .../UniTask/Runtime/Linq/SkipLast.cs | 159 + .../UniTask/Runtime/Linq/SkipLast.cs.meta | 11 + .../UniTask/Runtime/Linq/SkipUntil.cs | 187 + .../UniTask/Runtime/Linq/SkipUntil.cs.meta | 11 + .../UniTask/Runtime/Linq/SkipUntilCanceled.cs | 173 + .../Runtime/Linq/SkipUntilCanceled.cs.meta | 11 + .../UniTask/Runtime/Linq/SkipWhile.cs | 379 + .../UniTask/Runtime/Linq/SkipWhile.cs.meta | 11 + .../UniTask/Runtime/Linq/Subscribe.cs | 536 + .../UniTask/Runtime/Linq/Subscribe.cs.meta | 11 + .../UniTask/Runtime/Linq/Sum.cs | 1244 ++ .../UniTask/Runtime/Linq/Sum.cs.meta | 11 + .../UniTask/Runtime/Linq/Sum.tt | 167 + .../UniTask/Runtime/Linq/Sum.tt.meta | 7 + .../UniTask/Runtime/Linq/Take.cs | 124 + .../UniTask/Runtime/Linq/Take.cs.meta | 11 + .../UniTask/Runtime/Linq/TakeLast.cs | 175 + .../UniTask/Runtime/Linq/TakeLast.cs.meta | 11 + .../UniTask/Runtime/Linq/TakeUntil.cs | 190 + .../UniTask/Runtime/Linq/TakeUntil.cs.meta | 11 + .../UniTask/Runtime/Linq/TakeUntilCanceled.cs | 164 + .../Runtime/Linq/TakeUntilCanceled.cs.meta | 11 + .../UniTask/Runtime/Linq/TakeWhile.cs | 342 + .../UniTask/Runtime/Linq/TakeWhile.cs.meta | 11 + .../UniTask/Runtime/Linq/Throw.cs | 54 + .../UniTask/Runtime/Linq/Throw.cs.meta | 11 + .../UniTask/Runtime/Linq/ToArray.cs | 60 + .../UniTask/Runtime/Linq/ToArray.cs.meta | 11 + .../UniTask/Runtime/Linq/ToDictionary.cs | 278 + .../UniTask/Runtime/Linq/ToDictionary.cs.meta | 11 + .../UniTask/Runtime/Linq/ToHashSet.cs | 50 + .../UniTask/Runtime/Linq/ToHashSet.cs.meta | 11 + .../UniTask/Runtime/Linq/ToList.cs | 42 + .../UniTask/Runtime/Linq/ToList.cs.meta | 11 + .../UniTask/Runtime/Linq/ToLookup.cs | 554 + .../UniTask/Runtime/Linq/ToLookup.cs.meta | 11 + .../UniTask/Runtime/Linq/ToObservable.cs | 97 + .../UniTask/Runtime/Linq/ToObservable.cs.meta | 11 + .../Runtime/Linq/ToUniTaskAsyncEnumerable.cs | 1115 ++ .../Linq/ToUniTaskAsyncEnumerable.cs.meta | 11 + .../UniTask/Runtime/Linq/UniTask.Linq.asmdef | 15 + .../Runtime/Linq/UniTask.Linq.asmdef.meta | 7 + .../UniTask/Runtime/Linq/Union.cs | 26 + .../UniTask/Runtime/Linq/Union.cs.meta | 11 + .../UniTask/Runtime/Linq/UnityExtensions.meta | 8 + .../Linq/UnityExtensions/EveryUpdate.cs | 77 + .../Linq/UnityExtensions/EveryUpdate.cs.meta | 11 + .../Linq/UnityExtensions/EveryValueChanged.cs | 240 + .../UnityExtensions/EveryValueChanged.cs.meta | 11 + .../Runtime/Linq/UnityExtensions/Timer.cs | 312 + .../Linq/UnityExtensions/Timer.cs.meta | 11 + .../UniTask/Runtime/Linq/Where.cs | 818 ++ .../UniTask/Runtime/Linq/Where.cs.meta | 11 + .../UniTask/Runtime/Linq/Zip.cs | 541 + .../UniTask/Runtime/Linq/Zip.cs.meta | 11 + .../UniTask/Runtime/MoveNextSource.cs | 63 + .../UniTask/Runtime/MoveNextSource.cs.meta | 11 + .../UniTask/Runtime/PlayerLoopHelper.cs | 564 + .../UniTask/Runtime/PlayerLoopHelper.cs.meta | 11 + .../UniTask/Runtime/PlayerLoopTimer.cs | 262 + .../UniTask/Runtime/PlayerLoopTimer.cs.meta | 11 + .../UniTask/Runtime/Progress.cs | 87 + .../UniTask/Runtime/Progress.cs.meta | 11 + .../UniTask/Runtime/TaskPool.cs | 123 + .../UniTask/Runtime/TaskPool.cs.meta | 11 + .../UniTask/Runtime/TimeoutController.cs | 129 + .../UniTask/Runtime/TimeoutController.cs.meta | 11 + .../UniTask/Runtime/TriggerEvent.cs | 311 + .../UniTask/Runtime/TriggerEvent.cs.meta | 11 + .../UniTask/Runtime/Triggers.meta | 8 + .../Runtime/Triggers/AsyncAwakeTrigger.cs | 32 + .../Triggers/AsyncAwakeTrigger.cs.meta | 11 + .../Runtime/Triggers/AsyncDestroyTrigger.cs | 97 + .../Triggers/AsyncDestroyTrigger.cs.meta | 11 + .../Runtime/Triggers/AsyncStartTrigger.cs | 38 + .../Triggers/AsyncStartTrigger.cs.meta | 11 + .../Runtime/Triggers/AsyncTriggerBase.cs | 310 + .../Runtime/Triggers/AsyncTriggerBase.cs.meta | 11 + .../Triggers/AsyncTriggerExtensions.cs | 85 + .../Triggers/AsyncTriggerExtensions.cs.meta | 11 + .../Triggers/MonoBehaviourMessagesTriggers.cs | 4457 ++++++ .../MonoBehaviourMessagesTriggers.cs.meta | 11 + .../Triggers/MonoBehaviourMessagesTriggers.tt | 208 + .../MonoBehaviourMessagesTriggers.tt.meta | 7 + .../UniTask/Runtime/UniTask.Bridge.cs | 18 + .../UniTask/Runtime/UniTask.Bridge.cs.meta | 11 + .../UniTask/Runtime/UniTask.Delay.cs | 963 ++ .../UniTask/Runtime/UniTask.Delay.cs.meta | 11 + .../UniTask/Runtime/UniTask.Factory.cs | 499 + .../UniTask/Runtime/UniTask.Factory.cs.meta | 11 + .../UniTask/Runtime/UniTask.Run.cs | 289 + .../UniTask/Runtime/UniTask.Run.cs.meta | 11 + .../UniTask/Runtime/UniTask.Threading.cs | 412 + .../UniTask/Runtime/UniTask.Threading.cs.meta | 11 + .../UniTask/Runtime/UniTask.WaitUntil.cs | 582 + .../UniTask/Runtime/UniTask.WaitUntil.cs.meta | 11 + .../Runtime/UniTask.WhenAll.Generated.cs | 5011 +++++++ .../Runtime/UniTask.WhenAll.Generated.cs.meta | 11 + .../Runtime/UniTask.WhenAll.Generated.tt | 122 + .../Runtime/UniTask.WhenAll.Generated.tt.meta | 7 + .../UniTask/Runtime/UniTask.WhenAll.cs | 237 + .../UniTask/Runtime/UniTask.WhenAll.cs.meta | 11 + .../Runtime/UniTask.WhenAny.Generated.cs | 5060 +++++++ .../Runtime/UniTask.WhenAny.Generated.cs.meta | 11 + .../Runtime/UniTask.WhenAny.Generated.tt | 117 + .../Runtime/UniTask.WhenAny.Generated.tt.meta | 7 + .../UniTask/Runtime/UniTask.WhenAny.cs | 359 + .../UniTask/Runtime/UniTask.WhenAny.cs.meta | 11 + .../UniTask/Runtime/UniTask.asmdef | 45 + .../UniTask/Runtime/UniTask.asmdef.meta | 7 + .../UniTask Sample/UniTask/Runtime/UniTask.cs | 707 + .../UniTask/Runtime/UniTask.cs.meta | 11 + .../Runtime/UniTaskCompletionSource.cs | 941 ++ .../Runtime/UniTaskCompletionSource.cs.meta | 11 + .../Runtime/UniTaskExtensions.Shorthand.cs | 187 + .../UniTaskExtensions.Shorthand.cs.meta | 11 + .../Runtime/UniTaskExtensions.Shorthand.tt | 63 + .../UniTaskExtensions.Shorthand.tt.meta | 7 + .../UniTask/Runtime/UniTaskExtensions.cs | 921 ++ .../UniTask/Runtime/UniTaskExtensions.cs.meta | 11 + .../Runtime/UniTaskObservableExtensions.cs | 750 + .../UniTaskObservableExtensions.cs.meta | 11 + .../UniTask/Runtime/UniTaskScheduler.cs | 103 + .../UniTask/Runtime/UniTaskScheduler.cs.meta | 11 + .../Runtime/UniTaskSynchronizationContext.cs | 158 + .../UniTaskSynchronizationContext.cs.meta | 11 + .../UniTask/Runtime/UniTaskVoid.cs | 19 + .../UniTask/Runtime/UniTaskVoid.cs.meta | 11 + ...cExtensions.AssetBundleRequestAllAssets.cs | 198 + ...nsions.AssetBundleRequestAllAssets.cs.meta | 11 + .../UnityAsyncExtensions.AsyncGPUReadback.cs | 140 + ...tyAsyncExtensions.AsyncGPUReadback.cs.meta | 11 + .../Runtime/UnityAsyncExtensions.Jobs.cs | 102 + .../Runtime/UnityAsyncExtensions.Jobs.cs.meta | 11 + .../UnityAsyncExtensions.MonoBehaviour.cs | 14 + ...UnityAsyncExtensions.MonoBehaviour.cs.meta | 11 + .../UniTask/Runtime/UnityAsyncExtensions.cs | 928 ++ .../Runtime/UnityAsyncExtensions.cs.meta | 11 + .../UniTask/Runtime/UnityAsyncExtensions.tt | 278 + .../Runtime/UnityAsyncExtensions.tt.meta | 7 + .../Runtime/UnityAsyncExtensions.uGUI.cs | 826 ++ .../Runtime/UnityAsyncExtensions.uGUI.cs.meta | 11 + .../UniTask/Runtime/UnityBindingExtensions.cs | 245 + .../Runtime/UnityBindingExtensions.cs.meta | 11 + .../Runtime/UnityWebRequestException.cs | 67 + .../Runtime/UnityWebRequestException.cs.meta | 11 + .../UniTask/Runtime/_InternalVisibleTo.cs | 7 + .../Runtime/_InternalVisibleTo.cs.meta | 11 + .../UniTask Sample/UniTask/package.json | 12 + .../UniTask Sample/UniTask/package.json.meta | 7 + 348 files changed, 72220 insertions(+) create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/SplitterGUILayout.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/SplitterGUILayout.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTask.Editor.asmdef create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTask.Editor.asmdef.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTaskTrackerTreeView.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTaskTrackerTreeView.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTaskTrackerWindow.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTaskTrackerWindow.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncLazy.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncLazy.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncReactiveProperty.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncReactiveProperty.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncUnit.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncUnit.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenEqualityComparer.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenEqualityComparer.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenExtensions.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenExtensions.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenSourceExtensions.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenSourceExtensions.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Channel.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Channel.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncMethodBuilderAttribute.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncMethodBuilderAttribute.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncUniTaskMethodBuilder.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncUniTaskMethodBuilder.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncUniTaskVoidMethodBuilder.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncUniTaskVoidMethodBuilder.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/StateMachineRunner.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/StateMachineRunner.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/EnumerableAsyncExtensions.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/EnumerableAsyncExtensions.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/EnumeratorAsyncExtensions.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/EnumeratorAsyncExtensions.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/ExceptionExtensions.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/ExceptionExtensions.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables/UniTask.Addressables.asmdef create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables/UniTask.Addressables.asmdef.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween/DOTweenAsyncExtensions.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween/DOTweenAsyncExtensions.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween/UniTask.DOTween.asmdef create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween/UniTask.DOTween.asmdef.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.InputField.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.InputField.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.InputField.tt create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.InputField.tt.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/UniTask.TextMeshPro.asmdef create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/UniTask.TextMeshPro.asmdef.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/IUniTaskAsyncEnumerable.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/IUniTaskAsyncEnumerable.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/IUniTaskSource.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/IUniTaskSource.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayPool.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayPool.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayPoolUtil.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayPoolUtil.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayUtil.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayUtil.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ContinuationQueue.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ContinuationQueue.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/DiagnosticsExtensions.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/DiagnosticsExtensions.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/Error.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/Error.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/MinimumQueue.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/MinimumQueue.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/PlayerLoopRunner.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/PlayerLoopRunner.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/PooledDelegate.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/PooledDelegate.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/RuntimeHelpersAbstraction.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/RuntimeHelpersAbstraction.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/StatePool.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/StatePool.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/TaskTracker.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/TaskTracker.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/UnityEqualityComparer.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/UnityEqualityComparer.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/UnityWebRequestExtensions.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/UnityWebRequestExtensions.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ValueStopwatch.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ValueStopwatch.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/WeakDictionary.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/WeakDictionary.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Aggregate.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Aggregate.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/All.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/All.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Any.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Any.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AppendPrepend.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AppendPrepend.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AsUniTaskAsyncEnumerable.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AsUniTaskAsyncEnumerable.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AsyncEnumeratorBase.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AsyncEnumeratorBase.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Average.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Average.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Average.tt create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Average.tt.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Buffer.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Buffer.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Cast.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Cast.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/CombineLatest.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/CombineLatest.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/CombineLatest.tt create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/CombineLatest.tt.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Concat.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Concat.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Contains.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Contains.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Count.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Count.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Create.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Create.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/DefaultIfEmpty.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/DefaultIfEmpty.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Distinct.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Distinct.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/DistinctUntilChanged.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/DistinctUntilChanged.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Do.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Do.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ElementAt.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ElementAt.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Empty.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Empty.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Except.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Except.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/First.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/First.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ForEach.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ForEach.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/GroupBy.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/GroupBy.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/GroupJoin.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/GroupJoin.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Intersect.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Intersect.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Join.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Join.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Last.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Last.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/LongCount.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/LongCount.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Max.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Max.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Min.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Min.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/MinMax.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/MinMax.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/MinMax.tt create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/MinMax.tt.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Never.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Never.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/OfType.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/OfType.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/OrderBy.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/OrderBy.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Pairwise.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Pairwise.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Publish.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Publish.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Queue.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Queue.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Range.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Range.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Repeat.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Repeat.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Return.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Return.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Reverse.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Reverse.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Select.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Select.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SelectMany.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SelectMany.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SequenceEqual.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SequenceEqual.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Single.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Single.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Skip.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Skip.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipLast.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipLast.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipUntil.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipUntil.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipUntilCanceled.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipUntilCanceled.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipWhile.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipWhile.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Subscribe.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Subscribe.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Sum.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Sum.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Sum.tt create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Sum.tt.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Take.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Take.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeLast.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeLast.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeUntil.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeUntil.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeUntilCanceled.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeUntilCanceled.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeWhile.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeWhile.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Throw.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Throw.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToArray.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToArray.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToDictionary.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToDictionary.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToHashSet.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToHashSet.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToList.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToList.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToLookup.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToLookup.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToObservable.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToObservable.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToUniTaskAsyncEnumerable.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToUniTaskAsyncEnumerable.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UniTask.Linq.asmdef create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UniTask.Linq.asmdef.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Union.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Union.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/EveryUpdate.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/EveryUpdate.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/EveryValueChanged.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/EveryValueChanged.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/Timer.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/Timer.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Where.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Where.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Zip.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Zip.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/MoveNextSource.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/MoveNextSource.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/PlayerLoopHelper.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/PlayerLoopHelper.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/PlayerLoopTimer.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/PlayerLoopTimer.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Progress.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Progress.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TaskPool.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TaskPool.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TimeoutController.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TimeoutController.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TriggerEvent.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TriggerEvent.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncAwakeTrigger.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncAwakeTrigger.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncDestroyTrigger.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncDestroyTrigger.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncStartTrigger.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncStartTrigger.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncTriggerBase.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncTriggerBase.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncTriggerExtensions.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncTriggerExtensions.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/MonoBehaviourMessagesTriggers.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/MonoBehaviourMessagesTriggers.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/MonoBehaviourMessagesTriggers.tt create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/MonoBehaviourMessagesTriggers.tt.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Bridge.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Bridge.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Delay.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Delay.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Factory.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Factory.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Run.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Run.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Threading.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Threading.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WaitUntil.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WaitUntil.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.Generated.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.Generated.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.Generated.tt create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.Generated.tt.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.Generated.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.Generated.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.Generated.tt create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.Generated.tt.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.asmdef create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.asmdef.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskCompletionSource.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskCompletionSource.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.Shorthand.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.Shorthand.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.Shorthand.tt create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.Shorthand.tt.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskObservableExtensions.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskObservableExtensions.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskScheduler.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskScheduler.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskSynchronizationContext.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskSynchronizationContext.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskVoid.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskVoid.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.AsyncGPUReadback.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.AsyncGPUReadback.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.Jobs.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.Jobs.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.MonoBehaviour.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.MonoBehaviour.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.tt create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.tt.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.uGUI.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.uGUI.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityBindingExtensions.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityBindingExtensions.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityWebRequestException.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityWebRequestException.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/_InternalVisibleTo.cs create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/_InternalVisibleTo.cs.meta create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/package.json create mode 100644 Assets/YooAsset/Samples~/UniTask Sample/UniTask/package.json.meta diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask.meta new file mode 100644 index 0000000..408f676 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: aaf714c0c57134e7ab7675a7528c1505 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor.meta new file mode 100644 index 0000000..c0746ba --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 275b87293edc6634f9d72387851dbbdf +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/SplitterGUILayout.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/SplitterGUILayout.cs new file mode 100644 index 0000000..4189133 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/SplitterGUILayout.cs @@ -0,0 +1,62 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Linq; +using System.Reflection; +using UnityEditor; +using UnityEngine; + +namespace Cysharp.Threading.Tasks.Editor +{ + // reflection call of UnityEditor.SplitterGUILayout + internal static class SplitterGUILayout + { + static BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; + + static Lazy splitterStateType = new Lazy(() => + { + var type = typeof(EditorWindow).Assembly.GetTypes().First(x => x.FullName == "UnityEditor.SplitterState"); + return type; + }); + + static Lazy splitterStateCtor = new Lazy(() => + { + var type = splitterStateType.Value; + return type.GetConstructor(flags, null, new Type[] { typeof(float[]), typeof(int[]), typeof(int[]) }, null); + }); + + static Lazy splitterGUILayoutType = new Lazy(() => + { + var type = typeof(EditorWindow).Assembly.GetTypes().First(x => x.FullName == "UnityEditor.SplitterGUILayout"); + return type; + }); + + static Lazy beginVerticalSplit = new Lazy(() => + { + var type = splitterGUILayoutType.Value; + return type.GetMethod("BeginVerticalSplit", flags, null, new Type[] { splitterStateType.Value, typeof(GUILayoutOption[]) }, null); + }); + + static Lazy endVerticalSplit = new Lazy(() => + { + var type = splitterGUILayoutType.Value; + return type.GetMethod("EndVerticalSplit", flags, null, Type.EmptyTypes, null); + }); + + public static object CreateSplitterState(float[] relativeSizes, int[] minSizes, int[] maxSizes) + { + return splitterStateCtor.Value.Invoke(new object[] { relativeSizes, minSizes, maxSizes }); + } + + public static void BeginVerticalSplit(object splitterState, params GUILayoutOption[] options) + { + beginVerticalSplit.Value.Invoke(null, new object[] { splitterState, options }); + } + + public static void EndVerticalSplit() + { + endVerticalSplit.Value.Invoke(null, Type.EmptyTypes); + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/SplitterGUILayout.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/SplitterGUILayout.cs.meta new file mode 100644 index 0000000..4d718f4 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/SplitterGUILayout.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 40ef2e46f900131419e869398a8d3c9d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTask.Editor.asmdef b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTask.Editor.asmdef new file mode 100644 index 0000000..c618c6a --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTask.Editor.asmdef @@ -0,0 +1,17 @@ +{ + "name": "UniTask.Editor", + "references": [ + "UniTask" + ], + "includePlatforms": [ + "Editor" + ], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": false, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTask.Editor.asmdef.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTask.Editor.asmdef.meta new file mode 100644 index 0000000..821b87b --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTask.Editor.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 4129704b5a1a13841ba16f230bf24a57 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTaskTrackerTreeView.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTaskTrackerTreeView.cs new file mode 100644 index 0000000..e7b6269 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTaskTrackerTreeView.cs @@ -0,0 +1,182 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using UnityEngine; +using UnityEditor; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System; +using UnityEditor.IMGUI.Controls; +using Cysharp.Threading.Tasks.Internal; +using System.Text; +using System.Text.RegularExpressions; + +namespace Cysharp.Threading.Tasks.Editor +{ + public class UniTaskTrackerViewItem : TreeViewItem + { + static Regex removeHref = new Regex("(.+)", RegexOptions.Compiled); + + public string TaskType { get; set; } + public string Elapsed { get; set; } + public string Status { get; set; } + + string position; + public string Position + { + get { return position; } + set + { + position = value; + PositionFirstLine = GetFirstLine(position); + } + } + + public string PositionFirstLine { get; private set; } + + static string GetFirstLine(string str) + { + var sb = new StringBuilder(); + for (int i = 0; i < str.Length; i++) + { + if (str[i] == '\r' || str[i] == '\n') + { + break; + } + sb.Append(str[i]); + } + + return removeHref.Replace(sb.ToString(), "$1"); + } + + public UniTaskTrackerViewItem(int id) : base(id) + { + + } + } + + public class UniTaskTrackerTreeView : TreeView + { + const string sortedColumnIndexStateKey = "UniTaskTrackerTreeView_sortedColumnIndex"; + + public IReadOnlyList CurrentBindingItems; + + public UniTaskTrackerTreeView() + : this(new TreeViewState(), new MultiColumnHeader(new MultiColumnHeaderState(new[] + { + new MultiColumnHeaderState.Column() { headerContent = new GUIContent("TaskType"), width = 20}, + new MultiColumnHeaderState.Column() { headerContent = new GUIContent("Elapsed"), width = 10}, + new MultiColumnHeaderState.Column() { headerContent = new GUIContent("Status"), width = 10}, + new MultiColumnHeaderState.Column() { headerContent = new GUIContent("Position")}, + }))) + { + } + + UniTaskTrackerTreeView(TreeViewState state, MultiColumnHeader header) + : base(state, header) + { + rowHeight = 20; + showAlternatingRowBackgrounds = true; + showBorder = true; + header.sortingChanged += Header_sortingChanged; + + header.ResizeToFit(); + Reload(); + + header.sortedColumnIndex = SessionState.GetInt(sortedColumnIndexStateKey, 1); + } + + public void ReloadAndSort() + { + var currentSelected = this.state.selectedIDs; + Reload(); + Header_sortingChanged(this.multiColumnHeader); + this.state.selectedIDs = currentSelected; + } + + private void Header_sortingChanged(MultiColumnHeader multiColumnHeader) + { + SessionState.SetInt(sortedColumnIndexStateKey, multiColumnHeader.sortedColumnIndex); + var index = multiColumnHeader.sortedColumnIndex; + var ascending = multiColumnHeader.IsSortedAscending(multiColumnHeader.sortedColumnIndex); + + var items = rootItem.children.Cast(); + + IOrderedEnumerable orderedEnumerable; + switch (index) + { + case 0: + orderedEnumerable = ascending ? items.OrderBy(item => item.TaskType) : items.OrderByDescending(item => item.TaskType); + break; + case 1: + orderedEnumerable = ascending ? items.OrderBy(item => double.Parse(item.Elapsed)) : items.OrderByDescending(item => double.Parse(item.Elapsed)); + break; + case 2: + orderedEnumerable = ascending ? items.OrderBy(item => item.Status) : items.OrderByDescending(item => item.Elapsed); + break; + case 3: + orderedEnumerable = ascending ? items.OrderBy(item => item.Position) : items.OrderByDescending(item => item.PositionFirstLine); + break; + default: + throw new ArgumentOutOfRangeException(nameof(index), index, null); + } + + CurrentBindingItems = rootItem.children = orderedEnumerable.Cast().ToList(); + BuildRows(rootItem); + } + + protected override TreeViewItem BuildRoot() + { + var root = new TreeViewItem { depth = -1 }; + + var children = new List(); + + TaskTracker.ForEachActiveTask((trackingId, awaiterType, status, created, stackTrace) => + { + children.Add(new UniTaskTrackerViewItem(trackingId) { TaskType = awaiterType, Status = status.ToString(), Elapsed = (DateTime.UtcNow - created).TotalSeconds.ToString("00.00"), Position = stackTrace }); + }); + + CurrentBindingItems = children; + root.children = CurrentBindingItems as List; + return root; + } + + protected override bool CanMultiSelect(TreeViewItem item) + { + return false; + } + + protected override void RowGUI(RowGUIArgs args) + { + var item = args.item as UniTaskTrackerViewItem; + + for (var visibleColumnIndex = 0; visibleColumnIndex < args.GetNumVisibleColumns(); visibleColumnIndex++) + { + var rect = args.GetCellRect(visibleColumnIndex); + var columnIndex = args.GetColumn(visibleColumnIndex); + + var labelStyle = args.selected ? EditorStyles.whiteLabel : EditorStyles.label; + labelStyle.alignment = TextAnchor.MiddleLeft; + switch (columnIndex) + { + case 0: + EditorGUI.LabelField(rect, item.TaskType, labelStyle); + break; + case 1: + EditorGUI.LabelField(rect, item.Elapsed, labelStyle); + break; + case 2: + EditorGUI.LabelField(rect, item.Status, labelStyle); + break; + case 3: + EditorGUI.LabelField(rect, item.PositionFirstLine, labelStyle); + break; + default: + throw new ArgumentOutOfRangeException(nameof(columnIndex), columnIndex, null); + } + } + } + } + +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTaskTrackerTreeView.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTaskTrackerTreeView.cs.meta new file mode 100644 index 0000000..9b34d7b --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTaskTrackerTreeView.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 52e2d973a2156674e8c1c9433ed031f7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTaskTrackerWindow.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTaskTrackerWindow.cs new file mode 100644 index 0000000..242ac6d --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTaskTrackerWindow.cs @@ -0,0 +1,209 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using UnityEngine; +using UnityEditor; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System; +using UnityEditor.IMGUI.Controls; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks.Editor +{ + public class UniTaskTrackerWindow : EditorWindow + { + static int interval; + + static UniTaskTrackerWindow window; + + [MenuItem("Window/UniTask Tracker")] + public static void OpenWindow() + { + if (window != null) + { + window.Close(); + } + + // will called OnEnable(singleton instance will be set). + GetWindow("UniTask Tracker").Show(); + } + + static readonly GUILayoutOption[] EmptyLayoutOption = new GUILayoutOption[0]; + + UniTaskTrackerTreeView treeView; + object splitterState; + + void OnEnable() + { + window = this; // set singleton. + splitterState = SplitterGUILayout.CreateSplitterState(new float[] { 75f, 25f }, new int[] { 32, 32 }, null); + treeView = new UniTaskTrackerTreeView(); + TaskTracker.EditorEnableState.EnableAutoReload = EditorPrefs.GetBool(TaskTracker.EnableAutoReloadKey, false); + TaskTracker.EditorEnableState.EnableTracking = EditorPrefs.GetBool(TaskTracker.EnableTrackingKey, false); + TaskTracker.EditorEnableState.EnableStackTrace = EditorPrefs.GetBool(TaskTracker.EnableStackTraceKey, false); + } + + void OnGUI() + { + // Head + RenderHeadPanel(); + + // Splittable + SplitterGUILayout.BeginVerticalSplit(this.splitterState, EmptyLayoutOption); + { + // Column Tabble + RenderTable(); + + // StackTrace details + RenderDetailsPanel(); + } + SplitterGUILayout.EndVerticalSplit(); + } + + #region HeadPanel + + public static bool EnableAutoReload => TaskTracker.EditorEnableState.EnableAutoReload; + public static bool EnableTracking => TaskTracker.EditorEnableState.EnableTracking; + public static bool EnableStackTrace => TaskTracker.EditorEnableState.EnableStackTrace; + static readonly GUIContent EnableAutoReloadHeadContent = EditorGUIUtility.TrTextContent("Enable AutoReload", "Reload automatically.", (Texture)null); + static readonly GUIContent ReloadHeadContent = EditorGUIUtility.TrTextContent("Reload", "Reload View.", (Texture)null); + static readonly GUIContent GCHeadContent = EditorGUIUtility.TrTextContent("GC.Collect", "Invoke GC.Collect.", (Texture)null); + static readonly GUIContent EnableTrackingHeadContent = EditorGUIUtility.TrTextContent("Enable Tracking", "Start to track async/await UniTask. Performance impact: low", (Texture)null); + static readonly GUIContent EnableStackTraceHeadContent = EditorGUIUtility.TrTextContent("Enable StackTrace", "Capture StackTrace when task is started. Performance impact: high", (Texture)null); + + // [Enable Tracking] | [Enable StackTrace] + void RenderHeadPanel() + { + EditorGUILayout.BeginVertical(EmptyLayoutOption); + EditorGUILayout.BeginHorizontal(EditorStyles.toolbar, EmptyLayoutOption); + + if (GUILayout.Toggle(EnableAutoReload, EnableAutoReloadHeadContent, EditorStyles.toolbarButton, EmptyLayoutOption) != EnableAutoReload) + { + TaskTracker.EditorEnableState.EnableAutoReload = !EnableAutoReload; + } + + if (GUILayout.Toggle(EnableTracking, EnableTrackingHeadContent, EditorStyles.toolbarButton, EmptyLayoutOption) != EnableTracking) + { + TaskTracker.EditorEnableState.EnableTracking = !EnableTracking; + } + + if (GUILayout.Toggle(EnableStackTrace, EnableStackTraceHeadContent, EditorStyles.toolbarButton, EmptyLayoutOption) != EnableStackTrace) + { + TaskTracker.EditorEnableState.EnableStackTrace = !EnableStackTrace; + } + + GUILayout.FlexibleSpace(); + + if (GUILayout.Button(ReloadHeadContent, EditorStyles.toolbarButton, EmptyLayoutOption)) + { + TaskTracker.CheckAndResetDirty(); + treeView.ReloadAndSort(); + Repaint(); + } + + if (GUILayout.Button(GCHeadContent, EditorStyles.toolbarButton, EmptyLayoutOption)) + { + GC.Collect(0); + } + + EditorGUILayout.EndHorizontal(); + EditorGUILayout.EndVertical(); + } + + #endregion + + #region TableColumn + + Vector2 tableScroll; + GUIStyle tableListStyle; + + void RenderTable() + { + if (tableListStyle == null) + { + tableListStyle = new GUIStyle("CN Box"); + tableListStyle.margin.top = 0; + tableListStyle.padding.left = 3; + } + + EditorGUILayout.BeginVertical(tableListStyle, EmptyLayoutOption); + + this.tableScroll = EditorGUILayout.BeginScrollView(this.tableScroll, new GUILayoutOption[] + { + GUILayout.ExpandWidth(true), + GUILayout.MaxWidth(2000f) + }); + var controlRect = EditorGUILayout.GetControlRect(new GUILayoutOption[] + { + GUILayout.ExpandHeight(true), + GUILayout.ExpandWidth(true) + }); + + + treeView?.OnGUI(controlRect); + + EditorGUILayout.EndScrollView(); + EditorGUILayout.EndVertical(); + } + + private void Update() + { + if (EnableAutoReload) + { + if (interval++ % 120 == 0) + { + if (TaskTracker.CheckAndResetDirty()) + { + treeView.ReloadAndSort(); + Repaint(); + } + } + } + } + + #endregion + + #region Details + + static GUIStyle detailsStyle; + Vector2 detailsScroll; + + void RenderDetailsPanel() + { + if (detailsStyle == null) + { + detailsStyle = new GUIStyle("CN Message"); + detailsStyle.wordWrap = false; + detailsStyle.stretchHeight = true; + detailsStyle.margin.right = 15; + } + + string message = ""; + var selected = treeView.state.selectedIDs; + if (selected.Count > 0) + { + var first = selected[0]; + var item = treeView.CurrentBindingItems.FirstOrDefault(x => x.id == first) as UniTaskTrackerViewItem; + if (item != null) + { + message = item.Position; + } + } + + detailsScroll = EditorGUILayout.BeginScrollView(this.detailsScroll, EmptyLayoutOption); + var vector = detailsStyle.CalcSize(new GUIContent(message)); + EditorGUILayout.SelectableLabel(message, detailsStyle, new GUILayoutOption[] + { + GUILayout.ExpandHeight(true), + GUILayout.ExpandWidth(true), + GUILayout.MinWidth(vector.x), + GUILayout.MinHeight(vector.y) + }); + EditorGUILayout.EndScrollView(); + } + + #endregion + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTaskTrackerWindow.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTaskTrackerWindow.cs.meta new file mode 100644 index 0000000..ba1b704 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Editor/UniTaskTrackerWindow.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5bee3e3860e37484aa3b861bf76d129f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime.meta new file mode 100644 index 0000000..4ccbc47 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: aa765154468d4b34eb34304100d39e64 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncLazy.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncLazy.cs new file mode 100644 index 0000000..51bfadc --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncLazy.cs @@ -0,0 +1,245 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks +{ + public class AsyncLazy + { + static Action continuation = SetCompletionSource; + + Func taskFactory; + UniTaskCompletionSource completionSource; + UniTask.Awaiter awaiter; + + object syncLock; + bool initialized; + + public AsyncLazy(Func taskFactory) + { + this.taskFactory = taskFactory; + this.completionSource = new UniTaskCompletionSource(); + this.syncLock = new object(); + this.initialized = false; + } + + internal AsyncLazy(UniTask task) + { + this.taskFactory = null; + this.completionSource = new UniTaskCompletionSource(); + this.syncLock = null; + this.initialized = true; + + var awaiter = task.GetAwaiter(); + if (awaiter.IsCompleted) + { + SetCompletionSource(awaiter); + } + else + { + this.awaiter = awaiter; + awaiter.SourceOnCompleted(continuation, this); + } + } + + public UniTask Task + { + get + { + EnsureInitialized(); + return completionSource.Task; + } + } + + + public UniTask.Awaiter GetAwaiter() => Task.GetAwaiter(); + + void EnsureInitialized() + { + if (Volatile.Read(ref initialized)) + { + return; + } + + EnsureInitializedCore(); + } + + void EnsureInitializedCore() + { + lock (syncLock) + { + if (!Volatile.Read(ref initialized)) + { + var f = Interlocked.Exchange(ref taskFactory, null); + if (f != null) + { + var task = f(); + var awaiter = task.GetAwaiter(); + if (awaiter.IsCompleted) + { + SetCompletionSource(awaiter); + } + else + { + this.awaiter = awaiter; + awaiter.SourceOnCompleted(continuation, this); + } + + Volatile.Write(ref initialized, true); + } + } + } + } + + void SetCompletionSource(in UniTask.Awaiter awaiter) + { + try + { + awaiter.GetResult(); + completionSource.TrySetResult(); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + static void SetCompletionSource(object state) + { + var self = (AsyncLazy)state; + try + { + self.awaiter.GetResult(); + self.completionSource.TrySetResult(); + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + } + finally + { + self.awaiter = default; + } + } + } + + public class AsyncLazy + { + static Action continuation = SetCompletionSource; + + Func> taskFactory; + UniTaskCompletionSource completionSource; + UniTask.Awaiter awaiter; + + object syncLock; + bool initialized; + + public AsyncLazy(Func> taskFactory) + { + this.taskFactory = taskFactory; + this.completionSource = new UniTaskCompletionSource(); + this.syncLock = new object(); + this.initialized = false; + } + + internal AsyncLazy(UniTask task) + { + this.taskFactory = null; + this.completionSource = new UniTaskCompletionSource(); + this.syncLock = null; + this.initialized = true; + + var awaiter = task.GetAwaiter(); + if (awaiter.IsCompleted) + { + SetCompletionSource(awaiter); + } + else + { + this.awaiter = awaiter; + awaiter.SourceOnCompleted(continuation, this); + } + } + + public UniTask Task + { + get + { + EnsureInitialized(); + return completionSource.Task; + } + } + + + public UniTask.Awaiter GetAwaiter() => Task.GetAwaiter(); + + void EnsureInitialized() + { + if (Volatile.Read(ref initialized)) + { + return; + } + + EnsureInitializedCore(); + } + + void EnsureInitializedCore() + { + lock (syncLock) + { + if (!Volatile.Read(ref initialized)) + { + var f = Interlocked.Exchange(ref taskFactory, null); + if (f != null) + { + var task = f(); + var awaiter = task.GetAwaiter(); + if (awaiter.IsCompleted) + { + SetCompletionSource(awaiter); + } + else + { + this.awaiter = awaiter; + awaiter.SourceOnCompleted(continuation, this); + } + + Volatile.Write(ref initialized, true); + } + } + } + } + + void SetCompletionSource(in UniTask.Awaiter awaiter) + { + try + { + var result = awaiter.GetResult(); + completionSource.TrySetResult(result); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + static void SetCompletionSource(object state) + { + var self = (AsyncLazy)state; + try + { + var result = self.awaiter.GetResult(); + self.completionSource.TrySetResult(result); + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + } + finally + { + self.awaiter = default; + } + } + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncLazy.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncLazy.cs.meta new file mode 100644 index 0000000..554d162 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncLazy.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 01d1404ca421466419a7db7340ff5e77 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncReactiveProperty.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncReactiveProperty.cs new file mode 100644 index 0000000..a08844d --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncReactiveProperty.cs @@ -0,0 +1,644 @@ +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks +{ + public interface IReadOnlyAsyncReactiveProperty : IUniTaskAsyncEnumerable + { + T Value { get; } + IUniTaskAsyncEnumerable WithoutCurrent(); + UniTask WaitAsync(CancellationToken cancellationToken = default); + } + + public interface IAsyncReactiveProperty : IReadOnlyAsyncReactiveProperty + { + new T Value { get; set; } + } + + [Serializable] + public class AsyncReactiveProperty : IAsyncReactiveProperty, IDisposable + { + TriggerEvent triggerEvent; + +#if UNITY_2018_3_OR_NEWER + [UnityEngine.SerializeField] +#endif + T latestValue; + + public T Value + { + get + { + return latestValue; + } + set + { + this.latestValue = value; + triggerEvent.SetResult(value); + } + } + + public AsyncReactiveProperty(T value) + { + this.latestValue = value; + this.triggerEvent = default; + } + + public IUniTaskAsyncEnumerable WithoutCurrent() + { + return new WithoutCurrentEnumerable(this); + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken) + { + return new Enumerator(this, cancellationToken, true); + } + + public void Dispose() + { + triggerEvent.SetCompleted(); + } + + public static implicit operator T(AsyncReactiveProperty value) + { + return value.Value; + } + + public override string ToString() + { + if (isValueType) return latestValue.ToString(); + return latestValue?.ToString(); + } + + public UniTask WaitAsync(CancellationToken cancellationToken = default) + { + return new UniTask(WaitAsyncSource.Create(this, cancellationToken, out var token), token); + } + + static bool isValueType; + + static AsyncReactiveProperty() + { + isValueType = typeof(T).IsValueType; + } + + sealed class WaitAsyncSource : IUniTaskSource, ITriggerHandler, ITaskPoolNode + { + static Action cancellationCallback = CancellationCallback; + + static TaskPool pool; + WaitAsyncSource nextNode; + ref WaitAsyncSource ITaskPoolNode.NextNode => ref nextNode; + + static WaitAsyncSource() + { + TaskPool.RegisterSizeGetter(typeof(WaitAsyncSource), () => pool.Size); + } + + AsyncReactiveProperty parent; + CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + UniTaskCompletionSourceCore core; + + WaitAsyncSource() + { + } + + public static IUniTaskSource Create(AsyncReactiveProperty parent, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new WaitAsyncSource(); + } + + result.parent = parent; + result.cancellationToken = cancellationToken; + + if (cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, result); + } + + result.parent.triggerEvent.Add(result); + + TaskTracker.TrackActiveTask(result, 3); + + token = result.core.Version; + return result; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + cancellationTokenRegistration.Dispose(); + cancellationTokenRegistration = default; + parent.triggerEvent.Remove(this); + parent = null; + cancellationToken = default; + return pool.TryPush(this); + } + + static void CancellationCallback(object state) + { + var self = (WaitAsyncSource)state; + self.OnCanceled(self.cancellationToken); + } + + // IUniTaskSource + + public T GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + // ITriggerHandler + + ITriggerHandler ITriggerHandler.Prev { get; set; } + ITriggerHandler ITriggerHandler.Next { get; set; } + + public void OnCanceled(CancellationToken cancellationToken) + { + core.TrySetCanceled(cancellationToken); + } + + public void OnCompleted() + { + // Complete as Cancel. + core.TrySetCanceled(CancellationToken.None); + } + + public void OnError(Exception ex) + { + core.TrySetException(ex); + } + + public void OnNext(T value) + { + core.TrySetResult(value); + } + } + + sealed class WithoutCurrentEnumerable : IUniTaskAsyncEnumerable + { + readonly AsyncReactiveProperty parent; + + public WithoutCurrentEnumerable(AsyncReactiveProperty parent) + { + this.parent = parent; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new Enumerator(parent, cancellationToken, false); + } + } + + sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator, ITriggerHandler + { + static Action cancellationCallback = CancellationCallback; + + readonly AsyncReactiveProperty parent; + readonly CancellationToken cancellationToken; + readonly CancellationTokenRegistration cancellationTokenRegistration; + T value; + bool isDisposed; + bool firstCall; + + public Enumerator(AsyncReactiveProperty parent, CancellationToken cancellationToken, bool publishCurrentValue) + { + this.parent = parent; + this.cancellationToken = cancellationToken; + this.firstCall = publishCurrentValue; + + parent.triggerEvent.Add(this); + TaskTracker.TrackActiveTask(this, 3); + + if (cancellationToken.CanBeCanceled) + { + cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this); + } + } + + public T Current => value; + + ITriggerHandler ITriggerHandler.Prev { get; set; } + ITriggerHandler ITriggerHandler.Next { get; set; } + + public UniTask MoveNextAsync() + { + // raise latest value on first call. + if (firstCall) + { + firstCall = false; + value = parent.Value; + return CompletedTasks.True; + } + + completionSource.Reset(); + return new UniTask(this, completionSource.Version); + } + + public UniTask DisposeAsync() + { + if (!isDisposed) + { + isDisposed = true; + TaskTracker.RemoveTracking(this); + completionSource.TrySetCanceled(cancellationToken); + parent.triggerEvent.Remove(this); + } + return default; + } + + public void OnNext(T value) + { + this.value = value; + completionSource.TrySetResult(true); + } + + public void OnCanceled(CancellationToken cancellationToken) + { + DisposeAsync().Forget(); + } + + public void OnCompleted() + { + completionSource.TrySetResult(false); + } + + public void OnError(Exception ex) + { + completionSource.TrySetException(ex); + } + + static void CancellationCallback(object state) + { + var self = (Enumerator)state; + self.DisposeAsync().Forget(); + } + } + } + + public class ReadOnlyAsyncReactiveProperty : IReadOnlyAsyncReactiveProperty, IDisposable + { + TriggerEvent triggerEvent; + + T latestValue; + IUniTaskAsyncEnumerator enumerator; + + public T Value + { + get + { + return latestValue; + } + } + + public ReadOnlyAsyncReactiveProperty(T initialValue, IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + latestValue = initialValue; + ConsumeEnumerator(source, cancellationToken).Forget(); + } + + public ReadOnlyAsyncReactiveProperty(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + ConsumeEnumerator(source, cancellationToken).Forget(); + } + + async UniTaskVoid ConsumeEnumerator(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + enumerator = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await enumerator.MoveNextAsync()) + { + var value = enumerator.Current; + this.latestValue = value; + triggerEvent.SetResult(value); + } + } + finally + { + await enumerator.DisposeAsync(); + enumerator = null; + } + } + + public IUniTaskAsyncEnumerable WithoutCurrent() + { + return new WithoutCurrentEnumerable(this); + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken) + { + return new Enumerator(this, cancellationToken, true); + } + + public void Dispose() + { + if (enumerator != null) + { + enumerator.DisposeAsync().Forget(); + } + + triggerEvent.SetCompleted(); + } + + public static implicit operator T(ReadOnlyAsyncReactiveProperty value) + { + return value.Value; + } + + public override string ToString() + { + if (isValueType) return latestValue.ToString(); + return latestValue?.ToString(); + } + + public UniTask WaitAsync(CancellationToken cancellationToken = default) + { + return new UniTask(WaitAsyncSource.Create(this, cancellationToken, out var token), token); + } + + static bool isValueType; + + static ReadOnlyAsyncReactiveProperty() + { + isValueType = typeof(T).IsValueType; + } + + sealed class WaitAsyncSource : IUniTaskSource, ITriggerHandler, ITaskPoolNode + { + static Action cancellationCallback = CancellationCallback; + + static TaskPool pool; + WaitAsyncSource nextNode; + ref WaitAsyncSource ITaskPoolNode.NextNode => ref nextNode; + + static WaitAsyncSource() + { + TaskPool.RegisterSizeGetter(typeof(WaitAsyncSource), () => pool.Size); + } + + ReadOnlyAsyncReactiveProperty parent; + CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + UniTaskCompletionSourceCore core; + + WaitAsyncSource() + { + } + + public static IUniTaskSource Create(ReadOnlyAsyncReactiveProperty parent, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new WaitAsyncSource(); + } + + result.parent = parent; + result.cancellationToken = cancellationToken; + + if (cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, result); + } + + result.parent.triggerEvent.Add(result); + + TaskTracker.TrackActiveTask(result, 3); + + token = result.core.Version; + return result; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + cancellationTokenRegistration.Dispose(); + cancellationTokenRegistration = default; + parent.triggerEvent.Remove(this); + parent = null; + cancellationToken = default; + return pool.TryPush(this); + } + + static void CancellationCallback(object state) + { + var self = (WaitAsyncSource)state; + self.OnCanceled(self.cancellationToken); + } + + // IUniTaskSource + + public T GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + // ITriggerHandler + + ITriggerHandler ITriggerHandler.Prev { get; set; } + ITriggerHandler ITriggerHandler.Next { get; set; } + + public void OnCanceled(CancellationToken cancellationToken) + { + core.TrySetCanceled(cancellationToken); + } + + public void OnCompleted() + { + // Complete as Cancel. + core.TrySetCanceled(CancellationToken.None); + } + + public void OnError(Exception ex) + { + core.TrySetException(ex); + } + + public void OnNext(T value) + { + core.TrySetResult(value); + } + } + + sealed class WithoutCurrentEnumerable : IUniTaskAsyncEnumerable + { + readonly ReadOnlyAsyncReactiveProperty parent; + + public WithoutCurrentEnumerable(ReadOnlyAsyncReactiveProperty parent) + { + this.parent = parent; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new Enumerator(parent, cancellationToken, false); + } + } + + sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator, ITriggerHandler + { + static Action cancellationCallback = CancellationCallback; + + readonly ReadOnlyAsyncReactiveProperty parent; + readonly CancellationToken cancellationToken; + readonly CancellationTokenRegistration cancellationTokenRegistration; + T value; + bool isDisposed; + bool firstCall; + + public Enumerator(ReadOnlyAsyncReactiveProperty parent, CancellationToken cancellationToken, bool publishCurrentValue) + { + this.parent = parent; + this.cancellationToken = cancellationToken; + this.firstCall = publishCurrentValue; + + parent.triggerEvent.Add(this); + TaskTracker.TrackActiveTask(this, 3); + + if (cancellationToken.CanBeCanceled) + { + cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this); + } + } + + public T Current => value; + ITriggerHandler ITriggerHandler.Prev { get; set; } + ITriggerHandler ITriggerHandler.Next { get; set; } + + public UniTask MoveNextAsync() + { + // raise latest value on first call. + if (firstCall) + { + firstCall = false; + value = parent.Value; + return CompletedTasks.True; + } + + completionSource.Reset(); + return new UniTask(this, completionSource.Version); + } + + public UniTask DisposeAsync() + { + if (!isDisposed) + { + isDisposed = true; + TaskTracker.RemoveTracking(this); + completionSource.TrySetCanceled(cancellationToken); + parent.triggerEvent.Remove(this); + } + return default; + } + + public void OnNext(T value) + { + this.value = value; + completionSource.TrySetResult(true); + } + + public void OnCanceled(CancellationToken cancellationToken) + { + DisposeAsync().Forget(); + } + + public void OnCompleted() + { + completionSource.TrySetResult(false); + } + + public void OnError(Exception ex) + { + completionSource.TrySetException(ex); + } + + static void CancellationCallback(object state) + { + var self = (Enumerator)state; + self.DisposeAsync().Forget(); + } + } + } + + public static class StateExtensions + { + public static ReadOnlyAsyncReactiveProperty ToReadOnlyAsyncReactiveProperty(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + return new ReadOnlyAsyncReactiveProperty(source, cancellationToken); + } + + public static ReadOnlyAsyncReactiveProperty ToReadOnlyAsyncReactiveProperty(this IUniTaskAsyncEnumerable source, T initialValue, CancellationToken cancellationToken) + { + return new ReadOnlyAsyncReactiveProperty(initialValue, source, cancellationToken); + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncReactiveProperty.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncReactiveProperty.cs.meta new file mode 100644 index 0000000..d64e3cf --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncReactiveProperty.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8ef320b87f537ee4fb2282e765dc6166 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncUnit.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncUnit.cs new file mode 100644 index 0000000..1d4bc74 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncUnit.cs @@ -0,0 +1,26 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or + +using System; + +namespace Cysharp.Threading.Tasks +{ + public readonly struct AsyncUnit : IEquatable + { + public static readonly AsyncUnit Default = new AsyncUnit(); + + public override int GetHashCode() + { + return 0; + } + + public bool Equals(AsyncUnit other) + { + return true; + } + + public override string ToString() + { + return "()"; + } + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncUnit.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncUnit.cs.meta new file mode 100644 index 0000000..e0ee132 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/AsyncUnit.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4f95ac245430d304bb5128d13b6becc8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenEqualityComparer.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenEqualityComparer.cs new file mode 100644 index 0000000..42e9445 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenEqualityComparer.cs @@ -0,0 +1,23 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks +{ + public class CancellationTokenEqualityComparer : IEqualityComparer + { + public static readonly IEqualityComparer Default = new CancellationTokenEqualityComparer(); + + public bool Equals(CancellationToken x, CancellationToken y) + { + return x.Equals(y); + } + + public int GetHashCode(CancellationToken obj) + { + return obj.GetHashCode(); + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenEqualityComparer.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenEqualityComparer.cs.meta new file mode 100644 index 0000000..a4fe3fd --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenEqualityComparer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7d739f510b125b74fa7290ac4335e46e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenExtensions.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenExtensions.cs new file mode 100644 index 0000000..3f3a532 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenExtensions.cs @@ -0,0 +1,182 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Runtime.CompilerServices; +using System.Threading; + +namespace Cysharp.Threading.Tasks +{ + public static class CancellationTokenExtensions + { + static readonly Action cancellationTokenCallback = Callback; + static readonly Action disposeCallback = DisposeCallback; + + public static CancellationToken ToCancellationToken(this UniTask task) + { + var cts = new CancellationTokenSource(); + ToCancellationTokenCore(task, cts).Forget(); + return cts.Token; + } + + public static CancellationToken ToCancellationToken(this UniTask task, CancellationToken linkToken) + { + if (linkToken.IsCancellationRequested) + { + return linkToken; + } + + if (!linkToken.CanBeCanceled) + { + return ToCancellationToken(task); + } + + var cts = CancellationTokenSource.CreateLinkedTokenSource(linkToken); + ToCancellationTokenCore(task, cts).Forget(); + + return cts.Token; + } + + public static CancellationToken ToCancellationToken(this UniTask task) + { + return ToCancellationToken(task.AsUniTask()); + } + + public static CancellationToken ToCancellationToken(this UniTask task, CancellationToken linkToken) + { + return ToCancellationToken(task.AsUniTask(), linkToken); + } + + static async UniTaskVoid ToCancellationTokenCore(UniTask task, CancellationTokenSource cts) + { + try + { + await task; + } + catch (Exception ex) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + } + cts.Cancel(); + cts.Dispose(); + } + + public static (UniTask, CancellationTokenRegistration) ToUniTask(this CancellationToken cancellationToken) + { + if (cancellationToken.IsCancellationRequested) + { + return (UniTask.FromCanceled(cancellationToken), default(CancellationTokenRegistration)); + } + + var promise = new UniTaskCompletionSource(); + return (promise.Task, cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationTokenCallback, promise)); + } + + static void Callback(object state) + { + var promise = (UniTaskCompletionSource)state; + promise.TrySetResult(); + } + + public static CancellationTokenAwaitable WaitUntilCanceled(this CancellationToken cancellationToken) + { + return new CancellationTokenAwaitable(cancellationToken); + } + + public static CancellationTokenRegistration RegisterWithoutCaptureExecutionContext(this CancellationToken cancellationToken, Action callback) + { + var restoreFlow = false; + if (!ExecutionContext.IsFlowSuppressed()) + { + ExecutionContext.SuppressFlow(); + restoreFlow = true; + } + + try + { + return cancellationToken.Register(callback, false); + } + finally + { + if (restoreFlow) + { + ExecutionContext.RestoreFlow(); + } + } + } + + public static CancellationTokenRegistration RegisterWithoutCaptureExecutionContext(this CancellationToken cancellationToken, Action callback, object state) + { + var restoreFlow = false; + if (!ExecutionContext.IsFlowSuppressed()) + { + ExecutionContext.SuppressFlow(); + restoreFlow = true; + } + + try + { + return cancellationToken.Register(callback, state, false); + } + finally + { + if (restoreFlow) + { + ExecutionContext.RestoreFlow(); + } + } + } + + public static CancellationTokenRegistration AddTo(this IDisposable disposable, CancellationToken cancellationToken) + { + return cancellationToken.RegisterWithoutCaptureExecutionContext(disposeCallback, disposable); + } + + static void DisposeCallback(object state) + { + var d = (IDisposable)state; + d.Dispose(); + } + } + + public struct CancellationTokenAwaitable + { + CancellationToken cancellationToken; + + public CancellationTokenAwaitable(CancellationToken cancellationToken) + { + this.cancellationToken = cancellationToken; + } + + public Awaiter GetAwaiter() + { + return new Awaiter(cancellationToken); + } + + public struct Awaiter : ICriticalNotifyCompletion + { + CancellationToken cancellationToken; + + public Awaiter(CancellationToken cancellationToken) + { + this.cancellationToken = cancellationToken; + } + + public bool IsCompleted => !cancellationToken.CanBeCanceled || cancellationToken.IsCancellationRequested; + + public void GetResult() + { + } + + public void OnCompleted(Action continuation) + { + UnsafeOnCompleted(continuation); + } + + public void UnsafeOnCompleted(Action continuation) + { + cancellationToken.RegisterWithoutCaptureExecutionContext(continuation); + } + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenExtensions.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenExtensions.cs.meta new file mode 100644 index 0000000..28a6958 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4be7209f04146bd45ac5ee775a5f7c26 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenSourceExtensions.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenSourceExtensions.cs new file mode 100644 index 0000000..c519944 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenSourceExtensions.cs @@ -0,0 +1,44 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System.Threading; +using UnityEngine; +using Cysharp.Threading.Tasks.Triggers; +using System; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks +{ + + public static partial class CancellationTokenSourceExtensions + { + readonly static Action CancelCancellationTokenSourceStateDelegate = new Action(CancelCancellationTokenSourceState); + + static void CancelCancellationTokenSourceState(object state) + { + var cts = (CancellationTokenSource)state; + cts.Cancel(); + } + + public static IDisposable CancelAfterSlim(this CancellationTokenSource cts, int millisecondsDelay, DelayType delayType = DelayType.DeltaTime, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update) + { + return CancelAfterSlim(cts, TimeSpan.FromMilliseconds(millisecondsDelay), delayType, delayTiming); + } + + public static IDisposable CancelAfterSlim(this CancellationTokenSource cts, TimeSpan delayTimeSpan, DelayType delayType = DelayType.DeltaTime, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update) + { + return PlayerLoopTimer.StartNew(delayTimeSpan, false, delayType, delayTiming, cts.Token, CancelCancellationTokenSourceStateDelegate, cts); + } + + public static void RegisterRaiseCancelOnDestroy(this CancellationTokenSource cts, Component component) + { + RegisterRaiseCancelOnDestroy(cts, component.gameObject); + } + + public static void RegisterRaiseCancelOnDestroy(this CancellationTokenSource cts, GameObject gameObject) + { + var trigger = gameObject.GetAsyncDestroyTrigger(); + trigger.CancellationToken.RegisterWithoutCaptureExecutionContext(CancelCancellationTokenSourceStateDelegate, cts); + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenSourceExtensions.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenSourceExtensions.cs.meta new file mode 100644 index 0000000..fd09fe4 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CancellationTokenSourceExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 22d85d07f1e70ab42a7a4c25bd65e661 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Channel.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Channel.cs new file mode 100644 index 0000000..5a484fd --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Channel.cs @@ -0,0 +1,450 @@ +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks +{ + public static class Channel + { + public static Channel CreateSingleConsumerUnbounded() + { + return new SingleConsumerUnboundedChannel(); + } + } + + public abstract class Channel + { + public ChannelReader Reader { get; protected set; } + public ChannelWriter Writer { get; protected set; } + + public static implicit operator ChannelReader(Channel channel) => channel.Reader; + public static implicit operator ChannelWriter(Channel channel) => channel.Writer; + } + + public abstract class Channel : Channel + { + } + + public abstract class ChannelReader + { + public abstract bool TryRead(out T item); + public abstract UniTask WaitToReadAsync(CancellationToken cancellationToken = default(CancellationToken)); + + public abstract UniTask Completion { get; } + + public virtual UniTask ReadAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + if (this.TryRead(out var item)) + { + return UniTask.FromResult(item); + } + + return ReadAsyncCore(cancellationToken); + } + + async UniTask ReadAsyncCore(CancellationToken cancellationToken = default(CancellationToken)) + { + if (await WaitToReadAsync(cancellationToken)) + { + if (TryRead(out var item)) + { + return item; + } + } + + throw new ChannelClosedException(); + } + + public abstract IUniTaskAsyncEnumerable ReadAllAsync(CancellationToken cancellationToken = default(CancellationToken)); + } + + public abstract class ChannelWriter + { + public abstract bool TryWrite(T item); + public abstract bool TryComplete(Exception error = null); + + public void Complete(Exception error = null) + { + if (!TryComplete(error)) + { + throw new ChannelClosedException(); + } + } + } + + public partial class ChannelClosedException : InvalidOperationException + { + public ChannelClosedException() : + base("Channel is already closed.") + { } + + public ChannelClosedException(string message) : base(message) { } + + public ChannelClosedException(Exception innerException) : + base("Channel is already closed", innerException) + { } + + public ChannelClosedException(string message, Exception innerException) : base(message, innerException) { } + } + + internal class SingleConsumerUnboundedChannel : Channel + { + readonly Queue items; + readonly SingleConsumerUnboundedChannelReader readerSource; + UniTaskCompletionSource completedTaskSource; + UniTask completedTask; + + Exception completionError; + bool closed; + + public SingleConsumerUnboundedChannel() + { + items = new Queue(); + Writer = new SingleConsumerUnboundedChannelWriter(this); + readerSource = new SingleConsumerUnboundedChannelReader(this); + Reader = readerSource; + } + + sealed class SingleConsumerUnboundedChannelWriter : ChannelWriter + { + readonly SingleConsumerUnboundedChannel parent; + + public SingleConsumerUnboundedChannelWriter(SingleConsumerUnboundedChannel parent) + { + this.parent = parent; + } + + public override bool TryWrite(T item) + { + bool waiting; + lock (parent.items) + { + if (parent.closed) return false; + + parent.items.Enqueue(item); + waiting = parent.readerSource.isWaiting; + } + + if (waiting) + { + parent.readerSource.SingalContinuation(); + } + + return true; + } + + public override bool TryComplete(Exception error = null) + { + bool waiting; + lock (parent.items) + { + if (parent.closed) return false; + parent.closed = true; + waiting = parent.readerSource.isWaiting; + + if (parent.items.Count == 0) + { + if (error == null) + { + if (parent.completedTaskSource != null) + { + parent.completedTaskSource.TrySetResult(); + } + else + { + parent.completedTask = UniTask.CompletedTask; + } + } + else + { + if (parent.completedTaskSource != null) + { + parent.completedTaskSource.TrySetException(error); + } + else + { + parent.completedTask = UniTask.FromException(error); + } + } + + if (waiting) + { + parent.readerSource.SingalCompleted(error); + } + } + + parent.completionError = error; + } + + return true; + } + } + + sealed class SingleConsumerUnboundedChannelReader : ChannelReader, IUniTaskSource + { + readonly Action CancellationCallbackDelegate = CancellationCallback; + readonly SingleConsumerUnboundedChannel parent; + + CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + UniTaskCompletionSourceCore core; + internal bool isWaiting; + + public SingleConsumerUnboundedChannelReader(SingleConsumerUnboundedChannel parent) + { + this.parent = parent; + + TaskTracker.TrackActiveTask(this, 4); + } + + public override UniTask Completion + { + get + { + if (parent.completedTaskSource != null) return parent.completedTaskSource.Task; + + if (parent.closed) + { + return parent.completedTask; + } + + parent.completedTaskSource = new UniTaskCompletionSource(); + return parent.completedTaskSource.Task; + } + } + + public override bool TryRead(out T item) + { + lock (parent.items) + { + if (parent.items.Count != 0) + { + item = parent.items.Dequeue(); + + // complete when all value was consumed. + if (parent.closed && parent.items.Count == 0) + { + if (parent.completionError != null) + { + if (parent.completedTaskSource != null) + { + parent.completedTaskSource.TrySetException(parent.completionError); + } + else + { + parent.completedTask = UniTask.FromException(parent.completionError); + } + } + else + { + if (parent.completedTaskSource != null) + { + parent.completedTaskSource.TrySetResult(); + } + else + { + parent.completedTask = UniTask.CompletedTask; + } + } + } + } + else + { + item = default; + return false; + } + } + + return true; + } + + public override UniTask WaitToReadAsync(CancellationToken cancellationToken) + { + if (cancellationToken.IsCancellationRequested) + { + return UniTask.FromCanceled(cancellationToken); + } + + lock (parent.items) + { + if (parent.items.Count != 0) + { + return CompletedTasks.True; + } + + if (parent.closed) + { + if (parent.completionError == null) + { + return CompletedTasks.False; + } + else + { + return UniTask.FromException(parent.completionError); + } + } + + cancellationTokenRegistration.Dispose(); + + core.Reset(); + isWaiting = true; + + this.cancellationToken = cancellationToken; + if (this.cancellationToken.CanBeCanceled) + { + cancellationTokenRegistration = this.cancellationToken.RegisterWithoutCaptureExecutionContext(CancellationCallbackDelegate, this); + } + + return new UniTask(this, core.Version); + } + } + + public void SingalContinuation() + { + core.TrySetResult(true); + } + + public void SingalCancellation(CancellationToken cancellationToken) + { + TaskTracker.RemoveTracking(this); + core.TrySetCanceled(cancellationToken); + } + + public void SingalCompleted(Exception error) + { + if (error != null) + { + TaskTracker.RemoveTracking(this); + core.TrySetException(error); + } + else + { + TaskTracker.RemoveTracking(this); + core.TrySetResult(false); + } + } + + public override IUniTaskAsyncEnumerable ReadAllAsync(CancellationToken cancellationToken = default) + { + return new ReadAllAsyncEnumerable(this, cancellationToken); + } + + bool IUniTaskSource.GetResult(short token) + { + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + core.GetResult(token); + } + + UniTaskStatus IUniTaskSource.GetStatus(short token) + { + return core.GetStatus(token); + } + + void IUniTaskSource.OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + UniTaskStatus IUniTaskSource.UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + static void CancellationCallback(object state) + { + var self = (SingleConsumerUnboundedChannelReader)state; + self.SingalCancellation(self.cancellationToken); + } + + sealed class ReadAllAsyncEnumerable : IUniTaskAsyncEnumerable, IUniTaskAsyncEnumerator + { + readonly Action CancellationCallback1Delegate = CancellationCallback1; + readonly Action CancellationCallback2Delegate = CancellationCallback2; + + readonly SingleConsumerUnboundedChannelReader parent; + CancellationToken cancellationToken1; + CancellationToken cancellationToken2; + CancellationTokenRegistration cancellationTokenRegistration1; + CancellationTokenRegistration cancellationTokenRegistration2; + + T current; + bool cacheValue; + bool running; + + public ReadAllAsyncEnumerable(SingleConsumerUnboundedChannelReader parent, CancellationToken cancellationToken) + { + this.parent = parent; + this.cancellationToken1 = cancellationToken; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + if (running) + { + throw new InvalidOperationException("Enumerator is already running, does not allow call GetAsyncEnumerator twice."); + } + + if (this.cancellationToken1 != cancellationToken) + { + this.cancellationToken2 = cancellationToken; + } + + if (this.cancellationToken1.CanBeCanceled) + { + this.cancellationTokenRegistration1 = this.cancellationToken1.RegisterWithoutCaptureExecutionContext(CancellationCallback1Delegate, this); + } + + if (this.cancellationToken2.CanBeCanceled) + { + this.cancellationTokenRegistration2 = this.cancellationToken2.RegisterWithoutCaptureExecutionContext(CancellationCallback2Delegate, this); + } + + running = true; + return this; + } + + public T Current + { + get + { + if (cacheValue) + { + return current; + } + parent.TryRead(out current); + return current; + } + } + + public UniTask MoveNextAsync() + { + cacheValue = false; + return parent.WaitToReadAsync(CancellationToken.None); // ok to use None, registered in ctor. + } + + public UniTask DisposeAsync() + { + cancellationTokenRegistration1.Dispose(); + cancellationTokenRegistration2.Dispose(); + return default; + } + + static void CancellationCallback1(object state) + { + var self = (ReadAllAsyncEnumerable)state; + self.parent.SingalCancellation(self.cancellationToken1); + } + + static void CancellationCallback2(object state) + { + var self = (ReadAllAsyncEnumerable)state; + self.parent.SingalCancellation(self.cancellationToken2); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Channel.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Channel.cs.meta new file mode 100644 index 0000000..32edb9c --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Channel.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5ceb3107bbdd1f14eb39091273798360 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices.meta new file mode 100644 index 0000000..867c29f --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 64b064347ca7a404494a996b072e2e29 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncMethodBuilderAttribute.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncMethodBuilderAttribute.cs new file mode 100644 index 0000000..700fc33 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncMethodBuilderAttribute.cs @@ -0,0 +1,17 @@ + +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member +#pragma warning disable CS0436 + +namespace System.Runtime.CompilerServices +{ + internal sealed class AsyncMethodBuilderAttribute : Attribute + { + public Type BuilderType { get; } + + public AsyncMethodBuilderAttribute(Type builderType) + { + BuilderType = builderType; + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncMethodBuilderAttribute.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncMethodBuilderAttribute.cs.meta new file mode 100644 index 0000000..19961df --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncMethodBuilderAttribute.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 02ce354d37b10454e8376062f7cbe57a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncUniTaskMethodBuilder.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncUniTaskMethodBuilder.cs new file mode 100644 index 0000000..1aa990d --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncUniTaskMethodBuilder.cs @@ -0,0 +1,269 @@ + +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Security; + +namespace Cysharp.Threading.Tasks.CompilerServices +{ + [StructLayout(LayoutKind.Auto)] + public struct AsyncUniTaskMethodBuilder + { + IStateMachineRunnerPromise runnerPromise; + Exception ex; + + // 1. Static Create method. + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static AsyncUniTaskMethodBuilder Create() + { + return default; + } + + // 2. TaskLike Task property. + public UniTask Task + { + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + if (runnerPromise != null) + { + return runnerPromise.Task; + } + else if (ex != null) + { + return UniTask.FromException(ex); + } + else + { + return UniTask.CompletedTask; + } + } + } + + // 3. SetException + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetException(Exception exception) + { + if (runnerPromise == null) + { + ex = exception; + } + else + { + runnerPromise.SetException(exception); + } + } + + // 4. SetResult + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetResult() + { + if (runnerPromise != null) + { + runnerPromise.SetResult(); + } + } + + // 5. AwaitOnCompleted + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AwaitOnCompleted(ref TAwaiter awaiter, ref TStateMachine stateMachine) + where TAwaiter : INotifyCompletion + where TStateMachine : IAsyncStateMachine + { + if (runnerPromise == null) + { + AsyncUniTask.SetStateMachine(ref stateMachine, ref runnerPromise); + } + + awaiter.OnCompleted(runnerPromise.MoveNext); + } + + // 6. AwaitUnsafeOnCompleted + [DebuggerHidden] + [SecuritySafeCritical] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AwaitUnsafeOnCompleted(ref TAwaiter awaiter, ref TStateMachine stateMachine) + where TAwaiter : ICriticalNotifyCompletion + where TStateMachine : IAsyncStateMachine + { + if (runnerPromise == null) + { + AsyncUniTask.SetStateMachine(ref stateMachine, ref runnerPromise); + } + + awaiter.UnsafeOnCompleted(runnerPromise.MoveNext); + } + + // 7. Start + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Start(ref TStateMachine stateMachine) + where TStateMachine : IAsyncStateMachine + { + stateMachine.MoveNext(); + } + + // 8. SetStateMachine + [DebuggerHidden] + public void SetStateMachine(IAsyncStateMachine stateMachine) + { + // don't use boxed stateMachine. + } + +#if DEBUG || !UNITY_2018_3_OR_NEWER + // Important for IDE debugger. + object debuggingId; + private object ObjectIdForDebugger + { + get + { + if (debuggingId == null) + { + debuggingId = new object(); + } + return debuggingId; + } + } +#endif + } + + [StructLayout(LayoutKind.Auto)] + public struct AsyncUniTaskMethodBuilder + { + IStateMachineRunnerPromise runnerPromise; + Exception ex; + T result; + + // 1. Static Create method. + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static AsyncUniTaskMethodBuilder Create() + { + return default; + } + + // 2. TaskLike Task property. + public UniTask Task + { + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + if (runnerPromise != null) + { + return runnerPromise.Task; + } + else if (ex != null) + { + return UniTask.FromException(ex); + } + else + { + return UniTask.FromResult(result); + } + } + } + + // 3. SetException + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetException(Exception exception) + { + if (runnerPromise == null) + { + ex = exception; + } + else + { + runnerPromise.SetException(exception); + } + } + + // 4. SetResult + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetResult(T result) + { + if (runnerPromise == null) + { + this.result = result; + } + else + { + runnerPromise.SetResult(result); + } + } + + // 5. AwaitOnCompleted + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AwaitOnCompleted(ref TAwaiter awaiter, ref TStateMachine stateMachine) + where TAwaiter : INotifyCompletion + where TStateMachine : IAsyncStateMachine + { + if (runnerPromise == null) + { + AsyncUniTask.SetStateMachine(ref stateMachine, ref runnerPromise); + } + + awaiter.OnCompleted(runnerPromise.MoveNext); + } + + // 6. AwaitUnsafeOnCompleted + [DebuggerHidden] + [SecuritySafeCritical] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AwaitUnsafeOnCompleted(ref TAwaiter awaiter, ref TStateMachine stateMachine) + where TAwaiter : ICriticalNotifyCompletion + where TStateMachine : IAsyncStateMachine + { + if (runnerPromise == null) + { + AsyncUniTask.SetStateMachine(ref stateMachine, ref runnerPromise); + } + + awaiter.UnsafeOnCompleted(runnerPromise.MoveNext); + } + + // 7. Start + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Start(ref TStateMachine stateMachine) + where TStateMachine : IAsyncStateMachine + { + stateMachine.MoveNext(); + } + + // 8. SetStateMachine + [DebuggerHidden] + public void SetStateMachine(IAsyncStateMachine stateMachine) + { + // don't use boxed stateMachine. + } + +#if DEBUG || !UNITY_2018_3_OR_NEWER + // Important for IDE debugger. + object debuggingId; + private object ObjectIdForDebugger + { + get + { + if (debuggingId == null) + { + debuggingId = new object(); + } + return debuggingId; + } + } +#endif + + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncUniTaskMethodBuilder.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncUniTaskMethodBuilder.cs.meta new file mode 100644 index 0000000..ad43cfc --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncUniTaskMethodBuilder.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 68d72a45afdec574ebc26e7de2c38330 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncUniTaskVoidMethodBuilder.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncUniTaskVoidMethodBuilder.cs new file mode 100644 index 0000000..82e91f3 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncUniTaskVoidMethodBuilder.cs @@ -0,0 +1,137 @@ + +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Security; + +namespace Cysharp.Threading.Tasks.CompilerServices +{ + [StructLayout(LayoutKind.Auto)] + public struct AsyncUniTaskVoidMethodBuilder + { + IStateMachineRunner runner; + + // 1. Static Create method. + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static AsyncUniTaskVoidMethodBuilder Create() + { + return default; + } + + // 2. TaskLike Task property(void) + public UniTaskVoid Task + { + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + return default; + } + } + + // 3. SetException + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetException(Exception exception) + { + // runner is finished, return first. + if (runner != null) + { +#if ENABLE_IL2CPP + // workaround for IL2CPP bug. + PlayerLoopHelper.AddContinuation(PlayerLoopTiming.LastPostLateUpdate, runner.ReturnAction); +#else + runner.Return(); +#endif + runner = null; + } + + UniTaskScheduler.PublishUnobservedTaskException(exception); + } + + // 4. SetResult + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetResult() + { + // runner is finished, return. + if (runner != null) + { +#if ENABLE_IL2CPP + // workaround for IL2CPP bug. + PlayerLoopHelper.AddContinuation(PlayerLoopTiming.LastPostLateUpdate, runner.ReturnAction); +#else + runner.Return(); +#endif + runner = null; + } + } + + // 5. AwaitOnCompleted + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AwaitOnCompleted(ref TAwaiter awaiter, ref TStateMachine stateMachine) + where TAwaiter : INotifyCompletion + where TStateMachine : IAsyncStateMachine + { + if (runner == null) + { + AsyncUniTaskVoid.SetStateMachine(ref stateMachine, ref runner); + } + + awaiter.OnCompleted(runner.MoveNext); + } + + // 6. AwaitUnsafeOnCompleted + [DebuggerHidden] + [SecuritySafeCritical] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AwaitUnsafeOnCompleted(ref TAwaiter awaiter, ref TStateMachine stateMachine) + where TAwaiter : ICriticalNotifyCompletion + where TStateMachine : IAsyncStateMachine + { + if (runner == null) + { + AsyncUniTaskVoid.SetStateMachine(ref stateMachine, ref runner); + } + + awaiter.UnsafeOnCompleted(runner.MoveNext); + } + + // 7. Start + [DebuggerHidden] + public void Start(ref TStateMachine stateMachine) + where TStateMachine : IAsyncStateMachine + { + stateMachine.MoveNext(); + } + + // 8. SetStateMachine + [DebuggerHidden] + public void SetStateMachine(IAsyncStateMachine stateMachine) + { + // don't use boxed stateMachine. + } + +#if DEBUG || !UNITY_2018_3_OR_NEWER + // Important for IDE debugger. + object debuggingId; + private object ObjectIdForDebugger + { + get + { + if (debuggingId == null) + { + debuggingId = new object(); + } + return debuggingId; + } + } +#endif + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncUniTaskVoidMethodBuilder.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncUniTaskVoidMethodBuilder.cs.meta new file mode 100644 index 0000000..9bcc50e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/AsyncUniTaskVoidMethodBuilder.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e891aaac17b933a47a9d7fa3b8e1226f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/StateMachineRunner.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/StateMachineRunner.cs new file mode 100644 index 0000000..1cffece --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/StateMachineRunner.cs @@ -0,0 +1,380 @@ +#pragma warning disable CS1591 + +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Linq; +using System.Diagnostics; +using System.Runtime.CompilerServices; + +namespace Cysharp.Threading.Tasks.CompilerServices +{ + // #ENABLE_IL2CPP in this file is to avoid bug of IL2CPP VM. + // Issue is tracked on https://issuetracker.unity3d.com/issues/il2cpp-incorrect-results-when-calling-a-method-from-outside-class-in-a-struct + // but currently it is labeled `Won't Fix`. + + internal interface IStateMachineRunner + { + Action MoveNext { get; } + void Return(); + +#if ENABLE_IL2CPP + Action ReturnAction { get; } +#endif + } + + internal interface IStateMachineRunnerPromise : IUniTaskSource + { + Action MoveNext { get; } + UniTask Task { get; } + void SetResult(); + void SetException(Exception exception); + } + + internal interface IStateMachineRunnerPromise : IUniTaskSource + { + Action MoveNext { get; } + UniTask Task { get; } + void SetResult(T result); + void SetException(Exception exception); + } + + internal static class StateMachineUtility + { + // Get AsyncStateMachine internal state to check IL2CPP bug + public static int GetState(IAsyncStateMachine stateMachine) + { + var info = stateMachine.GetType().GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) + .First(x => x.Name.EndsWith("__state")); + return (int)info.GetValue(stateMachine); + } + } + + internal sealed class AsyncUniTaskVoid : IStateMachineRunner, ITaskPoolNode>, IUniTaskSource + where TStateMachine : IAsyncStateMachine + { + static TaskPool> pool; + +#if ENABLE_IL2CPP + public Action ReturnAction { get; } +#endif + + TStateMachine stateMachine; + + public Action MoveNext { get; } + + public AsyncUniTaskVoid() + { + MoveNext = Run; +#if ENABLE_IL2CPP + ReturnAction = Return; +#endif + } + + public static void SetStateMachine(ref TStateMachine stateMachine, ref IStateMachineRunner runnerFieldRef) + { + if (!pool.TryPop(out var result)) + { + result = new AsyncUniTaskVoid(); + } + TaskTracker.TrackActiveTask(result, 3); + + runnerFieldRef = result; // set runner before copied. + result.stateMachine = stateMachine; // copy struct StateMachine(in release build). + } + + static AsyncUniTaskVoid() + { + TaskPool.RegisterSizeGetter(typeof(AsyncUniTaskVoid), () => pool.Size); + } + + AsyncUniTaskVoid nextNode; + public ref AsyncUniTaskVoid NextNode => ref nextNode; + + public void Return() + { + TaskTracker.RemoveTracking(this); + stateMachine = default; + pool.TryPush(this); + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + void Run() + { + stateMachine.MoveNext(); + } + + // dummy interface implementation for TaskTracker. + + UniTaskStatus IUniTaskSource.GetStatus(short token) + { + return UniTaskStatus.Pending; + } + + UniTaskStatus IUniTaskSource.UnsafeGetStatus() + { + return UniTaskStatus.Pending; + } + + void IUniTaskSource.OnCompleted(Action continuation, object state, short token) + { + } + + void IUniTaskSource.GetResult(short token) + { + } + } + + internal sealed class AsyncUniTask : IStateMachineRunnerPromise, IUniTaskSource, ITaskPoolNode> + where TStateMachine : IAsyncStateMachine + { + static TaskPool> pool; + +#if ENABLE_IL2CPP + readonly Action returnDelegate; +#endif + public Action MoveNext { get; } + + TStateMachine stateMachine; + UniTaskCompletionSourceCore core; + + AsyncUniTask() + { + MoveNext = Run; +#if ENABLE_IL2CPP + returnDelegate = Return; +#endif + } + + public static void SetStateMachine(ref TStateMachine stateMachine, ref IStateMachineRunnerPromise runnerPromiseFieldRef) + { + if (!pool.TryPop(out var result)) + { + result = new AsyncUniTask(); + } + TaskTracker.TrackActiveTask(result, 3); + + runnerPromiseFieldRef = result; // set runner before copied. + result.stateMachine = stateMachine; // copy struct StateMachine(in release build). + } + + AsyncUniTask nextNode; + public ref AsyncUniTask NextNode => ref nextNode; + + static AsyncUniTask() + { + TaskPool.RegisterSizeGetter(typeof(AsyncUniTask), () => pool.Size); + } + + void Return() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + stateMachine = default; + pool.TryPush(this); + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + stateMachine = default; + return pool.TryPush(this); + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + void Run() + { + stateMachine.MoveNext(); + } + + public UniTask Task + { + [DebuggerHidden] + get + { + return new UniTask(this, core.Version); + } + } + + [DebuggerHidden] + public void SetResult() + { + core.TrySetResult(AsyncUnit.Default); + } + + [DebuggerHidden] + public void SetException(Exception exception) + { + core.TrySetException(exception); + } + + [DebuggerHidden] + public void GetResult(short token) + { + try + { + core.GetResult(token); + } + finally + { +#if ENABLE_IL2CPP + // workaround for IL2CPP bug. + PlayerLoopHelper.AddContinuation(PlayerLoopTiming.LastPostLateUpdate, returnDelegate); +#else + TryReturn(); +#endif + } + } + + [DebuggerHidden] + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + [DebuggerHidden] + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + [DebuggerHidden] + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + internal sealed class AsyncUniTask : IStateMachineRunnerPromise, IUniTaskSource, ITaskPoolNode> + where TStateMachine : IAsyncStateMachine + { + static TaskPool> pool; + +#if ENABLE_IL2CPP + readonly Action returnDelegate; +#endif + + public Action MoveNext { get; } + + TStateMachine stateMachine; + UniTaskCompletionSourceCore core; + + AsyncUniTask() + { + MoveNext = Run; +#if ENABLE_IL2CPP + returnDelegate = Return; +#endif + } + + public static void SetStateMachine(ref TStateMachine stateMachine, ref IStateMachineRunnerPromise runnerPromiseFieldRef) + { + if (!pool.TryPop(out var result)) + { + result = new AsyncUniTask(); + } + TaskTracker.TrackActiveTask(result, 3); + + runnerPromiseFieldRef = result; // set runner before copied. + result.stateMachine = stateMachine; // copy struct StateMachine(in release build). + } + + AsyncUniTask nextNode; + public ref AsyncUniTask NextNode => ref nextNode; + + static AsyncUniTask() + { + TaskPool.RegisterSizeGetter(typeof(AsyncUniTask), () => pool.Size); + } + + void Return() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + stateMachine = default; + pool.TryPush(this); + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + stateMachine = default; + return pool.TryPush(this); + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + void Run() + { + // UnityEngine.Debug.Log($"MoveNext State:" + StateMachineUtility.GetState(stateMachine)); + stateMachine.MoveNext(); + } + + public UniTask Task + { + [DebuggerHidden] + get + { + return new UniTask(this, core.Version); + } + } + + [DebuggerHidden] + public void SetResult(T result) + { + core.TrySetResult(result); + } + + [DebuggerHidden] + public void SetException(Exception exception) + { + core.TrySetException(exception); + } + + [DebuggerHidden] + public T GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { +#if ENABLE_IL2CPP + // workaround for IL2CPP bug. + PlayerLoopHelper.AddContinuation(PlayerLoopTiming.LastPostLateUpdate, returnDelegate); +#else + TryReturn(); +#endif + } + } + + [DebuggerHidden] + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + [DebuggerHidden] + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + [DebuggerHidden] + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + [DebuggerHidden] + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/StateMachineRunner.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/StateMachineRunner.cs.meta new file mode 100644 index 0000000..2cb82e0 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/CompilerServices/StateMachineRunner.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 98649642833cabf44a9dc060ce4c84a1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/EnumerableAsyncExtensions.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/EnumerableAsyncExtensions.cs new file mode 100644 index 0000000..004e763 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/EnumerableAsyncExtensions.cs @@ -0,0 +1,34 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Collections.Generic; + +namespace Cysharp.Threading.Tasks +{ + public static class EnumerableAsyncExtensions + { + // overload resolver - .Select(async x => { }) : IEnumerable> + + public static IEnumerable Select(this IEnumerable source, Func selector) + { + return System.Linq.Enumerable.Select(source, selector); + } + + public static IEnumerable> Select(this IEnumerable source, Func> selector) + { + return System.Linq.Enumerable.Select(source, selector); + } + + public static IEnumerable Select(this IEnumerable source, Func selector) + { + return System.Linq.Enumerable.Select(source, selector); + } + + public static IEnumerable> Select(this IEnumerable source, Func> selector) + { + return System.Linq.Enumerable.Select(source, selector); + } + } +} + + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/EnumerableAsyncExtensions.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/EnumerableAsyncExtensions.cs.meta new file mode 100644 index 0000000..d2e4930 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/EnumerableAsyncExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ff50260d74bd54c4b92cf99895549445 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/EnumeratorAsyncExtensions.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/EnumeratorAsyncExtensions.cs new file mode 100644 index 0000000..785bbc2 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/EnumeratorAsyncExtensions.cs @@ -0,0 +1,287 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Collections; +using System.Reflection; +using System.Runtime.ExceptionServices; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; +using UnityEngine; + +namespace Cysharp.Threading.Tasks +{ + public static class EnumeratorAsyncExtensions + { + public static UniTask.Awaiter GetAwaiter(this T enumerator) + where T : IEnumerator + { + var e = (IEnumerator)enumerator; + Error.ThrowArgumentNullException(e, nameof(enumerator)); + return new UniTask(EnumeratorPromise.Create(e, PlayerLoopTiming.Update, CancellationToken.None, out var token), token).GetAwaiter(); + } + + public static UniTask WithCancellation(this IEnumerator enumerator, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(enumerator, nameof(enumerator)); + return new UniTask(EnumeratorPromise.Create(enumerator, PlayerLoopTiming.Update, cancellationToken, out var token), token); + } + + public static UniTask ToUniTask(this IEnumerator enumerator, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + Error.ThrowArgumentNullException(enumerator, nameof(enumerator)); + return new UniTask(EnumeratorPromise.Create(enumerator, timing, cancellationToken, out var token), token); + } + + public static UniTask ToUniTask(this IEnumerator enumerator, MonoBehaviour coroutineRunner) + { + var source = AutoResetUniTaskCompletionSource.Create(); + coroutineRunner.StartCoroutine(Core(enumerator, coroutineRunner, source)); + return source.Task; + } + + static IEnumerator Core(IEnumerator inner, MonoBehaviour coroutineRunner, AutoResetUniTaskCompletionSource source) + { + yield return coroutineRunner.StartCoroutine(inner); + source.TrySetResult(); + } + + sealed class EnumeratorPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + EnumeratorPromise nextNode; + public ref EnumeratorPromise NextNode => ref nextNode; + + static EnumeratorPromise() + { + TaskPool.RegisterSizeGetter(typeof(EnumeratorPromise), () => pool.Size); + } + + IEnumerator innerEnumerator; + CancellationToken cancellationToken; + int initialFrame; + bool loopRunning; + bool calledGetResult; + + UniTaskCompletionSourceCore core; + + EnumeratorPromise() + { + } + + public static IUniTaskSource Create(IEnumerator innerEnumerator, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new EnumeratorPromise(); + } + TaskTracker.TrackActiveTask(result, 3); + + result.innerEnumerator = ConsumeEnumerator(innerEnumerator); + result.cancellationToken = cancellationToken; + result.loopRunning = true; + result.calledGetResult = false; + result.initialFrame = -1; + + token = result.core.Version; + + // run immediately. + if (result.MoveNext()) + { + PlayerLoopHelper.AddAction(timing, result); + } + + return result; + } + + public void GetResult(short token) + { + try + { + calledGetResult = true; + core.GetResult(token); + } + finally + { + if (!loopRunning) + { + TryReturn(); + } + } + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (calledGetResult) + { + loopRunning = false; + TryReturn(); + return false; + } + + if (innerEnumerator == null) // invalid status, returned but loop running? + { + return false; + } + + if (cancellationToken.IsCancellationRequested) + { + loopRunning = false; + core.TrySetCanceled(cancellationToken); + return false; + } + + if (initialFrame == -1) + { + // Time can not touch in threadpool. + if (PlayerLoopHelper.IsMainThread) + { + initialFrame = Time.frameCount; + } + } + else if (initialFrame == Time.frameCount) + { + return true; // already executed in first frame, skip. + } + + try + { + if (innerEnumerator.MoveNext()) + { + return true; + } + } + catch (Exception ex) + { + loopRunning = false; + core.TrySetException(ex); + return false; + } + + loopRunning = false; + core.TrySetResult(null); + return false; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + innerEnumerator = default; + cancellationToken = default; + + return pool.TryPush(this); + } + + // Unwrap YieldInstructions + + static IEnumerator ConsumeEnumerator(IEnumerator enumerator) + { + while (enumerator.MoveNext()) + { + var current = enumerator.Current; + if (current == null) + { + yield return null; + } + else if (current is CustomYieldInstruction cyi) + { + // WWW, WaitForSecondsRealtime + while (cyi.keepWaiting) + { + yield return null; + } + } + else if (current is YieldInstruction) + { + IEnumerator innerCoroutine = null; + switch (current) + { + case AsyncOperation ao: + innerCoroutine = UnwrapWaitAsyncOperation(ao); + break; + case WaitForSeconds wfs: + innerCoroutine = UnwrapWaitForSeconds(wfs); + break; + } + if (innerCoroutine != null) + { + while (innerCoroutine.MoveNext()) + { + yield return null; + } + } + else + { + goto WARN; + } + } + else if (current is IEnumerator e3) + { + var e4 = ConsumeEnumerator(e3); + while (e4.MoveNext()) + { + yield return null; + } + } + else + { + goto WARN; + } + + continue; + + WARN: + // WaitForEndOfFrame, WaitForFixedUpdate, others. + UnityEngine.Debug.LogWarning($"yield {current.GetType().Name} is not supported on await IEnumerator or IEnumerator.ToUniTask(), please use ToUniTask(MonoBehaviour coroutineRunner) instead."); + yield return null; + } + } + + static readonly FieldInfo waitForSeconds_Seconds = typeof(WaitForSeconds).GetField("m_Seconds", BindingFlags.Instance | BindingFlags.GetField | BindingFlags.NonPublic); + + static IEnumerator UnwrapWaitForSeconds(WaitForSeconds waitForSeconds) + { + var second = (float)waitForSeconds_Seconds.GetValue(waitForSeconds); + var elapsed = 0.0f; + while (true) + { + yield return null; + + elapsed += Time.deltaTime; + if (elapsed >= second) + { + break; + } + }; + } + + static IEnumerator UnwrapWaitAsyncOperation(AsyncOperation asyncOperation) + { + while (!asyncOperation.isDone) + { + yield return null; + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/EnumeratorAsyncExtensions.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/EnumeratorAsyncExtensions.cs.meta new file mode 100644 index 0000000..a07b336 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/EnumeratorAsyncExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bc661232f11e4a741af54ba1c175d5ee +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/ExceptionExtensions.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/ExceptionExtensions.cs new file mode 100644 index 0000000..e411898 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/ExceptionExtensions.cs @@ -0,0 +1,14 @@ + +using System; + +namespace Cysharp.Threading.Tasks +{ + public static class ExceptionExtensions + { + public static bool IsOperationCanceledException(this Exception exception) + { + return exception is OperationCanceledException; + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/ExceptionExtensions.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/ExceptionExtensions.cs.meta new file mode 100644 index 0000000..9833001 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/ExceptionExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 930800098504c0d46958ce23a0495202 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External.meta new file mode 100644 index 0000000..81d3fca --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a3e874acee8398745b1dc3eddac09eaa +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables.meta new file mode 100644 index 0000000..cea9408 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a5b9231662e24c942b544bd85d4b39cb +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs new file mode 100644 index 0000000..f321bdb --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs @@ -0,0 +1,401 @@ +// asmdef Version Defines, enabled when com.unity.addressables is imported. + +#if UNITASK_ADDRESSABLE_SUPPORT + +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Runtime.CompilerServices; +using System.Runtime.ExceptionServices; +using System.Threading; +using UnityEngine.ResourceManagement.AsyncOperations; + +namespace Cysharp.Threading.Tasks +{ + public static class AddressablesAsyncExtensions + { +#region AsyncOperationHandle + + public static UniTask.Awaiter GetAwaiter(this AsyncOperationHandle handle) + { + return ToUniTask(handle).GetAwaiter(); + } + + public static UniTask WithCancellation(this AsyncOperationHandle handle, CancellationToken cancellationToken) + { + return ToUniTask(handle, cancellationToken: cancellationToken); + } + + public static UniTask ToUniTask(this AsyncOperationHandle handle, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); + + if (!handle.IsValid()) + { + // autoReleaseHandle:true handle is invalid(immediately internal handle == null) so return completed. + return UniTask.CompletedTask; + } + + if (handle.IsDone) + { + if (handle.Status == AsyncOperationStatus.Failed) + { + return UniTask.FromException(handle.OperationException); + } + return UniTask.CompletedTask; + } + + return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, timing, progress, cancellationToken, out var token), token); + } + + public struct AsyncOperationHandleAwaiter : ICriticalNotifyCompletion + { + AsyncOperationHandle handle; + Action continuationAction; + + public AsyncOperationHandleAwaiter(AsyncOperationHandle handle) + { + this.handle = handle; + this.continuationAction = null; + } + + public bool IsCompleted => handle.IsDone; + + public void GetResult() + { + if (continuationAction != null) + { + handle.Completed -= continuationAction; + continuationAction = null; + } + + if (handle.Status == AsyncOperationStatus.Failed) + { + var e = handle.OperationException; + handle = default; + ExceptionDispatchInfo.Capture(e).Throw(); + } + + var result = handle.Result; + handle = default; + } + + public void OnCompleted(Action continuation) + { + UnsafeOnCompleted(continuation); + } + + public void UnsafeOnCompleted(Action continuation) + { + Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction); + continuationAction = PooledDelegate.Create(continuation); + handle.Completed += continuationAction; + } + } + + sealed class AsyncOperationHandleConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + AsyncOperationHandleConfiguredSource nextNode; + public ref AsyncOperationHandleConfiguredSource NextNode => ref nextNode; + + static AsyncOperationHandleConfiguredSource() + { + TaskPool.RegisterSizeGetter(typeof(AsyncOperationHandleConfiguredSource), () => pool.Size); + } + + readonly Action continuationAction; + AsyncOperationHandle handle; + CancellationToken cancellationToken; + IProgress progress; + bool completed; + + UniTaskCompletionSourceCore core; + + AsyncOperationHandleConfiguredSource() + { + continuationAction = Continuation; + } + + public static IUniTaskSource Create(AsyncOperationHandle handle, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new AsyncOperationHandleConfiguredSource(); + } + + result.handle = handle; + result.progress = progress; + result.cancellationToken = cancellationToken; + result.completed = false; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + handle.Completed += result.continuationAction; + + token = result.core.Version; + return result; + } + + void Continuation(AsyncOperationHandle _) + { + handle.Completed -= continuationAction; + + if (completed) + { + TryReturn(); + } + else + { + completed = true; + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + } + else if (handle.Status == AsyncOperationStatus.Failed) + { + core.TrySetException(handle.OperationException); + } + else + { + core.TrySetResult(AsyncUnit.Default); + } + } + } + + public void GetResult(short token) + { + core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (completed) + { + TryReturn(); + return false; + } + + if (cancellationToken.IsCancellationRequested) + { + completed = true; + core.TrySetCanceled(cancellationToken); + return false; + } + + if (progress != null && handle.IsValid()) + { + progress.Report(handle.PercentComplete); + } + + return true; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + handle = default; + progress = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + +#endregion + +#region AsyncOperationHandle_T + + public static UniTask.Awaiter GetAwaiter(this AsyncOperationHandle handle) + { + return ToUniTask(handle).GetAwaiter(); + } + + public static UniTask WithCancellation(this AsyncOperationHandle handle, CancellationToken cancellationToken) + { + return ToUniTask(handle, cancellationToken: cancellationToken); + } + + public static UniTask ToUniTask(this AsyncOperationHandle handle, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); + + if (!handle.IsValid()) + { + throw new Exception("Attempting to use an invalid operation handle"); + } + + if (handle.IsDone) + { + if (handle.Status == AsyncOperationStatus.Failed) + { + return UniTask.FromException(handle.OperationException); + } + return UniTask.FromResult(handle.Result); + } + + return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, timing, progress, cancellationToken, out var token), token); + } + + sealed class AsyncOperationHandleConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode> + { + static TaskPool> pool; + AsyncOperationHandleConfiguredSource nextNode; + public ref AsyncOperationHandleConfiguredSource NextNode => ref nextNode; + + static AsyncOperationHandleConfiguredSource() + { + TaskPool.RegisterSizeGetter(typeof(AsyncOperationHandleConfiguredSource), () => pool.Size); + } + + readonly Action> continuationAction; + AsyncOperationHandle handle; + CancellationToken cancellationToken; + IProgress progress; + bool completed; + + UniTaskCompletionSourceCore core; + + AsyncOperationHandleConfiguredSource() + { + continuationAction = Continuation; + } + + public static IUniTaskSource Create(AsyncOperationHandle handle, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new AsyncOperationHandleConfiguredSource(); + } + + result.handle = handle; + result.cancellationToken = cancellationToken; + result.completed = false; + result.progress = progress; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + handle.Completed += result.continuationAction; + + token = result.core.Version; + return result; + } + + void Continuation(AsyncOperationHandle argHandle) + { + handle.Completed -= continuationAction; + + if (completed) + { + TryReturn(); + } + else + { + completed = true; + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + } + else if (argHandle.Status == AsyncOperationStatus.Failed) + { + core.TrySetException(argHandle.OperationException); + } + else + { + core.TrySetResult(argHandle.Result); + } + } + } + + public T GetResult(short token) + { + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (completed) + { + TryReturn(); + return false; + } + + if (cancellationToken.IsCancellationRequested) + { + completed = true; + core.TrySetCanceled(cancellationToken); + return false; + } + + if (progress != null && handle.IsValid()) + { + progress.Report(handle.PercentComplete); + } + + return true; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + handle = default; + progress = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + +#endregion + } +} + +#endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs.meta new file mode 100644 index 0000000..6927930 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3dc6441f9094f354b931dc3c79fb99e5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables/UniTask.Addressables.asmdef b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables/UniTask.Addressables.asmdef new file mode 100644 index 0000000..7b9e296 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables/UniTask.Addressables.asmdef @@ -0,0 +1,22 @@ +{ + "name": "UniTask.Addressables", + "references": [ + "UniTask", + "Unity.ResourceManager" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [ + { + "name": "com.unity.addressables", + "expression": "", + "define": "UNITASK_ADDRESSABLE_SUPPORT" + } + ], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables/UniTask.Addressables.asmdef.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables/UniTask.Addressables.asmdef.meta new file mode 100644 index 0000000..b0178c4 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/Addressables/UniTask.Addressables.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 593a5b492d29ac6448b1ebf7f035ef33 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween.meta new file mode 100644 index 0000000..c08509a --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 25cb2f742bfeb1d48a4e65d3140b955d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween/DOTweenAsyncExtensions.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween/DOTweenAsyncExtensions.cs new file mode 100644 index 0000000..80ae27c --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween/DOTweenAsyncExtensions.cs @@ -0,0 +1,470 @@ +// asmdef Version Defines, enabled when com.demigiant.dotween is imported. + +#if UNITASK_DOTWEEN_SUPPORT + +using Cysharp.Threading.Tasks.Internal; +using DG.Tweening; +using System; +using System.Collections.Concurrent; +using System.Runtime.CompilerServices; +using System.Threading; + +namespace Cysharp.Threading.Tasks +{ + public enum TweenCancelBehaviour + { + Kill, + KillWithCompleteCallback, + Complete, + CompleteWithSequenceCallback, + CancelAwait, + + // AndCancelAwait + KillAndCancelAwait, + KillWithCompleteCallbackAndCancelAwait, + CompleteAndCancelAwait, + CompleteWithSequenceCallbackAndCancelAwait + } + + public static class DOTweenAsyncExtensions + { + enum CallbackType + { + Kill, + Complete, + Pause, + Play, + Rewind, + StepComplete + } + + public static TweenAwaiter GetAwaiter(this Tween tween) + { + return new TweenAwaiter(tween); + } + + public static UniTask WithCancellation(this Tween tween, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(tween, nameof(tween)); + + if (!tween.IsActive()) return UniTask.CompletedTask; + return new UniTask(TweenConfiguredSource.Create(tween, TweenCancelBehaviour.Kill, cancellationToken, CallbackType.Kill, out var token), token); + } + + public static UniTask ToUniTask(this Tween tween, TweenCancelBehaviour tweenCancelBehaviour = TweenCancelBehaviour.Kill, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(tween, nameof(tween)); + + if (!tween.IsActive()) return UniTask.CompletedTask; + return new UniTask(TweenConfiguredSource.Create(tween, tweenCancelBehaviour, cancellationToken, CallbackType.Kill, out var token), token); + } + + public static UniTask AwaitForComplete(this Tween tween, TweenCancelBehaviour tweenCancelBehaviour = TweenCancelBehaviour.Kill, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(tween, nameof(tween)); + + if (!tween.IsActive()) return UniTask.CompletedTask; + return new UniTask(TweenConfiguredSource.Create(tween, tweenCancelBehaviour, cancellationToken, CallbackType.Complete, out var token), token); + } + + public static UniTask AwaitForPause(this Tween tween, TweenCancelBehaviour tweenCancelBehaviour = TweenCancelBehaviour.Kill, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(tween, nameof(tween)); + + if (!tween.IsActive()) return UniTask.CompletedTask; + return new UniTask(TweenConfiguredSource.Create(tween, tweenCancelBehaviour, cancellationToken, CallbackType.Pause, out var token), token); + } + + public static UniTask AwaitForPlay(this Tween tween, TweenCancelBehaviour tweenCancelBehaviour = TweenCancelBehaviour.Kill, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(tween, nameof(tween)); + + if (!tween.IsActive()) return UniTask.CompletedTask; + return new UniTask(TweenConfiguredSource.Create(tween, tweenCancelBehaviour, cancellationToken, CallbackType.Play, out var token), token); + } + + public static UniTask AwaitForRewind(this Tween tween, TweenCancelBehaviour tweenCancelBehaviour = TweenCancelBehaviour.Kill, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(tween, nameof(tween)); + + if (!tween.IsActive()) return UniTask.CompletedTask; + return new UniTask(TweenConfiguredSource.Create(tween, tweenCancelBehaviour, cancellationToken, CallbackType.Rewind, out var token), token); + } + + public static UniTask AwaitForStepComplete(this Tween tween, TweenCancelBehaviour tweenCancelBehaviour = TweenCancelBehaviour.Kill, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(tween, nameof(tween)); + + if (!tween.IsActive()) return UniTask.CompletedTask; + return new UniTask(TweenConfiguredSource.Create(tween, tweenCancelBehaviour, cancellationToken, CallbackType.StepComplete, out var token), token); + } + + public struct TweenAwaiter : ICriticalNotifyCompletion + { + readonly Tween tween; + + // killed(non active) as completed. + public bool IsCompleted => !tween.IsActive(); + + public TweenAwaiter(Tween tween) + { + this.tween = tween; + } + + public TweenAwaiter GetAwaiter() + { + return this; + } + + public void GetResult() + { + } + + public void OnCompleted(System.Action continuation) + { + UnsafeOnCompleted(continuation); + } + + public void UnsafeOnCompleted(System.Action continuation) + { + // onKill is called after OnCompleted, both Complete(false/true) and Kill(false/true). + tween.onKill = PooledTweenCallback.Create(continuation); + } + } + + sealed class TweenConfiguredSource : IUniTaskSource, ITaskPoolNode + { + static TaskPool pool; + TweenConfiguredSource nextNode; + public ref TweenConfiguredSource NextNode => ref nextNode; + + static TweenConfiguredSource() + { + TaskPool.RegisterSizeGetter(typeof(TweenConfiguredSource), () => pool.Size); + } + + readonly TweenCallback onCompleteCallbackDelegate; + readonly TweenCallback onUpdateDelegate; + + Tween tween; + TweenCancelBehaviour cancelBehaviour; + CancellationToken cancellationToken; + CallbackType callbackType; + bool canceled; + + TweenCallback originalUpdateAction; + TweenCallback originalCompleteAction; + UniTaskCompletionSourceCore core; + + TweenConfiguredSource() + { + onCompleteCallbackDelegate = OnCompleteCallbackDelegate; + onUpdateDelegate = OnUpdate; + } + + public static IUniTaskSource Create(Tween tween, TweenCancelBehaviour cancelBehaviour, CancellationToken cancellationToken, CallbackType callbackType, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + DoCancelBeforeCreate(tween, cancelBehaviour); + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new TweenConfiguredSource(); + } + + result.tween = tween; + result.cancelBehaviour = cancelBehaviour; + result.cancellationToken = cancellationToken; + result.callbackType = callbackType; + + result.originalUpdateAction = tween.onUpdate; + result.canceled = false; + + if (result.originalUpdateAction == result.onUpdateDelegate) + { + result.originalUpdateAction = null; + } + + tween.onUpdate = result.onUpdateDelegate; + + switch (callbackType) + { + case CallbackType.Kill: + result.originalCompleteAction = tween.onKill; + tween.onKill = result.onCompleteCallbackDelegate; + break; + case CallbackType.Complete: + result.originalCompleteAction = tween.onComplete; + tween.onComplete = result.onCompleteCallbackDelegate; + break; + case CallbackType.Pause: + result.originalCompleteAction = tween.onPause; + tween.onPause = result.onCompleteCallbackDelegate; + break; + case CallbackType.Play: + result.originalCompleteAction = tween.onPlay; + tween.onPlay = result.onCompleteCallbackDelegate; + break; + case CallbackType.Rewind: + result.originalCompleteAction = tween.onRewind; + tween.onRewind = result.onCompleteCallbackDelegate; + break; + case CallbackType.StepComplete: + result.originalCompleteAction = tween.onStepComplete; + tween.onStepComplete = result.onCompleteCallbackDelegate; + break; + default: + break; + } + + if (result.originalCompleteAction == result.onCompleteCallbackDelegate) + { + result.originalCompleteAction = null; + } + + TaskTracker.TrackActiveTask(result, 3); + + token = result.core.Version; + return result; + } + + void OnCompleteCallbackDelegate() + { + if (cancellationToken.IsCancellationRequested) + { + if (this.cancelBehaviour == TweenCancelBehaviour.KillAndCancelAwait + || this.cancelBehaviour == TweenCancelBehaviour.KillWithCompleteCallbackAndCancelAwait + || this.cancelBehaviour == TweenCancelBehaviour.CompleteAndCancelAwait + || this.cancelBehaviour == TweenCancelBehaviour.CompleteWithSequenceCallbackAndCancelAwait + || this.cancelBehaviour == TweenCancelBehaviour.CancelAwait) + { + canceled = true; + } + } + if (canceled) + { + core.TrySetCanceled(cancellationToken); + } + else + { + originalCompleteAction?.Invoke(); + core.TrySetResult(AsyncUnit.Default); + } + } + + void OnUpdate() + { + originalUpdateAction?.Invoke(); + + if (!cancellationToken.IsCancellationRequested) + { + return; + } + + switch (this.cancelBehaviour) + { + case TweenCancelBehaviour.Kill: + default: + this.tween.Kill(false); + break; + case TweenCancelBehaviour.KillAndCancelAwait: + this.canceled = true; + this.tween.Kill(false); + break; + case TweenCancelBehaviour.KillWithCompleteCallback: + this.tween.Kill(true); + break; + case TweenCancelBehaviour.KillWithCompleteCallbackAndCancelAwait: + this.canceled = true; + this.tween.Kill(true); + break; + case TweenCancelBehaviour.Complete: + this.tween.Complete(false); + break; + case TweenCancelBehaviour.CompleteAndCancelAwait: + this.canceled = true; + this.tween.Complete(false); + break; + case TweenCancelBehaviour.CompleteWithSequenceCallback: + this.tween.Complete(true); + break; + case TweenCancelBehaviour.CompleteWithSequenceCallbackAndCancelAwait: + this.canceled = true; + this.tween.Complete(true); + break; + case TweenCancelBehaviour.CancelAwait: + // restore to original callback + switch (callbackType) + { + case CallbackType.Kill: + tween.onKill = originalCompleteAction; + break; + case CallbackType.Complete: + tween.onComplete = originalCompleteAction; + break; + case CallbackType.Pause: + tween.onPause = originalCompleteAction; + break; + case CallbackType.Play: + tween.onPlay = originalCompleteAction; + break; + case CallbackType.Rewind: + tween.onRewind = originalCompleteAction; + break; + case CallbackType.StepComplete: + tween.onStepComplete = originalCompleteAction; + break; + default: + break; + } + + this.core.TrySetCanceled(this.cancellationToken); + break; + } + } + + static void DoCancelBeforeCreate(Tween tween, TweenCancelBehaviour tweenCancelBehaviour) + { + + switch (tweenCancelBehaviour) + { + case TweenCancelBehaviour.Kill: + default: + tween.Kill(false); + break; + case TweenCancelBehaviour.KillAndCancelAwait: + tween.Kill(false); + break; + case TweenCancelBehaviour.KillWithCompleteCallback: + tween.Kill(true); + break; + case TweenCancelBehaviour.KillWithCompleteCallbackAndCancelAwait: + tween.Kill(true); + break; + case TweenCancelBehaviour.Complete: + tween.Complete(false); + break; + case TweenCancelBehaviour.CompleteAndCancelAwait: + tween.Complete(false); + break; + case TweenCancelBehaviour.CompleteWithSequenceCallback: + tween.Complete(true); + break; + case TweenCancelBehaviour.CompleteWithSequenceCallbackAndCancelAwait: + tween.Complete(true); + break; + case TweenCancelBehaviour.CancelAwait: + break; + } + } + + public void GetResult(short token) + { + try + { + core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + tween.onUpdate = originalUpdateAction; + + switch (callbackType) + { + case CallbackType.Kill: + tween.onKill = originalCompleteAction; + break; + case CallbackType.Complete: + tween.onComplete = originalCompleteAction; + break; + case CallbackType.Pause: + tween.onPause = originalCompleteAction; + break; + case CallbackType.Play: + tween.onPlay = originalCompleteAction; + break; + case CallbackType.Rewind: + tween.onRewind = originalCompleteAction; + break; + case CallbackType.StepComplete: + tween.onStepComplete = originalCompleteAction; + break; + default: + break; + } + + tween = default; + cancellationToken = default; + originalUpdateAction = default; + originalCompleteAction = default; + return pool.TryPush(this); + } + } + } + + sealed class PooledTweenCallback + { + static readonly ConcurrentQueue pool = new ConcurrentQueue(); + + readonly TweenCallback runDelegate; + + Action continuation; + + + PooledTweenCallback() + { + runDelegate = Run; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static TweenCallback Create(Action continuation) + { + if (!pool.TryDequeue(out var item)) + { + item = new PooledTweenCallback(); + } + + item.continuation = continuation; + return item.runDelegate; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + void Run() + { + var call = continuation; + continuation = null; + if (call != null) + { + pool.Enqueue(this); + call.Invoke(); + } + } + } +} + +#endif diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween/DOTweenAsyncExtensions.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween/DOTweenAsyncExtensions.cs.meta new file mode 100644 index 0000000..63131b0 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween/DOTweenAsyncExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1f448d5bc5b232e4f98d89d5d1832e8e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween/UniTask.DOTween.asmdef b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween/UniTask.DOTween.asmdef new file mode 100644 index 0000000..7bdb2b6 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween/UniTask.DOTween.asmdef @@ -0,0 +1,22 @@ +{ + "name": "UniTask.DOTween", + "references": [ + "UniTask", + "DOTween.Modules" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [ + { + "name": "com.demigiant.dotween", + "expression": "", + "define": "UNITASK_DOTWEEN_SUPPORT" + } + ], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween/UniTask.DOTween.asmdef.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween/UniTask.DOTween.asmdef.meta new file mode 100644 index 0000000..427fe29 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/DOTween/UniTask.DOTween.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 029c1c1b674aaae47a6841a0b89ad80e +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro.meta new file mode 100644 index 0000000..ac860aa --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: f89da606bde9a4e4e94ae1189a029887 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.InputField.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.InputField.cs new file mode 100644 index 0000000..22f081c --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.InputField.cs @@ -0,0 +1,224 @@ +#if UNITASK_TEXTMESHPRO_SUPPORT + +using System; +using System.Threading; +using TMPro; + +namespace Cysharp.Threading.Tasks +{ + public static partial class TextMeshProAsyncExtensions + { + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this TMP_InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onValueChanged, cancellationToken, false); + } + + public static UniTask OnValueChangedAsync(this TMP_InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask OnValueChangedAsync(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onValueChanged, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this TMP_InputField inputField) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onValueChanged, cancellationToken); + } + + public static IAsyncEndEditEventHandler GetAsyncEndEditEventHandler(this TMP_InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onEndEdit, inputField.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncEndEditEventHandler GetAsyncEndEditEventHandler(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onEndEdit, cancellationToken, false); + } + + public static UniTask OnEndEditAsync(this TMP_InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onEndEdit, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask OnEndEditAsync(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onEndEdit, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnEndEditAsAsyncEnumerable(this TMP_InputField inputField) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onEndEdit, inputField.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable OnEndEditAsAsyncEnumerable(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onEndEdit, cancellationToken); + } + + public static IAsyncEndTextSelectionEventHandler<(string, int, int)> GetAsyncEndTextSelectionEventHandler(this TMP_InputField inputField) + { + return new AsyncUnityEventHandler<(string, int, int)>(new TextSelectionEventConverter(inputField.onEndTextSelection), inputField.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncEndTextSelectionEventHandler<(string, int, int)> GetAsyncEndTextSelectionEventHandler(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler<(string, int, int)>(new TextSelectionEventConverter(inputField.onEndTextSelection), cancellationToken, false); + } + + public static UniTask<(string, int, int)> OnEndTextSelectionAsync(this TMP_InputField inputField) + { + return new AsyncUnityEventHandler<(string, int, int)>(new TextSelectionEventConverter(inputField.onEndTextSelection), inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask<(string, int, int)> OnEndTextSelectionAsync(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler<(string, int, int)>(new TextSelectionEventConverter(inputField.onEndTextSelection), cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable<(string, int, int)> OnEndTextSelectionAsAsyncEnumerable(this TMP_InputField inputField) + { + return new UnityEventHandlerAsyncEnumerable<(string, int, int)>(new TextSelectionEventConverter(inputField.onEndTextSelection), inputField.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable<(string, int, int)> OnEndTextSelectionAsAsyncEnumerable(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable<(string, int, int)>(new TextSelectionEventConverter(inputField.onEndTextSelection), cancellationToken); + } + + public static IAsyncTextSelectionEventHandler<(string, int, int)> GetAsyncTextSelectionEventHandler(this TMP_InputField inputField) + { + return new AsyncUnityEventHandler<(string, int, int)>(new TextSelectionEventConverter(inputField.onTextSelection), inputField.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncTextSelectionEventHandler<(string, int, int)> GetAsyncTextSelectionEventHandler(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler<(string, int, int)>(new TextSelectionEventConverter(inputField.onTextSelection), cancellationToken, false); + } + + public static UniTask<(string, int, int)> OnTextSelectionAsync(this TMP_InputField inputField) + { + return new AsyncUnityEventHandler<(string, int, int)>(new TextSelectionEventConverter(inputField.onTextSelection), inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask<(string, int, int)> OnTextSelectionAsync(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler<(string, int, int)>(new TextSelectionEventConverter(inputField.onTextSelection), cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable<(string, int, int)> OnTextSelectionAsAsyncEnumerable(this TMP_InputField inputField) + { + return new UnityEventHandlerAsyncEnumerable<(string, int, int)>(new TextSelectionEventConverter(inputField.onTextSelection), inputField.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable<(string, int, int)> OnTextSelectionAsAsyncEnumerable(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable<(string, int, int)>(new TextSelectionEventConverter(inputField.onTextSelection), cancellationToken); + } + + public static IAsyncDeselectEventHandler GetAsyncDeselectEventHandler(this TMP_InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onDeselect, inputField.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncDeselectEventHandler GetAsyncDeselectEventHandler(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onDeselect, cancellationToken, false); + } + + public static UniTask OnDeselectAsync(this TMP_InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onDeselect, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask OnDeselectAsync(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onDeselect, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnDeselectAsAsyncEnumerable(this TMP_InputField inputField) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onDeselect, inputField.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable OnDeselectAsAsyncEnumerable(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onDeselect, cancellationToken); + } + + public static IAsyncSelectEventHandler GetAsyncSelectEventHandler(this TMP_InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onSelect, inputField.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncSelectEventHandler GetAsyncSelectEventHandler(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onSelect, cancellationToken, false); + } + + public static UniTask OnSelectAsync(this TMP_InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onSelect, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask OnSelectAsync(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onSelect, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnSelectAsAsyncEnumerable(this TMP_InputField inputField) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onSelect, inputField.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable OnSelectAsAsyncEnumerable(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onSelect, cancellationToken); + } + + public static IAsyncSubmitEventHandler GetAsyncSubmitEventHandler(this TMP_InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onSubmit, inputField.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncSubmitEventHandler GetAsyncSubmitEventHandler(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onSubmit, cancellationToken, false); + } + + public static UniTask OnSubmitAsync(this TMP_InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onSubmit, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask OnSubmitAsync(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onSubmit, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnSubmitAsAsyncEnumerable(this TMP_InputField inputField) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onSubmit, inputField.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable OnSubmitAsAsyncEnumerable(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onSubmit, cancellationToken); + } + + } +} + +#endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.InputField.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.InputField.cs.meta new file mode 100644 index 0000000..2e39d2e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.InputField.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 79f4f2475e0b2c44e97ed1dee760627b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.InputField.tt b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.InputField.tt new file mode 100644 index 0000000..f9acf4d --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.InputField.tt @@ -0,0 +1,66 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".cs" #> +<# + var handlers = new (string name, string type)[] { + ("ValueChanged", "string"), + ("EndEdit", "string"), + ("EndTextSelection", "(string, int, int)"), + ("TextSelection", "(string, int, int)"), + ("Deselect", "string"), + ("Select", "string"), + ("Submit", "string"), + }; + + Func shouldConvert = x => x.EndsWith("TextSelection"); + Func eventName = x => shouldConvert(x) ? $"new TextSelectionEventConverter(inputField.on{x})" : $"inputField.on{x}"; +#> +#if UNITASK_TEXTMESHPRO_SUPPORT + +using System; +using System.Threading; +using TMPro; + +namespace Cysharp.Threading.Tasks +{ + public static partial class TextMeshProAsyncExtensions + { +<# foreach(var (name, type) in handlers) { #> + public static IAsync<#= (name) #>EventHandler<<#= type #>> GetAsync<#= (name) #>EventHandler(this TMP_InputField inputField) + { + return new AsyncUnityEventHandler<<#= type #>>(<#= eventName(name) #>, inputField.GetCancellationTokenOnDestroy(), false); + } + + public static IAsync<#= (name) #>EventHandler<<#= type #>> GetAsync<#= (name) #>EventHandler(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler<<#= type #>>(<#= eventName(name) #>, cancellationToken, false); + } + + public static UniTask<<#= type #>> On<#= (name) #>Async(this TMP_InputField inputField) + { + return new AsyncUnityEventHandler<<#= type #>>(<#= eventName(name) #>, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask<<#= type #>> On<#= (name) #>Async(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler<<#= type #>>(<#= eventName(name) #>, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable<<#= type #>> On<#= (name) #>AsAsyncEnumerable(this TMP_InputField inputField) + { + return new UnityEventHandlerAsyncEnumerable<<#= type #>>(<#= eventName(name) #>, inputField.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable<<#= type #>> On<#= (name) #>AsAsyncEnumerable(this TMP_InputField inputField, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable<<#= type #>>(<#= eventName(name) #>, cancellationToken); + } + +<# } #> + } +} + +#endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.InputField.tt.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.InputField.tt.meta new file mode 100644 index 0000000..d27b2e0 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.InputField.tt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: e9bb9fc551a975d44a7180e022a2debe +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.cs new file mode 100644 index 0000000..362aa83 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.cs @@ -0,0 +1,130 @@ +#if UNITASK_TEXTMESHPRO_SUPPORT + +using System; +using System.Threading; +using TMPro; +using UnityEngine.Events; + +namespace Cysharp.Threading.Tasks +{ + public static partial class TextMeshProAsyncExtensions + { + // -> Text + public static void BindTo(this IUniTaskAsyncEnumerable source, TMP_Text text, bool rebindOnError = true) + { + BindToCore(source, text, text.GetCancellationTokenOnDestroy(), rebindOnError).Forget(); + } + + public static void BindTo(this IUniTaskAsyncEnumerable source, TMP_Text text, CancellationToken cancellationToken, bool rebindOnError = true) + { + BindToCore(source, text, cancellationToken, rebindOnError).Forget(); + } + + static async UniTaskVoid BindToCore(IUniTaskAsyncEnumerable source, TMP_Text text, CancellationToken cancellationToken, bool rebindOnError) + { + var repeat = false; + BIND_AGAIN: + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (true) + { + bool moveNext; + try + { + moveNext = await e.MoveNextAsync(); + repeat = false; + } + catch (Exception ex) + { + if (ex is OperationCanceledException) return; + + if (rebindOnError && !repeat) + { + repeat = true; + goto BIND_AGAIN; + } + else + { + throw; + } + } + + if (!moveNext) return; + + text.text = e.Current; + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + // -> Text + + public static void BindTo(this IUniTaskAsyncEnumerable source, TMP_Text text, bool rebindOnError = true) + { + BindToCore(source, text, text.GetCancellationTokenOnDestroy(), rebindOnError).Forget(); + } + + public static void BindTo(this IUniTaskAsyncEnumerable source, TMP_Text text, CancellationToken cancellationToken, bool rebindOnError = true) + { + BindToCore(source, text, cancellationToken, rebindOnError).Forget(); + } + + public static void BindTo(this AsyncReactiveProperty source, TMP_Text text, bool rebindOnError = true) + { + BindToCore(source, text, text.GetCancellationTokenOnDestroy(), rebindOnError).Forget(); + } + + static async UniTaskVoid BindToCore(IUniTaskAsyncEnumerable source, TMP_Text text, CancellationToken cancellationToken, bool rebindOnError) + { + var repeat = false; + BIND_AGAIN: + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (true) + { + bool moveNext; + try + { + moveNext = await e.MoveNextAsync(); + repeat = false; + } + catch (Exception ex) + { + if (ex is OperationCanceledException) return; + + if (rebindOnError && !repeat) + { + repeat = true; + goto BIND_AGAIN; + } + else + { + throw; + } + } + + if (!moveNext) return; + + text.text = e.Current.ToString(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.cs.meta new file mode 100644 index 0000000..752d125 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/TextMeshProAsyncExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b6ba480edafb67d4e91bb10feb64fae5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/UniTask.TextMeshPro.asmdef b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/UniTask.TextMeshPro.asmdef new file mode 100644 index 0000000..6bb01d7 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/UniTask.TextMeshPro.asmdef @@ -0,0 +1,22 @@ +{ + "name": "UniTask.TextMeshPro", + "references": [ + "UniTask", + "Unity.TextMeshPro" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [ + { + "name": "com.unity.textmeshpro", + "expression": "", + "define": "UNITASK_TEXTMESHPRO_SUPPORT" + } + ], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/UniTask.TextMeshPro.asmdef.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/UniTask.TextMeshPro.asmdef.meta new file mode 100644 index 0000000..4b59831 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/External/TextMeshPro/UniTask.TextMeshPro.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: dc47925d1a5fa2946bdd37746b2b5d48 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/IUniTaskAsyncEnumerable.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/IUniTaskAsyncEnumerable.cs new file mode 100644 index 0000000..847d430 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/IUniTaskAsyncEnumerable.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Threading; + +namespace Cysharp.Threading.Tasks +{ + public interface IUniTaskAsyncEnumerable + { + IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default); + } + + public interface IUniTaskAsyncEnumerator : IUniTaskAsyncDisposable + { + T Current { get; } + UniTask MoveNextAsync(); + } + + public interface IUniTaskAsyncDisposable + { + UniTask DisposeAsync(); + } + + public interface IUniTaskOrderedAsyncEnumerable : IUniTaskAsyncEnumerable + { + IUniTaskOrderedAsyncEnumerable CreateOrderedEnumerable(Func keySelector, IComparer comparer, bool descending); + IUniTaskOrderedAsyncEnumerable CreateOrderedEnumerable(Func> keySelector, IComparer comparer, bool descending); + IUniTaskOrderedAsyncEnumerable CreateOrderedEnumerable(Func> keySelector, IComparer comparer, bool descending); + } + + public interface IConnectableUniTaskAsyncEnumerable : IUniTaskAsyncEnumerable + { + IDisposable Connect(); + } + + // don't use AsyncGrouping. + //public interface IUniTaskAsyncGrouping : IUniTaskAsyncEnumerable + //{ + // TKey Key { get; } + //} + + public static class UniTaskAsyncEnumerableExtensions + { + public static UniTaskCancelableAsyncEnumerable WithCancellation(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + return new UniTaskCancelableAsyncEnumerable(source, cancellationToken); + } + } + + [StructLayout(LayoutKind.Auto)] + public readonly struct UniTaskCancelableAsyncEnumerable + { + private readonly IUniTaskAsyncEnumerable enumerable; + private readonly CancellationToken cancellationToken; + + internal UniTaskCancelableAsyncEnumerable(IUniTaskAsyncEnumerable enumerable, CancellationToken cancellationToken) + { + this.enumerable = enumerable; + this.cancellationToken = cancellationToken; + } + + public Enumerator GetAsyncEnumerator() + { + return new Enumerator(enumerable.GetAsyncEnumerator(cancellationToken)); + } + + [StructLayout(LayoutKind.Auto)] + public readonly struct Enumerator + { + private readonly IUniTaskAsyncEnumerator enumerator; + + internal Enumerator(IUniTaskAsyncEnumerator enumerator) + { + this.enumerator = enumerator; + } + + public T Current => enumerator.Current; + + public UniTask MoveNextAsync() + { + return enumerator.MoveNextAsync(); + } + + + public UniTask DisposeAsync() + { + return enumerator.DisposeAsync(); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/IUniTaskAsyncEnumerable.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/IUniTaskAsyncEnumerable.cs.meta new file mode 100644 index 0000000..12f0fe5 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/IUniTaskAsyncEnumerable.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b20cf9f02ac585948a4372fa4ee06504 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/IUniTaskSource.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/IUniTaskSource.cs new file mode 100644 index 0000000..8814781 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/IUniTaskSource.cs @@ -0,0 +1,124 @@ +#pragma warning disable CS1591 + +using System; +using System.Runtime.CompilerServices; + +namespace Cysharp.Threading.Tasks +{ + public enum UniTaskStatus + { + /// The operation has not yet completed. + Pending = 0, + /// The operation completed successfully. + Succeeded = 1, + /// The operation completed with an error. + Faulted = 2, + /// The operation completed due to cancellation. + Canceled = 3 + } + + // similar as IValueTaskSource + public interface IUniTaskSource +#if !UNITY_2018_3_OR_NEWER && !NETSTANDARD2_0 + : System.Threading.Tasks.Sources.IValueTaskSource +#pragma warning disable CS0108 +#endif + { + UniTaskStatus GetStatus(short token); + void OnCompleted(Action continuation, object state, short token); + void GetResult(short token); + + UniTaskStatus UnsafeGetStatus(); // only for debug use. + +#if !UNITY_2018_3_OR_NEWER && !NETSTANDARD2_0 +#pragma warning restore CS0108 + + System.Threading.Tasks.Sources.ValueTaskSourceStatus System.Threading.Tasks.Sources.IValueTaskSource.GetStatus(short token) + { + return (System.Threading.Tasks.Sources.ValueTaskSourceStatus)(int)((IUniTaskSource)this).GetStatus(token); + } + + void System.Threading.Tasks.Sources.IValueTaskSource.GetResult(short token) + { + ((IUniTaskSource)this).GetResult(token); + } + + void System.Threading.Tasks.Sources.IValueTaskSource.OnCompleted(Action continuation, object state, short token, System.Threading.Tasks.Sources.ValueTaskSourceOnCompletedFlags flags) + { + // ignore flags, always none. + ((IUniTaskSource)this).OnCompleted(continuation, state, token); + } + +#endif + } + + public interface IUniTaskSource : IUniTaskSource +#if !UNITY_2018_3_OR_NEWER && !NETSTANDARD2_0 + , System.Threading.Tasks.Sources.IValueTaskSource +#endif + { + new T GetResult(short token); + +#if !UNITY_2018_3_OR_NEWER && !NETSTANDARD2_0 + + new public UniTaskStatus GetStatus(short token) + { + return ((IUniTaskSource)this).GetStatus(token); + } + + new public void OnCompleted(Action continuation, object state, short token) + { + ((IUniTaskSource)this).OnCompleted(continuation, state, token); + } + + System.Threading.Tasks.Sources.ValueTaskSourceStatus System.Threading.Tasks.Sources.IValueTaskSource.GetStatus(short token) + { + return (System.Threading.Tasks.Sources.ValueTaskSourceStatus)(int)((IUniTaskSource)this).GetStatus(token); + } + + T System.Threading.Tasks.Sources.IValueTaskSource.GetResult(short token) + { + return ((IUniTaskSource)this).GetResult(token); + } + + void System.Threading.Tasks.Sources.IValueTaskSource.OnCompleted(Action continuation, object state, short token, System.Threading.Tasks.Sources.ValueTaskSourceOnCompletedFlags flags) + { + // ignore flags, always none. + ((IUniTaskSource)this).OnCompleted(continuation, state, token); + } + +#endif + } + + public static class UniTaskStatusExtensions + { + /// status != Pending. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsCompleted(this UniTaskStatus status) + { + return status != UniTaskStatus.Pending; + } + + /// status == Succeeded. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsCompletedSuccessfully(this UniTaskStatus status) + { + return status == UniTaskStatus.Succeeded; + } + + /// status == Canceled. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsCanceled(this UniTaskStatus status) + { + return status == UniTaskStatus.Canceled; + } + + /// status == Faulted. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsFaulted(this UniTaskStatus status) + { + return status == UniTaskStatus.Faulted; + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/IUniTaskSource.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/IUniTaskSource.cs.meta new file mode 100644 index 0000000..b225d1c --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/IUniTaskSource.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3e4d023d8404ab742b5e808c98097c3c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal.meta new file mode 100644 index 0000000..7ac2b17 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 633f49a8aafb6fa43894cd4646c71743 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayPool.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayPool.cs new file mode 100644 index 0000000..e1d9d3b --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayPool.cs @@ -0,0 +1,150 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Internal +{ + // Same interface as System.Buffers.ArrayPool but only provides Shared. + + internal sealed class ArrayPool + { + // Same size as System.Buffers.DefaultArrayPool + const int DefaultMaxNumberOfArraysPerBucket = 50; + + static readonly T[] EmptyArray = new T[0]; + + public static readonly ArrayPool Shared = new ArrayPool(); + + readonly MinimumQueue[] buckets; + readonly SpinLock[] locks; + + ArrayPool() + { + // see: GetQueueIndex + buckets = new MinimumQueue[18]; + locks = new SpinLock[18]; + for (int i = 0; i < buckets.Length; i++) + { + buckets[i] = new MinimumQueue(4); + locks[i] = new SpinLock(false); + } + } + + public T[] Rent(int minimumLength) + { + if (minimumLength < 0) + { + throw new ArgumentOutOfRangeException("minimumLength"); + } + else if (minimumLength == 0) + { + return EmptyArray; + } + + var size = CalculateSize(minimumLength); + var index = GetQueueIndex(size); + if (index != -1) + { + var q = buckets[index]; + var lockTaken = false; + try + { + locks[index].Enter(ref lockTaken); + + if (q.Count != 0) + { + return q.Dequeue(); + } + } + finally + { + if (lockTaken) locks[index].Exit(false); + } + } + + return new T[size]; + } + + public void Return(T[] array, bool clearArray = false) + { + if (array == null || array.Length == 0) + { + return; + } + + var index = GetQueueIndex(array.Length); + if (index != -1) + { + if (clearArray) + { + Array.Clear(array, 0, array.Length); + } + + var q = buckets[index]; + var lockTaken = false; + + try + { + locks[index].Enter(ref lockTaken); + + if (q.Count > DefaultMaxNumberOfArraysPerBucket) + { + return; + } + + q.Enqueue(array); + } + finally + { + if (lockTaken) locks[index].Exit(false); + } + } + } + + static int CalculateSize(int size) + { + size--; + size |= size >> 1; + size |= size >> 2; + size |= size >> 4; + size |= size >> 8; + size |= size >> 16; + size += 1; + + if (size < 8) + { + size = 8; + } + + return size; + } + + static int GetQueueIndex(int size) + { + switch (size) + { + case 8: return 0; + case 16: return 1; + case 32: return 2; + case 64: return 3; + case 128: return 4; + case 256: return 5; + case 512: return 6; + case 1024: return 7; + case 2048: return 8; + case 4096: return 9; + case 8192: return 10; + case 16384: return 11; + case 32768: return 12; + case 65536: return 13; + case 131072: return 14; + case 262144: return 15; + case 524288: return 16; + case 1048576: return 17; // max array length + default: + return -1; + } + } + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayPool.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayPool.cs.meta new file mode 100644 index 0000000..693816c --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayPool.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: f83ebad81fb89fb4882331616ca6d248 +timeCreated: 1532361008 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayPoolUtil.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayPoolUtil.cs new file mode 100644 index 0000000..016901d --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayPoolUtil.cs @@ -0,0 +1,115 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; + +namespace Cysharp.Threading.Tasks.Internal +{ + internal static class ArrayPoolUtil + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void EnsureCapacity(ref T[] array, int index, ArrayPool pool) + { + if (array.Length <= index) + { + EnsureCapacityCore(ref array, index, pool); + } + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static void EnsureCapacityCore(ref T[] array, int index, ArrayPool pool) + { + if (array.Length <= index) + { + var newSize = array.Length * 2; + var newArray = pool.Rent((index < newSize) ? newSize : (index * 2)); + Array.Copy(array, 0, newArray, 0, array.Length); + + pool.Return(array, clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType()); + + array = newArray; + } + } + + public static RentArray Materialize(IEnumerable source) + { + if (source is T[] array) + { + return new RentArray(array, array.Length, null); + } + + var defaultCount = 32; + if (source is ICollection coll) + { + if (coll.Count == 0) + { + return new RentArray(Array.Empty(), 0, null); + } + + defaultCount = coll.Count; + var pool = ArrayPool.Shared; + var buffer = pool.Rent(defaultCount); + coll.CopyTo(buffer, 0); + return new RentArray(buffer, coll.Count, pool); + } + else if (source is IReadOnlyCollection rcoll) + { + defaultCount = rcoll.Count; + } + + if (defaultCount == 0) + { + return new RentArray(Array.Empty(), 0, null); + } + + { + var pool = ArrayPool.Shared; + + var index = 0; + var buffer = pool.Rent(defaultCount); + foreach (var item in source) + { + EnsureCapacity(ref buffer, index, pool); + buffer[index++] = item; + } + + return new RentArray(buffer, index, pool); + } + } + + public struct RentArray : IDisposable + { + public readonly T[] Array; + public readonly int Length; + ArrayPool pool; + + public RentArray(T[] array, int length, ArrayPool pool) + { + this.Array = array; + this.Length = length; + this.pool = pool; + } + + public void Dispose() + { + DisposeManually(!RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType()); + } + + public void DisposeManually(bool clearArray) + { + if (pool != null) + { + if (clearArray) + { + System.Array.Clear(Array, 0, Length); + } + + pool.Return(Array, clearArray: false); + pool = null; + } + } + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayPoolUtil.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayPoolUtil.cs.meta new file mode 100644 index 0000000..e06ec65 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayPoolUtil.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 424cc208fb61d4e448b08fcfa0eee25e +timeCreated: 1532361007 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayUtil.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayUtil.cs new file mode 100644 index 0000000..fc7a808 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayUtil.cs @@ -0,0 +1,73 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; + +namespace Cysharp.Threading.Tasks.Internal +{ + internal static class ArrayUtil + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void EnsureCapacity(ref T[] array, int index) + { + if (array.Length <= index) + { + EnsureCore(ref array, index); + } + } + + // rare case, no inlining. + [MethodImpl(MethodImplOptions.NoInlining)] + static void EnsureCore(ref T[] array, int index) + { + var newSize = array.Length * 2; + var newArray = new T[(index < newSize) ? newSize : (index * 2)]; + Array.Copy(array, 0, newArray, 0, array.Length); + + array = newArray; + } + + /// + /// Optimizing utility to avoid .ToArray() that creates buffer copy(cut to just size). + /// + public static (T[] array, int length) Materialize(IEnumerable source) + { + if (source is T[] array) + { + return (array, array.Length); + } + + var defaultCount = 4; + if (source is ICollection coll) + { + defaultCount = coll.Count; + var buffer = new T[defaultCount]; + coll.CopyTo(buffer, 0); + return (buffer, defaultCount); + } + else if (source is IReadOnlyCollection rcoll) + { + defaultCount = rcoll.Count; + } + + if (defaultCount == 0) + { + return (Array.Empty(), 0); + } + + { + var index = 0; + var buffer = new T[defaultCount]; + foreach (var item in source) + { + EnsureCapacity(ref buffer, index); + buffer[index++] = item; + } + + return (buffer, index); + } + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayUtil.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayUtil.cs.meta new file mode 100644 index 0000000..645fc4e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ArrayUtil.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 23146a82ec99f2542a87971c8d3d7988 +timeCreated: 1532361007 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ContinuationQueue.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ContinuationQueue.cs new file mode 100644 index 0000000..a311126 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ContinuationQueue.cs @@ -0,0 +1,225 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Internal +{ + internal sealed class ContinuationQueue + { + const int MaxArrayLength = 0X7FEFFFFF; + const int InitialSize = 16; + + readonly PlayerLoopTiming timing; + + SpinLock gate = new SpinLock(false); + bool dequing = false; + + int actionListCount = 0; + Action[] actionList = new Action[InitialSize]; + + int waitingListCount = 0; + Action[] waitingList = new Action[InitialSize]; + + public ContinuationQueue(PlayerLoopTiming timing) + { + this.timing = timing; + } + + public void Enqueue(Action continuation) + { + bool lockTaken = false; + try + { + gate.Enter(ref lockTaken); + + if (dequing) + { + // Ensure Capacity + if (waitingList.Length == waitingListCount) + { + var newLength = waitingListCount * 2; + if ((uint)newLength > MaxArrayLength) newLength = MaxArrayLength; + + var newArray = new Action[newLength]; + Array.Copy(waitingList, newArray, waitingListCount); + waitingList = newArray; + } + waitingList[waitingListCount] = continuation; + waitingListCount++; + } + else + { + // Ensure Capacity + if (actionList.Length == actionListCount) + { + var newLength = actionListCount * 2; + if ((uint)newLength > MaxArrayLength) newLength = MaxArrayLength; + + var newArray = new Action[newLength]; + Array.Copy(actionList, newArray, actionListCount); + actionList = newArray; + } + actionList[actionListCount] = continuation; + actionListCount++; + } + } + finally + { + if (lockTaken) gate.Exit(false); + } + } + + public int Clear() + { + var rest = actionListCount + waitingListCount; + + actionListCount = 0; + actionList = new Action[InitialSize]; + + waitingListCount = 0; + waitingList = new Action[InitialSize]; + + return rest; + } + + // delegate entrypoint. + public void Run() + { + // for debugging, create named stacktrace. +#if DEBUG + switch (timing) + { + case PlayerLoopTiming.Initialization: + Initialization(); + break; + case PlayerLoopTiming.LastInitialization: + LastInitialization(); + break; + case PlayerLoopTiming.EarlyUpdate: + EarlyUpdate(); + break; + case PlayerLoopTiming.LastEarlyUpdate: + LastEarlyUpdate(); + break; + case PlayerLoopTiming.FixedUpdate: + FixedUpdate(); + break; + case PlayerLoopTiming.LastFixedUpdate: + LastFixedUpdate(); + break; + case PlayerLoopTiming.PreUpdate: + PreUpdate(); + break; + case PlayerLoopTiming.LastPreUpdate: + LastPreUpdate(); + break; + case PlayerLoopTiming.Update: + Update(); + break; + case PlayerLoopTiming.LastUpdate: + LastUpdate(); + break; + case PlayerLoopTiming.PreLateUpdate: + PreLateUpdate(); + break; + case PlayerLoopTiming.LastPreLateUpdate: + LastPreLateUpdate(); + break; + case PlayerLoopTiming.PostLateUpdate: + PostLateUpdate(); + break; + case PlayerLoopTiming.LastPostLateUpdate: + LastPostLateUpdate(); + break; +#if UNITY_2020_2_OR_NEWER + case PlayerLoopTiming.TimeUpdate: + TimeUpdate(); + break; + case PlayerLoopTiming.LastTimeUpdate: + LastTimeUpdate(); + break; +#endif + default: + break; + } +#else + RunCore(); +#endif + } + + void Initialization() => RunCore(); + void LastInitialization() => RunCore(); + void EarlyUpdate() => RunCore(); + void LastEarlyUpdate() => RunCore(); + void FixedUpdate() => RunCore(); + void LastFixedUpdate() => RunCore(); + void PreUpdate() => RunCore(); + void LastPreUpdate() => RunCore(); + void Update() => RunCore(); + void LastUpdate() => RunCore(); + void PreLateUpdate() => RunCore(); + void LastPreLateUpdate() => RunCore(); + void PostLateUpdate() => RunCore(); + void LastPostLateUpdate() => RunCore(); +#if UNITY_2020_2_OR_NEWER + void TimeUpdate() => RunCore(); + void LastTimeUpdate() => RunCore(); +#endif + + [System.Diagnostics.DebuggerHidden] + void RunCore() + { + { + bool lockTaken = false; + try + { + gate.Enter(ref lockTaken); + if (actionListCount == 0) return; + dequing = true; + } + finally + { + if (lockTaken) gate.Exit(false); + } + } + + for (int i = 0; i < actionListCount; i++) + { + + var action = actionList[i]; + actionList[i] = null; + try + { + action(); + } + catch (Exception ex) + { + UnityEngine.Debug.LogException(ex); + } + } + + { + bool lockTaken = false; + try + { + gate.Enter(ref lockTaken); + dequing = false; + + var swapTempActionList = actionList; + + actionListCount = waitingListCount; + actionList = waitingList; + + waitingListCount = 0; + waitingList = swapTempActionList; + } + finally + { + if (lockTaken) gate.Exit(false); + } + } + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ContinuationQueue.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ContinuationQueue.cs.meta new file mode 100644 index 0000000..b04e541 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ContinuationQueue.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f66c32454e50f2546b17deadc80a4c77 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/DiagnosticsExtensions.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/DiagnosticsExtensions.cs new file mode 100644 index 0000000..77d998f --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/DiagnosticsExtensions.cs @@ -0,0 +1,249 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Security; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using UnityEngine; + +namespace Cysharp.Threading.Tasks.Internal +{ + internal static class DiagnosticsExtensions + { + static bool displayFilenames = true; + + static readonly Regex typeBeautifyRegex = new Regex("`.+$", RegexOptions.Compiled); + + static readonly Dictionary builtInTypeNames = new Dictionary + { + { typeof(void), "void" }, + { typeof(bool), "bool" }, + { typeof(byte), "byte" }, + { typeof(char), "char" }, + { typeof(decimal), "decimal" }, + { typeof(double), "double" }, + { typeof(float), "float" }, + { typeof(int), "int" }, + { typeof(long), "long" }, + { typeof(object), "object" }, + { typeof(sbyte), "sbyte" }, + { typeof(short), "short" }, + { typeof(string), "string" }, + { typeof(uint), "uint" }, + { typeof(ulong), "ulong" }, + { typeof(ushort), "ushort" }, + { typeof(Task), "Task" }, + { typeof(UniTask), "UniTask" }, + { typeof(UniTaskVoid), "UniTaskVoid" } + }; + + public static string CleanupAsyncStackTrace(this StackTrace stackTrace) + { + if (stackTrace == null) return ""; + + var sb = new StringBuilder(); + for (int i = 0; i < stackTrace.FrameCount; i++) + { + var sf = stackTrace.GetFrame(i); + + var mb = sf.GetMethod(); + + if (IgnoreLine(mb)) continue; + if (IsAsync(mb)) + { + sb.Append("async "); + TryResolveStateMachineMethod(ref mb, out var decType); + } + + // return type + if (mb is MethodInfo mi) + { + sb.Append(BeautifyType(mi.ReturnType, false)); + sb.Append(" "); + } + + // method name + sb.Append(BeautifyType(mb.DeclaringType, false)); + if (!mb.IsConstructor) + { + sb.Append("."); + } + sb.Append(mb.Name); + if (mb.IsGenericMethod) + { + sb.Append("<"); + foreach (var item in mb.GetGenericArguments()) + { + sb.Append(BeautifyType(item, true)); + } + sb.Append(">"); + } + + // parameter + sb.Append("("); + sb.Append(string.Join(", ", mb.GetParameters().Select(p => BeautifyType(p.ParameterType, true) + " " + p.Name))); + sb.Append(")"); + + // file name + if (displayFilenames && (sf.GetILOffset() != -1)) + { + String fileName = null; + + try + { + fileName = sf.GetFileName(); + } + catch (NotSupportedException) + { + displayFilenames = false; + } + catch (SecurityException) + { + displayFilenames = false; + } + + if (fileName != null) + { + sb.Append(' '); + sb.AppendFormat(CultureInfo.InvariantCulture, "(at {0})", AppendHyperLink(fileName, sf.GetFileLineNumber().ToString())); + } + } + + sb.AppendLine(); + } + return sb.ToString(); + } + + + static bool IsAsync(MethodBase methodInfo) + { + var declareType = methodInfo.DeclaringType; + return typeof(IAsyncStateMachine).IsAssignableFrom(declareType); + } + + // code from Ben.Demystifier/EnhancedStackTrace.Frame.cs + static bool TryResolveStateMachineMethod(ref MethodBase method, out Type declaringType) + { + declaringType = method.DeclaringType; + + var parentType = declaringType.DeclaringType; + if (parentType == null) + { + return false; + } + + var methods = parentType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly); + if (methods == null) + { + return false; + } + + foreach (var candidateMethod in methods) + { + var attributes = candidateMethod.GetCustomAttributes(false); + if (attributes == null) + { + continue; + } + + foreach (var asma in attributes) + { + if (asma.StateMachineType == declaringType) + { + method = candidateMethod; + declaringType = candidateMethod.DeclaringType; + // Mark the iterator as changed; so it gets the + annotation of the original method + // async statemachines resolve directly to their builder methods so aren't marked as changed + return asma is IteratorStateMachineAttribute; + } + } + } + + return false; + } + + static string BeautifyType(Type t, bool shortName) + { + if (builtInTypeNames.TryGetValue(t, out var builtin)) + { + return builtin; + } + if (t.IsGenericParameter) return t.Name; + if (t.IsArray) return BeautifyType(t.GetElementType(), shortName) + "[]"; + if (t.FullName?.StartsWith("System.ValueTuple") ?? false) + { + return "(" + string.Join(", ", t.GetGenericArguments().Select(x => BeautifyType(x, true))) + ")"; + } + if (!t.IsGenericType) return shortName ? t.Name : t.FullName.Replace("Cysharp.Threading.Tasks.Triggers.", "").Replace("Cysharp.Threading.Tasks.Internal.", "").Replace("Cysharp.Threading.Tasks.", "") ?? t.Name; + + var innerFormat = string.Join(", ", t.GetGenericArguments().Select(x => BeautifyType(x, true))); + + var genericType = t.GetGenericTypeDefinition().FullName; + if (genericType == "System.Threading.Tasks.Task`1") + { + genericType = "Task"; + } + + return typeBeautifyRegex.Replace(genericType, "").Replace("Cysharp.Threading.Tasks.Triggers.", "").Replace("Cysharp.Threading.Tasks.Internal.", "").Replace("Cysharp.Threading.Tasks.", "") + "<" + innerFormat + ">"; + } + + static bool IgnoreLine(MethodBase methodInfo) + { + var declareType = methodInfo.DeclaringType.FullName; + if (declareType == "System.Threading.ExecutionContext") + { + return true; + } + else if (declareType.StartsWith("System.Runtime.CompilerServices")) + { + return true; + } + else if (declareType.StartsWith("Cysharp.Threading.Tasks.CompilerServices")) + { + return true; + } + else if (declareType == "System.Threading.Tasks.AwaitTaskContinuation") + { + return true; + } + else if (declareType.StartsWith("System.Threading.Tasks.Task")) + { + return true; + } + else if (declareType.StartsWith("Cysharp.Threading.Tasks.UniTaskCompletionSourceCore")) + { + return true; + } + else if (declareType.StartsWith("Cysharp.Threading.Tasks.AwaiterActions")) + { + return true; + } + + return false; + } + + static string AppendHyperLink(string path, string line) + { + var fi = new FileInfo(path); + if (fi.Directory == null) + { + return fi.Name; + } + else + { + var fname = fi.FullName.Replace(Path.DirectorySeparatorChar, '/').Replace(PlayerLoopHelper.ApplicationDataPath, ""); + var withAssetsPath = "Assets/" + fname; + return "" + withAssetsPath + ":" + line + ""; + } + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/DiagnosticsExtensions.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/DiagnosticsExtensions.cs.meta new file mode 100644 index 0000000..6c1f06c --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/DiagnosticsExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f80fb1c9ed4c99447be1b0a47a8d980b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/Error.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/Error.cs new file mode 100644 index 0000000..5c7bc93 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/Error.cs @@ -0,0 +1,79 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Runtime.CompilerServices; + +namespace Cysharp.Threading.Tasks.Internal +{ + internal static class Error + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowArgumentNullException(T value, string paramName) + where T : class + { + if (value == null) ThrowArgumentNullExceptionCore(paramName); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static void ThrowArgumentNullExceptionCore(string paramName) + { + throw new ArgumentNullException(paramName); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Exception ArgumentOutOfRange(string paramName) + { + return new ArgumentOutOfRangeException(paramName); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Exception NoElements() + { + return new InvalidOperationException("Source sequence doesn't contain any elements."); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Exception MoreThanOneElement() + { + return new InvalidOperationException("Source sequence contains more than one element."); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentException(string message) + { + throw new ArgumentException(message); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowNotYetCompleted() + { + throw new InvalidOperationException("Not yet completed."); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + public static T ThrowNotYetCompleted() + { + throw new InvalidOperationException("Not yet completed."); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowWhenContinuationIsAlreadyRegistered(T continuationField) + where T : class + { + if (continuationField != null) ThrowInvalidOperationExceptionCore("continuation is already registered."); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static void ThrowInvalidOperationExceptionCore(string message) + { + throw new InvalidOperationException(message); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowOperationCanceledException() + { + throw new OperationCanceledException(); + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/Error.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/Error.cs.meta new file mode 100644 index 0000000..2e5d219 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/Error.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 5f39f495294d4604b8082202faf98554 +timeCreated: 1532361007 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/MinimumQueue.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/MinimumQueue.cs new file mode 100644 index 0000000..a6b567a --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/MinimumQueue.cs @@ -0,0 +1,112 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Runtime.CompilerServices; + +namespace Cysharp.Threading.Tasks.Internal +{ + // optimized version of Standard Queue. + internal class MinimumQueue + { + const int MinimumGrow = 4; + const int GrowFactor = 200; + + T[] array; + int head; + int tail; + int size; + + public MinimumQueue(int capacity) + { + if (capacity < 0) throw new ArgumentOutOfRangeException("capacity"); + array = new T[capacity]; + head = tail = size = 0; + } + + public int Count + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get { return size; } + } + + public T Peek() + { + if (size == 0) ThrowForEmptyQueue(); + return array[head]; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Enqueue(T item) + { + if (size == array.Length) + { + Grow(); + } + + array[tail] = item; + MoveNext(ref tail); + size++; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public T Dequeue() + { + if (size == 0) ThrowForEmptyQueue(); + + int head = this.head; + T[] array = this.array; + T removed = array[head]; + array[head] = default(T); + MoveNext(ref this.head); + size--; + return removed; + } + + void Grow() + { + int newcapacity = (int)((long)array.Length * (long)GrowFactor / 100); + if (newcapacity < array.Length + MinimumGrow) + { + newcapacity = array.Length + MinimumGrow; + } + SetCapacity(newcapacity); + } + + void SetCapacity(int capacity) + { + T[] newarray = new T[capacity]; + if (size > 0) + { + if (head < tail) + { + Array.Copy(array, head, newarray, 0, size); + } + else + { + Array.Copy(array, head, newarray, 0, array.Length - head); + Array.Copy(array, 0, newarray, array.Length - head, tail); + } + } + + array = newarray; + head = 0; + tail = (size == capacity) ? 0 : size; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + void MoveNext(ref int index) + { + int tmp = index + 1; + if (tmp == array.Length) + { + tmp = 0; + } + index = tmp; + } + + void ThrowForEmptyQueue() + { + throw new InvalidOperationException("EmptyQueue"); + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/MinimumQueue.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/MinimumQueue.cs.meta new file mode 100644 index 0000000..dc06736 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/MinimumQueue.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7d63add489ccc99498114d79702b904d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/PlayerLoopRunner.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/PlayerLoopRunner.cs new file mode 100644 index 0000000..43625ab --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/PlayerLoopRunner.cs @@ -0,0 +1,260 @@ + +using System; +using UnityEngine; + +namespace Cysharp.Threading.Tasks.Internal +{ + internal sealed class PlayerLoopRunner + { + const int InitialSize = 16; + + readonly PlayerLoopTiming timing; + readonly object runningAndQueueLock = new object(); + readonly object arrayLock = new object(); + readonly Action unhandledExceptionCallback; + + int tail = 0; + bool running = false; + IPlayerLoopItem[] loopItems = new IPlayerLoopItem[InitialSize]; + MinimumQueue waitQueue = new MinimumQueue(InitialSize); + + + + public PlayerLoopRunner(PlayerLoopTiming timing) + { + this.unhandledExceptionCallback = ex => Debug.LogException(ex); + this.timing = timing; + } + + public void AddAction(IPlayerLoopItem item) + { + lock (runningAndQueueLock) + { + if (running) + { + waitQueue.Enqueue(item); + return; + } + } + + lock (arrayLock) + { + // Ensure Capacity + if (loopItems.Length == tail) + { + Array.Resize(ref loopItems, checked(tail * 2)); + } + loopItems[tail++] = item; + } + } + + public int Clear() + { + lock (arrayLock) + { + var rest = 0; + + for (var index = 0; index < loopItems.Length; index++) + { + if (loopItems[index] != null) + { + rest++; + } + + loopItems[index] = null; + } + + tail = 0; + return rest; + } + } + + // delegate entrypoint. + public void Run() + { + // for debugging, create named stacktrace. +#if DEBUG + switch (timing) + { + case PlayerLoopTiming.Initialization: + Initialization(); + break; + case PlayerLoopTiming.LastInitialization: + LastInitialization(); + break; + case PlayerLoopTiming.EarlyUpdate: + EarlyUpdate(); + break; + case PlayerLoopTiming.LastEarlyUpdate: + LastEarlyUpdate(); + break; + case PlayerLoopTiming.FixedUpdate: + FixedUpdate(); + break; + case PlayerLoopTiming.LastFixedUpdate: + LastFixedUpdate(); + break; + case PlayerLoopTiming.PreUpdate: + PreUpdate(); + break; + case PlayerLoopTiming.LastPreUpdate: + LastPreUpdate(); + break; + case PlayerLoopTiming.Update: + Update(); + break; + case PlayerLoopTiming.LastUpdate: + LastUpdate(); + break; + case PlayerLoopTiming.PreLateUpdate: + PreLateUpdate(); + break; + case PlayerLoopTiming.LastPreLateUpdate: + LastPreLateUpdate(); + break; + case PlayerLoopTiming.PostLateUpdate: + PostLateUpdate(); + break; + case PlayerLoopTiming.LastPostLateUpdate: + LastPostLateUpdate(); + break; +#if UNITY_2020_2_OR_NEWER + case PlayerLoopTiming.TimeUpdate: + TimeUpdate(); + break; + case PlayerLoopTiming.LastTimeUpdate: + LastTimeUpdate(); + break; +#endif + default: + break; + } +#else + RunCore(); +#endif + } + + void Initialization() => RunCore(); + void LastInitialization() => RunCore(); + void EarlyUpdate() => RunCore(); + void LastEarlyUpdate() => RunCore(); + void FixedUpdate() => RunCore(); + void LastFixedUpdate() => RunCore(); + void PreUpdate() => RunCore(); + void LastPreUpdate() => RunCore(); + void Update() => RunCore(); + void LastUpdate() => RunCore(); + void PreLateUpdate() => RunCore(); + void LastPreLateUpdate() => RunCore(); + void PostLateUpdate() => RunCore(); + void LastPostLateUpdate() => RunCore(); +#if UNITY_2020_2_OR_NEWER + void TimeUpdate() => RunCore(); + void LastTimeUpdate() => RunCore(); +#endif + + [System.Diagnostics.DebuggerHidden] + void RunCore() + { + lock (runningAndQueueLock) + { + running = true; + } + + lock (arrayLock) + { + var j = tail - 1; + + for (int i = 0; i < loopItems.Length; i++) + { + var action = loopItems[i]; + if (action != null) + { + try + { + if (!action.MoveNext()) + { + loopItems[i] = null; + } + else + { + continue; // next i + } + } + catch (Exception ex) + { + loopItems[i] = null; + try + { + unhandledExceptionCallback(ex); + } + catch { } + } + } + + // find null, loop from tail + while (i < j) + { + var fromTail = loopItems[j]; + if (fromTail != null) + { + try + { + if (!fromTail.MoveNext()) + { + loopItems[j] = null; + j--; + continue; // next j + } + else + { + // swap + loopItems[i] = fromTail; + loopItems[j] = null; + j--; + goto NEXT_LOOP; // next i + } + } + catch (Exception ex) + { + loopItems[j] = null; + j--; + try + { + unhandledExceptionCallback(ex); + } + catch { } + continue; // next j + } + } + else + { + j--; + } + } + + tail = i; // loop end + break; // LOOP END + + NEXT_LOOP: + continue; + } + + + lock (runningAndQueueLock) + { + running = false; + while (waitQueue.Count != 0) + { + if (loopItems.Length == tail) + { + Array.Resize(ref loopItems, checked(tail * 2)); + } + loopItems[tail++] = waitQueue.Dequeue(); + } + } + } + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/PlayerLoopRunner.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/PlayerLoopRunner.cs.meta new file mode 100644 index 0000000..603dbc9 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/PlayerLoopRunner.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 340c6d420bb4f484aa8683415ea92571 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/PooledDelegate.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/PooledDelegate.cs new file mode 100644 index 0000000..518244f --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/PooledDelegate.cs @@ -0,0 +1,50 @@ +using System; +using System.Runtime.CompilerServices; + +namespace Cysharp.Threading.Tasks.Internal +{ + internal sealed class PooledDelegate : ITaskPoolNode> + { + static TaskPool> pool; + + PooledDelegate nextNode; + public ref PooledDelegate NextNode => ref nextNode; + + static PooledDelegate() + { + TaskPool.RegisterSizeGetter(typeof(PooledDelegate), () => pool.Size); + } + + readonly Action runDelegate; + Action continuation; + + PooledDelegate() + { + runDelegate = Run; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Action Create(Action continuation) + { + if (!pool.TryPop(out var item)) + { + item = new PooledDelegate(); + } + + item.continuation = continuation; + return item.runDelegate; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + void Run(T _) + { + var call = continuation; + continuation = null; + if (call != null) + { + pool.TryPush(this); + call.Invoke(); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/PooledDelegate.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/PooledDelegate.cs.meta new file mode 100644 index 0000000..7f92aff --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/PooledDelegate.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8932579438742fa40b010edd412dbfba +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/RuntimeHelpersAbstraction.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/RuntimeHelpersAbstraction.cs new file mode 100644 index 0000000..cbabdab --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/RuntimeHelpersAbstraction.cs @@ -0,0 +1,64 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +#if UNITY_2018_3_OR_NEWER +using UnityEngine; +#endif + +namespace Cysharp.Threading.Tasks.Internal +{ + internal static class RuntimeHelpersAbstraction + { + // If we can use RuntimeHelpers.IsReferenceOrContainsReferences(.NET Core 2.0), use it. + public static bool IsWellKnownNoReferenceContainsType() + { + return WellKnownNoReferenceContainsType.IsWellKnownType; + } + + static bool WellKnownNoReferenceContainsTypeInitialize(Type t) + { + // The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single. + if (t.IsPrimitive) return true; + + if (t.IsEnum) return true; + if (t == typeof(DateTime)) return true; + if (t == typeof(DateTimeOffset)) return true; + if (t == typeof(Guid)) return true; + if (t == typeof(decimal)) return true; + + // unwrap nullable + if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + return WellKnownNoReferenceContainsTypeInitialize(t.GetGenericArguments()[0]); + } + +#if UNITY_2018_3_OR_NEWER + + // or add other wellknown types(Vector, etc...) here + if (t == typeof(Vector2)) return true; + if (t == typeof(Vector3)) return true; + if (t == typeof(Vector4)) return true; + if (t == typeof(Color)) return true; + if (t == typeof(Rect)) return true; + if (t == typeof(Bounds)) return true; + if (t == typeof(Quaternion)) return true; + if (t == typeof(Vector2Int)) return true; + if (t == typeof(Vector3Int)) return true; + +#endif + + return false; + } + + static class WellKnownNoReferenceContainsType + { + public static readonly bool IsWellKnownType; + + static WellKnownNoReferenceContainsType() + { + IsWellKnownType = WellKnownNoReferenceContainsTypeInitialize(typeof(T)); + } + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/RuntimeHelpersAbstraction.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/RuntimeHelpersAbstraction.cs.meta new file mode 100644 index 0000000..4254391 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/RuntimeHelpersAbstraction.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 94975e4d4e0c0ea4ba787d3872ce9bb4 +timeCreated: 1532361007 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/StatePool.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/StatePool.cs new file mode 100644 index 0000000..e1d40bd --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/StatePool.cs @@ -0,0 +1,153 @@ +using System; +using System.Collections.Concurrent; +using System.Runtime.CompilerServices; + +namespace Cysharp.Threading.Tasks.Internal +{ + internal static class StateTuple + { + public static StateTuple Create(T1 item1) + { + return StatePool.Create(item1); + } + + public static StateTuple Create(T1 item1, T2 item2) + { + return StatePool.Create(item1, item2); + } + + public static StateTuple Create(T1 item1, T2 item2, T3 item3) + { + return StatePool.Create(item1, item2, item3); + } + } + + internal class StateTuple : IDisposable + { + public T1 Item1; + + public void Deconstruct(out T1 item1) + { + item1 = this.Item1; + } + + public void Dispose() + { + StatePool.Return(this); + } + } + + internal static class StatePool + { + static readonly ConcurrentQueue> queue = new ConcurrentQueue>(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static StateTuple Create(T1 item1) + { + if (queue.TryDequeue(out var value)) + { + value.Item1 = item1; + return value; + } + + return new StateTuple { Item1 = item1 }; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Return(StateTuple tuple) + { + tuple.Item1 = default; + queue.Enqueue(tuple); + } + } + + internal class StateTuple : IDisposable + { + public T1 Item1; + public T2 Item2; + + public void Deconstruct(out T1 item1, out T2 item2) + { + item1 = this.Item1; + item2 = this.Item2; + } + + public void Dispose() + { + StatePool.Return(this); + } + } + + internal static class StatePool + { + static readonly ConcurrentQueue> queue = new ConcurrentQueue>(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static StateTuple Create(T1 item1, T2 item2) + { + if (queue.TryDequeue(out var value)) + { + value.Item1 = item1; + value.Item2 = item2; + return value; + } + + return new StateTuple { Item1 = item1, Item2 = item2 }; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Return(StateTuple tuple) + { + tuple.Item1 = default; + tuple.Item2 = default; + queue.Enqueue(tuple); + } + } + + internal class StateTuple : IDisposable + { + public T1 Item1; + public T2 Item2; + public T3 Item3; + + public void Deconstruct(out T1 item1, out T2 item2, out T3 item3) + { + item1 = this.Item1; + item2 = this.Item2; + item3 = this.Item3; + } + + public void Dispose() + { + StatePool.Return(this); + } + } + + internal static class StatePool + { + static readonly ConcurrentQueue> queue = new ConcurrentQueue>(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static StateTuple Create(T1 item1, T2 item2, T3 item3) + { + if (queue.TryDequeue(out var value)) + { + value.Item1 = item1; + value.Item2 = item2; + value.Item3 = item3; + return value; + } + + return new StateTuple { Item1 = item1, Item2 = item2, Item3 = item3 }; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Return(StateTuple tuple) + { + tuple.Item1 = default; + tuple.Item2 = default; + tuple.Item3 = default; + queue.Enqueue(tuple); + } + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/StatePool.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/StatePool.cs.meta new file mode 100644 index 0000000..6779aa1 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/StatePool.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 60cdf0bcaea36b444a7ae7263ae7598f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/TaskTracker.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/TaskTracker.cs new file mode 100644 index 0000000..c163e22 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/TaskTracker.cs @@ -0,0 +1,178 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks +{ + // public for add user custom. + + public static class TaskTracker + { +#if UNITY_EDITOR + + static int trackingId = 0; + + public const string EnableAutoReloadKey = "UniTaskTrackerWindow_EnableAutoReloadKey"; + public const string EnableTrackingKey = "UniTaskTrackerWindow_EnableTrackingKey"; + public const string EnableStackTraceKey = "UniTaskTrackerWindow_EnableStackTraceKey"; + + public static class EditorEnableState + { + static bool enableAutoReload; + public static bool EnableAutoReload + { + get { return enableAutoReload; } + set + { + enableAutoReload = value; + UnityEditor.EditorPrefs.SetBool(EnableAutoReloadKey, value); + } + } + + static bool enableTracking; + public static bool EnableTracking + { + get { return enableTracking; } + set + { + enableTracking = value; + UnityEditor.EditorPrefs.SetBool(EnableTrackingKey, value); + } + } + + static bool enableStackTrace; + public static bool EnableStackTrace + { + get { return enableStackTrace; } + set + { + enableStackTrace = value; + UnityEditor.EditorPrefs.SetBool(EnableStackTraceKey, value); + } + } + } + +#endif + + + static List> listPool = new List>(); + + static readonly WeakDictionary tracking = new WeakDictionary(); + + [Conditional("UNITY_EDITOR")] + public static void TrackActiveTask(IUniTaskSource task, int skipFrame) + { +#if UNITY_EDITOR + dirty = true; + if (!EditorEnableState.EnableTracking) return; + var stackTrace = EditorEnableState.EnableStackTrace ? new StackTrace(skipFrame, true).CleanupAsyncStackTrace() : ""; + + string typeName; + if (EditorEnableState.EnableStackTrace) + { + var sb = new StringBuilder(); + TypeBeautify(task.GetType(), sb); + typeName = sb.ToString(); + } + else + { + typeName = task.GetType().Name; + } + tracking.TryAdd(task, (typeName, Interlocked.Increment(ref trackingId), DateTime.UtcNow, stackTrace)); +#endif + } + + [Conditional("UNITY_EDITOR")] + public static void RemoveTracking(IUniTaskSource task) + { +#if UNITY_EDITOR + dirty = true; + if (!EditorEnableState.EnableTracking) return; + var success = tracking.TryRemove(task); +#endif + } + + static bool dirty; + + public static bool CheckAndResetDirty() + { + var current = dirty; + dirty = false; + return current; + } + + /// (trackingId, awaiterType, awaiterStatus, createdTime, stackTrace) + public static void ForEachActiveTask(Action action) + { + lock (listPool) + { + var count = tracking.ToList(ref listPool, clear: false); + try + { + for (int i = 0; i < count; i++) + { + action(listPool[i].Value.trackingId, listPool[i].Value.formattedType, listPool[i].Key.UnsafeGetStatus(), listPool[i].Value.addTime, listPool[i].Value.stackTrace); + listPool[i] = default; + } + } + catch + { + listPool.Clear(); + throw; + } + } + } + + static void TypeBeautify(Type type, StringBuilder sb) + { + if (type.IsNested) + { + // TypeBeautify(type.DeclaringType, sb); + sb.Append(type.DeclaringType.Name.ToString()); + sb.Append("."); + } + + if (type.IsGenericType) + { + var genericsStart = type.Name.IndexOf("`"); + if (genericsStart != -1) + { + sb.Append(type.Name.Substring(0, genericsStart)); + } + else + { + sb.Append(type.Name); + } + sb.Append("<"); + var first = true; + foreach (var item in type.GetGenericArguments()) + { + if (!first) + { + sb.Append(", "); + } + first = false; + TypeBeautify(item, sb); + } + sb.Append(">"); + } + else + { + sb.Append(type.Name); + } + } + + //static string RemoveUniTaskNamespace(string str) + //{ + // return str.Replace("Cysharp.Threading.Tasks.CompilerServices", "") + // .Replace("Cysharp.Threading.Tasks.Linq", "") + // .Replace("Cysharp.Threading.Tasks", ""); + //} + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/TaskTracker.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/TaskTracker.cs.meta new file mode 100644 index 0000000..5563bf7 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/TaskTracker.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a203c73eb4ccdbb44bddfd82d38fdda9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/UnityEqualityComparer.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/UnityEqualityComparer.cs new file mode 100644 index 0000000..906f3b6 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/UnityEqualityComparer.cs @@ -0,0 +1,267 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + +namespace Cysharp.Threading.Tasks.Internal +{ + internal static class UnityEqualityComparer + { + public static readonly IEqualityComparer Vector2 = new Vector2EqualityComparer(); + public static readonly IEqualityComparer Vector3 = new Vector3EqualityComparer(); + public static readonly IEqualityComparer Vector4 = new Vector4EqualityComparer(); + public static readonly IEqualityComparer Color = new ColorEqualityComparer(); + public static readonly IEqualityComparer Color32 = new Color32EqualityComparer(); + public static readonly IEqualityComparer Rect = new RectEqualityComparer(); + public static readonly IEqualityComparer Bounds = new BoundsEqualityComparer(); + public static readonly IEqualityComparer Quaternion = new QuaternionEqualityComparer(); + + static readonly RuntimeTypeHandle vector2Type = typeof(Vector2).TypeHandle; + static readonly RuntimeTypeHandle vector3Type = typeof(Vector3).TypeHandle; + static readonly RuntimeTypeHandle vector4Type = typeof(Vector4).TypeHandle; + static readonly RuntimeTypeHandle colorType = typeof(Color).TypeHandle; + static readonly RuntimeTypeHandle color32Type = typeof(Color32).TypeHandle; + static readonly RuntimeTypeHandle rectType = typeof(Rect).TypeHandle; + static readonly RuntimeTypeHandle boundsType = typeof(Bounds).TypeHandle; + static readonly RuntimeTypeHandle quaternionType = typeof(Quaternion).TypeHandle; + +#if UNITY_2017_2_OR_NEWER + + public static readonly IEqualityComparer Vector2Int = new Vector2IntEqualityComparer(); + public static readonly IEqualityComparer Vector3Int = new Vector3IntEqualityComparer(); + public static readonly IEqualityComparer RangeInt = new RangeIntEqualityComparer(); + public static readonly IEqualityComparer RectInt = new RectIntEqualityComparer(); + public static readonly IEqualityComparer BoundsInt = new BoundsIntEqualityComparer(); + + static readonly RuntimeTypeHandle vector2IntType = typeof(Vector2Int).TypeHandle; + static readonly RuntimeTypeHandle vector3IntType = typeof(Vector3Int).TypeHandle; + static readonly RuntimeTypeHandle rangeIntType = typeof(RangeInt).TypeHandle; + static readonly RuntimeTypeHandle rectIntType = typeof(RectInt).TypeHandle; + static readonly RuntimeTypeHandle boundsIntType = typeof(BoundsInt).TypeHandle; + +#endif + + static class Cache + { + public static readonly IEqualityComparer Comparer; + + static Cache() + { + var comparer = GetDefaultHelper(typeof(T)); + if (comparer == null) + { + Comparer = EqualityComparer.Default; + } + else + { + Comparer = (IEqualityComparer)comparer; + } + } + } + + public static IEqualityComparer GetDefault() + { + return Cache.Comparer; + } + + static object GetDefaultHelper(Type type) + { + var t = type.TypeHandle; + + if (t.Equals(vector2Type)) return (object)UnityEqualityComparer.Vector2; + if (t.Equals(vector3Type)) return (object)UnityEqualityComparer.Vector3; + if (t.Equals(vector4Type)) return (object)UnityEqualityComparer.Vector4; + if (t.Equals(colorType)) return (object)UnityEqualityComparer.Color; + if (t.Equals(color32Type)) return (object)UnityEqualityComparer.Color32; + if (t.Equals(rectType)) return (object)UnityEqualityComparer.Rect; + if (t.Equals(boundsType)) return (object)UnityEqualityComparer.Bounds; + if (t.Equals(quaternionType)) return (object)UnityEqualityComparer.Quaternion; + +#if UNITY_2017_2_OR_NEWER + + if (t.Equals(vector2IntType)) return (object)UnityEqualityComparer.Vector2Int; + if (t.Equals(vector3IntType)) return (object)UnityEqualityComparer.Vector3Int; + if (t.Equals(rangeIntType)) return (object)UnityEqualityComparer.RangeInt; + if (t.Equals(rectIntType)) return (object)UnityEqualityComparer.RectInt; + if (t.Equals(boundsIntType)) return (object)UnityEqualityComparer.BoundsInt; +#endif + + return null; + } + + sealed class Vector2EqualityComparer : IEqualityComparer + { + public bool Equals(Vector2 self, Vector2 vector) + { + return self.x.Equals(vector.x) && self.y.Equals(vector.y); + } + + public int GetHashCode(Vector2 obj) + { + return obj.x.GetHashCode() ^ obj.y.GetHashCode() << 2; + } + } + + sealed class Vector3EqualityComparer : IEqualityComparer + { + public bool Equals(Vector3 self, Vector3 vector) + { + return self.x.Equals(vector.x) && self.y.Equals(vector.y) && self.z.Equals(vector.z); + } + + public int GetHashCode(Vector3 obj) + { + return obj.x.GetHashCode() ^ obj.y.GetHashCode() << 2 ^ obj.z.GetHashCode() >> 2; + } + } + + sealed class Vector4EqualityComparer : IEqualityComparer + { + public bool Equals(Vector4 self, Vector4 vector) + { + return self.x.Equals(vector.x) && self.y.Equals(vector.y) && self.z.Equals(vector.z) && self.w.Equals(vector.w); + } + + public int GetHashCode(Vector4 obj) + { + return obj.x.GetHashCode() ^ obj.y.GetHashCode() << 2 ^ obj.z.GetHashCode() >> 2 ^ obj.w.GetHashCode() >> 1; + } + } + + sealed class ColorEqualityComparer : IEqualityComparer + { + public bool Equals(Color self, Color other) + { + return self.r.Equals(other.r) && self.g.Equals(other.g) && self.b.Equals(other.b) && self.a.Equals(other.a); + } + + public int GetHashCode(Color obj) + { + return obj.r.GetHashCode() ^ obj.g.GetHashCode() << 2 ^ obj.b.GetHashCode() >> 2 ^ obj.a.GetHashCode() >> 1; + } + } + + sealed class RectEqualityComparer : IEqualityComparer + { + public bool Equals(Rect self, Rect other) + { + return self.x.Equals(other.x) && self.width.Equals(other.width) && self.y.Equals(other.y) && self.height.Equals(other.height); + } + + public int GetHashCode(Rect obj) + { + return obj.x.GetHashCode() ^ obj.width.GetHashCode() << 2 ^ obj.y.GetHashCode() >> 2 ^ obj.height.GetHashCode() >> 1; + } + } + + sealed class BoundsEqualityComparer : IEqualityComparer + { + public bool Equals(Bounds self, Bounds vector) + { + return self.center.Equals(vector.center) && self.extents.Equals(vector.extents); + } + + public int GetHashCode(Bounds obj) + { + return obj.center.GetHashCode() ^ obj.extents.GetHashCode() << 2; + } + } + + sealed class QuaternionEqualityComparer : IEqualityComparer + { + public bool Equals(Quaternion self, Quaternion vector) + { + return self.x.Equals(vector.x) && self.y.Equals(vector.y) && self.z.Equals(vector.z) && self.w.Equals(vector.w); + } + + public int GetHashCode(Quaternion obj) + { + return obj.x.GetHashCode() ^ obj.y.GetHashCode() << 2 ^ obj.z.GetHashCode() >> 2 ^ obj.w.GetHashCode() >> 1; + } + } + + sealed class Color32EqualityComparer : IEqualityComparer + { + public bool Equals(Color32 self, Color32 vector) + { + return self.a.Equals(vector.a) && self.r.Equals(vector.r) && self.g.Equals(vector.g) && self.b.Equals(vector.b); + } + + public int GetHashCode(Color32 obj) + { + return obj.a.GetHashCode() ^ obj.r.GetHashCode() << 2 ^ obj.g.GetHashCode() >> 2 ^ obj.b.GetHashCode() >> 1; + } + } + +#if UNITY_2017_2_OR_NEWER + + sealed class Vector2IntEqualityComparer : IEqualityComparer + { + public bool Equals(Vector2Int self, Vector2Int vector) + { + return self.x.Equals(vector.x) && self.y.Equals(vector.y); + } + + public int GetHashCode(Vector2Int obj) + { + return obj.x.GetHashCode() ^ obj.y.GetHashCode() << 2; + } + } + + sealed class Vector3IntEqualityComparer : IEqualityComparer + { + public static readonly Vector3IntEqualityComparer Default = new Vector3IntEqualityComparer(); + + public bool Equals(Vector3Int self, Vector3Int vector) + { + return self.x.Equals(vector.x) && self.y.Equals(vector.y) && self.z.Equals(vector.z); + } + + public int GetHashCode(Vector3Int obj) + { + return obj.x.GetHashCode() ^ obj.y.GetHashCode() << 2 ^ obj.z.GetHashCode() >> 2; + } + } + + sealed class RangeIntEqualityComparer : IEqualityComparer + { + public bool Equals(RangeInt self, RangeInt vector) + { + return self.start.Equals(vector.start) && self.length.Equals(vector.length); + } + + public int GetHashCode(RangeInt obj) + { + return obj.start.GetHashCode() ^ obj.length.GetHashCode() << 2; + } + } + + sealed class RectIntEqualityComparer : IEqualityComparer + { + public bool Equals(RectInt self, RectInt other) + { + return self.x.Equals(other.x) && self.width.Equals(other.width) && self.y.Equals(other.y) && self.height.Equals(other.height); + } + + public int GetHashCode(RectInt obj) + { + return obj.x.GetHashCode() ^ obj.width.GetHashCode() << 2 ^ obj.y.GetHashCode() >> 2 ^ obj.height.GetHashCode() >> 1; + } + } + + sealed class BoundsIntEqualityComparer : IEqualityComparer + { + public bool Equals(BoundsInt self, BoundsInt vector) + { + return Vector3IntEqualityComparer.Default.Equals(self.position, vector.position) + && Vector3IntEqualityComparer.Default.Equals(self.size, vector.size); + } + + public int GetHashCode(BoundsInt obj) + { + return Vector3IntEqualityComparer.Default.GetHashCode(obj.position) ^ Vector3IntEqualityComparer.Default.GetHashCode(obj.size) << 2; + } + } + +#endif + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/UnityEqualityComparer.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/UnityEqualityComparer.cs.meta new file mode 100644 index 0000000..79eb04f --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/UnityEqualityComparer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ebaaf14253c9cfb47b23283218ff9b67 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/UnityWebRequestExtensions.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/UnityWebRequestExtensions.cs new file mode 100644 index 0000000..0da9f5a --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/UnityWebRequestExtensions.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine.Networking; + +namespace Cysharp.Threading.Tasks.Internal +{ +#if ENABLE_UNITYWEBREQUEST && (!UNITY_2019_1_OR_NEWER || UNITASK_WEBREQUEST_SUPPORT) + + internal static class UnityWebRequestResultExtensions + { + public static bool IsError(this UnityWebRequest unityWebRequest) + { +#if UNITY_2020_2_OR_NEWER + var result = unityWebRequest.result; + return (result == UnityWebRequest.Result.ConnectionError) + || (result == UnityWebRequest.Result.DataProcessingError) + || (result == UnityWebRequest.Result.ProtocolError); +#else + return unityWebRequest.isHttpError || unityWebRequest.isNetworkError; +#endif + } + } + +#endif +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/UnityWebRequestExtensions.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/UnityWebRequestExtensions.cs.meta new file mode 100644 index 0000000..54bd2eb --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/UnityWebRequestExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 111ba0e639de1d7428af6c823ead4918 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ValueStopwatch.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ValueStopwatch.cs new file mode 100644 index 0000000..d55d1f6 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ValueStopwatch.cs @@ -0,0 +1,37 @@ +using System; +using System.Diagnostics; + +namespace Cysharp.Threading.Tasks.Internal +{ + internal readonly struct ValueStopwatch + { + static readonly double TimestampToTicks = TimeSpan.TicksPerSecond / (double)Stopwatch.Frequency; + + readonly long startTimestamp; + + public static ValueStopwatch StartNew() => new ValueStopwatch(Stopwatch.GetTimestamp()); + + ValueStopwatch(long startTimestamp) + { + this.startTimestamp = startTimestamp; + } + + public TimeSpan Elapsed => TimeSpan.FromTicks(this.ElapsedTicks); + + public bool IsInvalid => startTimestamp == 0; + + public long ElapsedTicks + { + get + { + if (startTimestamp == 0) + { + throw new InvalidOperationException("Detected invalid initialization(use 'default'), only to create from StartNew()."); + } + + var delta = Stopwatch.GetTimestamp() - startTimestamp; + return (long)(delta * TimestampToTicks); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ValueStopwatch.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ValueStopwatch.cs.meta new file mode 100644 index 0000000..b7c6b09 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/ValueStopwatch.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f16fb466974ad034c8732c79c7fd67ea +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/WeakDictionary.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/WeakDictionary.cs new file mode 100644 index 0000000..3feaad8 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/WeakDictionary.cs @@ -0,0 +1,334 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Internal +{ + // Add, Remove, Enumerate with sweep. All operations are thread safe(in spinlock). + internal class WeakDictionary + where TKey : class + { + Entry[] buckets; + int size; + SpinLock gate; // mutable struct(not readonly) + + readonly float loadFactor; + readonly IEqualityComparer keyEqualityComparer; + + public WeakDictionary(int capacity = 4, float loadFactor = 0.75f, IEqualityComparer keyComparer = null) + { + var tableSize = CalculateCapacity(capacity, loadFactor); + this.buckets = new Entry[tableSize]; + this.loadFactor = loadFactor; + this.gate = new SpinLock(false); + this.keyEqualityComparer = keyComparer ?? EqualityComparer.Default; + } + + public bool TryAdd(TKey key, TValue value) + { + bool lockTaken = false; + try + { + gate.Enter(ref lockTaken); + return TryAddInternal(key, value); + } + finally + { + if (lockTaken) gate.Exit(false); + } + } + + public bool TryGetValue(TKey key, out TValue value) + { + bool lockTaken = false; + try + { + gate.Enter(ref lockTaken); + if (TryGetEntry(key, out _, out var entry)) + { + value = entry.Value; + return true; + } + + value = default(TValue); + return false; + } + finally + { + if (lockTaken) gate.Exit(false); + } + } + + public bool TryRemove(TKey key) + { + bool lockTaken = false; + try + { + gate.Enter(ref lockTaken); + if (TryGetEntry(key, out var hashIndex, out var entry)) + { + Remove(hashIndex, entry); + return true; + } + + return false; + } + finally + { + if (lockTaken) gate.Exit(false); + } + } + + bool TryAddInternal(TKey key, TValue value) + { + var nextCapacity = CalculateCapacity(size + 1, loadFactor); + + TRY_ADD_AGAIN: + if (buckets.Length < nextCapacity) + { + // rehash + var nextBucket = new Entry[nextCapacity]; + for (int i = 0; i < buckets.Length; i++) + { + var e = buckets[i]; + while (e != null) + { + AddToBuckets(nextBucket, key, e.Value, e.Hash); + e = e.Next; + } + } + + buckets = nextBucket; + goto TRY_ADD_AGAIN; + } + else + { + // add entry + var successAdd = AddToBuckets(buckets, key, value, keyEqualityComparer.GetHashCode(key)); + if (successAdd) size++; + return successAdd; + } + } + + bool AddToBuckets(Entry[] targetBuckets, TKey newKey, TValue value, int keyHash) + { + var h = keyHash; + var hashIndex = h & (targetBuckets.Length - 1); + + TRY_ADD_AGAIN: + if (targetBuckets[hashIndex] == null) + { + targetBuckets[hashIndex] = new Entry + { + Key = new WeakReference(newKey, false), + Value = value, + Hash = h + }; + + return true; + } + else + { + // add to last. + var entry = targetBuckets[hashIndex]; + while (entry != null) + { + if (entry.Key.TryGetTarget(out var target)) + { + if (keyEqualityComparer.Equals(newKey, target)) + { + return false; // duplicate + } + } + else + { + Remove(hashIndex, entry); + if (targetBuckets[hashIndex] == null) goto TRY_ADD_AGAIN; // add new entry + } + + if (entry.Next != null) + { + entry = entry.Next; + } + else + { + // found last + entry.Next = new Entry + { + Key = new WeakReference(newKey, false), + Value = value, + Hash = h + }; + entry.Next.Prev = entry; + } + } + + return false; + } + } + + bool TryGetEntry(TKey key, out int hashIndex, out Entry entry) + { + var table = buckets; + var hash = keyEqualityComparer.GetHashCode(key); + hashIndex = hash & table.Length - 1; + entry = table[hashIndex]; + + while (entry != null) + { + if (entry.Key.TryGetTarget(out var target)) + { + if (keyEqualityComparer.Equals(key, target)) + { + return true; + } + } + else + { + // sweap + Remove(hashIndex, entry); + } + + entry = entry.Next; + } + + return false; + } + + void Remove(int hashIndex, Entry entry) + { + if (entry.Prev == null && entry.Next == null) + { + buckets[hashIndex] = null; + } + else + { + if (entry.Prev == null) + { + buckets[hashIndex] = entry.Next; + } + if (entry.Prev != null) + { + entry.Prev.Next = entry.Next; + } + if (entry.Next != null) + { + entry.Next.Prev = entry.Prev; + } + } + size--; + } + + public List> ToList() + { + var list = new List>(size); + ToList(ref list, false); + return list; + } + + // avoid allocate everytime. + public int ToList(ref List> list, bool clear = true) + { + if (clear) + { + list.Clear(); + } + + var listIndex = 0; + + bool lockTaken = false; + try + { + for (int i = 0; i < buckets.Length; i++) + { + var entry = buckets[i]; + while (entry != null) + { + if (entry.Key.TryGetTarget(out var target)) + { + var item = new KeyValuePair(target, entry.Value); + if (listIndex < list.Count) + { + list[listIndex++] = item; + } + else + { + list.Add(item); + listIndex++; + } + } + else + { + // sweap + Remove(i, entry); + } + + entry = entry.Next; + } + } + } + finally + { + if (lockTaken) gate.Exit(false); + } + + return listIndex; + } + + static int CalculateCapacity(int collectionSize, float loadFactor) + { + var size = (int)(((float)collectionSize) / loadFactor); + + size--; + size |= size >> 1; + size |= size >> 2; + size |= size >> 4; + size |= size >> 8; + size |= size >> 16; + size += 1; + + if (size < 8) + { + size = 8; + } + return size; + } + + class Entry + { + public WeakReference Key; + public TValue Value; + public int Hash; + public Entry Prev; + public Entry Next; + + // debug only + public override string ToString() + { + if (Key.TryGetTarget(out var target)) + { + return target + "(" + Count() + ")"; + } + else + { + return "(Dead)"; + } + } + + int Count() + { + var count = 1; + var n = this; + while (n.Next != null) + { + count++; + n = n.Next; + } + return count; + } + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/WeakDictionary.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/WeakDictionary.cs.meta new file mode 100644 index 0000000..9dc1672 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Internal/WeakDictionary.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6c78563864409714593226af59bcb6f3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq.meta new file mode 100644 index 0000000..1dcbc32 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4cc94a232b1c1154b8084bdda29c3484 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Aggregate.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Aggregate.cs new file mode 100644 index 0000000..78647ff --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Aggregate.cs @@ -0,0 +1,318 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask AggregateAsync(this IUniTaskAsyncEnumerable source, Func accumulator, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(accumulator, nameof(accumulator)); + + return Aggregate.AggregateAsync(source, accumulator, cancellationToken); + } + + public static UniTask AggregateAsync(this IUniTaskAsyncEnumerable source, TAccumulate seed, Func accumulator, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(accumulator, nameof(accumulator)); + + return Aggregate.AggregateAsync(source, seed, accumulator, cancellationToken); + } + + public static UniTask AggregateAsync(this IUniTaskAsyncEnumerable source, TAccumulate seed, Func accumulator, Func resultSelector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(accumulator, nameof(accumulator)); + Error.ThrowArgumentNullException(accumulator, nameof(resultSelector)); + + return Aggregate.AggregateAsync(source, seed, accumulator, resultSelector, cancellationToken); + } + + public static UniTask AggregateAwaitAsync(this IUniTaskAsyncEnumerable source, Func> accumulator, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(accumulator, nameof(accumulator)); + + return Aggregate.AggregateAwaitAsync(source, accumulator, cancellationToken); + } + + public static UniTask AggregateAwaitAsync(this IUniTaskAsyncEnumerable source, TAccumulate seed, Func> accumulator, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(accumulator, nameof(accumulator)); + + return Aggregate.AggregateAwaitAsync(source, seed, accumulator, cancellationToken); + } + + public static UniTask AggregateAwaitAsync(this IUniTaskAsyncEnumerable source, TAccumulate seed, Func> accumulator, Func> resultSelector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(accumulator, nameof(accumulator)); + Error.ThrowArgumentNullException(accumulator, nameof(resultSelector)); + + return Aggregate.AggregateAwaitAsync(source, seed, accumulator, resultSelector, cancellationToken); + } + + public static UniTask AggregateAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> accumulator, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(accumulator, nameof(accumulator)); + + return Aggregate.AggregateAwaitWithCancellationAsync(source, accumulator, cancellationToken); + } + + public static UniTask AggregateAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, TAccumulate seed, Func> accumulator, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(accumulator, nameof(accumulator)); + + return Aggregate.AggregateAwaitWithCancellationAsync(source, seed, accumulator, cancellationToken); + } + + public static UniTask AggregateAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, TAccumulate seed, Func> accumulator, Func> resultSelector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(accumulator, nameof(accumulator)); + Error.ThrowArgumentNullException(accumulator, nameof(resultSelector)); + + return Aggregate.AggregateAwaitWithCancellationAsync(source, seed, accumulator, resultSelector, cancellationToken); + } + } + + internal static class Aggregate + { + internal static async UniTask AggregateAsync(IUniTaskAsyncEnumerable source, Func accumulator, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + TSource value; + if (await e.MoveNextAsync()) + { + value = e.Current; + } + else + { + throw Error.NoElements(); + } + + while (await e.MoveNextAsync()) + { + value = accumulator(value, e.Current); + } + return value; + + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + internal static async UniTask AggregateAsync(IUniTaskAsyncEnumerable source, TAccumulate seed, Func accumulator, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + TAccumulate value = seed; + while (await e.MoveNextAsync()) + { + value = accumulator(value, e.Current); + } + return value; + + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + internal static async UniTask AggregateAsync(IUniTaskAsyncEnumerable source, TAccumulate seed, Func accumulator, Func resultSelector, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + TAccumulate value = seed; + while (await e.MoveNextAsync()) + { + value = accumulator(value, e.Current); + } + return resultSelector(value); + + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + // with async + + internal static async UniTask AggregateAwaitAsync(IUniTaskAsyncEnumerable source, Func> accumulator, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + TSource value; + if (await e.MoveNextAsync()) + { + value = e.Current; + } + else + { + throw Error.NoElements(); + } + + while (await e.MoveNextAsync()) + { + value = await accumulator(value, e.Current); + } + return value; + + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + internal static async UniTask AggregateAwaitAsync(IUniTaskAsyncEnumerable source, TAccumulate seed, Func> accumulator, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + TAccumulate value = seed; + while (await e.MoveNextAsync()) + { + value = await accumulator(value, e.Current); + } + return value; + + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + internal static async UniTask AggregateAwaitAsync(IUniTaskAsyncEnumerable source, TAccumulate seed, Func> accumulator, Func> resultSelector, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + TAccumulate value = seed; + while (await e.MoveNextAsync()) + { + value = await accumulator(value, e.Current); + } + return await resultSelector(value); + + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + + // with cancellation + + internal static async UniTask AggregateAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> accumulator, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + TSource value; + if (await e.MoveNextAsync()) + { + value = e.Current; + } + else + { + throw Error.NoElements(); + } + + while (await e.MoveNextAsync()) + { + value = await accumulator(value, e.Current, cancellationToken); + } + return value; + + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + internal static async UniTask AggregateAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, TAccumulate seed, Func> accumulator, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + TAccumulate value = seed; + while (await e.MoveNextAsync()) + { + value = await accumulator(value, e.Current, cancellationToken); + } + return value; + + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + internal static async UniTask AggregateAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, TAccumulate seed, Func> accumulator, Func> resultSelector, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + TAccumulate value = seed; + while (await e.MoveNextAsync()) + { + value = await accumulator(value, e.Current, cancellationToken); + } + return await resultSelector(value, cancellationToken); + + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Aggregate.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Aggregate.cs.meta new file mode 100644 index 0000000..837df4a --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Aggregate.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5dc68c05a4228c643937f6ebd185bcca +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/All.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/All.cs new file mode 100644 index 0000000..5d6d5f0 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/All.cs @@ -0,0 +1,108 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask AllAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return All.AllAsync(source, predicate, cancellationToken); + } + + public static UniTask AllAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return All.AllAwaitAsync(source, predicate, cancellationToken); + } + + public static UniTask AllAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return All.AllAwaitWithCancellationAsync(source, predicate, cancellationToken); + } + } + + internal static class All + { + internal static async UniTask AllAsync(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + if (!predicate(e.Current)) + { + return false; + } + } + + return true; + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + internal static async UniTask AllAwaitAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + if (!await predicate(e.Current)) + { + return false; + } + } + + return true; + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + internal static async UniTask AllAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + if (!await predicate(e.Current, cancellationToken)) + { + return false; + } + } + + return true; + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/All.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/All.cs.meta new file mode 100644 index 0000000..d378ff0 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/All.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7271437e0033af2448b600ee248924dd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Any.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Any.cs new file mode 100644 index 0000000..2d43167 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Any.cs @@ -0,0 +1,136 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask AnyAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Any.AnyAsync(source, cancellationToken); + } + + public static UniTask AnyAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return Any.AnyAsync(source, predicate, cancellationToken); + } + + public static UniTask AnyAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return Any.AnyAwaitAsync(source, predicate, cancellationToken); + } + + public static UniTask AnyAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return Any.AnyAwaitWithCancellationAsync(source, predicate, cancellationToken); + } + } + + internal static class Any + { + internal static async UniTask AnyAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + if (await e.MoveNextAsync()) + { + return true; + } + + return false; + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + internal static async UniTask AnyAsync(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + if (predicate(e.Current)) + { + return true; + } + } + + return false; + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + internal static async UniTask AnyAwaitAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + if (await predicate(e.Current)) + { + return true; + } + } + + return false; + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + internal static async UniTask AnyAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + if (await predicate(e.Current, cancellationToken)) + { + return true; + } + } + + return false; + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Any.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Any.cs.meta new file mode 100644 index 0000000..1070bcc --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Any.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e2b2e65745263994fbe34f3e0ec8eb12 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AppendPrepend.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AppendPrepend.cs new file mode 100644 index 0000000..3935afd --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AppendPrepend.cs @@ -0,0 +1,151 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Append(this IUniTaskAsyncEnumerable source, TSource element) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new AppendPrepend(source, element, true); + } + + public static IUniTaskAsyncEnumerable Prepend(this IUniTaskAsyncEnumerable source, TSource element) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new AppendPrepend(source, element, false); + } + } + + internal sealed class AppendPrepend : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly TSource element; + readonly bool append; // or prepend + + public AppendPrepend(IUniTaskAsyncEnumerable source, TSource element, bool append) + { + this.source = source; + this.element = element; + this.append = append; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _AppendPrepend(source, element, append, cancellationToken); + } + + sealed class _AppendPrepend : MoveNextSource, IUniTaskAsyncEnumerator + { + enum State : byte + { + None, + RequirePrepend, + RequireAppend, + Completed + } + + static readonly Action MoveNextCoreDelegate = MoveNextCore; + + readonly IUniTaskAsyncEnumerable source; + readonly TSource element; + CancellationToken cancellationToken; + + State state; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + + public _AppendPrepend(IUniTaskAsyncEnumerable source, TSource element, bool append, CancellationToken cancellationToken) + { + this.source = source; + this.element = element; + this.state = append ? State.RequireAppend : State.RequirePrepend; + this.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (enumerator == null) + { + if (state == State.RequirePrepend) + { + Current = element; + state = State.None; + return CompletedTasks.True; + } + + enumerator = source.GetAsyncEnumerator(cancellationToken); + } + + if (state == State.Completed) + { + return CompletedTasks.False; + } + + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + + if (awaiter.IsCompleted) + { + MoveNextCoreDelegate(this); + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + + return new UniTask(this, completionSource.Version); + } + + static void MoveNextCore(object state) + { + var self = (_AppendPrepend)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + self.Current = self.enumerator.Current; + self.completionSource.TrySetResult(true); + } + else + { + if (self.state == State.RequireAppend) + { + self.state = State.Completed; + self.Current = self.element; + self.completionSource.TrySetResult(true); + } + else + { + self.state = State.Completed; + self.completionSource.TrySetResult(false); + } + } + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + return default; + } + } + } + +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AppendPrepend.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AppendPrepend.cs.meta new file mode 100644 index 0000000..6d2ee04 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AppendPrepend.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3268ec424b8055f45aa2a26d17c80468 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AsUniTaskAsyncEnumerable.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AsUniTaskAsyncEnumerable.cs new file mode 100644 index 0000000..c00452e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AsUniTaskAsyncEnumerable.cs @@ -0,0 +1,10 @@ +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable AsUniTaskAsyncEnumerable(this IUniTaskAsyncEnumerable source) + { + return source; + } + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AsUniTaskAsyncEnumerable.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AsUniTaskAsyncEnumerable.cs.meta new file mode 100644 index 0000000..90f6207 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AsUniTaskAsyncEnumerable.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 69866e262589ea643bbc62a1d696077a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AsyncEnumeratorBase.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AsyncEnumeratorBase.cs new file mode 100644 index 0000000..e7f9968 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AsyncEnumeratorBase.cs @@ -0,0 +1,356 @@ +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + // note: refactor all inherit class and should remove this. + // see Select and Where. + internal abstract class AsyncEnumeratorBase : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action moveNextCallbackDelegate = MoveNextCallBack; + + readonly IUniTaskAsyncEnumerable source; + protected CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter sourceMoveNext; + + public AsyncEnumeratorBase(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + this.source = source; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 4); + } + + // abstract + + /// + /// If return value is false, continue source.MoveNext. + /// + protected abstract bool TryMoveNextCore(bool sourceHasCurrent, out bool result); + + // Util + protected TSource SourceCurrent => enumerator.Current; + + // IUniTaskAsyncEnumerator + + public TResult Current { get; protected set; } + + public UniTask MoveNextAsync() + { + if (enumerator == null) + { + enumerator = source.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + if (!OnFirstIteration()) + { + SourceMoveNext(); + } + return new UniTask(this, completionSource.Version); + } + + protected virtual bool OnFirstIteration() + { + return false; + } + + protected void SourceMoveNext() + { + CONTINUE: + sourceMoveNext = enumerator.MoveNextAsync().GetAwaiter(); + if (sourceMoveNext.IsCompleted) + { + bool result = false; + try + { + if (!TryMoveNextCore(sourceMoveNext.GetResult(), out result)) + { + goto CONTINUE; + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + + if (cancellationToken.IsCancellationRequested) + { + completionSource.TrySetCanceled(cancellationToken); + } + else + { + completionSource.TrySetResult(result); + } + } + else + { + sourceMoveNext.SourceOnCompleted(moveNextCallbackDelegate, this); + } + } + + static void MoveNextCallBack(object state) + { + var self = (AsyncEnumeratorBase)state; + bool result; + try + { + if (!self.TryMoveNextCore(self.sourceMoveNext.GetResult(), out result)) + { + self.SourceMoveNext(); + return; + } + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + return; + } + + if (self.cancellationToken.IsCancellationRequested) + { + self.completionSource.TrySetCanceled(self.cancellationToken); + } + else + { + self.completionSource.TrySetResult(result); + } + } + + // if require additional resource to dispose, override and call base.DisposeAsync. + public virtual UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + return default; + } + } + + internal abstract class AsyncEnumeratorAwaitSelectorBase : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action moveNextCallbackDelegate = MoveNextCallBack; + static readonly Action setCurrentCallbackDelegate = SetCurrentCallBack; + + + readonly IUniTaskAsyncEnumerable source; + protected CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter sourceMoveNext; + + UniTask.Awaiter resultAwaiter; + + public AsyncEnumeratorAwaitSelectorBase(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + this.source = source; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 4); + } + + // abstract + + protected abstract UniTask TransformAsync(TSource sourceCurrent); + protected abstract bool TrySetCurrentCore(TAwait awaitResult, out bool terminateIteration); + + // Util + protected TSource SourceCurrent { get; private set; } + + protected (bool waitCallback, bool requireNextIteration) ActionCompleted(bool trySetCurrentResult, out bool moveNextResult) + { + if (trySetCurrentResult) + { + moveNextResult = true; + return (false, false); + } + else + { + moveNextResult = default; + return (false, true); + } + } + protected (bool waitCallback, bool requireNextIteration) WaitAwaitCallback(out bool moveNextResult) { moveNextResult = default; return (true, false); } + protected (bool waitCallback, bool requireNextIteration) IterateFinished(out bool moveNextResult) { moveNextResult = false; return (false, false); } + + // IUniTaskAsyncEnumerator + + public TResult Current { get; protected set; } + + public UniTask MoveNextAsync() + { + if (enumerator == null) + { + enumerator = source.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + SourceMoveNext(); + return new UniTask(this, completionSource.Version); + } + + protected void SourceMoveNext() + { + CONTINUE: + sourceMoveNext = enumerator.MoveNextAsync().GetAwaiter(); + if (sourceMoveNext.IsCompleted) + { + bool result = false; + try + { + (bool waitCallback, bool requireNextIteration) = TryMoveNextCore(sourceMoveNext.GetResult(), out result); + + if (waitCallback) + { + return; + } + + if (requireNextIteration) + { + goto CONTINUE; + } + else + { + completionSource.TrySetResult(result); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + } + else + { + sourceMoveNext.SourceOnCompleted(moveNextCallbackDelegate, this); + } + } + + (bool waitCallback, bool requireNextIteration) TryMoveNextCore(bool sourceHasCurrent, out bool result) + { + if (sourceHasCurrent) + { + SourceCurrent = enumerator.Current; + var task = TransformAsync(SourceCurrent); + if (UnwarapTask(task, out var taskResult)) + { + var currentResult = TrySetCurrentCore(taskResult, out var terminateIteration); + if (terminateIteration) + { + return IterateFinished(out result); + } + + return ActionCompleted(currentResult, out result); + } + else + { + return WaitAwaitCallback(out result); + } + } + + return IterateFinished(out result); + } + + protected bool UnwarapTask(UniTask taskResult, out TAwait result) + { + resultAwaiter = taskResult.GetAwaiter(); + + if (resultAwaiter.IsCompleted) + { + result = resultAwaiter.GetResult(); + return true; + } + else + { + resultAwaiter.SourceOnCompleted(setCurrentCallbackDelegate, this); + result = default; + return false; + } + } + + static void MoveNextCallBack(object state) + { + var self = (AsyncEnumeratorAwaitSelectorBase)state; + bool result = false; + try + { + (bool waitCallback, bool requireNextIteration) = self.TryMoveNextCore(self.sourceMoveNext.GetResult(), out result); + + if (waitCallback) + { + return; + } + + if (requireNextIteration) + { + self.SourceMoveNext(); + return; + } + else + { + self.completionSource.TrySetResult(result); + } + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + return; + } + } + + static void SetCurrentCallBack(object state) + { + var self = (AsyncEnumeratorAwaitSelectorBase)state; + + bool doneSetCurrent; + bool terminateIteration; + try + { + var result = self.resultAwaiter.GetResult(); + doneSetCurrent = self.TrySetCurrentCore(result, out terminateIteration); + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + return; + } + + if (self.cancellationToken.IsCancellationRequested) + { + self.completionSource.TrySetCanceled(self.cancellationToken); + } + else + { + if (doneSetCurrent) + { + self.completionSource.TrySetResult(true); + } + else + { + if (terminateIteration) + { + self.completionSource.TrySetResult(false); + } + else + { + self.SourceMoveNext(); + } + } + } + } + + // if require additional resource to dispose, override and call base.DisposeAsync. + public virtual UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + return default; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AsyncEnumeratorBase.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AsyncEnumeratorBase.cs.meta new file mode 100644 index 0000000..a4e96dc --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/AsyncEnumeratorBase.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 01ba1d3b17e13fb4c95740131c7e6e19 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Average.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Average.cs new file mode 100644 index 0000000..b2ce42c --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Average.cs @@ -0,0 +1,1524 @@ +using System; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Average.AverageAsync(source, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Average.AverageAsync(source, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Average.AverageAsync(source, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Average.AverageAsync(source, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Average.AverageAsync(source, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Average.AverageAsync(source, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Average.AverageAsync(source, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Average.AverageAsync(source, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Average.AverageAsync(source, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Average.AverageAsync(source, cancellationToken); + } + + public static UniTask AverageAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask AverageAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + } + + internal static class Average + { + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + long count = 0; + Int32 sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += e.Current; + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (double)sum / count; + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + long count = 0; + Int32 sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += selector(e.Current); + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (double)sum / count; + } + + public static async UniTask AverageAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Int32 sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += await selector(e.Current); + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (double)sum / count; + } + + public static async UniTask AverageAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Int32 sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += await selector(e.Current, cancellationToken); + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (double)sum / count; + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + long count = 0; + Int64 sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += e.Current; + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (double)sum / count; + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + long count = 0; + Int64 sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += selector(e.Current); + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (double)sum / count; + } + + public static async UniTask AverageAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Int64 sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += await selector(e.Current); + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (double)sum / count; + } + + public static async UniTask AverageAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Int64 sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += await selector(e.Current, cancellationToken); + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (double)sum / count; + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + long count = 0; + Single sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += e.Current; + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (float)(sum / count); + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + long count = 0; + Single sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += selector(e.Current); + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (float)(sum / count); + } + + public static async UniTask AverageAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Single sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += await selector(e.Current); + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (float)(sum / count); + } + + public static async UniTask AverageAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Single sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += await selector(e.Current, cancellationToken); + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (float)(sum / count); + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + long count = 0; + Double sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += e.Current; + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum / count; + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + long count = 0; + Double sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += selector(e.Current); + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum / count; + } + + public static async UniTask AverageAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Double sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += await selector(e.Current); + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum / count; + } + + public static async UniTask AverageAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Double sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += await selector(e.Current, cancellationToken); + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum / count; + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + long count = 0; + Decimal sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += e.Current; + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum / count; + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + long count = 0; + Decimal sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += selector(e.Current); + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum / count; + } + + public static async UniTask AverageAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Decimal sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += await selector(e.Current); + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum / count; + } + + public static async UniTask AverageAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Decimal sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked + { + sum += await selector(e.Current, cancellationToken); + count++; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum / count; + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + long count = 0; + Int32? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = e.Current; + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (double)sum / count; + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + long count = 0; + Int32? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = selector(e.Current); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (double)sum / count; + } + + public static async UniTask AverageAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Int32? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = await selector(e.Current); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (double)sum / count; + } + + public static async UniTask AverageAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Int32? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = await selector(e.Current, cancellationToken); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (double)sum / count; + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + long count = 0; + Int64? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = e.Current; + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (double)sum / count; + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + long count = 0; + Int64? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = selector(e.Current); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (double)sum / count; + } + + public static async UniTask AverageAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Int64? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = await selector(e.Current); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (double)sum / count; + } + + public static async UniTask AverageAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Int64? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = await selector(e.Current, cancellationToken); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (double)sum / count; + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + long count = 0; + Single? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = e.Current; + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (float)(sum / count); + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + long count = 0; + Single? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = selector(e.Current); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (float)(sum / count); + } + + public static async UniTask AverageAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Single? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = await selector(e.Current); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (float)(sum / count); + } + + public static async UniTask AverageAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Single? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = await selector(e.Current, cancellationToken); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return (float)(sum / count); + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + long count = 0; + Double? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = e.Current; + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum / count; + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + long count = 0; + Double? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = selector(e.Current); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum / count; + } + + public static async UniTask AverageAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Double? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = await selector(e.Current); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum / count; + } + + public static async UniTask AverageAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Double? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = await selector(e.Current, cancellationToken); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum / count; + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + long count = 0; + Decimal? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = e.Current; + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum / count; + } + + public static async UniTask AverageAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + long count = 0; + Decimal? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = selector(e.Current); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum / count; + } + + public static async UniTask AverageAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Decimal? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = await selector(e.Current); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum / count; + } + + public static async UniTask AverageAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + Decimal? sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = await selector(e.Current, cancellationToken); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum / count; + } + + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Average.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Average.cs.meta new file mode 100644 index 0000000..8f60dfc --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Average.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 58499f95012fb3c47bb7bcbc5862e562 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Average.tt b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Average.tt new file mode 100644 index 0000000..9009fd1 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Average.tt @@ -0,0 +1,237 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".cs" #> +<# + var types = new[] + { + (typeof(int), "double"), + (typeof(long), "double"), + (typeof(float),"float"), + (typeof(double),"double"), + (typeof(decimal),"decimal"), + + (typeof(int?),"double?"), + (typeof(long?),"double?"), + (typeof(float?),"float?"), + (typeof(double?),"double?"), + (typeof(decimal?),"decimal?"), + }; + + Func IsNullable = x => x.IsGenericType; + Func ElementType = x => IsNullable(x) ? x.GetGenericArguments()[0] : x; + Func TypeName = x => IsNullable(x) ? x.GetGenericArguments()[0].Name + "?" : x.Name; + Func WithSuffix = x => IsNullable(x) ? ".GetValueOrDefault()" : ""; + Func CalcResult = x => { var e = ElementType(x); return (e == typeof(int) || e == typeof(long)) ? "(double)sum / count" : (e == typeof(float)) ? "(float)(sum / count)" : "sum / count"; }; +#> +using System; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { +<# foreach(var (t, ret) in types) { #> + public static UniTask<<#= ret #>> AverageAsync(this IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Average.AverageAsync(source, cancellationToken); + } + + public static UniTask<<#= ret #>> AverageAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAsync(source, selector, cancellationToken); + } + + public static UniTask<<#= ret #>> AverageAwaitAsync(this IUniTaskAsyncEnumerable source, Func>> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask<<#= ret #>> AverageAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func>> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Average.AverageAwaitWithCancellationAsync(source, selector, cancellationToken); + } + +<# } #> + } + + internal static class Average + { +<# foreach(var (t, ret) in types) { #> + public static async UniTask<<#= ret #>> AverageAsync(IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken) + { + long count = 0; + <#= TypeName(t) #> sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { +<# if (IsNullable(t)) { #> + var v = e.Current; + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } +<# } else { #> + checked + { + sum += e.Current; + count++; + } +<# } #> + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return <#= CalcResult(t) #>; + } + + public static async UniTask<<#= ret #>> AverageAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + long count = 0; + <#= TypeName(t) #> sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { +<# if (IsNullable(t)) { #> + var v = selector(e.Current); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } +<# } else { #> + checked + { + sum += selector(e.Current); + count++; + } +<# } #> + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return <#= CalcResult(t) #>; + } + + public static async UniTask<<#= ret #>> AverageAwaitAsync(IUniTaskAsyncEnumerable source, Func>> selector, CancellationToken cancellationToken) + { + long count = 0; + <#= TypeName(t) #> sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { +<# if (IsNullable(t)) { #> + var v = await selector(e.Current); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } +<# } else { #> + checked + { + sum += await selector(e.Current); + count++; + } +<# } #> + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return <#= CalcResult(t) #>; + } + + public static async UniTask<<#= ret #>> AverageAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func>> selector, CancellationToken cancellationToken) + { + long count = 0; + <#= TypeName(t) #> sum = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { +<# if (IsNullable(t)) { #> + var v = await selector(e.Current, cancellationToken); + if (v.HasValue) + { + checked + { + sum += v.Value; + count++; + } + } +<# } else { #> + checked + { + sum += await selector(e.Current, cancellationToken); + count++; + } +<# } #> + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return <#= CalcResult(t) #>; + } + +<# } #> + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Average.tt.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Average.tt.meta new file mode 100644 index 0000000..e0a43fd --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Average.tt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 84bce45768c171d4490153eb08630a98 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Buffer.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Buffer.cs new file mode 100644 index 0000000..be395b6 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Buffer.cs @@ -0,0 +1,345 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable> Buffer(this IUniTaskAsyncEnumerable source, Int32 count) + { + Error.ThrowArgumentNullException(source, nameof(source)); + if (count <= 0) throw Error.ArgumentOutOfRange(nameof(count)); + + return new Buffer(source, count); + } + + public static IUniTaskAsyncEnumerable> Buffer(this IUniTaskAsyncEnumerable source, Int32 count, Int32 skip) + { + Error.ThrowArgumentNullException(source, nameof(source)); + if (count <= 0) throw Error.ArgumentOutOfRange(nameof(count)); + if (skip <= 0) throw Error.ArgumentOutOfRange(nameof(skip)); + + return new BufferSkip(source, count, skip); + } + } + + internal sealed class Buffer : IUniTaskAsyncEnumerable> + { + readonly IUniTaskAsyncEnumerable source; + readonly int count; + + public Buffer(IUniTaskAsyncEnumerable source, int count) + { + this.source = source; + this.count = count; + } + + public IUniTaskAsyncEnumerator> GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Buffer(source, count, cancellationToken); + } + + sealed class _Buffer : MoveNextSource, IUniTaskAsyncEnumerator> + { + static readonly Action MoveNextCoreDelegate = MoveNextCore; + + readonly IUniTaskAsyncEnumerable source; + readonly int count; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + bool continueNext; + + bool completed; + List buffer; + + public _Buffer(IUniTaskAsyncEnumerable source, int count, CancellationToken cancellationToken) + { + this.source = source; + this.count = count; + this.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(this, 3); + } + + public IList Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + if (enumerator == null) + { + enumerator = source.GetAsyncEnumerator(cancellationToken); + buffer = new List(count); + } + + completionSource.Reset(); + SourceMoveNext(); + return new UniTask(this, completionSource.Version); + } + + void SourceMoveNext() + { + if (completed) + { + if (buffer != null && buffer.Count > 0) + { + var ret = buffer; + buffer = null; + Current = ret; + completionSource.TrySetResult(true); + return; + } + else + { + completionSource.TrySetResult(false); + return; + } + } + + try + { + + LOOP: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + continueNext = true; + MoveNextCore(this); + if (continueNext) + { + continueNext = false; + goto LOOP; // avoid recursive + } + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + + static void MoveNextCore(object state) + { + var self = (_Buffer)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + self.buffer.Add(self.enumerator.Current); + + if (self.buffer.Count == self.count) + { + self.Current = self.buffer; + self.buffer = new List(self.count); + self.continueNext = false; + self.completionSource.TrySetResult(true); + return; + } + else + { + if (!self.continueNext) + { + self.SourceMoveNext(); + } + } + } + else + { + self.continueNext = false; + self.completed = true; + self.SourceMoveNext(); + } + } + else + { + self.continueNext = false; + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + return default; + } + } + } + + internal sealed class BufferSkip : IUniTaskAsyncEnumerable> + { + readonly IUniTaskAsyncEnumerable source; + readonly int count; + readonly int skip; + + public BufferSkip(IUniTaskAsyncEnumerable source, int count, int skip) + { + this.source = source; + this.count = count; + this.skip = skip; + } + + public IUniTaskAsyncEnumerator> GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _BufferSkip(source, count, skip, cancellationToken); + } + + sealed class _BufferSkip : MoveNextSource, IUniTaskAsyncEnumerator> + { + static readonly Action MoveNextCoreDelegate = MoveNextCore; + + readonly IUniTaskAsyncEnumerable source; + readonly int count; + readonly int skip; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + bool continueNext; + + bool completed; + Queue> buffers; + int index = 0; + + public _BufferSkip(IUniTaskAsyncEnumerable source, int count, int skip, CancellationToken cancellationToken) + { + this.source = source; + this.count = count; + this.skip = skip; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public IList Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + if (enumerator == null) + { + enumerator = source.GetAsyncEnumerator(cancellationToken); + buffers = new Queue>(); + } + + completionSource.Reset(); + SourceMoveNext(); + return new UniTask(this, completionSource.Version); + } + + void SourceMoveNext() + { + if (completed) + { + if (buffers.Count > 0) + { + Current = buffers.Dequeue(); + completionSource.TrySetResult(true); + return; + } + else + { + completionSource.TrySetResult(false); + return; + } + } + + try + { + + LOOP: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + continueNext = true; + MoveNextCore(this); + if (continueNext) + { + continueNext = false; + goto LOOP; // avoid recursive + } + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + + static void MoveNextCore(object state) + { + var self = (_BufferSkip)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + if (self.index++ % self.skip == 0) + { + self.buffers.Enqueue(new List(self.count)); + } + + var item = self.enumerator.Current; + foreach (var buffer in self.buffers) + { + buffer.Add(item); + } + + if (self.buffers.Count > 0 && self.buffers.Peek().Count == self.count) + { + self.Current = self.buffers.Dequeue(); + self.continueNext = false; + self.completionSource.TrySetResult(true); + return; + } + else + { + if (!self.continueNext) + { + self.SourceMoveNext(); + } + } + } + else + { + self.continueNext = false; + self.completed = true; + self.SourceMoveNext(); + } + } + else + { + self.continueNext = false; + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Buffer.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Buffer.cs.meta new file mode 100644 index 0000000..e7154e4 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Buffer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 951310243334a3148a7872977cb31c5c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Cast.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Cast.cs new file mode 100644 index 0000000..0a0c0f8 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Cast.cs @@ -0,0 +1,53 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Cast(this IUniTaskAsyncEnumerable source) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new Cast(source); + } + } + + internal sealed class Cast : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + + public Cast(IUniTaskAsyncEnumerable source) + { + this.source = source; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Cast(source, cancellationToken); + } + + class _Cast : AsyncEnumeratorBase + { + public _Cast(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + + : base(source, cancellationToken) + { + } + + protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result) + { + if (sourceHasCurrent) + { + Current = (TResult)SourceCurrent; + result = true; + return true; + } + + result = false; + return true; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Cast.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Cast.cs.meta new file mode 100644 index 0000000..913b043 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Cast.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: edebeae8b61352b428abe9ce8f3fc71a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/CombineLatest.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/CombineLatest.cs new file mode 100644 index 0000000..92fb1da --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/CombineLatest.cs @@ -0,0 +1,11372 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable CombineLatest(this IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, Func resultSelector) + { + Error.ThrowArgumentNullException(source1, nameof(source1)); + Error.ThrowArgumentNullException(source2, nameof(source2)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new CombineLatest(source1, source2, resultSelector); + } + + public static IUniTaskAsyncEnumerable CombineLatest(this IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, Func resultSelector) + { + Error.ThrowArgumentNullException(source1, nameof(source1)); + Error.ThrowArgumentNullException(source2, nameof(source2)); + Error.ThrowArgumentNullException(source3, nameof(source3)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new CombineLatest(source1, source2, source3, resultSelector); + } + + public static IUniTaskAsyncEnumerable CombineLatest(this IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, Func resultSelector) + { + Error.ThrowArgumentNullException(source1, nameof(source1)); + Error.ThrowArgumentNullException(source2, nameof(source2)); + Error.ThrowArgumentNullException(source3, nameof(source3)); + Error.ThrowArgumentNullException(source4, nameof(source4)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new CombineLatest(source1, source2, source3, source4, resultSelector); + } + + public static IUniTaskAsyncEnumerable CombineLatest(this IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, Func resultSelector) + { + Error.ThrowArgumentNullException(source1, nameof(source1)); + Error.ThrowArgumentNullException(source2, nameof(source2)); + Error.ThrowArgumentNullException(source3, nameof(source3)); + Error.ThrowArgumentNullException(source4, nameof(source4)); + Error.ThrowArgumentNullException(source5, nameof(source5)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new CombineLatest(source1, source2, source3, source4, source5, resultSelector); + } + + public static IUniTaskAsyncEnumerable CombineLatest(this IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, Func resultSelector) + { + Error.ThrowArgumentNullException(source1, nameof(source1)); + Error.ThrowArgumentNullException(source2, nameof(source2)); + Error.ThrowArgumentNullException(source3, nameof(source3)); + Error.ThrowArgumentNullException(source4, nameof(source4)); + Error.ThrowArgumentNullException(source5, nameof(source5)); + Error.ThrowArgumentNullException(source6, nameof(source6)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new CombineLatest(source1, source2, source3, source4, source5, source6, resultSelector); + } + + public static IUniTaskAsyncEnumerable CombineLatest(this IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, Func resultSelector) + { + Error.ThrowArgumentNullException(source1, nameof(source1)); + Error.ThrowArgumentNullException(source2, nameof(source2)); + Error.ThrowArgumentNullException(source3, nameof(source3)); + Error.ThrowArgumentNullException(source4, nameof(source4)); + Error.ThrowArgumentNullException(source5, nameof(source5)); + Error.ThrowArgumentNullException(source6, nameof(source6)); + Error.ThrowArgumentNullException(source7, nameof(source7)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new CombineLatest(source1, source2, source3, source4, source5, source6, source7, resultSelector); + } + + public static IUniTaskAsyncEnumerable CombineLatest(this IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, Func resultSelector) + { + Error.ThrowArgumentNullException(source1, nameof(source1)); + Error.ThrowArgumentNullException(source2, nameof(source2)); + Error.ThrowArgumentNullException(source3, nameof(source3)); + Error.ThrowArgumentNullException(source4, nameof(source4)); + Error.ThrowArgumentNullException(source5, nameof(source5)); + Error.ThrowArgumentNullException(source6, nameof(source6)); + Error.ThrowArgumentNullException(source7, nameof(source7)); + Error.ThrowArgumentNullException(source8, nameof(source8)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, resultSelector); + } + + public static IUniTaskAsyncEnumerable CombineLatest(this IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, Func resultSelector) + { + Error.ThrowArgumentNullException(source1, nameof(source1)); + Error.ThrowArgumentNullException(source2, nameof(source2)); + Error.ThrowArgumentNullException(source3, nameof(source3)); + Error.ThrowArgumentNullException(source4, nameof(source4)); + Error.ThrowArgumentNullException(source5, nameof(source5)); + Error.ThrowArgumentNullException(source6, nameof(source6)); + Error.ThrowArgumentNullException(source7, nameof(source7)); + Error.ThrowArgumentNullException(source8, nameof(source8)); + Error.ThrowArgumentNullException(source9, nameof(source9)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, resultSelector); + } + + public static IUniTaskAsyncEnumerable CombineLatest(this IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, Func resultSelector) + { + Error.ThrowArgumentNullException(source1, nameof(source1)); + Error.ThrowArgumentNullException(source2, nameof(source2)); + Error.ThrowArgumentNullException(source3, nameof(source3)); + Error.ThrowArgumentNullException(source4, nameof(source4)); + Error.ThrowArgumentNullException(source5, nameof(source5)); + Error.ThrowArgumentNullException(source6, nameof(source6)); + Error.ThrowArgumentNullException(source7, nameof(source7)); + Error.ThrowArgumentNullException(source8, nameof(source8)); + Error.ThrowArgumentNullException(source9, nameof(source9)); + Error.ThrowArgumentNullException(source10, nameof(source10)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, resultSelector); + } + + public static IUniTaskAsyncEnumerable CombineLatest(this IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, IUniTaskAsyncEnumerable source11, Func resultSelector) + { + Error.ThrowArgumentNullException(source1, nameof(source1)); + Error.ThrowArgumentNullException(source2, nameof(source2)); + Error.ThrowArgumentNullException(source3, nameof(source3)); + Error.ThrowArgumentNullException(source4, nameof(source4)); + Error.ThrowArgumentNullException(source5, nameof(source5)); + Error.ThrowArgumentNullException(source6, nameof(source6)); + Error.ThrowArgumentNullException(source7, nameof(source7)); + Error.ThrowArgumentNullException(source8, nameof(source8)); + Error.ThrowArgumentNullException(source9, nameof(source9)); + Error.ThrowArgumentNullException(source10, nameof(source10)); + Error.ThrowArgumentNullException(source11, nameof(source11)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, resultSelector); + } + + public static IUniTaskAsyncEnumerable CombineLatest(this IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, IUniTaskAsyncEnumerable source11, IUniTaskAsyncEnumerable source12, Func resultSelector) + { + Error.ThrowArgumentNullException(source1, nameof(source1)); + Error.ThrowArgumentNullException(source2, nameof(source2)); + Error.ThrowArgumentNullException(source3, nameof(source3)); + Error.ThrowArgumentNullException(source4, nameof(source4)); + Error.ThrowArgumentNullException(source5, nameof(source5)); + Error.ThrowArgumentNullException(source6, nameof(source6)); + Error.ThrowArgumentNullException(source7, nameof(source7)); + Error.ThrowArgumentNullException(source8, nameof(source8)); + Error.ThrowArgumentNullException(source9, nameof(source9)); + Error.ThrowArgumentNullException(source10, nameof(source10)); + Error.ThrowArgumentNullException(source11, nameof(source11)); + Error.ThrowArgumentNullException(source12, nameof(source12)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, source12, resultSelector); + } + + public static IUniTaskAsyncEnumerable CombineLatest(this IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, IUniTaskAsyncEnumerable source11, IUniTaskAsyncEnumerable source12, IUniTaskAsyncEnumerable source13, Func resultSelector) + { + Error.ThrowArgumentNullException(source1, nameof(source1)); + Error.ThrowArgumentNullException(source2, nameof(source2)); + Error.ThrowArgumentNullException(source3, nameof(source3)); + Error.ThrowArgumentNullException(source4, nameof(source4)); + Error.ThrowArgumentNullException(source5, nameof(source5)); + Error.ThrowArgumentNullException(source6, nameof(source6)); + Error.ThrowArgumentNullException(source7, nameof(source7)); + Error.ThrowArgumentNullException(source8, nameof(source8)); + Error.ThrowArgumentNullException(source9, nameof(source9)); + Error.ThrowArgumentNullException(source10, nameof(source10)); + Error.ThrowArgumentNullException(source11, nameof(source11)); + Error.ThrowArgumentNullException(source12, nameof(source12)); + Error.ThrowArgumentNullException(source13, nameof(source13)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, source12, source13, resultSelector); + } + + public static IUniTaskAsyncEnumerable CombineLatest(this IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, IUniTaskAsyncEnumerable source11, IUniTaskAsyncEnumerable source12, IUniTaskAsyncEnumerable source13, IUniTaskAsyncEnumerable source14, Func resultSelector) + { + Error.ThrowArgumentNullException(source1, nameof(source1)); + Error.ThrowArgumentNullException(source2, nameof(source2)); + Error.ThrowArgumentNullException(source3, nameof(source3)); + Error.ThrowArgumentNullException(source4, nameof(source4)); + Error.ThrowArgumentNullException(source5, nameof(source5)); + Error.ThrowArgumentNullException(source6, nameof(source6)); + Error.ThrowArgumentNullException(source7, nameof(source7)); + Error.ThrowArgumentNullException(source8, nameof(source8)); + Error.ThrowArgumentNullException(source9, nameof(source9)); + Error.ThrowArgumentNullException(source10, nameof(source10)); + Error.ThrowArgumentNullException(source11, nameof(source11)); + Error.ThrowArgumentNullException(source12, nameof(source12)); + Error.ThrowArgumentNullException(source13, nameof(source13)); + Error.ThrowArgumentNullException(source14, nameof(source14)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, source12, source13, source14, resultSelector); + } + + public static IUniTaskAsyncEnumerable CombineLatest(this IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, IUniTaskAsyncEnumerable source11, IUniTaskAsyncEnumerable source12, IUniTaskAsyncEnumerable source13, IUniTaskAsyncEnumerable source14, IUniTaskAsyncEnumerable source15, Func resultSelector) + { + Error.ThrowArgumentNullException(source1, nameof(source1)); + Error.ThrowArgumentNullException(source2, nameof(source2)); + Error.ThrowArgumentNullException(source3, nameof(source3)); + Error.ThrowArgumentNullException(source4, nameof(source4)); + Error.ThrowArgumentNullException(source5, nameof(source5)); + Error.ThrowArgumentNullException(source6, nameof(source6)); + Error.ThrowArgumentNullException(source7, nameof(source7)); + Error.ThrowArgumentNullException(source8, nameof(source8)); + Error.ThrowArgumentNullException(source9, nameof(source9)); + Error.ThrowArgumentNullException(source10, nameof(source10)); + Error.ThrowArgumentNullException(source11, nameof(source11)); + Error.ThrowArgumentNullException(source12, nameof(source12)); + Error.ThrowArgumentNullException(source13, nameof(source13)); + Error.ThrowArgumentNullException(source14, nameof(source14)); + Error.ThrowArgumentNullException(source15, nameof(source15)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, source12, source13, source14, source15, resultSelector); + } + + } + + internal class CombineLatest : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + + readonly Func resultSelector; + + public CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, Func resultSelector) + { + this.source1 = source1; + this.source2 = source2; + + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _CombineLatest(source1, source2, resultSelector, cancellationToken); + } + + class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action Completed1Delegate = Completed1; + static readonly Action Completed2Delegate = Completed2; + const int CompleteCount = 2; + + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + + readonly Func resultSelector; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator1; + UniTask.Awaiter awaiter1; + bool hasCurrent1; + bool running1; + T1 current1; + + IUniTaskAsyncEnumerator enumerator2; + UniTask.Awaiter awaiter2; + bool hasCurrent2; + bool running2; + T2 current2; + + int completedCount; + bool syncRunning; + TResult result; + + public _CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, Func resultSelector, CancellationToken cancellationToken) + { + this.source1 = source1; + this.source2 = source2; + + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current => result; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + if (completedCount == CompleteCount) return CompletedTasks.False; + + if (enumerator1 == null) + { + enumerator1 = source1.GetAsyncEnumerator(cancellationToken); + enumerator2 = source2.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + + AGAIN: + syncRunning = true; + if (!running1) + { + running1 = true; + awaiter1 = enumerator1.MoveNextAsync().GetAwaiter(); + if (awaiter1.IsCompleted) + { + Completed1(this); + } + else + { + awaiter1.SourceOnCompleted(Completed1Delegate, this); + } + } + if (!running2) + { + running2 = true; + awaiter2 = enumerator2.MoveNextAsync().GetAwaiter(); + if (awaiter2.IsCompleted) + { + Completed2(this); + } + else + { + awaiter2.SourceOnCompleted(Completed2Delegate, this); + } + } + + if (!running1 || !running2) + { + goto AGAIN; + } + syncRunning = false; + + return new UniTask(this, completionSource.Version); + } + + static void Completed1(object state) + { + var self = (_CombineLatest)state; + self.running1 = false; + + try + { + if (self.awaiter1.GetResult()) + { + self.hasCurrent1 = true; + self.current1 = self.enumerator1.Current; + goto SUCCESS; + } + else + { + self.running1 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running1 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running1 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter1.SourceOnCompleted(Completed1Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed2(object state) + { + var self = (_CombineLatest)state; + self.running2 = false; + + try + { + if (self.awaiter2.GetResult()) + { + self.hasCurrent2 = true; + self.current2 = self.enumerator2.Current; + goto SUCCESS; + } + else + { + self.running2 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running2 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running2 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter2.SourceOnCompleted(Completed2Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + bool TrySetResult() + { + if (hasCurrent1 && hasCurrent2) + { + result = resultSelector(current1, current2); + completionSource.TrySetResult(true); + return true; + } + else + { + return false; + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator1 != null) + { + await enumerator1.DisposeAsync(); + } + if (enumerator2 != null) + { + await enumerator2.DisposeAsync(); + } + } + } + } + + internal class CombineLatest : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + + readonly Func resultSelector; + + public CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, Func resultSelector) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _CombineLatest(source1, source2, source3, resultSelector, cancellationToken); + } + + class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action Completed1Delegate = Completed1; + static readonly Action Completed2Delegate = Completed2; + static readonly Action Completed3Delegate = Completed3; + const int CompleteCount = 3; + + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + + readonly Func resultSelector; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator1; + UniTask.Awaiter awaiter1; + bool hasCurrent1; + bool running1; + T1 current1; + + IUniTaskAsyncEnumerator enumerator2; + UniTask.Awaiter awaiter2; + bool hasCurrent2; + bool running2; + T2 current2; + + IUniTaskAsyncEnumerator enumerator3; + UniTask.Awaiter awaiter3; + bool hasCurrent3; + bool running3; + T3 current3; + + int completedCount; + bool syncRunning; + TResult result; + + public _CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, Func resultSelector, CancellationToken cancellationToken) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current => result; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + if (completedCount == CompleteCount) return CompletedTasks.False; + + if (enumerator1 == null) + { + enumerator1 = source1.GetAsyncEnumerator(cancellationToken); + enumerator2 = source2.GetAsyncEnumerator(cancellationToken); + enumerator3 = source3.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + + AGAIN: + syncRunning = true; + if (!running1) + { + running1 = true; + awaiter1 = enumerator1.MoveNextAsync().GetAwaiter(); + if (awaiter1.IsCompleted) + { + Completed1(this); + } + else + { + awaiter1.SourceOnCompleted(Completed1Delegate, this); + } + } + if (!running2) + { + running2 = true; + awaiter2 = enumerator2.MoveNextAsync().GetAwaiter(); + if (awaiter2.IsCompleted) + { + Completed2(this); + } + else + { + awaiter2.SourceOnCompleted(Completed2Delegate, this); + } + } + if (!running3) + { + running3 = true; + awaiter3 = enumerator3.MoveNextAsync().GetAwaiter(); + if (awaiter3.IsCompleted) + { + Completed3(this); + } + else + { + awaiter3.SourceOnCompleted(Completed3Delegate, this); + } + } + + if (!running1 || !running2 || !running3) + { + goto AGAIN; + } + syncRunning = false; + + return new UniTask(this, completionSource.Version); + } + + static void Completed1(object state) + { + var self = (_CombineLatest)state; + self.running1 = false; + + try + { + if (self.awaiter1.GetResult()) + { + self.hasCurrent1 = true; + self.current1 = self.enumerator1.Current; + goto SUCCESS; + } + else + { + self.running1 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running1 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running1 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter1.SourceOnCompleted(Completed1Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed2(object state) + { + var self = (_CombineLatest)state; + self.running2 = false; + + try + { + if (self.awaiter2.GetResult()) + { + self.hasCurrent2 = true; + self.current2 = self.enumerator2.Current; + goto SUCCESS; + } + else + { + self.running2 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running2 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running2 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter2.SourceOnCompleted(Completed2Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed3(object state) + { + var self = (_CombineLatest)state; + self.running3 = false; + + try + { + if (self.awaiter3.GetResult()) + { + self.hasCurrent3 = true; + self.current3 = self.enumerator3.Current; + goto SUCCESS; + } + else + { + self.running3 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running3 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running3 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter3.SourceOnCompleted(Completed3Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + bool TrySetResult() + { + if (hasCurrent1 && hasCurrent2 && hasCurrent3) + { + result = resultSelector(current1, current2, current3); + completionSource.TrySetResult(true); + return true; + } + else + { + return false; + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator1 != null) + { + await enumerator1.DisposeAsync(); + } + if (enumerator2 != null) + { + await enumerator2.DisposeAsync(); + } + if (enumerator3 != null) + { + await enumerator3.DisposeAsync(); + } + } + } + } + + internal class CombineLatest : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + + readonly Func resultSelector; + + public CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, Func resultSelector) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _CombineLatest(source1, source2, source3, source4, resultSelector, cancellationToken); + } + + class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action Completed1Delegate = Completed1; + static readonly Action Completed2Delegate = Completed2; + static readonly Action Completed3Delegate = Completed3; + static readonly Action Completed4Delegate = Completed4; + const int CompleteCount = 4; + + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + + readonly Func resultSelector; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator1; + UniTask.Awaiter awaiter1; + bool hasCurrent1; + bool running1; + T1 current1; + + IUniTaskAsyncEnumerator enumerator2; + UniTask.Awaiter awaiter2; + bool hasCurrent2; + bool running2; + T2 current2; + + IUniTaskAsyncEnumerator enumerator3; + UniTask.Awaiter awaiter3; + bool hasCurrent3; + bool running3; + T3 current3; + + IUniTaskAsyncEnumerator enumerator4; + UniTask.Awaiter awaiter4; + bool hasCurrent4; + bool running4; + T4 current4; + + int completedCount; + bool syncRunning; + TResult result; + + public _CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, Func resultSelector, CancellationToken cancellationToken) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current => result; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + if (completedCount == CompleteCount) return CompletedTasks.False; + + if (enumerator1 == null) + { + enumerator1 = source1.GetAsyncEnumerator(cancellationToken); + enumerator2 = source2.GetAsyncEnumerator(cancellationToken); + enumerator3 = source3.GetAsyncEnumerator(cancellationToken); + enumerator4 = source4.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + + AGAIN: + syncRunning = true; + if (!running1) + { + running1 = true; + awaiter1 = enumerator1.MoveNextAsync().GetAwaiter(); + if (awaiter1.IsCompleted) + { + Completed1(this); + } + else + { + awaiter1.SourceOnCompleted(Completed1Delegate, this); + } + } + if (!running2) + { + running2 = true; + awaiter2 = enumerator2.MoveNextAsync().GetAwaiter(); + if (awaiter2.IsCompleted) + { + Completed2(this); + } + else + { + awaiter2.SourceOnCompleted(Completed2Delegate, this); + } + } + if (!running3) + { + running3 = true; + awaiter3 = enumerator3.MoveNextAsync().GetAwaiter(); + if (awaiter3.IsCompleted) + { + Completed3(this); + } + else + { + awaiter3.SourceOnCompleted(Completed3Delegate, this); + } + } + if (!running4) + { + running4 = true; + awaiter4 = enumerator4.MoveNextAsync().GetAwaiter(); + if (awaiter4.IsCompleted) + { + Completed4(this); + } + else + { + awaiter4.SourceOnCompleted(Completed4Delegate, this); + } + } + + if (!running1 || !running2 || !running3 || !running4) + { + goto AGAIN; + } + syncRunning = false; + + return new UniTask(this, completionSource.Version); + } + + static void Completed1(object state) + { + var self = (_CombineLatest)state; + self.running1 = false; + + try + { + if (self.awaiter1.GetResult()) + { + self.hasCurrent1 = true; + self.current1 = self.enumerator1.Current; + goto SUCCESS; + } + else + { + self.running1 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running1 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running1 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter1.SourceOnCompleted(Completed1Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed2(object state) + { + var self = (_CombineLatest)state; + self.running2 = false; + + try + { + if (self.awaiter2.GetResult()) + { + self.hasCurrent2 = true; + self.current2 = self.enumerator2.Current; + goto SUCCESS; + } + else + { + self.running2 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running2 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running2 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter2.SourceOnCompleted(Completed2Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed3(object state) + { + var self = (_CombineLatest)state; + self.running3 = false; + + try + { + if (self.awaiter3.GetResult()) + { + self.hasCurrent3 = true; + self.current3 = self.enumerator3.Current; + goto SUCCESS; + } + else + { + self.running3 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running3 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running3 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter3.SourceOnCompleted(Completed3Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed4(object state) + { + var self = (_CombineLatest)state; + self.running4 = false; + + try + { + if (self.awaiter4.GetResult()) + { + self.hasCurrent4 = true; + self.current4 = self.enumerator4.Current; + goto SUCCESS; + } + else + { + self.running4 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running4 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running4 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter4.SourceOnCompleted(Completed4Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + bool TrySetResult() + { + if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4) + { + result = resultSelector(current1, current2, current3, current4); + completionSource.TrySetResult(true); + return true; + } + else + { + return false; + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator1 != null) + { + await enumerator1.DisposeAsync(); + } + if (enumerator2 != null) + { + await enumerator2.DisposeAsync(); + } + if (enumerator3 != null) + { + await enumerator3.DisposeAsync(); + } + if (enumerator4 != null) + { + await enumerator4.DisposeAsync(); + } + } + } + } + + internal class CombineLatest : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + + readonly Func resultSelector; + + public CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, Func resultSelector) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _CombineLatest(source1, source2, source3, source4, source5, resultSelector, cancellationToken); + } + + class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action Completed1Delegate = Completed1; + static readonly Action Completed2Delegate = Completed2; + static readonly Action Completed3Delegate = Completed3; + static readonly Action Completed4Delegate = Completed4; + static readonly Action Completed5Delegate = Completed5; + const int CompleteCount = 5; + + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + + readonly Func resultSelector; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator1; + UniTask.Awaiter awaiter1; + bool hasCurrent1; + bool running1; + T1 current1; + + IUniTaskAsyncEnumerator enumerator2; + UniTask.Awaiter awaiter2; + bool hasCurrent2; + bool running2; + T2 current2; + + IUniTaskAsyncEnumerator enumerator3; + UniTask.Awaiter awaiter3; + bool hasCurrent3; + bool running3; + T3 current3; + + IUniTaskAsyncEnumerator enumerator4; + UniTask.Awaiter awaiter4; + bool hasCurrent4; + bool running4; + T4 current4; + + IUniTaskAsyncEnumerator enumerator5; + UniTask.Awaiter awaiter5; + bool hasCurrent5; + bool running5; + T5 current5; + + int completedCount; + bool syncRunning; + TResult result; + + public _CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, Func resultSelector, CancellationToken cancellationToken) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current => result; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + if (completedCount == CompleteCount) return CompletedTasks.False; + + if (enumerator1 == null) + { + enumerator1 = source1.GetAsyncEnumerator(cancellationToken); + enumerator2 = source2.GetAsyncEnumerator(cancellationToken); + enumerator3 = source3.GetAsyncEnumerator(cancellationToken); + enumerator4 = source4.GetAsyncEnumerator(cancellationToken); + enumerator5 = source5.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + + AGAIN: + syncRunning = true; + if (!running1) + { + running1 = true; + awaiter1 = enumerator1.MoveNextAsync().GetAwaiter(); + if (awaiter1.IsCompleted) + { + Completed1(this); + } + else + { + awaiter1.SourceOnCompleted(Completed1Delegate, this); + } + } + if (!running2) + { + running2 = true; + awaiter2 = enumerator2.MoveNextAsync().GetAwaiter(); + if (awaiter2.IsCompleted) + { + Completed2(this); + } + else + { + awaiter2.SourceOnCompleted(Completed2Delegate, this); + } + } + if (!running3) + { + running3 = true; + awaiter3 = enumerator3.MoveNextAsync().GetAwaiter(); + if (awaiter3.IsCompleted) + { + Completed3(this); + } + else + { + awaiter3.SourceOnCompleted(Completed3Delegate, this); + } + } + if (!running4) + { + running4 = true; + awaiter4 = enumerator4.MoveNextAsync().GetAwaiter(); + if (awaiter4.IsCompleted) + { + Completed4(this); + } + else + { + awaiter4.SourceOnCompleted(Completed4Delegate, this); + } + } + if (!running5) + { + running5 = true; + awaiter5 = enumerator5.MoveNextAsync().GetAwaiter(); + if (awaiter5.IsCompleted) + { + Completed5(this); + } + else + { + awaiter5.SourceOnCompleted(Completed5Delegate, this); + } + } + + if (!running1 || !running2 || !running3 || !running4 || !running5) + { + goto AGAIN; + } + syncRunning = false; + + return new UniTask(this, completionSource.Version); + } + + static void Completed1(object state) + { + var self = (_CombineLatest)state; + self.running1 = false; + + try + { + if (self.awaiter1.GetResult()) + { + self.hasCurrent1 = true; + self.current1 = self.enumerator1.Current; + goto SUCCESS; + } + else + { + self.running1 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running1 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running1 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter1.SourceOnCompleted(Completed1Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed2(object state) + { + var self = (_CombineLatest)state; + self.running2 = false; + + try + { + if (self.awaiter2.GetResult()) + { + self.hasCurrent2 = true; + self.current2 = self.enumerator2.Current; + goto SUCCESS; + } + else + { + self.running2 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running2 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running2 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter2.SourceOnCompleted(Completed2Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed3(object state) + { + var self = (_CombineLatest)state; + self.running3 = false; + + try + { + if (self.awaiter3.GetResult()) + { + self.hasCurrent3 = true; + self.current3 = self.enumerator3.Current; + goto SUCCESS; + } + else + { + self.running3 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running3 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running3 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter3.SourceOnCompleted(Completed3Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed4(object state) + { + var self = (_CombineLatest)state; + self.running4 = false; + + try + { + if (self.awaiter4.GetResult()) + { + self.hasCurrent4 = true; + self.current4 = self.enumerator4.Current; + goto SUCCESS; + } + else + { + self.running4 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running4 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running4 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter4.SourceOnCompleted(Completed4Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed5(object state) + { + var self = (_CombineLatest)state; + self.running5 = false; + + try + { + if (self.awaiter5.GetResult()) + { + self.hasCurrent5 = true; + self.current5 = self.enumerator5.Current; + goto SUCCESS; + } + else + { + self.running5 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running5 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running5 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter5.SourceOnCompleted(Completed5Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + bool TrySetResult() + { + if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5) + { + result = resultSelector(current1, current2, current3, current4, current5); + completionSource.TrySetResult(true); + return true; + } + else + { + return false; + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator1 != null) + { + await enumerator1.DisposeAsync(); + } + if (enumerator2 != null) + { + await enumerator2.DisposeAsync(); + } + if (enumerator3 != null) + { + await enumerator3.DisposeAsync(); + } + if (enumerator4 != null) + { + await enumerator4.DisposeAsync(); + } + if (enumerator5 != null) + { + await enumerator5.DisposeAsync(); + } + } + } + } + + internal class CombineLatest : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + + readonly Func resultSelector; + + public CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, Func resultSelector) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _CombineLatest(source1, source2, source3, source4, source5, source6, resultSelector, cancellationToken); + } + + class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action Completed1Delegate = Completed1; + static readonly Action Completed2Delegate = Completed2; + static readonly Action Completed3Delegate = Completed3; + static readonly Action Completed4Delegate = Completed4; + static readonly Action Completed5Delegate = Completed5; + static readonly Action Completed6Delegate = Completed6; + const int CompleteCount = 6; + + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + + readonly Func resultSelector; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator1; + UniTask.Awaiter awaiter1; + bool hasCurrent1; + bool running1; + T1 current1; + + IUniTaskAsyncEnumerator enumerator2; + UniTask.Awaiter awaiter2; + bool hasCurrent2; + bool running2; + T2 current2; + + IUniTaskAsyncEnumerator enumerator3; + UniTask.Awaiter awaiter3; + bool hasCurrent3; + bool running3; + T3 current3; + + IUniTaskAsyncEnumerator enumerator4; + UniTask.Awaiter awaiter4; + bool hasCurrent4; + bool running4; + T4 current4; + + IUniTaskAsyncEnumerator enumerator5; + UniTask.Awaiter awaiter5; + bool hasCurrent5; + bool running5; + T5 current5; + + IUniTaskAsyncEnumerator enumerator6; + UniTask.Awaiter awaiter6; + bool hasCurrent6; + bool running6; + T6 current6; + + int completedCount; + bool syncRunning; + TResult result; + + public _CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, Func resultSelector, CancellationToken cancellationToken) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current => result; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + if (completedCount == CompleteCount) return CompletedTasks.False; + + if (enumerator1 == null) + { + enumerator1 = source1.GetAsyncEnumerator(cancellationToken); + enumerator2 = source2.GetAsyncEnumerator(cancellationToken); + enumerator3 = source3.GetAsyncEnumerator(cancellationToken); + enumerator4 = source4.GetAsyncEnumerator(cancellationToken); + enumerator5 = source5.GetAsyncEnumerator(cancellationToken); + enumerator6 = source6.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + + AGAIN: + syncRunning = true; + if (!running1) + { + running1 = true; + awaiter1 = enumerator1.MoveNextAsync().GetAwaiter(); + if (awaiter1.IsCompleted) + { + Completed1(this); + } + else + { + awaiter1.SourceOnCompleted(Completed1Delegate, this); + } + } + if (!running2) + { + running2 = true; + awaiter2 = enumerator2.MoveNextAsync().GetAwaiter(); + if (awaiter2.IsCompleted) + { + Completed2(this); + } + else + { + awaiter2.SourceOnCompleted(Completed2Delegate, this); + } + } + if (!running3) + { + running3 = true; + awaiter3 = enumerator3.MoveNextAsync().GetAwaiter(); + if (awaiter3.IsCompleted) + { + Completed3(this); + } + else + { + awaiter3.SourceOnCompleted(Completed3Delegate, this); + } + } + if (!running4) + { + running4 = true; + awaiter4 = enumerator4.MoveNextAsync().GetAwaiter(); + if (awaiter4.IsCompleted) + { + Completed4(this); + } + else + { + awaiter4.SourceOnCompleted(Completed4Delegate, this); + } + } + if (!running5) + { + running5 = true; + awaiter5 = enumerator5.MoveNextAsync().GetAwaiter(); + if (awaiter5.IsCompleted) + { + Completed5(this); + } + else + { + awaiter5.SourceOnCompleted(Completed5Delegate, this); + } + } + if (!running6) + { + running6 = true; + awaiter6 = enumerator6.MoveNextAsync().GetAwaiter(); + if (awaiter6.IsCompleted) + { + Completed6(this); + } + else + { + awaiter6.SourceOnCompleted(Completed6Delegate, this); + } + } + + if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6) + { + goto AGAIN; + } + syncRunning = false; + + return new UniTask(this, completionSource.Version); + } + + static void Completed1(object state) + { + var self = (_CombineLatest)state; + self.running1 = false; + + try + { + if (self.awaiter1.GetResult()) + { + self.hasCurrent1 = true; + self.current1 = self.enumerator1.Current; + goto SUCCESS; + } + else + { + self.running1 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running1 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running1 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter1.SourceOnCompleted(Completed1Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed2(object state) + { + var self = (_CombineLatest)state; + self.running2 = false; + + try + { + if (self.awaiter2.GetResult()) + { + self.hasCurrent2 = true; + self.current2 = self.enumerator2.Current; + goto SUCCESS; + } + else + { + self.running2 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running2 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running2 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter2.SourceOnCompleted(Completed2Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed3(object state) + { + var self = (_CombineLatest)state; + self.running3 = false; + + try + { + if (self.awaiter3.GetResult()) + { + self.hasCurrent3 = true; + self.current3 = self.enumerator3.Current; + goto SUCCESS; + } + else + { + self.running3 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running3 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running3 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter3.SourceOnCompleted(Completed3Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed4(object state) + { + var self = (_CombineLatest)state; + self.running4 = false; + + try + { + if (self.awaiter4.GetResult()) + { + self.hasCurrent4 = true; + self.current4 = self.enumerator4.Current; + goto SUCCESS; + } + else + { + self.running4 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running4 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running4 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter4.SourceOnCompleted(Completed4Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed5(object state) + { + var self = (_CombineLatest)state; + self.running5 = false; + + try + { + if (self.awaiter5.GetResult()) + { + self.hasCurrent5 = true; + self.current5 = self.enumerator5.Current; + goto SUCCESS; + } + else + { + self.running5 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running5 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running5 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter5.SourceOnCompleted(Completed5Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed6(object state) + { + var self = (_CombineLatest)state; + self.running6 = false; + + try + { + if (self.awaiter6.GetResult()) + { + self.hasCurrent6 = true; + self.current6 = self.enumerator6.Current; + goto SUCCESS; + } + else + { + self.running6 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running6 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running6 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter6.SourceOnCompleted(Completed6Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + bool TrySetResult() + { + if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6) + { + result = resultSelector(current1, current2, current3, current4, current5, current6); + completionSource.TrySetResult(true); + return true; + } + else + { + return false; + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator1 != null) + { + await enumerator1.DisposeAsync(); + } + if (enumerator2 != null) + { + await enumerator2.DisposeAsync(); + } + if (enumerator3 != null) + { + await enumerator3.DisposeAsync(); + } + if (enumerator4 != null) + { + await enumerator4.DisposeAsync(); + } + if (enumerator5 != null) + { + await enumerator5.DisposeAsync(); + } + if (enumerator6 != null) + { + await enumerator6.DisposeAsync(); + } + } + } + } + + internal class CombineLatest : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + + readonly Func resultSelector; + + public CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, Func resultSelector) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, resultSelector, cancellationToken); + } + + class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action Completed1Delegate = Completed1; + static readonly Action Completed2Delegate = Completed2; + static readonly Action Completed3Delegate = Completed3; + static readonly Action Completed4Delegate = Completed4; + static readonly Action Completed5Delegate = Completed5; + static readonly Action Completed6Delegate = Completed6; + static readonly Action Completed7Delegate = Completed7; + const int CompleteCount = 7; + + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + + readonly Func resultSelector; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator1; + UniTask.Awaiter awaiter1; + bool hasCurrent1; + bool running1; + T1 current1; + + IUniTaskAsyncEnumerator enumerator2; + UniTask.Awaiter awaiter2; + bool hasCurrent2; + bool running2; + T2 current2; + + IUniTaskAsyncEnumerator enumerator3; + UniTask.Awaiter awaiter3; + bool hasCurrent3; + bool running3; + T3 current3; + + IUniTaskAsyncEnumerator enumerator4; + UniTask.Awaiter awaiter4; + bool hasCurrent4; + bool running4; + T4 current4; + + IUniTaskAsyncEnumerator enumerator5; + UniTask.Awaiter awaiter5; + bool hasCurrent5; + bool running5; + T5 current5; + + IUniTaskAsyncEnumerator enumerator6; + UniTask.Awaiter awaiter6; + bool hasCurrent6; + bool running6; + T6 current6; + + IUniTaskAsyncEnumerator enumerator7; + UniTask.Awaiter awaiter7; + bool hasCurrent7; + bool running7; + T7 current7; + + int completedCount; + bool syncRunning; + TResult result; + + public _CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, Func resultSelector, CancellationToken cancellationToken) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current => result; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + if (completedCount == CompleteCount) return CompletedTasks.False; + + if (enumerator1 == null) + { + enumerator1 = source1.GetAsyncEnumerator(cancellationToken); + enumerator2 = source2.GetAsyncEnumerator(cancellationToken); + enumerator3 = source3.GetAsyncEnumerator(cancellationToken); + enumerator4 = source4.GetAsyncEnumerator(cancellationToken); + enumerator5 = source5.GetAsyncEnumerator(cancellationToken); + enumerator6 = source6.GetAsyncEnumerator(cancellationToken); + enumerator7 = source7.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + + AGAIN: + syncRunning = true; + if (!running1) + { + running1 = true; + awaiter1 = enumerator1.MoveNextAsync().GetAwaiter(); + if (awaiter1.IsCompleted) + { + Completed1(this); + } + else + { + awaiter1.SourceOnCompleted(Completed1Delegate, this); + } + } + if (!running2) + { + running2 = true; + awaiter2 = enumerator2.MoveNextAsync().GetAwaiter(); + if (awaiter2.IsCompleted) + { + Completed2(this); + } + else + { + awaiter2.SourceOnCompleted(Completed2Delegate, this); + } + } + if (!running3) + { + running3 = true; + awaiter3 = enumerator3.MoveNextAsync().GetAwaiter(); + if (awaiter3.IsCompleted) + { + Completed3(this); + } + else + { + awaiter3.SourceOnCompleted(Completed3Delegate, this); + } + } + if (!running4) + { + running4 = true; + awaiter4 = enumerator4.MoveNextAsync().GetAwaiter(); + if (awaiter4.IsCompleted) + { + Completed4(this); + } + else + { + awaiter4.SourceOnCompleted(Completed4Delegate, this); + } + } + if (!running5) + { + running5 = true; + awaiter5 = enumerator5.MoveNextAsync().GetAwaiter(); + if (awaiter5.IsCompleted) + { + Completed5(this); + } + else + { + awaiter5.SourceOnCompleted(Completed5Delegate, this); + } + } + if (!running6) + { + running6 = true; + awaiter6 = enumerator6.MoveNextAsync().GetAwaiter(); + if (awaiter6.IsCompleted) + { + Completed6(this); + } + else + { + awaiter6.SourceOnCompleted(Completed6Delegate, this); + } + } + if (!running7) + { + running7 = true; + awaiter7 = enumerator7.MoveNextAsync().GetAwaiter(); + if (awaiter7.IsCompleted) + { + Completed7(this); + } + else + { + awaiter7.SourceOnCompleted(Completed7Delegate, this); + } + } + + if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7) + { + goto AGAIN; + } + syncRunning = false; + + return new UniTask(this, completionSource.Version); + } + + static void Completed1(object state) + { + var self = (_CombineLatest)state; + self.running1 = false; + + try + { + if (self.awaiter1.GetResult()) + { + self.hasCurrent1 = true; + self.current1 = self.enumerator1.Current; + goto SUCCESS; + } + else + { + self.running1 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running1 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running1 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter1.SourceOnCompleted(Completed1Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed2(object state) + { + var self = (_CombineLatest)state; + self.running2 = false; + + try + { + if (self.awaiter2.GetResult()) + { + self.hasCurrent2 = true; + self.current2 = self.enumerator2.Current; + goto SUCCESS; + } + else + { + self.running2 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running2 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running2 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter2.SourceOnCompleted(Completed2Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed3(object state) + { + var self = (_CombineLatest)state; + self.running3 = false; + + try + { + if (self.awaiter3.GetResult()) + { + self.hasCurrent3 = true; + self.current3 = self.enumerator3.Current; + goto SUCCESS; + } + else + { + self.running3 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running3 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running3 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter3.SourceOnCompleted(Completed3Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed4(object state) + { + var self = (_CombineLatest)state; + self.running4 = false; + + try + { + if (self.awaiter4.GetResult()) + { + self.hasCurrent4 = true; + self.current4 = self.enumerator4.Current; + goto SUCCESS; + } + else + { + self.running4 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running4 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running4 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter4.SourceOnCompleted(Completed4Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed5(object state) + { + var self = (_CombineLatest)state; + self.running5 = false; + + try + { + if (self.awaiter5.GetResult()) + { + self.hasCurrent5 = true; + self.current5 = self.enumerator5.Current; + goto SUCCESS; + } + else + { + self.running5 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running5 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running5 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter5.SourceOnCompleted(Completed5Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed6(object state) + { + var self = (_CombineLatest)state; + self.running6 = false; + + try + { + if (self.awaiter6.GetResult()) + { + self.hasCurrent6 = true; + self.current6 = self.enumerator6.Current; + goto SUCCESS; + } + else + { + self.running6 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running6 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running6 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter6.SourceOnCompleted(Completed6Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed7(object state) + { + var self = (_CombineLatest)state; + self.running7 = false; + + try + { + if (self.awaiter7.GetResult()) + { + self.hasCurrent7 = true; + self.current7 = self.enumerator7.Current; + goto SUCCESS; + } + else + { + self.running7 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running7 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running7 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter7.SourceOnCompleted(Completed7Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + bool TrySetResult() + { + if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7) + { + result = resultSelector(current1, current2, current3, current4, current5, current6, current7); + completionSource.TrySetResult(true); + return true; + } + else + { + return false; + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator1 != null) + { + await enumerator1.DisposeAsync(); + } + if (enumerator2 != null) + { + await enumerator2.DisposeAsync(); + } + if (enumerator3 != null) + { + await enumerator3.DisposeAsync(); + } + if (enumerator4 != null) + { + await enumerator4.DisposeAsync(); + } + if (enumerator5 != null) + { + await enumerator5.DisposeAsync(); + } + if (enumerator6 != null) + { + await enumerator6.DisposeAsync(); + } + if (enumerator7 != null) + { + await enumerator7.DisposeAsync(); + } + } + } + } + + internal class CombineLatest : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + readonly IUniTaskAsyncEnumerable source8; + + readonly Func resultSelector; + + public CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, Func resultSelector) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + this.source8 = source8; + + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, resultSelector, cancellationToken); + } + + class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action Completed1Delegate = Completed1; + static readonly Action Completed2Delegate = Completed2; + static readonly Action Completed3Delegate = Completed3; + static readonly Action Completed4Delegate = Completed4; + static readonly Action Completed5Delegate = Completed5; + static readonly Action Completed6Delegate = Completed6; + static readonly Action Completed7Delegate = Completed7; + static readonly Action Completed8Delegate = Completed8; + const int CompleteCount = 8; + + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + readonly IUniTaskAsyncEnumerable source8; + + readonly Func resultSelector; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator1; + UniTask.Awaiter awaiter1; + bool hasCurrent1; + bool running1; + T1 current1; + + IUniTaskAsyncEnumerator enumerator2; + UniTask.Awaiter awaiter2; + bool hasCurrent2; + bool running2; + T2 current2; + + IUniTaskAsyncEnumerator enumerator3; + UniTask.Awaiter awaiter3; + bool hasCurrent3; + bool running3; + T3 current3; + + IUniTaskAsyncEnumerator enumerator4; + UniTask.Awaiter awaiter4; + bool hasCurrent4; + bool running4; + T4 current4; + + IUniTaskAsyncEnumerator enumerator5; + UniTask.Awaiter awaiter5; + bool hasCurrent5; + bool running5; + T5 current5; + + IUniTaskAsyncEnumerator enumerator6; + UniTask.Awaiter awaiter6; + bool hasCurrent6; + bool running6; + T6 current6; + + IUniTaskAsyncEnumerator enumerator7; + UniTask.Awaiter awaiter7; + bool hasCurrent7; + bool running7; + T7 current7; + + IUniTaskAsyncEnumerator enumerator8; + UniTask.Awaiter awaiter8; + bool hasCurrent8; + bool running8; + T8 current8; + + int completedCount; + bool syncRunning; + TResult result; + + public _CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, Func resultSelector, CancellationToken cancellationToken) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + this.source8 = source8; + + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current => result; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + if (completedCount == CompleteCount) return CompletedTasks.False; + + if (enumerator1 == null) + { + enumerator1 = source1.GetAsyncEnumerator(cancellationToken); + enumerator2 = source2.GetAsyncEnumerator(cancellationToken); + enumerator3 = source3.GetAsyncEnumerator(cancellationToken); + enumerator4 = source4.GetAsyncEnumerator(cancellationToken); + enumerator5 = source5.GetAsyncEnumerator(cancellationToken); + enumerator6 = source6.GetAsyncEnumerator(cancellationToken); + enumerator7 = source7.GetAsyncEnumerator(cancellationToken); + enumerator8 = source8.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + + AGAIN: + syncRunning = true; + if (!running1) + { + running1 = true; + awaiter1 = enumerator1.MoveNextAsync().GetAwaiter(); + if (awaiter1.IsCompleted) + { + Completed1(this); + } + else + { + awaiter1.SourceOnCompleted(Completed1Delegate, this); + } + } + if (!running2) + { + running2 = true; + awaiter2 = enumerator2.MoveNextAsync().GetAwaiter(); + if (awaiter2.IsCompleted) + { + Completed2(this); + } + else + { + awaiter2.SourceOnCompleted(Completed2Delegate, this); + } + } + if (!running3) + { + running3 = true; + awaiter3 = enumerator3.MoveNextAsync().GetAwaiter(); + if (awaiter3.IsCompleted) + { + Completed3(this); + } + else + { + awaiter3.SourceOnCompleted(Completed3Delegate, this); + } + } + if (!running4) + { + running4 = true; + awaiter4 = enumerator4.MoveNextAsync().GetAwaiter(); + if (awaiter4.IsCompleted) + { + Completed4(this); + } + else + { + awaiter4.SourceOnCompleted(Completed4Delegate, this); + } + } + if (!running5) + { + running5 = true; + awaiter5 = enumerator5.MoveNextAsync().GetAwaiter(); + if (awaiter5.IsCompleted) + { + Completed5(this); + } + else + { + awaiter5.SourceOnCompleted(Completed5Delegate, this); + } + } + if (!running6) + { + running6 = true; + awaiter6 = enumerator6.MoveNextAsync().GetAwaiter(); + if (awaiter6.IsCompleted) + { + Completed6(this); + } + else + { + awaiter6.SourceOnCompleted(Completed6Delegate, this); + } + } + if (!running7) + { + running7 = true; + awaiter7 = enumerator7.MoveNextAsync().GetAwaiter(); + if (awaiter7.IsCompleted) + { + Completed7(this); + } + else + { + awaiter7.SourceOnCompleted(Completed7Delegate, this); + } + } + if (!running8) + { + running8 = true; + awaiter8 = enumerator8.MoveNextAsync().GetAwaiter(); + if (awaiter8.IsCompleted) + { + Completed8(this); + } + else + { + awaiter8.SourceOnCompleted(Completed8Delegate, this); + } + } + + if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7 || !running8) + { + goto AGAIN; + } + syncRunning = false; + + return new UniTask(this, completionSource.Version); + } + + static void Completed1(object state) + { + var self = (_CombineLatest)state; + self.running1 = false; + + try + { + if (self.awaiter1.GetResult()) + { + self.hasCurrent1 = true; + self.current1 = self.enumerator1.Current; + goto SUCCESS; + } + else + { + self.running1 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running1 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running1 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter1.SourceOnCompleted(Completed1Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed2(object state) + { + var self = (_CombineLatest)state; + self.running2 = false; + + try + { + if (self.awaiter2.GetResult()) + { + self.hasCurrent2 = true; + self.current2 = self.enumerator2.Current; + goto SUCCESS; + } + else + { + self.running2 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running2 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running2 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter2.SourceOnCompleted(Completed2Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed3(object state) + { + var self = (_CombineLatest)state; + self.running3 = false; + + try + { + if (self.awaiter3.GetResult()) + { + self.hasCurrent3 = true; + self.current3 = self.enumerator3.Current; + goto SUCCESS; + } + else + { + self.running3 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running3 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running3 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter3.SourceOnCompleted(Completed3Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed4(object state) + { + var self = (_CombineLatest)state; + self.running4 = false; + + try + { + if (self.awaiter4.GetResult()) + { + self.hasCurrent4 = true; + self.current4 = self.enumerator4.Current; + goto SUCCESS; + } + else + { + self.running4 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running4 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running4 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter4.SourceOnCompleted(Completed4Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed5(object state) + { + var self = (_CombineLatest)state; + self.running5 = false; + + try + { + if (self.awaiter5.GetResult()) + { + self.hasCurrent5 = true; + self.current5 = self.enumerator5.Current; + goto SUCCESS; + } + else + { + self.running5 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running5 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running5 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter5.SourceOnCompleted(Completed5Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed6(object state) + { + var self = (_CombineLatest)state; + self.running6 = false; + + try + { + if (self.awaiter6.GetResult()) + { + self.hasCurrent6 = true; + self.current6 = self.enumerator6.Current; + goto SUCCESS; + } + else + { + self.running6 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running6 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running6 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter6.SourceOnCompleted(Completed6Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed7(object state) + { + var self = (_CombineLatest)state; + self.running7 = false; + + try + { + if (self.awaiter7.GetResult()) + { + self.hasCurrent7 = true; + self.current7 = self.enumerator7.Current; + goto SUCCESS; + } + else + { + self.running7 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running7 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running7 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter7.SourceOnCompleted(Completed7Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed8(object state) + { + var self = (_CombineLatest)state; + self.running8 = false; + + try + { + if (self.awaiter8.GetResult()) + { + self.hasCurrent8 = true; + self.current8 = self.enumerator8.Current; + goto SUCCESS; + } + else + { + self.running8 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running8 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running8 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter8 = self.enumerator8.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter8.SourceOnCompleted(Completed8Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + bool TrySetResult() + { + if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7 && hasCurrent8) + { + result = resultSelector(current1, current2, current3, current4, current5, current6, current7, current8); + completionSource.TrySetResult(true); + return true; + } + else + { + return false; + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator1 != null) + { + await enumerator1.DisposeAsync(); + } + if (enumerator2 != null) + { + await enumerator2.DisposeAsync(); + } + if (enumerator3 != null) + { + await enumerator3.DisposeAsync(); + } + if (enumerator4 != null) + { + await enumerator4.DisposeAsync(); + } + if (enumerator5 != null) + { + await enumerator5.DisposeAsync(); + } + if (enumerator6 != null) + { + await enumerator6.DisposeAsync(); + } + if (enumerator7 != null) + { + await enumerator7.DisposeAsync(); + } + if (enumerator8 != null) + { + await enumerator8.DisposeAsync(); + } + } + } + } + + internal class CombineLatest : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + readonly IUniTaskAsyncEnumerable source8; + readonly IUniTaskAsyncEnumerable source9; + + readonly Func resultSelector; + + public CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, Func resultSelector) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + this.source8 = source8; + this.source9 = source9; + + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, resultSelector, cancellationToken); + } + + class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action Completed1Delegate = Completed1; + static readonly Action Completed2Delegate = Completed2; + static readonly Action Completed3Delegate = Completed3; + static readonly Action Completed4Delegate = Completed4; + static readonly Action Completed5Delegate = Completed5; + static readonly Action Completed6Delegate = Completed6; + static readonly Action Completed7Delegate = Completed7; + static readonly Action Completed8Delegate = Completed8; + static readonly Action Completed9Delegate = Completed9; + const int CompleteCount = 9; + + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + readonly IUniTaskAsyncEnumerable source8; + readonly IUniTaskAsyncEnumerable source9; + + readonly Func resultSelector; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator1; + UniTask.Awaiter awaiter1; + bool hasCurrent1; + bool running1; + T1 current1; + + IUniTaskAsyncEnumerator enumerator2; + UniTask.Awaiter awaiter2; + bool hasCurrent2; + bool running2; + T2 current2; + + IUniTaskAsyncEnumerator enumerator3; + UniTask.Awaiter awaiter3; + bool hasCurrent3; + bool running3; + T3 current3; + + IUniTaskAsyncEnumerator enumerator4; + UniTask.Awaiter awaiter4; + bool hasCurrent4; + bool running4; + T4 current4; + + IUniTaskAsyncEnumerator enumerator5; + UniTask.Awaiter awaiter5; + bool hasCurrent5; + bool running5; + T5 current5; + + IUniTaskAsyncEnumerator enumerator6; + UniTask.Awaiter awaiter6; + bool hasCurrent6; + bool running6; + T6 current6; + + IUniTaskAsyncEnumerator enumerator7; + UniTask.Awaiter awaiter7; + bool hasCurrent7; + bool running7; + T7 current7; + + IUniTaskAsyncEnumerator enumerator8; + UniTask.Awaiter awaiter8; + bool hasCurrent8; + bool running8; + T8 current8; + + IUniTaskAsyncEnumerator enumerator9; + UniTask.Awaiter awaiter9; + bool hasCurrent9; + bool running9; + T9 current9; + + int completedCount; + bool syncRunning; + TResult result; + + public _CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, Func resultSelector, CancellationToken cancellationToken) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + this.source8 = source8; + this.source9 = source9; + + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current => result; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + if (completedCount == CompleteCount) return CompletedTasks.False; + + if (enumerator1 == null) + { + enumerator1 = source1.GetAsyncEnumerator(cancellationToken); + enumerator2 = source2.GetAsyncEnumerator(cancellationToken); + enumerator3 = source3.GetAsyncEnumerator(cancellationToken); + enumerator4 = source4.GetAsyncEnumerator(cancellationToken); + enumerator5 = source5.GetAsyncEnumerator(cancellationToken); + enumerator6 = source6.GetAsyncEnumerator(cancellationToken); + enumerator7 = source7.GetAsyncEnumerator(cancellationToken); + enumerator8 = source8.GetAsyncEnumerator(cancellationToken); + enumerator9 = source9.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + + AGAIN: + syncRunning = true; + if (!running1) + { + running1 = true; + awaiter1 = enumerator1.MoveNextAsync().GetAwaiter(); + if (awaiter1.IsCompleted) + { + Completed1(this); + } + else + { + awaiter1.SourceOnCompleted(Completed1Delegate, this); + } + } + if (!running2) + { + running2 = true; + awaiter2 = enumerator2.MoveNextAsync().GetAwaiter(); + if (awaiter2.IsCompleted) + { + Completed2(this); + } + else + { + awaiter2.SourceOnCompleted(Completed2Delegate, this); + } + } + if (!running3) + { + running3 = true; + awaiter3 = enumerator3.MoveNextAsync().GetAwaiter(); + if (awaiter3.IsCompleted) + { + Completed3(this); + } + else + { + awaiter3.SourceOnCompleted(Completed3Delegate, this); + } + } + if (!running4) + { + running4 = true; + awaiter4 = enumerator4.MoveNextAsync().GetAwaiter(); + if (awaiter4.IsCompleted) + { + Completed4(this); + } + else + { + awaiter4.SourceOnCompleted(Completed4Delegate, this); + } + } + if (!running5) + { + running5 = true; + awaiter5 = enumerator5.MoveNextAsync().GetAwaiter(); + if (awaiter5.IsCompleted) + { + Completed5(this); + } + else + { + awaiter5.SourceOnCompleted(Completed5Delegate, this); + } + } + if (!running6) + { + running6 = true; + awaiter6 = enumerator6.MoveNextAsync().GetAwaiter(); + if (awaiter6.IsCompleted) + { + Completed6(this); + } + else + { + awaiter6.SourceOnCompleted(Completed6Delegate, this); + } + } + if (!running7) + { + running7 = true; + awaiter7 = enumerator7.MoveNextAsync().GetAwaiter(); + if (awaiter7.IsCompleted) + { + Completed7(this); + } + else + { + awaiter7.SourceOnCompleted(Completed7Delegate, this); + } + } + if (!running8) + { + running8 = true; + awaiter8 = enumerator8.MoveNextAsync().GetAwaiter(); + if (awaiter8.IsCompleted) + { + Completed8(this); + } + else + { + awaiter8.SourceOnCompleted(Completed8Delegate, this); + } + } + if (!running9) + { + running9 = true; + awaiter9 = enumerator9.MoveNextAsync().GetAwaiter(); + if (awaiter9.IsCompleted) + { + Completed9(this); + } + else + { + awaiter9.SourceOnCompleted(Completed9Delegate, this); + } + } + + if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7 || !running8 || !running9) + { + goto AGAIN; + } + syncRunning = false; + + return new UniTask(this, completionSource.Version); + } + + static void Completed1(object state) + { + var self = (_CombineLatest)state; + self.running1 = false; + + try + { + if (self.awaiter1.GetResult()) + { + self.hasCurrent1 = true; + self.current1 = self.enumerator1.Current; + goto SUCCESS; + } + else + { + self.running1 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running1 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running1 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter1.SourceOnCompleted(Completed1Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed2(object state) + { + var self = (_CombineLatest)state; + self.running2 = false; + + try + { + if (self.awaiter2.GetResult()) + { + self.hasCurrent2 = true; + self.current2 = self.enumerator2.Current; + goto SUCCESS; + } + else + { + self.running2 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running2 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running2 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter2.SourceOnCompleted(Completed2Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed3(object state) + { + var self = (_CombineLatest)state; + self.running3 = false; + + try + { + if (self.awaiter3.GetResult()) + { + self.hasCurrent3 = true; + self.current3 = self.enumerator3.Current; + goto SUCCESS; + } + else + { + self.running3 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running3 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running3 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter3.SourceOnCompleted(Completed3Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed4(object state) + { + var self = (_CombineLatest)state; + self.running4 = false; + + try + { + if (self.awaiter4.GetResult()) + { + self.hasCurrent4 = true; + self.current4 = self.enumerator4.Current; + goto SUCCESS; + } + else + { + self.running4 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running4 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running4 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter4.SourceOnCompleted(Completed4Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed5(object state) + { + var self = (_CombineLatest)state; + self.running5 = false; + + try + { + if (self.awaiter5.GetResult()) + { + self.hasCurrent5 = true; + self.current5 = self.enumerator5.Current; + goto SUCCESS; + } + else + { + self.running5 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running5 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running5 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter5.SourceOnCompleted(Completed5Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed6(object state) + { + var self = (_CombineLatest)state; + self.running6 = false; + + try + { + if (self.awaiter6.GetResult()) + { + self.hasCurrent6 = true; + self.current6 = self.enumerator6.Current; + goto SUCCESS; + } + else + { + self.running6 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running6 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running6 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter6.SourceOnCompleted(Completed6Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed7(object state) + { + var self = (_CombineLatest)state; + self.running7 = false; + + try + { + if (self.awaiter7.GetResult()) + { + self.hasCurrent7 = true; + self.current7 = self.enumerator7.Current; + goto SUCCESS; + } + else + { + self.running7 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running7 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running7 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter7.SourceOnCompleted(Completed7Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed8(object state) + { + var self = (_CombineLatest)state; + self.running8 = false; + + try + { + if (self.awaiter8.GetResult()) + { + self.hasCurrent8 = true; + self.current8 = self.enumerator8.Current; + goto SUCCESS; + } + else + { + self.running8 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running8 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running8 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter8 = self.enumerator8.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter8.SourceOnCompleted(Completed8Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed9(object state) + { + var self = (_CombineLatest)state; + self.running9 = false; + + try + { + if (self.awaiter9.GetResult()) + { + self.hasCurrent9 = true; + self.current9 = self.enumerator9.Current; + goto SUCCESS; + } + else + { + self.running9 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running9 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running9 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter9 = self.enumerator9.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter9.SourceOnCompleted(Completed9Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + bool TrySetResult() + { + if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7 && hasCurrent8 && hasCurrent9) + { + result = resultSelector(current1, current2, current3, current4, current5, current6, current7, current8, current9); + completionSource.TrySetResult(true); + return true; + } + else + { + return false; + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator1 != null) + { + await enumerator1.DisposeAsync(); + } + if (enumerator2 != null) + { + await enumerator2.DisposeAsync(); + } + if (enumerator3 != null) + { + await enumerator3.DisposeAsync(); + } + if (enumerator4 != null) + { + await enumerator4.DisposeAsync(); + } + if (enumerator5 != null) + { + await enumerator5.DisposeAsync(); + } + if (enumerator6 != null) + { + await enumerator6.DisposeAsync(); + } + if (enumerator7 != null) + { + await enumerator7.DisposeAsync(); + } + if (enumerator8 != null) + { + await enumerator8.DisposeAsync(); + } + if (enumerator9 != null) + { + await enumerator9.DisposeAsync(); + } + } + } + } + + internal class CombineLatest : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + readonly IUniTaskAsyncEnumerable source8; + readonly IUniTaskAsyncEnumerable source9; + readonly IUniTaskAsyncEnumerable source10; + + readonly Func resultSelector; + + public CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, Func resultSelector) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + this.source8 = source8; + this.source9 = source9; + this.source10 = source10; + + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, resultSelector, cancellationToken); + } + + class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action Completed1Delegate = Completed1; + static readonly Action Completed2Delegate = Completed2; + static readonly Action Completed3Delegate = Completed3; + static readonly Action Completed4Delegate = Completed4; + static readonly Action Completed5Delegate = Completed5; + static readonly Action Completed6Delegate = Completed6; + static readonly Action Completed7Delegate = Completed7; + static readonly Action Completed8Delegate = Completed8; + static readonly Action Completed9Delegate = Completed9; + static readonly Action Completed10Delegate = Completed10; + const int CompleteCount = 10; + + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + readonly IUniTaskAsyncEnumerable source8; + readonly IUniTaskAsyncEnumerable source9; + readonly IUniTaskAsyncEnumerable source10; + + readonly Func resultSelector; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator1; + UniTask.Awaiter awaiter1; + bool hasCurrent1; + bool running1; + T1 current1; + + IUniTaskAsyncEnumerator enumerator2; + UniTask.Awaiter awaiter2; + bool hasCurrent2; + bool running2; + T2 current2; + + IUniTaskAsyncEnumerator enumerator3; + UniTask.Awaiter awaiter3; + bool hasCurrent3; + bool running3; + T3 current3; + + IUniTaskAsyncEnumerator enumerator4; + UniTask.Awaiter awaiter4; + bool hasCurrent4; + bool running4; + T4 current4; + + IUniTaskAsyncEnumerator enumerator5; + UniTask.Awaiter awaiter5; + bool hasCurrent5; + bool running5; + T5 current5; + + IUniTaskAsyncEnumerator enumerator6; + UniTask.Awaiter awaiter6; + bool hasCurrent6; + bool running6; + T6 current6; + + IUniTaskAsyncEnumerator enumerator7; + UniTask.Awaiter awaiter7; + bool hasCurrent7; + bool running7; + T7 current7; + + IUniTaskAsyncEnumerator enumerator8; + UniTask.Awaiter awaiter8; + bool hasCurrent8; + bool running8; + T8 current8; + + IUniTaskAsyncEnumerator enumerator9; + UniTask.Awaiter awaiter9; + bool hasCurrent9; + bool running9; + T9 current9; + + IUniTaskAsyncEnumerator enumerator10; + UniTask.Awaiter awaiter10; + bool hasCurrent10; + bool running10; + T10 current10; + + int completedCount; + bool syncRunning; + TResult result; + + public _CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, Func resultSelector, CancellationToken cancellationToken) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + this.source8 = source8; + this.source9 = source9; + this.source10 = source10; + + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current => result; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + if (completedCount == CompleteCount) return CompletedTasks.False; + + if (enumerator1 == null) + { + enumerator1 = source1.GetAsyncEnumerator(cancellationToken); + enumerator2 = source2.GetAsyncEnumerator(cancellationToken); + enumerator3 = source3.GetAsyncEnumerator(cancellationToken); + enumerator4 = source4.GetAsyncEnumerator(cancellationToken); + enumerator5 = source5.GetAsyncEnumerator(cancellationToken); + enumerator6 = source6.GetAsyncEnumerator(cancellationToken); + enumerator7 = source7.GetAsyncEnumerator(cancellationToken); + enumerator8 = source8.GetAsyncEnumerator(cancellationToken); + enumerator9 = source9.GetAsyncEnumerator(cancellationToken); + enumerator10 = source10.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + + AGAIN: + syncRunning = true; + if (!running1) + { + running1 = true; + awaiter1 = enumerator1.MoveNextAsync().GetAwaiter(); + if (awaiter1.IsCompleted) + { + Completed1(this); + } + else + { + awaiter1.SourceOnCompleted(Completed1Delegate, this); + } + } + if (!running2) + { + running2 = true; + awaiter2 = enumerator2.MoveNextAsync().GetAwaiter(); + if (awaiter2.IsCompleted) + { + Completed2(this); + } + else + { + awaiter2.SourceOnCompleted(Completed2Delegate, this); + } + } + if (!running3) + { + running3 = true; + awaiter3 = enumerator3.MoveNextAsync().GetAwaiter(); + if (awaiter3.IsCompleted) + { + Completed3(this); + } + else + { + awaiter3.SourceOnCompleted(Completed3Delegate, this); + } + } + if (!running4) + { + running4 = true; + awaiter4 = enumerator4.MoveNextAsync().GetAwaiter(); + if (awaiter4.IsCompleted) + { + Completed4(this); + } + else + { + awaiter4.SourceOnCompleted(Completed4Delegate, this); + } + } + if (!running5) + { + running5 = true; + awaiter5 = enumerator5.MoveNextAsync().GetAwaiter(); + if (awaiter5.IsCompleted) + { + Completed5(this); + } + else + { + awaiter5.SourceOnCompleted(Completed5Delegate, this); + } + } + if (!running6) + { + running6 = true; + awaiter6 = enumerator6.MoveNextAsync().GetAwaiter(); + if (awaiter6.IsCompleted) + { + Completed6(this); + } + else + { + awaiter6.SourceOnCompleted(Completed6Delegate, this); + } + } + if (!running7) + { + running7 = true; + awaiter7 = enumerator7.MoveNextAsync().GetAwaiter(); + if (awaiter7.IsCompleted) + { + Completed7(this); + } + else + { + awaiter7.SourceOnCompleted(Completed7Delegate, this); + } + } + if (!running8) + { + running8 = true; + awaiter8 = enumerator8.MoveNextAsync().GetAwaiter(); + if (awaiter8.IsCompleted) + { + Completed8(this); + } + else + { + awaiter8.SourceOnCompleted(Completed8Delegate, this); + } + } + if (!running9) + { + running9 = true; + awaiter9 = enumerator9.MoveNextAsync().GetAwaiter(); + if (awaiter9.IsCompleted) + { + Completed9(this); + } + else + { + awaiter9.SourceOnCompleted(Completed9Delegate, this); + } + } + if (!running10) + { + running10 = true; + awaiter10 = enumerator10.MoveNextAsync().GetAwaiter(); + if (awaiter10.IsCompleted) + { + Completed10(this); + } + else + { + awaiter10.SourceOnCompleted(Completed10Delegate, this); + } + } + + if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7 || !running8 || !running9 || !running10) + { + goto AGAIN; + } + syncRunning = false; + + return new UniTask(this, completionSource.Version); + } + + static void Completed1(object state) + { + var self = (_CombineLatest)state; + self.running1 = false; + + try + { + if (self.awaiter1.GetResult()) + { + self.hasCurrent1 = true; + self.current1 = self.enumerator1.Current; + goto SUCCESS; + } + else + { + self.running1 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running1 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running1 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter1.SourceOnCompleted(Completed1Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed2(object state) + { + var self = (_CombineLatest)state; + self.running2 = false; + + try + { + if (self.awaiter2.GetResult()) + { + self.hasCurrent2 = true; + self.current2 = self.enumerator2.Current; + goto SUCCESS; + } + else + { + self.running2 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running2 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running2 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter2.SourceOnCompleted(Completed2Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed3(object state) + { + var self = (_CombineLatest)state; + self.running3 = false; + + try + { + if (self.awaiter3.GetResult()) + { + self.hasCurrent3 = true; + self.current3 = self.enumerator3.Current; + goto SUCCESS; + } + else + { + self.running3 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running3 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running3 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter3.SourceOnCompleted(Completed3Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed4(object state) + { + var self = (_CombineLatest)state; + self.running4 = false; + + try + { + if (self.awaiter4.GetResult()) + { + self.hasCurrent4 = true; + self.current4 = self.enumerator4.Current; + goto SUCCESS; + } + else + { + self.running4 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running4 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running4 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter4.SourceOnCompleted(Completed4Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed5(object state) + { + var self = (_CombineLatest)state; + self.running5 = false; + + try + { + if (self.awaiter5.GetResult()) + { + self.hasCurrent5 = true; + self.current5 = self.enumerator5.Current; + goto SUCCESS; + } + else + { + self.running5 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running5 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running5 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter5.SourceOnCompleted(Completed5Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed6(object state) + { + var self = (_CombineLatest)state; + self.running6 = false; + + try + { + if (self.awaiter6.GetResult()) + { + self.hasCurrent6 = true; + self.current6 = self.enumerator6.Current; + goto SUCCESS; + } + else + { + self.running6 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running6 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running6 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter6.SourceOnCompleted(Completed6Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed7(object state) + { + var self = (_CombineLatest)state; + self.running7 = false; + + try + { + if (self.awaiter7.GetResult()) + { + self.hasCurrent7 = true; + self.current7 = self.enumerator7.Current; + goto SUCCESS; + } + else + { + self.running7 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running7 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running7 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter7.SourceOnCompleted(Completed7Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed8(object state) + { + var self = (_CombineLatest)state; + self.running8 = false; + + try + { + if (self.awaiter8.GetResult()) + { + self.hasCurrent8 = true; + self.current8 = self.enumerator8.Current; + goto SUCCESS; + } + else + { + self.running8 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running8 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running8 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter8 = self.enumerator8.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter8.SourceOnCompleted(Completed8Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed9(object state) + { + var self = (_CombineLatest)state; + self.running9 = false; + + try + { + if (self.awaiter9.GetResult()) + { + self.hasCurrent9 = true; + self.current9 = self.enumerator9.Current; + goto SUCCESS; + } + else + { + self.running9 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running9 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running9 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter9 = self.enumerator9.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter9.SourceOnCompleted(Completed9Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed10(object state) + { + var self = (_CombineLatest)state; + self.running10 = false; + + try + { + if (self.awaiter10.GetResult()) + { + self.hasCurrent10 = true; + self.current10 = self.enumerator10.Current; + goto SUCCESS; + } + else + { + self.running10 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running10 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running10 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter10 = self.enumerator10.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter10.SourceOnCompleted(Completed10Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + bool TrySetResult() + { + if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7 && hasCurrent8 && hasCurrent9 && hasCurrent10) + { + result = resultSelector(current1, current2, current3, current4, current5, current6, current7, current8, current9, current10); + completionSource.TrySetResult(true); + return true; + } + else + { + return false; + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator1 != null) + { + await enumerator1.DisposeAsync(); + } + if (enumerator2 != null) + { + await enumerator2.DisposeAsync(); + } + if (enumerator3 != null) + { + await enumerator3.DisposeAsync(); + } + if (enumerator4 != null) + { + await enumerator4.DisposeAsync(); + } + if (enumerator5 != null) + { + await enumerator5.DisposeAsync(); + } + if (enumerator6 != null) + { + await enumerator6.DisposeAsync(); + } + if (enumerator7 != null) + { + await enumerator7.DisposeAsync(); + } + if (enumerator8 != null) + { + await enumerator8.DisposeAsync(); + } + if (enumerator9 != null) + { + await enumerator9.DisposeAsync(); + } + if (enumerator10 != null) + { + await enumerator10.DisposeAsync(); + } + } + } + } + + internal class CombineLatest : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + readonly IUniTaskAsyncEnumerable source8; + readonly IUniTaskAsyncEnumerable source9; + readonly IUniTaskAsyncEnumerable source10; + readonly IUniTaskAsyncEnumerable source11; + + readonly Func resultSelector; + + public CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, IUniTaskAsyncEnumerable source11, Func resultSelector) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + this.source8 = source8; + this.source9 = source9; + this.source10 = source10; + this.source11 = source11; + + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, resultSelector, cancellationToken); + } + + class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action Completed1Delegate = Completed1; + static readonly Action Completed2Delegate = Completed2; + static readonly Action Completed3Delegate = Completed3; + static readonly Action Completed4Delegate = Completed4; + static readonly Action Completed5Delegate = Completed5; + static readonly Action Completed6Delegate = Completed6; + static readonly Action Completed7Delegate = Completed7; + static readonly Action Completed8Delegate = Completed8; + static readonly Action Completed9Delegate = Completed9; + static readonly Action Completed10Delegate = Completed10; + static readonly Action Completed11Delegate = Completed11; + const int CompleteCount = 11; + + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + readonly IUniTaskAsyncEnumerable source8; + readonly IUniTaskAsyncEnumerable source9; + readonly IUniTaskAsyncEnumerable source10; + readonly IUniTaskAsyncEnumerable source11; + + readonly Func resultSelector; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator1; + UniTask.Awaiter awaiter1; + bool hasCurrent1; + bool running1; + T1 current1; + + IUniTaskAsyncEnumerator enumerator2; + UniTask.Awaiter awaiter2; + bool hasCurrent2; + bool running2; + T2 current2; + + IUniTaskAsyncEnumerator enumerator3; + UniTask.Awaiter awaiter3; + bool hasCurrent3; + bool running3; + T3 current3; + + IUniTaskAsyncEnumerator enumerator4; + UniTask.Awaiter awaiter4; + bool hasCurrent4; + bool running4; + T4 current4; + + IUniTaskAsyncEnumerator enumerator5; + UniTask.Awaiter awaiter5; + bool hasCurrent5; + bool running5; + T5 current5; + + IUniTaskAsyncEnumerator enumerator6; + UniTask.Awaiter awaiter6; + bool hasCurrent6; + bool running6; + T6 current6; + + IUniTaskAsyncEnumerator enumerator7; + UniTask.Awaiter awaiter7; + bool hasCurrent7; + bool running7; + T7 current7; + + IUniTaskAsyncEnumerator enumerator8; + UniTask.Awaiter awaiter8; + bool hasCurrent8; + bool running8; + T8 current8; + + IUniTaskAsyncEnumerator enumerator9; + UniTask.Awaiter awaiter9; + bool hasCurrent9; + bool running9; + T9 current9; + + IUniTaskAsyncEnumerator enumerator10; + UniTask.Awaiter awaiter10; + bool hasCurrent10; + bool running10; + T10 current10; + + IUniTaskAsyncEnumerator enumerator11; + UniTask.Awaiter awaiter11; + bool hasCurrent11; + bool running11; + T11 current11; + + int completedCount; + bool syncRunning; + TResult result; + + public _CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, IUniTaskAsyncEnumerable source11, Func resultSelector, CancellationToken cancellationToken) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + this.source8 = source8; + this.source9 = source9; + this.source10 = source10; + this.source11 = source11; + + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current => result; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + if (completedCount == CompleteCount) return CompletedTasks.False; + + if (enumerator1 == null) + { + enumerator1 = source1.GetAsyncEnumerator(cancellationToken); + enumerator2 = source2.GetAsyncEnumerator(cancellationToken); + enumerator3 = source3.GetAsyncEnumerator(cancellationToken); + enumerator4 = source4.GetAsyncEnumerator(cancellationToken); + enumerator5 = source5.GetAsyncEnumerator(cancellationToken); + enumerator6 = source6.GetAsyncEnumerator(cancellationToken); + enumerator7 = source7.GetAsyncEnumerator(cancellationToken); + enumerator8 = source8.GetAsyncEnumerator(cancellationToken); + enumerator9 = source9.GetAsyncEnumerator(cancellationToken); + enumerator10 = source10.GetAsyncEnumerator(cancellationToken); + enumerator11 = source11.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + + AGAIN: + syncRunning = true; + if (!running1) + { + running1 = true; + awaiter1 = enumerator1.MoveNextAsync().GetAwaiter(); + if (awaiter1.IsCompleted) + { + Completed1(this); + } + else + { + awaiter1.SourceOnCompleted(Completed1Delegate, this); + } + } + if (!running2) + { + running2 = true; + awaiter2 = enumerator2.MoveNextAsync().GetAwaiter(); + if (awaiter2.IsCompleted) + { + Completed2(this); + } + else + { + awaiter2.SourceOnCompleted(Completed2Delegate, this); + } + } + if (!running3) + { + running3 = true; + awaiter3 = enumerator3.MoveNextAsync().GetAwaiter(); + if (awaiter3.IsCompleted) + { + Completed3(this); + } + else + { + awaiter3.SourceOnCompleted(Completed3Delegate, this); + } + } + if (!running4) + { + running4 = true; + awaiter4 = enumerator4.MoveNextAsync().GetAwaiter(); + if (awaiter4.IsCompleted) + { + Completed4(this); + } + else + { + awaiter4.SourceOnCompleted(Completed4Delegate, this); + } + } + if (!running5) + { + running5 = true; + awaiter5 = enumerator5.MoveNextAsync().GetAwaiter(); + if (awaiter5.IsCompleted) + { + Completed5(this); + } + else + { + awaiter5.SourceOnCompleted(Completed5Delegate, this); + } + } + if (!running6) + { + running6 = true; + awaiter6 = enumerator6.MoveNextAsync().GetAwaiter(); + if (awaiter6.IsCompleted) + { + Completed6(this); + } + else + { + awaiter6.SourceOnCompleted(Completed6Delegate, this); + } + } + if (!running7) + { + running7 = true; + awaiter7 = enumerator7.MoveNextAsync().GetAwaiter(); + if (awaiter7.IsCompleted) + { + Completed7(this); + } + else + { + awaiter7.SourceOnCompleted(Completed7Delegate, this); + } + } + if (!running8) + { + running8 = true; + awaiter8 = enumerator8.MoveNextAsync().GetAwaiter(); + if (awaiter8.IsCompleted) + { + Completed8(this); + } + else + { + awaiter8.SourceOnCompleted(Completed8Delegate, this); + } + } + if (!running9) + { + running9 = true; + awaiter9 = enumerator9.MoveNextAsync().GetAwaiter(); + if (awaiter9.IsCompleted) + { + Completed9(this); + } + else + { + awaiter9.SourceOnCompleted(Completed9Delegate, this); + } + } + if (!running10) + { + running10 = true; + awaiter10 = enumerator10.MoveNextAsync().GetAwaiter(); + if (awaiter10.IsCompleted) + { + Completed10(this); + } + else + { + awaiter10.SourceOnCompleted(Completed10Delegate, this); + } + } + if (!running11) + { + running11 = true; + awaiter11 = enumerator11.MoveNextAsync().GetAwaiter(); + if (awaiter11.IsCompleted) + { + Completed11(this); + } + else + { + awaiter11.SourceOnCompleted(Completed11Delegate, this); + } + } + + if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7 || !running8 || !running9 || !running10 || !running11) + { + goto AGAIN; + } + syncRunning = false; + + return new UniTask(this, completionSource.Version); + } + + static void Completed1(object state) + { + var self = (_CombineLatest)state; + self.running1 = false; + + try + { + if (self.awaiter1.GetResult()) + { + self.hasCurrent1 = true; + self.current1 = self.enumerator1.Current; + goto SUCCESS; + } + else + { + self.running1 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running1 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running1 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter1.SourceOnCompleted(Completed1Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed2(object state) + { + var self = (_CombineLatest)state; + self.running2 = false; + + try + { + if (self.awaiter2.GetResult()) + { + self.hasCurrent2 = true; + self.current2 = self.enumerator2.Current; + goto SUCCESS; + } + else + { + self.running2 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running2 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running2 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter2.SourceOnCompleted(Completed2Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed3(object state) + { + var self = (_CombineLatest)state; + self.running3 = false; + + try + { + if (self.awaiter3.GetResult()) + { + self.hasCurrent3 = true; + self.current3 = self.enumerator3.Current; + goto SUCCESS; + } + else + { + self.running3 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running3 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running3 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter3.SourceOnCompleted(Completed3Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed4(object state) + { + var self = (_CombineLatest)state; + self.running4 = false; + + try + { + if (self.awaiter4.GetResult()) + { + self.hasCurrent4 = true; + self.current4 = self.enumerator4.Current; + goto SUCCESS; + } + else + { + self.running4 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running4 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running4 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter4.SourceOnCompleted(Completed4Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed5(object state) + { + var self = (_CombineLatest)state; + self.running5 = false; + + try + { + if (self.awaiter5.GetResult()) + { + self.hasCurrent5 = true; + self.current5 = self.enumerator5.Current; + goto SUCCESS; + } + else + { + self.running5 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running5 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running5 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter5.SourceOnCompleted(Completed5Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed6(object state) + { + var self = (_CombineLatest)state; + self.running6 = false; + + try + { + if (self.awaiter6.GetResult()) + { + self.hasCurrent6 = true; + self.current6 = self.enumerator6.Current; + goto SUCCESS; + } + else + { + self.running6 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running6 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running6 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter6.SourceOnCompleted(Completed6Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed7(object state) + { + var self = (_CombineLatest)state; + self.running7 = false; + + try + { + if (self.awaiter7.GetResult()) + { + self.hasCurrent7 = true; + self.current7 = self.enumerator7.Current; + goto SUCCESS; + } + else + { + self.running7 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running7 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running7 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter7.SourceOnCompleted(Completed7Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed8(object state) + { + var self = (_CombineLatest)state; + self.running8 = false; + + try + { + if (self.awaiter8.GetResult()) + { + self.hasCurrent8 = true; + self.current8 = self.enumerator8.Current; + goto SUCCESS; + } + else + { + self.running8 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running8 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running8 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter8 = self.enumerator8.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter8.SourceOnCompleted(Completed8Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed9(object state) + { + var self = (_CombineLatest)state; + self.running9 = false; + + try + { + if (self.awaiter9.GetResult()) + { + self.hasCurrent9 = true; + self.current9 = self.enumerator9.Current; + goto SUCCESS; + } + else + { + self.running9 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running9 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running9 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter9 = self.enumerator9.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter9.SourceOnCompleted(Completed9Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed10(object state) + { + var self = (_CombineLatest)state; + self.running10 = false; + + try + { + if (self.awaiter10.GetResult()) + { + self.hasCurrent10 = true; + self.current10 = self.enumerator10.Current; + goto SUCCESS; + } + else + { + self.running10 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running10 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running10 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter10 = self.enumerator10.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter10.SourceOnCompleted(Completed10Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed11(object state) + { + var self = (_CombineLatest)state; + self.running11 = false; + + try + { + if (self.awaiter11.GetResult()) + { + self.hasCurrent11 = true; + self.current11 = self.enumerator11.Current; + goto SUCCESS; + } + else + { + self.running11 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running11 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running11 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter11 = self.enumerator11.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter11.SourceOnCompleted(Completed11Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + bool TrySetResult() + { + if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7 && hasCurrent8 && hasCurrent9 && hasCurrent10 && hasCurrent11) + { + result = resultSelector(current1, current2, current3, current4, current5, current6, current7, current8, current9, current10, current11); + completionSource.TrySetResult(true); + return true; + } + else + { + return false; + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator1 != null) + { + await enumerator1.DisposeAsync(); + } + if (enumerator2 != null) + { + await enumerator2.DisposeAsync(); + } + if (enumerator3 != null) + { + await enumerator3.DisposeAsync(); + } + if (enumerator4 != null) + { + await enumerator4.DisposeAsync(); + } + if (enumerator5 != null) + { + await enumerator5.DisposeAsync(); + } + if (enumerator6 != null) + { + await enumerator6.DisposeAsync(); + } + if (enumerator7 != null) + { + await enumerator7.DisposeAsync(); + } + if (enumerator8 != null) + { + await enumerator8.DisposeAsync(); + } + if (enumerator9 != null) + { + await enumerator9.DisposeAsync(); + } + if (enumerator10 != null) + { + await enumerator10.DisposeAsync(); + } + if (enumerator11 != null) + { + await enumerator11.DisposeAsync(); + } + } + } + } + + internal class CombineLatest : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + readonly IUniTaskAsyncEnumerable source8; + readonly IUniTaskAsyncEnumerable source9; + readonly IUniTaskAsyncEnumerable source10; + readonly IUniTaskAsyncEnumerable source11; + readonly IUniTaskAsyncEnumerable source12; + + readonly Func resultSelector; + + public CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, IUniTaskAsyncEnumerable source11, IUniTaskAsyncEnumerable source12, Func resultSelector) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + this.source8 = source8; + this.source9 = source9; + this.source10 = source10; + this.source11 = source11; + this.source12 = source12; + + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, source12, resultSelector, cancellationToken); + } + + class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action Completed1Delegate = Completed1; + static readonly Action Completed2Delegate = Completed2; + static readonly Action Completed3Delegate = Completed3; + static readonly Action Completed4Delegate = Completed4; + static readonly Action Completed5Delegate = Completed5; + static readonly Action Completed6Delegate = Completed6; + static readonly Action Completed7Delegate = Completed7; + static readonly Action Completed8Delegate = Completed8; + static readonly Action Completed9Delegate = Completed9; + static readonly Action Completed10Delegate = Completed10; + static readonly Action Completed11Delegate = Completed11; + static readonly Action Completed12Delegate = Completed12; + const int CompleteCount = 12; + + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + readonly IUniTaskAsyncEnumerable source8; + readonly IUniTaskAsyncEnumerable source9; + readonly IUniTaskAsyncEnumerable source10; + readonly IUniTaskAsyncEnumerable source11; + readonly IUniTaskAsyncEnumerable source12; + + readonly Func resultSelector; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator1; + UniTask.Awaiter awaiter1; + bool hasCurrent1; + bool running1; + T1 current1; + + IUniTaskAsyncEnumerator enumerator2; + UniTask.Awaiter awaiter2; + bool hasCurrent2; + bool running2; + T2 current2; + + IUniTaskAsyncEnumerator enumerator3; + UniTask.Awaiter awaiter3; + bool hasCurrent3; + bool running3; + T3 current3; + + IUniTaskAsyncEnumerator enumerator4; + UniTask.Awaiter awaiter4; + bool hasCurrent4; + bool running4; + T4 current4; + + IUniTaskAsyncEnumerator enumerator5; + UniTask.Awaiter awaiter5; + bool hasCurrent5; + bool running5; + T5 current5; + + IUniTaskAsyncEnumerator enumerator6; + UniTask.Awaiter awaiter6; + bool hasCurrent6; + bool running6; + T6 current6; + + IUniTaskAsyncEnumerator enumerator7; + UniTask.Awaiter awaiter7; + bool hasCurrent7; + bool running7; + T7 current7; + + IUniTaskAsyncEnumerator enumerator8; + UniTask.Awaiter awaiter8; + bool hasCurrent8; + bool running8; + T8 current8; + + IUniTaskAsyncEnumerator enumerator9; + UniTask.Awaiter awaiter9; + bool hasCurrent9; + bool running9; + T9 current9; + + IUniTaskAsyncEnumerator enumerator10; + UniTask.Awaiter awaiter10; + bool hasCurrent10; + bool running10; + T10 current10; + + IUniTaskAsyncEnumerator enumerator11; + UniTask.Awaiter awaiter11; + bool hasCurrent11; + bool running11; + T11 current11; + + IUniTaskAsyncEnumerator enumerator12; + UniTask.Awaiter awaiter12; + bool hasCurrent12; + bool running12; + T12 current12; + + int completedCount; + bool syncRunning; + TResult result; + + public _CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, IUniTaskAsyncEnumerable source11, IUniTaskAsyncEnumerable source12, Func resultSelector, CancellationToken cancellationToken) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + this.source8 = source8; + this.source9 = source9; + this.source10 = source10; + this.source11 = source11; + this.source12 = source12; + + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current => result; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + if (completedCount == CompleteCount) return CompletedTasks.False; + + if (enumerator1 == null) + { + enumerator1 = source1.GetAsyncEnumerator(cancellationToken); + enumerator2 = source2.GetAsyncEnumerator(cancellationToken); + enumerator3 = source3.GetAsyncEnumerator(cancellationToken); + enumerator4 = source4.GetAsyncEnumerator(cancellationToken); + enumerator5 = source5.GetAsyncEnumerator(cancellationToken); + enumerator6 = source6.GetAsyncEnumerator(cancellationToken); + enumerator7 = source7.GetAsyncEnumerator(cancellationToken); + enumerator8 = source8.GetAsyncEnumerator(cancellationToken); + enumerator9 = source9.GetAsyncEnumerator(cancellationToken); + enumerator10 = source10.GetAsyncEnumerator(cancellationToken); + enumerator11 = source11.GetAsyncEnumerator(cancellationToken); + enumerator12 = source12.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + + AGAIN: + syncRunning = true; + if (!running1) + { + running1 = true; + awaiter1 = enumerator1.MoveNextAsync().GetAwaiter(); + if (awaiter1.IsCompleted) + { + Completed1(this); + } + else + { + awaiter1.SourceOnCompleted(Completed1Delegate, this); + } + } + if (!running2) + { + running2 = true; + awaiter2 = enumerator2.MoveNextAsync().GetAwaiter(); + if (awaiter2.IsCompleted) + { + Completed2(this); + } + else + { + awaiter2.SourceOnCompleted(Completed2Delegate, this); + } + } + if (!running3) + { + running3 = true; + awaiter3 = enumerator3.MoveNextAsync().GetAwaiter(); + if (awaiter3.IsCompleted) + { + Completed3(this); + } + else + { + awaiter3.SourceOnCompleted(Completed3Delegate, this); + } + } + if (!running4) + { + running4 = true; + awaiter4 = enumerator4.MoveNextAsync().GetAwaiter(); + if (awaiter4.IsCompleted) + { + Completed4(this); + } + else + { + awaiter4.SourceOnCompleted(Completed4Delegate, this); + } + } + if (!running5) + { + running5 = true; + awaiter5 = enumerator5.MoveNextAsync().GetAwaiter(); + if (awaiter5.IsCompleted) + { + Completed5(this); + } + else + { + awaiter5.SourceOnCompleted(Completed5Delegate, this); + } + } + if (!running6) + { + running6 = true; + awaiter6 = enumerator6.MoveNextAsync().GetAwaiter(); + if (awaiter6.IsCompleted) + { + Completed6(this); + } + else + { + awaiter6.SourceOnCompleted(Completed6Delegate, this); + } + } + if (!running7) + { + running7 = true; + awaiter7 = enumerator7.MoveNextAsync().GetAwaiter(); + if (awaiter7.IsCompleted) + { + Completed7(this); + } + else + { + awaiter7.SourceOnCompleted(Completed7Delegate, this); + } + } + if (!running8) + { + running8 = true; + awaiter8 = enumerator8.MoveNextAsync().GetAwaiter(); + if (awaiter8.IsCompleted) + { + Completed8(this); + } + else + { + awaiter8.SourceOnCompleted(Completed8Delegate, this); + } + } + if (!running9) + { + running9 = true; + awaiter9 = enumerator9.MoveNextAsync().GetAwaiter(); + if (awaiter9.IsCompleted) + { + Completed9(this); + } + else + { + awaiter9.SourceOnCompleted(Completed9Delegate, this); + } + } + if (!running10) + { + running10 = true; + awaiter10 = enumerator10.MoveNextAsync().GetAwaiter(); + if (awaiter10.IsCompleted) + { + Completed10(this); + } + else + { + awaiter10.SourceOnCompleted(Completed10Delegate, this); + } + } + if (!running11) + { + running11 = true; + awaiter11 = enumerator11.MoveNextAsync().GetAwaiter(); + if (awaiter11.IsCompleted) + { + Completed11(this); + } + else + { + awaiter11.SourceOnCompleted(Completed11Delegate, this); + } + } + if (!running12) + { + running12 = true; + awaiter12 = enumerator12.MoveNextAsync().GetAwaiter(); + if (awaiter12.IsCompleted) + { + Completed12(this); + } + else + { + awaiter12.SourceOnCompleted(Completed12Delegate, this); + } + } + + if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7 || !running8 || !running9 || !running10 || !running11 || !running12) + { + goto AGAIN; + } + syncRunning = false; + + return new UniTask(this, completionSource.Version); + } + + static void Completed1(object state) + { + var self = (_CombineLatest)state; + self.running1 = false; + + try + { + if (self.awaiter1.GetResult()) + { + self.hasCurrent1 = true; + self.current1 = self.enumerator1.Current; + goto SUCCESS; + } + else + { + self.running1 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running1 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running1 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter1.SourceOnCompleted(Completed1Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed2(object state) + { + var self = (_CombineLatest)state; + self.running2 = false; + + try + { + if (self.awaiter2.GetResult()) + { + self.hasCurrent2 = true; + self.current2 = self.enumerator2.Current; + goto SUCCESS; + } + else + { + self.running2 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running2 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running2 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter2.SourceOnCompleted(Completed2Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed3(object state) + { + var self = (_CombineLatest)state; + self.running3 = false; + + try + { + if (self.awaiter3.GetResult()) + { + self.hasCurrent3 = true; + self.current3 = self.enumerator3.Current; + goto SUCCESS; + } + else + { + self.running3 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running3 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running3 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter3.SourceOnCompleted(Completed3Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed4(object state) + { + var self = (_CombineLatest)state; + self.running4 = false; + + try + { + if (self.awaiter4.GetResult()) + { + self.hasCurrent4 = true; + self.current4 = self.enumerator4.Current; + goto SUCCESS; + } + else + { + self.running4 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running4 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running4 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter4.SourceOnCompleted(Completed4Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed5(object state) + { + var self = (_CombineLatest)state; + self.running5 = false; + + try + { + if (self.awaiter5.GetResult()) + { + self.hasCurrent5 = true; + self.current5 = self.enumerator5.Current; + goto SUCCESS; + } + else + { + self.running5 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running5 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running5 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter5.SourceOnCompleted(Completed5Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed6(object state) + { + var self = (_CombineLatest)state; + self.running6 = false; + + try + { + if (self.awaiter6.GetResult()) + { + self.hasCurrent6 = true; + self.current6 = self.enumerator6.Current; + goto SUCCESS; + } + else + { + self.running6 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running6 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running6 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter6.SourceOnCompleted(Completed6Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed7(object state) + { + var self = (_CombineLatest)state; + self.running7 = false; + + try + { + if (self.awaiter7.GetResult()) + { + self.hasCurrent7 = true; + self.current7 = self.enumerator7.Current; + goto SUCCESS; + } + else + { + self.running7 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running7 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running7 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter7.SourceOnCompleted(Completed7Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed8(object state) + { + var self = (_CombineLatest)state; + self.running8 = false; + + try + { + if (self.awaiter8.GetResult()) + { + self.hasCurrent8 = true; + self.current8 = self.enumerator8.Current; + goto SUCCESS; + } + else + { + self.running8 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running8 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running8 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter8 = self.enumerator8.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter8.SourceOnCompleted(Completed8Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed9(object state) + { + var self = (_CombineLatest)state; + self.running9 = false; + + try + { + if (self.awaiter9.GetResult()) + { + self.hasCurrent9 = true; + self.current9 = self.enumerator9.Current; + goto SUCCESS; + } + else + { + self.running9 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running9 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running9 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter9 = self.enumerator9.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter9.SourceOnCompleted(Completed9Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed10(object state) + { + var self = (_CombineLatest)state; + self.running10 = false; + + try + { + if (self.awaiter10.GetResult()) + { + self.hasCurrent10 = true; + self.current10 = self.enumerator10.Current; + goto SUCCESS; + } + else + { + self.running10 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running10 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running10 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter10 = self.enumerator10.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter10.SourceOnCompleted(Completed10Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed11(object state) + { + var self = (_CombineLatest)state; + self.running11 = false; + + try + { + if (self.awaiter11.GetResult()) + { + self.hasCurrent11 = true; + self.current11 = self.enumerator11.Current; + goto SUCCESS; + } + else + { + self.running11 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running11 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running11 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter11 = self.enumerator11.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter11.SourceOnCompleted(Completed11Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed12(object state) + { + var self = (_CombineLatest)state; + self.running12 = false; + + try + { + if (self.awaiter12.GetResult()) + { + self.hasCurrent12 = true; + self.current12 = self.enumerator12.Current; + goto SUCCESS; + } + else + { + self.running12 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running12 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running12 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter12 = self.enumerator12.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter12.SourceOnCompleted(Completed12Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + bool TrySetResult() + { + if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7 && hasCurrent8 && hasCurrent9 && hasCurrent10 && hasCurrent11 && hasCurrent12) + { + result = resultSelector(current1, current2, current3, current4, current5, current6, current7, current8, current9, current10, current11, current12); + completionSource.TrySetResult(true); + return true; + } + else + { + return false; + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator1 != null) + { + await enumerator1.DisposeAsync(); + } + if (enumerator2 != null) + { + await enumerator2.DisposeAsync(); + } + if (enumerator3 != null) + { + await enumerator3.DisposeAsync(); + } + if (enumerator4 != null) + { + await enumerator4.DisposeAsync(); + } + if (enumerator5 != null) + { + await enumerator5.DisposeAsync(); + } + if (enumerator6 != null) + { + await enumerator6.DisposeAsync(); + } + if (enumerator7 != null) + { + await enumerator7.DisposeAsync(); + } + if (enumerator8 != null) + { + await enumerator8.DisposeAsync(); + } + if (enumerator9 != null) + { + await enumerator9.DisposeAsync(); + } + if (enumerator10 != null) + { + await enumerator10.DisposeAsync(); + } + if (enumerator11 != null) + { + await enumerator11.DisposeAsync(); + } + if (enumerator12 != null) + { + await enumerator12.DisposeAsync(); + } + } + } + } + + internal class CombineLatest : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + readonly IUniTaskAsyncEnumerable source8; + readonly IUniTaskAsyncEnumerable source9; + readonly IUniTaskAsyncEnumerable source10; + readonly IUniTaskAsyncEnumerable source11; + readonly IUniTaskAsyncEnumerable source12; + readonly IUniTaskAsyncEnumerable source13; + + readonly Func resultSelector; + + public CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, IUniTaskAsyncEnumerable source11, IUniTaskAsyncEnumerable source12, IUniTaskAsyncEnumerable source13, Func resultSelector) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + this.source8 = source8; + this.source9 = source9; + this.source10 = source10; + this.source11 = source11; + this.source12 = source12; + this.source13 = source13; + + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, source12, source13, resultSelector, cancellationToken); + } + + class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action Completed1Delegate = Completed1; + static readonly Action Completed2Delegate = Completed2; + static readonly Action Completed3Delegate = Completed3; + static readonly Action Completed4Delegate = Completed4; + static readonly Action Completed5Delegate = Completed5; + static readonly Action Completed6Delegate = Completed6; + static readonly Action Completed7Delegate = Completed7; + static readonly Action Completed8Delegate = Completed8; + static readonly Action Completed9Delegate = Completed9; + static readonly Action Completed10Delegate = Completed10; + static readonly Action Completed11Delegate = Completed11; + static readonly Action Completed12Delegate = Completed12; + static readonly Action Completed13Delegate = Completed13; + const int CompleteCount = 13; + + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + readonly IUniTaskAsyncEnumerable source8; + readonly IUniTaskAsyncEnumerable source9; + readonly IUniTaskAsyncEnumerable source10; + readonly IUniTaskAsyncEnumerable source11; + readonly IUniTaskAsyncEnumerable source12; + readonly IUniTaskAsyncEnumerable source13; + + readonly Func resultSelector; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator1; + UniTask.Awaiter awaiter1; + bool hasCurrent1; + bool running1; + T1 current1; + + IUniTaskAsyncEnumerator enumerator2; + UniTask.Awaiter awaiter2; + bool hasCurrent2; + bool running2; + T2 current2; + + IUniTaskAsyncEnumerator enumerator3; + UniTask.Awaiter awaiter3; + bool hasCurrent3; + bool running3; + T3 current3; + + IUniTaskAsyncEnumerator enumerator4; + UniTask.Awaiter awaiter4; + bool hasCurrent4; + bool running4; + T4 current4; + + IUniTaskAsyncEnumerator enumerator5; + UniTask.Awaiter awaiter5; + bool hasCurrent5; + bool running5; + T5 current5; + + IUniTaskAsyncEnumerator enumerator6; + UniTask.Awaiter awaiter6; + bool hasCurrent6; + bool running6; + T6 current6; + + IUniTaskAsyncEnumerator enumerator7; + UniTask.Awaiter awaiter7; + bool hasCurrent7; + bool running7; + T7 current7; + + IUniTaskAsyncEnumerator enumerator8; + UniTask.Awaiter awaiter8; + bool hasCurrent8; + bool running8; + T8 current8; + + IUniTaskAsyncEnumerator enumerator9; + UniTask.Awaiter awaiter9; + bool hasCurrent9; + bool running9; + T9 current9; + + IUniTaskAsyncEnumerator enumerator10; + UniTask.Awaiter awaiter10; + bool hasCurrent10; + bool running10; + T10 current10; + + IUniTaskAsyncEnumerator enumerator11; + UniTask.Awaiter awaiter11; + bool hasCurrent11; + bool running11; + T11 current11; + + IUniTaskAsyncEnumerator enumerator12; + UniTask.Awaiter awaiter12; + bool hasCurrent12; + bool running12; + T12 current12; + + IUniTaskAsyncEnumerator enumerator13; + UniTask.Awaiter awaiter13; + bool hasCurrent13; + bool running13; + T13 current13; + + int completedCount; + bool syncRunning; + TResult result; + + public _CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, IUniTaskAsyncEnumerable source11, IUniTaskAsyncEnumerable source12, IUniTaskAsyncEnumerable source13, Func resultSelector, CancellationToken cancellationToken) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + this.source8 = source8; + this.source9 = source9; + this.source10 = source10; + this.source11 = source11; + this.source12 = source12; + this.source13 = source13; + + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current => result; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + if (completedCount == CompleteCount) return CompletedTasks.False; + + if (enumerator1 == null) + { + enumerator1 = source1.GetAsyncEnumerator(cancellationToken); + enumerator2 = source2.GetAsyncEnumerator(cancellationToken); + enumerator3 = source3.GetAsyncEnumerator(cancellationToken); + enumerator4 = source4.GetAsyncEnumerator(cancellationToken); + enumerator5 = source5.GetAsyncEnumerator(cancellationToken); + enumerator6 = source6.GetAsyncEnumerator(cancellationToken); + enumerator7 = source7.GetAsyncEnumerator(cancellationToken); + enumerator8 = source8.GetAsyncEnumerator(cancellationToken); + enumerator9 = source9.GetAsyncEnumerator(cancellationToken); + enumerator10 = source10.GetAsyncEnumerator(cancellationToken); + enumerator11 = source11.GetAsyncEnumerator(cancellationToken); + enumerator12 = source12.GetAsyncEnumerator(cancellationToken); + enumerator13 = source13.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + + AGAIN: + syncRunning = true; + if (!running1) + { + running1 = true; + awaiter1 = enumerator1.MoveNextAsync().GetAwaiter(); + if (awaiter1.IsCompleted) + { + Completed1(this); + } + else + { + awaiter1.SourceOnCompleted(Completed1Delegate, this); + } + } + if (!running2) + { + running2 = true; + awaiter2 = enumerator2.MoveNextAsync().GetAwaiter(); + if (awaiter2.IsCompleted) + { + Completed2(this); + } + else + { + awaiter2.SourceOnCompleted(Completed2Delegate, this); + } + } + if (!running3) + { + running3 = true; + awaiter3 = enumerator3.MoveNextAsync().GetAwaiter(); + if (awaiter3.IsCompleted) + { + Completed3(this); + } + else + { + awaiter3.SourceOnCompleted(Completed3Delegate, this); + } + } + if (!running4) + { + running4 = true; + awaiter4 = enumerator4.MoveNextAsync().GetAwaiter(); + if (awaiter4.IsCompleted) + { + Completed4(this); + } + else + { + awaiter4.SourceOnCompleted(Completed4Delegate, this); + } + } + if (!running5) + { + running5 = true; + awaiter5 = enumerator5.MoveNextAsync().GetAwaiter(); + if (awaiter5.IsCompleted) + { + Completed5(this); + } + else + { + awaiter5.SourceOnCompleted(Completed5Delegate, this); + } + } + if (!running6) + { + running6 = true; + awaiter6 = enumerator6.MoveNextAsync().GetAwaiter(); + if (awaiter6.IsCompleted) + { + Completed6(this); + } + else + { + awaiter6.SourceOnCompleted(Completed6Delegate, this); + } + } + if (!running7) + { + running7 = true; + awaiter7 = enumerator7.MoveNextAsync().GetAwaiter(); + if (awaiter7.IsCompleted) + { + Completed7(this); + } + else + { + awaiter7.SourceOnCompleted(Completed7Delegate, this); + } + } + if (!running8) + { + running8 = true; + awaiter8 = enumerator8.MoveNextAsync().GetAwaiter(); + if (awaiter8.IsCompleted) + { + Completed8(this); + } + else + { + awaiter8.SourceOnCompleted(Completed8Delegate, this); + } + } + if (!running9) + { + running9 = true; + awaiter9 = enumerator9.MoveNextAsync().GetAwaiter(); + if (awaiter9.IsCompleted) + { + Completed9(this); + } + else + { + awaiter9.SourceOnCompleted(Completed9Delegate, this); + } + } + if (!running10) + { + running10 = true; + awaiter10 = enumerator10.MoveNextAsync().GetAwaiter(); + if (awaiter10.IsCompleted) + { + Completed10(this); + } + else + { + awaiter10.SourceOnCompleted(Completed10Delegate, this); + } + } + if (!running11) + { + running11 = true; + awaiter11 = enumerator11.MoveNextAsync().GetAwaiter(); + if (awaiter11.IsCompleted) + { + Completed11(this); + } + else + { + awaiter11.SourceOnCompleted(Completed11Delegate, this); + } + } + if (!running12) + { + running12 = true; + awaiter12 = enumerator12.MoveNextAsync().GetAwaiter(); + if (awaiter12.IsCompleted) + { + Completed12(this); + } + else + { + awaiter12.SourceOnCompleted(Completed12Delegate, this); + } + } + if (!running13) + { + running13 = true; + awaiter13 = enumerator13.MoveNextAsync().GetAwaiter(); + if (awaiter13.IsCompleted) + { + Completed13(this); + } + else + { + awaiter13.SourceOnCompleted(Completed13Delegate, this); + } + } + + if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7 || !running8 || !running9 || !running10 || !running11 || !running12 || !running13) + { + goto AGAIN; + } + syncRunning = false; + + return new UniTask(this, completionSource.Version); + } + + static void Completed1(object state) + { + var self = (_CombineLatest)state; + self.running1 = false; + + try + { + if (self.awaiter1.GetResult()) + { + self.hasCurrent1 = true; + self.current1 = self.enumerator1.Current; + goto SUCCESS; + } + else + { + self.running1 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running1 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running1 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter1.SourceOnCompleted(Completed1Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed2(object state) + { + var self = (_CombineLatest)state; + self.running2 = false; + + try + { + if (self.awaiter2.GetResult()) + { + self.hasCurrent2 = true; + self.current2 = self.enumerator2.Current; + goto SUCCESS; + } + else + { + self.running2 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running2 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running2 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter2.SourceOnCompleted(Completed2Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed3(object state) + { + var self = (_CombineLatest)state; + self.running3 = false; + + try + { + if (self.awaiter3.GetResult()) + { + self.hasCurrent3 = true; + self.current3 = self.enumerator3.Current; + goto SUCCESS; + } + else + { + self.running3 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running3 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running3 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter3.SourceOnCompleted(Completed3Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed4(object state) + { + var self = (_CombineLatest)state; + self.running4 = false; + + try + { + if (self.awaiter4.GetResult()) + { + self.hasCurrent4 = true; + self.current4 = self.enumerator4.Current; + goto SUCCESS; + } + else + { + self.running4 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running4 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running4 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter4.SourceOnCompleted(Completed4Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed5(object state) + { + var self = (_CombineLatest)state; + self.running5 = false; + + try + { + if (self.awaiter5.GetResult()) + { + self.hasCurrent5 = true; + self.current5 = self.enumerator5.Current; + goto SUCCESS; + } + else + { + self.running5 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running5 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running5 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter5.SourceOnCompleted(Completed5Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed6(object state) + { + var self = (_CombineLatest)state; + self.running6 = false; + + try + { + if (self.awaiter6.GetResult()) + { + self.hasCurrent6 = true; + self.current6 = self.enumerator6.Current; + goto SUCCESS; + } + else + { + self.running6 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running6 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running6 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter6.SourceOnCompleted(Completed6Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed7(object state) + { + var self = (_CombineLatest)state; + self.running7 = false; + + try + { + if (self.awaiter7.GetResult()) + { + self.hasCurrent7 = true; + self.current7 = self.enumerator7.Current; + goto SUCCESS; + } + else + { + self.running7 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running7 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running7 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter7.SourceOnCompleted(Completed7Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed8(object state) + { + var self = (_CombineLatest)state; + self.running8 = false; + + try + { + if (self.awaiter8.GetResult()) + { + self.hasCurrent8 = true; + self.current8 = self.enumerator8.Current; + goto SUCCESS; + } + else + { + self.running8 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running8 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running8 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter8 = self.enumerator8.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter8.SourceOnCompleted(Completed8Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed9(object state) + { + var self = (_CombineLatest)state; + self.running9 = false; + + try + { + if (self.awaiter9.GetResult()) + { + self.hasCurrent9 = true; + self.current9 = self.enumerator9.Current; + goto SUCCESS; + } + else + { + self.running9 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running9 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running9 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter9 = self.enumerator9.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter9.SourceOnCompleted(Completed9Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed10(object state) + { + var self = (_CombineLatest)state; + self.running10 = false; + + try + { + if (self.awaiter10.GetResult()) + { + self.hasCurrent10 = true; + self.current10 = self.enumerator10.Current; + goto SUCCESS; + } + else + { + self.running10 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running10 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running10 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter10 = self.enumerator10.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter10.SourceOnCompleted(Completed10Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed11(object state) + { + var self = (_CombineLatest)state; + self.running11 = false; + + try + { + if (self.awaiter11.GetResult()) + { + self.hasCurrent11 = true; + self.current11 = self.enumerator11.Current; + goto SUCCESS; + } + else + { + self.running11 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running11 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running11 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter11 = self.enumerator11.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter11.SourceOnCompleted(Completed11Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed12(object state) + { + var self = (_CombineLatest)state; + self.running12 = false; + + try + { + if (self.awaiter12.GetResult()) + { + self.hasCurrent12 = true; + self.current12 = self.enumerator12.Current; + goto SUCCESS; + } + else + { + self.running12 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running12 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running12 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter12 = self.enumerator12.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter12.SourceOnCompleted(Completed12Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed13(object state) + { + var self = (_CombineLatest)state; + self.running13 = false; + + try + { + if (self.awaiter13.GetResult()) + { + self.hasCurrent13 = true; + self.current13 = self.enumerator13.Current; + goto SUCCESS; + } + else + { + self.running13 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running13 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running13 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter13 = self.enumerator13.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter13.SourceOnCompleted(Completed13Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + bool TrySetResult() + { + if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7 && hasCurrent8 && hasCurrent9 && hasCurrent10 && hasCurrent11 && hasCurrent12 && hasCurrent13) + { + result = resultSelector(current1, current2, current3, current4, current5, current6, current7, current8, current9, current10, current11, current12, current13); + completionSource.TrySetResult(true); + return true; + } + else + { + return false; + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator1 != null) + { + await enumerator1.DisposeAsync(); + } + if (enumerator2 != null) + { + await enumerator2.DisposeAsync(); + } + if (enumerator3 != null) + { + await enumerator3.DisposeAsync(); + } + if (enumerator4 != null) + { + await enumerator4.DisposeAsync(); + } + if (enumerator5 != null) + { + await enumerator5.DisposeAsync(); + } + if (enumerator6 != null) + { + await enumerator6.DisposeAsync(); + } + if (enumerator7 != null) + { + await enumerator7.DisposeAsync(); + } + if (enumerator8 != null) + { + await enumerator8.DisposeAsync(); + } + if (enumerator9 != null) + { + await enumerator9.DisposeAsync(); + } + if (enumerator10 != null) + { + await enumerator10.DisposeAsync(); + } + if (enumerator11 != null) + { + await enumerator11.DisposeAsync(); + } + if (enumerator12 != null) + { + await enumerator12.DisposeAsync(); + } + if (enumerator13 != null) + { + await enumerator13.DisposeAsync(); + } + } + } + } + + internal class CombineLatest : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + readonly IUniTaskAsyncEnumerable source8; + readonly IUniTaskAsyncEnumerable source9; + readonly IUniTaskAsyncEnumerable source10; + readonly IUniTaskAsyncEnumerable source11; + readonly IUniTaskAsyncEnumerable source12; + readonly IUniTaskAsyncEnumerable source13; + readonly IUniTaskAsyncEnumerable source14; + + readonly Func resultSelector; + + public CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, IUniTaskAsyncEnumerable source11, IUniTaskAsyncEnumerable source12, IUniTaskAsyncEnumerable source13, IUniTaskAsyncEnumerable source14, Func resultSelector) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + this.source8 = source8; + this.source9 = source9; + this.source10 = source10; + this.source11 = source11; + this.source12 = source12; + this.source13 = source13; + this.source14 = source14; + + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, source12, source13, source14, resultSelector, cancellationToken); + } + + class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action Completed1Delegate = Completed1; + static readonly Action Completed2Delegate = Completed2; + static readonly Action Completed3Delegate = Completed3; + static readonly Action Completed4Delegate = Completed4; + static readonly Action Completed5Delegate = Completed5; + static readonly Action Completed6Delegate = Completed6; + static readonly Action Completed7Delegate = Completed7; + static readonly Action Completed8Delegate = Completed8; + static readonly Action Completed9Delegate = Completed9; + static readonly Action Completed10Delegate = Completed10; + static readonly Action Completed11Delegate = Completed11; + static readonly Action Completed12Delegate = Completed12; + static readonly Action Completed13Delegate = Completed13; + static readonly Action Completed14Delegate = Completed14; + const int CompleteCount = 14; + + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + readonly IUniTaskAsyncEnumerable source8; + readonly IUniTaskAsyncEnumerable source9; + readonly IUniTaskAsyncEnumerable source10; + readonly IUniTaskAsyncEnumerable source11; + readonly IUniTaskAsyncEnumerable source12; + readonly IUniTaskAsyncEnumerable source13; + readonly IUniTaskAsyncEnumerable source14; + + readonly Func resultSelector; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator1; + UniTask.Awaiter awaiter1; + bool hasCurrent1; + bool running1; + T1 current1; + + IUniTaskAsyncEnumerator enumerator2; + UniTask.Awaiter awaiter2; + bool hasCurrent2; + bool running2; + T2 current2; + + IUniTaskAsyncEnumerator enumerator3; + UniTask.Awaiter awaiter3; + bool hasCurrent3; + bool running3; + T3 current3; + + IUniTaskAsyncEnumerator enumerator4; + UniTask.Awaiter awaiter4; + bool hasCurrent4; + bool running4; + T4 current4; + + IUniTaskAsyncEnumerator enumerator5; + UniTask.Awaiter awaiter5; + bool hasCurrent5; + bool running5; + T5 current5; + + IUniTaskAsyncEnumerator enumerator6; + UniTask.Awaiter awaiter6; + bool hasCurrent6; + bool running6; + T6 current6; + + IUniTaskAsyncEnumerator enumerator7; + UniTask.Awaiter awaiter7; + bool hasCurrent7; + bool running7; + T7 current7; + + IUniTaskAsyncEnumerator enumerator8; + UniTask.Awaiter awaiter8; + bool hasCurrent8; + bool running8; + T8 current8; + + IUniTaskAsyncEnumerator enumerator9; + UniTask.Awaiter awaiter9; + bool hasCurrent9; + bool running9; + T9 current9; + + IUniTaskAsyncEnumerator enumerator10; + UniTask.Awaiter awaiter10; + bool hasCurrent10; + bool running10; + T10 current10; + + IUniTaskAsyncEnumerator enumerator11; + UniTask.Awaiter awaiter11; + bool hasCurrent11; + bool running11; + T11 current11; + + IUniTaskAsyncEnumerator enumerator12; + UniTask.Awaiter awaiter12; + bool hasCurrent12; + bool running12; + T12 current12; + + IUniTaskAsyncEnumerator enumerator13; + UniTask.Awaiter awaiter13; + bool hasCurrent13; + bool running13; + T13 current13; + + IUniTaskAsyncEnumerator enumerator14; + UniTask.Awaiter awaiter14; + bool hasCurrent14; + bool running14; + T14 current14; + + int completedCount; + bool syncRunning; + TResult result; + + public _CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, IUniTaskAsyncEnumerable source11, IUniTaskAsyncEnumerable source12, IUniTaskAsyncEnumerable source13, IUniTaskAsyncEnumerable source14, Func resultSelector, CancellationToken cancellationToken) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + this.source8 = source8; + this.source9 = source9; + this.source10 = source10; + this.source11 = source11; + this.source12 = source12; + this.source13 = source13; + this.source14 = source14; + + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current => result; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + if (completedCount == CompleteCount) return CompletedTasks.False; + + if (enumerator1 == null) + { + enumerator1 = source1.GetAsyncEnumerator(cancellationToken); + enumerator2 = source2.GetAsyncEnumerator(cancellationToken); + enumerator3 = source3.GetAsyncEnumerator(cancellationToken); + enumerator4 = source4.GetAsyncEnumerator(cancellationToken); + enumerator5 = source5.GetAsyncEnumerator(cancellationToken); + enumerator6 = source6.GetAsyncEnumerator(cancellationToken); + enumerator7 = source7.GetAsyncEnumerator(cancellationToken); + enumerator8 = source8.GetAsyncEnumerator(cancellationToken); + enumerator9 = source9.GetAsyncEnumerator(cancellationToken); + enumerator10 = source10.GetAsyncEnumerator(cancellationToken); + enumerator11 = source11.GetAsyncEnumerator(cancellationToken); + enumerator12 = source12.GetAsyncEnumerator(cancellationToken); + enumerator13 = source13.GetAsyncEnumerator(cancellationToken); + enumerator14 = source14.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + + AGAIN: + syncRunning = true; + if (!running1) + { + running1 = true; + awaiter1 = enumerator1.MoveNextAsync().GetAwaiter(); + if (awaiter1.IsCompleted) + { + Completed1(this); + } + else + { + awaiter1.SourceOnCompleted(Completed1Delegate, this); + } + } + if (!running2) + { + running2 = true; + awaiter2 = enumerator2.MoveNextAsync().GetAwaiter(); + if (awaiter2.IsCompleted) + { + Completed2(this); + } + else + { + awaiter2.SourceOnCompleted(Completed2Delegate, this); + } + } + if (!running3) + { + running3 = true; + awaiter3 = enumerator3.MoveNextAsync().GetAwaiter(); + if (awaiter3.IsCompleted) + { + Completed3(this); + } + else + { + awaiter3.SourceOnCompleted(Completed3Delegate, this); + } + } + if (!running4) + { + running4 = true; + awaiter4 = enumerator4.MoveNextAsync().GetAwaiter(); + if (awaiter4.IsCompleted) + { + Completed4(this); + } + else + { + awaiter4.SourceOnCompleted(Completed4Delegate, this); + } + } + if (!running5) + { + running5 = true; + awaiter5 = enumerator5.MoveNextAsync().GetAwaiter(); + if (awaiter5.IsCompleted) + { + Completed5(this); + } + else + { + awaiter5.SourceOnCompleted(Completed5Delegate, this); + } + } + if (!running6) + { + running6 = true; + awaiter6 = enumerator6.MoveNextAsync().GetAwaiter(); + if (awaiter6.IsCompleted) + { + Completed6(this); + } + else + { + awaiter6.SourceOnCompleted(Completed6Delegate, this); + } + } + if (!running7) + { + running7 = true; + awaiter7 = enumerator7.MoveNextAsync().GetAwaiter(); + if (awaiter7.IsCompleted) + { + Completed7(this); + } + else + { + awaiter7.SourceOnCompleted(Completed7Delegate, this); + } + } + if (!running8) + { + running8 = true; + awaiter8 = enumerator8.MoveNextAsync().GetAwaiter(); + if (awaiter8.IsCompleted) + { + Completed8(this); + } + else + { + awaiter8.SourceOnCompleted(Completed8Delegate, this); + } + } + if (!running9) + { + running9 = true; + awaiter9 = enumerator9.MoveNextAsync().GetAwaiter(); + if (awaiter9.IsCompleted) + { + Completed9(this); + } + else + { + awaiter9.SourceOnCompleted(Completed9Delegate, this); + } + } + if (!running10) + { + running10 = true; + awaiter10 = enumerator10.MoveNextAsync().GetAwaiter(); + if (awaiter10.IsCompleted) + { + Completed10(this); + } + else + { + awaiter10.SourceOnCompleted(Completed10Delegate, this); + } + } + if (!running11) + { + running11 = true; + awaiter11 = enumerator11.MoveNextAsync().GetAwaiter(); + if (awaiter11.IsCompleted) + { + Completed11(this); + } + else + { + awaiter11.SourceOnCompleted(Completed11Delegate, this); + } + } + if (!running12) + { + running12 = true; + awaiter12 = enumerator12.MoveNextAsync().GetAwaiter(); + if (awaiter12.IsCompleted) + { + Completed12(this); + } + else + { + awaiter12.SourceOnCompleted(Completed12Delegate, this); + } + } + if (!running13) + { + running13 = true; + awaiter13 = enumerator13.MoveNextAsync().GetAwaiter(); + if (awaiter13.IsCompleted) + { + Completed13(this); + } + else + { + awaiter13.SourceOnCompleted(Completed13Delegate, this); + } + } + if (!running14) + { + running14 = true; + awaiter14 = enumerator14.MoveNextAsync().GetAwaiter(); + if (awaiter14.IsCompleted) + { + Completed14(this); + } + else + { + awaiter14.SourceOnCompleted(Completed14Delegate, this); + } + } + + if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7 || !running8 || !running9 || !running10 || !running11 || !running12 || !running13 || !running14) + { + goto AGAIN; + } + syncRunning = false; + + return new UniTask(this, completionSource.Version); + } + + static void Completed1(object state) + { + var self = (_CombineLatest)state; + self.running1 = false; + + try + { + if (self.awaiter1.GetResult()) + { + self.hasCurrent1 = true; + self.current1 = self.enumerator1.Current; + goto SUCCESS; + } + else + { + self.running1 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running1 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running1 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter1.SourceOnCompleted(Completed1Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed2(object state) + { + var self = (_CombineLatest)state; + self.running2 = false; + + try + { + if (self.awaiter2.GetResult()) + { + self.hasCurrent2 = true; + self.current2 = self.enumerator2.Current; + goto SUCCESS; + } + else + { + self.running2 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running2 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running2 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter2.SourceOnCompleted(Completed2Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed3(object state) + { + var self = (_CombineLatest)state; + self.running3 = false; + + try + { + if (self.awaiter3.GetResult()) + { + self.hasCurrent3 = true; + self.current3 = self.enumerator3.Current; + goto SUCCESS; + } + else + { + self.running3 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running3 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running3 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter3.SourceOnCompleted(Completed3Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed4(object state) + { + var self = (_CombineLatest)state; + self.running4 = false; + + try + { + if (self.awaiter4.GetResult()) + { + self.hasCurrent4 = true; + self.current4 = self.enumerator4.Current; + goto SUCCESS; + } + else + { + self.running4 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running4 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running4 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter4.SourceOnCompleted(Completed4Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed5(object state) + { + var self = (_CombineLatest)state; + self.running5 = false; + + try + { + if (self.awaiter5.GetResult()) + { + self.hasCurrent5 = true; + self.current5 = self.enumerator5.Current; + goto SUCCESS; + } + else + { + self.running5 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running5 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running5 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter5.SourceOnCompleted(Completed5Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed6(object state) + { + var self = (_CombineLatest)state; + self.running6 = false; + + try + { + if (self.awaiter6.GetResult()) + { + self.hasCurrent6 = true; + self.current6 = self.enumerator6.Current; + goto SUCCESS; + } + else + { + self.running6 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running6 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running6 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter6.SourceOnCompleted(Completed6Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed7(object state) + { + var self = (_CombineLatest)state; + self.running7 = false; + + try + { + if (self.awaiter7.GetResult()) + { + self.hasCurrent7 = true; + self.current7 = self.enumerator7.Current; + goto SUCCESS; + } + else + { + self.running7 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running7 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running7 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter7.SourceOnCompleted(Completed7Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed8(object state) + { + var self = (_CombineLatest)state; + self.running8 = false; + + try + { + if (self.awaiter8.GetResult()) + { + self.hasCurrent8 = true; + self.current8 = self.enumerator8.Current; + goto SUCCESS; + } + else + { + self.running8 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running8 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running8 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter8 = self.enumerator8.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter8.SourceOnCompleted(Completed8Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed9(object state) + { + var self = (_CombineLatest)state; + self.running9 = false; + + try + { + if (self.awaiter9.GetResult()) + { + self.hasCurrent9 = true; + self.current9 = self.enumerator9.Current; + goto SUCCESS; + } + else + { + self.running9 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running9 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running9 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter9 = self.enumerator9.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter9.SourceOnCompleted(Completed9Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed10(object state) + { + var self = (_CombineLatest)state; + self.running10 = false; + + try + { + if (self.awaiter10.GetResult()) + { + self.hasCurrent10 = true; + self.current10 = self.enumerator10.Current; + goto SUCCESS; + } + else + { + self.running10 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running10 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running10 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter10 = self.enumerator10.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter10.SourceOnCompleted(Completed10Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed11(object state) + { + var self = (_CombineLatest)state; + self.running11 = false; + + try + { + if (self.awaiter11.GetResult()) + { + self.hasCurrent11 = true; + self.current11 = self.enumerator11.Current; + goto SUCCESS; + } + else + { + self.running11 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running11 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running11 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter11 = self.enumerator11.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter11.SourceOnCompleted(Completed11Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed12(object state) + { + var self = (_CombineLatest)state; + self.running12 = false; + + try + { + if (self.awaiter12.GetResult()) + { + self.hasCurrent12 = true; + self.current12 = self.enumerator12.Current; + goto SUCCESS; + } + else + { + self.running12 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running12 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running12 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter12 = self.enumerator12.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter12.SourceOnCompleted(Completed12Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed13(object state) + { + var self = (_CombineLatest)state; + self.running13 = false; + + try + { + if (self.awaiter13.GetResult()) + { + self.hasCurrent13 = true; + self.current13 = self.enumerator13.Current; + goto SUCCESS; + } + else + { + self.running13 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running13 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running13 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter13 = self.enumerator13.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter13.SourceOnCompleted(Completed13Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed14(object state) + { + var self = (_CombineLatest)state; + self.running14 = false; + + try + { + if (self.awaiter14.GetResult()) + { + self.hasCurrent14 = true; + self.current14 = self.enumerator14.Current; + goto SUCCESS; + } + else + { + self.running14 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running14 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running14 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter14 = self.enumerator14.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter14.SourceOnCompleted(Completed14Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + bool TrySetResult() + { + if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7 && hasCurrent8 && hasCurrent9 && hasCurrent10 && hasCurrent11 && hasCurrent12 && hasCurrent13 && hasCurrent14) + { + result = resultSelector(current1, current2, current3, current4, current5, current6, current7, current8, current9, current10, current11, current12, current13, current14); + completionSource.TrySetResult(true); + return true; + } + else + { + return false; + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator1 != null) + { + await enumerator1.DisposeAsync(); + } + if (enumerator2 != null) + { + await enumerator2.DisposeAsync(); + } + if (enumerator3 != null) + { + await enumerator3.DisposeAsync(); + } + if (enumerator4 != null) + { + await enumerator4.DisposeAsync(); + } + if (enumerator5 != null) + { + await enumerator5.DisposeAsync(); + } + if (enumerator6 != null) + { + await enumerator6.DisposeAsync(); + } + if (enumerator7 != null) + { + await enumerator7.DisposeAsync(); + } + if (enumerator8 != null) + { + await enumerator8.DisposeAsync(); + } + if (enumerator9 != null) + { + await enumerator9.DisposeAsync(); + } + if (enumerator10 != null) + { + await enumerator10.DisposeAsync(); + } + if (enumerator11 != null) + { + await enumerator11.DisposeAsync(); + } + if (enumerator12 != null) + { + await enumerator12.DisposeAsync(); + } + if (enumerator13 != null) + { + await enumerator13.DisposeAsync(); + } + if (enumerator14 != null) + { + await enumerator14.DisposeAsync(); + } + } + } + } + + internal class CombineLatest : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + readonly IUniTaskAsyncEnumerable source8; + readonly IUniTaskAsyncEnumerable source9; + readonly IUniTaskAsyncEnumerable source10; + readonly IUniTaskAsyncEnumerable source11; + readonly IUniTaskAsyncEnumerable source12; + readonly IUniTaskAsyncEnumerable source13; + readonly IUniTaskAsyncEnumerable source14; + readonly IUniTaskAsyncEnumerable source15; + + readonly Func resultSelector; + + public CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, IUniTaskAsyncEnumerable source11, IUniTaskAsyncEnumerable source12, IUniTaskAsyncEnumerable source13, IUniTaskAsyncEnumerable source14, IUniTaskAsyncEnumerable source15, Func resultSelector) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + this.source8 = source8; + this.source9 = source9; + this.source10 = source10; + this.source11 = source11; + this.source12 = source12; + this.source13 = source13; + this.source14 = source14; + this.source15 = source15; + + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, source12, source13, source14, source15, resultSelector, cancellationToken); + } + + class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action Completed1Delegate = Completed1; + static readonly Action Completed2Delegate = Completed2; + static readonly Action Completed3Delegate = Completed3; + static readonly Action Completed4Delegate = Completed4; + static readonly Action Completed5Delegate = Completed5; + static readonly Action Completed6Delegate = Completed6; + static readonly Action Completed7Delegate = Completed7; + static readonly Action Completed8Delegate = Completed8; + static readonly Action Completed9Delegate = Completed9; + static readonly Action Completed10Delegate = Completed10; + static readonly Action Completed11Delegate = Completed11; + static readonly Action Completed12Delegate = Completed12; + static readonly Action Completed13Delegate = Completed13; + static readonly Action Completed14Delegate = Completed14; + static readonly Action Completed15Delegate = Completed15; + const int CompleteCount = 15; + + readonly IUniTaskAsyncEnumerable source1; + readonly IUniTaskAsyncEnumerable source2; + readonly IUniTaskAsyncEnumerable source3; + readonly IUniTaskAsyncEnumerable source4; + readonly IUniTaskAsyncEnumerable source5; + readonly IUniTaskAsyncEnumerable source6; + readonly IUniTaskAsyncEnumerable source7; + readonly IUniTaskAsyncEnumerable source8; + readonly IUniTaskAsyncEnumerable source9; + readonly IUniTaskAsyncEnumerable source10; + readonly IUniTaskAsyncEnumerable source11; + readonly IUniTaskAsyncEnumerable source12; + readonly IUniTaskAsyncEnumerable source13; + readonly IUniTaskAsyncEnumerable source14; + readonly IUniTaskAsyncEnumerable source15; + + readonly Func resultSelector; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator1; + UniTask.Awaiter awaiter1; + bool hasCurrent1; + bool running1; + T1 current1; + + IUniTaskAsyncEnumerator enumerator2; + UniTask.Awaiter awaiter2; + bool hasCurrent2; + bool running2; + T2 current2; + + IUniTaskAsyncEnumerator enumerator3; + UniTask.Awaiter awaiter3; + bool hasCurrent3; + bool running3; + T3 current3; + + IUniTaskAsyncEnumerator enumerator4; + UniTask.Awaiter awaiter4; + bool hasCurrent4; + bool running4; + T4 current4; + + IUniTaskAsyncEnumerator enumerator5; + UniTask.Awaiter awaiter5; + bool hasCurrent5; + bool running5; + T5 current5; + + IUniTaskAsyncEnumerator enumerator6; + UniTask.Awaiter awaiter6; + bool hasCurrent6; + bool running6; + T6 current6; + + IUniTaskAsyncEnumerator enumerator7; + UniTask.Awaiter awaiter7; + bool hasCurrent7; + bool running7; + T7 current7; + + IUniTaskAsyncEnumerator enumerator8; + UniTask.Awaiter awaiter8; + bool hasCurrent8; + bool running8; + T8 current8; + + IUniTaskAsyncEnumerator enumerator9; + UniTask.Awaiter awaiter9; + bool hasCurrent9; + bool running9; + T9 current9; + + IUniTaskAsyncEnumerator enumerator10; + UniTask.Awaiter awaiter10; + bool hasCurrent10; + bool running10; + T10 current10; + + IUniTaskAsyncEnumerator enumerator11; + UniTask.Awaiter awaiter11; + bool hasCurrent11; + bool running11; + T11 current11; + + IUniTaskAsyncEnumerator enumerator12; + UniTask.Awaiter awaiter12; + bool hasCurrent12; + bool running12; + T12 current12; + + IUniTaskAsyncEnumerator enumerator13; + UniTask.Awaiter awaiter13; + bool hasCurrent13; + bool running13; + T13 current13; + + IUniTaskAsyncEnumerator enumerator14; + UniTask.Awaiter awaiter14; + bool hasCurrent14; + bool running14; + T14 current14; + + IUniTaskAsyncEnumerator enumerator15; + UniTask.Awaiter awaiter15; + bool hasCurrent15; + bool running15; + T15 current15; + + int completedCount; + bool syncRunning; + TResult result; + + public _CombineLatest(IUniTaskAsyncEnumerable source1, IUniTaskAsyncEnumerable source2, IUniTaskAsyncEnumerable source3, IUniTaskAsyncEnumerable source4, IUniTaskAsyncEnumerable source5, IUniTaskAsyncEnumerable source6, IUniTaskAsyncEnumerable source7, IUniTaskAsyncEnumerable source8, IUniTaskAsyncEnumerable source9, IUniTaskAsyncEnumerable source10, IUniTaskAsyncEnumerable source11, IUniTaskAsyncEnumerable source12, IUniTaskAsyncEnumerable source13, IUniTaskAsyncEnumerable source14, IUniTaskAsyncEnumerable source15, Func resultSelector, CancellationToken cancellationToken) + { + this.source1 = source1; + this.source2 = source2; + this.source3 = source3; + this.source4 = source4; + this.source5 = source5; + this.source6 = source6; + this.source7 = source7; + this.source8 = source8; + this.source9 = source9; + this.source10 = source10; + this.source11 = source11; + this.source12 = source12; + this.source13 = source13; + this.source14 = source14; + this.source15 = source15; + + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current => result; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + if (completedCount == CompleteCount) return CompletedTasks.False; + + if (enumerator1 == null) + { + enumerator1 = source1.GetAsyncEnumerator(cancellationToken); + enumerator2 = source2.GetAsyncEnumerator(cancellationToken); + enumerator3 = source3.GetAsyncEnumerator(cancellationToken); + enumerator4 = source4.GetAsyncEnumerator(cancellationToken); + enumerator5 = source5.GetAsyncEnumerator(cancellationToken); + enumerator6 = source6.GetAsyncEnumerator(cancellationToken); + enumerator7 = source7.GetAsyncEnumerator(cancellationToken); + enumerator8 = source8.GetAsyncEnumerator(cancellationToken); + enumerator9 = source9.GetAsyncEnumerator(cancellationToken); + enumerator10 = source10.GetAsyncEnumerator(cancellationToken); + enumerator11 = source11.GetAsyncEnumerator(cancellationToken); + enumerator12 = source12.GetAsyncEnumerator(cancellationToken); + enumerator13 = source13.GetAsyncEnumerator(cancellationToken); + enumerator14 = source14.GetAsyncEnumerator(cancellationToken); + enumerator15 = source15.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + + AGAIN: + syncRunning = true; + if (!running1) + { + running1 = true; + awaiter1 = enumerator1.MoveNextAsync().GetAwaiter(); + if (awaiter1.IsCompleted) + { + Completed1(this); + } + else + { + awaiter1.SourceOnCompleted(Completed1Delegate, this); + } + } + if (!running2) + { + running2 = true; + awaiter2 = enumerator2.MoveNextAsync().GetAwaiter(); + if (awaiter2.IsCompleted) + { + Completed2(this); + } + else + { + awaiter2.SourceOnCompleted(Completed2Delegate, this); + } + } + if (!running3) + { + running3 = true; + awaiter3 = enumerator3.MoveNextAsync().GetAwaiter(); + if (awaiter3.IsCompleted) + { + Completed3(this); + } + else + { + awaiter3.SourceOnCompleted(Completed3Delegate, this); + } + } + if (!running4) + { + running4 = true; + awaiter4 = enumerator4.MoveNextAsync().GetAwaiter(); + if (awaiter4.IsCompleted) + { + Completed4(this); + } + else + { + awaiter4.SourceOnCompleted(Completed4Delegate, this); + } + } + if (!running5) + { + running5 = true; + awaiter5 = enumerator5.MoveNextAsync().GetAwaiter(); + if (awaiter5.IsCompleted) + { + Completed5(this); + } + else + { + awaiter5.SourceOnCompleted(Completed5Delegate, this); + } + } + if (!running6) + { + running6 = true; + awaiter6 = enumerator6.MoveNextAsync().GetAwaiter(); + if (awaiter6.IsCompleted) + { + Completed6(this); + } + else + { + awaiter6.SourceOnCompleted(Completed6Delegate, this); + } + } + if (!running7) + { + running7 = true; + awaiter7 = enumerator7.MoveNextAsync().GetAwaiter(); + if (awaiter7.IsCompleted) + { + Completed7(this); + } + else + { + awaiter7.SourceOnCompleted(Completed7Delegate, this); + } + } + if (!running8) + { + running8 = true; + awaiter8 = enumerator8.MoveNextAsync().GetAwaiter(); + if (awaiter8.IsCompleted) + { + Completed8(this); + } + else + { + awaiter8.SourceOnCompleted(Completed8Delegate, this); + } + } + if (!running9) + { + running9 = true; + awaiter9 = enumerator9.MoveNextAsync().GetAwaiter(); + if (awaiter9.IsCompleted) + { + Completed9(this); + } + else + { + awaiter9.SourceOnCompleted(Completed9Delegate, this); + } + } + if (!running10) + { + running10 = true; + awaiter10 = enumerator10.MoveNextAsync().GetAwaiter(); + if (awaiter10.IsCompleted) + { + Completed10(this); + } + else + { + awaiter10.SourceOnCompleted(Completed10Delegate, this); + } + } + if (!running11) + { + running11 = true; + awaiter11 = enumerator11.MoveNextAsync().GetAwaiter(); + if (awaiter11.IsCompleted) + { + Completed11(this); + } + else + { + awaiter11.SourceOnCompleted(Completed11Delegate, this); + } + } + if (!running12) + { + running12 = true; + awaiter12 = enumerator12.MoveNextAsync().GetAwaiter(); + if (awaiter12.IsCompleted) + { + Completed12(this); + } + else + { + awaiter12.SourceOnCompleted(Completed12Delegate, this); + } + } + if (!running13) + { + running13 = true; + awaiter13 = enumerator13.MoveNextAsync().GetAwaiter(); + if (awaiter13.IsCompleted) + { + Completed13(this); + } + else + { + awaiter13.SourceOnCompleted(Completed13Delegate, this); + } + } + if (!running14) + { + running14 = true; + awaiter14 = enumerator14.MoveNextAsync().GetAwaiter(); + if (awaiter14.IsCompleted) + { + Completed14(this); + } + else + { + awaiter14.SourceOnCompleted(Completed14Delegate, this); + } + } + if (!running15) + { + running15 = true; + awaiter15 = enumerator15.MoveNextAsync().GetAwaiter(); + if (awaiter15.IsCompleted) + { + Completed15(this); + } + else + { + awaiter15.SourceOnCompleted(Completed15Delegate, this); + } + } + + if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7 || !running8 || !running9 || !running10 || !running11 || !running12 || !running13 || !running14 || !running15) + { + goto AGAIN; + } + syncRunning = false; + + return new UniTask(this, completionSource.Version); + } + + static void Completed1(object state) + { + var self = (_CombineLatest)state; + self.running1 = false; + + try + { + if (self.awaiter1.GetResult()) + { + self.hasCurrent1 = true; + self.current1 = self.enumerator1.Current; + goto SUCCESS; + } + else + { + self.running1 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running1 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running1 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter1.SourceOnCompleted(Completed1Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed2(object state) + { + var self = (_CombineLatest)state; + self.running2 = false; + + try + { + if (self.awaiter2.GetResult()) + { + self.hasCurrent2 = true; + self.current2 = self.enumerator2.Current; + goto SUCCESS; + } + else + { + self.running2 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running2 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running2 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter2.SourceOnCompleted(Completed2Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed3(object state) + { + var self = (_CombineLatest)state; + self.running3 = false; + + try + { + if (self.awaiter3.GetResult()) + { + self.hasCurrent3 = true; + self.current3 = self.enumerator3.Current; + goto SUCCESS; + } + else + { + self.running3 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running3 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running3 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter3.SourceOnCompleted(Completed3Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed4(object state) + { + var self = (_CombineLatest)state; + self.running4 = false; + + try + { + if (self.awaiter4.GetResult()) + { + self.hasCurrent4 = true; + self.current4 = self.enumerator4.Current; + goto SUCCESS; + } + else + { + self.running4 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running4 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running4 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter4.SourceOnCompleted(Completed4Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed5(object state) + { + var self = (_CombineLatest)state; + self.running5 = false; + + try + { + if (self.awaiter5.GetResult()) + { + self.hasCurrent5 = true; + self.current5 = self.enumerator5.Current; + goto SUCCESS; + } + else + { + self.running5 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running5 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running5 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter5.SourceOnCompleted(Completed5Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed6(object state) + { + var self = (_CombineLatest)state; + self.running6 = false; + + try + { + if (self.awaiter6.GetResult()) + { + self.hasCurrent6 = true; + self.current6 = self.enumerator6.Current; + goto SUCCESS; + } + else + { + self.running6 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running6 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running6 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter6.SourceOnCompleted(Completed6Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed7(object state) + { + var self = (_CombineLatest)state; + self.running7 = false; + + try + { + if (self.awaiter7.GetResult()) + { + self.hasCurrent7 = true; + self.current7 = self.enumerator7.Current; + goto SUCCESS; + } + else + { + self.running7 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running7 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running7 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter7.SourceOnCompleted(Completed7Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed8(object state) + { + var self = (_CombineLatest)state; + self.running8 = false; + + try + { + if (self.awaiter8.GetResult()) + { + self.hasCurrent8 = true; + self.current8 = self.enumerator8.Current; + goto SUCCESS; + } + else + { + self.running8 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running8 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running8 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter8 = self.enumerator8.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter8.SourceOnCompleted(Completed8Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed9(object state) + { + var self = (_CombineLatest)state; + self.running9 = false; + + try + { + if (self.awaiter9.GetResult()) + { + self.hasCurrent9 = true; + self.current9 = self.enumerator9.Current; + goto SUCCESS; + } + else + { + self.running9 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running9 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running9 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter9 = self.enumerator9.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter9.SourceOnCompleted(Completed9Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed10(object state) + { + var self = (_CombineLatest)state; + self.running10 = false; + + try + { + if (self.awaiter10.GetResult()) + { + self.hasCurrent10 = true; + self.current10 = self.enumerator10.Current; + goto SUCCESS; + } + else + { + self.running10 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running10 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running10 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter10 = self.enumerator10.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter10.SourceOnCompleted(Completed10Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed11(object state) + { + var self = (_CombineLatest)state; + self.running11 = false; + + try + { + if (self.awaiter11.GetResult()) + { + self.hasCurrent11 = true; + self.current11 = self.enumerator11.Current; + goto SUCCESS; + } + else + { + self.running11 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running11 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running11 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter11 = self.enumerator11.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter11.SourceOnCompleted(Completed11Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed12(object state) + { + var self = (_CombineLatest)state; + self.running12 = false; + + try + { + if (self.awaiter12.GetResult()) + { + self.hasCurrent12 = true; + self.current12 = self.enumerator12.Current; + goto SUCCESS; + } + else + { + self.running12 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running12 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running12 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter12 = self.enumerator12.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter12.SourceOnCompleted(Completed12Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed13(object state) + { + var self = (_CombineLatest)state; + self.running13 = false; + + try + { + if (self.awaiter13.GetResult()) + { + self.hasCurrent13 = true; + self.current13 = self.enumerator13.Current; + goto SUCCESS; + } + else + { + self.running13 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running13 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running13 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter13 = self.enumerator13.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter13.SourceOnCompleted(Completed13Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed14(object state) + { + var self = (_CombineLatest)state; + self.running14 = false; + + try + { + if (self.awaiter14.GetResult()) + { + self.hasCurrent14 = true; + self.current14 = self.enumerator14.Current; + goto SUCCESS; + } + else + { + self.running14 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running14 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running14 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter14 = self.enumerator14.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter14.SourceOnCompleted(Completed14Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + static void Completed15(object state) + { + var self = (_CombineLatest)state; + self.running15 = false; + + try + { + if (self.awaiter15.GetResult()) + { + self.hasCurrent15 = true; + self.current15 = self.enumerator15.Current; + goto SUCCESS; + } + else + { + self.running15 = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running15 = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running15 = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter15 = self.enumerator15.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter15.SourceOnCompleted(Completed15Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + + bool TrySetResult() + { + if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7 && hasCurrent8 && hasCurrent9 && hasCurrent10 && hasCurrent11 && hasCurrent12 && hasCurrent13 && hasCurrent14 && hasCurrent15) + { + result = resultSelector(current1, current2, current3, current4, current5, current6, current7, current8, current9, current10, current11, current12, current13, current14, current15); + completionSource.TrySetResult(true); + return true; + } + else + { + return false; + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator1 != null) + { + await enumerator1.DisposeAsync(); + } + if (enumerator2 != null) + { + await enumerator2.DisposeAsync(); + } + if (enumerator3 != null) + { + await enumerator3.DisposeAsync(); + } + if (enumerator4 != null) + { + await enumerator4.DisposeAsync(); + } + if (enumerator5 != null) + { + await enumerator5.DisposeAsync(); + } + if (enumerator6 != null) + { + await enumerator6.DisposeAsync(); + } + if (enumerator7 != null) + { + await enumerator7.DisposeAsync(); + } + if (enumerator8 != null) + { + await enumerator8.DisposeAsync(); + } + if (enumerator9 != null) + { + await enumerator9.DisposeAsync(); + } + if (enumerator10 != null) + { + await enumerator10.DisposeAsync(); + } + if (enumerator11 != null) + { + await enumerator11.DisposeAsync(); + } + if (enumerator12 != null) + { + await enumerator12.DisposeAsync(); + } + if (enumerator13 != null) + { + await enumerator13.DisposeAsync(); + } + if (enumerator14 != null) + { + await enumerator14.DisposeAsync(); + } + if (enumerator15 != null) + { + await enumerator15.DisposeAsync(); + } + } + } + } + +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/CombineLatest.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/CombineLatest.cs.meta new file mode 100644 index 0000000..4e8b1c3 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/CombineLatest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6cb07f6e88287e34d9b9301a572284a5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/CombineLatest.tt b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/CombineLatest.tt new file mode 100644 index 0000000..5692376 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/CombineLatest.tt @@ -0,0 +1,221 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".cs" #> +<# + var tMax = 15; + Func typeArgs = x => string.Join(", ", Enumerable.Range(1, x).Select(x => $"T{x}")) + ", TResult"; + Func paramArgs = x => string.Join(", ", Enumerable.Range(1, x).Select(x => $"IUniTaskAsyncEnumerable source{x}")); + Func parameters = x => string.Join(", ", Enumerable.Range(1, x).Select(x => $"source{x}")); + + +#> +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { +<# for(var i = 2; i <= tMax; i++) { #> + public static IUniTaskAsyncEnumerable CombineLatest<<#= typeArgs(i) #>>(this <#= paramArgs(i) #>, Func<<#= typeArgs(i) #>> resultSelector) + { +<# for(var j = 1; j <= i; j++) { #> + Error.ThrowArgumentNullException(source<#= j #>, nameof(source<#= j #>)); +<# } #> + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new CombineLatest<<#= typeArgs(i) #>>(<#= parameters(i) #>, resultSelector); + } + +<# } #> + } + +<# for(var i = 2; i <= tMax; i++) { #> + internal class CombineLatest<<#= typeArgs(i) #>> : IUniTaskAsyncEnumerable + { +<# for(var j = 1; j <= i; j++) { #> + readonly IUniTaskAsyncEnumerable> source<#= j #>; +<# } #> + readonly Func<<#= typeArgs(i) #>> resultSelector; + + public CombineLatest(<#= paramArgs(i) #>, Func<<#= typeArgs(i) #>> resultSelector) + { +<# for(var j = 1; j <= i; j++) { #> + this.source<#= j #> = source<#= j #>; +<# } #> + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _CombineLatest(<#= parameters(i) #>, resultSelector, cancellationToken); + } + + class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator + { +<# for(var j = 1; j <= i; j++) { #> + static readonly Action Completed<#= j #>Delegate = Completed<#= j #>; +<# } #> + const int CompleteCount = <#= i #>; + +<# for(var j = 1; j <= i; j++) { #> + readonly IUniTaskAsyncEnumerable> source<#= j #>; +<# } #> + readonly Func<<#= typeArgs(i) #>> resultSelector; + CancellationToken cancellationToken; + +<# for(var j = 1; j <= i; j++) { #> + IUniTaskAsyncEnumerator> enumerator<#= j #>; + UniTask.Awaiter awaiter<#= j #>; + bool hasCurrent<#= j #>; + bool running<#= j #>; + T<#= j #> current<#= j #>; + +<# } #> + int completedCount; + bool syncRunning; + TResult result; + + public _CombineLatest(<#= paramArgs(i) #>, Func<<#= typeArgs(i) #>> resultSelector, CancellationToken cancellationToken) + { +<# for(var j = 1; j <= i; j++) { #> + this.source<#= j #> = source<#= j #>; +<# } #> + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current => result; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + if (completedCount == CompleteCount) return CompletedTasks.False; + + if (enumerator1 == null) + { +<# for(var j = 1; j <= i; j++) { #> + enumerator<#= j #> = source<#= j #>.GetAsyncEnumerator(cancellationToken); +<# } #> + } + + completionSource.Reset(); + + AGAIN: + syncRunning = true; +<# for(var j = 1; j <= i; j++) { #> + if (!running<#= j #>) + { + running<#= j #> = true; + awaiter<#= j #> = enumerator<#= j #>.MoveNextAsync().GetAwaiter(); + if (awaiter<#= j #>.IsCompleted) + { + Completed<#= j #>(this); + } + else + { + awaiter<#= j #>.SourceOnCompleted(Completed<#= j #>Delegate, this); + } + } +<# } #> + + if (<#= string.Join(" || ", Enumerable.Range(1, i).Select(x => $"!running{x}")) #>) + { + goto AGAIN; + } + syncRunning = false; + + return new UniTask(this, completionSource.Version); + } + +<# for(var j = 1; j <= i; j++) { #> + static void Completed<#= j #>(object state) + { + var self = (_CombineLatest)state; + self.running<#= j #> = false; + + try + { + if (self.awaiter<#= j #>.GetResult()) + { + self.hasCurrent<#= j #> = true; + self.current<#= j #> = self.enumerator<#= j #>.Current; + goto SUCCESS; + } + else + { + self.running<#= j #> = true; // as complete, no more call MoveNextAsync. + if (Interlocked.Increment(ref self.completedCount) == CompleteCount) + { + goto COMPLETE; + } + return; + } + } + catch (Exception ex) + { + self.running<#= j #> = true; // as complete, no more call MoveNextAsync. + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + SUCCESS: + if (!self.TrySetResult()) + { + if (self.syncRunning) return; + self.running<#= j #> = true; // as complete, no more call MoveNextAsync. + try + { + self.awaiter<#= j #> = self.enumerator<#= j #>.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completedCount = CompleteCount; + self.completionSource.TrySetException(ex); + return; + } + + self.awaiter<#= j #>.SourceOnCompleted(Completed<#= j #>Delegate, self); + } + return; + COMPLETE: + self.completionSource.TrySetResult(false); + return; + } + +<# } #> + bool TrySetResult() + { + if (<#= string.Join(" && ", Enumerable.Range(1, i).Select(x => $"hasCurrent{x}")) #>) + { + result = resultSelector(<#= string.Join(", ", Enumerable.Range(1, i).Select(x => $"current{x}")) #>); + completionSource.TrySetResult(true); + return true; + } + else + { + return false; + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); +<# for(var j = 1; j <= i; j++) { #> + if (enumerator<#= j #> != null) + { + await enumerator<#= j #>.DisposeAsync(); + } +<# } #> + } + } + } + +<# } #> +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/CombineLatest.tt.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/CombineLatest.tt.meta new file mode 100644 index 0000000..c8e26d4 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/CombineLatest.tt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: b1b8cfa9d17af814a971ee2224aaaaa2 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Concat.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Concat.cs new file mode 100644 index 0000000..715795e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Concat.cs @@ -0,0 +1,164 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Concat(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second) + { + Error.ThrowArgumentNullException(first, nameof(first)); + Error.ThrowArgumentNullException(second, nameof(second)); + + return new Concat(first, second); + } + } + + internal sealed class Concat : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable first; + readonly IUniTaskAsyncEnumerable second; + + public Concat(IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second) + { + this.first = first; + this.second = second; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Concat(first, second, cancellationToken); + } + + sealed class _Concat : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action MoveNextCoreDelegate = MoveNextCore; + + enum IteratingState + { + IteratingFirst, + IteratingSecond, + Complete + } + + readonly IUniTaskAsyncEnumerable first; + readonly IUniTaskAsyncEnumerable second; + CancellationToken cancellationToken; + + IteratingState iteratingState; + + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + + public _Concat(IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, CancellationToken cancellationToken) + { + this.first = first; + this.second = second; + this.cancellationToken = cancellationToken; + this.iteratingState = IteratingState.IteratingFirst; + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + if (iteratingState == IteratingState.Complete) return CompletedTasks.False; + + completionSource.Reset(); + StartIterate(); + return new UniTask(this, completionSource.Version); + } + + void StartIterate() + { + if (enumerator == null) + { + if (iteratingState == IteratingState.IteratingFirst) + { + enumerator = first.GetAsyncEnumerator(cancellationToken); + } + else if (iteratingState == IteratingState.IteratingSecond) + { + enumerator = second.GetAsyncEnumerator(cancellationToken); + } + } + + try + { + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + + if (awaiter.IsCompleted) + { + MoveNextCoreDelegate(this); + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + + static void MoveNextCore(object state) + { + var self = (_Concat)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + self.Current = self.enumerator.Current; + self.completionSource.TrySetResult(true); + } + else + { + if (self.iteratingState == IteratingState.IteratingFirst) + { + self.RunSecondAfterDisposeAsync().Forget(); + return; + } + + self.iteratingState = IteratingState.Complete; + self.completionSource.TrySetResult(false); + } + } + } + + async UniTaskVoid RunSecondAfterDisposeAsync() + { + try + { + await enumerator.DisposeAsync(); + enumerator = null; + awaiter = default; + iteratingState = IteratingState.IteratingSecond; + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + + StartIterate(); + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Concat.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Concat.cs.meta new file mode 100644 index 0000000..6bfcf31 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Concat.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7cb9e19c449127a459851a135ce7d527 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Contains.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Contains.cs new file mode 100644 index 0000000..a93f566 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Contains.cs @@ -0,0 +1,50 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask ContainsAsync(this IUniTaskAsyncEnumerable source, TSource value, CancellationToken cancellationToken = default) + { + return ContainsAsync(source, value, EqualityComparer.Default, cancellationToken); + } + + public static UniTask ContainsAsync(this IUniTaskAsyncEnumerable source, TSource value, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return Contains.ContainsAsync(source, value, comparer, cancellationToken); + } + } + + internal static class Contains + { + internal static async UniTask ContainsAsync(IUniTaskAsyncEnumerable source, TSource value, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + if (comparer.Equals(value, e.Current)) + { + return true; + } + } + + return false; + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Contains.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Contains.cs.meta new file mode 100644 index 0000000..9bd414b --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Contains.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 36ab06d30f3223048b4f676e05431a7f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Count.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Count.cs new file mode 100644 index 0000000..807b529 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Count.cs @@ -0,0 +1,144 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask CountAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Count.CountAsync(source, cancellationToken); + } + + public static UniTask CountAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return Count.CountAsync(source, predicate, cancellationToken); + } + + public static UniTask CountAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return Count.CountAwaitAsync(source, predicate, cancellationToken); + } + + public static UniTask CountAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return Count.CountAwaitWithCancellationAsync(source, predicate, cancellationToken); + } + } + + internal static class Count + { + internal static async UniTask CountAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + var count = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked { count++; } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return count; + } + + internal static async UniTask CountAsync(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) + { + var count = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + if (predicate(e.Current)) + { + checked { count++; } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return count; + } + + internal static async UniTask CountAwaitAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + { + var count = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + if (await predicate(e.Current)) + { + checked { count++; } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return count; + } + + internal static async UniTask CountAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + { + var count = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + if (await predicate(e.Current, cancellationToken)) + { + checked { count++; } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return count; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Count.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Count.cs.meta new file mode 100644 index 0000000..35db332 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Count.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e606d38eed688574bb2ba89d983cc9bb +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Create.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Create.cs new file mode 100644 index 0000000..61b7afd --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Create.cs @@ -0,0 +1,174 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Create(Func, CancellationToken, UniTask> create) + { + Error.ThrowArgumentNullException(create, nameof(create)); + return new Create(create); + } + } + + public interface IAsyncWriter + { + UniTask YieldAsync(T value); + } + + internal sealed class Create : IUniTaskAsyncEnumerable + { + readonly Func, CancellationToken, UniTask> create; + + public Create(Func, CancellationToken, UniTask> create) + { + this.create = create; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Create(create, cancellationToken); + } + + sealed class _Create : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly Func, CancellationToken, UniTask> create; + readonly CancellationToken cancellationToken; + + int state = -1; + AsyncWriter writer; + + public _Create(Func, CancellationToken, UniTask> create, CancellationToken cancellationToken) + { + this.create = create; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public T Current { get; private set; } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + return default; + } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + try + { + switch (state) + { + case -1: // init + { + writer = new AsyncWriter(this); + RunWriterTask(create(writer, cancellationToken)).Forget(); + if (Volatile.Read(ref state) == -2) + { + return; // complete synchronously + } + state = 0; // wait YieldAsync, it set TrySetResult(true) + return; + } + case 0: + writer.SignalWriter(); + return; + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + } + + async UniTaskVoid RunWriterTask(UniTask task) + { + try + { + await task; + goto DONE; + } + catch (Exception ex) + { + Volatile.Write(ref state, -2); + completionSource.TrySetException(ex); + return; + } + + DONE: + Volatile.Write(ref state, -2); + completionSource.TrySetResult(false); + } + + public void SetResult(T value) + { + Current = value; + completionSource.TrySetResult(true); + } + } + + sealed class AsyncWriter : IUniTaskSource, IAsyncWriter + { + readonly _Create enumerator; + + UniTaskCompletionSourceCore core; + + public AsyncWriter(_Create enumerator) + { + this.enumerator = enumerator; + } + + public void GetResult(short token) + { + core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTask YieldAsync(T value) + { + core.Reset(); + enumerator.SetResult(value); + return new UniTask(this, core.Version); + } + + public void SignalWriter() + { + core.TrySetResult(AsyncUnit.Default); + } + } + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Create.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Create.cs.meta new file mode 100644 index 0000000..5aba456 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Create.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0202f723469f93945afa063bfb440d15 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/DefaultIfEmpty.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/DefaultIfEmpty.cs new file mode 100644 index 0000000..3d21bd7 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/DefaultIfEmpty.cs @@ -0,0 +1,142 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable DefaultIfEmpty(this IUniTaskAsyncEnumerable source) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new DefaultIfEmpty(source, default); + } + + public static IUniTaskAsyncEnumerable DefaultIfEmpty(this IUniTaskAsyncEnumerable source, TSource defaultValue) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new DefaultIfEmpty(source, defaultValue); + } + } + + internal sealed class DefaultIfEmpty : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly TSource defaultValue; + + public DefaultIfEmpty(IUniTaskAsyncEnumerable source, TSource defaultValue) + { + this.source = source; + this.defaultValue = defaultValue; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _DefaultIfEmpty(source, defaultValue, cancellationToken); + } + + sealed class _DefaultIfEmpty : MoveNextSource, IUniTaskAsyncEnumerator + { + enum IteratingState : byte + { + Empty, + Iterating, + Completed + } + + static readonly Action MoveNextCoreDelegate = MoveNextCore; + + readonly IUniTaskAsyncEnumerable source; + readonly TSource defaultValue; + CancellationToken cancellationToken; + + IteratingState iteratingState; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + + public _DefaultIfEmpty(IUniTaskAsyncEnumerable source, TSource defaultValue, CancellationToken cancellationToken) + { + this.source = source; + this.defaultValue = defaultValue; + this.cancellationToken = cancellationToken; + + this.iteratingState = IteratingState.Empty; + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (iteratingState == IteratingState.Completed) + { + return CompletedTasks.False; + } + + if (enumerator == null) + { + enumerator = source.GetAsyncEnumerator(cancellationToken); + } + + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + + if (awaiter.IsCompleted) + { + MoveNextCore(this); + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + + return new UniTask(this, completionSource.Version); + } + + static void MoveNextCore(object state) + { + var self = (_DefaultIfEmpty)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + self.iteratingState = IteratingState.Iterating; + self.Current = self.enumerator.Current; + self.completionSource.TrySetResult(true); + } + else + { + if (self.iteratingState == IteratingState.Empty) + { + self.iteratingState = IteratingState.Completed; + + self.Current = self.defaultValue; + self.completionSource.TrySetResult(true); + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + return default; + } + } + } + +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/DefaultIfEmpty.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/DefaultIfEmpty.cs.meta new file mode 100644 index 0000000..5aa5993 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/DefaultIfEmpty.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 19e437c039ad7e1478dbce1779ef8660 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Distinct.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Distinct.cs new file mode 100644 index 0000000..85bf795 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Distinct.cs @@ -0,0 +1,277 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Distinct(this IUniTaskAsyncEnumerable source) + { + return Distinct(source, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable Distinct(this IUniTaskAsyncEnumerable source, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new Distinct(source, comparer); + } + + public static IUniTaskAsyncEnumerable Distinct(this IUniTaskAsyncEnumerable source, Func keySelector) + { + return Distinct(source, keySelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable Distinct(this IUniTaskAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new Distinct(source, keySelector, comparer); + } + + public static IUniTaskAsyncEnumerable DistinctAwait(this IUniTaskAsyncEnumerable source, Func> keySelector) + { + return DistinctAwait(source, keySelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable DistinctAwait(this IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new DistinctAwait(source, keySelector, comparer); + } + + public static IUniTaskAsyncEnumerable DistinctAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector) + { + return DistinctAwaitWithCancellation(source, keySelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable DistinctAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new DistinctAwaitWithCancellation(source, keySelector, comparer); + } + } + + internal sealed class Distinct : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly IEqualityComparer comparer; + + public Distinct(IUniTaskAsyncEnumerable source, IEqualityComparer comparer) + { + this.source = source; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Distinct(source, comparer, cancellationToken); + } + + class _Distinct : AsyncEnumeratorBase + { + readonly HashSet set; + + public _Distinct(IUniTaskAsyncEnumerable source, IEqualityComparer comparer, CancellationToken cancellationToken) + + : base(source, cancellationToken) + { + this.set = new HashSet(comparer); + } + + protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result) + { + if (sourceHasCurrent) + { + var v = SourceCurrent; + if (set.Add(v)) + { + Current = v; + result = true; + return true; + } + else + { + result = default; + return false; + } + } + + result = false; + return true; + } + } + } + + internal sealed class Distinct : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func keySelector; + readonly IEqualityComparer comparer; + + public Distinct(IUniTaskAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) + { + this.source = source; + this.keySelector = keySelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Distinct(source, keySelector, comparer, cancellationToken); + } + + class _Distinct : AsyncEnumeratorBase + { + readonly HashSet set; + readonly Func keySelector; + + public _Distinct(IUniTaskAsyncEnumerable source, Func keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) + + : base(source, cancellationToken) + { + this.set = new HashSet(comparer); + this.keySelector = keySelector; + } + + protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result) + { + if (sourceHasCurrent) + { + var v = SourceCurrent; + if (set.Add(keySelector(v))) + { + Current = v; + result = true; + return true; + } + else + { + result = default; + return false; + } + } + + result = false; + return true; + } + } + } + + internal sealed class DistinctAwait : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> keySelector; + readonly IEqualityComparer comparer; + + public DistinctAwait(IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) + { + this.source = source; + this.keySelector = keySelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _DistinctAwait(source, keySelector, comparer, cancellationToken); + } + + class _DistinctAwait : AsyncEnumeratorAwaitSelectorBase + { + readonly HashSet set; + readonly Func> keySelector; + + public _DistinctAwait(IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) + + : base(source, cancellationToken) + { + this.set = new HashSet(comparer); + this.keySelector = keySelector; + } + + protected override UniTask TransformAsync(TSource sourceCurrent) + { + return keySelector(sourceCurrent); + } + + protected override bool TrySetCurrentCore(TKey awaitResult, out bool terminateIteration) + { + if (set.Add(awaitResult)) + { + Current = SourceCurrent; + terminateIteration = false; + return true; + } + else + { + terminateIteration = false; + return false; + } + } + } + } + + internal sealed class DistinctAwaitWithCancellation : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> keySelector; + readonly IEqualityComparer comparer; + + public DistinctAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) + { + this.source = source; + this.keySelector = keySelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _DistinctAwaitWithCancellation(source, keySelector, comparer, cancellationToken); + } + + class _DistinctAwaitWithCancellation : AsyncEnumeratorAwaitSelectorBase + { + readonly HashSet set; + readonly Func> keySelector; + + public _DistinctAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) + + : base(source, cancellationToken) + { + this.set = new HashSet(comparer); + this.keySelector = keySelector; + } + + protected override UniTask TransformAsync(TSource sourceCurrent) + { + return keySelector(sourceCurrent, cancellationToken); + } + + protected override bool TrySetCurrentCore(TKey awaitResult, out bool terminateIteration) + { + if (set.Add(awaitResult)) + { + Current = SourceCurrent; + terminateIteration = false; + return true; + } + else + { + terminateIteration = false; + return false; + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Distinct.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Distinct.cs.meta new file mode 100644 index 0000000..61804b7 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Distinct.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8f09903be66e5d943b243d7c19cb3811 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/DistinctUntilChanged.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/DistinctUntilChanged.cs new file mode 100644 index 0000000..d91bef9 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/DistinctUntilChanged.cs @@ -0,0 +1,662 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable DistinctUntilChanged(this IUniTaskAsyncEnumerable source) + { + return DistinctUntilChanged(source, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable DistinctUntilChanged(this IUniTaskAsyncEnumerable source, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new DistinctUntilChanged(source, comparer); + } + + public static IUniTaskAsyncEnumerable DistinctUntilChanged(this IUniTaskAsyncEnumerable source, Func keySelector) + { + return DistinctUntilChanged(source, keySelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable DistinctUntilChanged(this IUniTaskAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new DistinctUntilChanged(source, keySelector, comparer); + } + + public static IUniTaskAsyncEnumerable DistinctUntilChangedAwait(this IUniTaskAsyncEnumerable source, Func> keySelector) + { + return DistinctUntilChangedAwait(source, keySelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable DistinctUntilChangedAwait(this IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new DistinctUntilChangedAwait(source, keySelector, comparer); + } + + public static IUniTaskAsyncEnumerable DistinctUntilChangedAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector) + { + return DistinctUntilChangedAwaitWithCancellation(source, keySelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable DistinctUntilChangedAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new DistinctUntilChangedAwaitWithCancellation(source, keySelector, comparer); + } + } + + internal sealed class DistinctUntilChanged : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly IEqualityComparer comparer; + + public DistinctUntilChanged(IUniTaskAsyncEnumerable source, IEqualityComparer comparer) + { + this.source = source; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _DistinctUntilChanged(source, comparer, cancellationToken); + } + + sealed class _DistinctUntilChanged : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly IEqualityComparer comparer; + readonly CancellationToken cancellationToken; + + int state = -1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + Action moveNextAction; + + public _DistinctUntilChanged(IUniTaskAsyncEnumerable source, IEqualityComparer comparer, CancellationToken cancellationToken) + { + this.source = source; + this.comparer = comparer; + this.cancellationToken = cancellationToken; + this.moveNextAction = MoveNext; + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + REPEAT: + try + { + switch (state) + { + case -1: // init + enumerator = source.GetAsyncEnumerator(cancellationToken); + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case -3; + } + else + { + state = -3; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case -3: // first + if (awaiter.GetResult()) + { + Current = enumerator.Current; + goto CONTINUE; + } + else + { + goto DONE; + } + case 0: // normal + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case 1; + } + else + { + state = 1; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case 1: + if (awaiter.GetResult()) + { + var v = enumerator.Current; + if (!comparer.Equals(Current, v)) + { + Current = v; + goto CONTINUE; + } + else + { + state = 0; + goto REPEAT; + } + } + else + { + goto DONE; + } + case -2: + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + + CONTINUE: + state = 0; + completionSource.TrySetResult(true); + return; + } + + public UniTask DisposeAsync() + { + return enumerator.DisposeAsync(); + } + } + } + + internal sealed class DistinctUntilChanged : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func keySelector; + readonly IEqualityComparer comparer; + + public DistinctUntilChanged(IUniTaskAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) + { + this.source = source; + this.keySelector = keySelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _DistinctUntilChanged(source, keySelector, comparer, cancellationToken); + } + + sealed class _DistinctUntilChanged : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly Func keySelector; + readonly IEqualityComparer comparer; + readonly CancellationToken cancellationToken; + + int state = -1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + Action moveNextAction; + TKey prev; + + public _DistinctUntilChanged(IUniTaskAsyncEnumerable source, Func keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + this.source = source; + this.keySelector = keySelector; + this.comparer = comparer; + this.cancellationToken = cancellationToken; + this.moveNextAction = MoveNext; + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + REPEAT: + try + { + switch (state) + { + case -1: // init + enumerator = source.GetAsyncEnumerator(cancellationToken); + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case -3; + } + else + { + state = -3; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case -3: // first + if (awaiter.GetResult()) + { + Current = enumerator.Current; + goto CONTINUE; + } + else + { + goto DONE; + } + case 0: // normal + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case 1; + } + else + { + state = 1; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case 1: + if (awaiter.GetResult()) + { + var v = enumerator.Current; + var key = keySelector(v); + if (!comparer.Equals(prev, key)) + { + prev = key; + Current = v; + goto CONTINUE; + } + else + { + state = 0; + goto REPEAT; + } + } + else + { + goto DONE; + } + case -2: + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + + CONTINUE: + state = 0; + completionSource.TrySetResult(true); + return; + } + + public UniTask DisposeAsync() + { + return enumerator.DisposeAsync(); + } + } + } + + internal sealed class DistinctUntilChangedAwait : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> keySelector; + readonly IEqualityComparer comparer; + + public DistinctUntilChangedAwait(IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) + { + this.source = source; + this.keySelector = keySelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _DistinctUntilChangedAwait(source, keySelector, comparer, cancellationToken); + } + + sealed class _DistinctUntilChangedAwait : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> keySelector; + readonly IEqualityComparer comparer; + readonly CancellationToken cancellationToken; + + int state = -1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + UniTask.Awaiter awaiter2; + Action moveNextAction; + TSource enumeratorCurrent; + TKey prev; + + public _DistinctUntilChangedAwait(IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + this.source = source; + this.keySelector = keySelector; + this.comparer = comparer; + this.cancellationToken = cancellationToken; + this.moveNextAction = MoveNext; + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + REPEAT: + try + { + switch (state) + { + case -1: // init + enumerator = source.GetAsyncEnumerator(cancellationToken); + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case -3; + } + else + { + state = -3; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case -3: // first + if (awaiter.GetResult()) + { + Current = enumerator.Current; + goto CONTINUE; + } + else + { + goto DONE; + } + case 0: // normal + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case 1; + } + else + { + state = 1; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case 1: + if (awaiter.GetResult()) + { + enumeratorCurrent = enumerator.Current; + awaiter2 = keySelector(enumeratorCurrent).GetAwaiter(); + if (awaiter2.IsCompleted) + { + goto case 2; + } + else + { + state = 2; + awaiter2.UnsafeOnCompleted(moveNextAction); + return; + } + } + else + { + goto DONE; + } + case 2: + var key = awaiter2.GetResult(); + if (!comparer.Equals(prev, key)) + { + prev = key; + Current = enumeratorCurrent; + goto CONTINUE; + } + else + { + state = 0; + goto REPEAT; + } + case -2: + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + + CONTINUE: + state = 0; + completionSource.TrySetResult(true); + return; + } + + public UniTask DisposeAsync() + { + return enumerator.DisposeAsync(); + } + } + } + + internal sealed class DistinctUntilChangedAwaitWithCancellation : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> keySelector; + readonly IEqualityComparer comparer; + + public DistinctUntilChangedAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) + { + this.source = source; + this.keySelector = keySelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _DistinctUntilChangedAwaitWithCancellation(source, keySelector, comparer, cancellationToken); + } + + sealed class _DistinctUntilChangedAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> keySelector; + readonly IEqualityComparer comparer; + readonly CancellationToken cancellationToken; + + int state = -1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + UniTask.Awaiter awaiter2; + Action moveNextAction; + TSource enumeratorCurrent; + TKey prev; + + public _DistinctUntilChangedAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + this.source = source; + this.keySelector = keySelector; + this.comparer = comparer; + this.cancellationToken = cancellationToken; + this.moveNextAction = MoveNext; + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + REPEAT: + try + { + switch (state) + { + case -1: // init + enumerator = source.GetAsyncEnumerator(cancellationToken); + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case -3; + } + else + { + state = -3; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case -3: // first + if (awaiter.GetResult()) + { + Current = enumerator.Current; + goto CONTINUE; + } + else + { + goto DONE; + } + case 0: // normal + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case 1; + } + else + { + state = 1; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case 1: + if (awaiter.GetResult()) + { + enumeratorCurrent = enumerator.Current; + awaiter2 = keySelector(enumeratorCurrent, cancellationToken).GetAwaiter(); + if (awaiter2.IsCompleted) + { + goto case 2; + } + else + { + state = 2; + awaiter2.UnsafeOnCompleted(moveNextAction); + return; + } + } + else + { + goto DONE; + } + case 2: + var key = awaiter2.GetResult(); + if (!comparer.Equals(prev, key)) + { + prev = key; + Current = enumeratorCurrent; + goto CONTINUE; + } + else + { + state = 0; + goto REPEAT; + } + case -2: + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + + CONTINUE: + state = 0; + completionSource.TrySetResult(true); + return; + } + + public UniTask DisposeAsync() + { + return enumerator.DisposeAsync(); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/DistinctUntilChanged.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/DistinctUntilChanged.cs.meta new file mode 100644 index 0000000..84cddf8 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/DistinctUntilChanged.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0351f6767df7e644b935d4d599968162 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Do.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Do.cs new file mode 100644 index 0000000..f6df368 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Do.cs @@ -0,0 +1,258 @@ +using Cysharp.Threading.Tasks; +using Cysharp.Threading.Tasks.Internal; +using Cysharp.Threading.Tasks.Linq; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Do(this IUniTaskAsyncEnumerable source, Action onNext) + { + Error.ThrowArgumentNullException(source, nameof(source)); + return source.Do(onNext, null, null); + } + + public static IUniTaskAsyncEnumerable Do(this IUniTaskAsyncEnumerable source, Action onNext, Action onError) + { + Error.ThrowArgumentNullException(source, nameof(source)); + return source.Do(onNext, onError, null); + } + + public static IUniTaskAsyncEnumerable Do(this IUniTaskAsyncEnumerable source, Action onNext, Action onCompleted) + { + Error.ThrowArgumentNullException(source, nameof(source)); + return source.Do(onNext, null, onCompleted); + } + + public static IUniTaskAsyncEnumerable Do(this IUniTaskAsyncEnumerable source, Action onNext, Action onError, Action onCompleted) + { + Error.ThrowArgumentNullException(source, nameof(source)); + return new Do(source, onNext, onError, onCompleted); + } + + public static IUniTaskAsyncEnumerable Do(this IUniTaskAsyncEnumerable source, IObserver observer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(observer, nameof(observer)); + + return source.Do(observer.OnNext, observer.OnError, observer.OnCompleted); // alloc delegate. + } + + // not yet impl. + + //public static IUniTaskAsyncEnumerable DoAwait(this IUniTaskAsyncEnumerable source, Func onNext) + //{ + // throw new NotImplementedException(); + //} + + //public static IUniTaskAsyncEnumerable DoAwait(this IUniTaskAsyncEnumerable source, Func onNext, Func onError) + //{ + // throw new NotImplementedException(); + //} + + //public static IUniTaskAsyncEnumerable DoAwait(this IUniTaskAsyncEnumerable source, Func onNext, Func onCompleted) + //{ + // throw new NotImplementedException(); + //} + + //public static IUniTaskAsyncEnumerable DoAwait(this IUniTaskAsyncEnumerable source, Func onNext, Func onError, Func onCompleted) + //{ + // throw new NotImplementedException(); + //} + + //public static IUniTaskAsyncEnumerable DoAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func onNext) + //{ + // throw new NotImplementedException(); + //} + + //public static IUniTaskAsyncEnumerable DoAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func onNext, Func onError) + //{ + // throw new NotImplementedException(); + //} + + //public static IUniTaskAsyncEnumerable DoAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func onNext, Func onCompleted) + //{ + // throw new NotImplementedException(); + //} + + //public static IUniTaskAsyncEnumerable DoAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func onNext, Func onError, Func onCompleted) + //{ + // throw new NotImplementedException(); + //} + } + + internal sealed class Do : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Action onNext; + readonly Action onError; + readonly Action onCompleted; + + public Do(IUniTaskAsyncEnumerable source, Action onNext, Action onError, Action onCompleted) + { + this.source = source; + this.onNext = onNext; + this.onError = onError; + this.onCompleted = onCompleted; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Do(source, onNext, onError, onCompleted, cancellationToken); + } + + sealed class _Do : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action MoveNextCoreDelegate = MoveNextCore; + + readonly IUniTaskAsyncEnumerable source; + readonly Action onNext; + readonly Action onError; + readonly Action onCompleted; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + + public _Do(IUniTaskAsyncEnumerable source, Action onNext, Action onError, Action onCompleted, CancellationToken cancellationToken) + { + this.source = source; + this.onNext = onNext; + this.onError = onError; + this.onCompleted = onCompleted; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + completionSource.Reset(); + + bool isCompleted = false; + try + { + if (enumerator == null) + { + enumerator = source.GetAsyncEnumerator(cancellationToken); + } + + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + isCompleted = awaiter.IsCompleted; + } + catch (Exception ex) + { + CallTrySetExceptionAfterNotification(ex); + return new UniTask(this, completionSource.Version); + } + + if (isCompleted) + { + MoveNextCore(this); + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + + return new UniTask(this, completionSource.Version); + } + + void CallTrySetExceptionAfterNotification(Exception ex) + { + if (onError != null) + { + try + { + onError(ex); + } + catch (Exception ex2) + { + completionSource.TrySetException(ex2); + return; + } + } + + completionSource.TrySetException(ex); + } + + bool TryGetResultWithNotification(UniTask.Awaiter awaiter, out T result) + { + try + { + result = awaiter.GetResult(); + return true; + } + catch (Exception ex) + { + CallTrySetExceptionAfterNotification(ex); + result = default; + return false; + } + } + + + static void MoveNextCore(object state) + { + var self = (_Do)state; + + if (self.TryGetResultWithNotification(self.awaiter, out var result)) + { + if (result) + { + var v = self.enumerator.Current; + + if (self.onNext != null) + { + try + { + self.onNext(v); + } + catch (Exception ex) + { + self.CallTrySetExceptionAfterNotification(ex); + } + } + + self.Current = v; + self.completionSource.TrySetResult(true); + } + else + { + if (self.onCompleted != null) + { + try + { + self.onCompleted(); + } + catch (Exception ex) + { + self.CallTrySetExceptionAfterNotification(ex); + return; + } + } + + self.completionSource.TrySetResult(false); + } + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + return default; + } + } + } + +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Do.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Do.cs.meta new file mode 100644 index 0000000..766bbb5 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Do.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dd83c8e12dedf75409b829b93146d130 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ElementAt.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ElementAt.cs new file mode 100644 index 0000000..930675e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ElementAt.cs @@ -0,0 +1,58 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask ElementAtAsync(this IUniTaskAsyncEnumerable source, int index, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return ElementAt.ElementAtAsync(source, index, cancellationToken, false); + } + + public static UniTask ElementAtOrDefaultAsync(this IUniTaskAsyncEnumerable source, int index, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return ElementAt.ElementAtAsync(source, index, cancellationToken, true); + } + } + + internal static class ElementAt + { + public static async UniTask ElementAtAsync(IUniTaskAsyncEnumerable source, int index, CancellationToken cancellationToken, bool defaultIfEmpty) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + int i = 0; + while (await e.MoveNextAsync()) + { + if (i++ == index) + { + return e.Current; + } + } + + if (defaultIfEmpty) + { + return default; + } + else + { + throw Error.ArgumentOutOfRange(nameof(index)); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ElementAt.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ElementAt.cs.meta new file mode 100644 index 0000000..fb0850b --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ElementAt.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c835bd2dd8555234c8919c7b8ef3b69a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Empty.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Empty.cs new file mode 100644 index 0000000..2f5b3a4 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Empty.cs @@ -0,0 +1,47 @@ +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Empty() + { + return Cysharp.Threading.Tasks.Linq.Empty.Instance; + } + } + + internal class Empty : IUniTaskAsyncEnumerable + { + public static readonly IUniTaskAsyncEnumerable Instance = new Empty(); + + Empty() + { + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return _Empty.Instance; + } + + class _Empty : IUniTaskAsyncEnumerator + { + public static readonly IUniTaskAsyncEnumerator Instance = new _Empty(); + + _Empty() + { + } + + public T Current => default; + + public UniTask MoveNextAsync() + { + return CompletedTasks.False; + } + + public UniTask DisposeAsync() + { + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Empty.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Empty.cs.meta new file mode 100644 index 0000000..bfa577a --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Empty.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4fa123ad6258abb4184721b719a13810 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Except.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Except.cs new file mode 100644 index 0000000..c405482 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Except.cs @@ -0,0 +1,116 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Except(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second) + { + Error.ThrowArgumentNullException(first, nameof(first)); + Error.ThrowArgumentNullException(second, nameof(second)); + + return new Except(first, second, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable Except(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(first, nameof(first)); + Error.ThrowArgumentNullException(second, nameof(second)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new Except(first, second, comparer); + } + } + + internal sealed class Except : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable first; + readonly IUniTaskAsyncEnumerable second; + readonly IEqualityComparer comparer; + + public Except(IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, IEqualityComparer comparer) + { + this.first = first; + this.second = second; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Except(first, second, comparer, cancellationToken); + } + + class _Except : AsyncEnumeratorBase + { + static Action HashSetAsyncCoreDelegate = HashSetAsyncCore; + + readonly IEqualityComparer comparer; + readonly IUniTaskAsyncEnumerable second; + + HashSet set; + UniTask>.Awaiter awaiter; + + public _Except(IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, IEqualityComparer comparer, CancellationToken cancellationToken) + + : base(first, cancellationToken) + { + this.second = second; + this.comparer = comparer; + } + + protected override bool OnFirstIteration() + { + if (set != null) return false; + + awaiter = second.ToHashSetAsync(cancellationToken).GetAwaiter(); + if (awaiter.IsCompleted) + { + set = awaiter.GetResult(); + SourceMoveNext(); + } + else + { + awaiter.SourceOnCompleted(HashSetAsyncCoreDelegate, this); + } + + return true; + } + + static void HashSetAsyncCore(object state) + { + var self = (_Except)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + self.set = result; + self.SourceMoveNext(); + } + } + + protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result) + { + if (sourceHasCurrent) + { + var v = SourceCurrent; + if (set.Add(v)) + { + Current = v; + result = true; + return true; + } + else + { + result = default; + return false; + } + } + + result = false; + return true; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Except.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Except.cs.meta new file mode 100644 index 0000000..f61a1aa --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Except.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 38c1c4129f59dcb49a5b864eaf4ec63c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/First.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/First.cs new file mode 100644 index 0000000..da5688b --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/First.cs @@ -0,0 +1,200 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask FirstAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return First.FirstAsync(source, cancellationToken, false); + } + + public static UniTask FirstAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return First.FirstAsync(source, predicate, cancellationToken, false); + } + + public static UniTask FirstAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return First.FirstAwaitAsync(source, predicate, cancellationToken, false); + } + + public static UniTask FirstAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return First.FirstAwaitWithCancellationAsync(source, predicate, cancellationToken, false); + } + + public static UniTask FirstOrDefaultAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return First.FirstAsync(source, cancellationToken, true); + } + + public static UniTask FirstOrDefaultAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return First.FirstAsync(source, predicate, cancellationToken, true); + } + + public static UniTask FirstOrDefaultAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return First.FirstAwaitAsync(source, predicate, cancellationToken, true); + } + + public static UniTask FirstOrDefaultAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return First.FirstAwaitWithCancellationAsync(source, predicate, cancellationToken, true); + } + } + + internal static class First + { + public static async UniTask FirstAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken, bool defaultIfEmpty) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + if (await e.MoveNextAsync()) + { + return e.Current; + } + else + { + if (defaultIfEmpty) + { + return default; + } + else + { + throw Error.NoElements(); + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTask FirstAsync(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken, bool defaultIfEmpty) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = e.Current; + if (predicate(v)) + { + return v; + } + } + + if (defaultIfEmpty) + { + return default; + } + else + { + throw Error.NoElements(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTask FirstAwaitAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken, bool defaultIfEmpty) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = e.Current; + if (await predicate(v)) + { + return v; + } + } + + if (defaultIfEmpty) + { + return default; + } + else + { + throw Error.NoElements(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTask FirstAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken, bool defaultIfEmpty) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = e.Current; + if (await predicate(v, cancellationToken)) + { + return v; + } + } + + if (defaultIfEmpty) + { + return default; + } + else + { + throw Error.NoElements(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/First.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/First.cs.meta new file mode 100644 index 0000000..6924307 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/First.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 417946e97e9eed84db6f840f57037ca6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ForEach.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ForEach.cs new file mode 100644 index 0000000..60f246d --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ForEach.cs @@ -0,0 +1,193 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask ForEachAsync(this IUniTaskAsyncEnumerable source, Action action, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(action, nameof(action)); + + return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAsync(source, action, cancellationToken); + } + + public static UniTask ForEachAsync(this IUniTaskAsyncEnumerable source, Action action, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(action, nameof(action)); + + return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAsync(source, action, cancellationToken); + } + + /// Obsolete(Error), Use Use ForEachAwaitAsync instead. + [Obsolete("Use ForEachAwaitAsync instead.", true)] + [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] + public static UniTask ForEachAsync(this IUniTaskAsyncEnumerable source, Func action, CancellationToken cancellationToken = default) + { + throw new NotSupportedException("Use ForEachAwaitAsync instead."); + } + + /// Obsolete(Error), Use Use ForEachAwaitAsync instead. + [Obsolete("Use ForEachAwaitAsync instead.", true)] + [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] + public static UniTask ForEachAsync(this IUniTaskAsyncEnumerable source, Func action, CancellationToken cancellationToken = default) + { + throw new NotSupportedException("Use ForEachAwaitAsync instead."); + } + + public static UniTask ForEachAwaitAsync(this IUniTaskAsyncEnumerable source, Func action, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(action, nameof(action)); + + return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAwaitAsync(source, action, cancellationToken); + } + + public static UniTask ForEachAwaitAsync(this IUniTaskAsyncEnumerable source, Func action, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(action, nameof(action)); + + return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAwaitAsync(source, action, cancellationToken); + } + + public static UniTask ForEachAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func action, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(action, nameof(action)); + + return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAwaitWithCancellationAsync(source, action, cancellationToken); + } + + public static UniTask ForEachAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func action, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(action, nameof(action)); + + return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAwaitWithCancellationAsync(source, action, cancellationToken); + } + } + + internal static class ForEach + { + public static async UniTask ForEachAsync(IUniTaskAsyncEnumerable source, Action action, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + action(e.Current); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTask ForEachAsync(IUniTaskAsyncEnumerable source, Action action, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + int index = 0; + while (await e.MoveNextAsync()) + { + action(e.Current, checked(index++)); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTask ForEachAwaitAsync(IUniTaskAsyncEnumerable source, Func action, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + await action(e.Current); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTask ForEachAwaitAsync(IUniTaskAsyncEnumerable source, Func action, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + int index = 0; + while (await e.MoveNextAsync()) + { + await action(e.Current, checked(index++)); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTask ForEachAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func action, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + await action(e.Current, cancellationToken); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTask ForEachAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func action, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + int index = 0; + while (await e.MoveNextAsync()) + { + await action(e.Current, checked(index++), cancellationToken); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ForEach.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ForEach.cs.meta new file mode 100644 index 0000000..5317756 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ForEach.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ca8d7f8177ba16140920af405aea3fd4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/GroupBy.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/GroupBy.cs new file mode 100644 index 0000000..b9460ae --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/GroupBy.cs @@ -0,0 +1,923 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + // Ix-Async returns IGrouping but it is competely waste, use standard IGrouping. + + public static IUniTaskAsyncEnumerable> GroupBy(this IUniTaskAsyncEnumerable source, Func keySelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + return new GroupBy(source, keySelector, x => x, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable> GroupBy(this IUniTaskAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + return new GroupBy(source, keySelector, x => x, comparer); + } + + public static IUniTaskAsyncEnumerable> GroupBy(this IUniTaskAsyncEnumerable source, Func keySelector, Func elementSelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + return new GroupBy(source, keySelector, elementSelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable> GroupBy(this IUniTaskAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + return new GroupBy(source, keySelector, elementSelector, comparer); + } + + public static IUniTaskAsyncEnumerable GroupBy(this IUniTaskAsyncEnumerable source, Func keySelector, Func, TResult> resultSelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + return new GroupBy(source, keySelector, x => x, resultSelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable GroupBy(this IUniTaskAsyncEnumerable source, Func keySelector, Func, TResult> resultSelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + return new GroupBy(source, keySelector, x => x, resultSelector, comparer); + } + + public static IUniTaskAsyncEnumerable GroupBy(this IUniTaskAsyncEnumerable source, Func keySelector, Func elementSelector, Func, TResult> resultSelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + return new GroupBy(source, keySelector, elementSelector, resultSelector, EqualityComparer.Default); + } + public static IUniTaskAsyncEnumerable GroupBy(this IUniTaskAsyncEnumerable source, Func keySelector, Func elementSelector, Func, TResult> resultSelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + return new GroupBy(source, keySelector, elementSelector, resultSelector, comparer); + } + + // await + + public static IUniTaskAsyncEnumerable> GroupByAwait(this IUniTaskAsyncEnumerable source, Func> keySelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + return new GroupByAwait(source, keySelector, x => UniTask.FromResult(x), EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable> GroupByAwait(this IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + return new GroupByAwait(source, keySelector, x => UniTask.FromResult(x), comparer); + } + + public static IUniTaskAsyncEnumerable> GroupByAwait(this IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + return new GroupByAwait(source, keySelector, elementSelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable> GroupByAwait(this IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + return new GroupByAwait(source, keySelector, elementSelector, comparer); + } + + public static IUniTaskAsyncEnumerable GroupByAwait(this IUniTaskAsyncEnumerable source, Func> keySelector, Func, UniTask> resultSelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + return new GroupByAwait(source, keySelector, x => UniTask.FromResult(x), resultSelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable GroupByAwait(this IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, Func, UniTask> resultSelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + return new GroupByAwait(source, keySelector, elementSelector, resultSelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable GroupByAwait(this IUniTaskAsyncEnumerable source, Func> keySelector, Func, UniTask> resultSelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + return new GroupByAwait(source, keySelector, x => UniTask.FromResult(x), resultSelector, comparer); + } + + public static IUniTaskAsyncEnumerable GroupByAwait(this IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, Func, UniTask> resultSelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + return new GroupByAwait(source, keySelector, elementSelector, resultSelector, comparer); + } + + // with ct + + public static IUniTaskAsyncEnumerable> GroupByAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + return new GroupByAwaitWithCancellation(source, keySelector, (x, _) => UniTask.FromResult(x), EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable> GroupByAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + return new GroupByAwaitWithCancellation(source, keySelector, (x, _) => UniTask.FromResult(x), comparer); + } + + public static IUniTaskAsyncEnumerable> GroupByAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + return new GroupByAwaitWithCancellation(source, keySelector, elementSelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable> GroupByAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + return new GroupByAwaitWithCancellation(source, keySelector, elementSelector, comparer); + } + + public static IUniTaskAsyncEnumerable GroupByAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector, Func, CancellationToken, UniTask> resultSelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + return new GroupByAwaitWithCancellation(source, keySelector, (x, _) => UniTask.FromResult(x), resultSelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable GroupByAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, Func, CancellationToken, UniTask> resultSelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + return new GroupByAwaitWithCancellation(source, keySelector, elementSelector, resultSelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable GroupByAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector, Func, CancellationToken, UniTask> resultSelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + return new GroupByAwaitWithCancellation(source, keySelector, (x, _) => UniTask.FromResult(x), resultSelector, comparer); + } + + public static IUniTaskAsyncEnumerable GroupByAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, Func, CancellationToken, UniTask> resultSelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + return new GroupByAwaitWithCancellation(source, keySelector, elementSelector, resultSelector, comparer); + } + } + + internal sealed class GroupBy : IUniTaskAsyncEnumerable> + { + readonly IUniTaskAsyncEnumerable source; + readonly Func keySelector; + readonly Func elementSelector; + readonly IEqualityComparer comparer; + + public GroupBy(IUniTaskAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer) + { + this.source = source; + this.keySelector = keySelector; + this.elementSelector = elementSelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator> GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _GroupBy(source, keySelector, elementSelector, comparer, cancellationToken); + } + + sealed class _GroupBy : MoveNextSource, IUniTaskAsyncEnumerator> + { + readonly IUniTaskAsyncEnumerable source; + readonly Func keySelector; + readonly Func elementSelector; + readonly IEqualityComparer comparer; + CancellationToken cancellationToken; + + IEnumerator> groupEnumerator; + + public _GroupBy(IUniTaskAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + this.source = source; + this.keySelector = keySelector; + this.elementSelector = elementSelector; + this.comparer = comparer; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public IGrouping Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (groupEnumerator == null) + { + CreateLookup().Forget(); + } + else + { + SourceMoveNext(); + } + return new UniTask(this, completionSource.Version); + } + + async UniTaskVoid CreateLookup() + { + try + { + var lookup = await source.ToLookupAsync(keySelector, elementSelector, comparer, cancellationToken); + groupEnumerator = lookup.GetEnumerator(); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + SourceMoveNext(); + } + + void SourceMoveNext() + { + try + { + if (groupEnumerator.MoveNext()) + { + Current = groupEnumerator.Current as IGrouping; + completionSource.TrySetResult(true); + } + else + { + completionSource.TrySetResult(false); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (groupEnumerator != null) + { + groupEnumerator.Dispose(); + } + + return default; + } + } + } + + internal sealed class GroupBy : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func keySelector; + readonly Func elementSelector; + readonly Func, TResult> resultSelector; + readonly IEqualityComparer comparer; + + public GroupBy(IUniTaskAsyncEnumerable source, Func keySelector, Func elementSelector, Func, TResult> resultSelector, IEqualityComparer comparer) + { + this.source = source; + this.keySelector = keySelector; + this.elementSelector = elementSelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _GroupBy(source, keySelector, elementSelector, resultSelector, comparer, cancellationToken); + } + + sealed class _GroupBy : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly Func keySelector; + readonly Func elementSelector; + readonly Func, TResult> resultSelector; + readonly IEqualityComparer comparer; + CancellationToken cancellationToken; + + IEnumerator> groupEnumerator; + + public _GroupBy(IUniTaskAsyncEnumerable source, Func keySelector, Func elementSelector, Func, TResult> resultSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + this.source = source; + this.keySelector = keySelector; + this.elementSelector = elementSelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (groupEnumerator == null) + { + CreateLookup().Forget(); + } + else + { + SourceMoveNext(); + } + return new UniTask(this, completionSource.Version); + } + + async UniTaskVoid CreateLookup() + { + try + { + var lookup = await source.ToLookupAsync(keySelector, elementSelector, comparer, cancellationToken); + groupEnumerator = lookup.GetEnumerator(); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + SourceMoveNext(); + } + + void SourceMoveNext() + { + try + { + if (groupEnumerator.MoveNext()) + { + var current = groupEnumerator.Current; + Current = resultSelector(current.Key, current); + completionSource.TrySetResult(true); + } + else + { + completionSource.TrySetResult(false); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (groupEnumerator != null) + { + groupEnumerator.Dispose(); + } + + return default; + } + } + } + + internal sealed class GroupByAwait : IUniTaskAsyncEnumerable> + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> keySelector; + readonly Func> elementSelector; + readonly IEqualityComparer comparer; + + public GroupByAwait(IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer) + { + this.source = source; + this.keySelector = keySelector; + this.elementSelector = elementSelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator> GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _GroupByAwait(source, keySelector, elementSelector, comparer, cancellationToken); + } + + sealed class _GroupByAwait : MoveNextSource, IUniTaskAsyncEnumerator> + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> keySelector; + readonly Func> elementSelector; + readonly IEqualityComparer comparer; + CancellationToken cancellationToken; + + IEnumerator> groupEnumerator; + + public _GroupByAwait(IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + this.source = source; + this.keySelector = keySelector; + this.elementSelector = elementSelector; + this.comparer = comparer; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public IGrouping Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (groupEnumerator == null) + { + CreateLookup().Forget(); + } + else + { + SourceMoveNext(); + } + return new UniTask(this, completionSource.Version); + } + + async UniTaskVoid CreateLookup() + { + try + { + var lookup = await source.ToLookupAwaitAsync(keySelector, elementSelector, comparer, cancellationToken); + groupEnumerator = lookup.GetEnumerator(); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + SourceMoveNext(); + } + + void SourceMoveNext() + { + try + { + if (groupEnumerator.MoveNext()) + { + Current = groupEnumerator.Current as IGrouping; + completionSource.TrySetResult(true); + } + else + { + completionSource.TrySetResult(false); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (groupEnumerator != null) + { + groupEnumerator.Dispose(); + } + + return default; + } + } + } + + internal sealed class GroupByAwait : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> keySelector; + readonly Func> elementSelector; + readonly Func, UniTask> resultSelector; + readonly IEqualityComparer comparer; + + public GroupByAwait(IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, Func, UniTask> resultSelector, IEqualityComparer comparer) + { + this.source = source; + this.keySelector = keySelector; + this.elementSelector = elementSelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _GroupByAwait(source, keySelector, elementSelector, resultSelector, comparer, cancellationToken); + } + + sealed class _GroupByAwait : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly static Action ResultSelectCoreDelegate = ResultSelectCore; + + readonly IUniTaskAsyncEnumerable source; + readonly Func> keySelector; + readonly Func> elementSelector; + readonly Func, UniTask> resultSelector; + readonly IEqualityComparer comparer; + CancellationToken cancellationToken; + + IEnumerator> groupEnumerator; + UniTask.Awaiter awaiter; + + public _GroupByAwait(IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, Func, UniTask> resultSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + this.source = source; + this.keySelector = keySelector; + this.elementSelector = elementSelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (groupEnumerator == null) + { + CreateLookup().Forget(); + } + else + { + SourceMoveNext(); + } + return new UniTask(this, completionSource.Version); + } + + async UniTaskVoid CreateLookup() + { + try + { + var lookup = await source.ToLookupAwaitAsync(keySelector, elementSelector, comparer, cancellationToken); + groupEnumerator = lookup.GetEnumerator(); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + SourceMoveNext(); + } + + void SourceMoveNext() + { + try + { + if (groupEnumerator.MoveNext()) + { + var current = groupEnumerator.Current; + + awaiter = resultSelector(current.Key, current).GetAwaiter(); + if (awaiter.IsCompleted) + { + ResultSelectCore(this); + } + else + { + awaiter.SourceOnCompleted(ResultSelectCoreDelegate, this); + } + return; + } + else + { + completionSource.TrySetResult(false); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + } + + static void ResultSelectCore(object state) + { + var self = (_GroupByAwait)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + self.Current = result; + self.completionSource.TrySetResult(true); + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (groupEnumerator != null) + { + groupEnumerator.Dispose(); + } + + return default; + } + } + } + + internal sealed class GroupByAwaitWithCancellation : IUniTaskAsyncEnumerable> + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> keySelector; + readonly Func> elementSelector; + readonly IEqualityComparer comparer; + + public GroupByAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer) + { + this.source = source; + this.keySelector = keySelector; + this.elementSelector = elementSelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator> GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _GroupByAwaitWithCancellation(source, keySelector, elementSelector, comparer, cancellationToken); + } + + sealed class _GroupByAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator> + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> keySelector; + readonly Func> elementSelector; + readonly IEqualityComparer comparer; + CancellationToken cancellationToken; + + IEnumerator> groupEnumerator; + + public _GroupByAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + this.source = source; + this.keySelector = keySelector; + this.elementSelector = elementSelector; + this.comparer = comparer; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public IGrouping Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (groupEnumerator == null) + { + CreateLookup().Forget(); + } + else + { + SourceMoveNext(); + } + return new UniTask(this, completionSource.Version); + } + + async UniTaskVoid CreateLookup() + { + try + { + var lookup = await source.ToLookupAwaitWithCancellationAsync(keySelector, elementSelector, comparer, cancellationToken); + groupEnumerator = lookup.GetEnumerator(); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + SourceMoveNext(); + } + + void SourceMoveNext() + { + try + { + if (groupEnumerator.MoveNext()) + { + Current = groupEnumerator.Current as IGrouping; + completionSource.TrySetResult(true); + } + else + { + completionSource.TrySetResult(false); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (groupEnumerator != null) + { + groupEnumerator.Dispose(); + } + + return default; + } + } + } + + internal sealed class GroupByAwaitWithCancellation : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> keySelector; + readonly Func> elementSelector; + readonly Func, CancellationToken, UniTask> resultSelector; + readonly IEqualityComparer comparer; + + public GroupByAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, Func, CancellationToken, UniTask> resultSelector, IEqualityComparer comparer) + { + this.source = source; + this.keySelector = keySelector; + this.elementSelector = elementSelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _GroupByAwaitWithCancellation(source, keySelector, elementSelector, resultSelector, comparer, cancellationToken); + } + + sealed class _GroupByAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly static Action ResultSelectCoreDelegate = ResultSelectCore; + + readonly IUniTaskAsyncEnumerable source; + readonly Func> keySelector; + readonly Func> elementSelector; + readonly Func, CancellationToken, UniTask> resultSelector; + readonly IEqualityComparer comparer; + CancellationToken cancellationToken; + + IEnumerator> groupEnumerator; + UniTask.Awaiter awaiter; + + public _GroupByAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, Func, CancellationToken, UniTask> resultSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + this.source = source; + this.keySelector = keySelector; + this.elementSelector = elementSelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (groupEnumerator == null) + { + CreateLookup().Forget(); + } + else + { + SourceMoveNext(); + } + return new UniTask(this, completionSource.Version); + } + + async UniTaskVoid CreateLookup() + { + try + { + var lookup = await source.ToLookupAwaitWithCancellationAsync(keySelector, elementSelector, comparer, cancellationToken); + groupEnumerator = lookup.GetEnumerator(); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + SourceMoveNext(); + } + + void SourceMoveNext() + { + try + { + if (groupEnumerator.MoveNext()) + { + var current = groupEnumerator.Current; + + awaiter = resultSelector(current.Key, current, cancellationToken).GetAwaiter(); + if (awaiter.IsCompleted) + { + ResultSelectCore(this); + } + else + { + awaiter.SourceOnCompleted(ResultSelectCoreDelegate, this); + } + return; + } + else + { + completionSource.TrySetResult(false); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + } + + static void ResultSelectCore(object state) + { + var self = (_GroupByAwaitWithCancellation)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + self.Current = result; + self.completionSource.TrySetResult(true); + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (groupEnumerator != null) + { + groupEnumerator.Dispose(); + } + + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/GroupBy.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/GroupBy.cs.meta new file mode 100644 index 0000000..1489701 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/GroupBy.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a2de80df1cc8a1240ab0ee7badd334d0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/GroupJoin.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/GroupJoin.cs new file mode 100644 index 0000000..607b221 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/GroupJoin.cs @@ -0,0 +1,612 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable GroupJoin(this IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func, TResult> resultSelector) + { + Error.ThrowArgumentNullException(outer, nameof(outer)); + Error.ThrowArgumentNullException(inner, nameof(inner)); + Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector)); + Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new GroupJoin(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable GroupJoin(this IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func, TResult> resultSelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(outer, nameof(outer)); + Error.ThrowArgumentNullException(inner, nameof(inner)); + Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector)); + Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new GroupJoin(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer); + } + + public static IUniTaskAsyncEnumerable GroupJoinAwait(this IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func, UniTask> resultSelector) + { + Error.ThrowArgumentNullException(outer, nameof(outer)); + Error.ThrowArgumentNullException(inner, nameof(inner)); + Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector)); + Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new GroupJoinAwait(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable GroupJoinAwait(this IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func, UniTask> resultSelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(outer, nameof(outer)); + Error.ThrowArgumentNullException(inner, nameof(inner)); + Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector)); + Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new GroupJoinAwait(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer); + } + + public static IUniTaskAsyncEnumerable GroupJoinAwaitWithCancellation(this IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func, CancellationToken, UniTask> resultSelector) + { + Error.ThrowArgumentNullException(outer, nameof(outer)); + Error.ThrowArgumentNullException(inner, nameof(inner)); + Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector)); + Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new GroupJoinAwaitWithCancellation(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable GroupJoinAwaitWithCancellation(this IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func, CancellationToken, UniTask> resultSelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(outer, nameof(outer)); + Error.ThrowArgumentNullException(inner, nameof(inner)); + Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector)); + Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new GroupJoinAwaitWithCancellation(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer); + } + + } + + internal sealed class GroupJoin : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable outer; + readonly IUniTaskAsyncEnumerable inner; + readonly Func outerKeySelector; + readonly Func innerKeySelector; + readonly Func, TResult> resultSelector; + readonly IEqualityComparer comparer; + + public GroupJoin(IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func, TResult> resultSelector, IEqualityComparer comparer) + { + this.outer = outer; + this.inner = inner; + this.outerKeySelector = outerKeySelector; + this.innerKeySelector = innerKeySelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _GroupJoin(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken); + } + + sealed class _GroupJoin : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action MoveNextCoreDelegate = MoveNextCore; + + readonly IUniTaskAsyncEnumerable outer; + readonly IUniTaskAsyncEnumerable inner; + readonly Func outerKeySelector; + readonly Func innerKeySelector; + readonly Func, TResult> resultSelector; + readonly IEqualityComparer comparer; + CancellationToken cancellationToken; + + ILookup lookup; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + + + public _GroupJoin(IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func, TResult> resultSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + this.outer = outer; + this.inner = inner; + this.outerKeySelector = outerKeySelector; + this.innerKeySelector = innerKeySelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (lookup == null) + { + CreateLookup().Forget(); + } + else + { + SourceMoveNext(); + } + return new UniTask(this, completionSource.Version); + } + + async UniTaskVoid CreateLookup() + { + try + { + lookup = await inner.ToLookupAsync(innerKeySelector, comparer, cancellationToken); + enumerator = outer.GetAsyncEnumerator(cancellationToken); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + SourceMoveNext(); + } + + void SourceMoveNext() + { + try + { + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + MoveNextCore(this); + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + static void MoveNextCore(object state) + { + var self = (_GroupJoin)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + var outer = self.enumerator.Current; + var key = self.outerKeySelector(outer); + var values = self.lookup[key]; + + self.Current = self.resultSelector(outer, values); + self.completionSource.TrySetResult(true); + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + + return default; + } + } + } + + internal sealed class GroupJoinAwait : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable outer; + readonly IUniTaskAsyncEnumerable inner; + readonly Func> outerKeySelector; + readonly Func> innerKeySelector; + readonly Func, UniTask> resultSelector; + readonly IEqualityComparer comparer; + + public GroupJoinAwait(IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func, UniTask> resultSelector, IEqualityComparer comparer) + { + this.outer = outer; + this.inner = inner; + this.outerKeySelector = outerKeySelector; + this.innerKeySelector = innerKeySelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _GroupJoinAwait(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken); + } + + sealed class _GroupJoinAwait : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action MoveNextCoreDelegate = MoveNextCore; + readonly static Action ResultSelectCoreDelegate = ResultSelectCore; + readonly static Action OuterKeySelectCoreDelegate = OuterKeySelectCore; + + readonly IUniTaskAsyncEnumerable outer; + readonly IUniTaskAsyncEnumerable inner; + readonly Func> outerKeySelector; + readonly Func> innerKeySelector; + readonly Func, UniTask> resultSelector; + readonly IEqualityComparer comparer; + CancellationToken cancellationToken; + + ILookup lookup; + IUniTaskAsyncEnumerator enumerator; + TOuter outerValue; + UniTask.Awaiter awaiter; + UniTask.Awaiter outerKeyAwaiter; + UniTask.Awaiter resultAwaiter; + + + public _GroupJoinAwait(IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func, UniTask> resultSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + this.outer = outer; + this.inner = inner; + this.outerKeySelector = outerKeySelector; + this.innerKeySelector = innerKeySelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (lookup == null) + { + CreateLookup().Forget(); + } + else + { + SourceMoveNext(); + } + return new UniTask(this, completionSource.Version); + } + + async UniTaskVoid CreateLookup() + { + try + { + lookup = await inner.ToLookupAwaitAsync(innerKeySelector, comparer, cancellationToken); + enumerator = outer.GetAsyncEnumerator(cancellationToken); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + SourceMoveNext(); + } + + void SourceMoveNext() + { + try + { + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + MoveNextCore(this); + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + static void MoveNextCore(object state) + { + var self = (_GroupJoinAwait)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + try + { + + self.outerValue = self.enumerator.Current; + self.outerKeyAwaiter = self.outerKeySelector(self.outerValue).GetAwaiter(); + if (self.outerKeyAwaiter.IsCompleted) + { + OuterKeySelectCore(self); + } + else + { + self.outerKeyAwaiter.SourceOnCompleted(OuterKeySelectCoreDelegate, self); + } + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + } + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + static void OuterKeySelectCore(object state) + { + var self = (_GroupJoinAwait)state; + + if (self.TryGetResult(self.outerKeyAwaiter, out var result)) + { + try + { + var values = self.lookup[result]; + self.resultAwaiter = self.resultSelector(self.outerValue, values).GetAwaiter(); + if (self.resultAwaiter.IsCompleted) + { + ResultSelectCore(self); + } + else + { + self.resultAwaiter.SourceOnCompleted(ResultSelectCoreDelegate, self); + } + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + } + } + } + + static void ResultSelectCore(object state) + { + var self = (_GroupJoinAwait)state; + + if (self.TryGetResult(self.resultAwaiter, out var result)) + { + self.Current = result; + self.completionSource.TrySetResult(true); + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + + return default; + } + } + } + + internal sealed class GroupJoinAwaitWithCancellation : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable outer; + readonly IUniTaskAsyncEnumerable inner; + readonly Func> outerKeySelector; + readonly Func> innerKeySelector; + readonly Func, CancellationToken, UniTask> resultSelector; + readonly IEqualityComparer comparer; + + public GroupJoinAwaitWithCancellation(IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func, CancellationToken, UniTask> resultSelector, IEqualityComparer comparer) + { + this.outer = outer; + this.inner = inner; + this.outerKeySelector = outerKeySelector; + this.innerKeySelector = innerKeySelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _GroupJoinAwaitWithCancellation(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken); + } + + sealed class _GroupJoinAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action MoveNextCoreDelegate = MoveNextCore; + readonly static Action ResultSelectCoreDelegate = ResultSelectCore; + readonly static Action OuterKeySelectCoreDelegate = OuterKeySelectCore; + + readonly IUniTaskAsyncEnumerable outer; + readonly IUniTaskAsyncEnumerable inner; + readonly Func> outerKeySelector; + readonly Func> innerKeySelector; + readonly Func, CancellationToken, UniTask> resultSelector; + readonly IEqualityComparer comparer; + CancellationToken cancellationToken; + + ILookup lookup; + IUniTaskAsyncEnumerator enumerator; + TOuter outerValue; + UniTask.Awaiter awaiter; + UniTask.Awaiter outerKeyAwaiter; + UniTask.Awaiter resultAwaiter; + + + public _GroupJoinAwaitWithCancellation(IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func, CancellationToken, UniTask> resultSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + this.outer = outer; + this.inner = inner; + this.outerKeySelector = outerKeySelector; + this.innerKeySelector = innerKeySelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (lookup == null) + { + CreateLookup().Forget(); + } + else + { + SourceMoveNext(); + } + return new UniTask(this, completionSource.Version); + } + + async UniTaskVoid CreateLookup() + { + try + { + lookup = await inner.ToLookupAwaitWithCancellationAsync(innerKeySelector, comparer, cancellationToken); + enumerator = outer.GetAsyncEnumerator(cancellationToken); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + SourceMoveNext(); + } + + void SourceMoveNext() + { + try + { + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + MoveNextCore(this); + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + static void MoveNextCore(object state) + { + var self = (_GroupJoinAwaitWithCancellation)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + try + { + + self.outerValue = self.enumerator.Current; + self.outerKeyAwaiter = self.outerKeySelector(self.outerValue, self.cancellationToken).GetAwaiter(); + if (self.outerKeyAwaiter.IsCompleted) + { + OuterKeySelectCore(self); + } + else + { + self.outerKeyAwaiter.SourceOnCompleted(OuterKeySelectCoreDelegate, self); + } + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + } + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + static void OuterKeySelectCore(object state) + { + var self = (_GroupJoinAwaitWithCancellation)state; + + if (self.TryGetResult(self.outerKeyAwaiter, out var result)) + { + try + { + var values = self.lookup[result]; + self.resultAwaiter = self.resultSelector(self.outerValue, values, self.cancellationToken).GetAwaiter(); + if (self.resultAwaiter.IsCompleted) + { + ResultSelectCore(self); + } + else + { + self.resultAwaiter.SourceOnCompleted(ResultSelectCoreDelegate, self); + } + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + } + } + } + + static void ResultSelectCore(object state) + { + var self = (_GroupJoinAwaitWithCancellation)state; + + if (self.TryGetResult(self.resultAwaiter, out var result)) + { + self.Current = result; + self.completionSource.TrySetResult(true); + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/GroupJoin.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/GroupJoin.cs.meta new file mode 100644 index 0000000..f171ed1 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/GroupJoin.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7bf7759d03bf3f64190d3ae83b182c2c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Intersect.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Intersect.cs new file mode 100644 index 0000000..3faf645 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Intersect.cs @@ -0,0 +1,117 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Intersect(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second) + { + Error.ThrowArgumentNullException(first, nameof(first)); + Error.ThrowArgumentNullException(second, nameof(second)); + + return new Intersect(first, second, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable Intersect(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(first, nameof(first)); + Error.ThrowArgumentNullException(second, nameof(second)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new Intersect(first, second, comparer); + } + } + + internal sealed class Intersect : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable first; + readonly IUniTaskAsyncEnumerable second; + readonly IEqualityComparer comparer; + + public Intersect(IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, IEqualityComparer comparer) + { + this.first = first; + this.second = second; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Intersect(first, second, comparer, cancellationToken); + } + + class _Intersect : AsyncEnumeratorBase + { + static Action HashSetAsyncCoreDelegate = HashSetAsyncCore; + + readonly IEqualityComparer comparer; + readonly IUniTaskAsyncEnumerable second; + + HashSet set; + UniTask>.Awaiter awaiter; + + public _Intersect(IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, IEqualityComparer comparer, CancellationToken cancellationToken) + + : base(first, cancellationToken) + { + this.second = second; + this.comparer = comparer; + } + + protected override bool OnFirstIteration() + { + if (set != null) return false; + + awaiter = second.ToHashSetAsync(cancellationToken).GetAwaiter(); + if (awaiter.IsCompleted) + { + set = awaiter.GetResult(); + SourceMoveNext(); + } + else + { + awaiter.SourceOnCompleted(HashSetAsyncCoreDelegate, this); + } + + return true; + } + + static void HashSetAsyncCore(object state) + { + var self = (_Intersect)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + self.set = result; + self.SourceMoveNext(); + } + } + + protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result) + { + if (sourceHasCurrent) + { + var v = SourceCurrent; + + if (set.Remove(v)) + { + Current = v; + result = true; + return true; + } + else + { + result = default; + return false; + } + } + + result = false; + return true; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Intersect.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Intersect.cs.meta new file mode 100644 index 0000000..28cf8e3 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Intersect.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 93999a70f5d57134bbe971f3e988c4f2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Join.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Join.cs new file mode 100644 index 0000000..2d80889 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Join.cs @@ -0,0 +1,728 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Join(this IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func resultSelector) + { + Error.ThrowArgumentNullException(outer, nameof(outer)); + Error.ThrowArgumentNullException(inner, nameof(inner)); + Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector)); + Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new Join(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable Join(this IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func resultSelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(outer, nameof(outer)); + Error.ThrowArgumentNullException(inner, nameof(inner)); + Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector)); + Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new Join(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer); + } + + public static IUniTaskAsyncEnumerable JoinAwait(this IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func> resultSelector) + { + Error.ThrowArgumentNullException(outer, nameof(outer)); + Error.ThrowArgumentNullException(inner, nameof(inner)); + Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector)); + Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new JoinAwait(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable JoinAwait(this IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func> resultSelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(outer, nameof(outer)); + Error.ThrowArgumentNullException(inner, nameof(inner)); + Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector)); + Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new JoinAwait(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer); + } + + public static IUniTaskAsyncEnumerable JoinAwaitWithCancellation(this IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func> resultSelector) + { + Error.ThrowArgumentNullException(outer, nameof(outer)); + Error.ThrowArgumentNullException(inner, nameof(inner)); + Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector)); + Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new JoinAwaitWithCancellation(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable JoinAwaitWithCancellation(this IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func> resultSelector, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(outer, nameof(outer)); + Error.ThrowArgumentNullException(inner, nameof(inner)); + Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector)); + Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new JoinAwaitWithCancellation(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer); + } + } + + internal sealed class Join : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable outer; + readonly IUniTaskAsyncEnumerable inner; + readonly Func outerKeySelector; + readonly Func innerKeySelector; + readonly Func resultSelector; + readonly IEqualityComparer comparer; + + public Join(IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func resultSelector, IEqualityComparer comparer) + { + this.outer = outer; + this.inner = inner; + this.outerKeySelector = outerKeySelector; + this.innerKeySelector = innerKeySelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Join(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken); + } + + sealed class _Join : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action MoveNextCoreDelegate = MoveNextCore; + + readonly IUniTaskAsyncEnumerable outer; + readonly IUniTaskAsyncEnumerable inner; + readonly Func outerKeySelector; + readonly Func innerKeySelector; + readonly Func resultSelector; + readonly IEqualityComparer comparer; + CancellationToken cancellationToken; + + ILookup lookup; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + TOuter currentOuterValue; + IEnumerator valueEnumerator; + + bool continueNext; + + public _Join(IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func resultSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + this.outer = outer; + this.inner = inner; + this.outerKeySelector = outerKeySelector; + this.innerKeySelector = innerKeySelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (lookup == null) + { + CreateInnerHashSet().Forget(); + } + else + { + SourceMoveNext(); + } + return new UniTask(this, completionSource.Version); + } + + async UniTaskVoid CreateInnerHashSet() + { + try + { + lookup = await inner.ToLookupAsync(innerKeySelector, comparer, cancellationToken); + enumerator = outer.GetAsyncEnumerator(cancellationToken); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + SourceMoveNext(); + } + + void SourceMoveNext() + { + try + { + LOOP: + if (valueEnumerator != null) + { + if (valueEnumerator.MoveNext()) + { + Current = resultSelector(currentOuterValue, valueEnumerator.Current); + goto TRY_SET_RESULT_TRUE; + } + else + { + valueEnumerator.Dispose(); + valueEnumerator = null; + } + } + + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + continueNext = true; + MoveNextCore(this); + if (continueNext) + { + continueNext = false; + goto LOOP; // avoid recursive + } + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + + return; + + TRY_SET_RESULT_TRUE: + completionSource.TrySetResult(true); + } + + + static void MoveNextCore(object state) + { + var self = (_Join)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + self.currentOuterValue = self.enumerator.Current; + var key = self.outerKeySelector(self.currentOuterValue); + self.valueEnumerator = self.lookup[key].GetEnumerator(); + + if (self.continueNext) + { + return; + } + else + { + self.SourceMoveNext(); + } + } + else + { + self.continueNext = false; + self.completionSource.TrySetResult(false); + } + } + else + { + self.continueNext = false; + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (valueEnumerator != null) + { + valueEnumerator.Dispose(); + } + + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + + return default; + } + } + } + + internal sealed class JoinAwait : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable outer; + readonly IUniTaskAsyncEnumerable inner; + readonly Func> outerKeySelector; + readonly Func> innerKeySelector; + readonly Func> resultSelector; + readonly IEqualityComparer comparer; + + public JoinAwait(IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func> resultSelector, IEqualityComparer comparer) + { + this.outer = outer; + this.inner = inner; + this.outerKeySelector = outerKeySelector; + this.innerKeySelector = innerKeySelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _JoinAwait(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken); + } + + sealed class _JoinAwait : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action MoveNextCoreDelegate = MoveNextCore; + static readonly Action OuterSelectCoreDelegate = OuterSelectCore; + static readonly Action ResultSelectCoreDelegate = ResultSelectCore; + + readonly IUniTaskAsyncEnumerable outer; + readonly IUniTaskAsyncEnumerable inner; + readonly Func> outerKeySelector; + readonly Func> innerKeySelector; + readonly Func> resultSelector; + readonly IEqualityComparer comparer; + CancellationToken cancellationToken; + + ILookup lookup; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + TOuter currentOuterValue; + IEnumerator valueEnumerator; + + UniTask.Awaiter resultAwaiter; + UniTask.Awaiter outerKeyAwaiter; + + bool continueNext; + + public _JoinAwait(IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func> resultSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + this.outer = outer; + this.inner = inner; + this.outerKeySelector = outerKeySelector; + this.innerKeySelector = innerKeySelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (lookup == null) + { + CreateInnerHashSet().Forget(); + } + else + { + SourceMoveNext(); + } + return new UniTask(this, completionSource.Version); + } + + async UniTaskVoid CreateInnerHashSet() + { + try + { + lookup = await inner.ToLookupAwaitAsync(innerKeySelector, comparer, cancellationToken); + enumerator = outer.GetAsyncEnumerator(cancellationToken); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + SourceMoveNext(); + } + + void SourceMoveNext() + { + try + { + LOOP: + if (valueEnumerator != null) + { + if (valueEnumerator.MoveNext()) + { + resultAwaiter = resultSelector(currentOuterValue, valueEnumerator.Current).GetAwaiter(); + if (resultAwaiter.IsCompleted) + { + ResultSelectCore(this); + } + else + { + resultAwaiter.SourceOnCompleted(ResultSelectCoreDelegate, this); + } + return; + } + else + { + valueEnumerator.Dispose(); + valueEnumerator = null; + } + } + + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + continueNext = true; + MoveNextCore(this); + if (continueNext) + { + continueNext = false; + goto LOOP; // avoid recursive + } + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + + static void MoveNextCore(object state) + { + var self = (_JoinAwait)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + self.currentOuterValue = self.enumerator.Current; + + self.outerKeyAwaiter = self.outerKeySelector(self.currentOuterValue).GetAwaiter(); + + if (self.outerKeyAwaiter.IsCompleted) + { + OuterSelectCore(self); + } + else + { + self.continueNext = false; + self.outerKeyAwaiter.SourceOnCompleted(OuterSelectCoreDelegate, self); + } + } + else + { + self.continueNext = false; + self.completionSource.TrySetResult(false); + } + } + else + { + self.continueNext = false; + } + } + + static void OuterSelectCore(object state) + { + var self = (_JoinAwait)state; + + if (self.TryGetResult(self.outerKeyAwaiter, out var key)) + { + self.valueEnumerator = self.lookup[key].GetEnumerator(); + + if (self.continueNext) + { + return; + } + else + { + self.SourceMoveNext(); + } + } + else + { + self.continueNext = false; + } + } + + static void ResultSelectCore(object state) + { + var self = (_JoinAwait)state; + + if (self.TryGetResult(self.resultAwaiter, out var result)) + { + self.Current = result; + self.completionSource.TrySetResult(true); + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (valueEnumerator != null) + { + valueEnumerator.Dispose(); + } + + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + + return default; + } + } + } + + internal sealed class JoinAwaitWithCancellation : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable outer; + readonly IUniTaskAsyncEnumerable inner; + readonly Func> outerKeySelector; + readonly Func> innerKeySelector; + readonly Func> resultSelector; + readonly IEqualityComparer comparer; + + public JoinAwaitWithCancellation(IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func> resultSelector, IEqualityComparer comparer) + { + this.outer = outer; + this.inner = inner; + this.outerKeySelector = outerKeySelector; + this.innerKeySelector = innerKeySelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _JoinAwaitWithCancellation(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken); + } + + sealed class _JoinAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action MoveNextCoreDelegate = MoveNextCore; + static readonly Action OuterSelectCoreDelegate = OuterSelectCore; + static readonly Action ResultSelectCoreDelegate = ResultSelectCore; + + readonly IUniTaskAsyncEnumerable outer; + readonly IUniTaskAsyncEnumerable inner; + readonly Func> outerKeySelector; + readonly Func> innerKeySelector; + readonly Func> resultSelector; + readonly IEqualityComparer comparer; + CancellationToken cancellationToken; + + ILookup lookup; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + TOuter currentOuterValue; + IEnumerator valueEnumerator; + + UniTask.Awaiter resultAwaiter; + UniTask.Awaiter outerKeyAwaiter; + + bool continueNext; + + public _JoinAwaitWithCancellation(IUniTaskAsyncEnumerable outer, IUniTaskAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func> resultSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + this.outer = outer; + this.inner = inner; + this.outerKeySelector = outerKeySelector; + this.innerKeySelector = innerKeySelector; + this.resultSelector = resultSelector; + this.comparer = comparer; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (lookup == null) + { + CreateInnerHashSet().Forget(); + } + else + { + SourceMoveNext(); + } + return new UniTask(this, completionSource.Version); + } + + async UniTaskVoid CreateInnerHashSet() + { + try + { + lookup = await inner.ToLookupAwaitWithCancellationAsync(innerKeySelector, comparer, cancellationToken: cancellationToken); + enumerator = outer.GetAsyncEnumerator(cancellationToken); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + SourceMoveNext(); + } + + void SourceMoveNext() + { + try + { + LOOP: + if (valueEnumerator != null) + { + if (valueEnumerator.MoveNext()) + { + resultAwaiter = resultSelector(currentOuterValue, valueEnumerator.Current, cancellationToken).GetAwaiter(); + if (resultAwaiter.IsCompleted) + { + ResultSelectCore(this); + } + else + { + resultAwaiter.SourceOnCompleted(ResultSelectCoreDelegate, this); + } + return; + } + else + { + valueEnumerator.Dispose(); + valueEnumerator = null; + } + } + + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + continueNext = true; + MoveNextCore(this); + if (continueNext) + { + continueNext = false; + goto LOOP; // avoid recursive + } + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + + static void MoveNextCore(object state) + { + var self = (_JoinAwaitWithCancellation)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + self.currentOuterValue = self.enumerator.Current; + + self.outerKeyAwaiter = self.outerKeySelector(self.currentOuterValue, self.cancellationToken).GetAwaiter(); + + if (self.outerKeyAwaiter.IsCompleted) + { + OuterSelectCore(self); + } + else + { + self.continueNext = false; + self.outerKeyAwaiter.SourceOnCompleted(OuterSelectCoreDelegate, self); + } + } + else + { + self.continueNext = false; + self.completionSource.TrySetResult(false); + } + } + else + { + self.continueNext = false; + } + } + + static void OuterSelectCore(object state) + { + var self = (_JoinAwaitWithCancellation)state; + + if (self.TryGetResult(self.outerKeyAwaiter, out var key)) + { + self.valueEnumerator = self.lookup[key].GetEnumerator(); + + if (self.continueNext) + { + return; + } + else + { + self.SourceMoveNext(); + } + } + else + { + self.continueNext = false; + } + } + + static void ResultSelectCore(object state) + { + var self = (_JoinAwaitWithCancellation)state; + + if (self.TryGetResult(self.resultAwaiter, out var result)) + { + self.Current = result; + self.completionSource.TrySetResult(true); + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (valueEnumerator != null) + { + valueEnumerator.Dispose(); + } + + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + + return default; + } + } + } + +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Join.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Join.cs.meta new file mode 100644 index 0000000..3ab1015 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Join.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dc4ff8cb6d7c9a64896f2f082124d6b3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Last.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Last.cs new file mode 100644 index 0000000..664bb27 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Last.cs @@ -0,0 +1,240 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask LastAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Last.LastAsync(source, cancellationToken, false); + } + + public static UniTask LastAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return Last.LastAsync(source, predicate, cancellationToken, false); + } + + public static UniTask LastAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return Last.LastAwaitAsync(source, predicate, cancellationToken, false); + } + + public static UniTask LastAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return Last.LastAwaitWithCancellationAsync(source, predicate, cancellationToken, false); + } + + public static UniTask LastOrDefaultAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Last.LastAsync(source, cancellationToken, true); + } + + public static UniTask LastOrDefaultAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return Last.LastAsync(source, predicate, cancellationToken, true); + } + + public static UniTask LastOrDefaultAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return Last.LastAwaitAsync(source, predicate, cancellationToken, true); + } + + public static UniTask LastOrDefaultAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return Last.LastAwaitWithCancellationAsync(source, predicate, cancellationToken, true); + } + } + + internal static class Last + { + public static async UniTask LastAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken, bool defaultIfEmpty) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + TSource value = default; + if (await e.MoveNextAsync()) + { + value = e.Current; + } + else + { + if (defaultIfEmpty) + { + return value; + } + else + { + throw Error.NoElements(); + } + } + + while (await e.MoveNextAsync()) + { + value = e.Current; + } + return value; + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTask LastAsync(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken, bool defaultIfEmpty) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + TSource value = default; + + bool found = false; + while (await e.MoveNextAsync()) + { + var v = e.Current; + if (predicate(v)) + { + found = true; + value = v; + } + } + + if (defaultIfEmpty) + { + return value; + } + else + { + if (found) + { + return value; + } + else + { + throw Error.NoElements(); + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTask LastAwaitAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken, bool defaultIfEmpty) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + TSource value = default; + + bool found = false; + while (await e.MoveNextAsync()) + { + var v = e.Current; + if (await predicate(v)) + { + found = true; + value = v; + } + } + + if (defaultIfEmpty) + { + return value; + } + else + { + if (found) + { + return value; + } + else + { + throw Error.NoElements(); + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTask LastAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken, bool defaultIfEmpty) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + TSource value = default; + + bool found = false; + while (await e.MoveNextAsync()) + { + var v = e.Current; + if (await predicate(v, cancellationToken)) + { + found = true; + value = v; + } + } + + if (defaultIfEmpty) + { + return value; + } + else + { + if (found) + { + return value; + } + else + { + throw Error.NoElements(); + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Last.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Last.cs.meta new file mode 100644 index 0000000..edfa124 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Last.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a0ccc93be1387fa4a975f06310127c11 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/LongCount.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/LongCount.cs new file mode 100644 index 0000000..78ae805 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/LongCount.cs @@ -0,0 +1,144 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask LongCountAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return LongCount.LongCountAsync(source, cancellationToken); + } + + public static UniTask LongCountAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return LongCount.LongCountAsync(source, predicate, cancellationToken); + } + + public static UniTask LongCountAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return LongCount.LongCountAwaitAsync(source, predicate, cancellationToken); + } + + public static UniTask LongCountAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return LongCount.LongCountAwaitWithCancellationAsync(source, predicate, cancellationToken); + } + } + + internal static class LongCount + { + internal static async UniTask LongCountAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + long count = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + checked { count++; } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return count; + } + + internal static async UniTask LongCountAsync(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) + { + long count = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + if (predicate(e.Current)) + { + checked { count++; } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return count; + } + + internal static async UniTask LongCountAwaitAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + { + long count = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + if (await predicate(e.Current)) + { + checked { count++; } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return count; + } + + internal static async UniTask LongCountAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + { + long count = 0; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + if (await predicate(e.Current, cancellationToken)) + { + checked { count++; } + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return count; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/LongCount.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/LongCount.cs.meta new file mode 100644 index 0000000..862c2bc --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/LongCount.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 198b39e58ced3ab4f97ccbe0916787d5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Max.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Max.cs new file mode 100644 index 0000000..d244a15 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Max.cs @@ -0,0 +1,200 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Max.MaxAsync(source, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitWithCancellationAsync(source, selector, cancellationToken); + } + } + + internal static partial class Max + { + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + TSource value = default; + var comparer = Comparer.Default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + + goto NEXT_LOOP; + } + + return value; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if (comparer.Compare(value, x) < 0) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + TResult value = default; + var comparer = Comparer.Default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + + goto NEXT_LOOP; + } + + return value; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if (comparer.Compare(value, x) < 0) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + TResult value = default; + var comparer = Comparer.Default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + + goto NEXT_LOOP; + } + + return value; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if (comparer.Compare(value, x) < 0) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + TResult value = default; + var comparer = Comparer.Default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + + goto NEXT_LOOP; + } + + return value; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if (comparer.Compare(value, x) < 0) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Max.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Max.cs.meta new file mode 100644 index 0000000..2125edf --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Max.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5c8a118a6b664c441820b8a87d7f6e28 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Min.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Min.cs new file mode 100644 index 0000000..8768a86 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Min.cs @@ -0,0 +1,200 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Min.MinAsync(source, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitWithCancellationAsync(source, selector, cancellationToken); + } + } + + internal static partial class Min + { + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + TSource value = default; + var comparer = Comparer.Default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + + goto NEXT_LOOP; + } + + return value; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if (comparer.Compare(value, x) > 0) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + TResult value = default; + var comparer = Comparer.Default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + + goto NEXT_LOOP; + } + + return value; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if (comparer.Compare(value, x) > 0) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + TResult value = default; + var comparer = Comparer.Default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + + goto NEXT_LOOP; + } + + return value; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if (comparer.Compare(value, x) > 0) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + TResult value = default; + var comparer = Comparer.Default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + + goto NEXT_LOOP; + } + + return value; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if (comparer.Compare(value, x) > 0) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Min.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Min.cs.meta new file mode 100644 index 0000000..91378dc --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Min.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 57ac9da21d3457849a8e45548290a508 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/MinMax.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/MinMax.cs new file mode 100644 index 0000000..aae3541 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/MinMax.cs @@ -0,0 +1,3763 @@ +using System; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Min.MinAsync(source, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Min.MinAsync(source, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Min.MinAsync(source, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Min.MinAsync(source, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Min.MinAsync(source, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Min.MinAsync(source, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Min.MinAsync(source, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Min.MinAsync(source, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Min.MinAsync(source, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Min.MinAsync(source, cancellationToken); + } + + public static UniTask MinAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MinAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Min.MinAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + } + + internal static partial class Min + { + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Int32 value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Int32 value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int32 value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int32 value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Int64 value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Int64 value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int64 value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int64 value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Single value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Single value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Single value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Single value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Double value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Double value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Double value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Double value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Decimal value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Decimal value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Decimal value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Decimal value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Int32? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Int32? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int32? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int32? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Int64? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Int64? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int64? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int64? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Single? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Single? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Single? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Single? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Double? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Double? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Double? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Double? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Decimal? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Decimal? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Decimal? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MinAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Decimal? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if( x == null) continue; + if (value > x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + } + + public static partial class UniTaskAsyncEnumerable + { + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Max.MaxAsync(source, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Max.MaxAsync(source, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Max.MaxAsync(source, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Max.MaxAsync(source, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Max.MaxAsync(source, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Max.MaxAsync(source, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Max.MaxAsync(source, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Max.MaxAsync(source, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Max.MaxAsync(source, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Max.MaxAsync(source, cancellationToken); + } + + public static UniTask MaxAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask MaxAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Max.MaxAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + } + + internal static partial class Max + { + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Int32 value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Int32 value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int32 value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int32 value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Int64 value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Int64 value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int64 value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int64 value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Single value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Single value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Single value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Single value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Double value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Double value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Double value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Double value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Decimal value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Decimal value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Decimal value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Decimal value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + + goto NEXT_LOOP; + } + + throw Error.NoElements(); + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Int32? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Int32? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int32? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int32? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Int64? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Int64? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int64? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int64? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Single? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Single? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Single? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Single? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Double? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Double? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Double? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Double? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Decimal? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Decimal? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Decimal? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask MaxAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Decimal? value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); + if(value == null) continue; + + goto NEXT_LOOP; + } + + return default; + + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); + if( x == null) continue; + if (value < x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + } + +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/MinMax.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/MinMax.cs.meta new file mode 100644 index 0000000..3856b65 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/MinMax.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2d6da02d9ab970e4999daf7147d98e36 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/MinMax.tt b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/MinMax.tt new file mode 100644 index 0000000..1a95890 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/MinMax.tt @@ -0,0 +1,262 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".cs" #> +<# + var types = new[] + { + typeof(int), + typeof(long), + typeof(float), + typeof(double), + typeof(decimal), + + typeof(int?), + typeof(long?), + typeof(float?), + typeof(double?), + typeof(decimal?), + }; + + Func IsNullable = x => x.IsGenericType; + Func TypeName = x => IsNullable(x) ? x.GetGenericArguments()[0].Name + "?" : x.Name; + Func WithSuffix = x => IsNullable(x) ? ".GetValueOrDefault()" : ""; +#> +using System; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks.Linq +{ +<# foreach(var (minMax, op) in new[]{("Min",">"), ("Max", "<")}) { #> + public static partial class UniTaskAsyncEnumerable + { +<# foreach(var t in types) { #> + public static UniTask<<#= TypeName(t) #>> <#= minMax #>Async(this IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return <#= minMax #>.<#= minMax #>Async(source, cancellationToken); + } + + public static UniTask<<#= TypeName(t) #>> <#= minMax #>Async(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return <#= minMax #>.<#= minMax #>Async(source, selector, cancellationToken); + } + + public static UniTask<<#= TypeName(t) #>> <#= minMax #>AwaitAsync(this IUniTaskAsyncEnumerable source, Func>> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return <#= minMax #>.<#= minMax #>AwaitAsync(source, selector, cancellationToken); + } + + public static UniTask<<#= TypeName(t) #>> <#= minMax #>AwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func>> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return <#= minMax #>.<#= minMax #>AwaitWithCancellationAsync(source, selector, cancellationToken); + } + +<# } #> + } + + internal static partial class <#= minMax #> + { +<# foreach(var t in types) { #> + public static async UniTask<<#= TypeName(t) #>> <#= minMax #>Async(IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken) + { + <#= TypeName(t) #> value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = e.Current; +<# if (IsNullable(t)) { #> + if(value == null) continue; +<# } #> + goto NEXT_LOOP; + } + +<# if (IsNullable(t)) { #> + return default; +<# } else { #> + throw Error.NoElements(); +<# } #> + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = e.Current; +<# if (IsNullable(t)) { #> + if( x == null) continue; +<# } #> + if (value <#= op #> x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask<<#= TypeName(t) #>> <#= minMax #>Async(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + <#= TypeName(t) #> value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = selector(e.Current); +<# if (IsNullable(t)) { #> + if(value == null) continue; +<# } #> + goto NEXT_LOOP; + } + +<# if (IsNullable(t)) { #> + return default; +<# } else { #> + throw Error.NoElements(); +<# } #> + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = selector(e.Current); +<# if (IsNullable(t)) { #> + if( x == null) continue; +<# } #> + if (value <#= op #> x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask<<#= TypeName(t) #>> <#= minMax #>AwaitAsync(IUniTaskAsyncEnumerable source, Func>> selector, CancellationToken cancellationToken) + { + <#= TypeName(t) #> value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current); +<# if (IsNullable(t)) { #> + if(value == null) continue; +<# } #> + goto NEXT_LOOP; + } + +<# if (IsNullable(t)) { #> + return default; +<# } else { #> + throw Error.NoElements(); +<# } #> + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current); +<# if (IsNullable(t)) { #> + if( x == null) continue; +<# } #> + if (value <#= op #> x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + + public static async UniTask<<#= TypeName(t) #>> <#= minMax #>AwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func>> selector, CancellationToken cancellationToken) + { + <#= TypeName(t) #> value = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + value = await selector(e.Current, cancellationToken); +<# if (IsNullable(t)) { #> + if(value == null) continue; +<# } #> + goto NEXT_LOOP; + } + +<# if (IsNullable(t)) { #> + return default; +<# } else { #> + throw Error.NoElements(); +<# } #> + NEXT_LOOP: + + while (await e.MoveNextAsync()) + { + var x = await selector(e.Current, cancellationToken); +<# if (IsNullable(t)) { #> + if( x == null) continue; +<# } #> + if (value <#= op #> x) + { + value = x; + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return value; + } + +<# } #> + } + +<# } #> +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/MinMax.tt.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/MinMax.tt.meta new file mode 100644 index 0000000..f77d718 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/MinMax.tt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 18108e9feb2ec40498df573cfef2ea15 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Never.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Never.cs new file mode 100644 index 0000000..2dbce71 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Never.cs @@ -0,0 +1,56 @@ +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Never() + { + return Cysharp.Threading.Tasks.Linq.Never.Instance; + } + } + + internal class Never : IUniTaskAsyncEnumerable + { + public static readonly IUniTaskAsyncEnumerable Instance = new Never(); + + Never() + { + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Never(cancellationToken); + } + + class _Never : IUniTaskAsyncEnumerator + { + CancellationToken cancellationToken; + + public _Never(CancellationToken cancellationToken) + { + this.cancellationToken = cancellationToken; + } + + public T Current => default; + + public UniTask MoveNextAsync() + { + var tcs = new UniTaskCompletionSource(); + + cancellationToken.Register(state => + { + var task = (UniTaskCompletionSource)state; + task.TrySetCanceled(cancellationToken); + }, tcs); + + return tcs.Task; + } + + public UniTask DisposeAsync() + { + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Never.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Never.cs.meta new file mode 100644 index 0000000..ba9d358 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Never.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8b307c3d3be71a94da251564bcdefa3d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/OfType.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/OfType.cs new file mode 100644 index 0000000..fea8069 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/OfType.cs @@ -0,0 +1,61 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable OfType(this IUniTaskAsyncEnumerable source) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new OfType(source); + } + } + + internal sealed class OfType : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + + public OfType(IUniTaskAsyncEnumerable source) + { + this.source = source; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _OfType(source, cancellationToken); + } + + class _OfType : AsyncEnumeratorBase + { + public _OfType(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + + : base(source, cancellationToken) + { + } + + protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result) + { + if (sourceHasCurrent) + { + if (SourceCurrent is TResult castCurent) + { + Current = castCurent; + result = true; + return true; + } + else + { + result = default; + return false; + } + } + + result = false; + return true; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/OfType.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/OfType.cs.meta new file mode 100644 index 0000000..6ace53f --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/OfType.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 111ffe87a7d700442a9ef5af554b252c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/OrderBy.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/OrderBy.cs new file mode 100644 index 0000000..d0c379f --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/OrderBy.cs @@ -0,0 +1,558 @@ +using Cysharp.Threading.Tasks; +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + #region OrderBy_OrderByDescending + + public static IUniTaskOrderedAsyncEnumerable OrderBy(this IUniTaskAsyncEnumerable source, Func keySelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return new OrderedAsyncEnumerable(source, keySelector, Comparer.Default, false, null); + } + + public static IUniTaskOrderedAsyncEnumerable OrderBy(this IUniTaskAsyncEnumerable source, Func keySelector, IComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new OrderedAsyncEnumerable(source, keySelector, comparer, false, null); + } + + public static IUniTaskOrderedAsyncEnumerable OrderByAwait(this IUniTaskAsyncEnumerable source, Func> keySelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return new OrderedAsyncEnumerableAwait(source, keySelector, Comparer.Default, false, null); + } + + public static IUniTaskOrderedAsyncEnumerable OrderByAwait(this IUniTaskAsyncEnumerable source, Func> keySelector, IComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new OrderedAsyncEnumerableAwait(source, keySelector, comparer, false, null); + } + + public static IUniTaskOrderedAsyncEnumerable OrderByAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return new OrderedAsyncEnumerableAwaitWithCancellation(source, keySelector, Comparer.Default, false, null); + } + + public static IUniTaskOrderedAsyncEnumerable OrderByAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector, IComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new OrderedAsyncEnumerableAwaitWithCancellation(source, keySelector, comparer, false, null); + } + + public static IUniTaskOrderedAsyncEnumerable OrderByDescending(this IUniTaskAsyncEnumerable source, Func keySelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return new OrderedAsyncEnumerable(source, keySelector, Comparer.Default, true, null); + } + + public static IUniTaskOrderedAsyncEnumerable OrderByDescending(this IUniTaskAsyncEnumerable source, Func keySelector, IComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new OrderedAsyncEnumerable(source, keySelector, comparer, true, null); + } + + public static IUniTaskOrderedAsyncEnumerable OrderByDescendingAwait(this IUniTaskAsyncEnumerable source, Func> keySelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return new OrderedAsyncEnumerableAwait(source, keySelector, Comparer.Default, true, null); + } + + public static IUniTaskOrderedAsyncEnumerable OrderByDescendingAwait(this IUniTaskAsyncEnumerable source, Func> keySelector, IComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new OrderedAsyncEnumerableAwait(source, keySelector, comparer, true, null); + } + + public static IUniTaskOrderedAsyncEnumerable OrderByDescendingAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return new OrderedAsyncEnumerableAwaitWithCancellation(source, keySelector, Comparer.Default, true, null); + } + + public static IUniTaskOrderedAsyncEnumerable OrderByDescendingAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector, IComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return new OrderedAsyncEnumerableAwaitWithCancellation(source, keySelector, comparer, true, null); + } + + #endregion + + #region ThenBy_ThenByDescending + + public static IUniTaskOrderedAsyncEnumerable ThenBy(this IUniTaskOrderedAsyncEnumerable source, Func keySelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return source.CreateOrderedEnumerable(keySelector, Comparer.Default, false); + } + + public static IUniTaskOrderedAsyncEnumerable ThenBy(this IUniTaskOrderedAsyncEnumerable source, Func keySelector, IComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return source.CreateOrderedEnumerable(keySelector, comparer, false); + } + + public static IUniTaskOrderedAsyncEnumerable ThenByAwait(this IUniTaskOrderedAsyncEnumerable source, Func> keySelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return source.CreateOrderedEnumerable(keySelector, Comparer.Default, false); + } + + public static IUniTaskOrderedAsyncEnumerable ThenByAwait(this IUniTaskOrderedAsyncEnumerable source, Func> keySelector, IComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return source.CreateOrderedEnumerable(keySelector, comparer, false); + } + + public static IUniTaskOrderedAsyncEnumerable ThenByAwaitWithCancellation(this IUniTaskOrderedAsyncEnumerable source, Func> keySelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return source.CreateOrderedEnumerable(keySelector, Comparer.Default, false); + } + + public static IUniTaskOrderedAsyncEnumerable ThenByAwaitWithCancellation(this IUniTaskOrderedAsyncEnumerable source, Func> keySelector, IComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return source.CreateOrderedEnumerable(keySelector, comparer, false); + } + + public static IUniTaskOrderedAsyncEnumerable ThenByDescending(this IUniTaskOrderedAsyncEnumerable source, Func keySelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return source.CreateOrderedEnumerable(keySelector, Comparer.Default, true); + } + + public static IUniTaskOrderedAsyncEnumerable ThenByDescending(this IUniTaskOrderedAsyncEnumerable source, Func keySelector, IComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return source.CreateOrderedEnumerable(keySelector, comparer, true); + } + + public static IUniTaskOrderedAsyncEnumerable ThenByDescendingAwait(this IUniTaskOrderedAsyncEnumerable source, Func> keySelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return source.CreateOrderedEnumerable(keySelector, Comparer.Default, true); + } + + public static IUniTaskOrderedAsyncEnumerable ThenByDescendingAwait(this IUniTaskOrderedAsyncEnumerable source, Func> keySelector, IComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return source.CreateOrderedEnumerable(keySelector, comparer, true); + } + + public static IUniTaskOrderedAsyncEnumerable ThenByDescendingAwaitWithCancellation(this IUniTaskOrderedAsyncEnumerable source, Func> keySelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return source.CreateOrderedEnumerable(keySelector, Comparer.Default, true); + } + + public static IUniTaskOrderedAsyncEnumerable ThenByDescendingAwaitWithCancellation(this IUniTaskOrderedAsyncEnumerable source, Func> keySelector, IComparer comparer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return source.CreateOrderedEnumerable(keySelector, comparer, true); + } + + #endregion + } + + internal abstract class AsyncEnumerableSorter + { + internal abstract UniTask ComputeKeysAsync(TElement[] elements, int count); + + internal abstract int CompareKeys(int index1, int index2); + + internal async UniTask SortAsync(TElement[] elements, int count) + { + await ComputeKeysAsync(elements, count); + + int[] map = new int[count]; + for (int i = 0; i < count; i++) map[i] = i; + QuickSort(map, 0, count - 1); + return map; + } + + void QuickSort(int[] map, int left, int right) + { + do + { + int i = left; + int j = right; + int x = map[i + ((j - i) >> 1)]; + do + { + while (i < map.Length && CompareKeys(x, map[i]) > 0) i++; + while (j >= 0 && CompareKeys(x, map[j]) < 0) j--; + if (i > j) break; + if (i < j) + { + int temp = map[i]; + map[i] = map[j]; + map[j] = temp; + } + i++; + j--; + } while (i <= j); + if (j - left <= right - i) + { + if (left < j) QuickSort(map, left, j); + left = i; + } + else + { + if (i < right) QuickSort(map, i, right); + right = j; + } + } while (left < right); + } + } + + internal class SyncSelectorAsyncEnumerableSorter : AsyncEnumerableSorter + { + readonly Func keySelector; + readonly IComparer comparer; + readonly bool descending; + readonly AsyncEnumerableSorter next; + TKey[] keys; + + internal SyncSelectorAsyncEnumerableSorter(Func keySelector, IComparer comparer, bool descending, AsyncEnumerableSorter next) + { + this.keySelector = keySelector; + this.comparer = comparer; + this.descending = descending; + this.next = next; + } + + internal override async UniTask ComputeKeysAsync(TElement[] elements, int count) + { + keys = new TKey[count]; + for (int i = 0; i < count; i++) keys[i] = keySelector(elements[i]); + if (next != null) await next.ComputeKeysAsync(elements, count); + } + + internal override int CompareKeys(int index1, int index2) + { + int c = comparer.Compare(keys[index1], keys[index2]); + if (c == 0) + { + if (next == null) return index1 - index2; + return next.CompareKeys(index1, index2); + } + return descending ? -c : c; + } + } + + internal class AsyncSelectorEnumerableSorter : AsyncEnumerableSorter + { + readonly Func> keySelector; + readonly IComparer comparer; + readonly bool descending; + readonly AsyncEnumerableSorter next; + TKey[] keys; + + internal AsyncSelectorEnumerableSorter(Func> keySelector, IComparer comparer, bool descending, AsyncEnumerableSorter next) + { + this.keySelector = keySelector; + this.comparer = comparer; + this.descending = descending; + this.next = next; + } + + internal override async UniTask ComputeKeysAsync(TElement[] elements, int count) + { + keys = new TKey[count]; + for (int i = 0; i < count; i++) keys[i] = await keySelector(elements[i]); + if (next != null) await next.ComputeKeysAsync(elements, count); + } + + internal override int CompareKeys(int index1, int index2) + { + int c = comparer.Compare(keys[index1], keys[index2]); + if (c == 0) + { + if (next == null) return index1 - index2; + return next.CompareKeys(index1, index2); + } + return descending ? -c : c; + } + } + + internal class AsyncSelectorWithCancellationEnumerableSorter : AsyncEnumerableSorter + { + readonly Func> keySelector; + readonly IComparer comparer; + readonly bool descending; + readonly AsyncEnumerableSorter next; + CancellationToken cancellationToken; + TKey[] keys; + + internal AsyncSelectorWithCancellationEnumerableSorter(Func> keySelector, IComparer comparer, bool descending, AsyncEnumerableSorter next, CancellationToken cancellationToken) + { + this.keySelector = keySelector; + this.comparer = comparer; + this.descending = descending; + this.next = next; + this.cancellationToken = cancellationToken; + } + + internal override async UniTask ComputeKeysAsync(TElement[] elements, int count) + { + keys = new TKey[count]; + for (int i = 0; i < count; i++) keys[i] = await keySelector(elements[i], cancellationToken); + if (next != null) await next.ComputeKeysAsync(elements, count); + } + + internal override int CompareKeys(int index1, int index2) + { + int c = comparer.Compare(keys[index1], keys[index2]); + if (c == 0) + { + if (next == null) return index1 - index2; + return next.CompareKeys(index1, index2); + } + return descending ? -c : c; + } + } + + internal abstract class OrderedAsyncEnumerable : IUniTaskOrderedAsyncEnumerable + { + protected readonly IUniTaskAsyncEnumerable source; + + public OrderedAsyncEnumerable(IUniTaskAsyncEnumerable source) + { + this.source = source; + } + + public IUniTaskOrderedAsyncEnumerable CreateOrderedEnumerable(Func keySelector, IComparer comparer, bool descending) + { + return new OrderedAsyncEnumerable(source, keySelector, comparer, descending, this); + } + + public IUniTaskOrderedAsyncEnumerable CreateOrderedEnumerable(Func> keySelector, IComparer comparer, bool descending) + { + return new OrderedAsyncEnumerableAwait(source, keySelector, comparer, descending, this); + } + + public IUniTaskOrderedAsyncEnumerable CreateOrderedEnumerable(Func> keySelector, IComparer comparer, bool descending) + { + return new OrderedAsyncEnumerableAwaitWithCancellation(source, keySelector, comparer, descending, this); + } + + internal abstract AsyncEnumerableSorter GetAsyncEnumerableSorter(AsyncEnumerableSorter next, CancellationToken cancellationToken); + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _OrderedAsyncEnumerator(this, cancellationToken); + } + + class _OrderedAsyncEnumerator : MoveNextSource, IUniTaskAsyncEnumerator + { + protected readonly OrderedAsyncEnumerable parent; + CancellationToken cancellationToken; + TElement[] buffer; + int[] map; + int index; + + public _OrderedAsyncEnumerator(OrderedAsyncEnumerable parent, CancellationToken cancellationToken) + { + this.parent = parent; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TElement Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + if (map == null) + { + completionSource.Reset(); + CreateSortSource().Forget(); + return new UniTask(this, completionSource.Version); + } + + if (index < buffer.Length) + { + Current = buffer[map[index++]]; + return CompletedTasks.True; + } + else + { + return CompletedTasks.False; + } + } + + async UniTaskVoid CreateSortSource() + { + try + { + buffer = await parent.source.ToArrayAsync(); + if (buffer.Length == 0) + { + completionSource.TrySetResult(false); + return; + } + + var sorter = parent.GetAsyncEnumerableSorter(null, cancellationToken); + map = await sorter.SortAsync(buffer, buffer.Length); + sorter = null; + + // set first value + Current = buffer[map[index++]]; + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + + completionSource.TrySetResult(true); + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + return default; + } + } + } + + internal class OrderedAsyncEnumerable : OrderedAsyncEnumerable + { + readonly Func keySelector; + readonly IComparer comparer; + readonly bool descending; + readonly OrderedAsyncEnumerable parent; + + public OrderedAsyncEnumerable(IUniTaskAsyncEnumerable source, Func keySelector, IComparer comparer, bool descending, OrderedAsyncEnumerable parent) + : base(source) + { + this.keySelector = keySelector; + this.comparer = comparer; + this.descending = descending; + this.parent = parent; + } + + internal override AsyncEnumerableSorter GetAsyncEnumerableSorter(AsyncEnumerableSorter next, CancellationToken cancellationToken) + { + AsyncEnumerableSorter sorter = new SyncSelectorAsyncEnumerableSorter(keySelector, comparer, descending, next); + if (parent != null) sorter = parent.GetAsyncEnumerableSorter(sorter, cancellationToken); + return sorter; + } + } + + internal class OrderedAsyncEnumerableAwait : OrderedAsyncEnumerable + { + readonly Func> keySelector; + readonly IComparer comparer; + readonly bool descending; + readonly OrderedAsyncEnumerable parent; + + public OrderedAsyncEnumerableAwait(IUniTaskAsyncEnumerable source, Func> keySelector, IComparer comparer, bool descending, OrderedAsyncEnumerable parent) + : base(source) + { + this.keySelector = keySelector; + this.comparer = comparer; + this.descending = descending; + this.parent = parent; + } + + internal override AsyncEnumerableSorter GetAsyncEnumerableSorter(AsyncEnumerableSorter next, CancellationToken cancellationToken) + { + AsyncEnumerableSorter sorter = new AsyncSelectorEnumerableSorter(keySelector, comparer, descending, next); + if (parent != null) sorter = parent.GetAsyncEnumerableSorter(sorter, cancellationToken); + return sorter; + } + } + + internal class OrderedAsyncEnumerableAwaitWithCancellation : OrderedAsyncEnumerable + { + readonly Func> keySelector; + readonly IComparer comparer; + readonly bool descending; + readonly OrderedAsyncEnumerable parent; + + public OrderedAsyncEnumerableAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> keySelector, IComparer comparer, bool descending, OrderedAsyncEnumerable parent) + : base(source) + { + this.keySelector = keySelector; + this.comparer = comparer; + this.descending = descending; + this.parent = parent; + } + + internal override AsyncEnumerableSorter GetAsyncEnumerableSorter(AsyncEnumerableSorter next, CancellationToken cancellationToken) + { + AsyncEnumerableSorter sorter = new AsyncSelectorWithCancellationEnumerableSorter(keySelector, comparer, descending, next, cancellationToken); + if (parent != null) sorter = parent.GetAsyncEnumerableSorter(sorter, cancellationToken); + return sorter; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/OrderBy.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/OrderBy.cs.meta new file mode 100644 index 0000000..5c6b3e4 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/OrderBy.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 413883ceff8546143bdf200aafa4b8f7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Pairwise.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Pairwise.cs new file mode 100644 index 0000000..5d44a9e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Pairwise.cs @@ -0,0 +1,128 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable<(TSource, TSource)> Pairwise(this IUniTaskAsyncEnumerable source) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new Pairwise(source); + } + } + + internal sealed class Pairwise : IUniTaskAsyncEnumerable<(TSource, TSource)> + { + readonly IUniTaskAsyncEnumerable source; + + public Pairwise(IUniTaskAsyncEnumerable source) + { + this.source = source; + } + + public IUniTaskAsyncEnumerator<(TSource, TSource)> GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Pairwise(source, cancellationToken); + } + + sealed class _Pairwise : MoveNextSource, IUniTaskAsyncEnumerator<(TSource, TSource)> + { + static readonly Action MoveNextCoreDelegate = MoveNextCore; + + readonly IUniTaskAsyncEnumerable source; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + + TSource prev; + bool isFirst; + + public _Pairwise(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + this.source = source; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public (TSource, TSource) Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + if (enumerator == null) + { + isFirst = true; + enumerator = source.GetAsyncEnumerator(cancellationToken); + } + + completionSource.Reset(); + SourceMoveNext(); + return new UniTask(this, completionSource.Version); + } + + void SourceMoveNext() + { + try + { + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + MoveNextCore(this); + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + static void MoveNextCore(object state) + { + var self = (_Pairwise)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + if (self.isFirst) + { + self.isFirst = false; + self.prev = self.enumerator.Current; + self.SourceMoveNext(); // run again. okay to use recursive(only one more). + } + else + { + var p = self.prev; + self.prev = self.enumerator.Current; + self.Current = (p, self.prev); + self.completionSource.TrySetResult(true); + } + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Pairwise.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Pairwise.cs.meta new file mode 100644 index 0000000..727b8cf --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Pairwise.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: cddbf051d2a88f549986c468b23214af +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Publish.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Publish.cs new file mode 100644 index 0000000..d218c0f --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Publish.cs @@ -0,0 +1,173 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IConnectableUniTaskAsyncEnumerable Publish(this IUniTaskAsyncEnumerable source) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new Publish(source); + } + } + + internal sealed class Publish : IConnectableUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly CancellationTokenSource cancellationTokenSource; + + TriggerEvent trigger; + IUniTaskAsyncEnumerator enumerator; + IDisposable connectedDisposable; + bool isCompleted; + + public Publish(IUniTaskAsyncEnumerable source) + { + this.source = source; + this.cancellationTokenSource = new CancellationTokenSource(); + } + + public IDisposable Connect() + { + if (connectedDisposable != null) return connectedDisposable; + + if (enumerator == null) + { + enumerator = source.GetAsyncEnumerator(cancellationTokenSource.Token); + } + + ConsumeEnumerator().Forget(); + + connectedDisposable = new ConnectDisposable(cancellationTokenSource); + return connectedDisposable; + } + + async UniTaskVoid ConsumeEnumerator() + { + try + { + try + { + while (await enumerator.MoveNextAsync()) + { + trigger.SetResult(enumerator.Current); + } + trigger.SetCompleted(); + } + catch (Exception ex) + { + trigger.SetError(ex); + } + } + finally + { + isCompleted = true; + await enumerator.DisposeAsync(); + } + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Publish(this, cancellationToken); + } + + sealed class ConnectDisposable : IDisposable + { + readonly CancellationTokenSource cancellationTokenSource; + + public ConnectDisposable(CancellationTokenSource cancellationTokenSource) + { + this.cancellationTokenSource = cancellationTokenSource; + } + + public void Dispose() + { + this.cancellationTokenSource.Cancel(); + } + } + + sealed class _Publish : MoveNextSource, IUniTaskAsyncEnumerator, ITriggerHandler + { + static readonly Action CancelDelegate = OnCanceled; + + readonly Publish parent; + CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + bool isDisposed; + + public _Publish(Publish parent, CancellationToken cancellationToken) + { + if (cancellationToken.IsCancellationRequested) return; + + this.parent = parent; + this.cancellationToken = cancellationToken; + + if (cancellationToken.CanBeCanceled) + { + this.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(CancelDelegate, this); + } + + parent.trigger.Add(this); + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + ITriggerHandler ITriggerHandler.Prev { get; set; } + ITriggerHandler ITriggerHandler.Next { get; set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + if (parent.isCompleted) return CompletedTasks.False; + + completionSource.Reset(); + return new UniTask(this, completionSource.Version); + } + + static void OnCanceled(object state) + { + var self = (_Publish)state; + self.completionSource.TrySetCanceled(self.cancellationToken); + self.DisposeAsync().Forget(); + } + + public UniTask DisposeAsync() + { + if (!isDisposed) + { + isDisposed = true; + TaskTracker.RemoveTracking(this); + cancellationTokenRegistration.Dispose(); + parent.trigger.Remove(this); + } + + return default; + } + + public void OnNext(TSource value) + { + Current = value; + completionSource.TrySetResult(true); + } + + public void OnCanceled(CancellationToken cancellationToken) + { + completionSource.TrySetCanceled(cancellationToken); + } + + public void OnCompleted() + { + completionSource.TrySetResult(false); + } + + public void OnError(Exception ex) + { + completionSource.TrySetException(ex); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Publish.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Publish.cs.meta new file mode 100644 index 0000000..f3a81ba --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Publish.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 93c684d1e88c09d4e89b79437d97b810 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Queue.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Queue.cs new file mode 100644 index 0000000..b5c221f --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Queue.cs @@ -0,0 +1,103 @@ +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Queue(this IUniTaskAsyncEnumerable source) + { + return new QueueOperator(source); + } + } + + internal sealed class QueueOperator : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + + public QueueOperator(IUniTaskAsyncEnumerable source) + { + this.source = source; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Queue(source, cancellationToken); + } + + sealed class _Queue : IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + CancellationToken cancellationToken; + + Channel channel; + IUniTaskAsyncEnumerator channelEnumerator; + IUniTaskAsyncEnumerator sourceEnumerator; + bool channelClosed; + + public _Queue(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + this.source = source; + this.cancellationToken = cancellationToken; + } + + public TSource Current => channelEnumerator.Current; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + if (sourceEnumerator == null) + { + sourceEnumerator = source.GetAsyncEnumerator(cancellationToken); + channel = Channel.CreateSingleConsumerUnbounded(); + + channelEnumerator = channel.Reader.ReadAllAsync().GetAsyncEnumerator(cancellationToken); + + ConsumeAll(this, sourceEnumerator, channel).Forget(); + } + + return channelEnumerator.MoveNextAsync(); + } + + static async UniTaskVoid ConsumeAll(_Queue self, IUniTaskAsyncEnumerator enumerator, ChannelWriter writer) + { + try + { + while (await enumerator.MoveNextAsync()) + { + writer.TryWrite(enumerator.Current); + } + writer.TryComplete(); + } + catch (Exception ex) + { + writer.TryComplete(ex); + } + finally + { + self.channelClosed = true; + await enumerator.DisposeAsync(); + } + } + + public async UniTask DisposeAsync() + { + if (sourceEnumerator != null) + { + await sourceEnumerator.DisposeAsync(); + } + if (channelEnumerator != null) + { + await channelEnumerator.DisposeAsync(); + } + + if (!channelClosed) + { + channelClosed = true; + channel.Writer.TryComplete(new OperationCanceledException()); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Queue.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Queue.cs.meta new file mode 100644 index 0000000..35f3fab --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Queue.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b7ea1bcf9dbebb042bc99c7816249e02 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Range.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Range.cs new file mode 100644 index 0000000..24a795d --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Range.cs @@ -0,0 +1,75 @@ +using Cysharp.Threading.Tasks.Internal; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Range(int start, int count) + { + if (count < 0) throw Error.ArgumentOutOfRange(nameof(count)); + + var end = (long)start + count - 1L; + if (end > int.MaxValue) throw Error.ArgumentOutOfRange(nameof(count)); + + if (count == 0) UniTaskAsyncEnumerable.Empty(); + + return new Cysharp.Threading.Tasks.Linq.Range(start, count); + } + } + + internal class Range : IUniTaskAsyncEnumerable + { + readonly int start; + readonly int end; + + public Range(int start, int count) + { + this.start = start; + this.end = start + count; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Range(start, end, cancellationToken); + } + + class _Range : IUniTaskAsyncEnumerator + { + readonly int start; + readonly int end; + int current; + CancellationToken cancellationToken; + + public _Range(int start, int end, CancellationToken cancellationToken) + { + this.start = start; + this.end = end; + this.cancellationToken = cancellationToken; + + this.current = start - 1; + } + + public int Current => current; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + current++; + + if (current != end) + { + return CompletedTasks.True; + } + + return CompletedTasks.False; + } + + public UniTask DisposeAsync() + { + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Range.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Range.cs.meta new file mode 100644 index 0000000..36272fc --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Range.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d826418a813498648b10542d0a5fb173 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Repeat.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Repeat.cs new file mode 100644 index 0000000..db90a1a --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Repeat.cs @@ -0,0 +1,68 @@ +using Cysharp.Threading.Tasks.Internal; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Repeat(TElement element, int count) + { + if (count < 0) throw Error.ArgumentOutOfRange(nameof(count)); + + return new Repeat(element, count); + } + } + + internal class Repeat : IUniTaskAsyncEnumerable + { + readonly TElement element; + readonly int count; + + public Repeat(TElement element, int count) + { + this.element = element; + this.count = count; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Repeat(element, count, cancellationToken); + } + + class _Repeat : IUniTaskAsyncEnumerator + { + readonly TElement element; + readonly int count; + int remaining; + CancellationToken cancellationToken; + + public _Repeat(TElement element, int count, CancellationToken cancellationToken) + { + this.element = element; + this.count = count; + this.cancellationToken = cancellationToken; + + this.remaining = count; + } + + public TElement Current => element; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + if (remaining-- != 0) + { + return CompletedTasks.True; + } + + return CompletedTasks.False; + } + + public UniTask DisposeAsync() + { + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Repeat.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Repeat.cs.meta new file mode 100644 index 0000000..693d579 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Repeat.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3819a3925165a674d80ee848c8600379 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Return.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Return.cs new file mode 100644 index 0000000..ba8fa8d --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Return.cs @@ -0,0 +1,63 @@ +using Cysharp.Threading.Tasks.Internal; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Return(TValue value) + { + return new Return(value); + } + } + + internal class Return : IUniTaskAsyncEnumerable + { + readonly TValue value; + + public Return(TValue value) + { + this.value = value; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Return(value, cancellationToken); + } + + class _Return : IUniTaskAsyncEnumerator + { + readonly TValue value; + CancellationToken cancellationToken; + + bool called; + + public _Return(TValue value, CancellationToken cancellationToken) + { + this.value = value; + this.cancellationToken = cancellationToken; + this.called = false; + } + + public TValue Current => value; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + if (!called) + { + called = true; + return CompletedTasks.True; + } + + return CompletedTasks.False; + } + + public UniTask DisposeAsync() + { + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Return.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Return.cs.meta new file mode 100644 index 0000000..ad264d0 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Return.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4313cd8ecf705e44f9064ce46e293c2c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Reverse.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Reverse.cs new file mode 100644 index 0000000..48d4318 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Reverse.cs @@ -0,0 +1,78 @@ +using Cysharp.Threading.Tasks.Internal; +using System.Collections.Generic; +using System.Linq; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Reverse(this IUniTaskAsyncEnumerable source) + { + Error.ThrowArgumentNullException(source, nameof(source)); + return new Reverse(source); + } + } + + internal sealed class Reverse : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + + public Reverse(IUniTaskAsyncEnumerable source) + { + this.source = source; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Reverse(source, cancellationToken); + } + + sealed class _Reverse : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + CancellationToken cancellationToken; + + TSource[] array; + int index; + + public _Reverse(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + this.source = source; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + + // after consumed array, don't use await so allow async(not require UniTaskCompletionSourceCore). + public async UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + if (array == null) + { + array = await source.ToArrayAsync(cancellationToken); + index = array.Length - 1; + } + + if (index != -1) + { + Current = array[index]; + --index; + return true; + } + else + { + return false; + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Reverse.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Reverse.cs.meta new file mode 100644 index 0000000..4a28306 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Reverse.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b2769e65c729b4f4ca6af9826d9c7b90 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Select.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Select.cs new file mode 100644 index 0000000..50e9cec --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Select.cs @@ -0,0 +1,760 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Select(this IUniTaskAsyncEnumerable source, Func selector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(selector, nameof(selector)); + + return new Cysharp.Threading.Tasks.Linq.Select(source, selector); + } + + public static IUniTaskAsyncEnumerable Select(this IUniTaskAsyncEnumerable source, Func selector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(selector, nameof(selector)); + + return new Cysharp.Threading.Tasks.Linq.SelectInt(source, selector); + } + + public static IUniTaskAsyncEnumerable SelectAwait(this IUniTaskAsyncEnumerable source, Func> selector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(selector, nameof(selector)); + + return new Cysharp.Threading.Tasks.Linq.SelectAwait(source, selector); + } + + public static IUniTaskAsyncEnumerable SelectAwait(this IUniTaskAsyncEnumerable source, Func> selector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(selector, nameof(selector)); + + return new Cysharp.Threading.Tasks.Linq.SelectIntAwait(source, selector); + } + + public static IUniTaskAsyncEnumerable SelectAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> selector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(selector, nameof(selector)); + + return new Cysharp.Threading.Tasks.Linq.SelectAwaitWithCancellation(source, selector); + } + + public static IUniTaskAsyncEnumerable SelectAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> selector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(selector, nameof(selector)); + + return new Cysharp.Threading.Tasks.Linq.SelectIntAwaitWithCancellation(source, selector); + } + } + + internal sealed class Select : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func selector; + + public Select(IUniTaskAsyncEnumerable source, Func selector) + { + this.source = source; + this.selector = selector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Select(source, selector, cancellationToken); + } + + sealed class _Select : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly Func selector; + readonly CancellationToken cancellationToken; + + int state = -1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + Action moveNextAction; + + public _Select(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + this.source = source; + this.selector = selector; + this.cancellationToken = cancellationToken; + this.moveNextAction = MoveNext; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + try + { + switch (state) + { + case -1: // init + enumerator = source.GetAsyncEnumerator(cancellationToken); + goto case 0; + case 0: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case 1; + } + else + { + state = 1; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case 1: + if (awaiter.GetResult()) + { + Current = selector(enumerator.Current); + goto CONTINUE; + } + else + { + goto DONE; + } + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + + CONTINUE: + state = 0; + completionSource.TrySetResult(true); + return; + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + return enumerator.DisposeAsync(); + } + } + } + + internal sealed class SelectInt : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func selector; + + public SelectInt(IUniTaskAsyncEnumerable source, Func selector) + { + this.source = source; + this.selector = selector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Select(source, selector, cancellationToken); + } + + sealed class _Select : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly Func selector; + readonly CancellationToken cancellationToken; + + int state = -1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + Action moveNextAction; + int index; + + public _Select(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + this.source = source; + this.selector = selector; + this.cancellationToken = cancellationToken; + this.moveNextAction = MoveNext; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + try + { + switch (state) + { + case -1: // init + enumerator = source.GetAsyncEnumerator(cancellationToken); + goto case 0; + case 0: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case 1; + } + else + { + state = 1; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case 1: + if (awaiter.GetResult()) + { + Current = selector(enumerator.Current, checked(index++)); + goto CONTINUE; + } + else + { + goto DONE; + } + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + + CONTINUE: + state = 0; + completionSource.TrySetResult(true); + return; + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + return enumerator.DisposeAsync(); + } + } + } + + internal sealed class SelectAwait : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> selector; + + public SelectAwait(IUniTaskAsyncEnumerable source, Func> selector) + { + this.source = source; + this.selector = selector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _SelectAwait(source, selector, cancellationToken); + } + + sealed class _SelectAwait : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> selector; + readonly CancellationToken cancellationToken; + + int state = -1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + UniTask.Awaiter awaiter2; + Action moveNextAction; + + public _SelectAwait(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + this.source = source; + this.selector = selector; + this.cancellationToken = cancellationToken; + this.moveNextAction = MoveNext; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + try + { + switch (state) + { + case -1: // init + enumerator = source.GetAsyncEnumerator(cancellationToken); + goto case 0; + case 0: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case 1; + } + else + { + state = 1; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case 1: + if (awaiter.GetResult()) + { + awaiter2 = selector(enumerator.Current).GetAwaiter(); + if (awaiter2.IsCompleted) + { + goto case 2; + } + else + { + state = 2; + awaiter2.UnsafeOnCompleted(moveNextAction); + return; + } + } + else + { + goto DONE; + } + case 2: + Current = awaiter2.GetResult(); + goto CONTINUE; + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + + CONTINUE: + state = 0; + completionSource.TrySetResult(true); + return; + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + return enumerator.DisposeAsync(); + } + } + } + + internal sealed class SelectIntAwait : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> selector; + + public SelectIntAwait(IUniTaskAsyncEnumerable source, Func> selector) + { + this.source = source; + this.selector = selector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _SelectAwait(source, selector, cancellationToken); + } + + sealed class _SelectAwait : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> selector; + readonly CancellationToken cancellationToken; + + int state = -1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + UniTask.Awaiter awaiter2; + Action moveNextAction; + int index; + + public _SelectAwait(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + this.source = source; + this.selector = selector; + this.cancellationToken = cancellationToken; + this.moveNextAction = MoveNext; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + try + { + switch (state) + { + case -1: // init + enumerator = source.GetAsyncEnumerator(cancellationToken); + goto case 0; + case 0: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case 1; + } + else + { + state = 1; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case 1: + if (awaiter.GetResult()) + { + awaiter2 = selector(enumerator.Current, checked(index++)).GetAwaiter(); + if (awaiter2.IsCompleted) + { + goto case 2; + } + else + { + state = 2; + awaiter2.UnsafeOnCompleted(moveNextAction); + return; + } + } + else + { + goto DONE; + } + case 2: + Current = awaiter2.GetResult(); + goto CONTINUE; + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + + CONTINUE: + state = 0; + completionSource.TrySetResult(true); + return; + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + return enumerator.DisposeAsync(); + } + } + } + + internal sealed class SelectAwaitWithCancellation : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> selector; + + public SelectAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> selector) + { + this.source = source; + this.selector = selector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _SelectAwaitWithCancellation(source, selector, cancellationToken); + } + + sealed class _SelectAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> selector; + readonly CancellationToken cancellationToken; + + int state = -1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + UniTask.Awaiter awaiter2; + Action moveNextAction; + + public _SelectAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + this.source = source; + this.selector = selector; + this.cancellationToken = cancellationToken; + this.moveNextAction = MoveNext; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + try + { + switch (state) + { + case -1: // init + enumerator = source.GetAsyncEnumerator(cancellationToken); + goto case 0; + case 0: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case 1; + } + else + { + state = 1; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case 1: + if (awaiter.GetResult()) + { + awaiter2 = selector(enumerator.Current, cancellationToken).GetAwaiter(); + if (awaiter2.IsCompleted) + { + goto case 2; + } + else + { + state = 2; + awaiter2.UnsafeOnCompleted(moveNextAction); + return; + } + } + else + { + goto DONE; + } + case 2: + Current = awaiter2.GetResult(); + goto CONTINUE; + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + + CONTINUE: + state = 0; + completionSource.TrySetResult(true); + return; + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + return enumerator.DisposeAsync(); + } + } + } + + internal sealed class SelectIntAwaitWithCancellation : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> selector; + + public SelectIntAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> selector) + { + this.source = source; + this.selector = selector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _SelectAwaitWithCancellation(source, selector, cancellationToken); + } + + sealed class _SelectAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> selector; + readonly CancellationToken cancellationToken; + + int state = -1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + UniTask.Awaiter awaiter2; + Action moveNextAction; + int index; + + public _SelectAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + this.source = source; + this.selector = selector; + this.cancellationToken = cancellationToken; + this.moveNextAction = MoveNext; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + try + { + switch (state) + { + case -1: // init + enumerator = source.GetAsyncEnumerator(cancellationToken); + goto case 0; + case 0: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case 1; + } + else + { + state = 1; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case 1: + if (awaiter.GetResult()) + { + awaiter2 = selector(enumerator.Current, checked(index++), cancellationToken).GetAwaiter(); + if (awaiter2.IsCompleted) + { + goto case 2; + } + else + { + state = 2; + awaiter2.UnsafeOnCompleted(moveNextAction); + return; + } + } + else + { + goto DONE; + } + case 2: + Current = awaiter2.GetResult(); + goto CONTINUE; + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + + CONTINUE: + state = 0; + completionSource.TrySetResult(true); + return; + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + return enumerator.DisposeAsync(); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Select.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Select.cs.meta new file mode 100644 index 0000000..476e972 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Select.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dc68e598ca44a134b988dfaf5e53bfba +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SelectMany.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SelectMany.cs new file mode 100644 index 0000000..6cad2a5 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SelectMany.cs @@ -0,0 +1,892 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + + public static IUniTaskAsyncEnumerable SelectMany(this IUniTaskAsyncEnumerable source, Func> selector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(selector, nameof(selector)); + + return new SelectMany(source, selector, (x, y) => y); + } + + public static IUniTaskAsyncEnumerable SelectMany(this IUniTaskAsyncEnumerable source, Func> selector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(selector, nameof(selector)); + + return new SelectMany(source, selector, (x, y) => y); + } + + public static IUniTaskAsyncEnumerable SelectMany(this IUniTaskAsyncEnumerable source, Func> collectionSelector, Func resultSelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(collectionSelector, nameof(collectionSelector)); + + return new SelectMany(source, collectionSelector, resultSelector); + } + + public static IUniTaskAsyncEnumerable SelectMany(this IUniTaskAsyncEnumerable source, Func> collectionSelector, Func resultSelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(collectionSelector, nameof(collectionSelector)); + + return new SelectMany(source, collectionSelector, resultSelector); + } + + public static IUniTaskAsyncEnumerable SelectManyAwait(this IUniTaskAsyncEnumerable source, Func>> selector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(selector, nameof(selector)); + + return new SelectManyAwait(source, selector, (x, y) => UniTask.FromResult(y)); + } + + public static IUniTaskAsyncEnumerable SelectManyAwait(this IUniTaskAsyncEnumerable source, Func>> selector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(selector, nameof(selector)); + + return new SelectManyAwait(source, selector, (x, y) => UniTask.FromResult(y)); + } + + public static IUniTaskAsyncEnumerable SelectManyAwait(this IUniTaskAsyncEnumerable source, Func>> collectionSelector, Func> resultSelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(collectionSelector, nameof(collectionSelector)); + + return new SelectManyAwait(source, collectionSelector, resultSelector); + } + + public static IUniTaskAsyncEnumerable SelectManyAwait(this IUniTaskAsyncEnumerable source, Func>> collectionSelector, Func> resultSelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(collectionSelector, nameof(collectionSelector)); + + return new SelectManyAwait(source, collectionSelector, resultSelector); + } + + public static IUniTaskAsyncEnumerable SelectManyAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func>> selector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(selector, nameof(selector)); + + return new SelectManyAwaitWithCancellation(source, selector, (x, y, c) => UniTask.FromResult(y)); + } + + public static IUniTaskAsyncEnumerable SelectManyAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func>> selector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(selector, nameof(selector)); + + return new SelectManyAwaitWithCancellation(source, selector, (x, y, c) => UniTask.FromResult(y)); + } + + public static IUniTaskAsyncEnumerable SelectManyAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func>> collectionSelector, Func> resultSelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(collectionSelector, nameof(collectionSelector)); + + return new SelectManyAwaitWithCancellation(source, collectionSelector, resultSelector); + } + + public static IUniTaskAsyncEnumerable SelectManyAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func>> collectionSelector, Func> resultSelector) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(collectionSelector, nameof(collectionSelector)); + + return new SelectManyAwaitWithCancellation(source, collectionSelector, resultSelector); + } + } + + internal sealed class SelectMany : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> selector1; + readonly Func> selector2; + readonly Func resultSelector; + + public SelectMany(IUniTaskAsyncEnumerable source, Func> selector, Func resultSelector) + { + this.source = source; + this.selector1 = selector; + this.selector2 = null; + this.resultSelector = resultSelector; + } + + public SelectMany(IUniTaskAsyncEnumerable source, Func> selector, Func resultSelector) + { + this.source = source; + this.selector1 = null; + this.selector2 = selector; + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _SelectMany(source, selector1, selector2, resultSelector, cancellationToken); + } + + sealed class _SelectMany : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action sourceMoveNextCoreDelegate = SourceMoveNextCore; + static readonly Action selectedSourceMoveNextCoreDelegate = SeletedSourceMoveNextCore; + static readonly Action selectedEnumeratorDisposeAsyncCoreDelegate = SelectedEnumeratorDisposeAsyncCore; + + readonly IUniTaskAsyncEnumerable source; + + readonly Func> selector1; + readonly Func> selector2; + readonly Func resultSelector; + CancellationToken cancellationToken; + + TSource sourceCurrent; + int sourceIndex; + IUniTaskAsyncEnumerator sourceEnumerator; + IUniTaskAsyncEnumerator selectedEnumerator; + UniTask.Awaiter sourceAwaiter; + UniTask.Awaiter selectedAwaiter; + UniTask.Awaiter selectedDisposeAsyncAwaiter; + + public _SelectMany(IUniTaskAsyncEnumerable source, Func> selector1, Func> selector2, Func resultSelector, CancellationToken cancellationToken) + { + this.source = source; + this.selector1 = selector1; + this.selector2 = selector2; + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + completionSource.Reset(); + + // iterate selected field + if (selectedEnumerator != null) + { + MoveNextSelected(); + } + else + { + // iterate source field + if (sourceEnumerator == null) + { + sourceEnumerator = source.GetAsyncEnumerator(cancellationToken); + } + MoveNextSource(); + } + + return new UniTask(this, completionSource.Version); + } + + void MoveNextSource() + { + try + { + sourceAwaiter = sourceEnumerator.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + + if (sourceAwaiter.IsCompleted) + { + SourceMoveNextCore(this); + } + else + { + sourceAwaiter.SourceOnCompleted(sourceMoveNextCoreDelegate, this); + } + } + + void MoveNextSelected() + { + try + { + selectedAwaiter = selectedEnumerator.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + + if (selectedAwaiter.IsCompleted) + { + SeletedSourceMoveNextCore(this); + } + else + { + selectedAwaiter.SourceOnCompleted(selectedSourceMoveNextCoreDelegate, this); + } + } + + static void SourceMoveNextCore(object state) + { + var self = (_SelectMany)state; + + if (self.TryGetResult(self.sourceAwaiter, out var result)) + { + if (result) + { + try + { + self.sourceCurrent = self.sourceEnumerator.Current; + if (self.selector1 != null) + { + self.selectedEnumerator = self.selector1(self.sourceCurrent).GetAsyncEnumerator(self.cancellationToken); + } + else + { + self.selectedEnumerator = self.selector2(self.sourceCurrent, checked(self.sourceIndex++)).GetAsyncEnumerator(self.cancellationToken); + } + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + return; + } + + self.MoveNextSelected(); // iterated selected source. + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + static void SeletedSourceMoveNextCore(object state) + { + var self = (_SelectMany)state; + + if (self.TryGetResult(self.selectedAwaiter, out var result)) + { + if (result) + { + try + { + self.Current = self.resultSelector(self.sourceCurrent, self.selectedEnumerator.Current); + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + return; + } + + self.completionSource.TrySetResult(true); + } + else + { + // dispose selected source and try iterate source. + try + { + self.selectedDisposeAsyncAwaiter = self.selectedEnumerator.DisposeAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + return; + } + if (self.selectedDisposeAsyncAwaiter.IsCompleted) + { + SelectedEnumeratorDisposeAsyncCore(self); + } + else + { + self.selectedDisposeAsyncAwaiter.SourceOnCompleted(selectedEnumeratorDisposeAsyncCoreDelegate, self); + } + } + } + } + + static void SelectedEnumeratorDisposeAsyncCore(object state) + { + var self = (_SelectMany)state; + + if (self.TryGetResult(self.selectedDisposeAsyncAwaiter)) + { + self.selectedEnumerator = null; + self.selectedAwaiter = default; + + self.MoveNextSource(); // iterate next source + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (selectedEnumerator != null) + { + await selectedEnumerator.DisposeAsync(); + } + if (sourceEnumerator != null) + { + await sourceEnumerator.DisposeAsync(); + } + } + } + } + + internal sealed class SelectManyAwait : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func>> selector1; + readonly Func>> selector2; + readonly Func> resultSelector; + + public SelectManyAwait(IUniTaskAsyncEnumerable source, Func>> selector, Func> resultSelector) + { + this.source = source; + this.selector1 = selector; + this.selector2 = null; + this.resultSelector = resultSelector; + } + + public SelectManyAwait(IUniTaskAsyncEnumerable source, Func>> selector, Func> resultSelector) + { + this.source = source; + this.selector1 = null; + this.selector2 = selector; + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _SelectManyAwait(source, selector1, selector2, resultSelector, cancellationToken); + } + + sealed class _SelectManyAwait : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action sourceMoveNextCoreDelegate = SourceMoveNextCore; + static readonly Action selectedSourceMoveNextCoreDelegate = SeletedSourceMoveNextCore; + static readonly Action selectedEnumeratorDisposeAsyncCoreDelegate = SelectedEnumeratorDisposeAsyncCore; + static readonly Action selectorAwaitCoreDelegate = SelectorAwaitCore; + static readonly Action resultSelectorAwaitCoreDelegate = ResultSelectorAwaitCore; + + readonly IUniTaskAsyncEnumerable source; + + readonly Func>> selector1; + readonly Func>> selector2; + readonly Func> resultSelector; + CancellationToken cancellationToken; + + TSource sourceCurrent; + int sourceIndex; + IUniTaskAsyncEnumerator sourceEnumerator; + IUniTaskAsyncEnumerator selectedEnumerator; + UniTask.Awaiter sourceAwaiter; + UniTask.Awaiter selectedAwaiter; + UniTask.Awaiter selectedDisposeAsyncAwaiter; + + // await additional + UniTask>.Awaiter collectionSelectorAwaiter; + UniTask.Awaiter resultSelectorAwaiter; + + public _SelectManyAwait(IUniTaskAsyncEnumerable source, Func>> selector1, Func>> selector2, Func> resultSelector, CancellationToken cancellationToken) + { + this.source = source; + this.selector1 = selector1; + this.selector2 = selector2; + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + completionSource.Reset(); + + // iterate selected field + if (selectedEnumerator != null) + { + MoveNextSelected(); + } + else + { + // iterate source field + if (sourceEnumerator == null) + { + sourceEnumerator = source.GetAsyncEnumerator(cancellationToken); + } + MoveNextSource(); + } + + return new UniTask(this, completionSource.Version); + } + + void MoveNextSource() + { + try + { + sourceAwaiter = sourceEnumerator.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + + if (sourceAwaiter.IsCompleted) + { + SourceMoveNextCore(this); + } + else + { + sourceAwaiter.SourceOnCompleted(sourceMoveNextCoreDelegate, this); + } + } + + void MoveNextSelected() + { + try + { + selectedAwaiter = selectedEnumerator.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + + if (selectedAwaiter.IsCompleted) + { + SeletedSourceMoveNextCore(this); + } + else + { + selectedAwaiter.SourceOnCompleted(selectedSourceMoveNextCoreDelegate, this); + } + } + + static void SourceMoveNextCore(object state) + { + var self = (_SelectManyAwait)state; + + if (self.TryGetResult(self.sourceAwaiter, out var result)) + { + if (result) + { + try + { + self.sourceCurrent = self.sourceEnumerator.Current; + + if (self.selector1 != null) + { + self.collectionSelectorAwaiter = self.selector1(self.sourceCurrent).GetAwaiter(); + } + else + { + self.collectionSelectorAwaiter = self.selector2(self.sourceCurrent, checked(self.sourceIndex++)).GetAwaiter(); + } + + if (self.collectionSelectorAwaiter.IsCompleted) + { + SelectorAwaitCore(self); + } + else + { + self.collectionSelectorAwaiter.SourceOnCompleted(selectorAwaitCoreDelegate, self); + } + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + return; + } + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + static void SeletedSourceMoveNextCore(object state) + { + var self = (_SelectManyAwait)state; + + if (self.TryGetResult(self.selectedAwaiter, out var result)) + { + if (result) + { + try + { + self.resultSelectorAwaiter = self.resultSelector(self.sourceCurrent, self.selectedEnumerator.Current).GetAwaiter(); + if (self.resultSelectorAwaiter.IsCompleted) + { + ResultSelectorAwaitCore(self); + } + else + { + self.resultSelectorAwaiter.SourceOnCompleted(resultSelectorAwaitCoreDelegate, self); + } + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + return; + } + } + else + { + // dispose selected source and try iterate source. + try + { + self.selectedDisposeAsyncAwaiter = self.selectedEnumerator.DisposeAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + return; + } + if (self.selectedDisposeAsyncAwaiter.IsCompleted) + { + SelectedEnumeratorDisposeAsyncCore(self); + } + else + { + self.selectedDisposeAsyncAwaiter.SourceOnCompleted(selectedEnumeratorDisposeAsyncCoreDelegate, self); + } + } + } + } + + static void SelectedEnumeratorDisposeAsyncCore(object state) + { + var self = (_SelectManyAwait)state; + + if (self.TryGetResult(self.selectedDisposeAsyncAwaiter)) + { + self.selectedEnumerator = null; + self.selectedAwaiter = default; + + self.MoveNextSource(); // iterate next source + } + } + + static void SelectorAwaitCore(object state) + { + var self = (_SelectManyAwait)state; + + if (self.TryGetResult(self.collectionSelectorAwaiter, out var result)) + { + self.selectedEnumerator = result.GetAsyncEnumerator(self.cancellationToken); + self.MoveNextSelected(); // iterated selected source. + } + } + + static void ResultSelectorAwaitCore(object state) + { + var self = (_SelectManyAwait)state; + + if (self.TryGetResult(self.resultSelectorAwaiter, out var result)) + { + self.Current = result; + self.completionSource.TrySetResult(true); + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (selectedEnumerator != null) + { + await selectedEnumerator.DisposeAsync(); + } + if (sourceEnumerator != null) + { + await sourceEnumerator.DisposeAsync(); + } + } + } + } + + internal sealed class SelectManyAwaitWithCancellation : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func>> selector1; + readonly Func>> selector2; + readonly Func> resultSelector; + + public SelectManyAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func>> selector, Func> resultSelector) + { + this.source = source; + this.selector1 = selector; + this.selector2 = null; + this.resultSelector = resultSelector; + } + + public SelectManyAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func>> selector, Func> resultSelector) + { + this.source = source; + this.selector1 = null; + this.selector2 = selector; + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _SelectManyAwaitWithCancellation(source, selector1, selector2, resultSelector, cancellationToken); + } + + sealed class _SelectManyAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action sourceMoveNextCoreDelegate = SourceMoveNextCore; + static readonly Action selectedSourceMoveNextCoreDelegate = SeletedSourceMoveNextCore; + static readonly Action selectedEnumeratorDisposeAsyncCoreDelegate = SelectedEnumeratorDisposeAsyncCore; + static readonly Action selectorAwaitCoreDelegate = SelectorAwaitCore; + static readonly Action resultSelectorAwaitCoreDelegate = ResultSelectorAwaitCore; + + readonly IUniTaskAsyncEnumerable source; + + readonly Func>> selector1; + readonly Func>> selector2; + readonly Func> resultSelector; + CancellationToken cancellationToken; + + TSource sourceCurrent; + int sourceIndex; + IUniTaskAsyncEnumerator sourceEnumerator; + IUniTaskAsyncEnumerator selectedEnumerator; + UniTask.Awaiter sourceAwaiter; + UniTask.Awaiter selectedAwaiter; + UniTask.Awaiter selectedDisposeAsyncAwaiter; + + // await additional + UniTask>.Awaiter collectionSelectorAwaiter; + UniTask.Awaiter resultSelectorAwaiter; + + public _SelectManyAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func>> selector1, Func>> selector2, Func> resultSelector, CancellationToken cancellationToken) + { + this.source = source; + this.selector1 = selector1; + this.selector2 = selector2; + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + completionSource.Reset(); + + // iterate selected field + if (selectedEnumerator != null) + { + MoveNextSelected(); + } + else + { + // iterate source field + if (sourceEnumerator == null) + { + sourceEnumerator = source.GetAsyncEnumerator(cancellationToken); + } + MoveNextSource(); + } + + return new UniTask(this, completionSource.Version); + } + + void MoveNextSource() + { + try + { + sourceAwaiter = sourceEnumerator.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + + if (sourceAwaiter.IsCompleted) + { + SourceMoveNextCore(this); + } + else + { + sourceAwaiter.SourceOnCompleted(sourceMoveNextCoreDelegate, this); + } + } + + void MoveNextSelected() + { + try + { + selectedAwaiter = selectedEnumerator.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return; + } + + if (selectedAwaiter.IsCompleted) + { + SeletedSourceMoveNextCore(this); + } + else + { + selectedAwaiter.SourceOnCompleted(selectedSourceMoveNextCoreDelegate, this); + } + } + + static void SourceMoveNextCore(object state) + { + var self = (_SelectManyAwaitWithCancellation)state; + + if (self.TryGetResult(self.sourceAwaiter, out var result)) + { + if (result) + { + try + { + self.sourceCurrent = self.sourceEnumerator.Current; + + if (self.selector1 != null) + { + self.collectionSelectorAwaiter = self.selector1(self.sourceCurrent, self.cancellationToken).GetAwaiter(); + } + else + { + self.collectionSelectorAwaiter = self.selector2(self.sourceCurrent, checked(self.sourceIndex++), self.cancellationToken).GetAwaiter(); + } + + if (self.collectionSelectorAwaiter.IsCompleted) + { + SelectorAwaitCore(self); + } + else + { + self.collectionSelectorAwaiter.SourceOnCompleted(selectorAwaitCoreDelegate, self); + } + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + return; + } + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + static void SeletedSourceMoveNextCore(object state) + { + var self = (_SelectManyAwaitWithCancellation)state; + + if (self.TryGetResult(self.selectedAwaiter, out var result)) + { + if (result) + { + try + { + self.resultSelectorAwaiter = self.resultSelector(self.sourceCurrent, self.selectedEnumerator.Current, self.cancellationToken).GetAwaiter(); + if (self.resultSelectorAwaiter.IsCompleted) + { + ResultSelectorAwaitCore(self); + } + else + { + self.resultSelectorAwaiter.SourceOnCompleted(resultSelectorAwaitCoreDelegate, self); + } + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + return; + } + } + else + { + // dispose selected source and try iterate source. + try + { + self.selectedDisposeAsyncAwaiter = self.selectedEnumerator.DisposeAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + return; + } + if (self.selectedDisposeAsyncAwaiter.IsCompleted) + { + SelectedEnumeratorDisposeAsyncCore(self); + } + else + { + self.selectedDisposeAsyncAwaiter.SourceOnCompleted(selectedEnumeratorDisposeAsyncCoreDelegate, self); + } + } + } + } + + static void SelectedEnumeratorDisposeAsyncCore(object state) + { + var self = (_SelectManyAwaitWithCancellation)state; + + if (self.TryGetResult(self.selectedDisposeAsyncAwaiter)) + { + self.selectedEnumerator = null; + self.selectedAwaiter = default; + + self.MoveNextSource(); // iterate next source + } + } + + static void SelectorAwaitCore(object state) + { + var self = (_SelectManyAwaitWithCancellation)state; + + if (self.TryGetResult(self.collectionSelectorAwaiter, out var result)) + { + self.selectedEnumerator = result.GetAsyncEnumerator(self.cancellationToken); + self.MoveNextSelected(); // iterated selected source. + } + } + + static void ResultSelectorAwaitCore(object state) + { + var self = (_SelectManyAwaitWithCancellation)state; + + if (self.TryGetResult(self.resultSelectorAwaiter, out var result)) + { + self.Current = result; + self.completionSource.TrySetResult(true); + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (selectedEnumerator != null) + { + await selectedEnumerator.DisposeAsync(); + } + if (sourceEnumerator != null) + { + await sourceEnumerator.DisposeAsync(); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SelectMany.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SelectMany.cs.meta new file mode 100644 index 0000000..a8dbbaf --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SelectMany.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d81862f0eb12680479ccaaf2ac319d24 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SequenceEqual.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SequenceEqual.cs new file mode 100644 index 0000000..9512ea7 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SequenceEqual.cs @@ -0,0 +1,87 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask SequenceEqualAsync(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, CancellationToken cancellationToken = default) + { + return SequenceEqualAsync(first, second, EqualityComparer.Default, cancellationToken); + } + + public static UniTask SequenceEqualAsync(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(first, nameof(first)); + Error.ThrowArgumentNullException(second, nameof(second)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return SequenceEqual.SequenceEqualAsync(first, second, comparer, cancellationToken); + } + } + + internal static class SequenceEqual + { + internal static async UniTask SequenceEqualAsync(IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var e1 = first.GetAsyncEnumerator(cancellationToken); + try + { + var e2 = second.GetAsyncEnumerator(cancellationToken); + try + { + while (true) + { + if (await e1.MoveNextAsync()) + { + if (await e2.MoveNextAsync()) + { + if (comparer.Equals(e1.Current, e2.Current)) + { + continue; + } + else + { + return false; + } + } + else + { + // e2 is finished, but e1 has value + return false; + } + } + else + { + // e1 is finished, e2? + if (await e2.MoveNextAsync()) + { + return false; + } + else + { + return true; + } + } + } + } + finally + { + if (e2 != null) + { + await e2.DisposeAsync(); + } + } + } + finally + { + if (e1 != null) + { + await e1.DisposeAsync(); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SequenceEqual.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SequenceEqual.cs.meta new file mode 100644 index 0000000..ee2b75c --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SequenceEqual.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b382772aba6128842928cdb6b2e034b0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Single.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Single.cs new file mode 100644 index 0000000..30df1b3 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Single.cs @@ -0,0 +1,230 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask SingleAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return SingleOperator.SingleAsync(source, cancellationToken, false); + } + + public static UniTask SingleAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return SingleOperator.SingleAsync(source, predicate, cancellationToken, false); + } + + public static UniTask SingleAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return SingleOperator.SingleAwaitAsync(source, predicate, cancellationToken, false); + } + + public static UniTask SingleAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return SingleOperator.SingleAwaitWithCancellationAsync(source, predicate, cancellationToken, false); + } + + public static UniTask SingleOrDefaultAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return SingleOperator.SingleAsync(source, cancellationToken, true); + } + + public static UniTask SingleOrDefaultAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return SingleOperator.SingleAsync(source, predicate, cancellationToken, true); + } + + public static UniTask SingleOrDefaultAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return SingleOperator.SingleAwaitAsync(source, predicate, cancellationToken, true); + } + + public static UniTask SingleOrDefaultAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return SingleOperator.SingleAwaitWithCancellationAsync(source, predicate, cancellationToken, true); + } + } + + internal static class SingleOperator + { + public static async UniTask SingleAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken, bool defaultIfEmpty) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + if (await e.MoveNextAsync()) + { + var v = e.Current; + if (!await e.MoveNextAsync()) + { + return v; + } + + throw Error.MoreThanOneElement(); + } + else + { + if (defaultIfEmpty) + { + return default; + } + else + { + throw Error.NoElements(); + } + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTask SingleAsync(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken, bool defaultIfEmpty) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + TSource value = default; + bool found = false; + while (await e.MoveNextAsync()) + { + var v = e.Current; + if (predicate(v)) + { + if (found) + { + throw Error.MoreThanOneElement(); + } + else + { + found = true; + value = v; + } + } + } + + if (found || defaultIfEmpty) + { + return value; + } + + throw Error.NoElements(); + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTask SingleAwaitAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken, bool defaultIfEmpty) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + TSource value = default; + bool found = false; + while (await e.MoveNextAsync()) + { + var v = e.Current; + if (await predicate(v)) + { + if (found) + { + throw Error.MoreThanOneElement(); + } + else + { + found = true; + value = v; + } + } + } + + if (found || defaultIfEmpty) + { + return value; + } + + throw Error.NoElements(); + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTask SingleAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken, bool defaultIfEmpty) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + TSource value = default; + bool found = false; + while (await e.MoveNextAsync()) + { + var v = e.Current; + if (await predicate(v, cancellationToken)) + { + if (found) + { + throw Error.MoreThanOneElement(); + } + else + { + found = true; + value = v; + } + } + } + + if (found || defaultIfEmpty) + { + return value; + } + + throw Error.NoElements(); + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Single.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Single.cs.meta new file mode 100644 index 0000000..c053dfd --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Single.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1bcd3928b90472e43a3a92c3ba708967 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Skip.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Skip.cs new file mode 100644 index 0000000..6f4831d --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Skip.cs @@ -0,0 +1,69 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Skip(this IUniTaskAsyncEnumerable source, Int32 count) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new Skip(source, count); + } + } + + internal sealed class Skip : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly int count; + + public Skip(IUniTaskAsyncEnumerable source, int count) + { + this.source = source; + this.count = count; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Skip(source, count, cancellationToken); + } + + sealed class _Skip : AsyncEnumeratorBase + { + readonly int count; + + int index; + + public _Skip(IUniTaskAsyncEnumerable source, int count, CancellationToken cancellationToken) + : base(source, cancellationToken) + { + this.count = count; + } + + protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result) + { + if (sourceHasCurrent) + { + if (count <= checked(index++)) + { + Current = SourceCurrent; + result = true; + return true; + } + else + { + result = default; + return false; + } + } + else + { + result = false; + return true; + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Skip.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Skip.cs.meta new file mode 100644 index 0000000..25ad847 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Skip.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9c46b6c7dce0cb049a73c81084c75154 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipLast.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipLast.cs new file mode 100644 index 0000000..9d127b8 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipLast.cs @@ -0,0 +1,159 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable SkipLast(this IUniTaskAsyncEnumerable source, Int32 count) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + // non skip. + if (count <= 0) + { + return source; + } + + return new SkipLast(source, count); + } + } + + internal sealed class SkipLast : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly int count; + + public SkipLast(IUniTaskAsyncEnumerable source, int count) + { + this.source = source; + this.count = count; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _SkipLast(source, count, cancellationToken); + } + + sealed class _SkipLast : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action MoveNextCoreDelegate = MoveNextCore; + + readonly IUniTaskAsyncEnumerable source; + readonly int count; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + Queue queue; + + bool continueNext; + + public _SkipLast(IUniTaskAsyncEnumerable source, int count, CancellationToken cancellationToken) + { + this.source = source; + this.count = count; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + if (enumerator == null) + { + enumerator = source.GetAsyncEnumerator(cancellationToken); + queue = new Queue(); + } + + completionSource.Reset(); + SourceMoveNext(); + return new UniTask(this, completionSource.Version); + } + + void SourceMoveNext() + { + try + { + + LOOP: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + continueNext = true; + MoveNextCore(this); + if (continueNext) + { + continueNext = false; + goto LOOP; // avoid recursive + } + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + + static void MoveNextCore(object state) + { + var self = (_SkipLast)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + if (self.queue.Count == self.count) + { + self.continueNext = false; + + var deq = self.queue.Dequeue(); + self.Current = deq; + self.queue.Enqueue(self.enumerator.Current); + + self.completionSource.TrySetResult(true); + } + else + { + self.queue.Enqueue(self.enumerator.Current); + + if (!self.continueNext) + { + self.SourceMoveNext(); + } + } + } + else + { + self.continueNext = false; + self.completionSource.TrySetResult(false); + } + } + else + { + self.continueNext = false; + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipLast.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipLast.cs.meta new file mode 100644 index 0000000..06b1ede --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipLast.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: df1d7f44d4fe7754f972c9e0b6fa72d5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipUntil.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipUntil.cs new file mode 100644 index 0000000..5a707bb --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipUntil.cs @@ -0,0 +1,187 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable SkipUntil(this IUniTaskAsyncEnumerable source, UniTask other) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new SkipUntil(source, other, null); + } + + public static IUniTaskAsyncEnumerable SkipUntil(this IUniTaskAsyncEnumerable source, Func other) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(other)); + + return new SkipUntil(source, default, other); + } + } + + internal sealed class SkipUntil : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly UniTask other; + readonly Func other2; + + public SkipUntil(IUniTaskAsyncEnumerable source, UniTask other, Func other2) + { + this.source = source; + this.other = other; + this.other2 = other2; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + if (other2 != null) + { + return new _SkipUntil(source, this.other2(cancellationToken), cancellationToken); + } + else + { + return new _SkipUntil(source, this.other, cancellationToken); + } + } + + sealed class _SkipUntil : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action CancelDelegate1 = OnCanceled1; + static readonly Action MoveNextCoreDelegate = MoveNextCore; + + readonly IUniTaskAsyncEnumerable source; + CancellationToken cancellationToken1; + + bool completed; + CancellationTokenRegistration cancellationTokenRegistration1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + bool continueNext; + Exception exception; + + public _SkipUntil(IUniTaskAsyncEnumerable source, UniTask other, CancellationToken cancellationToken1) + { + this.source = source; + this.cancellationToken1 = cancellationToken1; + if (cancellationToken1.CanBeCanceled) + { + this.cancellationTokenRegistration1 = cancellationToken1.RegisterWithoutCaptureExecutionContext(CancelDelegate1, this); + } + + TaskTracker.TrackActiveTask(this, 3); + RunOther(other).Forget(); + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (exception != null) + { + return UniTask.FromException(exception); + } + + if (cancellationToken1.IsCancellationRequested) + { + return UniTask.FromCanceled(cancellationToken1); + } + + if (enumerator == null) + { + enumerator = source.GetAsyncEnumerator(cancellationToken1); + } + completionSource.Reset(); + + if (completed) + { + SourceMoveNext(); + } + return new UniTask(this, completionSource.Version); + } + + void SourceMoveNext() + { + try + { + LOOP: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + continueNext = true; + MoveNextCore(this); + if (continueNext) + { + continueNext = false; + goto LOOP; + } + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + static void MoveNextCore(object state) + { + var self = (_SkipUntil)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + self.Current = self.enumerator.Current; + self.completionSource.TrySetResult(true); + if (self.continueNext) + { + self.SourceMoveNext(); + } + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + async UniTaskVoid RunOther(UniTask other) + { + try + { + await other; + completed = true; + SourceMoveNext(); + } + catch (Exception ex) + { + exception = ex; + completionSource.TrySetException(ex); + } + } + + static void OnCanceled1(object state) + { + var self = (_SkipUntil)state; + self.completionSource.TrySetCanceled(self.cancellationToken1); + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + cancellationTokenRegistration1.Dispose(); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipUntil.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipUntil.cs.meta new file mode 100644 index 0000000..0772ed0 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipUntil.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: de932d79c8d9f3841a066d05ff29edc9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipUntilCanceled.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipUntilCanceled.cs new file mode 100644 index 0000000..f4c9679 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipUntilCanceled.cs @@ -0,0 +1,173 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable SkipUntilCanceled(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new SkipUntilCanceled(source, cancellationToken); + } + } + + internal sealed class SkipUntilCanceled : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly CancellationToken cancellationToken; + + public SkipUntilCanceled(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + this.source = source; + this.cancellationToken = cancellationToken; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _SkipUntilCanceled(source, this.cancellationToken, cancellationToken); + } + + sealed class _SkipUntilCanceled : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action CancelDelegate1 = OnCanceled1; + static readonly Action CancelDelegate2 = OnCanceled2; + static readonly Action MoveNextCoreDelegate = MoveNextCore; + + readonly IUniTaskAsyncEnumerable source; + CancellationToken cancellationToken1; + CancellationToken cancellationToken2; + CancellationTokenRegistration cancellationTokenRegistration1; + CancellationTokenRegistration cancellationTokenRegistration2; + + int isCanceled; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + bool continueNext; + + public _SkipUntilCanceled(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken1, CancellationToken cancellationToken2) + { + this.source = source; + this.cancellationToken1 = cancellationToken1; + this.cancellationToken2 = cancellationToken2; + if (cancellationToken1.CanBeCanceled) + { + this.cancellationTokenRegistration1 = cancellationToken1.RegisterWithoutCaptureExecutionContext(CancelDelegate1, this); + } + if (cancellationToken1 != cancellationToken2 && cancellationToken2.CanBeCanceled) + { + this.cancellationTokenRegistration2 = cancellationToken2.RegisterWithoutCaptureExecutionContext(CancelDelegate2, this); + } + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (enumerator == null) + { + if (cancellationToken1.IsCancellationRequested) isCanceled = 1; + if (cancellationToken2.IsCancellationRequested) isCanceled = 1; + enumerator = source.GetAsyncEnumerator(cancellationToken2); // use only AsyncEnumerator provided token. + } + completionSource.Reset(); + + if (isCanceled != 0) + { + SourceMoveNext(); + } + return new UniTask(this, completionSource.Version); + } + + void SourceMoveNext() + { + try + { + LOOP: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + continueNext = true; + MoveNextCore(this); + if (continueNext) + { + continueNext = false; + goto LOOP; + } + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + static void MoveNextCore(object state) + { + var self = (_SkipUntilCanceled)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + self.Current = self.enumerator.Current; + self.completionSource.TrySetResult(true); + if (self.continueNext) + { + self.SourceMoveNext(); + } + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + static void OnCanceled1(object state) + { + var self = (_SkipUntilCanceled)state; + if (self.isCanceled == 0) + { + if (Interlocked.Increment(ref self.isCanceled) == 1) + { + self.cancellationTokenRegistration2.Dispose(); + self.SourceMoveNext(); + } + } + } + + static void OnCanceled2(object state) + { + var self = (_SkipUntilCanceled)state; + if (self.isCanceled == 0) + { + if (Interlocked.Increment(ref self.isCanceled) == 1) + { + self.cancellationTokenRegistration2.Dispose(); + self.SourceMoveNext(); + } + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + cancellationTokenRegistration1.Dispose(); + cancellationTokenRegistration2.Dispose(); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipUntilCanceled.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipUntilCanceled.cs.meta new file mode 100644 index 0000000..9f67181 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipUntilCanceled.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4b1a778aef7150d47b93a49aa1bc34ae +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipWhile.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipWhile.cs new file mode 100644 index 0000000..771a2e2 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipWhile.cs @@ -0,0 +1,379 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable SkipWhile(this IUniTaskAsyncEnumerable source, Func predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new SkipWhile(source, predicate); + } + + public static IUniTaskAsyncEnumerable SkipWhile(this IUniTaskAsyncEnumerable source, Func predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new SkipWhileInt(source, predicate); + } + + public static IUniTaskAsyncEnumerable SkipWhileAwait(this IUniTaskAsyncEnumerable source, Func> predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new SkipWhileAwait(source, predicate); + } + + public static IUniTaskAsyncEnumerable SkipWhileAwait(this IUniTaskAsyncEnumerable source, Func> predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new SkipWhileIntAwait(source, predicate); + } + + public static IUniTaskAsyncEnumerable SkipWhileAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new SkipWhileAwaitWithCancellation(source, predicate); + } + + public static IUniTaskAsyncEnumerable SkipWhileAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new SkipWhileIntAwaitWithCancellation(source, predicate); + } + } + + internal sealed class SkipWhile : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func predicate; + + public SkipWhile(IUniTaskAsyncEnumerable source, Func predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _SkipWhile(source, predicate, cancellationToken); + } + + class _SkipWhile : AsyncEnumeratorBase + { + Func predicate; + + public _SkipWhile(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) + + : base(source, cancellationToken) + { + this.predicate = predicate; + } + + protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result) + { + if (sourceHasCurrent) + { + if (predicate == null || !predicate(SourceCurrent)) + { + predicate = null; + Current = SourceCurrent; + result = true; + return true; + } + else + { + result = default; + return false; + } + } + + result = false; + return true; + } + } + } + + internal sealed class SkipWhileInt : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func predicate; + + public SkipWhileInt(IUniTaskAsyncEnumerable source, Func predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _SkipWhileInt(source, predicate, cancellationToken); + } + + class _SkipWhileInt : AsyncEnumeratorBase + { + Func predicate; + int index; + + public _SkipWhileInt(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) + + : base(source, cancellationToken) + { + this.predicate = predicate; + } + + protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result) + { + if (sourceHasCurrent) + { + if (predicate == null || !predicate(SourceCurrent, checked(index++))) + { + predicate = null; + Current = SourceCurrent; + result = true; + return true; + } + else + { + result = default; + return false; + } + } + + result = false; + return true; + } + } + } + + internal sealed class SkipWhileAwait : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> predicate; + + public SkipWhileAwait(IUniTaskAsyncEnumerable source, Func> predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _SkipWhileAwait(source, predicate, cancellationToken); + } + + class _SkipWhileAwait : AsyncEnumeratorAwaitSelectorBase + { + Func> predicate; + + public _SkipWhileAwait(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + + : base(source, cancellationToken) + { + this.predicate = predicate; + } + + protected override UniTask TransformAsync(TSource sourceCurrent) + { + if (predicate == null) + { + return CompletedTasks.False; + } + + return predicate(sourceCurrent); + } + + protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration) + { + if (!awaitResult) + { + predicate = null; + Current = SourceCurrent; + terminateIteration= false; + return true; + } + else + { + terminateIteration= false; + return false; + } + } + } + } + + internal sealed class SkipWhileIntAwait : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> predicate; + + public SkipWhileIntAwait(IUniTaskAsyncEnumerable source, Func> predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _SkipWhileIntAwait(source, predicate, cancellationToken); + } + + class _SkipWhileIntAwait : AsyncEnumeratorAwaitSelectorBase + { + Func> predicate; + int index; + + public _SkipWhileIntAwait(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + + : base(source, cancellationToken) + { + this.predicate = predicate; + } + + protected override UniTask TransformAsync(TSource sourceCurrent) + { + if (predicate == null) + { + return CompletedTasks.False; + } + + return predicate(sourceCurrent, checked(index++)); + } + + protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration) + { + terminateIteration= false; + if (!awaitResult) + { + predicate = null; + Current = SourceCurrent; + return true; + } + else + { + return false; + } + } + } + } + + internal sealed class SkipWhileAwaitWithCancellation : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> predicate; + + public SkipWhileAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _SkipWhileAwaitWithCancellation(source, predicate, cancellationToken); + } + + class _SkipWhileAwaitWithCancellation : AsyncEnumeratorAwaitSelectorBase + { + Func> predicate; + + public _SkipWhileAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + + : base(source, cancellationToken) + { + this.predicate = predicate; + } + + protected override UniTask TransformAsync(TSource sourceCurrent) + { + if (predicate == null) + { + return CompletedTasks.False; + } + + return predicate(sourceCurrent, cancellationToken); + } + + protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration) + { + terminateIteration= false; + if (!awaitResult) + { + predicate = null; + Current = SourceCurrent; + return true; + } + else + { + return false; + } + } + } + } + + internal sealed class SkipWhileIntAwaitWithCancellation : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> predicate; + + public SkipWhileIntAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _SkipWhileIntAwaitWithCancellation(source, predicate, cancellationToken); + } + + class _SkipWhileIntAwaitWithCancellation : AsyncEnumeratorAwaitSelectorBase + { + Func> predicate; + int index; + + public _SkipWhileIntAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + + : base(source, cancellationToken) + { + this.predicate = predicate; + } + + protected override UniTask TransformAsync(TSource sourceCurrent) + { + if (predicate == null) + { + return CompletedTasks.False; + } + + return predicate(sourceCurrent, checked(index++), cancellationToken); + } + + protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration) + { + terminateIteration= false; + if (!awaitResult) + { + predicate = null; + Current = SourceCurrent; + return true; + } + else + { + return false; + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipWhile.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipWhile.cs.meta new file mode 100644 index 0000000..f2b210a --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/SkipWhile.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0b74b9fe361bf7148b51a29c8b2561e8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Subscribe.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Subscribe.cs new file mode 100644 index 0000000..0785bc2 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Subscribe.cs @@ -0,0 +1,536 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; +using Subscribes = Cysharp.Threading.Tasks.Linq.Subscribe; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + // OnNext + + public static IDisposable Subscribe(this IUniTaskAsyncEnumerable source, Action action) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(action, nameof(action)); + + var cts = new CancellationTokenDisposable(); + Subscribes.SubscribeCore(source, action, Subscribes.NopError, Subscribes.NopCompleted, cts.Token).Forget(); + return cts; + } + + public static IDisposable Subscribe(this IUniTaskAsyncEnumerable source, Func action) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(action, nameof(action)); + + var cts = new CancellationTokenDisposable(); + Subscribes.SubscribeCore(source, action, Subscribes.NopError, Subscribes.NopCompleted, cts.Token).Forget(); + return cts; + } + + public static IDisposable Subscribe(this IUniTaskAsyncEnumerable source, Func action) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(action, nameof(action)); + + var cts = new CancellationTokenDisposable(); + Subscribes.SubscribeCore(source, action, Subscribes.NopError, Subscribes.NopCompleted, cts.Token).Forget(); + return cts; + } + + public static void Subscribe(this IUniTaskAsyncEnumerable source, Action action, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(action, nameof(action)); + + Subscribes.SubscribeCore(source, action, Subscribes.NopError, Subscribes.NopCompleted, cancellationToken).Forget(); + } + + public static void Subscribe(this IUniTaskAsyncEnumerable source, Func action, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(action, nameof(action)); + + Subscribes.SubscribeCore(source, action, Subscribes.NopError, Subscribes.NopCompleted, cancellationToken).Forget(); + } + + public static void Subscribe(this IUniTaskAsyncEnumerable source, Func action, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(action, nameof(action)); + + Subscribes.SubscribeCore(source, action, Subscribes.NopError, Subscribes.NopCompleted, cancellationToken).Forget(); + } + + public static IDisposable SubscribeAwait(this IUniTaskAsyncEnumerable source, Func onNext) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + + var cts = new CancellationTokenDisposable(); + Subscribes.SubscribeAwaitCore(source, onNext, Subscribes.NopError, Subscribes.NopCompleted, cts.Token).Forget(); + return cts; + } + + public static void SubscribeAwait(this IUniTaskAsyncEnumerable source, Func onNext, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + + Subscribes.SubscribeAwaitCore(source, onNext, Subscribes.NopError, Subscribes.NopCompleted, cancellationToken).Forget(); + } + + public static IDisposable SubscribeAwait(this IUniTaskAsyncEnumerable source, Func onNext) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + + var cts = new CancellationTokenDisposable(); + Subscribes.SubscribeAwaitCore(source, onNext, Subscribes.NopError, Subscribes.NopCompleted, cts.Token).Forget(); + return cts; + } + + public static void SubscribeAwait(this IUniTaskAsyncEnumerable source, Func onNext, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + + Subscribes.SubscribeAwaitCore(source, onNext, Subscribes.NopError, Subscribes.NopCompleted, cancellationToken).Forget(); + } + + // OnNext, OnError + + public static IDisposable Subscribe(this IUniTaskAsyncEnumerable source, Action onNext, Action onError) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + Error.ThrowArgumentNullException(onError, nameof(onError)); + + var cts = new CancellationTokenDisposable(); + Subscribes.SubscribeCore(source, onNext, onError, Subscribes.NopCompleted, cts.Token).Forget(); + return cts; + } + + public static IDisposable Subscribe(this IUniTaskAsyncEnumerable source, Func onNext, Action onError) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + Error.ThrowArgumentNullException(onError, nameof(onError)); + + var cts = new CancellationTokenDisposable(); + Subscribes.SubscribeCore(source, onNext, onError, Subscribes.NopCompleted, cts.Token).Forget(); + return cts; + } + + public static void Subscribe(this IUniTaskAsyncEnumerable source, Action onNext, Action onError, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + Error.ThrowArgumentNullException(onError, nameof(onError)); + + Subscribes.SubscribeCore(source, onNext, onError, Subscribes.NopCompleted, cancellationToken).Forget(); + } + + public static void Subscribe(this IUniTaskAsyncEnumerable source, Func onNext, Action onError, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + Error.ThrowArgumentNullException(onError, nameof(onError)); + + Subscribes.SubscribeCore(source, onNext, onError, Subscribes.NopCompleted, cancellationToken).Forget(); + } + + public static IDisposable SubscribeAwait(this IUniTaskAsyncEnumerable source, Func onNext, Action onError) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + Error.ThrowArgumentNullException(onError, nameof(onError)); + + var cts = new CancellationTokenDisposable(); + Subscribes.SubscribeAwaitCore(source, onNext, onError, Subscribes.NopCompleted, cts.Token).Forget(); + return cts; + } + + public static void SubscribeAwait(this IUniTaskAsyncEnumerable source, Func onNext, Action onError, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + Error.ThrowArgumentNullException(onError, nameof(onError)); + + Subscribes.SubscribeAwaitCore(source, onNext, onError, Subscribes.NopCompleted, cancellationToken).Forget(); + } + + public static IDisposable SubscribeAwait(this IUniTaskAsyncEnumerable source, Func onNext, Action onError) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + Error.ThrowArgumentNullException(onError, nameof(onError)); + + var cts = new CancellationTokenDisposable(); + Subscribes.SubscribeAwaitCore(source, onNext, onError, Subscribes.NopCompleted, cts.Token).Forget(); + return cts; + } + + public static void SubscribeAwait(this IUniTaskAsyncEnumerable source, Func onNext, Action onError, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + Error.ThrowArgumentNullException(onError, nameof(onError)); + + Subscribes.SubscribeAwaitCore(source, onNext, onError, Subscribes.NopCompleted, cancellationToken).Forget(); + } + + // OnNext, OnCompleted + + public static IDisposable Subscribe(this IUniTaskAsyncEnumerable source, Action onNext, Action onCompleted) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + Error.ThrowArgumentNullException(onCompleted, nameof(onCompleted)); + + var cts = new CancellationTokenDisposable(); + Subscribes.SubscribeCore(source, onNext, Subscribes.NopError, onCompleted, cts.Token).Forget(); + return cts; + } + + public static IDisposable Subscribe(this IUniTaskAsyncEnumerable source, Func onNext, Action onCompleted) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + Error.ThrowArgumentNullException(onCompleted, nameof(onCompleted)); + + var cts = new CancellationTokenDisposable(); + Subscribes.SubscribeCore(source, onNext, Subscribes.NopError, onCompleted, cts.Token).Forget(); + return cts; + } + + public static void Subscribe(this IUniTaskAsyncEnumerable source, Action onNext, Action onCompleted, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + Error.ThrowArgumentNullException(onCompleted, nameof(onCompleted)); + + Subscribes.SubscribeCore(source, onNext, Subscribes.NopError, onCompleted, cancellationToken).Forget(); + } + + public static void Subscribe(this IUniTaskAsyncEnumerable source, Func onNext, Action onCompleted, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + Error.ThrowArgumentNullException(onCompleted, nameof(onCompleted)); + + Subscribes.SubscribeCore(source, onNext, Subscribes.NopError, onCompleted, cancellationToken).Forget(); + } + + public static IDisposable SubscribeAwait(this IUniTaskAsyncEnumerable source, Func onNext, Action onCompleted) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + Error.ThrowArgumentNullException(onCompleted, nameof(onCompleted)); + + var cts = new CancellationTokenDisposable(); + Subscribes.SubscribeAwaitCore(source, onNext, Subscribes.NopError, onCompleted, cts.Token).Forget(); + return cts; + } + + public static void SubscribeAwait(this IUniTaskAsyncEnumerable source, Func onNext, Action onCompleted, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + Error.ThrowArgumentNullException(onCompleted, nameof(onCompleted)); + + Subscribes.SubscribeAwaitCore(source, onNext, Subscribes.NopError, onCompleted, cancellationToken).Forget(); + } + + public static IDisposable SubscribeAwait(this IUniTaskAsyncEnumerable source, Func onNext, Action onCompleted) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + Error.ThrowArgumentNullException(onCompleted, nameof(onCompleted)); + + var cts = new CancellationTokenDisposable(); + Subscribes.SubscribeAwaitCore(source, onNext, Subscribes.NopError, onCompleted, cts.Token).Forget(); + return cts; + } + + public static void SubscribeAwait(this IUniTaskAsyncEnumerable source, Func onNext, Action onCompleted, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(onNext, nameof(onNext)); + Error.ThrowArgumentNullException(onCompleted, nameof(onCompleted)); + + Subscribes.SubscribeAwaitCore(source, onNext, Subscribes.NopError, onCompleted, cancellationToken).Forget(); + } + + // IObserver + + public static IDisposable Subscribe(this IUniTaskAsyncEnumerable source, IObserver observer) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(observer, nameof(observer)); + + var cts = new CancellationTokenDisposable(); + Subscribes.SubscribeCore(source, observer, cts.Token).Forget(); + return cts; + } + + public static void Subscribe(this IUniTaskAsyncEnumerable source, IObserver observer, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(observer, nameof(observer)); + + Subscribes.SubscribeCore(source, observer, cancellationToken).Forget(); + } + } + + internal sealed class CancellationTokenDisposable : IDisposable + { + readonly CancellationTokenSource cts = new CancellationTokenSource(); + + public CancellationToken Token => cts.Token; + + public void Dispose() + { + if (!cts.IsCancellationRequested) + { + cts.Cancel(); + } + } + } + + internal static class Subscribe + { + public static readonly Action NopError = _ => { }; + public static readonly Action NopCompleted = () => { }; + + public static async UniTaskVoid SubscribeCore(IUniTaskAsyncEnumerable source, Action onNext, Action onError, Action onCompleted, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + try + { + onNext(e.Current); + } + catch (Exception ex) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + } + } + onCompleted(); + } + catch (Exception ex) + { + if (onError == NopError) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + return; + } + + if (ex is OperationCanceledException) return; + + onError(ex); + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTaskVoid SubscribeCore(IUniTaskAsyncEnumerable source, Func onNext, Action onError, Action onCompleted, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + try + { + onNext(e.Current).Forget(); + } + catch (Exception ex) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + } + } + onCompleted(); + } + catch (Exception ex) + { + if (onError == NopError) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + return; + } + + if (ex is OperationCanceledException) return; + + onError(ex); + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTaskVoid SubscribeCore(IUniTaskAsyncEnumerable source, Func onNext, Action onError, Action onCompleted, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + try + { + onNext(e.Current, cancellationToken).Forget(); + } + catch (Exception ex) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + } + } + onCompleted(); + } + catch (Exception ex) + { + if (onError == NopError) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + return; + } + + if (ex is OperationCanceledException) return; + + onError(ex); + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTaskVoid SubscribeCore(IUniTaskAsyncEnumerable source, IObserver observer, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + try + { + observer.OnNext(e.Current); + } + catch (Exception ex) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + } + } + observer.OnCompleted(); + } + catch (Exception ex) + { + if (ex is OperationCanceledException) return; + + observer.OnError(ex); + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTaskVoid SubscribeAwaitCore(IUniTaskAsyncEnumerable source, Func onNext, Action onError, Action onCompleted, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + try + { + await onNext(e.Current); + } + catch (Exception ex) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + } + } + onCompleted(); + } + catch (Exception ex) + { + if (onError == NopError) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + return; + } + + if (ex is OperationCanceledException) return; + + onError(ex); + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + public static async UniTaskVoid SubscribeAwaitCore(IUniTaskAsyncEnumerable source, Func onNext, Action onError, Action onCompleted, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + try + { + await onNext(e.Current, cancellationToken); + } + catch (Exception ex) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + } + } + onCompleted(); + } + catch (Exception ex) + { + if (onError == NopError) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + return; + } + + if (ex is OperationCanceledException) return; + + onError(ex); + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Subscribe.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Subscribe.cs.meta new file mode 100644 index 0000000..ea83567 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Subscribe.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 263479eb04c189741931fc0e2f615c2d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Sum.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Sum.cs new file mode 100644 index 0000000..1101cd7 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Sum.cs @@ -0,0 +1,1244 @@ +using System; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Sum.SumAsync(source, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Sum.SumAsync(source, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Sum.SumAsync(source, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Sum.SumAsync(source, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Sum.SumAsync(source, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Sum.SumAsync(source, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Sum.SumAsync(source, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Sum.SumAsync(source, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Sum.SumAsync(source, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Sum.SumAsync(source, cancellationToken); + } + + public static UniTask SumAsync(this IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask SumAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken); + } + + } + + internal static class Sum + { + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Int32 sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += e.Current; + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Int32 sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += selector(e.Current); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int32 sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current)); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int32 sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current, cancellationToken)); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Int64 sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += e.Current; + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Int64 sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += selector(e.Current); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int64 sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current)); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int64 sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current, cancellationToken)); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Single sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += e.Current; + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Single sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += selector(e.Current); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Single sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current)); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Single sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current, cancellationToken)); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Double sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += e.Current; + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Double sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += selector(e.Current); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Double sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current)); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Double sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current, cancellationToken)); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Decimal sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += e.Current; + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Decimal sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += selector(e.Current); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Decimal sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current)); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Decimal sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current, cancellationToken)); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Int32? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += e.Current.GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Int32? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += selector(e.Current).GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int32? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current)).GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int32? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current, cancellationToken)).GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Int64? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += e.Current.GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Int64? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += selector(e.Current).GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int64? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current)).GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Int64? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current, cancellationToken)).GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Single? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += e.Current.GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Single? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += selector(e.Current).GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Single? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current)).GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Single? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current, cancellationToken)).GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Double? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += e.Current.GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Double? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += selector(e.Current).GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Double? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current)).GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Double? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current, cancellationToken)).GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Decimal? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += e.Current.GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAsync(IUniTaskAsyncEnumerable source, Func selector, CancellationToken cancellationToken) + { + Decimal? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += selector(e.Current).GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Decimal? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current)).GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask SumAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + Decimal? sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current, cancellationToken)).GetValueOrDefault(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Sum.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Sum.cs.meta new file mode 100644 index 0000000..5331e34 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Sum.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4149754066a21a341be58c04357061f6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Sum.tt b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Sum.tt new file mode 100644 index 0000000..a04983e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Sum.tt @@ -0,0 +1,167 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".cs" #> +<# + var types = new[] + { + typeof(int), + typeof(long), + typeof(float), + typeof(double), + typeof(decimal), + + typeof(int?), + typeof(long?), + typeof(float?), + typeof(double?), + typeof(decimal?), + }; + + Func IsNullable = x => x.IsGenericType; + Func TypeName = x => IsNullable(x) ? x.GetGenericArguments()[0].Name + "?" : x.Name; + Func WithSuffix = x => IsNullable(x) ? ".GetValueOrDefault()" : ""; +#> +using System; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { +<# foreach(var t in types) { #> + public static UniTask<<#= TypeName(t) #>> SumAsync(this IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Sum.SumAsync(source, cancellationToken); + } + + public static UniTask<<#= TypeName(t) #>> SumAsync(this IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAsync(source, selector, cancellationToken); + } + + public static UniTask<<#= TypeName(t) #>> SumAwaitAsync(this IUniTaskAsyncEnumerable source, Func>> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitAsync(source, selector, cancellationToken); + } + + public static UniTask<<#= TypeName(t) #>> SumAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func>> selector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(selector)); + + return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken); + } + +<# } #> + } + + internal static class Sum + { +<# foreach(var t in types) { #> + public static async UniTask<<#= TypeName(t) #>> SumAsync(IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken) + { + <#= TypeName(t) #> sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += e.Current<#= WithSuffix(t) #>; + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask<<#= TypeName(t) #>> SumAsync(IUniTaskAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) + { + <#= TypeName(t) #> sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += selector(e.Current)<#= WithSuffix(t) #>; + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask<<#= TypeName(t) #>> SumAwaitAsync(IUniTaskAsyncEnumerable source, Func>> selector, CancellationToken cancellationToken) + { + <#= TypeName(t) #> sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current))<#= WithSuffix(t) #>; + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + + public static async UniTask<<#= TypeName(t) #>> SumAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func>> selector, CancellationToken cancellationToken) + { + <#= TypeName(t) #> sum = default; + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + sum += (await selector(e.Current, cancellationToken))<#= WithSuffix(t) #>; + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return sum; + } + +<# } #> + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Sum.tt.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Sum.tt.meta new file mode 100644 index 0000000..eaec35c --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Sum.tt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: b61271ca8e712494ab1ce2d10b180b6f +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Take.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Take.cs new file mode 100644 index 0000000..6cd4eda --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Take.cs @@ -0,0 +1,124 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Take(this IUniTaskAsyncEnumerable source, Int32 count) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new Take(source, count); + } + } + + internal sealed class Take : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly int count; + + public Take(IUniTaskAsyncEnumerable source, int count) + { + this.source = source; + this.count = count; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Take(source, count, cancellationToken); + } + + sealed class _Take : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action MoveNextCoreDelegate = MoveNextCore; + + readonly IUniTaskAsyncEnumerable source; + readonly int count; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + int index; + + public _Take(IUniTaskAsyncEnumerable source, int count, CancellationToken cancellationToken) + { + this.source = source; + this.count = count; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + if (enumerator == null) + { + enumerator = source.GetAsyncEnumerator(cancellationToken); + } + + if (checked(index) >= count) + { + return CompletedTasks.False; + } + + completionSource.Reset(); + SourceMoveNext(); + return new UniTask(this, completionSource.Version); + } + + void SourceMoveNext() + { + try + { + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + MoveNextCore(this); + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + static void MoveNextCore(object state) + { + var self = (_Take)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + self.index++; + self.Current = self.enumerator.Current; + self.completionSource.TrySetResult(true); + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Take.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Take.cs.meta new file mode 100644 index 0000000..1cc91ab --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Take.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 42f02cb84e5875b488304755d0e1383d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeLast.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeLast.cs new file mode 100644 index 0000000..ca0084e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeLast.cs @@ -0,0 +1,175 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable TakeLast(this IUniTaskAsyncEnumerable source, Int32 count) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + // non take. + if (count <= 0) + { + return Empty(); + } + + return new TakeLast(source, count); + } + } + + internal sealed class TakeLast : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly int count; + + public TakeLast(IUniTaskAsyncEnumerable source, int count) + { + this.source = source; + this.count = count; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _TakeLast(source, count, cancellationToken); + } + + sealed class _TakeLast : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action MoveNextCoreDelegate = MoveNextCore; + + readonly IUniTaskAsyncEnumerable source; + readonly int count; + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + Queue queue; + + bool iterateCompleted; + bool continueNext; + + public _TakeLast(IUniTaskAsyncEnumerable source, int count, CancellationToken cancellationToken) + { + this.source = source; + this.count = count; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + if (enumerator == null) + { + enumerator = source.GetAsyncEnumerator(cancellationToken); + queue = new Queue(); + } + + completionSource.Reset(); + SourceMoveNext(); + return new UniTask(this, completionSource.Version); + } + + void SourceMoveNext() + { + if (iterateCompleted) + { + if (queue.Count > 0) + { + Current = queue.Dequeue(); + completionSource.TrySetResult(true); + } + else + { + completionSource.TrySetResult(false); + } + + return; + } + + try + { + LOOP: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + continueNext = true; + MoveNextCore(this); + if (continueNext) + { + continueNext = false; + goto LOOP; // avoid recursive + } + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + + static void MoveNextCore(object state) + { + var self = (_TakeLast)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + if (self.queue.Count < self.count) + { + self.queue.Enqueue(self.enumerator.Current); + + if (!self.continueNext) + { + self.SourceMoveNext(); + } + } + else + { + self.queue.Dequeue(); + self.queue.Enqueue(self.enumerator.Current); + + if (!self.continueNext) + { + self.SourceMoveNext(); + } + } + } + else + { + self.continueNext = false; + self.iterateCompleted = true; + self.SourceMoveNext(); + } + } + else + { + self.continueNext = false; + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeLast.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeLast.cs.meta new file mode 100644 index 0000000..d80037f --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeLast.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 510aa9fd35b45fc40bcdb7e59f01fd1b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeUntil.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeUntil.cs new file mode 100644 index 0000000..25371ad --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeUntil.cs @@ -0,0 +1,190 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable TakeUntil(this IUniTaskAsyncEnumerable source, UniTask other) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new TakeUntil(source, other, null); + } + + public static IUniTaskAsyncEnumerable TakeUntil(this IUniTaskAsyncEnumerable source, Func other) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(source, nameof(other)); + + return new TakeUntil(source, default, other); + } + } + + internal sealed class TakeUntil : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly UniTask other; + readonly Func other2; + + public TakeUntil(IUniTaskAsyncEnumerable source, UniTask other, Func other2) + { + this.source = source; + this.other = other; + this.other2 = other2; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + if (other2 != null) + { + return new _TakeUntil(source, this.other2(cancellationToken), cancellationToken); + } + else + { + return new _TakeUntil(source, this.other, cancellationToken); + } + } + + sealed class _TakeUntil : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action CancelDelegate1 = OnCanceled1; + static readonly Action MoveNextCoreDelegate = MoveNextCore; + + readonly IUniTaskAsyncEnumerable source; + CancellationToken cancellationToken1; + CancellationTokenRegistration cancellationTokenRegistration1; + + bool completed; + Exception exception; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + + public _TakeUntil(IUniTaskAsyncEnumerable source, UniTask other, CancellationToken cancellationToken1) + { + this.source = source; + this.cancellationToken1 = cancellationToken1; + + if (cancellationToken1.CanBeCanceled) + { + this.cancellationTokenRegistration1 = cancellationToken1.RegisterWithoutCaptureExecutionContext(CancelDelegate1, this); + } + + TaskTracker.TrackActiveTask(this, 3); + + RunOther(other).Forget(); + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (completed) + { + return CompletedTasks.False; + } + + if (exception != null) + { + return UniTask.FromException(exception); + } + + if (cancellationToken1.IsCancellationRequested) + { + return UniTask.FromCanceled(cancellationToken1); + } + + if (enumerator == null) + { + enumerator = source.GetAsyncEnumerator(cancellationToken1); + } + + completionSource.Reset(); + SourceMoveNext(); + return new UniTask(this, completionSource.Version); + } + + void SourceMoveNext() + { + try + { + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + MoveNextCore(this); + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + static void MoveNextCore(object state) + { + var self = (_TakeUntil)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + if (self.exception != null) + { + self.completionSource.TrySetException(self.exception); + } + else if (self.cancellationToken1.IsCancellationRequested) + { + self.completionSource.TrySetCanceled(self.cancellationToken1); + } + else + { + self.Current = self.enumerator.Current; + self.completionSource.TrySetResult(true); + } + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + async UniTaskVoid RunOther(UniTask other) + { + try + { + await other; + completed = true; + completionSource.TrySetResult(false); + } + catch (Exception ex) + { + exception = ex; + completionSource.TrySetException(ex); + } + } + + static void OnCanceled1(object state) + { + var self = (_TakeUntil)state; + self.completionSource.TrySetCanceled(self.cancellationToken1); + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + cancellationTokenRegistration1.Dispose(); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeUntil.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeUntil.cs.meta new file mode 100644 index 0000000..44cf63e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeUntil.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 12bda324162f15349afefc2c152ac07f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeUntilCanceled.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeUntilCanceled.cs new file mode 100644 index 0000000..67ee3c8 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeUntilCanceled.cs @@ -0,0 +1,164 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable TakeUntilCanceled(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new TakeUntilCanceled(source, cancellationToken); + } + } + + internal sealed class TakeUntilCanceled : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly CancellationToken cancellationToken; + + public TakeUntilCanceled(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + this.source = source; + this.cancellationToken = cancellationToken; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _TakeUntilCanceled(source, this.cancellationToken, cancellationToken); + } + + sealed class _TakeUntilCanceled : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action CancelDelegate1 = OnCanceled1; + static readonly Action CancelDelegate2 = OnCanceled2; + static readonly Action MoveNextCoreDelegate = MoveNextCore; + + readonly IUniTaskAsyncEnumerable source; + CancellationToken cancellationToken1; + CancellationToken cancellationToken2; + CancellationTokenRegistration cancellationTokenRegistration1; + CancellationTokenRegistration cancellationTokenRegistration2; + + bool isCanceled; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + + public _TakeUntilCanceled(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken1, CancellationToken cancellationToken2) + { + this.source = source; + this.cancellationToken1 = cancellationToken1; + this.cancellationToken2 = cancellationToken2; + + if (cancellationToken1.CanBeCanceled) + { + this.cancellationTokenRegistration1 = cancellationToken1.RegisterWithoutCaptureExecutionContext(CancelDelegate1, this); + } + + if (cancellationToken1 != cancellationToken2 && cancellationToken2.CanBeCanceled) + { + this.cancellationTokenRegistration2 = cancellationToken2.RegisterWithoutCaptureExecutionContext(CancelDelegate2, this); + } + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (cancellationToken1.IsCancellationRequested) isCanceled = true; + if (cancellationToken2.IsCancellationRequested) isCanceled = true; + + if (enumerator == null) + { + enumerator = source.GetAsyncEnumerator(cancellationToken2); // use only AsyncEnumerator provided token. + } + + if (isCanceled) return CompletedTasks.False; + + completionSource.Reset(); + SourceMoveNext(); + return new UniTask(this, completionSource.Version); + } + + void SourceMoveNext() + { + try + { + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + MoveNextCore(this); + } + else + { + awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + } + } + + static void MoveNextCore(object state) + { + var self = (_TakeUntilCanceled)state; + + if (self.TryGetResult(self.awaiter, out var result)) + { + if (result) + { + if (self.isCanceled) + { + self.completionSource.TrySetResult(false); + } + else + { + self.Current = self.enumerator.Current; + self.completionSource.TrySetResult(true); + } + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + static void OnCanceled1(object state) + { + var self = (_TakeUntilCanceled)state; + if (!self.isCanceled) + { + self.cancellationTokenRegistration2.Dispose(); + self.completionSource.TrySetResult(false); + } + } + + static void OnCanceled2(object state) + { + var self = (_TakeUntilCanceled)state; + if (!self.isCanceled) + { + self.cancellationTokenRegistration1.Dispose(); + self.completionSource.TrySetResult(false); + } + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + cancellationTokenRegistration1.Dispose(); + cancellationTokenRegistration2.Dispose(); + if (enumerator != null) + { + return enumerator.DisposeAsync(); + } + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeUntilCanceled.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeUntilCanceled.cs.meta new file mode 100644 index 0000000..4a89be5 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeUntilCanceled.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e82f498cf3a1df04cbf646773fc11319 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeWhile.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeWhile.cs new file mode 100644 index 0000000..6239c77 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeWhile.cs @@ -0,0 +1,342 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable TakeWhile(this IUniTaskAsyncEnumerable source, Func predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new TakeWhile(source, predicate); + } + + public static IUniTaskAsyncEnumerable TakeWhile(this IUniTaskAsyncEnumerable source, Func predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new TakeWhileInt(source, predicate); + } + + public static IUniTaskAsyncEnumerable TakeWhileAwait(this IUniTaskAsyncEnumerable source, Func> predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new TakeWhileAwait(source, predicate); + } + + public static IUniTaskAsyncEnumerable TakeWhileAwait(this IUniTaskAsyncEnumerable source, Func> predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new TakeWhileIntAwait(source, predicate); + } + + public static IUniTaskAsyncEnumerable TakeWhileAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new TakeWhileAwaitWithCancellation(source, predicate); + } + + public static IUniTaskAsyncEnumerable TakeWhileAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new TakeWhileIntAwaitWithCancellation(source, predicate); + } + } + + internal sealed class TakeWhile : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func predicate; + + public TakeWhile(IUniTaskAsyncEnumerable source, Func predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _TakeWhile(source, predicate, cancellationToken); + } + + class _TakeWhile : AsyncEnumeratorBase + { + Func predicate; + + public _TakeWhile(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) + + : base(source, cancellationToken) + { + this.predicate = predicate; + } + + protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result) + { + if (sourceHasCurrent) + { + if (predicate(SourceCurrent)) + { + Current = SourceCurrent; + result = true; + return true; + } + } + + result = false; + return true; + } + } + } + + internal sealed class TakeWhileInt : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func predicate; + + public TakeWhileInt(IUniTaskAsyncEnumerable source, Func predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _TakeWhileInt(source, predicate, cancellationToken); + } + + class _TakeWhileInt : AsyncEnumeratorBase + { + readonly Func predicate; + int index; + + public _TakeWhileInt(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) + + : base(source, cancellationToken) + { + this.predicate = predicate; + } + + protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result) + { + if (sourceHasCurrent) + { + if (predicate(SourceCurrent, checked(index++))) + { + Current = SourceCurrent; + result = true; + return true; + } + } + + result = false; + return true; + } + } + } + + internal sealed class TakeWhileAwait : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> predicate; + + public TakeWhileAwait(IUniTaskAsyncEnumerable source, Func> predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _TakeWhileAwait(source, predicate, cancellationToken); + } + + class _TakeWhileAwait : AsyncEnumeratorAwaitSelectorBase + { + Func> predicate; + + public _TakeWhileAwait(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + : base(source, cancellationToken) + { + this.predicate = predicate; + } + + protected override UniTask TransformAsync(TSource sourceCurrent) + { + return predicate(sourceCurrent); + } + + protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration) + { + if (awaitResult) + { + Current = SourceCurrent; + terminateIteration = false; + return true; + } + else + { + terminateIteration = true; + return false; + } + } + } + } + + internal sealed class TakeWhileIntAwait : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> predicate; + + public TakeWhileIntAwait(IUniTaskAsyncEnumerable source, Func> predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _TakeWhileIntAwait(source, predicate, cancellationToken); + } + + class _TakeWhileIntAwait : AsyncEnumeratorAwaitSelectorBase + { + readonly Func> predicate; + int index; + + public _TakeWhileIntAwait(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + : base(source, cancellationToken) + { + this.predicate = predicate; + } + + protected override UniTask TransformAsync(TSource sourceCurrent) + { + return predicate(sourceCurrent, checked(index++)); + } + + protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration) + { + if (awaitResult) + { + Current = SourceCurrent; + terminateIteration = false; + return true; + } + else + { + terminateIteration = true; + return false; + } + } + } + } + + internal sealed class TakeWhileAwaitWithCancellation : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> predicate; + + public TakeWhileAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _TakeWhileAwaitWithCancellation(source, predicate, cancellationToken); + } + + class _TakeWhileAwaitWithCancellation : AsyncEnumeratorAwaitSelectorBase + { + Func> predicate; + + public _TakeWhileAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + : base(source, cancellationToken) + { + this.predicate = predicate; + } + + protected override UniTask TransformAsync(TSource sourceCurrent) + { + return predicate(sourceCurrent, cancellationToken); + } + + protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration) + { + if (awaitResult) + { + Current = SourceCurrent; + terminateIteration = false; + return true; + } + else + { + terminateIteration = true; + return false; + } + } + } + } + + internal sealed class TakeWhileIntAwaitWithCancellation : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> predicate; + + public TakeWhileIntAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _TakeWhileIntAwaitWithCancellation(source, predicate, cancellationToken); + } + + class _TakeWhileIntAwaitWithCancellation : AsyncEnumeratorAwaitSelectorBase + { + readonly Func> predicate; + int index; + + public _TakeWhileIntAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + : base(source, cancellationToken) + { + this.predicate = predicate; + } + + protected override UniTask TransformAsync(TSource sourceCurrent) + { + return predicate(sourceCurrent, checked(index++), cancellationToken); + } + + protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration) + { + if (awaitResult) + { + Current = SourceCurrent; + terminateIteration = false; + return true; + } + else + { + terminateIteration = true; + return false; + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeWhile.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeWhile.cs.meta new file mode 100644 index 0000000..f2173d5 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/TakeWhile.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bca55adabcc4b3141b50b8b09634f764 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Throw.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Throw.cs new file mode 100644 index 0000000..b6994c4 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Throw.cs @@ -0,0 +1,54 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Throw(Exception exception) + { + return new Throw(exception); + } + } + + internal class Throw : IUniTaskAsyncEnumerable + { + readonly Exception exception; + + public Throw(Exception exception) + { + this.exception = exception; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Throw(exception, cancellationToken); + } + + class _Throw : IUniTaskAsyncEnumerator + { + readonly Exception exception; + CancellationToken cancellationToken; + + public _Throw(Exception exception, CancellationToken cancellationToken) + { + this.exception = exception; + this.cancellationToken = cancellationToken; + } + + public TValue Current => default; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + return UniTask.FromException(exception); + } + + public UniTask DisposeAsync() + { + return default; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Throw.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Throw.cs.meta new file mode 100644 index 0000000..c768ef1 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Throw.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9d05a7d4f4161e549b4789e1022baae8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToArray.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToArray.cs new file mode 100644 index 0000000..3554968 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToArray.cs @@ -0,0 +1,60 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask ToArrayAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Cysharp.Threading.Tasks.Linq.ToArray.ToArrayAsync(source, cancellationToken); + } + } + + internal static class ToArray + { + internal static async UniTask ToArrayAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + var pool = ArrayPool.Shared; + var array = pool.Rent(16); + + TSource[] result = default; + IUniTaskAsyncEnumerator e = default; + try + { + e = source.GetAsyncEnumerator(cancellationToken); + var i = 0; + while (await e.MoveNextAsync()) + { + ArrayPoolUtil.EnsureCapacity(ref array, i, pool); + array[i++] = e.Current; + } + + if (i == 0) + { + result = Array.Empty(); + } + else + { + result = new TSource[i]; + Array.Copy(array, result, i); + } + } + finally + { + pool.Return(array, clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType()); + + if (e != null) + { + await e.DisposeAsync(); + } + } + + return result; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToArray.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToArray.cs.meta new file mode 100644 index 0000000..679d61c --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToArray.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: debb010bbb1622e43b94fe70ec0133dd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToDictionary.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToDictionary.cs new file mode 100644 index 0000000..083ace0 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToDictionary.cs @@ -0,0 +1,278 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask> ToDictionaryAsync(this IUniTaskAsyncEnumerable source, Func keySelector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return ToDictionary.ToDictionaryAsync(source, keySelector, EqualityComparer.Default, cancellationToken); + } + + public static UniTask> ToDictionaryAsync(this IUniTaskAsyncEnumerable source, Func keySelector, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return ToDictionary.ToDictionaryAsync(source, keySelector, comparer, cancellationToken); + } + + public static UniTask> ToDictionaryAsync(this IUniTaskAsyncEnumerable source, Func keySelector, Func elementSelector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + + return ToDictionary.ToDictionaryAsync(source, keySelector, elementSelector, EqualityComparer.Default, cancellationToken); + } + + public static UniTask> ToDictionaryAsync(this IUniTaskAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return ToDictionary.ToDictionaryAsync(source, keySelector, elementSelector, comparer, cancellationToken); + } + + public static UniTask> ToDictionaryAwaitAsync(this IUniTaskAsyncEnumerable source, Func> keySelector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return ToDictionary.ToDictionaryAwaitAsync(source, keySelector, EqualityComparer.Default, cancellationToken); + } + + public static UniTask> ToDictionaryAwaitAsync(this IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return ToDictionary.ToDictionaryAwaitAsync(source, keySelector, comparer, cancellationToken); + } + + public static UniTask> ToDictionaryAwaitAsync(this IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + + return ToDictionary.ToDictionaryAwaitAsync(source, keySelector, elementSelector, EqualityComparer.Default, cancellationToken); + } + + public static UniTask> ToDictionaryAwaitAsync(this IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return ToDictionary.ToDictionaryAwaitAsync(source, keySelector, elementSelector, comparer, cancellationToken); + } + + public static UniTask> ToDictionaryAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> keySelector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return ToDictionary.ToDictionaryAwaitWithCancellationAsync(source, keySelector, EqualityComparer.Default, cancellationToken); + } + + public static UniTask> ToDictionaryAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return ToDictionary.ToDictionaryAwaitWithCancellationAsync(source, keySelector, comparer, cancellationToken); + } + + public static UniTask> ToDictionaryAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + + return ToDictionary.ToDictionaryAwaitWithCancellationAsync(source, keySelector, elementSelector, EqualityComparer.Default, cancellationToken); + } + + public static UniTask> ToDictionaryAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return ToDictionary.ToDictionaryAwaitWithCancellationAsync(source, keySelector, elementSelector, comparer, cancellationToken); + } + } + + internal static class ToDictionary + { + internal static async UniTask> ToDictionaryAsync(IUniTaskAsyncEnumerable source, Func keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var dict = new Dictionary(comparer); + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = e.Current; + var key = keySelector(v); + dict.Add(key, v); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return dict; + } + + internal static async UniTask> ToDictionaryAsync(IUniTaskAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var dict = new Dictionary(comparer); + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = e.Current; + var key = keySelector(v); + var value = elementSelector(v); + dict.Add(key, value); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return dict; + } + + // with await + + internal static async UniTask> ToDictionaryAwaitAsync(IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var dict = new Dictionary(comparer); + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = e.Current; + var key = await keySelector(v); + dict.Add(key, v); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return dict; + } + + internal static async UniTask> ToDictionaryAwaitAsync(IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var dict = new Dictionary(comparer); + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = e.Current; + var key = await keySelector(v); + var value = await elementSelector(v); + dict.Add(key, value); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return dict; + } + + // with cancellation + + internal static async UniTask> ToDictionaryAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var dict = new Dictionary(comparer); + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = e.Current; + var key = await keySelector(v, cancellationToken); + dict.Add(key, v); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return dict; + } + + internal static async UniTask> ToDictionaryAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var dict = new Dictionary(comparer); + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + var v = e.Current; + var key = await keySelector(v, cancellationToken); + var value = await elementSelector(v, cancellationToken); + dict.Add(key, value); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return dict; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToDictionary.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToDictionary.cs.meta new file mode 100644 index 0000000..4deed19 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToDictionary.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 03b109b1fe1f2df46aa56ffb26747654 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToHashSet.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToHashSet.cs new file mode 100644 index 0000000..d058cb1 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToHashSet.cs @@ -0,0 +1,50 @@ +using Cysharp.Threading.Tasks.Internal; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask> ToHashSetAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Cysharp.Threading.Tasks.Linq.ToHashSet.ToHashSetAsync(source, EqualityComparer.Default, cancellationToken); + } + + public static UniTask> ToHashSetAsync(this IUniTaskAsyncEnumerable source, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return Cysharp.Threading.Tasks.Linq.ToHashSet.ToHashSetAsync(source, comparer, cancellationToken); + } + } + + internal static class ToHashSet + { + internal static async UniTask> ToHashSetAsync(IUniTaskAsyncEnumerable source, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var set = new HashSet(comparer); + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + set.Add(e.Current); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return set; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToHashSet.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToHashSet.cs.meta new file mode 100644 index 0000000..8d3c4af --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToHashSet.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7a3e552113af96e4986805ec3c4fc80a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToList.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToList.cs new file mode 100644 index 0000000..e6fa35e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToList.cs @@ -0,0 +1,42 @@ +using Cysharp.Threading.Tasks.Internal; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask> ToListAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Cysharp.Threading.Tasks.Linq.ToList.ToListAsync(source, cancellationToken); + } + } + + internal static class ToList + { + internal static async UniTask> ToListAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + { + var list = new List(); + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + list.Add(e.Current); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + + return list; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToList.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToList.cs.meta new file mode 100644 index 0000000..4f09373 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToList.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3859c1b31e81d9b44b282e7d97e11635 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToLookup.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToLookup.cs new file mode 100644 index 0000000..015c1c0 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToLookup.cs @@ -0,0 +1,554 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static UniTask> ToLookupAsync(this IUniTaskAsyncEnumerable source, Func keySelector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return ToLookup.ToLookupAsync(source, keySelector, EqualityComparer.Default, cancellationToken); + } + + public static UniTask> ToLookupAsync(this IUniTaskAsyncEnumerable source, Func keySelector, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return ToLookup.ToLookupAsync(source, keySelector, comparer, cancellationToken); + } + + public static UniTask> ToLookupAsync(this IUniTaskAsyncEnumerable source, Func keySelector, Func elementSelector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + + return ToLookup.ToLookupAsync(source, keySelector, elementSelector, EqualityComparer.Default, cancellationToken); + } + + public static UniTask> ToLookupAsync(this IUniTaskAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return ToLookup.ToLookupAsync(source, keySelector, elementSelector, comparer, cancellationToken); + } + + public static UniTask> ToLookupAwaitAsync(this IUniTaskAsyncEnumerable source, Func> keySelector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return ToLookup.ToLookupAwaitAsync(source, keySelector, EqualityComparer.Default, cancellationToken); + } + + public static UniTask> ToLookupAwaitAsync(this IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return ToLookup.ToLookupAwaitAsync(source, keySelector, comparer, cancellationToken); + } + + public static UniTask> ToLookupAwaitAsync(this IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + + return ToLookup.ToLookupAwaitAsync(source, keySelector, elementSelector, EqualityComparer.Default, cancellationToken); + } + + public static UniTask> ToLookupAwaitAsync(this IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return ToLookup.ToLookupAwaitAsync(source, keySelector, elementSelector, comparer, cancellationToken); + } + + public static UniTask> ToLookupAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> keySelector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + + return ToLookup.ToLookupAwaitWithCancellationAsync(source, keySelector, EqualityComparer.Default, cancellationToken); + } + + public static UniTask> ToLookupAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return ToLookup.ToLookupAwaitWithCancellationAsync(source, keySelector, comparer, cancellationToken); + } + + public static UniTask> ToLookupAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + + return ToLookup.ToLookupAwaitWithCancellationAsync(source, keySelector, elementSelector, EqualityComparer.Default, cancellationToken); + } + + public static UniTask> ToLookupAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); + Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return ToLookup.ToLookupAwaitWithCancellationAsync(source, keySelector, elementSelector, comparer, cancellationToken); + } + } + + internal static class ToLookup + { + internal static async UniTask> ToLookupAsync(IUniTaskAsyncEnumerable source, Func keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var pool = ArrayPool.Shared; + var array = pool.Rent(16); + + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + var i = 0; + while (await e.MoveNextAsync()) + { + ArrayPoolUtil.EnsureCapacity(ref array, i, pool); + array[i++] = e.Current; + } + + if (i == 0) + { + return Lookup.CreateEmpty(); + } + else + { + return Lookup.Create(new ArraySegment(array, 0, i), keySelector, comparer); + } + } + finally + { + pool.Return(array, clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType()); + + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + internal static async UniTask> ToLookupAsync(IUniTaskAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var pool = ArrayPool.Shared; + var array = pool.Rent(16); + + IUniTaskAsyncEnumerator e = default; + try + { + e = source.GetAsyncEnumerator(cancellationToken); + var i = 0; + while (await e.MoveNextAsync()) + { + ArrayPoolUtil.EnsureCapacity(ref array, i, pool); + array[i++] = e.Current; + } + + if (i == 0) + { + return Lookup.CreateEmpty(); + } + else + { + return Lookup.Create(new ArraySegment(array, 0, i), keySelector, elementSelector, comparer); + } + } + finally + { + pool.Return(array, clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType()); + + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + + // with await + + internal static async UniTask> ToLookupAwaitAsync(IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var pool = ArrayPool.Shared; + var array = pool.Rent(16); + + IUniTaskAsyncEnumerator e = default; + try + { + e = source.GetAsyncEnumerator(cancellationToken); + var i = 0; + while (await e.MoveNextAsync()) + { + ArrayPoolUtil.EnsureCapacity(ref array, i, pool); + array[i++] = e.Current; + } + + if (i == 0) + { + return Lookup.CreateEmpty(); + } + else + { + return await Lookup.CreateAsync(new ArraySegment(array, 0, i), keySelector, comparer); + } + } + finally + { + pool.Return(array, clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType()); + + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + internal static async UniTask> ToLookupAwaitAsync(IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var pool = ArrayPool.Shared; + var array = pool.Rent(16); + + IUniTaskAsyncEnumerator e = default; + try + { + e = source.GetAsyncEnumerator(cancellationToken); + var i = 0; + while (await e.MoveNextAsync()) + { + ArrayPoolUtil.EnsureCapacity(ref array, i, pool); + array[i++] = e.Current; + } + + if (i == 0) + { + return Lookup.CreateEmpty(); + } + else + { + return await Lookup.CreateAsync(new ArraySegment(array, 0, i), keySelector, elementSelector, comparer); + } + } + finally + { + pool.Return(array, clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType()); + + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + // with cancellation + + internal static async UniTask> ToLookupAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var pool = ArrayPool.Shared; + var array = pool.Rent(16); + + IUniTaskAsyncEnumerator e = default; + try + { + e = source.GetAsyncEnumerator(cancellationToken); + var i = 0; + while (await e.MoveNextAsync()) + { + ArrayPoolUtil.EnsureCapacity(ref array, i, pool); + array[i++] = e.Current; + } + + if (i == 0) + { + return Lookup.CreateEmpty(); + } + else + { + return await Lookup.CreateAsync(new ArraySegment(array, 0, i), keySelector, comparer, cancellationToken); + } + } + finally + { + pool.Return(array, clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType()); + + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + internal static async UniTask> ToLookupAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var pool = ArrayPool.Shared; + var array = pool.Rent(16); + + IUniTaskAsyncEnumerator e = default; + try + { + e = source.GetAsyncEnumerator(cancellationToken); + var i = 0; + while (await e.MoveNextAsync()) + { + ArrayPoolUtil.EnsureCapacity(ref array, i, pool); + array[i++] = e.Current; + } + + if (i == 0) + { + return Lookup.CreateEmpty(); + } + else + { + return await Lookup.CreateAsync(new ArraySegment(array, 0, i), keySelector, elementSelector, comparer, cancellationToken); + } + } + finally + { + pool.Return(array, clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType()); + + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + // Lookup + + class Lookup : ILookup + { + static readonly Lookup empty = new Lookup(new Dictionary>()); + + // original lookup keeps order but this impl does not(dictionary not guarantee) + readonly Dictionary> dict; + + Lookup(Dictionary> dict) + { + this.dict = dict; + } + + public static Lookup CreateEmpty() + { + return empty; + } + + public static Lookup Create(ArraySegment source, Func keySelector, IEqualityComparer comparer) + { + var dict = new Dictionary>(comparer); + + var arr = source.Array; + var c = source.Count; + for (int i = source.Offset; i < c; i++) + { + var key = keySelector(arr[i]); + + if (!dict.TryGetValue(key, out var list)) + { + list = new Grouping(key); + dict[key] = list; + } + + list.Add(arr[i]); + } + + return new Lookup(dict); + } + + public static Lookup Create(ArraySegment source, Func keySelector, Func elementSelector, IEqualityComparer comparer) + { + var dict = new Dictionary>(comparer); + + var arr = source.Array; + var c = source.Count; + for (int i = source.Offset; i < c; i++) + { + var key = keySelector(arr[i]); + var elem = elementSelector(arr[i]); + + if (!dict.TryGetValue(key, out var list)) + { + list = new Grouping(key); + dict[key] = list; + } + + list.Add(elem); + } + + return new Lookup(dict); + } + + public static async UniTask> CreateAsync(ArraySegment source, Func> keySelector, IEqualityComparer comparer) + { + var dict = new Dictionary>(comparer); + + var arr = source.Array; + var c = source.Count; + for (int i = source.Offset; i < c; i++) + { + var key = await keySelector(arr[i]); + + if (!dict.TryGetValue(key, out var list)) + { + list = new Grouping(key); + dict[key] = list; + } + + list.Add(arr[i]); + } + + return new Lookup(dict); + } + + public static async UniTask> CreateAsync(ArraySegment source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer) + { + var dict = new Dictionary>(comparer); + + var arr = source.Array; + var c = source.Count; + for (int i = source.Offset; i < c; i++) + { + var key = await keySelector(arr[i]); + var elem = await elementSelector(arr[i]); + + if (!dict.TryGetValue(key, out var list)) + { + list = new Grouping(key); + dict[key] = list; + } + + list.Add(elem); + } + + return new Lookup(dict); + } + + public static async UniTask> CreateAsync(ArraySegment source, Func> keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var dict = new Dictionary>(comparer); + + var arr = source.Array; + var c = source.Count; + for (int i = source.Offset; i < c; i++) + { + var key = await keySelector(arr[i], cancellationToken); + + if (!dict.TryGetValue(key, out var list)) + { + list = new Grouping(key); + dict[key] = list; + } + + list.Add(arr[i]); + } + + return new Lookup(dict); + } + + public static async UniTask> CreateAsync(ArraySegment source, Func> keySelector, Func> elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var dict = new Dictionary>(comparer); + + var arr = source.Array; + var c = source.Count; + for (int i = source.Offset; i < c; i++) + { + var key = await keySelector(arr[i], cancellationToken); + var elem = await elementSelector(arr[i], cancellationToken); + + if (!dict.TryGetValue(key, out var list)) + { + list = new Grouping(key); + dict[key] = list; + } + + list.Add(elem); + } + + return new Lookup(dict); + } + + public IEnumerable this[TKey key] => dict.TryGetValue(key, out var g) ? g : Enumerable.Empty(); + + public int Count => dict.Count; + + public bool Contains(TKey key) + { + return dict.ContainsKey(key); + } + + public IEnumerator> GetEnumerator() + { + return dict.Values.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return dict.Values.GetEnumerator(); + } + } + + class Grouping : IGrouping // , IUniTaskAsyncGrouping + { + readonly List elements; + + public TKey Key { get; private set; } + + public Grouping(TKey key) + { + this.Key = key; + this.elements = new List(); + } + + public void Add(TElement value) + { + elements.Add(value); + } + public IEnumerator GetEnumerator() + { + return elements.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return elements.GetEnumerator(); + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return this.ToUniTaskAsyncEnumerable().GetAsyncEnumerator(cancellationToken); + } + + public override string ToString() + { + return "Key: " + Key + ", Count: " + elements.Count; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToLookup.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToLookup.cs.meta new file mode 100644 index 0000000..7dd8ecd --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToLookup.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 57da22563bcd6ca4aaf256d941de5cb0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToObservable.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToObservable.cs new file mode 100644 index 0000000..4f48388 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToObservable.cs @@ -0,0 +1,97 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IObservable ToObservable(this IUniTaskAsyncEnumerable source) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new ToObservable(source); + } + } + + internal sealed class ToObservable : IObservable + { + readonly IUniTaskAsyncEnumerable source; + + public ToObservable(IUniTaskAsyncEnumerable source) + { + this.source = source; + } + + public IDisposable Subscribe(IObserver observer) + { + var ctd = new CancellationTokenDisposable(); + + RunAsync(source, observer, ctd.Token).Forget(); + + return ctd; + } + + static async UniTaskVoid RunAsync(IUniTaskAsyncEnumerable src, IObserver observer, CancellationToken cancellationToken) + { + // cancellationToken.IsCancellationRequested is called when Rx's Disposed. + // when disposed, finish silently. + + var e = src.GetAsyncEnumerator(cancellationToken); + try + { + bool hasNext; + + do + { + try + { + hasNext = await e.MoveNextAsync(); + } + catch (Exception ex) + { + if (cancellationToken.IsCancellationRequested) + { + return; + } + + observer.OnError(ex); + return; + } + + if (hasNext) + { + observer.OnNext(e.Current); + } + else + { + observer.OnCompleted(); + return; + } + } while (!cancellationToken.IsCancellationRequested); + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + internal sealed class CancellationTokenDisposable : IDisposable + { + readonly CancellationTokenSource cts = new CancellationTokenSource(); + + public CancellationToken Token => cts.Token; + + public void Dispose() + { + if (!cts.IsCancellationRequested) + { + cts.Cancel(); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToObservable.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToObservable.cs.meta new file mode 100644 index 0000000..44d917e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToObservable.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b4f6f48a532188e4c80b7ebe69aea3a8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToUniTaskAsyncEnumerable.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToUniTaskAsyncEnumerable.cs new file mode 100644 index 0000000..02523c6 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToUniTaskAsyncEnumerable.cs @@ -0,0 +1,1115 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Runtime.ExceptionServices; +using System.Threading; +using System.Threading.Tasks; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable ToUniTaskAsyncEnumerable(this IEnumerable source) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new ToUniTaskAsyncEnumerable(source); + } + + public static IUniTaskAsyncEnumerable ToUniTaskAsyncEnumerable(this Task source) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new ToUniTaskAsyncEnumerableTask(source); + } + + public static IUniTaskAsyncEnumerable ToUniTaskAsyncEnumerable(this UniTask source) + { + return new ToUniTaskAsyncEnumerableUniTask(source); + } + + public static IUniTaskAsyncEnumerable ToUniTaskAsyncEnumerable(this IObservable source) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return new ToUniTaskAsyncEnumerableObservable(source); + } + } + + internal class ToUniTaskAsyncEnumerable : IUniTaskAsyncEnumerable + { + readonly IEnumerable source; + + public ToUniTaskAsyncEnumerable(IEnumerable source) + { + this.source = source; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _ToUniTaskAsyncEnumerable(source, cancellationToken); + } + + class _ToUniTaskAsyncEnumerable : IUniTaskAsyncEnumerator + { + readonly IEnumerable source; + CancellationToken cancellationToken; + + IEnumerator enumerator; + + public _ToUniTaskAsyncEnumerable(IEnumerable source, CancellationToken cancellationToken) + { + this.source = source; + this.cancellationToken = cancellationToken; + } + + public T Current => enumerator.Current; + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + if (enumerator == null) + { + enumerator = source.GetEnumerator(); + } + + if (enumerator.MoveNext()) + { + return CompletedTasks.True; + } + + return CompletedTasks.False; + } + + public UniTask DisposeAsync() + { + enumerator.Dispose(); + return default; + } + } + } + + internal class ToUniTaskAsyncEnumerableTask : IUniTaskAsyncEnumerable + { + readonly Task source; + + public ToUniTaskAsyncEnumerableTask(Task source) + { + this.source = source; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _ToUniTaskAsyncEnumerableTask(source, cancellationToken); + } + + class _ToUniTaskAsyncEnumerableTask : IUniTaskAsyncEnumerator + { + readonly Task source; + CancellationToken cancellationToken; + + T current; + bool called; + + public _ToUniTaskAsyncEnumerableTask(Task source, CancellationToken cancellationToken) + { + this.source = source; + this.cancellationToken = cancellationToken; + + this.called = false; + } + + public T Current => current; + + public async UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + if (called) + { + return false; + } + called = true; + + current = await source; + return true; + } + + public UniTask DisposeAsync() + { + return default; + } + } + } + + internal class ToUniTaskAsyncEnumerableUniTask : IUniTaskAsyncEnumerable + { + readonly UniTask source; + + public ToUniTaskAsyncEnumerableUniTask(UniTask source) + { + this.source = source; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _ToUniTaskAsyncEnumerableUniTask(source, cancellationToken); + } + + class _ToUniTaskAsyncEnumerableUniTask : IUniTaskAsyncEnumerator + { + readonly UniTask source; + CancellationToken cancellationToken; + + T current; + bool called; + + public _ToUniTaskAsyncEnumerableUniTask(UniTask source, CancellationToken cancellationToken) + { + this.source = source; + this.cancellationToken = cancellationToken; + + this.called = false; + } + + public T Current => current; + + public async UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + + if (called) + { + return false; + } + called = true; + + current = await source; + return true; + } + + public UniTask DisposeAsync() + { + return default; + } + } + } + + internal class ToUniTaskAsyncEnumerableObservable : IUniTaskAsyncEnumerable + { + readonly IObservable source; + + public ToUniTaskAsyncEnumerableObservable(IObservable source) + { + this.source = source; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _ToUniTaskAsyncEnumerableObservable(source, cancellationToken); + } + + class _ToUniTaskAsyncEnumerableObservable : MoveNextSource, IUniTaskAsyncEnumerator, IObserver + { + static readonly Action OnCanceledDelegate = OnCanceled; + + readonly IObservable source; + CancellationToken cancellationToken; + + + bool useCachedCurrent; + T current; + bool subscribeCompleted; + readonly Queue queuedResult; + Exception error; + IDisposable subscription; + CancellationTokenRegistration cancellationTokenRegistration; + + public _ToUniTaskAsyncEnumerableObservable(IObservable source, CancellationToken cancellationToken) + { + this.source = source; + this.cancellationToken = cancellationToken; + this.queuedResult = new Queue(); + + if (cancellationToken.CanBeCanceled) + { + cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(OnCanceledDelegate, this); + } + } + + public T Current + { + get + { + if (useCachedCurrent) + { + return current; + } + + lock (queuedResult) + { + if (queuedResult.Count != 0) + { + current = queuedResult.Dequeue(); + useCachedCurrent = true; + return current; + } + else + { + return default; // undefined. + } + } + } + } + + public UniTask MoveNextAsync() + { + lock (queuedResult) + { + useCachedCurrent = false; + + if (cancellationToken.IsCancellationRequested) + { + return UniTask.FromCanceled(cancellationToken); + } + + if (subscription == null) + { + subscription = source.Subscribe(this); + } + + if (error != null) + { + return UniTask.FromException(error); + } + + if (queuedResult.Count != 0) + { + return CompletedTasks.True; + } + + if (subscribeCompleted) + { + return CompletedTasks.False; + } + + completionSource.Reset(); + return new UniTask(this, completionSource.Version); + } + } + + public UniTask DisposeAsync() + { + subscription.Dispose(); + cancellationTokenRegistration.Dispose(); + completionSource.Reset(); + return default; + } + + public void OnCompleted() + { + lock (queuedResult) + { + subscribeCompleted = true; + completionSource.TrySetResult(false); + } + } + + public void OnError(Exception error) + { + lock (queuedResult) + { + this.error = error; + completionSource.TrySetException(error); + } + } + + public void OnNext(T value) + { + lock (queuedResult) + { + queuedResult.Enqueue(value); + completionSource.TrySetResult(true); // include callback execution, too long lock? + } + } + + static void OnCanceled(object state) + { + var self = (_ToUniTaskAsyncEnumerableObservable)state; + lock (self.queuedResult) + { + self.completionSource.TrySetCanceled(self.cancellationToken); + } + } + } + } +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToUniTaskAsyncEnumerable.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToUniTaskAsyncEnumerable.cs.meta new file mode 100644 index 0000000..45fd3b0 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/ToUniTaskAsyncEnumerable.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d7192de2a0581ec4db62962cc1404af5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UniTask.Linq.asmdef b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UniTask.Linq.asmdef new file mode 100644 index 0000000..db84553 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UniTask.Linq.asmdef @@ -0,0 +1,15 @@ +{ + "name": "UniTask.Linq", + "references": [ + "UniTask" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UniTask.Linq.asmdef.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UniTask.Linq.asmdef.meta new file mode 100644 index 0000000..1c85d19 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UniTask.Linq.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 5c01796d064528144a599661eaab93a6 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Union.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Union.cs new file mode 100644 index 0000000..2ceefab --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Union.cs @@ -0,0 +1,26 @@ +using Cysharp.Threading.Tasks.Internal; +using System.Collections.Generic; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Union(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second) + { + Error.ThrowArgumentNullException(first, nameof(first)); + Error.ThrowArgumentNullException(second, nameof(second)); + + return Union(first, second, EqualityComparer.Default); + } + + public static IUniTaskAsyncEnumerable Union(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, IEqualityComparer comparer) + { + Error.ThrowArgumentNullException(first, nameof(first)); + Error.ThrowArgumentNullException(second, nameof(second)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + // improv without combinate? + return first.Concat(second).Distinct(comparer); + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Union.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Union.cs.meta new file mode 100644 index 0000000..1d9c7ad --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Union.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ae57a55bdeba98b4f8ff234d98d7dd76 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions.meta new file mode 100644 index 0000000..1097c37 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 669f5459819f7284ca1b35f4d55fe226 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/EveryUpdate.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/EveryUpdate.cs new file mode 100644 index 0000000..585fff9 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/EveryUpdate.cs @@ -0,0 +1,77 @@ +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable EveryUpdate(PlayerLoopTiming updateTiming = PlayerLoopTiming.Update) + { + return new EveryUpdate(updateTiming); + } + } + + internal class EveryUpdate : IUniTaskAsyncEnumerable + { + readonly PlayerLoopTiming updateTiming; + + public EveryUpdate(PlayerLoopTiming updateTiming) + { + this.updateTiming = updateTiming; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _EveryUpdate(updateTiming, cancellationToken); + } + + class _EveryUpdate : MoveNextSource, IUniTaskAsyncEnumerator, IPlayerLoopItem + { + readonly PlayerLoopTiming updateTiming; + CancellationToken cancellationToken; + + bool disposed; + + public _EveryUpdate(PlayerLoopTiming updateTiming, CancellationToken cancellationToken) + { + this.updateTiming = updateTiming; + this.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(this, 2); + PlayerLoopHelper.AddAction(updateTiming, this); + } + + public AsyncUnit Current => default; + + public UniTask MoveNextAsync() + { + // return false instead of throw + if (disposed || cancellationToken.IsCancellationRequested) return CompletedTasks.False; + + completionSource.Reset(); + return new UniTask(this, completionSource.Version); + } + + public UniTask DisposeAsync() + { + if (!disposed) + { + disposed = true; + TaskTracker.RemoveTracking(this); + } + return default; + } + + public bool MoveNext() + { + if (disposed || cancellationToken.IsCancellationRequested) + { + completionSource.TrySetResult(false); + return false; + } + + completionSource.TrySetResult(true); + return true; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/EveryUpdate.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/EveryUpdate.cs.meta new file mode 100644 index 0000000..6336e0e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/EveryUpdate.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 00520eb52e49b5b4e8d9870d6ff1aced +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/EveryValueChanged.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/EveryValueChanged.cs new file mode 100644 index 0000000..f678e7a --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/EveryValueChanged.cs @@ -0,0 +1,240 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable EveryValueChanged(TTarget target, Func propertySelector, PlayerLoopTiming monitorTiming = PlayerLoopTiming.Update, IEqualityComparer equalityComparer = null) + where TTarget : class + { + var unityObject = target as UnityEngine.Object; + var isUnityObject = target is UnityEngine.Object; // don't use (unityObject == null) + + if (isUnityObject) + { + return new EveryValueChangedUnityObject(target, propertySelector, equalityComparer ?? UnityEqualityComparer.GetDefault(), monitorTiming); + } + else + { + return new EveryValueChangedStandardObject(target, propertySelector, equalityComparer ?? UnityEqualityComparer.GetDefault(), monitorTiming); + } + } + } + + internal sealed class EveryValueChangedUnityObject : IUniTaskAsyncEnumerable + { + readonly TTarget target; + readonly Func propertySelector; + readonly IEqualityComparer equalityComparer; + readonly PlayerLoopTiming monitorTiming; + + public EveryValueChangedUnityObject(TTarget target, Func propertySelector, IEqualityComparer equalityComparer, PlayerLoopTiming monitorTiming) + { + this.target = target; + this.propertySelector = propertySelector; + this.equalityComparer = equalityComparer; + this.monitorTiming = monitorTiming; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _EveryValueChanged(target, propertySelector, equalityComparer, monitorTiming, cancellationToken); + } + + sealed class _EveryValueChanged : MoveNextSource, IUniTaskAsyncEnumerator, IPlayerLoopItem + { + readonly TTarget target; + readonly UnityEngine.Object targetAsUnityObject; + readonly IEqualityComparer equalityComparer; + readonly Func propertySelector; + CancellationToken cancellationToken; + + bool first; + TProperty currentValue; + bool disposed; + + public _EveryValueChanged(TTarget target, Func propertySelector, IEqualityComparer equalityComparer, PlayerLoopTiming monitorTiming, CancellationToken cancellationToken) + { + this.target = target; + this.targetAsUnityObject = target as UnityEngine.Object; + this.propertySelector = propertySelector; + this.equalityComparer = equalityComparer; + this.cancellationToken = cancellationToken; + this.first = true; + TaskTracker.TrackActiveTask(this, 2); + PlayerLoopHelper.AddAction(monitorTiming, this); + } + + public TProperty Current => currentValue; + + public UniTask MoveNextAsync() + { + // return false instead of throw + if (disposed || cancellationToken.IsCancellationRequested) return CompletedTasks.False; + + if (first) + { + first = false; + if (targetAsUnityObject == null) + { + return CompletedTasks.False; + } + this.currentValue = propertySelector(target); + return CompletedTasks.True; + } + + completionSource.Reset(); + return new UniTask(this, completionSource.Version); + } + + public UniTask DisposeAsync() + { + if (!disposed) + { + disposed = true; + TaskTracker.RemoveTracking(this); + } + return default; + } + + public bool MoveNext() + { + if (disposed || cancellationToken.IsCancellationRequested || targetAsUnityObject == null) // destroyed = cancel. + { + completionSource.TrySetResult(false); + DisposeAsync().Forget(); + return false; + } + + TProperty nextValue = default(TProperty); + try + { + nextValue = propertySelector(target); + if (equalityComparer.Equals(currentValue, nextValue)) + { + return true; + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + DisposeAsync().Forget(); + return false; + } + + currentValue = nextValue; + completionSource.TrySetResult(true); + return true; + } + } + } + + internal sealed class EveryValueChangedStandardObject : IUniTaskAsyncEnumerable + where TTarget : class + { + readonly WeakReference target; + readonly Func propertySelector; + readonly IEqualityComparer equalityComparer; + readonly PlayerLoopTiming monitorTiming; + + public EveryValueChangedStandardObject(TTarget target, Func propertySelector, IEqualityComparer equalityComparer, PlayerLoopTiming monitorTiming) + { + this.target = new WeakReference(target, false); + this.propertySelector = propertySelector; + this.equalityComparer = equalityComparer; + this.monitorTiming = monitorTiming; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _EveryValueChanged(target, propertySelector, equalityComparer, monitorTiming, cancellationToken); + } + + sealed class _EveryValueChanged : MoveNextSource, IUniTaskAsyncEnumerator, IPlayerLoopItem + { + readonly WeakReference target; + readonly IEqualityComparer equalityComparer; + readonly Func propertySelector; + CancellationToken cancellationToken; + + bool first; + TProperty currentValue; + bool disposed; + + public _EveryValueChanged(WeakReference target, Func propertySelector, IEqualityComparer equalityComparer, PlayerLoopTiming monitorTiming, CancellationToken cancellationToken) + { + this.target = target; + this.propertySelector = propertySelector; + this.equalityComparer = equalityComparer; + this.cancellationToken = cancellationToken; + this.first = true; + TaskTracker.TrackActiveTask(this, 2); + PlayerLoopHelper.AddAction(monitorTiming, this); + } + + public TProperty Current => currentValue; + + public UniTask MoveNextAsync() + { + if (disposed || cancellationToken.IsCancellationRequested) return CompletedTasks.False; + + if (first) + { + first = false; + if (!target.TryGetTarget(out var t)) + { + return CompletedTasks.False; + } + this.currentValue = propertySelector(t); + return CompletedTasks.True; + } + + completionSource.Reset(); + return new UniTask(this, completionSource.Version); + } + + public UniTask DisposeAsync() + { + if (!disposed) + { + disposed = true; + TaskTracker.RemoveTracking(this); + } + return default; + } + + public bool MoveNext() + { + if (disposed || cancellationToken.IsCancellationRequested || !target.TryGetTarget(out var t)) + { + completionSource.TrySetResult(false); + DisposeAsync().Forget(); + return false; + } + + TProperty nextValue = default(TProperty); + try + { + nextValue = propertySelector(t); + if (equalityComparer.Equals(currentValue, nextValue)) + { + return true; + } + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + DisposeAsync().Forget(); + return false; + } + + currentValue = nextValue; + completionSource.TrySetResult(true); + return true; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/EveryValueChanged.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/EveryValueChanged.cs.meta new file mode 100644 index 0000000..9d2be70 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/EveryValueChanged.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1ec39f1c41c305344854782c935ad354 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/Timer.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/Timer.cs new file mode 100644 index 0000000..53ecfcd --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/Timer.cs @@ -0,0 +1,312 @@ +using System; +using System.Threading; +using UnityEngine; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Timer(TimeSpan dueTime, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update, bool ignoreTimeScale = false) + { + return new Timer(dueTime, null, updateTiming, ignoreTimeScale); + } + + public static IUniTaskAsyncEnumerable Timer(TimeSpan dueTime, TimeSpan period, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update, bool ignoreTimeScale = false) + { + return new Timer(dueTime, period, updateTiming, ignoreTimeScale); + } + + public static IUniTaskAsyncEnumerable Interval(TimeSpan period, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update, bool ignoreTimeScale = false) + { + return new Timer(period, period, updateTiming, ignoreTimeScale); + } + + public static IUniTaskAsyncEnumerable TimerFrame(int dueTimeFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update) + { + if (dueTimeFrameCount < 0) + { + throw new ArgumentOutOfRangeException("Delay does not allow minus delayFrameCount. dueTimeFrameCount:" + dueTimeFrameCount); + } + + return new TimerFrame(dueTimeFrameCount, null, updateTiming); + } + + public static IUniTaskAsyncEnumerable TimerFrame(int dueTimeFrameCount, int periodFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update) + { + if (dueTimeFrameCount < 0) + { + throw new ArgumentOutOfRangeException("Delay does not allow minus delayFrameCount. dueTimeFrameCount:" + dueTimeFrameCount); + } + if (periodFrameCount < 0) + { + throw new ArgumentOutOfRangeException("Delay does not allow minus periodFrameCount. periodFrameCount:" + dueTimeFrameCount); + } + + return new TimerFrame(dueTimeFrameCount, periodFrameCount, updateTiming); + } + + public static IUniTaskAsyncEnumerable IntervalFrame(int intervalFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update) + { + if (intervalFrameCount < 0) + { + throw new ArgumentOutOfRangeException("Delay does not allow minus intervalFrameCount. intervalFrameCount:" + intervalFrameCount); + } + return new TimerFrame(intervalFrameCount, intervalFrameCount, updateTiming); + } + } + + internal class Timer : IUniTaskAsyncEnumerable + { + readonly PlayerLoopTiming updateTiming; + readonly TimeSpan dueTime; + readonly TimeSpan? period; + readonly bool ignoreTimeScale; + + public Timer(TimeSpan dueTime, TimeSpan? period, PlayerLoopTiming updateTiming, bool ignoreTimeScale) + { + this.updateTiming = updateTiming; + this.dueTime = dueTime; + this.period = period; + this.ignoreTimeScale = ignoreTimeScale; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Timer(dueTime, period, updateTiming, ignoreTimeScale, cancellationToken); + } + + class _Timer : MoveNextSource, IUniTaskAsyncEnumerator, IPlayerLoopItem + { + readonly float dueTime; + readonly float? period; + readonly PlayerLoopTiming updateTiming; + readonly bool ignoreTimeScale; + CancellationToken cancellationToken; + + int initialFrame; + float elapsed; + bool dueTimePhase; + bool completed; + bool disposed; + + public _Timer(TimeSpan dueTime, TimeSpan? period, PlayerLoopTiming updateTiming, bool ignoreTimeScale, CancellationToken cancellationToken) + { + this.dueTime = (float)dueTime.TotalSeconds; + this.period = (period == null) ? null : (float?)period.Value.TotalSeconds; + + if (this.dueTime <= 0) this.dueTime = 0; + if (this.period != null) + { + if (this.period <= 0) this.period = 1; + } + + this.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1; + this.dueTimePhase = true; + this.updateTiming = updateTiming; + this.ignoreTimeScale = ignoreTimeScale; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 2); + PlayerLoopHelper.AddAction(updateTiming, this); + } + + public AsyncUnit Current => default; + + public UniTask MoveNextAsync() + { + // return false instead of throw + if (disposed || cancellationToken.IsCancellationRequested || completed) return CompletedTasks.False; + + // reset value here. + this.elapsed = 0; + + completionSource.Reset(); + return new UniTask(this, completionSource.Version); + } + + public UniTask DisposeAsync() + { + if (!disposed) + { + disposed = true; + TaskTracker.RemoveTracking(this); + } + return default; + } + + public bool MoveNext() + { + if (disposed || cancellationToken.IsCancellationRequested) + { + completionSource.TrySetResult(false); + return false; + } + + if (dueTimePhase) + { + if (elapsed == 0) + { + // skip in initial frame. + if (initialFrame == Time.frameCount) + { + return true; + } + } + + elapsed += (ignoreTimeScale) ? UnityEngine.Time.unscaledDeltaTime : UnityEngine.Time.deltaTime; + + if (elapsed >= dueTime) + { + dueTimePhase = false; + completionSource.TrySetResult(true); + } + } + else + { + if (period == null) + { + completed = true; + completionSource.TrySetResult(false); + return false; + } + + elapsed += (ignoreTimeScale) ? UnityEngine.Time.unscaledDeltaTime : UnityEngine.Time.deltaTime; + + if (elapsed >= period) + { + completionSource.TrySetResult(true); + } + } + + return true; + } + } + } + + internal class TimerFrame : IUniTaskAsyncEnumerable + { + readonly PlayerLoopTiming updateTiming; + readonly int dueTimeFrameCount; + readonly int? periodFrameCount; + + public TimerFrame(int dueTimeFrameCount, int? periodFrameCount, PlayerLoopTiming updateTiming) + { + this.updateTiming = updateTiming; + this.dueTimeFrameCount = dueTimeFrameCount; + this.periodFrameCount = periodFrameCount; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _TimerFrame(dueTimeFrameCount, periodFrameCount, updateTiming, cancellationToken); + } + + class _TimerFrame : MoveNextSource, IUniTaskAsyncEnumerator, IPlayerLoopItem + { + readonly int dueTimeFrameCount; + readonly int? periodFrameCount; + CancellationToken cancellationToken; + + int initialFrame; + int currentFrame; + bool dueTimePhase; + bool completed; + bool disposed; + + public _TimerFrame(int dueTimeFrameCount, int? periodFrameCount, PlayerLoopTiming updateTiming, CancellationToken cancellationToken) + { + if (dueTimeFrameCount <= 0) dueTimeFrameCount = 0; + if (periodFrameCount != null) + { + if (periodFrameCount <= 0) periodFrameCount = 1; + } + + this.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1; + this.dueTimePhase = true; + this.dueTimeFrameCount = dueTimeFrameCount; + this.periodFrameCount = periodFrameCount; + this.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(this, 2); + PlayerLoopHelper.AddAction(updateTiming, this); + } + + public AsyncUnit Current => default; + + public UniTask MoveNextAsync() + { + // return false instead of throw + if (disposed || cancellationToken.IsCancellationRequested || completed) return CompletedTasks.False; + + + // reset value here. + this.currentFrame = 0; + + completionSource.Reset(); + return new UniTask(this, completionSource.Version); + } + + public UniTask DisposeAsync() + { + if (!disposed) + { + disposed = true; + TaskTracker.RemoveTracking(this); + } + return default; + } + + public bool MoveNext() + { + if (disposed || cancellationToken.IsCancellationRequested) + { + completionSource.TrySetResult(false); + return false; + } + + if (dueTimePhase) + { + if (currentFrame == 0) + { + if (dueTimeFrameCount == 0) + { + dueTimePhase = false; + completionSource.TrySetResult(true); + return true; + } + + // skip in initial frame. + if (initialFrame == Time.frameCount) + { + return true; + } + } + + if (++currentFrame >= dueTimeFrameCount) + { + dueTimePhase = false; + completionSource.TrySetResult(true); + } + else + { + } + } + else + { + if (periodFrameCount == null) + { + completed = true; + completionSource.TrySetResult(false); + return false; + } + + if (++currentFrame >= periodFrameCount) + { + completionSource.TrySetResult(true); + } + } + + return true; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/Timer.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/Timer.cs.meta new file mode 100644 index 0000000..aa790c5 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/UnityExtensions/Timer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 382caacde439855418709c641e4d7b04 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Where.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Where.cs new file mode 100644 index 0000000..1b5ac47 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Where.cs @@ -0,0 +1,818 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Where(this IUniTaskAsyncEnumerable source, Func predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new Where(source, predicate); + } + + public static IUniTaskAsyncEnumerable Where(this IUniTaskAsyncEnumerable source, Func predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new WhereInt(source, predicate); + } + + public static IUniTaskAsyncEnumerable WhereAwait(this IUniTaskAsyncEnumerable source, Func> predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new WhereAwait(source, predicate); + } + + public static IUniTaskAsyncEnumerable WhereAwait(this IUniTaskAsyncEnumerable source, Func> predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new WhereIntAwait(source, predicate); + } + + public static IUniTaskAsyncEnumerable WhereAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new WhereAwaitWithCancellation(source, predicate); + } + + public static IUniTaskAsyncEnumerable WhereAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> predicate) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return new WhereIntAwaitWithCancellation(source, predicate); + } + } + + internal sealed class Where : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func predicate; + + public Where(IUniTaskAsyncEnumerable source, Func predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Where(source, predicate, cancellationToken); + } + + sealed class _Where : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly Func predicate; + readonly CancellationToken cancellationToken; + + int state = -1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + Action moveNextAction; + + public _Where(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) + { + this.source = source; + this.predicate = predicate; + this.cancellationToken = cancellationToken; + this.moveNextAction = MoveNext; + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + REPEAT: + try + { + switch (state) + { + case -1: // init + enumerator = source.GetAsyncEnumerator(cancellationToken); + goto case 0; + case 0: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case 1; + } + else + { + state = 1; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case 1: + if (awaiter.GetResult()) + { + Current = enumerator.Current; + if (predicate(Current)) + { + goto CONTINUE; + } + else + { + state = 0; + goto REPEAT; + } + } + else + { + goto DONE; + } + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + + CONTINUE: + state = 0; + completionSource.TrySetResult(true); + return; + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + return enumerator.DisposeAsync(); + } + } + } + + internal sealed class WhereInt : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func predicate; + + public WhereInt(IUniTaskAsyncEnumerable source, Func predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Where(source, predicate, cancellationToken); + } + + sealed class _Where : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly Func predicate; + readonly CancellationToken cancellationToken; + + int state = -1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + Action moveNextAction; + int index; + + public _Where(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) + { + this.source = source; + this.predicate = predicate; + this.cancellationToken = cancellationToken; + this.moveNextAction = MoveNext; + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + REPEAT: + try + { + switch (state) + { + case -1: // init + enumerator = source.GetAsyncEnumerator(cancellationToken); + goto case 0; + case 0: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case 1; + } + else + { + state = 1; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case 1: + if (awaiter.GetResult()) + { + Current = enumerator.Current; + if (predicate(Current, checked(index++))) + { + goto CONTINUE; + } + else + { + state = 0; + goto REPEAT; + } + } + else + { + goto DONE; + } + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + + CONTINUE: + state = 0; + completionSource.TrySetResult(true); + return; + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + return enumerator.DisposeAsync(); + } + } + } + + internal sealed class WhereAwait : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> predicate; + + public WhereAwait(IUniTaskAsyncEnumerable source, Func> predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _WhereAwait(source, predicate, cancellationToken); + } + + sealed class _WhereAwait : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> predicate; + readonly CancellationToken cancellationToken; + + int state = -1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + UniTask.Awaiter awaiter2; + Action moveNextAction; + + public _WhereAwait(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + { + this.source = source; + this.predicate = predicate; + this.cancellationToken = cancellationToken; + this.moveNextAction = MoveNext; + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + REPEAT: + try + { + switch (state) + { + case -1: // init + enumerator = source.GetAsyncEnumerator(cancellationToken); + goto case 0; + case 0: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case 1; + } + else + { + state = 1; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case 1: + if (awaiter.GetResult()) + { + Current = enumerator.Current; + + awaiter2 = predicate(Current).GetAwaiter(); + if (awaiter2.IsCompleted) + { + goto case 2; + } + else + { + state = 2; + awaiter2.UnsafeOnCompleted(moveNextAction); + return; + } + } + else + { + goto DONE; + } + case 2: + if (awaiter2.GetResult()) + { + goto CONTINUE; + } + else + { + state = 0; + goto REPEAT; + } + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + + CONTINUE: + state = 0; + completionSource.TrySetResult(true); + return; + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + return enumerator.DisposeAsync(); + } + } + } + + internal sealed class WhereIntAwait : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> predicate; + + public WhereIntAwait(IUniTaskAsyncEnumerable source, Func> predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _WhereAwait(source, predicate, cancellationToken); + } + + sealed class _WhereAwait : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> predicate; + readonly CancellationToken cancellationToken; + + int state = -1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + UniTask.Awaiter awaiter2; + Action moveNextAction; + int index; + + public _WhereAwait(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + { + this.source = source; + this.predicate = predicate; + this.cancellationToken = cancellationToken; + this.moveNextAction = MoveNext; + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + REPEAT: + try + { + switch (state) + { + case -1: // init + enumerator = source.GetAsyncEnumerator(cancellationToken); + goto case 0; + case 0: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case 1; + } + else + { + state = 1; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case 1: + if (awaiter.GetResult()) + { + Current = enumerator.Current; + + awaiter2 = predicate(Current, checked(index++)).GetAwaiter(); + if (awaiter2.IsCompleted) + { + goto case 2; + } + else + { + state = 2; + awaiter2.UnsafeOnCompleted(moveNextAction); + return; + } + } + else + { + goto DONE; + } + case 2: + if (awaiter2.GetResult()) + { + goto CONTINUE; + } + else + { + state = 0; + goto REPEAT; + } + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + + CONTINUE: + state = 0; + completionSource.TrySetResult(true); + return; + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + return enumerator.DisposeAsync(); + } + } + } + + internal sealed class WhereAwaitWithCancellation : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> predicate; + + public WhereAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _WhereAwaitWithCancellation(source, predicate, cancellationToken); + } + + sealed class _WhereAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> predicate; + readonly CancellationToken cancellationToken; + + int state = -1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + UniTask.Awaiter awaiter2; + Action moveNextAction; + + public _WhereAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + { + this.source = source; + this.predicate = predicate; + this.cancellationToken = cancellationToken; + this.moveNextAction = MoveNext; + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + REPEAT: + try + { + switch (state) + { + case -1: // init + enumerator = source.GetAsyncEnumerator(cancellationToken); + goto case 0; + case 0: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case 1; + } + else + { + state = 1; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case 1: + if (awaiter.GetResult()) + { + Current = enumerator.Current; + + awaiter2 = predicate(Current, cancellationToken).GetAwaiter(); + if (awaiter2.IsCompleted) + { + goto case 2; + } + else + { + state = 2; + awaiter2.UnsafeOnCompleted(moveNextAction); + return; + } + } + else + { + goto DONE; + } + case 2: + if (awaiter2.GetResult()) + { + goto CONTINUE; + } + else + { + state = 0; + goto REPEAT; + } + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + + CONTINUE: + state = 0; + completionSource.TrySetResult(true); + return; + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + return enumerator.DisposeAsync(); + } + } + } + + internal sealed class WhereIntAwaitWithCancellation : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> predicate; + + public WhereIntAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> predicate) + { + this.source = source; + this.predicate = predicate; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _WhereAwaitWithCancellation(source, predicate, cancellationToken); + } + + sealed class _WhereAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator + { + readonly IUniTaskAsyncEnumerable source; + readonly Func> predicate; + readonly CancellationToken cancellationToken; + + int state = -1; + IUniTaskAsyncEnumerator enumerator; + UniTask.Awaiter awaiter; + UniTask.Awaiter awaiter2; + Action moveNextAction; + int index; + + public _WhereAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + { + this.source = source; + this.predicate = predicate; + this.cancellationToken = cancellationToken; + this.moveNextAction = MoveNext; + TaskTracker.TrackActiveTask(this, 3); + } + + public TSource Current { get; private set; } + + public UniTask MoveNextAsync() + { + if (state == -2) return default; + + completionSource.Reset(); + MoveNext(); + return new UniTask(this, completionSource.Version); + } + + void MoveNext() + { + REPEAT: + try + { + switch (state) + { + case -1: // init + enumerator = source.GetAsyncEnumerator(cancellationToken); + goto case 0; + case 0: + awaiter = enumerator.MoveNextAsync().GetAwaiter(); + if (awaiter.IsCompleted) + { + goto case 1; + } + else + { + state = 1; + awaiter.UnsafeOnCompleted(moveNextAction); + return; + } + case 1: + if (awaiter.GetResult()) + { + Current = enumerator.Current; + + awaiter2 = predicate(Current, checked(index++), cancellationToken).GetAwaiter(); + if (awaiter2.IsCompleted) + { + goto case 2; + } + else + { + state = 2; + awaiter2.UnsafeOnCompleted(moveNextAction); + return; + } + } + else + { + goto DONE; + } + case 2: + if (awaiter2.GetResult()) + { + goto CONTINUE; + } + else + { + state = 0; + goto REPEAT; + } + default: + goto DONE; + } + } + catch (Exception ex) + { + state = -2; + completionSource.TrySetException(ex); + return; + } + + DONE: + state = -2; + completionSource.TrySetResult(false); + return; + + CONTINUE: + state = 0; + completionSource.TrySetResult(true); + return; + } + + public UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + return enumerator.DisposeAsync(); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Where.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Where.cs.meta new file mode 100644 index 0000000..7e50337 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Where.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d882a3238d9535e4e8ce1ad3291eb7fb +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Zip.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Zip.cs new file mode 100644 index 0000000..af6d5f1 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Zip.cs @@ -0,0 +1,541 @@ +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + + public static IUniTaskAsyncEnumerable<(TFirst First, TSecond Second)> Zip(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second) + { + Error.ThrowArgumentNullException(first, nameof(first)); + Error.ThrowArgumentNullException(second, nameof(second)); + + return Zip(first, second, (x, y) => (x, y)); + } + + public static IUniTaskAsyncEnumerable Zip(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, Func resultSelector) + { + Error.ThrowArgumentNullException(first, nameof(first)); + Error.ThrowArgumentNullException(second, nameof(second)); + Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector)); + + return new Zip(first, second, resultSelector); + } + + public static IUniTaskAsyncEnumerable ZipAwait(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, Func> selector) + { + Error.ThrowArgumentNullException(first, nameof(first)); + Error.ThrowArgumentNullException(second, nameof(second)); + Error.ThrowArgumentNullException(selector, nameof(selector)); + + return new ZipAwait(first, second, selector); + } + + public static IUniTaskAsyncEnumerable ZipAwaitWithCancellation(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, Func> selector) + { + Error.ThrowArgumentNullException(first, nameof(first)); + Error.ThrowArgumentNullException(second, nameof(second)); + Error.ThrowArgumentNullException(selector, nameof(selector)); + + return new ZipAwaitWithCancellation(first, second, selector); + } + } + + internal sealed class Zip : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable first; + readonly IUniTaskAsyncEnumerable second; + readonly Func resultSelector; + + public Zip(IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, Func resultSelector) + { + this.first = first; + this.second = second; + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _Zip(first, second, resultSelector, cancellationToken); + } + + sealed class _Zip : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action firstMoveNextCoreDelegate = FirstMoveNextCore; + static readonly Action secondMoveNextCoreDelegate = SecondMoveNextCore; + + readonly IUniTaskAsyncEnumerable first; + readonly IUniTaskAsyncEnumerable second; + readonly Func resultSelector; + + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator firstEnumerator; + IUniTaskAsyncEnumerator secondEnumerator; + + UniTask.Awaiter firstAwaiter; + UniTask.Awaiter secondAwaiter; + + public _Zip(IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, Func resultSelector, CancellationToken cancellationToken) + { + this.first = first; + this.second = second; + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + completionSource.Reset(); + + if (firstEnumerator == null) + { + firstEnumerator = first.GetAsyncEnumerator(cancellationToken); + secondEnumerator = second.GetAsyncEnumerator(cancellationToken); + } + + firstAwaiter = firstEnumerator.MoveNextAsync().GetAwaiter(); + + if (firstAwaiter.IsCompleted) + { + FirstMoveNextCore(this); + } + else + { + firstAwaiter.SourceOnCompleted(firstMoveNextCoreDelegate, this); + } + + return new UniTask(this, completionSource.Version); + } + + static void FirstMoveNextCore(object state) + { + var self = (_Zip)state; + + if (self.TryGetResult(self.firstAwaiter, out var result)) + { + if (result) + { + try + { + self.secondAwaiter = self.secondEnumerator.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + return; + } + + if (self.secondAwaiter.IsCompleted) + { + SecondMoveNextCore(self); + } + else + { + self.secondAwaiter.SourceOnCompleted(secondMoveNextCoreDelegate, self); + } + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + static void SecondMoveNextCore(object state) + { + var self = (_Zip)state; + + if (self.TryGetResult(self.secondAwaiter, out var result)) + { + if (result) + { + try + { + self.Current = self.resultSelector(self.firstEnumerator.Current, self.secondEnumerator.Current); + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + } + + if (self.cancellationToken.IsCancellationRequested) + { + self.completionSource.TrySetCanceled(self.cancellationToken); + } + else + { + self.completionSource.TrySetResult(true); + } + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (firstEnumerator != null) + { + await firstEnumerator.DisposeAsync(); + } + if (secondEnumerator != null) + { + await secondEnumerator.DisposeAsync(); + } + } + } + } + + internal sealed class ZipAwait : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable first; + readonly IUniTaskAsyncEnumerable second; + readonly Func> resultSelector; + + public ZipAwait(IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, Func> resultSelector) + { + this.first = first; + this.second = second; + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _ZipAwait(first, second, resultSelector, cancellationToken); + } + + sealed class _ZipAwait : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action firstMoveNextCoreDelegate = FirstMoveNextCore; + static readonly Action secondMoveNextCoreDelegate = SecondMoveNextCore; + static readonly Action resultAwaitCoreDelegate = ResultAwaitCore; + + readonly IUniTaskAsyncEnumerable first; + readonly IUniTaskAsyncEnumerable second; + readonly Func> resultSelector; + + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator firstEnumerator; + IUniTaskAsyncEnumerator secondEnumerator; + + UniTask.Awaiter firstAwaiter; + UniTask.Awaiter secondAwaiter; + UniTask.Awaiter resultAwaiter; + + public _ZipAwait(IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, Func> resultSelector, CancellationToken cancellationToken) + { + this.first = first; + this.second = second; + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + completionSource.Reset(); + + if (firstEnumerator == null) + { + firstEnumerator = first.GetAsyncEnumerator(cancellationToken); + secondEnumerator = second.GetAsyncEnumerator(cancellationToken); + } + + firstAwaiter = firstEnumerator.MoveNextAsync().GetAwaiter(); + + if (firstAwaiter.IsCompleted) + { + FirstMoveNextCore(this); + } + else + { + firstAwaiter.SourceOnCompleted(firstMoveNextCoreDelegate, this); + } + + return new UniTask(this, completionSource.Version); + } + + static void FirstMoveNextCore(object state) + { + var self = (_ZipAwait)state; + + if (self.TryGetResult(self.firstAwaiter, out var result)) + { + if (result) + { + try + { + self.secondAwaiter = self.secondEnumerator.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + return; + } + + if (self.secondAwaiter.IsCompleted) + { + SecondMoveNextCore(self); + } + else + { + self.secondAwaiter.SourceOnCompleted(secondMoveNextCoreDelegate, self); + } + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + static void SecondMoveNextCore(object state) + { + var self = (_ZipAwait)state; + + if (self.TryGetResult(self.secondAwaiter, out var result)) + { + if (result) + { + try + { + self.resultAwaiter = self.resultSelector(self.firstEnumerator.Current, self.secondEnumerator.Current).GetAwaiter(); + if (self.resultAwaiter.IsCompleted) + { + ResultAwaitCore(self); + } + else + { + self.resultAwaiter.SourceOnCompleted(resultAwaitCoreDelegate, self); + } + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + } + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + static void ResultAwaitCore(object state) + { + var self = (_ZipAwait)state; + + if (self.TryGetResult(self.resultAwaiter, out var result)) + { + self.Current = result; + + if (self.cancellationToken.IsCancellationRequested) + { + self.completionSource.TrySetCanceled(self.cancellationToken); + } + else + { + self.completionSource.TrySetResult(true); + } + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (firstEnumerator != null) + { + await firstEnumerator.DisposeAsync(); + } + if (secondEnumerator != null) + { + await secondEnumerator.DisposeAsync(); + } + } + } + } + + internal sealed class ZipAwaitWithCancellation : IUniTaskAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable first; + readonly IUniTaskAsyncEnumerable second; + readonly Func> resultSelector; + + public ZipAwaitWithCancellation(IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, Func> resultSelector) + { + this.first = first; + this.second = second; + this.resultSelector = resultSelector; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new _ZipAwaitWithCancellation(first, second, resultSelector, cancellationToken); + } + + sealed class _ZipAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action firstMoveNextCoreDelegate = FirstMoveNextCore; + static readonly Action secondMoveNextCoreDelegate = SecondMoveNextCore; + static readonly Action resultAwaitCoreDelegate = ResultAwaitCore; + + readonly IUniTaskAsyncEnumerable first; + readonly IUniTaskAsyncEnumerable second; + readonly Func> resultSelector; + + CancellationToken cancellationToken; + + IUniTaskAsyncEnumerator firstEnumerator; + IUniTaskAsyncEnumerator secondEnumerator; + + UniTask.Awaiter firstAwaiter; + UniTask.Awaiter secondAwaiter; + UniTask.Awaiter resultAwaiter; + + public _ZipAwaitWithCancellation(IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, Func> resultSelector, CancellationToken cancellationToken) + { + this.first = first; + this.second = second; + this.resultSelector = resultSelector; + this.cancellationToken = cancellationToken; + TaskTracker.TrackActiveTask(this, 3); + } + + public TResult Current { get; private set; } + + public UniTask MoveNextAsync() + { + completionSource.Reset(); + + if (firstEnumerator == null) + { + firstEnumerator = first.GetAsyncEnumerator(cancellationToken); + secondEnumerator = second.GetAsyncEnumerator(cancellationToken); + } + + firstAwaiter = firstEnumerator.MoveNextAsync().GetAwaiter(); + + if (firstAwaiter.IsCompleted) + { + FirstMoveNextCore(this); + } + else + { + firstAwaiter.SourceOnCompleted(firstMoveNextCoreDelegate, this); + } + + return new UniTask(this, completionSource.Version); + } + + static void FirstMoveNextCore(object state) + { + var self = (_ZipAwaitWithCancellation)state; + + if (self.TryGetResult(self.firstAwaiter, out var result)) + { + if (result) + { + try + { + self.secondAwaiter = self.secondEnumerator.MoveNextAsync().GetAwaiter(); + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + return; + } + + if (self.secondAwaiter.IsCompleted) + { + SecondMoveNextCore(self); + } + else + { + self.secondAwaiter.SourceOnCompleted(secondMoveNextCoreDelegate, self); + } + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + static void SecondMoveNextCore(object state) + { + var self = (_ZipAwaitWithCancellation)state; + + if (self.TryGetResult(self.secondAwaiter, out var result)) + { + if (result) + { + try + { + self.resultAwaiter = self.resultSelector(self.firstEnumerator.Current, self.secondEnumerator.Current, self.cancellationToken).GetAwaiter(); + if (self.resultAwaiter.IsCompleted) + { + ResultAwaitCore(self); + } + else + { + self.resultAwaiter.SourceOnCompleted(resultAwaitCoreDelegate, self); + } + } + catch (Exception ex) + { + self.completionSource.TrySetException(ex); + } + } + else + { + self.completionSource.TrySetResult(false); + } + } + } + + static void ResultAwaitCore(object state) + { + var self = (_ZipAwaitWithCancellation)state; + + if (self.TryGetResult(self.resultAwaiter, out var result)) + { + self.Current = result; + + if (self.cancellationToken.IsCancellationRequested) + { + self.completionSource.TrySetCanceled(self.cancellationToken); + } + else + { + self.completionSource.TrySetResult(true); + } + } + } + + public async UniTask DisposeAsync() + { + TaskTracker.RemoveTracking(this); + if (firstEnumerator != null) + { + await firstEnumerator.DisposeAsync(); + } + if (secondEnumerator != null) + { + await secondEnumerator.DisposeAsync(); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Zip.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Zip.cs.meta new file mode 100644 index 0000000..bf12163 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Linq/Zip.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: acc1acff153e347418f0f30b1c535994 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/MoveNextSource.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/MoveNextSource.cs new file mode 100644 index 0000000..3e9ca23 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/MoveNextSource.cs @@ -0,0 +1,63 @@ +using System; + +namespace Cysharp.Threading.Tasks +{ + public abstract class MoveNextSource : IUniTaskSource + { + protected UniTaskCompletionSourceCore completionSource; + + public bool GetResult(short token) + { + return completionSource.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return completionSource.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + completionSource.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return completionSource.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + completionSource.GetResult(token); + } + + protected bool TryGetResult(UniTask.Awaiter awaiter, out T result) + { + try + { + result = awaiter.GetResult(); + return true; + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + result = default; + return false; + } + } + + protected bool TryGetResult(UniTask.Awaiter awaiter) + { + try + { + awaiter.GetResult(); + return true; + } + catch (Exception ex) + { + completionSource.TrySetException(ex); + return false; + } + } + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/MoveNextSource.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/MoveNextSource.cs.meta new file mode 100644 index 0000000..60a0908 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/MoveNextSource.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dc4c5dc2a5f246e4f8df44cab735826c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/PlayerLoopHelper.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/PlayerLoopHelper.cs new file mode 100644 index 0000000..3c605a4 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/PlayerLoopHelper.cs @@ -0,0 +1,564 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Linq; +using UnityEngine; +using Cysharp.Threading.Tasks.Internal; +using System.Threading; + +#if UNITY_2019_3_OR_NEWER +using UnityEngine.LowLevel; +using PlayerLoopType = UnityEngine.PlayerLoop; +#else +using UnityEngine.Experimental.LowLevel; +using PlayerLoopType = UnityEngine.Experimental.PlayerLoop; +#endif + +#if UNITY_EDITOR +using UnityEditor; +#endif + +namespace Cysharp.Threading.Tasks +{ + public static class UniTaskLoopRunners + { + public struct UniTaskLoopRunnerInitialization { }; + public struct UniTaskLoopRunnerEarlyUpdate { }; + public struct UniTaskLoopRunnerFixedUpdate { }; + public struct UniTaskLoopRunnerPreUpdate { }; + public struct UniTaskLoopRunnerUpdate { }; + public struct UniTaskLoopRunnerPreLateUpdate { }; + public struct UniTaskLoopRunnerPostLateUpdate { }; + + // Last + + public struct UniTaskLoopRunnerLastInitialization { }; + public struct UniTaskLoopRunnerLastEarlyUpdate { }; + public struct UniTaskLoopRunnerLastFixedUpdate { }; + public struct UniTaskLoopRunnerLastPreUpdate { }; + public struct UniTaskLoopRunnerLastUpdate { }; + public struct UniTaskLoopRunnerLastPreLateUpdate { }; + public struct UniTaskLoopRunnerLastPostLateUpdate { }; + + // Yield + + public struct UniTaskLoopRunnerYieldInitialization { }; + public struct UniTaskLoopRunnerYieldEarlyUpdate { }; + public struct UniTaskLoopRunnerYieldFixedUpdate { }; + public struct UniTaskLoopRunnerYieldPreUpdate { }; + public struct UniTaskLoopRunnerYieldUpdate { }; + public struct UniTaskLoopRunnerYieldPreLateUpdate { }; + public struct UniTaskLoopRunnerYieldPostLateUpdate { }; + + // Yield Last + + public struct UniTaskLoopRunnerLastYieldInitialization { }; + public struct UniTaskLoopRunnerLastYieldEarlyUpdate { }; + public struct UniTaskLoopRunnerLastYieldFixedUpdate { }; + public struct UniTaskLoopRunnerLastYieldPreUpdate { }; + public struct UniTaskLoopRunnerLastYieldUpdate { }; + public struct UniTaskLoopRunnerLastYieldPreLateUpdate { }; + public struct UniTaskLoopRunnerLastYieldPostLateUpdate { }; + +#if UNITY_2020_2_OR_NEWER + public struct UniTaskLoopRunnerTimeUpdate { }; + public struct UniTaskLoopRunnerLastTimeUpdate { }; + public struct UniTaskLoopRunnerYieldTimeUpdate { }; + public struct UniTaskLoopRunnerLastYieldTimeUpdate { }; +#endif + } + + public enum PlayerLoopTiming + { + Initialization = 0, + LastInitialization = 1, + + EarlyUpdate = 2, + LastEarlyUpdate = 3, + + FixedUpdate = 4, + LastFixedUpdate = 5, + + PreUpdate = 6, + LastPreUpdate = 7, + + Update = 8, + LastUpdate = 9, + + PreLateUpdate = 10, + LastPreLateUpdate = 11, + + PostLateUpdate = 12, + LastPostLateUpdate = 13, + +#if UNITY_2020_2_OR_NEWER + // Unity 2020.2 added TimeUpdate https://docs.unity3d.com/2020.2/Documentation/ScriptReference/PlayerLoop.TimeUpdate.html + TimeUpdate = 14, + LastTimeUpdate = 15, +#endif + } + + [Flags] + public enum InjectPlayerLoopTimings + { + /// + /// Preset: All loops(default). + /// + All = + Initialization | LastInitialization | + EarlyUpdate | LastEarlyUpdate | + FixedUpdate | LastFixedUpdate | + PreUpdate | LastPreUpdate | + Update | LastUpdate | + PreLateUpdate | LastPreLateUpdate | + PostLateUpdate | LastPostLateUpdate +#if UNITY_2020_2_OR_NEWER + | TimeUpdate | LastTimeUpdate, +#else + , +#endif + + /// + /// Preset: All without last except LastPostLateUpdate. + /// + Standard = + Initialization | + EarlyUpdate | + FixedUpdate | + PreUpdate | + Update | + PreLateUpdate | + PostLateUpdate | LastPostLateUpdate +#if UNITY_2020_2_OR_NEWER + | TimeUpdate +#endif + , + + /// + /// Preset: Minimum pattern, Update | FixedUpdate | LastPostLateUpdate + /// + Minimum = + Update | FixedUpdate | LastPostLateUpdate, + + // PlayerLoopTiming + + Initialization = 1, + LastInitialization = 2, + + EarlyUpdate = 4, + LastEarlyUpdate = 8, + + FixedUpdate = 16, + LastFixedUpdate = 32, + + PreUpdate = 64, + LastPreUpdate = 128, + + Update = 256, + LastUpdate = 512, + + PreLateUpdate = 1024, + LastPreLateUpdate = 2048, + + PostLateUpdate = 4096, + LastPostLateUpdate = 8192 + +#if UNITY_2020_2_OR_NEWER + , + // Unity 2020.2 added TimeUpdate https://docs.unity3d.com/2020.2/Documentation/ScriptReference/PlayerLoop.TimeUpdate.html + TimeUpdate = 16384, + LastTimeUpdate = 32768 +#endif + } + + public interface IPlayerLoopItem + { + bool MoveNext(); + } + + public static class PlayerLoopHelper + { + static readonly ContinuationQueue ThrowMarkerContinuationQueue = new ContinuationQueue(PlayerLoopTiming.Initialization); + static readonly PlayerLoopRunner ThrowMarkerPlayerLoopRunner = new PlayerLoopRunner(PlayerLoopTiming.Initialization); + + public static SynchronizationContext UnitySynchronizationContext => unitySynchronizationContext; + public static int MainThreadId => mainThreadId; + internal static string ApplicationDataPath => applicationDataPath; + + public static bool IsMainThread => Thread.CurrentThread.ManagedThreadId == mainThreadId; + + static int mainThreadId; + static string applicationDataPath; + static SynchronizationContext unitySynchronizationContext; + static ContinuationQueue[] yielders; + static PlayerLoopRunner[] runners; + internal static bool IsEditorApplicationQuitting { get; private set; } + static PlayerLoopSystem[] InsertRunner(PlayerLoopSystem loopSystem, + bool injectOnFirst, + Type loopRunnerYieldType, ContinuationQueue cq, + Type loopRunnerType, PlayerLoopRunner runner) + { + +#if UNITY_EDITOR + EditorApplication.playModeStateChanged += (state) => + { + if (state == PlayModeStateChange.EnteredEditMode || state == PlayModeStateChange.ExitingEditMode) + { + IsEditorApplicationQuitting = true; + // run rest action before clear. + if (runner != null) + { + runner.Run(); + runner.Clear(); + } + if (cq != null) + { + cq.Run(); + cq.Clear(); + } + IsEditorApplicationQuitting = false; + } + }; +#endif + + var yieldLoop = new PlayerLoopSystem + { + type = loopRunnerYieldType, + updateDelegate = cq.Run + }; + + var runnerLoop = new PlayerLoopSystem + { + type = loopRunnerType, + updateDelegate = runner.Run + }; + + // Remove items from previous initializations. + var source = RemoveRunner(loopSystem, loopRunnerYieldType, loopRunnerType); + var dest = new PlayerLoopSystem[source.Length + 2]; + + Array.Copy(source, 0, dest, injectOnFirst ? 2 : 0, source.Length); + if (injectOnFirst) + { + dest[0] = yieldLoop; + dest[1] = runnerLoop; + } + else + { + dest[dest.Length - 2] = yieldLoop; + dest[dest.Length - 1] = runnerLoop; + } + + return dest; + } + + static PlayerLoopSystem[] RemoveRunner(PlayerLoopSystem loopSystem, Type loopRunnerYieldType, Type loopRunnerType) + { + return loopSystem.subSystemList + .Where(ls => ls.type != loopRunnerYieldType && ls.type != loopRunnerType) + .ToArray(); + } + + static PlayerLoopSystem[] InsertUniTaskSynchronizationContext(PlayerLoopSystem loopSystem) + { + var loop = new PlayerLoopSystem + { + type = typeof(UniTaskSynchronizationContext), + updateDelegate = UniTaskSynchronizationContext.Run + }; + + // Remove items from previous initializations. + var source = loopSystem.subSystemList + .Where(ls => ls.type != typeof(UniTaskSynchronizationContext)) + .ToArray(); + + var dest = new System.Collections.Generic.List(source); + + var index = dest.FindIndex(x => x.type.Name == "ScriptRunDelayedTasks"); + if (index == -1) + { + index = dest.FindIndex(x => x.type.Name == "UniTaskLoopRunnerUpdate"); + } + + dest.Insert(index + 1, loop); + + return dest.ToArray(); + } + + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] + static void Init() + { + // capture default(unity) sync-context. + unitySynchronizationContext = SynchronizationContext.Current; + mainThreadId = Thread.CurrentThread.ManagedThreadId; + try + { + applicationDataPath = Application.dataPath; + } + catch { } + +#if UNITY_EDITOR && UNITY_2019_3_OR_NEWER + // When domain reload is disabled, re-initialization is required when entering play mode; + // otherwise, pending tasks will leak between play mode sessions. + var domainReloadDisabled = UnityEditor.EditorSettings.enterPlayModeOptionsEnabled && + UnityEditor.EditorSettings.enterPlayModeOptions.HasFlag(UnityEditor.EnterPlayModeOptions.DisableDomainReload); + if (!domainReloadDisabled && runners != null) return; +#else + if (runners != null) return; // already initialized +#endif + + var playerLoop = +#if UNITY_2019_3_OR_NEWER + PlayerLoop.GetCurrentPlayerLoop(); +#else + PlayerLoop.GetDefaultPlayerLoop(); +#endif + + Initialize(ref playerLoop); + } + + +#if UNITY_EDITOR + + [InitializeOnLoadMethod] + static void InitOnEditor() + { + // Execute the play mode init method + Init(); + + // register an Editor update delegate, used to forcing playerLoop update + EditorApplication.update += ForceEditorPlayerLoopUpdate; + } + + private static void ForceEditorPlayerLoopUpdate() + { + if (EditorApplication.isPlayingOrWillChangePlaymode || EditorApplication.isCompiling || EditorApplication.isUpdating) + { + // Not in Edit mode, don't interfere + return; + } + + // EditorApplication.QueuePlayerLoopUpdate causes performance issue, don't call directly. + // EditorApplication.QueuePlayerLoopUpdate(); + + if (yielders != null) + { + foreach (var item in yielders) + { + if (item != null) item.Run(); + } + } + + if (runners != null) + { + foreach (var item in runners) + { + if (item != null) item.Run(); + } + } + + UniTaskSynchronizationContext.Run(); + } + +#endif + + private static int FindLoopSystemIndex(PlayerLoopSystem[] playerLoopList, Type systemType) + { + for (int i = 0; i < playerLoopList.Length; i++) + { + if (playerLoopList[i].type == systemType) + { + return i; + } + } + + throw new Exception("Target PlayerLoopSystem does not found. Type:" + systemType.FullName); + } + + static void InsertLoop(PlayerLoopSystem[] copyList, InjectPlayerLoopTimings injectTimings, Type loopType, InjectPlayerLoopTimings targetTimings, + int index, bool injectOnFirst, Type loopRunnerYieldType, Type loopRunnerType, PlayerLoopTiming playerLoopTiming) + { + var i = FindLoopSystemIndex(copyList, loopType); + if ((injectTimings & targetTimings) == targetTimings) + { + copyList[i].subSystemList = InsertRunner(copyList[i], injectOnFirst, + loopRunnerYieldType, yielders[index] = new ContinuationQueue(playerLoopTiming), + loopRunnerType, runners[index] = new PlayerLoopRunner(playerLoopTiming)); + } + else + { + copyList[i].subSystemList = RemoveRunner(copyList[i], loopRunnerYieldType, loopRunnerType); + } + } + + public static void Initialize(ref PlayerLoopSystem playerLoop, InjectPlayerLoopTimings injectTimings = InjectPlayerLoopTimings.All) + { +#if UNITY_2020_2_OR_NEWER + yielders = new ContinuationQueue[16]; + runners = new PlayerLoopRunner[16]; +#else + yielders = new ContinuationQueue[14]; + runners = new PlayerLoopRunner[14]; +#endif + + var copyList = playerLoop.subSystemList.ToArray(); + + // Initialization + InsertLoop(copyList, injectTimings, typeof(PlayerLoopType.Initialization), + InjectPlayerLoopTimings.Initialization, 0, true, + typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldInitialization), typeof(UniTaskLoopRunners.UniTaskLoopRunnerInitialization), PlayerLoopTiming.Initialization); + + InsertLoop(copyList, injectTimings, typeof(PlayerLoopType.Initialization), + InjectPlayerLoopTimings.LastInitialization, 1, false, + typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldInitialization), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastInitialization), PlayerLoopTiming.LastInitialization); + + // EarlyUpdate + InsertLoop(copyList, injectTimings, typeof(PlayerLoopType.EarlyUpdate), + InjectPlayerLoopTimings.EarlyUpdate, 2, true, + typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldEarlyUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerEarlyUpdate), PlayerLoopTiming.EarlyUpdate); + + InsertLoop(copyList, injectTimings, typeof(PlayerLoopType.EarlyUpdate), + InjectPlayerLoopTimings.LastEarlyUpdate, 3, false, + typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldEarlyUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastEarlyUpdate), PlayerLoopTiming.LastEarlyUpdate); + + // FixedUpdate + InsertLoop(copyList, injectTimings, typeof(PlayerLoopType.FixedUpdate), + InjectPlayerLoopTimings.FixedUpdate, 4, true, + typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldFixedUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerFixedUpdate), PlayerLoopTiming.FixedUpdate); + + InsertLoop(copyList, injectTimings, typeof(PlayerLoopType.FixedUpdate), + InjectPlayerLoopTimings.LastFixedUpdate, 5, false, + typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldFixedUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastFixedUpdate), PlayerLoopTiming.LastFixedUpdate); + + // PreUpdate + InsertLoop(copyList, injectTimings, typeof(PlayerLoopType.PreUpdate), + InjectPlayerLoopTimings.PreUpdate, 6, true, + typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldPreUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerPreUpdate), PlayerLoopTiming.PreUpdate); + + InsertLoop(copyList, injectTimings, typeof(PlayerLoopType.PreUpdate), + InjectPlayerLoopTimings.LastPreUpdate, 7, false, + typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldPreUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastPreUpdate), PlayerLoopTiming.LastPreUpdate); + + // Update + InsertLoop(copyList, injectTimings, typeof(PlayerLoopType.Update), + InjectPlayerLoopTimings.Update, 8, true, + typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerUpdate), PlayerLoopTiming.Update); + + InsertLoop(copyList, injectTimings, typeof(PlayerLoopType.Update), + InjectPlayerLoopTimings.LastUpdate, 9, false, + typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastUpdate), PlayerLoopTiming.LastUpdate); + + // PreLateUpdate + InsertLoop(copyList, injectTimings, typeof(PlayerLoopType.PreLateUpdate), + InjectPlayerLoopTimings.PreLateUpdate, 10, true, + typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldPreLateUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerPreLateUpdate), PlayerLoopTiming.PreLateUpdate); + + InsertLoop(copyList, injectTimings, typeof(PlayerLoopType.PreLateUpdate), + InjectPlayerLoopTimings.LastPreLateUpdate, 11, false, + typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldPreLateUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastPreLateUpdate), PlayerLoopTiming.LastPreLateUpdate); + + // PostLateUpdate + InsertLoop(copyList, injectTimings, typeof(PlayerLoopType.PostLateUpdate), + InjectPlayerLoopTimings.PostLateUpdate, 12, true, + typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldPostLateUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerPostLateUpdate), PlayerLoopTiming.PostLateUpdate); + + InsertLoop(copyList, injectTimings, typeof(PlayerLoopType.PostLateUpdate), + InjectPlayerLoopTimings.LastPostLateUpdate, 13, false, + typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldPostLateUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastPostLateUpdate), PlayerLoopTiming.LastPostLateUpdate); + +#if UNITY_2020_2_OR_NEWER + // TimeUpdate + InsertLoop(copyList, injectTimings, typeof(PlayerLoopType.TimeUpdate), + InjectPlayerLoopTimings.TimeUpdate, 14, true, + typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldTimeUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerTimeUpdate), PlayerLoopTiming.TimeUpdate); + + InsertLoop(copyList, injectTimings, typeof(PlayerLoopType.TimeUpdate), + InjectPlayerLoopTimings.LastTimeUpdate, 15, false, + typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldTimeUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastTimeUpdate), PlayerLoopTiming.LastTimeUpdate); +#endif + + // Insert UniTaskSynchronizationContext to Update loop + var i = FindLoopSystemIndex(copyList, typeof(PlayerLoopType.Update)); + copyList[i].subSystemList = InsertUniTaskSynchronizationContext(copyList[i]); + + playerLoop.subSystemList = copyList; + PlayerLoop.SetPlayerLoop(playerLoop); + } + + public static void AddAction(PlayerLoopTiming timing, IPlayerLoopItem action) + { + var runner = runners[(int)timing]; + if (runner == null) + { + ThrowInvalidLoopTiming(timing); + } + runner.AddAction(action); + } + + static void ThrowInvalidLoopTiming(PlayerLoopTiming playerLoopTiming) + { + throw new InvalidOperationException("Target playerLoopTiming is not injected. Please check PlayerLoopHelper.Initialize. PlayerLoopTiming:" + playerLoopTiming); + } + + public static void AddContinuation(PlayerLoopTiming timing, Action continuation) + { + var q = yielders[(int)timing]; + if (q == null) + { + ThrowInvalidLoopTiming(timing); + } + q.Enqueue(continuation); + } + + // Diagnostics helper + +#if UNITY_2019_3_OR_NEWER + + public static void DumpCurrentPlayerLoop() + { + var playerLoop = UnityEngine.LowLevel.PlayerLoop.GetCurrentPlayerLoop(); + + var sb = new System.Text.StringBuilder(); + sb.AppendLine($"PlayerLoop List"); + foreach (var header in playerLoop.subSystemList) + { + sb.AppendFormat("------{0}------", header.type.Name); + sb.AppendLine(); + foreach (var subSystem in header.subSystemList) + { + sb.AppendFormat("{0}", subSystem.type.Name); + sb.AppendLine(); + + if (subSystem.subSystemList != null) + { + UnityEngine.Debug.LogWarning("More Subsystem:" + subSystem.subSystemList.Length); + } + } + } + + UnityEngine.Debug.Log(sb.ToString()); + } + + public static bool IsInjectedUniTaskPlayerLoop() + { + var playerLoop = UnityEngine.LowLevel.PlayerLoop.GetCurrentPlayerLoop(); + + foreach (var header in playerLoop.subSystemList) + { + foreach (var subSystem in header.subSystemList) + { + if (subSystem.type == typeof(UniTaskLoopRunners.UniTaskLoopRunnerInitialization)) + { + return true; + } + } + } + + return false; + } + +#endif + + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/PlayerLoopHelper.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/PlayerLoopHelper.cs.meta new file mode 100644 index 0000000..2487ef7 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/PlayerLoopHelper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 15fb5b85042f19640b973ce651795aca +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/PlayerLoopTimer.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/PlayerLoopTimer.cs new file mode 100644 index 0000000..f8a877a --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/PlayerLoopTimer.cs @@ -0,0 +1,262 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System.Threading; +using System; +using Cysharp.Threading.Tasks.Internal; +using UnityEngine; + +namespace Cysharp.Threading.Tasks +{ + public abstract class PlayerLoopTimer : IDisposable, IPlayerLoopItem + { + readonly CancellationToken cancellationToken; + readonly Action timerCallback; + readonly object state; + readonly PlayerLoopTiming playerLoopTiming; + readonly bool periodic; + + bool isRunning; + bool tryStop; + bool isDisposed; + + protected PlayerLoopTimer(bool periodic, PlayerLoopTiming playerLoopTiming, CancellationToken cancellationToken, Action timerCallback, object state) + { + this.periodic = periodic; + this.playerLoopTiming = playerLoopTiming; + this.cancellationToken = cancellationToken; + this.timerCallback = timerCallback; + this.state = state; + } + + public static PlayerLoopTimer Create(TimeSpan interval, bool periodic, DelayType delayType, PlayerLoopTiming playerLoopTiming, CancellationToken cancellationToken, Action timerCallback, object state) + { +#if UNITY_EDITOR + // force use Realtime. + if (PlayerLoopHelper.IsMainThread && !UnityEditor.EditorApplication.isPlaying) + { + delayType = DelayType.Realtime; + } +#endif + + switch (delayType) + { + case DelayType.UnscaledDeltaTime: + return new IgnoreTimeScalePlayerLoopTimer(interval, periodic, playerLoopTiming, cancellationToken, timerCallback, state); + case DelayType.Realtime: + return new RealtimePlayerLoopTimer(interval, periodic, playerLoopTiming, cancellationToken, timerCallback, state); + case DelayType.DeltaTime: + default: + return new DeltaTimePlayerLoopTimer(interval, periodic, playerLoopTiming, cancellationToken, timerCallback, state); + } + } + + public static PlayerLoopTimer StartNew(TimeSpan interval, bool periodic, DelayType delayType, PlayerLoopTiming playerLoopTiming, CancellationToken cancellationToken, Action timerCallback, object state) + { + var timer = Create(interval, periodic, delayType, playerLoopTiming, cancellationToken, timerCallback, state); + timer.Restart(); + return timer; + } + + /// + /// Restart(Reset and Start) timer. + /// + public void Restart() + { + if (isDisposed) throw new ObjectDisposedException(null); + + ResetCore(null); // init state + if (!isRunning) + { + isRunning = true; + PlayerLoopHelper.AddAction(playerLoopTiming, this); + } + tryStop = false; + } + + /// + /// Restart(Reset and Start) and change interval. + /// + public void Restart(TimeSpan interval) + { + if (isDisposed) throw new ObjectDisposedException(null); + + ResetCore(interval); // init state + if (!isRunning) + { + isRunning = true; + PlayerLoopHelper.AddAction(playerLoopTiming, this); + } + tryStop = false; + } + + /// + /// Stop timer. + /// + public void Stop() + { + tryStop = true; + } + + protected abstract void ResetCore(TimeSpan? newInterval); + + public void Dispose() + { + isDisposed = true; + } + + bool IPlayerLoopItem.MoveNext() + { + if (isDisposed) + { + isRunning = false; + return false; + } + if (tryStop) + { + isRunning = false; + return false; + } + if (cancellationToken.IsCancellationRequested) + { + isRunning = false; + return false; + } + + if (!MoveNextCore()) + { + timerCallback(state); + + if (periodic) + { + ResetCore(null); + return true; + } + else + { + isRunning = false; + return false; + } + } + + return true; + } + + protected abstract bool MoveNextCore(); + } + + sealed class DeltaTimePlayerLoopTimer : PlayerLoopTimer + { + int initialFrame; + float elapsed; + float interval; + + public DeltaTimePlayerLoopTimer(TimeSpan interval, bool periodic, PlayerLoopTiming playerLoopTiming, CancellationToken cancellationToken, Action timerCallback, object state) + : base(periodic, playerLoopTiming, cancellationToken, timerCallback, state) + { + ResetCore(interval); + } + + protected override bool MoveNextCore() + { + if (elapsed == 0.0f) + { + if (initialFrame == Time.frameCount) + { + return true; + } + } + + elapsed += Time.deltaTime; + if (elapsed >= interval) + { + return false; + } + + return true; + } + + protected override void ResetCore(TimeSpan? interval) + { + this.elapsed = 0.0f; + this.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1; + if (interval != null) + { + this.interval = (float)interval.Value.TotalSeconds; + } + } + } + + sealed class IgnoreTimeScalePlayerLoopTimer : PlayerLoopTimer + { + int initialFrame; + float elapsed; + float interval; + + public IgnoreTimeScalePlayerLoopTimer(TimeSpan interval, bool periodic, PlayerLoopTiming playerLoopTiming, CancellationToken cancellationToken, Action timerCallback, object state) + : base(periodic, playerLoopTiming, cancellationToken, timerCallback, state) + { + ResetCore(interval); + } + + protected override bool MoveNextCore() + { + if (elapsed == 0.0f) + { + if (initialFrame == Time.frameCount) + { + return true; + } + } + + elapsed += Time.unscaledDeltaTime; + if (elapsed >= interval) + { + return false; + } + + return true; + } + + protected override void ResetCore(TimeSpan? interval) + { + this.elapsed = 0.0f; + this.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1; + if (interval != null) + { + this.interval = (float)interval.Value.TotalSeconds; + } + } + } + + sealed class RealtimePlayerLoopTimer : PlayerLoopTimer + { + ValueStopwatch stopwatch; + long intervalTicks; + + public RealtimePlayerLoopTimer(TimeSpan interval, bool periodic, PlayerLoopTiming playerLoopTiming, CancellationToken cancellationToken, Action timerCallback, object state) + : base(periodic, playerLoopTiming, cancellationToken, timerCallback, state) + { + ResetCore(interval); + } + + protected override bool MoveNextCore() + { + if (stopwatch.ElapsedTicks >= intervalTicks) + { + return false; + } + + return true; + } + + protected override void ResetCore(TimeSpan? interval) + { + this.stopwatch = ValueStopwatch.StartNew(); + if (interval != null) + { + this.intervalTicks = interval.Value.Ticks; + } + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/PlayerLoopTimer.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/PlayerLoopTimer.cs.meta new file mode 100644 index 0000000..eb2b50a --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/PlayerLoopTimer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 57095a17fdca7ee4380450910afc7f26 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Progress.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Progress.cs new file mode 100644 index 0000000..ed11290 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Progress.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks +{ + /// + /// Lightweight IProgress[T] factory. + /// + public static class Progress + { + public static IProgress Create(Action handler) + { + if (handler == null) return NullProgress.Instance; + return new AnonymousProgress(handler); + } + + public static IProgress CreateOnlyValueChanged(Action handler, IEqualityComparer comparer = null) + { + if (handler == null) return NullProgress.Instance; +#if UNITY_2018_3_OR_NEWER + return new OnlyValueChangedProgress(handler, comparer ?? UnityEqualityComparer.GetDefault()); +#else + return new OnlyValueChangedProgress(handler, comparer ?? EqualityComparer.Default); +#endif + } + + sealed class NullProgress : IProgress + { + public static readonly IProgress Instance = new NullProgress(); + + NullProgress() + { + + } + + public void Report(T value) + { + } + } + + sealed class AnonymousProgress : IProgress + { + readonly Action action; + + public AnonymousProgress(Action action) + { + this.action = action; + } + + public void Report(T value) + { + action(value); + } + } + + sealed class OnlyValueChangedProgress : IProgress + { + readonly Action action; + readonly IEqualityComparer comparer; + bool isFirstCall; + T latestValue; + + public OnlyValueChangedProgress(Action action, IEqualityComparer comparer) + { + this.action = action; + this.comparer = comparer; + this.isFirstCall = true; + } + + public void Report(T value) + { + if (isFirstCall) + { + isFirstCall = false; + } + else if (comparer.Equals(value, latestValue)) + { + return; + } + + latestValue = value; + action(value); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Progress.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Progress.cs.meta new file mode 100644 index 0000000..f0e1f19 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Progress.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e3377e2ae934ed54fb8fd5388e2d9eb9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TaskPool.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TaskPool.cs new file mode 100644 index 0000000..8e61932 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TaskPool.cs @@ -0,0 +1,123 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Threading; + +namespace Cysharp.Threading.Tasks +{ + // internaly used but public, allow to user create custom operator with pooling. + + public static class TaskPool + { + internal static int MaxPoolSize; + + // avoid to use ConcurrentDictionary for safety of WebGL build. + static Dictionary> sizes = new Dictionary>(); + + static TaskPool() + { + try + { + var value = Environment.GetEnvironmentVariable("UNITASK_MAX_POOLSIZE"); + if (value != null) + { + if (int.TryParse(value, out var size)) + { + MaxPoolSize = size; + return; + } + } + } + catch { } + + MaxPoolSize = int.MaxValue; + } + + public static void SetMaxPoolSize(int maxPoolSize) + { + MaxPoolSize = maxPoolSize; + } + + public static IEnumerable<(Type, int)> GetCacheSizeInfo() + { + lock (sizes) + { + foreach (var item in sizes) + { + yield return (item.Key, item.Value()); + } + } + } + + public static void RegisterSizeGetter(Type type, Func getSize) + { + lock (sizes) + { + sizes[type] = getSize; + } + } + } + + public interface ITaskPoolNode + { + ref T NextNode { get; } + } + + // mutable struct, don't mark readonly. + [StructLayout(LayoutKind.Auto)] + public struct TaskPool + where T : class, ITaskPoolNode + { + int gate; + int size; + T root; + + public int Size => size; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool TryPop(out T result) + { + if (Interlocked.CompareExchange(ref gate, 1, 0) == 0) + { + var v = root; + if (!(v is null)) + { + ref var nextNode = ref v.NextNode; + root = nextNode; + nextNode = null; + size--; + result = v; + Volatile.Write(ref gate, 0); + return true; + } + + Volatile.Write(ref gate, 0); + } + result = default; + return false; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool TryPush(T item) + { + if (Interlocked.CompareExchange(ref gate, 1, 0) == 0) + { + if (size < TaskPool.MaxPoolSize) + { + item.NextNode = root; + root = item; + size++; + Volatile.Write(ref gate, 0); + return true; + } + else + { + Volatile.Write(ref gate, 0); + } + } + return false; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TaskPool.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TaskPool.cs.meta new file mode 100644 index 0000000..94c7805 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TaskPool.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 19f4e6575150765449cc99f25f06f25f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TimeoutController.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TimeoutController.cs new file mode 100644 index 0000000..ad95cb5 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TimeoutController.cs @@ -0,0 +1,129 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks +{ + // CancellationTokenSource itself can not reuse but CancelAfter(Timeout.InfiniteTimeSpan) allows reuse if did not reach timeout. + // Similar discussion: + // https://github.com/dotnet/runtime/issues/4694 + // https://github.com/dotnet/runtime/issues/48492 + // This TimeoutController emulate similar implementation, using CancelAfterSlim; to achieve zero allocation timeout. + + public sealed class TimeoutController : IDisposable + { + readonly static Action CancelCancellationTokenSourceStateDelegate = new Action(CancelCancellationTokenSourceState); + + static void CancelCancellationTokenSourceState(object state) + { + var cts = (CancellationTokenSource)state; + cts.Cancel(); + } + + CancellationTokenSource timeoutSource; + CancellationTokenSource linkedSource; + PlayerLoopTimer timer; + bool isDisposed; + + readonly DelayType delayType; + readonly PlayerLoopTiming delayTiming; + readonly CancellationTokenSource originalLinkCancellationTokenSource; + + public TimeoutController(DelayType delayType = DelayType.DeltaTime, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update) + { + this.timeoutSource = new CancellationTokenSource(); + this.originalLinkCancellationTokenSource = null; + this.linkedSource = null; + this.delayType = delayType; + this.delayTiming = delayTiming; + } + + public TimeoutController(CancellationTokenSource linkCancellationTokenSource, DelayType delayType = DelayType.DeltaTime, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update) + { + this.timeoutSource = new CancellationTokenSource(); + this.originalLinkCancellationTokenSource = linkCancellationTokenSource; + this.linkedSource = CancellationTokenSource.CreateLinkedTokenSource(timeoutSource.Token, linkCancellationTokenSource.Token); + this.delayType = delayType; + this.delayTiming = delayTiming; + } + + public CancellationToken Timeout(int millisecondsTimeout) + { + return Timeout(TimeSpan.FromMilliseconds(millisecondsTimeout)); + } + + public CancellationToken Timeout(TimeSpan timeout) + { + if (originalLinkCancellationTokenSource != null && originalLinkCancellationTokenSource.IsCancellationRequested) + { + return originalLinkCancellationTokenSource.Token; + } + + // Timeouted, create new source and timer. + if (timeoutSource.IsCancellationRequested) + { + timeoutSource.Dispose(); + timeoutSource = new CancellationTokenSource(); + if (linkedSource != null) + { + this.linkedSource.Cancel(); + this.linkedSource.Dispose(); + this.linkedSource = CancellationTokenSource.CreateLinkedTokenSource(timeoutSource.Token, originalLinkCancellationTokenSource.Token); + } + + timer?.Dispose(); + timer = null; + } + + var useSource = (linkedSource != null) ? linkedSource : timeoutSource; + var token = useSource.Token; + if (timer == null) + { + // Timer complete => timeoutSource.Cancel() -> linkedSource will be canceled. + // (linked)token is canceled => stop timer + timer = PlayerLoopTimer.StartNew(timeout, false, delayType, delayTiming, token, CancelCancellationTokenSourceStateDelegate, timeoutSource); + } + else + { + timer.Restart(timeout); + } + + return token; + } + + public bool IsTimeout() + { + return timeoutSource.IsCancellationRequested; + } + + public void Reset() + { + timer.Stop(); + } + + public void Dispose() + { + if (isDisposed) return; + + try + { + // stop timer. + timer.Dispose(); + + // cancel and dispose. + timeoutSource.Cancel(); + timeoutSource.Dispose(); + if (linkedSource != null) + { + linkedSource.Cancel(); + linkedSource.Dispose(); + } + } + finally + { + isDisposed = true; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TimeoutController.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TimeoutController.cs.meta new file mode 100644 index 0000000..4f3d16d --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TimeoutController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6347ab34d2db6d744a654e8d62d96b96 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TriggerEvent.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TriggerEvent.cs new file mode 100644 index 0000000..503e9db --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TriggerEvent.cs @@ -0,0 +1,311 @@ +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks +{ + public interface ITriggerHandler + { + void OnNext(T value); + void OnError(Exception ex); + void OnCompleted(); + void OnCanceled(CancellationToken cancellationToken); + + // set/get from TriggerEvent + ITriggerHandler Prev { get; set; } + ITriggerHandler Next { get; set; } + } + + // be careful to use, itself is struct. + public struct TriggerEvent + { + ITriggerHandler head; // head.prev is last + ITriggerHandler iteratingHead; + + bool preserveRemoveSelf; + ITriggerHandler iteratingNode; + + void LogError(Exception ex) + { +#if UNITY_2018_3_OR_NEWER + UnityEngine.Debug.LogException(ex); +#else + Console.WriteLine(ex); +#endif + } + + public void SetResult(T value) + { + if (iteratingNode != null) + { + throw new InvalidOperationException("Can not trigger itself in iterating."); + } + + var h = head; + while (h != null) + { + iteratingNode = h; + + try + { + h.OnNext(value); + } + catch (Exception ex) + { + LogError(ex); + Remove(h); + } + + if (preserveRemoveSelf) + { + preserveRemoveSelf = false; + iteratingNode = null; + var next = h.Next; + Remove(h); + h = next; + } + else + { + h = h.Next; + } + } + + iteratingNode = null; + if (iteratingHead != null) + { + Add(iteratingHead); + iteratingHead = null; + } + } + + public void SetCanceled(CancellationToken cancellationToken) + { + if (iteratingNode != null) + { + throw new InvalidOperationException("Can not trigger itself in iterating."); + } + + var h = head; + while (h != null) + { + iteratingNode = h; + try + { + h.OnCanceled(cancellationToken); + } + catch (Exception ex) + { + LogError(ex); + } + + preserveRemoveSelf = false; + iteratingNode = null; + var next = h.Next; + Remove(h); + h = next; + } + + iteratingNode = null; + if (iteratingHead != null) + { + Add(iteratingHead); + iteratingHead = null; + } + } + + public void SetCompleted() + { + if (iteratingNode != null) + { + throw new InvalidOperationException("Can not trigger itself in iterating."); + } + + var h = head; + while (h != null) + { + iteratingNode = h; + try + { + h.OnCompleted(); + } + catch (Exception ex) + { + LogError(ex); + } + + preserveRemoveSelf = false; + iteratingNode = null; + var next = h.Next; + Remove(h); + h = next; + } + + iteratingNode = null; + if (iteratingHead != null) + { + Add(iteratingHead); + iteratingHead = null; + } + } + + public void SetError(Exception exception) + { + if (iteratingNode != null) + { + throw new InvalidOperationException("Can not trigger itself in iterating."); + } + + var h = head; + while (h != null) + { + iteratingNode = h; + try + { + h.OnError(exception); + } + catch (Exception ex) + { + LogError(ex); + } + + preserveRemoveSelf = false; + iteratingNode = null; + var next = h.Next; + Remove(h); + h = next; + } + + iteratingNode = null; + if (iteratingHead != null) + { + Add(iteratingHead); + iteratingHead = null; + } + } + + public void Add(ITriggerHandler handler) + { + if (handler == null) throw new ArgumentNullException(nameof(handler)); + + // zero node. + if (head == null) + { + head = handler; + return; + } + + if (iteratingNode != null) + { + if (iteratingHead == null) + { + iteratingHead = handler; + return; + } + + var last = iteratingHead.Prev; + if (last == null) + { + // single node. + iteratingHead.Prev = handler; + iteratingHead.Next = handler; + handler.Prev = iteratingHead; + } + else + { + // multi node + iteratingHead.Prev = handler; + last.Next = handler; + handler.Prev = last; + } + } + else + { + var last = head.Prev; + if (last == null) + { + // single node. + head.Prev = handler; + head.Next = handler; + handler.Prev = head; + } + else + { + // multi node + head.Prev = handler; + last.Next = handler; + handler.Prev = last; + } + } + } + + public void Remove(ITriggerHandler handler) + { + if (handler == null) throw new ArgumentNullException(nameof(handler)); + + if (iteratingNode != null && iteratingNode == handler) + { + // if remove self, reserve remove self after invoke completed. + preserveRemoveSelf = true; + } + else + { + var prev = handler.Prev; + var next = handler.Next; + + if (next != null) + { + next.Prev = prev; + } + + if (handler == head) + { + head = next; + } + else if (handler == iteratingHead) + { + iteratingHead = next; + } + else + { + // when handler is head, prev indicate last so don't use it. + if (prev != null) + { + prev.Next = next; + } + } + + if (head != null) + { + if (head.Prev == handler) + { + if (prev != head) + { + head.Prev = prev; + } + else + { + head.Prev = null; + } + } + } + + if (iteratingHead != null) + { + if (iteratingHead.Prev == handler) + { + if (prev != iteratingHead.Prev) + { + iteratingHead.Prev = prev; + } + else + { + iteratingHead.Prev = null; + } + } + } + + handler.Prev = null; + handler.Next = null; + } + } + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TriggerEvent.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TriggerEvent.cs.meta new file mode 100644 index 0000000..bbd47af --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/TriggerEvent.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f68b22bb8f66f5c4885f9bd3c4fc43ed +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers.meta new file mode 100644 index 0000000..280b698 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 85c0c768ced512e42b24021b3258b669 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncAwakeTrigger.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncAwakeTrigger.cs new file mode 100644 index 0000000..a734f29 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncAwakeTrigger.cs @@ -0,0 +1,32 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System.Threading; +using UnityEngine; + +namespace Cysharp.Threading.Tasks.Triggers +{ + public static partial class AsyncTriggerExtensions + { + public static AsyncAwakeTrigger GetAsyncAwakeTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncAwakeTrigger GetAsyncAwakeTrigger(this Component component) + { + return component.gameObject.GetAsyncAwakeTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncAwakeTrigger : AsyncTriggerBase + { + public UniTask AwakeAsync() + { + if (calledAwake) return UniTask.CompletedTask; + + return ((IAsyncOneShotTrigger)new AsyncTriggerHandler(this, true)).OneShotAsync(); + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncAwakeTrigger.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncAwakeTrigger.cs.meta new file mode 100644 index 0000000..097fdb6 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncAwakeTrigger.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ef2840a2586894741a0ae211b8fd669b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncDestroyTrigger.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncDestroyTrigger.cs new file mode 100644 index 0000000..811f977 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncDestroyTrigger.cs @@ -0,0 +1,97 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System.Threading; +using UnityEngine; + +namespace Cysharp.Threading.Tasks.Triggers +{ + public static partial class AsyncTriggerExtensions + { + public static AsyncDestroyTrigger GetAsyncDestroyTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncDestroyTrigger GetAsyncDestroyTrigger(this Component component) + { + return component.gameObject.GetAsyncDestroyTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncDestroyTrigger : MonoBehaviour + { + bool awakeCalled = false; + bool called = false; + CancellationTokenSource cancellationTokenSource; + + public CancellationToken CancellationToken + { + get + { + if (cancellationTokenSource == null) + { + cancellationTokenSource = new CancellationTokenSource(); + } + + if (!awakeCalled) + { + PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this)); + } + + return cancellationTokenSource.Token; + } + } + + void Awake() + { + awakeCalled = true; + } + + void OnDestroy() + { + called = true; + + cancellationTokenSource?.Cancel(); + cancellationTokenSource?.Dispose(); + } + + public UniTask OnDestroyAsync() + { + if (called) return UniTask.CompletedTask; + + var tcs = new UniTaskCompletionSource(); + + // OnDestroy = Called Cancel. + CancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var tcs2 = (UniTaskCompletionSource)state; + tcs2.TrySetResult(); + }, tcs); + + return tcs.Task; + } + + class AwakeMonitor : IPlayerLoopItem + { + readonly AsyncDestroyTrigger trigger; + + public AwakeMonitor(AsyncDestroyTrigger trigger) + { + this.trigger = trigger; + } + + public bool MoveNext() + { + if (trigger.called) return false; + if (trigger == null) + { + trigger.OnDestroy(); + return false; + } + return true; + } + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncDestroyTrigger.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncDestroyTrigger.cs.meta new file mode 100644 index 0000000..6450049 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncDestroyTrigger.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f4afdcb1cbadf954ba8b1cf465429e17 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncStartTrigger.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncStartTrigger.cs new file mode 100644 index 0000000..63da82a --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncStartTrigger.cs @@ -0,0 +1,38 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using UnityEngine; + +namespace Cysharp.Threading.Tasks.Triggers +{ + public static partial class AsyncTriggerExtensions + { + public static AsyncStartTrigger GetAsyncStartTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncStartTrigger GetAsyncStartTrigger(this Component component) + { + return component.gameObject.GetAsyncStartTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncStartTrigger : AsyncTriggerBase + { + bool called; + + void Start() + { + called = true; + RaiseEvent(AsyncUnit.Default); + } + + public UniTask StartAsync() + { + if (called) return UniTask.CompletedTask; + + return ((IAsyncOneShotTrigger)new AsyncTriggerHandler(this, true)).OneShotAsync(); + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncStartTrigger.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncStartTrigger.cs.meta new file mode 100644 index 0000000..9ef06e8 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncStartTrigger.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b4fd0f75e54ec3d4fbcb7fc65b11646b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncTriggerBase.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncTriggerBase.cs new file mode 100644 index 0000000..fb6ca6e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncTriggerBase.cs @@ -0,0 +1,310 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Threading; +using UnityEngine; + +namespace Cysharp.Threading.Tasks.Triggers +{ + public abstract class AsyncTriggerBase : MonoBehaviour, IUniTaskAsyncEnumerable + { + TriggerEvent triggerEvent; + + internal protected bool calledAwake; + internal protected bool calledDestroy; + + void Awake() + { + calledAwake = true; + } + + void OnDestroy() + { + if (calledDestroy) return; + calledDestroy = true; + + triggerEvent.SetCompleted(); + } + + internal void AddHandler(ITriggerHandler handler) + { + if (!calledAwake) + { + PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this)); + } + + triggerEvent.Add(handler); + } + + internal void RemoveHandler(ITriggerHandler handler) + { + if (!calledAwake) + { + PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this)); + } + + triggerEvent.Remove(handler); + } + + protected void RaiseEvent(T value) + { + triggerEvent.SetResult(value); + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new AsyncTriggerEnumerator(this, cancellationToken); + } + + sealed class AsyncTriggerEnumerator : MoveNextSource, IUniTaskAsyncEnumerator, ITriggerHandler + { + static Action cancellationCallback = CancellationCallback; + + readonly AsyncTriggerBase parent; + CancellationToken cancellationToken; + CancellationTokenRegistration registration; + bool called; + bool isDisposed; + + public AsyncTriggerEnumerator(AsyncTriggerBase parent, CancellationToken cancellationToken) + { + this.parent = parent; + this.cancellationToken = cancellationToken; + } + + public void OnCanceled(CancellationToken cancellationToken = default) + { + completionSource.TrySetCanceled(cancellationToken); + } + + public void OnNext(T value) + { + Current = value; + completionSource.TrySetResult(true); + } + + public void OnCompleted() + { + completionSource.TrySetResult(false); + } + + public void OnError(Exception ex) + { + completionSource.TrySetException(ex); + } + + static void CancellationCallback(object state) + { + var self = (AsyncTriggerEnumerator)state; + self.DisposeAsync().Forget(); // sync + + self.completionSource.TrySetCanceled(self.cancellationToken); + } + + public T Current { get; private set; } + ITriggerHandler ITriggerHandler.Prev { get; set; } + ITriggerHandler ITriggerHandler.Next { get; set; } + + public UniTask MoveNextAsync() + { + cancellationToken.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (!called) + { + called = true; + + TaskTracker.TrackActiveTask(this, 3); + parent.AddHandler(this); + if (cancellationToken.CanBeCanceled) + { + registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this); + } + } + + return new UniTask(this, completionSource.Version); + } + + public UniTask DisposeAsync() + { + if (!isDisposed) + { + isDisposed = true; + TaskTracker.RemoveTracking(this); + registration.Dispose(); + parent.RemoveHandler(this); + } + + return default; + } + } + + class AwakeMonitor : IPlayerLoopItem + { + readonly AsyncTriggerBase trigger; + + public AwakeMonitor(AsyncTriggerBase trigger) + { + this.trigger = trigger; + } + + public bool MoveNext() + { + if (trigger.calledAwake) return false; + if (trigger == null) + { + trigger.OnDestroy(); + return false; + } + return true; + } + } + } + + public interface IAsyncOneShotTrigger + { + UniTask OneShotAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOneShotTrigger + { + UniTask IAsyncOneShotTrigger.OneShotAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)this, core.Version); + } + } + + public sealed partial class AsyncTriggerHandler : IUniTaskSource, ITriggerHandler, IDisposable + { + static Action cancellationCallback = CancellationCallback; + + readonly AsyncTriggerBase trigger; + + CancellationToken cancellationToken; + CancellationTokenRegistration registration; + bool isDisposed; + bool callOnce; + + UniTaskCompletionSourceCore core; + + internal CancellationToken CancellationToken => cancellationToken; + + ITriggerHandler ITriggerHandler.Prev { get; set; } + ITriggerHandler ITriggerHandler.Next { get; set; } + + internal AsyncTriggerHandler(AsyncTriggerBase trigger, bool callOnce) + { + if (cancellationToken.IsCancellationRequested) + { + isDisposed = true; + return; + } + + this.trigger = trigger; + this.cancellationToken = default; + this.registration = default; + this.callOnce = callOnce; + + trigger.AddHandler(this); + + TaskTracker.TrackActiveTask(this, 3); + } + + internal AsyncTriggerHandler(AsyncTriggerBase trigger, CancellationToken cancellationToken, bool callOnce) + { + if (cancellationToken.IsCancellationRequested) + { + isDisposed = true; + return; + } + + this.trigger = trigger; + this.cancellationToken = cancellationToken; + this.callOnce = callOnce; + + trigger.AddHandler(this); + + if (cancellationToken.CanBeCanceled) + { + registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this); + } + + TaskTracker.TrackActiveTask(this, 3); + } + + static void CancellationCallback(object state) + { + var self = (AsyncTriggerHandler)state; + self.Dispose(); + + self.core.TrySetCanceled(self.cancellationToken); + } + + public void Dispose() + { + if (!isDisposed) + { + isDisposed = true; + TaskTracker.RemoveTracking(this); + registration.Dispose(); + trigger.RemoveHandler(this); + } + } + + T IUniTaskSource.GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + if (callOnce) + { + Dispose(); + } + } + } + + void ITriggerHandler.OnNext(T value) + { + core.TrySetResult(value); + } + + void ITriggerHandler.OnCanceled(CancellationToken cancellationToken) + { + core.TrySetCanceled(cancellationToken); + } + + void ITriggerHandler.OnCompleted() + { + core.TrySetCanceled(CancellationToken.None); + } + + void ITriggerHandler.OnError(Exception ex) + { + core.TrySetException(ex); + } + + void IUniTaskSource.GetResult(short token) + { + ((IUniTaskSource)this).GetResult(token); + } + + UniTaskStatus IUniTaskSource.GetStatus(short token) + { + return core.GetStatus(token); + } + + UniTaskStatus IUniTaskSource.UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncTriggerBase.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncTriggerBase.cs.meta new file mode 100644 index 0000000..e101ea2 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncTriggerBase.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2c0c2bcee832c6641b25949c412f020f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncTriggerExtensions.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncTriggerExtensions.cs new file mode 100644 index 0000000..49e7f0b --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncTriggerExtensions.cs @@ -0,0 +1,85 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System.Threading; +using UnityEngine; +using Cysharp.Threading.Tasks.Triggers; + +namespace Cysharp.Threading.Tasks +{ + public static class UniTaskCancellationExtensions + { + /// This CancellationToken is canceled when the MonoBehaviour will be destroyed. + public static CancellationToken GetCancellationTokenOnDestroy(this GameObject gameObject) + { + return gameObject.GetAsyncDestroyTrigger().CancellationToken; + } + + /// This CancellationToken is canceled when the MonoBehaviour will be destroyed. + public static CancellationToken GetCancellationTokenOnDestroy(this Component component) + { + return component.GetAsyncDestroyTrigger().CancellationToken; + } + } +} + +namespace Cysharp.Threading.Tasks.Triggers +{ + public static partial class AsyncTriggerExtensions + { + // Util. + + static T GetOrAddComponent(GameObject gameObject) + where T : Component + { +#if UNITY_2019_2_OR_NEWER + if (!gameObject.TryGetComponent(out var component)) + { + component = gameObject.AddComponent(); + } +#else + var component = gameObject.GetComponent(); + if (component == null) + { + component = gameObject.AddComponent(); + } +#endif + + return component; + } + + // Special for single operation. + + /// This function is called when the MonoBehaviour will be destroyed. + public static UniTask OnDestroyAsync(this GameObject gameObject) + { + return gameObject.GetAsyncDestroyTrigger().OnDestroyAsync(); + } + + /// This function is called when the MonoBehaviour will be destroyed. + public static UniTask OnDestroyAsync(this Component component) + { + return component.GetAsyncDestroyTrigger().OnDestroyAsync(); + } + + public static UniTask StartAsync(this GameObject gameObject) + { + return gameObject.GetAsyncStartTrigger().StartAsync(); + } + + public static UniTask StartAsync(this Component component) + { + return component.GetAsyncStartTrigger().StartAsync(); + } + + public static UniTask AwakeAsync(this GameObject gameObject) + { + return gameObject.GetAsyncAwakeTrigger().AwakeAsync(); + } + + public static UniTask AwakeAsync(this Component component) + { + return component.GetAsyncAwakeTrigger().AwakeAsync(); + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncTriggerExtensions.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncTriggerExtensions.cs.meta new file mode 100644 index 0000000..348783d --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/AsyncTriggerExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 59b61dbea1562a84fb7a38ae0a0a0f88 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/MonoBehaviourMessagesTriggers.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/MonoBehaviourMessagesTriggers.cs new file mode 100644 index 0000000..6ef5014 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/MonoBehaviourMessagesTriggers.cs @@ -0,0 +1,4457 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System.Threading; +using UnityEngine; +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT +using UnityEngine.EventSystems; +#endif + +namespace Cysharp.Threading.Tasks.Triggers +{ +#region FixedUpdate + + public interface IAsyncFixedUpdateHandler + { + UniTask FixedUpdateAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncFixedUpdateHandler + { + UniTask IAsyncFixedUpdateHandler.FixedUpdateAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncFixedUpdateTrigger GetAsyncFixedUpdateTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncFixedUpdateTrigger GetAsyncFixedUpdateTrigger(this Component component) + { + return component.gameObject.GetAsyncFixedUpdateTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncFixedUpdateTrigger : AsyncTriggerBase + { + void FixedUpdate() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncFixedUpdateHandler GetFixedUpdateAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncFixedUpdateHandler GetFixedUpdateAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask FixedUpdateAsync() + { + return ((IAsyncFixedUpdateHandler)new AsyncTriggerHandler(this, true)).FixedUpdateAsync(); + } + + public UniTask FixedUpdateAsync(CancellationToken cancellationToken) + { + return ((IAsyncFixedUpdateHandler)new AsyncTriggerHandler(this, cancellationToken, true)).FixedUpdateAsync(); + } + } +#endregion + +#region LateUpdate + + public interface IAsyncLateUpdateHandler + { + UniTask LateUpdateAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncLateUpdateHandler + { + UniTask IAsyncLateUpdateHandler.LateUpdateAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncLateUpdateTrigger GetAsyncLateUpdateTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncLateUpdateTrigger GetAsyncLateUpdateTrigger(this Component component) + { + return component.gameObject.GetAsyncLateUpdateTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncLateUpdateTrigger : AsyncTriggerBase + { + void LateUpdate() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncLateUpdateHandler GetLateUpdateAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncLateUpdateHandler GetLateUpdateAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask LateUpdateAsync() + { + return ((IAsyncLateUpdateHandler)new AsyncTriggerHandler(this, true)).LateUpdateAsync(); + } + + public UniTask LateUpdateAsync(CancellationToken cancellationToken) + { + return ((IAsyncLateUpdateHandler)new AsyncTriggerHandler(this, cancellationToken, true)).LateUpdateAsync(); + } + } +#endregion + +#region AnimatorIK + + public interface IAsyncOnAnimatorIKHandler + { + UniTask OnAnimatorIKAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnAnimatorIKHandler + { + UniTask IAsyncOnAnimatorIKHandler.OnAnimatorIKAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncAnimatorIKTrigger GetAsyncAnimatorIKTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncAnimatorIKTrigger GetAsyncAnimatorIKTrigger(this Component component) + { + return component.gameObject.GetAsyncAnimatorIKTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncAnimatorIKTrigger : AsyncTriggerBase + { + void OnAnimatorIK(int layerIndex) + { + RaiseEvent((layerIndex)); + } + + public IAsyncOnAnimatorIKHandler GetOnAnimatorIKAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnAnimatorIKHandler GetOnAnimatorIKAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnAnimatorIKAsync() + { + return ((IAsyncOnAnimatorIKHandler)new AsyncTriggerHandler(this, true)).OnAnimatorIKAsync(); + } + + public UniTask OnAnimatorIKAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnAnimatorIKHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnAnimatorIKAsync(); + } + } +#endregion + +#region AnimatorMove + + public interface IAsyncOnAnimatorMoveHandler + { + UniTask OnAnimatorMoveAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnAnimatorMoveHandler + { + UniTask IAsyncOnAnimatorMoveHandler.OnAnimatorMoveAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncAnimatorMoveTrigger GetAsyncAnimatorMoveTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncAnimatorMoveTrigger GetAsyncAnimatorMoveTrigger(this Component component) + { + return component.gameObject.GetAsyncAnimatorMoveTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncAnimatorMoveTrigger : AsyncTriggerBase + { + void OnAnimatorMove() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnAnimatorMoveHandler GetOnAnimatorMoveAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnAnimatorMoveHandler GetOnAnimatorMoveAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnAnimatorMoveAsync() + { + return ((IAsyncOnAnimatorMoveHandler)new AsyncTriggerHandler(this, true)).OnAnimatorMoveAsync(); + } + + public UniTask OnAnimatorMoveAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnAnimatorMoveHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnAnimatorMoveAsync(); + } + } +#endregion + +#region ApplicationFocus + + public interface IAsyncOnApplicationFocusHandler + { + UniTask OnApplicationFocusAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnApplicationFocusHandler + { + UniTask IAsyncOnApplicationFocusHandler.OnApplicationFocusAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncApplicationFocusTrigger GetAsyncApplicationFocusTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncApplicationFocusTrigger GetAsyncApplicationFocusTrigger(this Component component) + { + return component.gameObject.GetAsyncApplicationFocusTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncApplicationFocusTrigger : AsyncTriggerBase + { + void OnApplicationFocus(bool hasFocus) + { + RaiseEvent((hasFocus)); + } + + public IAsyncOnApplicationFocusHandler GetOnApplicationFocusAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnApplicationFocusHandler GetOnApplicationFocusAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnApplicationFocusAsync() + { + return ((IAsyncOnApplicationFocusHandler)new AsyncTriggerHandler(this, true)).OnApplicationFocusAsync(); + } + + public UniTask OnApplicationFocusAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnApplicationFocusHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnApplicationFocusAsync(); + } + } +#endregion + +#region ApplicationPause + + public interface IAsyncOnApplicationPauseHandler + { + UniTask OnApplicationPauseAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnApplicationPauseHandler + { + UniTask IAsyncOnApplicationPauseHandler.OnApplicationPauseAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncApplicationPauseTrigger GetAsyncApplicationPauseTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncApplicationPauseTrigger GetAsyncApplicationPauseTrigger(this Component component) + { + return component.gameObject.GetAsyncApplicationPauseTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncApplicationPauseTrigger : AsyncTriggerBase + { + void OnApplicationPause(bool pauseStatus) + { + RaiseEvent((pauseStatus)); + } + + public IAsyncOnApplicationPauseHandler GetOnApplicationPauseAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnApplicationPauseHandler GetOnApplicationPauseAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnApplicationPauseAsync() + { + return ((IAsyncOnApplicationPauseHandler)new AsyncTriggerHandler(this, true)).OnApplicationPauseAsync(); + } + + public UniTask OnApplicationPauseAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnApplicationPauseHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnApplicationPauseAsync(); + } + } +#endregion + +#region ApplicationQuit + + public interface IAsyncOnApplicationQuitHandler + { + UniTask OnApplicationQuitAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnApplicationQuitHandler + { + UniTask IAsyncOnApplicationQuitHandler.OnApplicationQuitAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncApplicationQuitTrigger GetAsyncApplicationQuitTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncApplicationQuitTrigger GetAsyncApplicationQuitTrigger(this Component component) + { + return component.gameObject.GetAsyncApplicationQuitTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncApplicationQuitTrigger : AsyncTriggerBase + { + void OnApplicationQuit() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnApplicationQuitHandler GetOnApplicationQuitAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnApplicationQuitHandler GetOnApplicationQuitAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnApplicationQuitAsync() + { + return ((IAsyncOnApplicationQuitHandler)new AsyncTriggerHandler(this, true)).OnApplicationQuitAsync(); + } + + public UniTask OnApplicationQuitAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnApplicationQuitHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnApplicationQuitAsync(); + } + } +#endregion + +#region AudioFilterRead + + public interface IAsyncOnAudioFilterReadHandler + { + UniTask<(float[] data, int channels)> OnAudioFilterReadAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnAudioFilterReadHandler + { + UniTask<(float[] data, int channels)> IAsyncOnAudioFilterReadHandler.OnAudioFilterReadAsync() + { + core.Reset(); + return new UniTask<(float[] data, int channels)>((IUniTaskSource<(float[] data, int channels)>)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncAudioFilterReadTrigger GetAsyncAudioFilterReadTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncAudioFilterReadTrigger GetAsyncAudioFilterReadTrigger(this Component component) + { + return component.gameObject.GetAsyncAudioFilterReadTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncAudioFilterReadTrigger : AsyncTriggerBase<(float[] data, int channels)> + { + void OnAudioFilterRead(float[] data, int channels) + { + RaiseEvent((data, channels)); + } + + public IAsyncOnAudioFilterReadHandler GetOnAudioFilterReadAsyncHandler() + { + return new AsyncTriggerHandler<(float[] data, int channels)>(this, false); + } + + public IAsyncOnAudioFilterReadHandler GetOnAudioFilterReadAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler<(float[] data, int channels)>(this, cancellationToken, false); + } + + public UniTask<(float[] data, int channels)> OnAudioFilterReadAsync() + { + return ((IAsyncOnAudioFilterReadHandler)new AsyncTriggerHandler<(float[] data, int channels)>(this, true)).OnAudioFilterReadAsync(); + } + + public UniTask<(float[] data, int channels)> OnAudioFilterReadAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnAudioFilterReadHandler)new AsyncTriggerHandler<(float[] data, int channels)>(this, cancellationToken, true)).OnAudioFilterReadAsync(); + } + } +#endregion + +#region BecameInvisible + + public interface IAsyncOnBecameInvisibleHandler + { + UniTask OnBecameInvisibleAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnBecameInvisibleHandler + { + UniTask IAsyncOnBecameInvisibleHandler.OnBecameInvisibleAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncBecameInvisibleTrigger GetAsyncBecameInvisibleTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncBecameInvisibleTrigger GetAsyncBecameInvisibleTrigger(this Component component) + { + return component.gameObject.GetAsyncBecameInvisibleTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncBecameInvisibleTrigger : AsyncTriggerBase + { + void OnBecameInvisible() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnBecameInvisibleHandler GetOnBecameInvisibleAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnBecameInvisibleHandler GetOnBecameInvisibleAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnBecameInvisibleAsync() + { + return ((IAsyncOnBecameInvisibleHandler)new AsyncTriggerHandler(this, true)).OnBecameInvisibleAsync(); + } + + public UniTask OnBecameInvisibleAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnBecameInvisibleHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnBecameInvisibleAsync(); + } + } +#endregion + +#region BecameVisible + + public interface IAsyncOnBecameVisibleHandler + { + UniTask OnBecameVisibleAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnBecameVisibleHandler + { + UniTask IAsyncOnBecameVisibleHandler.OnBecameVisibleAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncBecameVisibleTrigger GetAsyncBecameVisibleTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncBecameVisibleTrigger GetAsyncBecameVisibleTrigger(this Component component) + { + return component.gameObject.GetAsyncBecameVisibleTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncBecameVisibleTrigger : AsyncTriggerBase + { + void OnBecameVisible() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnBecameVisibleHandler GetOnBecameVisibleAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnBecameVisibleHandler GetOnBecameVisibleAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnBecameVisibleAsync() + { + return ((IAsyncOnBecameVisibleHandler)new AsyncTriggerHandler(this, true)).OnBecameVisibleAsync(); + } + + public UniTask OnBecameVisibleAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnBecameVisibleHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnBecameVisibleAsync(); + } + } +#endregion + +#region BeforeTransformParentChanged + + public interface IAsyncOnBeforeTransformParentChangedHandler + { + UniTask OnBeforeTransformParentChangedAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnBeforeTransformParentChangedHandler + { + UniTask IAsyncOnBeforeTransformParentChangedHandler.OnBeforeTransformParentChangedAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncBeforeTransformParentChangedTrigger GetAsyncBeforeTransformParentChangedTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncBeforeTransformParentChangedTrigger GetAsyncBeforeTransformParentChangedTrigger(this Component component) + { + return component.gameObject.GetAsyncBeforeTransformParentChangedTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncBeforeTransformParentChangedTrigger : AsyncTriggerBase + { + void OnBeforeTransformParentChanged() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnBeforeTransformParentChangedHandler GetOnBeforeTransformParentChangedAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnBeforeTransformParentChangedHandler GetOnBeforeTransformParentChangedAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnBeforeTransformParentChangedAsync() + { + return ((IAsyncOnBeforeTransformParentChangedHandler)new AsyncTriggerHandler(this, true)).OnBeforeTransformParentChangedAsync(); + } + + public UniTask OnBeforeTransformParentChangedAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnBeforeTransformParentChangedHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnBeforeTransformParentChangedAsync(); + } + } +#endregion + +#region OnCanvasGroupChanged + + public interface IAsyncOnCanvasGroupChangedHandler + { + UniTask OnCanvasGroupChangedAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnCanvasGroupChangedHandler + { + UniTask IAsyncOnCanvasGroupChangedHandler.OnCanvasGroupChangedAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncOnCanvasGroupChangedTrigger GetAsyncOnCanvasGroupChangedTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncOnCanvasGroupChangedTrigger GetAsyncOnCanvasGroupChangedTrigger(this Component component) + { + return component.gameObject.GetAsyncOnCanvasGroupChangedTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncOnCanvasGroupChangedTrigger : AsyncTriggerBase + { + void OnCanvasGroupChanged() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnCanvasGroupChangedHandler GetOnCanvasGroupChangedAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnCanvasGroupChangedHandler GetOnCanvasGroupChangedAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnCanvasGroupChangedAsync() + { + return ((IAsyncOnCanvasGroupChangedHandler)new AsyncTriggerHandler(this, true)).OnCanvasGroupChangedAsync(); + } + + public UniTask OnCanvasGroupChangedAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnCanvasGroupChangedHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnCanvasGroupChangedAsync(); + } + } +#endregion + +#region CollisionEnter +#if !UNITY_2019_1_OR_NEWER || UNITASK_PHYSICS_SUPPORT + + public interface IAsyncOnCollisionEnterHandler + { + UniTask OnCollisionEnterAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnCollisionEnterHandler + { + UniTask IAsyncOnCollisionEnterHandler.OnCollisionEnterAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncCollisionEnterTrigger GetAsyncCollisionEnterTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncCollisionEnterTrigger GetAsyncCollisionEnterTrigger(this Component component) + { + return component.gameObject.GetAsyncCollisionEnterTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncCollisionEnterTrigger : AsyncTriggerBase + { + void OnCollisionEnter(Collision coll) + { + RaiseEvent((coll)); + } + + public IAsyncOnCollisionEnterHandler GetOnCollisionEnterAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnCollisionEnterHandler GetOnCollisionEnterAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnCollisionEnterAsync() + { + return ((IAsyncOnCollisionEnterHandler)new AsyncTriggerHandler(this, true)).OnCollisionEnterAsync(); + } + + public UniTask OnCollisionEnterAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnCollisionEnterHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnCollisionEnterAsync(); + } + } +#endif +#endregion + +#region CollisionEnter2D +#if !UNITY_2019_1_OR_NEWER || UNITASK_PHYSICS2D_SUPPORT + + public interface IAsyncOnCollisionEnter2DHandler + { + UniTask OnCollisionEnter2DAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnCollisionEnter2DHandler + { + UniTask IAsyncOnCollisionEnter2DHandler.OnCollisionEnter2DAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncCollisionEnter2DTrigger GetAsyncCollisionEnter2DTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncCollisionEnter2DTrigger GetAsyncCollisionEnter2DTrigger(this Component component) + { + return component.gameObject.GetAsyncCollisionEnter2DTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncCollisionEnter2DTrigger : AsyncTriggerBase + { + void OnCollisionEnter2D(Collision2D coll) + { + RaiseEvent((coll)); + } + + public IAsyncOnCollisionEnter2DHandler GetOnCollisionEnter2DAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnCollisionEnter2DHandler GetOnCollisionEnter2DAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnCollisionEnter2DAsync() + { + return ((IAsyncOnCollisionEnter2DHandler)new AsyncTriggerHandler(this, true)).OnCollisionEnter2DAsync(); + } + + public UniTask OnCollisionEnter2DAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnCollisionEnter2DHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnCollisionEnter2DAsync(); + } + } +#endif +#endregion + +#region CollisionExit +#if !UNITY_2019_1_OR_NEWER || UNITASK_PHYSICS_SUPPORT + + public interface IAsyncOnCollisionExitHandler + { + UniTask OnCollisionExitAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnCollisionExitHandler + { + UniTask IAsyncOnCollisionExitHandler.OnCollisionExitAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncCollisionExitTrigger GetAsyncCollisionExitTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncCollisionExitTrigger GetAsyncCollisionExitTrigger(this Component component) + { + return component.gameObject.GetAsyncCollisionExitTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncCollisionExitTrigger : AsyncTriggerBase + { + void OnCollisionExit(Collision coll) + { + RaiseEvent((coll)); + } + + public IAsyncOnCollisionExitHandler GetOnCollisionExitAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnCollisionExitHandler GetOnCollisionExitAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnCollisionExitAsync() + { + return ((IAsyncOnCollisionExitHandler)new AsyncTriggerHandler(this, true)).OnCollisionExitAsync(); + } + + public UniTask OnCollisionExitAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnCollisionExitHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnCollisionExitAsync(); + } + } +#endif +#endregion + +#region CollisionExit2D +#if !UNITY_2019_1_OR_NEWER || UNITASK_PHYSICS2D_SUPPORT + + public interface IAsyncOnCollisionExit2DHandler + { + UniTask OnCollisionExit2DAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnCollisionExit2DHandler + { + UniTask IAsyncOnCollisionExit2DHandler.OnCollisionExit2DAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncCollisionExit2DTrigger GetAsyncCollisionExit2DTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncCollisionExit2DTrigger GetAsyncCollisionExit2DTrigger(this Component component) + { + return component.gameObject.GetAsyncCollisionExit2DTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncCollisionExit2DTrigger : AsyncTriggerBase + { + void OnCollisionExit2D(Collision2D coll) + { + RaiseEvent((coll)); + } + + public IAsyncOnCollisionExit2DHandler GetOnCollisionExit2DAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnCollisionExit2DHandler GetOnCollisionExit2DAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnCollisionExit2DAsync() + { + return ((IAsyncOnCollisionExit2DHandler)new AsyncTriggerHandler(this, true)).OnCollisionExit2DAsync(); + } + + public UniTask OnCollisionExit2DAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnCollisionExit2DHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnCollisionExit2DAsync(); + } + } +#endif +#endregion + +#region CollisionStay +#if !UNITY_2019_1_OR_NEWER || UNITASK_PHYSICS_SUPPORT + + public interface IAsyncOnCollisionStayHandler + { + UniTask OnCollisionStayAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnCollisionStayHandler + { + UniTask IAsyncOnCollisionStayHandler.OnCollisionStayAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncCollisionStayTrigger GetAsyncCollisionStayTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncCollisionStayTrigger GetAsyncCollisionStayTrigger(this Component component) + { + return component.gameObject.GetAsyncCollisionStayTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncCollisionStayTrigger : AsyncTriggerBase + { + void OnCollisionStay(Collision coll) + { + RaiseEvent((coll)); + } + + public IAsyncOnCollisionStayHandler GetOnCollisionStayAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnCollisionStayHandler GetOnCollisionStayAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnCollisionStayAsync() + { + return ((IAsyncOnCollisionStayHandler)new AsyncTriggerHandler(this, true)).OnCollisionStayAsync(); + } + + public UniTask OnCollisionStayAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnCollisionStayHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnCollisionStayAsync(); + } + } +#endif +#endregion + +#region CollisionStay2D +#if !UNITY_2019_1_OR_NEWER || UNITASK_PHYSICS2D_SUPPORT + + public interface IAsyncOnCollisionStay2DHandler + { + UniTask OnCollisionStay2DAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnCollisionStay2DHandler + { + UniTask IAsyncOnCollisionStay2DHandler.OnCollisionStay2DAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncCollisionStay2DTrigger GetAsyncCollisionStay2DTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncCollisionStay2DTrigger GetAsyncCollisionStay2DTrigger(this Component component) + { + return component.gameObject.GetAsyncCollisionStay2DTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncCollisionStay2DTrigger : AsyncTriggerBase + { + void OnCollisionStay2D(Collision2D coll) + { + RaiseEvent((coll)); + } + + public IAsyncOnCollisionStay2DHandler GetOnCollisionStay2DAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnCollisionStay2DHandler GetOnCollisionStay2DAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnCollisionStay2DAsync() + { + return ((IAsyncOnCollisionStay2DHandler)new AsyncTriggerHandler(this, true)).OnCollisionStay2DAsync(); + } + + public UniTask OnCollisionStay2DAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnCollisionStay2DHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnCollisionStay2DAsync(); + } + } +#endif +#endregion + +#region ControllerColliderHit +#if !UNITY_2019_1_OR_NEWER || UNITASK_PHYSICS_SUPPORT + + public interface IAsyncOnControllerColliderHitHandler + { + UniTask OnControllerColliderHitAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnControllerColliderHitHandler + { + UniTask IAsyncOnControllerColliderHitHandler.OnControllerColliderHitAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncControllerColliderHitTrigger GetAsyncControllerColliderHitTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncControllerColliderHitTrigger GetAsyncControllerColliderHitTrigger(this Component component) + { + return component.gameObject.GetAsyncControllerColliderHitTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncControllerColliderHitTrigger : AsyncTriggerBase + { + void OnControllerColliderHit(ControllerColliderHit hit) + { + RaiseEvent((hit)); + } + + public IAsyncOnControllerColliderHitHandler GetOnControllerColliderHitAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnControllerColliderHitHandler GetOnControllerColliderHitAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnControllerColliderHitAsync() + { + return ((IAsyncOnControllerColliderHitHandler)new AsyncTriggerHandler(this, true)).OnControllerColliderHitAsync(); + } + + public UniTask OnControllerColliderHitAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnControllerColliderHitHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnControllerColliderHitAsync(); + } + } +#endif +#endregion + +#region Disable + + public interface IAsyncOnDisableHandler + { + UniTask OnDisableAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnDisableHandler + { + UniTask IAsyncOnDisableHandler.OnDisableAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncDisableTrigger GetAsyncDisableTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncDisableTrigger GetAsyncDisableTrigger(this Component component) + { + return component.gameObject.GetAsyncDisableTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncDisableTrigger : AsyncTriggerBase + { + void OnDisable() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnDisableHandler GetOnDisableAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnDisableHandler GetOnDisableAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnDisableAsync() + { + return ((IAsyncOnDisableHandler)new AsyncTriggerHandler(this, true)).OnDisableAsync(); + } + + public UniTask OnDisableAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnDisableHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnDisableAsync(); + } + } +#endregion + +#region DrawGizmos + + public interface IAsyncOnDrawGizmosHandler + { + UniTask OnDrawGizmosAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnDrawGizmosHandler + { + UniTask IAsyncOnDrawGizmosHandler.OnDrawGizmosAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncDrawGizmosTrigger GetAsyncDrawGizmosTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncDrawGizmosTrigger GetAsyncDrawGizmosTrigger(this Component component) + { + return component.gameObject.GetAsyncDrawGizmosTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncDrawGizmosTrigger : AsyncTriggerBase + { + void OnDrawGizmos() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnDrawGizmosHandler GetOnDrawGizmosAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnDrawGizmosHandler GetOnDrawGizmosAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnDrawGizmosAsync() + { + return ((IAsyncOnDrawGizmosHandler)new AsyncTriggerHandler(this, true)).OnDrawGizmosAsync(); + } + + public UniTask OnDrawGizmosAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnDrawGizmosHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnDrawGizmosAsync(); + } + } +#endregion + +#region DrawGizmosSelected + + public interface IAsyncOnDrawGizmosSelectedHandler + { + UniTask OnDrawGizmosSelectedAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnDrawGizmosSelectedHandler + { + UniTask IAsyncOnDrawGizmosSelectedHandler.OnDrawGizmosSelectedAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncDrawGizmosSelectedTrigger GetAsyncDrawGizmosSelectedTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncDrawGizmosSelectedTrigger GetAsyncDrawGizmosSelectedTrigger(this Component component) + { + return component.gameObject.GetAsyncDrawGizmosSelectedTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncDrawGizmosSelectedTrigger : AsyncTriggerBase + { + void OnDrawGizmosSelected() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnDrawGizmosSelectedHandler GetOnDrawGizmosSelectedAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnDrawGizmosSelectedHandler GetOnDrawGizmosSelectedAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnDrawGizmosSelectedAsync() + { + return ((IAsyncOnDrawGizmosSelectedHandler)new AsyncTriggerHandler(this, true)).OnDrawGizmosSelectedAsync(); + } + + public UniTask OnDrawGizmosSelectedAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnDrawGizmosSelectedHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnDrawGizmosSelectedAsync(); + } + } +#endregion + +#region Enable + + public interface IAsyncOnEnableHandler + { + UniTask OnEnableAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnEnableHandler + { + UniTask IAsyncOnEnableHandler.OnEnableAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncEnableTrigger GetAsyncEnableTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncEnableTrigger GetAsyncEnableTrigger(this Component component) + { + return component.gameObject.GetAsyncEnableTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncEnableTrigger : AsyncTriggerBase + { + void OnEnable() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnEnableHandler GetOnEnableAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnEnableHandler GetOnEnableAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnEnableAsync() + { + return ((IAsyncOnEnableHandler)new AsyncTriggerHandler(this, true)).OnEnableAsync(); + } + + public UniTask OnEnableAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnEnableHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnEnableAsync(); + } + } +#endregion + +#region GUI + + public interface IAsyncOnGUIHandler + { + UniTask OnGUIAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnGUIHandler + { + UniTask IAsyncOnGUIHandler.OnGUIAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncGUITrigger GetAsyncGUITrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncGUITrigger GetAsyncGUITrigger(this Component component) + { + return component.gameObject.GetAsyncGUITrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncGUITrigger : AsyncTriggerBase + { + void OnGUI() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnGUIHandler GetOnGUIAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnGUIHandler GetOnGUIAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnGUIAsync() + { + return ((IAsyncOnGUIHandler)new AsyncTriggerHandler(this, true)).OnGUIAsync(); + } + + public UniTask OnGUIAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnGUIHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnGUIAsync(); + } + } +#endregion + +#region JointBreak +#if !UNITY_2019_1_OR_NEWER || UNITASK_PHYSICS_SUPPORT + + public interface IAsyncOnJointBreakHandler + { + UniTask OnJointBreakAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnJointBreakHandler + { + UniTask IAsyncOnJointBreakHandler.OnJointBreakAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncJointBreakTrigger GetAsyncJointBreakTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncJointBreakTrigger GetAsyncJointBreakTrigger(this Component component) + { + return component.gameObject.GetAsyncJointBreakTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncJointBreakTrigger : AsyncTriggerBase + { + void OnJointBreak(float breakForce) + { + RaiseEvent((breakForce)); + } + + public IAsyncOnJointBreakHandler GetOnJointBreakAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnJointBreakHandler GetOnJointBreakAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnJointBreakAsync() + { + return ((IAsyncOnJointBreakHandler)new AsyncTriggerHandler(this, true)).OnJointBreakAsync(); + } + + public UniTask OnJointBreakAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnJointBreakHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnJointBreakAsync(); + } + } +#endif +#endregion + +#region JointBreak2D +#if !UNITY_2019_1_OR_NEWER || UNITASK_PHYSICS2D_SUPPORT + + public interface IAsyncOnJointBreak2DHandler + { + UniTask OnJointBreak2DAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnJointBreak2DHandler + { + UniTask IAsyncOnJointBreak2DHandler.OnJointBreak2DAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncJointBreak2DTrigger GetAsyncJointBreak2DTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncJointBreak2DTrigger GetAsyncJointBreak2DTrigger(this Component component) + { + return component.gameObject.GetAsyncJointBreak2DTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncJointBreak2DTrigger : AsyncTriggerBase + { + void OnJointBreak2D(Joint2D brokenJoint) + { + RaiseEvent((brokenJoint)); + } + + public IAsyncOnJointBreak2DHandler GetOnJointBreak2DAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnJointBreak2DHandler GetOnJointBreak2DAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnJointBreak2DAsync() + { + return ((IAsyncOnJointBreak2DHandler)new AsyncTriggerHandler(this, true)).OnJointBreak2DAsync(); + } + + public UniTask OnJointBreak2DAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnJointBreak2DHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnJointBreak2DAsync(); + } + } +#endif +#endregion + +#region MouseDown +#if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO) + + public interface IAsyncOnMouseDownHandler + { + UniTask OnMouseDownAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnMouseDownHandler + { + UniTask IAsyncOnMouseDownHandler.OnMouseDownAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncMouseDownTrigger GetAsyncMouseDownTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncMouseDownTrigger GetAsyncMouseDownTrigger(this Component component) + { + return component.gameObject.GetAsyncMouseDownTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncMouseDownTrigger : AsyncTriggerBase + { + void OnMouseDown() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnMouseDownHandler GetOnMouseDownAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnMouseDownHandler GetOnMouseDownAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnMouseDownAsync() + { + return ((IAsyncOnMouseDownHandler)new AsyncTriggerHandler(this, true)).OnMouseDownAsync(); + } + + public UniTask OnMouseDownAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnMouseDownHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnMouseDownAsync(); + } + } +#endif +#endregion + +#region MouseDrag +#if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO) + + public interface IAsyncOnMouseDragHandler + { + UniTask OnMouseDragAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnMouseDragHandler + { + UniTask IAsyncOnMouseDragHandler.OnMouseDragAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncMouseDragTrigger GetAsyncMouseDragTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncMouseDragTrigger GetAsyncMouseDragTrigger(this Component component) + { + return component.gameObject.GetAsyncMouseDragTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncMouseDragTrigger : AsyncTriggerBase + { + void OnMouseDrag() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnMouseDragHandler GetOnMouseDragAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnMouseDragHandler GetOnMouseDragAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnMouseDragAsync() + { + return ((IAsyncOnMouseDragHandler)new AsyncTriggerHandler(this, true)).OnMouseDragAsync(); + } + + public UniTask OnMouseDragAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnMouseDragHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnMouseDragAsync(); + } + } +#endif +#endregion + +#region MouseEnter +#if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO) + + public interface IAsyncOnMouseEnterHandler + { + UniTask OnMouseEnterAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnMouseEnterHandler + { + UniTask IAsyncOnMouseEnterHandler.OnMouseEnterAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncMouseEnterTrigger GetAsyncMouseEnterTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncMouseEnterTrigger GetAsyncMouseEnterTrigger(this Component component) + { + return component.gameObject.GetAsyncMouseEnterTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncMouseEnterTrigger : AsyncTriggerBase + { + void OnMouseEnter() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnMouseEnterHandler GetOnMouseEnterAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnMouseEnterHandler GetOnMouseEnterAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnMouseEnterAsync() + { + return ((IAsyncOnMouseEnterHandler)new AsyncTriggerHandler(this, true)).OnMouseEnterAsync(); + } + + public UniTask OnMouseEnterAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnMouseEnterHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnMouseEnterAsync(); + } + } +#endif +#endregion + +#region MouseExit +#if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO) + + public interface IAsyncOnMouseExitHandler + { + UniTask OnMouseExitAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnMouseExitHandler + { + UniTask IAsyncOnMouseExitHandler.OnMouseExitAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncMouseExitTrigger GetAsyncMouseExitTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncMouseExitTrigger GetAsyncMouseExitTrigger(this Component component) + { + return component.gameObject.GetAsyncMouseExitTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncMouseExitTrigger : AsyncTriggerBase + { + void OnMouseExit() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnMouseExitHandler GetOnMouseExitAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnMouseExitHandler GetOnMouseExitAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnMouseExitAsync() + { + return ((IAsyncOnMouseExitHandler)new AsyncTriggerHandler(this, true)).OnMouseExitAsync(); + } + + public UniTask OnMouseExitAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnMouseExitHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnMouseExitAsync(); + } + } +#endif +#endregion + +#region MouseOver +#if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO) + + public interface IAsyncOnMouseOverHandler + { + UniTask OnMouseOverAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnMouseOverHandler + { + UniTask IAsyncOnMouseOverHandler.OnMouseOverAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncMouseOverTrigger GetAsyncMouseOverTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncMouseOverTrigger GetAsyncMouseOverTrigger(this Component component) + { + return component.gameObject.GetAsyncMouseOverTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncMouseOverTrigger : AsyncTriggerBase + { + void OnMouseOver() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnMouseOverHandler GetOnMouseOverAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnMouseOverHandler GetOnMouseOverAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnMouseOverAsync() + { + return ((IAsyncOnMouseOverHandler)new AsyncTriggerHandler(this, true)).OnMouseOverAsync(); + } + + public UniTask OnMouseOverAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnMouseOverHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnMouseOverAsync(); + } + } +#endif +#endregion + +#region MouseUp +#if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO) + + public interface IAsyncOnMouseUpHandler + { + UniTask OnMouseUpAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnMouseUpHandler + { + UniTask IAsyncOnMouseUpHandler.OnMouseUpAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncMouseUpTrigger GetAsyncMouseUpTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncMouseUpTrigger GetAsyncMouseUpTrigger(this Component component) + { + return component.gameObject.GetAsyncMouseUpTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncMouseUpTrigger : AsyncTriggerBase + { + void OnMouseUp() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnMouseUpHandler GetOnMouseUpAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnMouseUpHandler GetOnMouseUpAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnMouseUpAsync() + { + return ((IAsyncOnMouseUpHandler)new AsyncTriggerHandler(this, true)).OnMouseUpAsync(); + } + + public UniTask OnMouseUpAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnMouseUpHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnMouseUpAsync(); + } + } +#endif +#endregion + +#region MouseUpAsButton +#if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO) + + public interface IAsyncOnMouseUpAsButtonHandler + { + UniTask OnMouseUpAsButtonAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnMouseUpAsButtonHandler + { + UniTask IAsyncOnMouseUpAsButtonHandler.OnMouseUpAsButtonAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncMouseUpAsButtonTrigger GetAsyncMouseUpAsButtonTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncMouseUpAsButtonTrigger GetAsyncMouseUpAsButtonTrigger(this Component component) + { + return component.gameObject.GetAsyncMouseUpAsButtonTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncMouseUpAsButtonTrigger : AsyncTriggerBase + { + void OnMouseUpAsButton() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnMouseUpAsButtonHandler GetOnMouseUpAsButtonAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnMouseUpAsButtonHandler GetOnMouseUpAsButtonAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnMouseUpAsButtonAsync() + { + return ((IAsyncOnMouseUpAsButtonHandler)new AsyncTriggerHandler(this, true)).OnMouseUpAsButtonAsync(); + } + + public UniTask OnMouseUpAsButtonAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnMouseUpAsButtonHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnMouseUpAsButtonAsync(); + } + } +#endif +#endregion + +#region ParticleCollision + + public interface IAsyncOnParticleCollisionHandler + { + UniTask OnParticleCollisionAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnParticleCollisionHandler + { + UniTask IAsyncOnParticleCollisionHandler.OnParticleCollisionAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncParticleCollisionTrigger GetAsyncParticleCollisionTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncParticleCollisionTrigger GetAsyncParticleCollisionTrigger(this Component component) + { + return component.gameObject.GetAsyncParticleCollisionTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncParticleCollisionTrigger : AsyncTriggerBase + { + void OnParticleCollision(GameObject other) + { + RaiseEvent((other)); + } + + public IAsyncOnParticleCollisionHandler GetOnParticleCollisionAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnParticleCollisionHandler GetOnParticleCollisionAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnParticleCollisionAsync() + { + return ((IAsyncOnParticleCollisionHandler)new AsyncTriggerHandler(this, true)).OnParticleCollisionAsync(); + } + + public UniTask OnParticleCollisionAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnParticleCollisionHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnParticleCollisionAsync(); + } + } +#endregion + +#region ParticleSystemStopped + + public interface IAsyncOnParticleSystemStoppedHandler + { + UniTask OnParticleSystemStoppedAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnParticleSystemStoppedHandler + { + UniTask IAsyncOnParticleSystemStoppedHandler.OnParticleSystemStoppedAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncParticleSystemStoppedTrigger GetAsyncParticleSystemStoppedTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncParticleSystemStoppedTrigger GetAsyncParticleSystemStoppedTrigger(this Component component) + { + return component.gameObject.GetAsyncParticleSystemStoppedTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncParticleSystemStoppedTrigger : AsyncTriggerBase + { + void OnParticleSystemStopped() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnParticleSystemStoppedHandler GetOnParticleSystemStoppedAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnParticleSystemStoppedHandler GetOnParticleSystemStoppedAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnParticleSystemStoppedAsync() + { + return ((IAsyncOnParticleSystemStoppedHandler)new AsyncTriggerHandler(this, true)).OnParticleSystemStoppedAsync(); + } + + public UniTask OnParticleSystemStoppedAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnParticleSystemStoppedHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnParticleSystemStoppedAsync(); + } + } +#endregion + +#region ParticleTrigger + + public interface IAsyncOnParticleTriggerHandler + { + UniTask OnParticleTriggerAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnParticleTriggerHandler + { + UniTask IAsyncOnParticleTriggerHandler.OnParticleTriggerAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncParticleTriggerTrigger GetAsyncParticleTriggerTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncParticleTriggerTrigger GetAsyncParticleTriggerTrigger(this Component component) + { + return component.gameObject.GetAsyncParticleTriggerTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncParticleTriggerTrigger : AsyncTriggerBase + { + void OnParticleTrigger() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnParticleTriggerHandler GetOnParticleTriggerAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnParticleTriggerHandler GetOnParticleTriggerAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnParticleTriggerAsync() + { + return ((IAsyncOnParticleTriggerHandler)new AsyncTriggerHandler(this, true)).OnParticleTriggerAsync(); + } + + public UniTask OnParticleTriggerAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnParticleTriggerHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnParticleTriggerAsync(); + } + } +#endregion + +#region ParticleUpdateJobScheduled +#if UNITY_2019_3_OR_NEWER && (!UNITY_2019_1_OR_NEWER || UNITASK_PARTICLESYSTEM_SUPPORT) + + public interface IAsyncOnParticleUpdateJobScheduledHandler + { + UniTask OnParticleUpdateJobScheduledAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnParticleUpdateJobScheduledHandler + { + UniTask IAsyncOnParticleUpdateJobScheduledHandler.OnParticleUpdateJobScheduledAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncParticleUpdateJobScheduledTrigger GetAsyncParticleUpdateJobScheduledTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncParticleUpdateJobScheduledTrigger GetAsyncParticleUpdateJobScheduledTrigger(this Component component) + { + return component.gameObject.GetAsyncParticleUpdateJobScheduledTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncParticleUpdateJobScheduledTrigger : AsyncTriggerBase + { + void OnParticleUpdateJobScheduled(UnityEngine.ParticleSystemJobs.ParticleSystemJobData particles) + { + RaiseEvent((particles)); + } + + public IAsyncOnParticleUpdateJobScheduledHandler GetOnParticleUpdateJobScheduledAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnParticleUpdateJobScheduledHandler GetOnParticleUpdateJobScheduledAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnParticleUpdateJobScheduledAsync() + { + return ((IAsyncOnParticleUpdateJobScheduledHandler)new AsyncTriggerHandler(this, true)).OnParticleUpdateJobScheduledAsync(); + } + + public UniTask OnParticleUpdateJobScheduledAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnParticleUpdateJobScheduledHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnParticleUpdateJobScheduledAsync(); + } + } +#endif +#endregion + +#region PostRender + + public interface IAsyncOnPostRenderHandler + { + UniTask OnPostRenderAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnPostRenderHandler + { + UniTask IAsyncOnPostRenderHandler.OnPostRenderAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncPostRenderTrigger GetAsyncPostRenderTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncPostRenderTrigger GetAsyncPostRenderTrigger(this Component component) + { + return component.gameObject.GetAsyncPostRenderTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncPostRenderTrigger : AsyncTriggerBase + { + void OnPostRender() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnPostRenderHandler GetOnPostRenderAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnPostRenderHandler GetOnPostRenderAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnPostRenderAsync() + { + return ((IAsyncOnPostRenderHandler)new AsyncTriggerHandler(this, true)).OnPostRenderAsync(); + } + + public UniTask OnPostRenderAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnPostRenderHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnPostRenderAsync(); + } + } +#endregion + +#region PreCull + + public interface IAsyncOnPreCullHandler + { + UniTask OnPreCullAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnPreCullHandler + { + UniTask IAsyncOnPreCullHandler.OnPreCullAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncPreCullTrigger GetAsyncPreCullTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncPreCullTrigger GetAsyncPreCullTrigger(this Component component) + { + return component.gameObject.GetAsyncPreCullTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncPreCullTrigger : AsyncTriggerBase + { + void OnPreCull() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnPreCullHandler GetOnPreCullAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnPreCullHandler GetOnPreCullAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnPreCullAsync() + { + return ((IAsyncOnPreCullHandler)new AsyncTriggerHandler(this, true)).OnPreCullAsync(); + } + + public UniTask OnPreCullAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnPreCullHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnPreCullAsync(); + } + } +#endregion + +#region PreRender + + public interface IAsyncOnPreRenderHandler + { + UniTask OnPreRenderAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnPreRenderHandler + { + UniTask IAsyncOnPreRenderHandler.OnPreRenderAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncPreRenderTrigger GetAsyncPreRenderTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncPreRenderTrigger GetAsyncPreRenderTrigger(this Component component) + { + return component.gameObject.GetAsyncPreRenderTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncPreRenderTrigger : AsyncTriggerBase + { + void OnPreRender() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnPreRenderHandler GetOnPreRenderAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnPreRenderHandler GetOnPreRenderAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnPreRenderAsync() + { + return ((IAsyncOnPreRenderHandler)new AsyncTriggerHandler(this, true)).OnPreRenderAsync(); + } + + public UniTask OnPreRenderAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnPreRenderHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnPreRenderAsync(); + } + } +#endregion + +#region RectTransformDimensionsChange + + public interface IAsyncOnRectTransformDimensionsChangeHandler + { + UniTask OnRectTransformDimensionsChangeAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnRectTransformDimensionsChangeHandler + { + UniTask IAsyncOnRectTransformDimensionsChangeHandler.OnRectTransformDimensionsChangeAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncRectTransformDimensionsChangeTrigger GetAsyncRectTransformDimensionsChangeTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncRectTransformDimensionsChangeTrigger GetAsyncRectTransformDimensionsChangeTrigger(this Component component) + { + return component.gameObject.GetAsyncRectTransformDimensionsChangeTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncRectTransformDimensionsChangeTrigger : AsyncTriggerBase + { + void OnRectTransformDimensionsChange() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnRectTransformDimensionsChangeHandler GetOnRectTransformDimensionsChangeAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnRectTransformDimensionsChangeHandler GetOnRectTransformDimensionsChangeAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnRectTransformDimensionsChangeAsync() + { + return ((IAsyncOnRectTransformDimensionsChangeHandler)new AsyncTriggerHandler(this, true)).OnRectTransformDimensionsChangeAsync(); + } + + public UniTask OnRectTransformDimensionsChangeAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnRectTransformDimensionsChangeHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnRectTransformDimensionsChangeAsync(); + } + } +#endregion + +#region RectTransformRemoved + + public interface IAsyncOnRectTransformRemovedHandler + { + UniTask OnRectTransformRemovedAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnRectTransformRemovedHandler + { + UniTask IAsyncOnRectTransformRemovedHandler.OnRectTransformRemovedAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncRectTransformRemovedTrigger GetAsyncRectTransformRemovedTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncRectTransformRemovedTrigger GetAsyncRectTransformRemovedTrigger(this Component component) + { + return component.gameObject.GetAsyncRectTransformRemovedTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncRectTransformRemovedTrigger : AsyncTriggerBase + { + void OnRectTransformRemoved() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnRectTransformRemovedHandler GetOnRectTransformRemovedAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnRectTransformRemovedHandler GetOnRectTransformRemovedAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnRectTransformRemovedAsync() + { + return ((IAsyncOnRectTransformRemovedHandler)new AsyncTriggerHandler(this, true)).OnRectTransformRemovedAsync(); + } + + public UniTask OnRectTransformRemovedAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnRectTransformRemovedHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnRectTransformRemovedAsync(); + } + } +#endregion + +#region RenderImage + + public interface IAsyncOnRenderImageHandler + { + UniTask<(RenderTexture source, RenderTexture destination)> OnRenderImageAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnRenderImageHandler + { + UniTask<(RenderTexture source, RenderTexture destination)> IAsyncOnRenderImageHandler.OnRenderImageAsync() + { + core.Reset(); + return new UniTask<(RenderTexture source, RenderTexture destination)>((IUniTaskSource<(RenderTexture source, RenderTexture destination)>)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncRenderImageTrigger GetAsyncRenderImageTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncRenderImageTrigger GetAsyncRenderImageTrigger(this Component component) + { + return component.gameObject.GetAsyncRenderImageTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncRenderImageTrigger : AsyncTriggerBase<(RenderTexture source, RenderTexture destination)> + { + void OnRenderImage(RenderTexture source, RenderTexture destination) + { + RaiseEvent((source, destination)); + } + + public IAsyncOnRenderImageHandler GetOnRenderImageAsyncHandler() + { + return new AsyncTriggerHandler<(RenderTexture source, RenderTexture destination)>(this, false); + } + + public IAsyncOnRenderImageHandler GetOnRenderImageAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler<(RenderTexture source, RenderTexture destination)>(this, cancellationToken, false); + } + + public UniTask<(RenderTexture source, RenderTexture destination)> OnRenderImageAsync() + { + return ((IAsyncOnRenderImageHandler)new AsyncTriggerHandler<(RenderTexture source, RenderTexture destination)>(this, true)).OnRenderImageAsync(); + } + + public UniTask<(RenderTexture source, RenderTexture destination)> OnRenderImageAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnRenderImageHandler)new AsyncTriggerHandler<(RenderTexture source, RenderTexture destination)>(this, cancellationToken, true)).OnRenderImageAsync(); + } + } +#endregion + +#region RenderObject + + public interface IAsyncOnRenderObjectHandler + { + UniTask OnRenderObjectAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnRenderObjectHandler + { + UniTask IAsyncOnRenderObjectHandler.OnRenderObjectAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncRenderObjectTrigger GetAsyncRenderObjectTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncRenderObjectTrigger GetAsyncRenderObjectTrigger(this Component component) + { + return component.gameObject.GetAsyncRenderObjectTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncRenderObjectTrigger : AsyncTriggerBase + { + void OnRenderObject() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnRenderObjectHandler GetOnRenderObjectAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnRenderObjectHandler GetOnRenderObjectAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnRenderObjectAsync() + { + return ((IAsyncOnRenderObjectHandler)new AsyncTriggerHandler(this, true)).OnRenderObjectAsync(); + } + + public UniTask OnRenderObjectAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnRenderObjectHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnRenderObjectAsync(); + } + } +#endregion + +#region ServerInitialized + + public interface IAsyncOnServerInitializedHandler + { + UniTask OnServerInitializedAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnServerInitializedHandler + { + UniTask IAsyncOnServerInitializedHandler.OnServerInitializedAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncServerInitializedTrigger GetAsyncServerInitializedTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncServerInitializedTrigger GetAsyncServerInitializedTrigger(this Component component) + { + return component.gameObject.GetAsyncServerInitializedTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncServerInitializedTrigger : AsyncTriggerBase + { + void OnServerInitialized() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnServerInitializedHandler GetOnServerInitializedAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnServerInitializedHandler GetOnServerInitializedAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnServerInitializedAsync() + { + return ((IAsyncOnServerInitializedHandler)new AsyncTriggerHandler(this, true)).OnServerInitializedAsync(); + } + + public UniTask OnServerInitializedAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnServerInitializedHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnServerInitializedAsync(); + } + } +#endregion + +#region TransformChildrenChanged + + public interface IAsyncOnTransformChildrenChangedHandler + { + UniTask OnTransformChildrenChangedAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnTransformChildrenChangedHandler + { + UniTask IAsyncOnTransformChildrenChangedHandler.OnTransformChildrenChangedAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncTransformChildrenChangedTrigger GetAsyncTransformChildrenChangedTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncTransformChildrenChangedTrigger GetAsyncTransformChildrenChangedTrigger(this Component component) + { + return component.gameObject.GetAsyncTransformChildrenChangedTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncTransformChildrenChangedTrigger : AsyncTriggerBase + { + void OnTransformChildrenChanged() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnTransformChildrenChangedHandler GetOnTransformChildrenChangedAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnTransformChildrenChangedHandler GetOnTransformChildrenChangedAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnTransformChildrenChangedAsync() + { + return ((IAsyncOnTransformChildrenChangedHandler)new AsyncTriggerHandler(this, true)).OnTransformChildrenChangedAsync(); + } + + public UniTask OnTransformChildrenChangedAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnTransformChildrenChangedHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnTransformChildrenChangedAsync(); + } + } +#endregion + +#region TransformParentChanged + + public interface IAsyncOnTransformParentChangedHandler + { + UniTask OnTransformParentChangedAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnTransformParentChangedHandler + { + UniTask IAsyncOnTransformParentChangedHandler.OnTransformParentChangedAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncTransformParentChangedTrigger GetAsyncTransformParentChangedTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncTransformParentChangedTrigger GetAsyncTransformParentChangedTrigger(this Component component) + { + return component.gameObject.GetAsyncTransformParentChangedTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncTransformParentChangedTrigger : AsyncTriggerBase + { + void OnTransformParentChanged() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnTransformParentChangedHandler GetOnTransformParentChangedAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnTransformParentChangedHandler GetOnTransformParentChangedAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnTransformParentChangedAsync() + { + return ((IAsyncOnTransformParentChangedHandler)new AsyncTriggerHandler(this, true)).OnTransformParentChangedAsync(); + } + + public UniTask OnTransformParentChangedAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnTransformParentChangedHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnTransformParentChangedAsync(); + } + } +#endregion + +#region TriggerEnter +#if !UNITY_2019_1_OR_NEWER || UNITASK_PHYSICS_SUPPORT + + public interface IAsyncOnTriggerEnterHandler + { + UniTask OnTriggerEnterAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnTriggerEnterHandler + { + UniTask IAsyncOnTriggerEnterHandler.OnTriggerEnterAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncTriggerEnterTrigger GetAsyncTriggerEnterTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncTriggerEnterTrigger GetAsyncTriggerEnterTrigger(this Component component) + { + return component.gameObject.GetAsyncTriggerEnterTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncTriggerEnterTrigger : AsyncTriggerBase + { + void OnTriggerEnter(Collider other) + { + RaiseEvent((other)); + } + + public IAsyncOnTriggerEnterHandler GetOnTriggerEnterAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnTriggerEnterHandler GetOnTriggerEnterAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnTriggerEnterAsync() + { + return ((IAsyncOnTriggerEnterHandler)new AsyncTriggerHandler(this, true)).OnTriggerEnterAsync(); + } + + public UniTask OnTriggerEnterAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnTriggerEnterHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnTriggerEnterAsync(); + } + } +#endif +#endregion + +#region TriggerEnter2D +#if !UNITY_2019_1_OR_NEWER || UNITASK_PHYSICS2D_SUPPORT + + public interface IAsyncOnTriggerEnter2DHandler + { + UniTask OnTriggerEnter2DAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnTriggerEnter2DHandler + { + UniTask IAsyncOnTriggerEnter2DHandler.OnTriggerEnter2DAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncTriggerEnter2DTrigger GetAsyncTriggerEnter2DTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncTriggerEnter2DTrigger GetAsyncTriggerEnter2DTrigger(this Component component) + { + return component.gameObject.GetAsyncTriggerEnter2DTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncTriggerEnter2DTrigger : AsyncTriggerBase + { + void OnTriggerEnter2D(Collider2D other) + { + RaiseEvent((other)); + } + + public IAsyncOnTriggerEnter2DHandler GetOnTriggerEnter2DAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnTriggerEnter2DHandler GetOnTriggerEnter2DAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnTriggerEnter2DAsync() + { + return ((IAsyncOnTriggerEnter2DHandler)new AsyncTriggerHandler(this, true)).OnTriggerEnter2DAsync(); + } + + public UniTask OnTriggerEnter2DAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnTriggerEnter2DHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnTriggerEnter2DAsync(); + } + } +#endif +#endregion + +#region TriggerExit +#if !UNITY_2019_1_OR_NEWER || UNITASK_PHYSICS_SUPPORT + + public interface IAsyncOnTriggerExitHandler + { + UniTask OnTriggerExitAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnTriggerExitHandler + { + UniTask IAsyncOnTriggerExitHandler.OnTriggerExitAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncTriggerExitTrigger GetAsyncTriggerExitTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncTriggerExitTrigger GetAsyncTriggerExitTrigger(this Component component) + { + return component.gameObject.GetAsyncTriggerExitTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncTriggerExitTrigger : AsyncTriggerBase + { + void OnTriggerExit(Collider other) + { + RaiseEvent((other)); + } + + public IAsyncOnTriggerExitHandler GetOnTriggerExitAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnTriggerExitHandler GetOnTriggerExitAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnTriggerExitAsync() + { + return ((IAsyncOnTriggerExitHandler)new AsyncTriggerHandler(this, true)).OnTriggerExitAsync(); + } + + public UniTask OnTriggerExitAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnTriggerExitHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnTriggerExitAsync(); + } + } +#endif +#endregion + +#region TriggerExit2D +#if !UNITY_2019_1_OR_NEWER || UNITASK_PHYSICS2D_SUPPORT + + public interface IAsyncOnTriggerExit2DHandler + { + UniTask OnTriggerExit2DAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnTriggerExit2DHandler + { + UniTask IAsyncOnTriggerExit2DHandler.OnTriggerExit2DAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncTriggerExit2DTrigger GetAsyncTriggerExit2DTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncTriggerExit2DTrigger GetAsyncTriggerExit2DTrigger(this Component component) + { + return component.gameObject.GetAsyncTriggerExit2DTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncTriggerExit2DTrigger : AsyncTriggerBase + { + void OnTriggerExit2D(Collider2D other) + { + RaiseEvent((other)); + } + + public IAsyncOnTriggerExit2DHandler GetOnTriggerExit2DAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnTriggerExit2DHandler GetOnTriggerExit2DAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnTriggerExit2DAsync() + { + return ((IAsyncOnTriggerExit2DHandler)new AsyncTriggerHandler(this, true)).OnTriggerExit2DAsync(); + } + + public UniTask OnTriggerExit2DAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnTriggerExit2DHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnTriggerExit2DAsync(); + } + } +#endif +#endregion + +#region TriggerStay +#if !UNITY_2019_1_OR_NEWER || UNITASK_PHYSICS_SUPPORT + + public interface IAsyncOnTriggerStayHandler + { + UniTask OnTriggerStayAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnTriggerStayHandler + { + UniTask IAsyncOnTriggerStayHandler.OnTriggerStayAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncTriggerStayTrigger GetAsyncTriggerStayTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncTriggerStayTrigger GetAsyncTriggerStayTrigger(this Component component) + { + return component.gameObject.GetAsyncTriggerStayTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncTriggerStayTrigger : AsyncTriggerBase + { + void OnTriggerStay(Collider other) + { + RaiseEvent((other)); + } + + public IAsyncOnTriggerStayHandler GetOnTriggerStayAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnTriggerStayHandler GetOnTriggerStayAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnTriggerStayAsync() + { + return ((IAsyncOnTriggerStayHandler)new AsyncTriggerHandler(this, true)).OnTriggerStayAsync(); + } + + public UniTask OnTriggerStayAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnTriggerStayHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnTriggerStayAsync(); + } + } +#endif +#endregion + +#region TriggerStay2D +#if !UNITY_2019_1_OR_NEWER || UNITASK_PHYSICS2D_SUPPORT + + public interface IAsyncOnTriggerStay2DHandler + { + UniTask OnTriggerStay2DAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnTriggerStay2DHandler + { + UniTask IAsyncOnTriggerStay2DHandler.OnTriggerStay2DAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncTriggerStay2DTrigger GetAsyncTriggerStay2DTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncTriggerStay2DTrigger GetAsyncTriggerStay2DTrigger(this Component component) + { + return component.gameObject.GetAsyncTriggerStay2DTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncTriggerStay2DTrigger : AsyncTriggerBase + { + void OnTriggerStay2D(Collider2D other) + { + RaiseEvent((other)); + } + + public IAsyncOnTriggerStay2DHandler GetOnTriggerStay2DAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnTriggerStay2DHandler GetOnTriggerStay2DAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnTriggerStay2DAsync() + { + return ((IAsyncOnTriggerStay2DHandler)new AsyncTriggerHandler(this, true)).OnTriggerStay2DAsync(); + } + + public UniTask OnTriggerStay2DAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnTriggerStay2DHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnTriggerStay2DAsync(); + } + } +#endif +#endregion + +#region Validate + + public interface IAsyncOnValidateHandler + { + UniTask OnValidateAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnValidateHandler + { + UniTask IAsyncOnValidateHandler.OnValidateAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncValidateTrigger GetAsyncValidateTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncValidateTrigger GetAsyncValidateTrigger(this Component component) + { + return component.gameObject.GetAsyncValidateTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncValidateTrigger : AsyncTriggerBase + { + void OnValidate() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnValidateHandler GetOnValidateAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnValidateHandler GetOnValidateAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnValidateAsync() + { + return ((IAsyncOnValidateHandler)new AsyncTriggerHandler(this, true)).OnValidateAsync(); + } + + public UniTask OnValidateAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnValidateHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnValidateAsync(); + } + } +#endregion + +#region WillRenderObject + + public interface IAsyncOnWillRenderObjectHandler + { + UniTask OnWillRenderObjectAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnWillRenderObjectHandler + { + UniTask IAsyncOnWillRenderObjectHandler.OnWillRenderObjectAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncWillRenderObjectTrigger GetAsyncWillRenderObjectTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncWillRenderObjectTrigger GetAsyncWillRenderObjectTrigger(this Component component) + { + return component.gameObject.GetAsyncWillRenderObjectTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncWillRenderObjectTrigger : AsyncTriggerBase + { + void OnWillRenderObject() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncOnWillRenderObjectHandler GetOnWillRenderObjectAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnWillRenderObjectHandler GetOnWillRenderObjectAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnWillRenderObjectAsync() + { + return ((IAsyncOnWillRenderObjectHandler)new AsyncTriggerHandler(this, true)).OnWillRenderObjectAsync(); + } + + public UniTask OnWillRenderObjectAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnWillRenderObjectHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnWillRenderObjectAsync(); + } + } +#endregion + +#region Reset + + public interface IAsyncResetHandler + { + UniTask ResetAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncResetHandler + { + UniTask IAsyncResetHandler.ResetAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncResetTrigger GetAsyncResetTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncResetTrigger GetAsyncResetTrigger(this Component component) + { + return component.gameObject.GetAsyncResetTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncResetTrigger : AsyncTriggerBase + { + void Reset() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncResetHandler GetResetAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncResetHandler GetResetAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask ResetAsync() + { + return ((IAsyncResetHandler)new AsyncTriggerHandler(this, true)).ResetAsync(); + } + + public UniTask ResetAsync(CancellationToken cancellationToken) + { + return ((IAsyncResetHandler)new AsyncTriggerHandler(this, cancellationToken, true)).ResetAsync(); + } + } +#endregion + +#region Update + + public interface IAsyncUpdateHandler + { + UniTask UpdateAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncUpdateHandler + { + UniTask IAsyncUpdateHandler.UpdateAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncUpdateTrigger GetAsyncUpdateTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncUpdateTrigger GetAsyncUpdateTrigger(this Component component) + { + return component.gameObject.GetAsyncUpdateTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncUpdateTrigger : AsyncTriggerBase + { + void Update() + { + RaiseEvent(AsyncUnit.Default); + } + + public IAsyncUpdateHandler GetUpdateAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncUpdateHandler GetUpdateAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask UpdateAsync() + { + return ((IAsyncUpdateHandler)new AsyncTriggerHandler(this, true)).UpdateAsync(); + } + + public UniTask UpdateAsync(CancellationToken cancellationToken) + { + return ((IAsyncUpdateHandler)new AsyncTriggerHandler(this, cancellationToken, true)).UpdateAsync(); + } + } +#endregion + +#region BeginDrag +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnBeginDragHandler + { + UniTask OnBeginDragAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnBeginDragHandler + { + UniTask IAsyncOnBeginDragHandler.OnBeginDragAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncBeginDragTrigger GetAsyncBeginDragTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncBeginDragTrigger GetAsyncBeginDragTrigger(this Component component) + { + return component.gameObject.GetAsyncBeginDragTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncBeginDragTrigger : AsyncTriggerBase, IBeginDragHandler + { + void IBeginDragHandler.OnBeginDrag(PointerEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnBeginDragHandler GetOnBeginDragAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnBeginDragHandler GetOnBeginDragAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnBeginDragAsync() + { + return ((IAsyncOnBeginDragHandler)new AsyncTriggerHandler(this, true)).OnBeginDragAsync(); + } + + public UniTask OnBeginDragAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnBeginDragHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnBeginDragAsync(); + } + } +#endif +#endregion + +#region Cancel +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnCancelHandler + { + UniTask OnCancelAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnCancelHandler + { + UniTask IAsyncOnCancelHandler.OnCancelAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncCancelTrigger GetAsyncCancelTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncCancelTrigger GetAsyncCancelTrigger(this Component component) + { + return component.gameObject.GetAsyncCancelTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncCancelTrigger : AsyncTriggerBase, ICancelHandler + { + void ICancelHandler.OnCancel(BaseEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnCancelHandler GetOnCancelAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnCancelHandler GetOnCancelAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnCancelAsync() + { + return ((IAsyncOnCancelHandler)new AsyncTriggerHandler(this, true)).OnCancelAsync(); + } + + public UniTask OnCancelAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnCancelHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnCancelAsync(); + } + } +#endif +#endregion + +#region Deselect +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnDeselectHandler + { + UniTask OnDeselectAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnDeselectHandler + { + UniTask IAsyncOnDeselectHandler.OnDeselectAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncDeselectTrigger GetAsyncDeselectTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncDeselectTrigger GetAsyncDeselectTrigger(this Component component) + { + return component.gameObject.GetAsyncDeselectTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncDeselectTrigger : AsyncTriggerBase, IDeselectHandler + { + void IDeselectHandler.OnDeselect(BaseEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnDeselectHandler GetOnDeselectAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnDeselectHandler GetOnDeselectAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnDeselectAsync() + { + return ((IAsyncOnDeselectHandler)new AsyncTriggerHandler(this, true)).OnDeselectAsync(); + } + + public UniTask OnDeselectAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnDeselectHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnDeselectAsync(); + } + } +#endif +#endregion + +#region Drag +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnDragHandler + { + UniTask OnDragAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnDragHandler + { + UniTask IAsyncOnDragHandler.OnDragAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncDragTrigger GetAsyncDragTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncDragTrigger GetAsyncDragTrigger(this Component component) + { + return component.gameObject.GetAsyncDragTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncDragTrigger : AsyncTriggerBase, IDragHandler + { + void IDragHandler.OnDrag(PointerEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnDragHandler GetOnDragAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnDragHandler GetOnDragAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnDragAsync() + { + return ((IAsyncOnDragHandler)new AsyncTriggerHandler(this, true)).OnDragAsync(); + } + + public UniTask OnDragAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnDragHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnDragAsync(); + } + } +#endif +#endregion + +#region Drop +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnDropHandler + { + UniTask OnDropAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnDropHandler + { + UniTask IAsyncOnDropHandler.OnDropAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncDropTrigger GetAsyncDropTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncDropTrigger GetAsyncDropTrigger(this Component component) + { + return component.gameObject.GetAsyncDropTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncDropTrigger : AsyncTriggerBase, IDropHandler + { + void IDropHandler.OnDrop(PointerEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnDropHandler GetOnDropAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnDropHandler GetOnDropAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnDropAsync() + { + return ((IAsyncOnDropHandler)new AsyncTriggerHandler(this, true)).OnDropAsync(); + } + + public UniTask OnDropAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnDropHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnDropAsync(); + } + } +#endif +#endregion + +#region EndDrag +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnEndDragHandler + { + UniTask OnEndDragAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnEndDragHandler + { + UniTask IAsyncOnEndDragHandler.OnEndDragAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncEndDragTrigger GetAsyncEndDragTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncEndDragTrigger GetAsyncEndDragTrigger(this Component component) + { + return component.gameObject.GetAsyncEndDragTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncEndDragTrigger : AsyncTriggerBase, IEndDragHandler + { + void IEndDragHandler.OnEndDrag(PointerEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnEndDragHandler GetOnEndDragAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnEndDragHandler GetOnEndDragAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnEndDragAsync() + { + return ((IAsyncOnEndDragHandler)new AsyncTriggerHandler(this, true)).OnEndDragAsync(); + } + + public UniTask OnEndDragAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnEndDragHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnEndDragAsync(); + } + } +#endif +#endregion + +#region InitializePotentialDrag +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnInitializePotentialDragHandler + { + UniTask OnInitializePotentialDragAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnInitializePotentialDragHandler + { + UniTask IAsyncOnInitializePotentialDragHandler.OnInitializePotentialDragAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncInitializePotentialDragTrigger GetAsyncInitializePotentialDragTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncInitializePotentialDragTrigger GetAsyncInitializePotentialDragTrigger(this Component component) + { + return component.gameObject.GetAsyncInitializePotentialDragTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncInitializePotentialDragTrigger : AsyncTriggerBase, IInitializePotentialDragHandler + { + void IInitializePotentialDragHandler.OnInitializePotentialDrag(PointerEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnInitializePotentialDragHandler GetOnInitializePotentialDragAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnInitializePotentialDragHandler GetOnInitializePotentialDragAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnInitializePotentialDragAsync() + { + return ((IAsyncOnInitializePotentialDragHandler)new AsyncTriggerHandler(this, true)).OnInitializePotentialDragAsync(); + } + + public UniTask OnInitializePotentialDragAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnInitializePotentialDragHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnInitializePotentialDragAsync(); + } + } +#endif +#endregion + +#region Move +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnMoveHandler + { + UniTask OnMoveAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnMoveHandler + { + UniTask IAsyncOnMoveHandler.OnMoveAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncMoveTrigger GetAsyncMoveTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncMoveTrigger GetAsyncMoveTrigger(this Component component) + { + return component.gameObject.GetAsyncMoveTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncMoveTrigger : AsyncTriggerBase, IMoveHandler + { + void IMoveHandler.OnMove(AxisEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnMoveHandler GetOnMoveAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnMoveHandler GetOnMoveAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnMoveAsync() + { + return ((IAsyncOnMoveHandler)new AsyncTriggerHandler(this, true)).OnMoveAsync(); + } + + public UniTask OnMoveAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnMoveHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnMoveAsync(); + } + } +#endif +#endregion + +#region PointerClick +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnPointerClickHandler + { + UniTask OnPointerClickAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnPointerClickHandler + { + UniTask IAsyncOnPointerClickHandler.OnPointerClickAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncPointerClickTrigger GetAsyncPointerClickTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncPointerClickTrigger GetAsyncPointerClickTrigger(this Component component) + { + return component.gameObject.GetAsyncPointerClickTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncPointerClickTrigger : AsyncTriggerBase, IPointerClickHandler + { + void IPointerClickHandler.OnPointerClick(PointerEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnPointerClickHandler GetOnPointerClickAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnPointerClickHandler GetOnPointerClickAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnPointerClickAsync() + { + return ((IAsyncOnPointerClickHandler)new AsyncTriggerHandler(this, true)).OnPointerClickAsync(); + } + + public UniTask OnPointerClickAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnPointerClickHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnPointerClickAsync(); + } + } +#endif +#endregion + +#region PointerDown +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnPointerDownHandler + { + UniTask OnPointerDownAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnPointerDownHandler + { + UniTask IAsyncOnPointerDownHandler.OnPointerDownAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncPointerDownTrigger GetAsyncPointerDownTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncPointerDownTrigger GetAsyncPointerDownTrigger(this Component component) + { + return component.gameObject.GetAsyncPointerDownTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncPointerDownTrigger : AsyncTriggerBase, IPointerDownHandler + { + void IPointerDownHandler.OnPointerDown(PointerEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnPointerDownHandler GetOnPointerDownAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnPointerDownHandler GetOnPointerDownAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnPointerDownAsync() + { + return ((IAsyncOnPointerDownHandler)new AsyncTriggerHandler(this, true)).OnPointerDownAsync(); + } + + public UniTask OnPointerDownAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnPointerDownHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnPointerDownAsync(); + } + } +#endif +#endregion + +#region PointerEnter +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnPointerEnterHandler + { + UniTask OnPointerEnterAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnPointerEnterHandler + { + UniTask IAsyncOnPointerEnterHandler.OnPointerEnterAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncPointerEnterTrigger GetAsyncPointerEnterTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncPointerEnterTrigger GetAsyncPointerEnterTrigger(this Component component) + { + return component.gameObject.GetAsyncPointerEnterTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncPointerEnterTrigger : AsyncTriggerBase, IPointerEnterHandler + { + void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnPointerEnterHandler GetOnPointerEnterAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnPointerEnterHandler GetOnPointerEnterAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnPointerEnterAsync() + { + return ((IAsyncOnPointerEnterHandler)new AsyncTriggerHandler(this, true)).OnPointerEnterAsync(); + } + + public UniTask OnPointerEnterAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnPointerEnterHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnPointerEnterAsync(); + } + } +#endif +#endregion + +#region PointerExit +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnPointerExitHandler + { + UniTask OnPointerExitAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnPointerExitHandler + { + UniTask IAsyncOnPointerExitHandler.OnPointerExitAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncPointerExitTrigger GetAsyncPointerExitTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncPointerExitTrigger GetAsyncPointerExitTrigger(this Component component) + { + return component.gameObject.GetAsyncPointerExitTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncPointerExitTrigger : AsyncTriggerBase, IPointerExitHandler + { + void IPointerExitHandler.OnPointerExit(PointerEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnPointerExitHandler GetOnPointerExitAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnPointerExitHandler GetOnPointerExitAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnPointerExitAsync() + { + return ((IAsyncOnPointerExitHandler)new AsyncTriggerHandler(this, true)).OnPointerExitAsync(); + } + + public UniTask OnPointerExitAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnPointerExitHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnPointerExitAsync(); + } + } +#endif +#endregion + +#region PointerUp +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnPointerUpHandler + { + UniTask OnPointerUpAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnPointerUpHandler + { + UniTask IAsyncOnPointerUpHandler.OnPointerUpAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncPointerUpTrigger GetAsyncPointerUpTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncPointerUpTrigger GetAsyncPointerUpTrigger(this Component component) + { + return component.gameObject.GetAsyncPointerUpTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncPointerUpTrigger : AsyncTriggerBase, IPointerUpHandler + { + void IPointerUpHandler.OnPointerUp(PointerEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnPointerUpHandler GetOnPointerUpAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnPointerUpHandler GetOnPointerUpAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnPointerUpAsync() + { + return ((IAsyncOnPointerUpHandler)new AsyncTriggerHandler(this, true)).OnPointerUpAsync(); + } + + public UniTask OnPointerUpAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnPointerUpHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnPointerUpAsync(); + } + } +#endif +#endregion + +#region Scroll +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnScrollHandler + { + UniTask OnScrollAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnScrollHandler + { + UniTask IAsyncOnScrollHandler.OnScrollAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncScrollTrigger GetAsyncScrollTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncScrollTrigger GetAsyncScrollTrigger(this Component component) + { + return component.gameObject.GetAsyncScrollTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncScrollTrigger : AsyncTriggerBase, IScrollHandler + { + void IScrollHandler.OnScroll(PointerEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnScrollHandler GetOnScrollAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnScrollHandler GetOnScrollAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnScrollAsync() + { + return ((IAsyncOnScrollHandler)new AsyncTriggerHandler(this, true)).OnScrollAsync(); + } + + public UniTask OnScrollAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnScrollHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnScrollAsync(); + } + } +#endif +#endregion + +#region Select +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnSelectHandler + { + UniTask OnSelectAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnSelectHandler + { + UniTask IAsyncOnSelectHandler.OnSelectAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncSelectTrigger GetAsyncSelectTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncSelectTrigger GetAsyncSelectTrigger(this Component component) + { + return component.gameObject.GetAsyncSelectTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncSelectTrigger : AsyncTriggerBase, ISelectHandler + { + void ISelectHandler.OnSelect(BaseEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnSelectHandler GetOnSelectAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnSelectHandler GetOnSelectAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnSelectAsync() + { + return ((IAsyncOnSelectHandler)new AsyncTriggerHandler(this, true)).OnSelectAsync(); + } + + public UniTask OnSelectAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnSelectHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnSelectAsync(); + } + } +#endif +#endregion + +#region Submit +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnSubmitHandler + { + UniTask OnSubmitAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnSubmitHandler + { + UniTask IAsyncOnSubmitHandler.OnSubmitAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncSubmitTrigger GetAsyncSubmitTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncSubmitTrigger GetAsyncSubmitTrigger(this Component component) + { + return component.gameObject.GetAsyncSubmitTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncSubmitTrigger : AsyncTriggerBase, ISubmitHandler + { + void ISubmitHandler.OnSubmit(BaseEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnSubmitHandler GetOnSubmitAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnSubmitHandler GetOnSubmitAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnSubmitAsync() + { + return ((IAsyncOnSubmitHandler)new AsyncTriggerHandler(this, true)).OnSubmitAsync(); + } + + public UniTask OnSubmitAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnSubmitHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnSubmitAsync(); + } + } +#endif +#endregion + +#region UpdateSelected +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + + public interface IAsyncOnUpdateSelectedHandler + { + UniTask OnUpdateSelectedAsync(); + } + + public partial class AsyncTriggerHandler : IAsyncOnUpdateSelectedHandler + { + UniTask IAsyncOnUpdateSelectedHandler.OnUpdateSelectedAsync() + { + core.Reset(); + return new UniTask((IUniTaskSource)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static AsyncUpdateSelectedTrigger GetAsyncUpdateSelectedTrigger(this GameObject gameObject) + { + return GetOrAddComponent(gameObject); + } + + public static AsyncUpdateSelectedTrigger GetAsyncUpdateSelectedTrigger(this Component component) + { + return component.gameObject.GetAsyncUpdateSelectedTrigger(); + } + } + + [DisallowMultipleComponent] + public sealed class AsyncUpdateSelectedTrigger : AsyncTriggerBase, IUpdateSelectedHandler + { + void IUpdateSelectedHandler.OnUpdateSelected(BaseEventData eventData) + { + RaiseEvent((eventData)); + } + + public IAsyncOnUpdateSelectedHandler GetOnUpdateSelectedAsyncHandler() + { + return new AsyncTriggerHandler(this, false); + } + + public IAsyncOnUpdateSelectedHandler GetOnUpdateSelectedAsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler(this, cancellationToken, false); + } + + public UniTask OnUpdateSelectedAsync() + { + return ((IAsyncOnUpdateSelectedHandler)new AsyncTriggerHandler(this, true)).OnUpdateSelectedAsync(); + } + + public UniTask OnUpdateSelectedAsync(CancellationToken cancellationToken) + { + return ((IAsyncOnUpdateSelectedHandler)new AsyncTriggerHandler(this, cancellationToken, true)).OnUpdateSelectedAsync(); + } + } +#endif +#endregion + +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/MonoBehaviourMessagesTriggers.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/MonoBehaviourMessagesTriggers.cs.meta new file mode 100644 index 0000000..82aa679 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/MonoBehaviourMessagesTriggers.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c30655636c35c3d4da44064af3d2d9a7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/MonoBehaviourMessagesTriggers.tt b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/MonoBehaviourMessagesTriggers.tt new file mode 100644 index 0000000..815d257 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/MonoBehaviourMessagesTriggers.tt @@ -0,0 +1,208 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".cs" #> +<# + var empty = new (string, string)[0]; + + var triggers = new (string triggerName, string methodName, string returnType, string handlerInterface, (string argType, string argName)[] arguments)[] + { + ("AnimatorIK", "OnAnimatorIK", "int", null, new []{ ("int", "layerIndex") }), + ("AnimatorMove", "OnAnimatorMove", "AsyncUnit", null, empty), + ("OnCanvasGroupChanged", "OnCanvasGroupChanged", "AsyncUnit", null, empty ), + ("CollisionEnter2D", "OnCollisionEnter2D", "Collision2D", null, new []{ ("Collision2D", "coll") }), + ("CollisionExit2D", "OnCollisionExit2D", "Collision2D", null, new []{ ("Collision2D", "coll") }), + ("CollisionStay2D", "OnCollisionStay2D", "Collision2D", null, new []{ ("Collision2D", "coll") }), + ("CollisionEnter", "OnCollisionEnter", "Collision", null, new []{ ("Collision", "coll") }), + ("CollisionExit", "OnCollisionExit", "Collision", null, new []{ ("Collision", "coll") }), + ("CollisionStay", "OnCollisionStay", "Collision", null, new []{ ("Collision", "coll") }), + ("Enable", "OnEnable", "AsyncUnit", null, empty), + ("Disable", "OnDisable", "AsyncUnit", null, empty), + ("JointBreak", "OnJointBreak", "float", null, new []{ ("float", "breakForce") }), + ("JointBreak2D", "OnJointBreak2D", "Joint2D", null, new []{ ("Joint2D", "brokenJoint") }), + ("Update", "Update", "AsyncUnit", null, empty), + ("FixedUpdate", "FixedUpdate", "AsyncUnit", null, empty), + ("LateUpdate", "LateUpdate", "AsyncUnit", null, empty), + + ("ParticleCollision", "OnParticleCollision", "GameObject", null, new []{ ("GameObject", "other") }), + ("RectTransformDimensionsChange", "OnRectTransformDimensionsChange", "AsyncUnit", null, empty), + ("RectTransformRemoved", "OnRectTransformRemoved", "AsyncUnit", null, empty), + ("BeforeTransformParentChanged", "OnBeforeTransformParentChanged", "AsyncUnit", null, empty), + ("TransformParentChanged", "OnTransformParentChanged", "AsyncUnit", null, empty), + ("TransformChildrenChanged", "OnTransformChildrenChanged", "AsyncUnit", null, empty), + ("TriggerEnter2D", "OnTriggerEnter2D", "Collider2D", null, new []{ ("Collider2D", "other") }), + ("TriggerExit2D", "OnTriggerExit2D", "Collider2D", null, new []{ ("Collider2D", "other") }), + ("TriggerStay2D", "OnTriggerStay2D", "Collider2D", null, new []{ ("Collider2D", "other") }), + ("TriggerEnter", "OnTriggerEnter", "Collider", null, new []{ ("Collider", "other") }), + ("TriggerExit", "OnTriggerExit", "Collider", null, new []{ ("Collider", "other") }), + ("TriggerStay", "OnTriggerStay", "Collider", null, new []{ ("Collider", "other") }), + ("BecameInvisible", "OnBecameInvisible", "AsyncUnit", null, empty), + ("BecameVisible", "OnBecameVisible", "AsyncUnit", null, empty), + + // Mouse... #if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO) + ("MouseDown", "OnMouseDown", "AsyncUnit", null, empty), + ("MouseDrag", "OnMouseDrag", "AsyncUnit", null, empty), + ("MouseEnter", "OnMouseEnter", "AsyncUnit", null, empty), + ("MouseExit", "OnMouseExit", "AsyncUnit", null, empty), + ("MouseOver", "OnMouseOver", "AsyncUnit", null, empty), + ("MouseUp", "OnMouseUp", "AsyncUnit", null, empty), + ("MouseUpAsButton", "OnMouseUpAsButton", "AsyncUnit", null, empty), + + // new in v2 + ("ApplicationFocus", "OnApplicationFocus", "bool", null, new []{("bool", "hasFocus") }), + ("ApplicationPause", "OnApplicationPause", "bool", null, new []{("bool", "pauseStatus") }), + ("ApplicationQuit", "OnApplicationQuit", "AsyncUnit", null, empty), + ("AudioFilterRead", "OnAudioFilterRead", "(float[] data, int channels)", null, new[]{("float[]", "data"), ("int", "channels")}), + ("ControllerColliderHit", "OnControllerColliderHit", "ControllerColliderHit", null, new[]{("ControllerColliderHit", "hit")}), + ("DrawGizmos", "OnDrawGizmos", "AsyncUnit", null, empty), + ("DrawGizmosSelected", "OnDrawGizmosSelected", "AsyncUnit", null, empty), + ("GUI", "OnGUI", "AsyncUnit", null, empty), + ("ParticleSystemStopped", "OnParticleSystemStopped", "AsyncUnit", null, empty), + ("ParticleTrigger", "OnParticleTrigger", "AsyncUnit", null, empty), + ("PostRender", "OnPostRender", "AsyncUnit", null, empty), + ("PreCull", "OnPreCull", "AsyncUnit", null, empty), + ("PreRender", "OnPreRender", "AsyncUnit", null, empty), + ("RenderImage", "OnRenderImage", "(RenderTexture source, RenderTexture destination)", null, new[]{("RenderTexture", "source"), ("RenderTexture", "destination")}), + ("RenderObject", "OnRenderObject", "AsyncUnit", null, empty), + ("ServerInitialized", "OnServerInitialized", "AsyncUnit", null, empty), + ("Validate", "OnValidate", "AsyncUnit", null, empty), + ("WillRenderObject", "OnWillRenderObject", "AsyncUnit", null, empty), + ("Reset", "Reset", "AsyncUnit", null, empty), + + // uGUI + ("BeginDrag", "OnBeginDrag", "PointerEventData", "IBeginDragHandler", new []{ ("PointerEventData", "eventData") }), + ("Cancel", "OnCancel", "BaseEventData", "ICancelHandler", new []{ ("BaseEventData", "eventData") }), + ("Deselect", "OnDeselect", "BaseEventData", "IDeselectHandler", new []{ ("BaseEventData", "eventData") }), + ("Drag", "OnDrag", "PointerEventData", "IDragHandler", new []{ ("PointerEventData", "eventData") }), + ("Drop", "OnDrop", "PointerEventData", "IDropHandler", new []{ ("PointerEventData", "eventData") }), + ("EndDrag", "OnEndDrag", "PointerEventData", "IEndDragHandler", new []{ ("PointerEventData", "eventData") }), + ("InitializePotentialDrag", "OnInitializePotentialDrag", "PointerEventData", "IInitializePotentialDragHandler", new []{ ("PointerEventData", "eventData") }), + ("Move", "OnMove", "AxisEventData", "IMoveHandler", new []{ ("AxisEventData", "eventData") }), + ("PointerClick", "OnPointerClick", "PointerEventData", "IPointerClickHandler", new []{ ("PointerEventData", "eventData") }), + ("PointerDown", "OnPointerDown", "PointerEventData", "IPointerDownHandler", new []{ ("PointerEventData", "eventData") }), + ("PointerEnter", "OnPointerEnter", "PointerEventData", "IPointerEnterHandler", new []{ ("PointerEventData", "eventData") }), + ("PointerExit", "OnPointerExit", "PointerEventData", "IPointerExitHandler", new []{ ("PointerEventData", "eventData") }), + ("PointerUp", "OnPointerUp", "PointerEventData", "IPointerUpHandler", new []{ ("PointerEventData", "eventData") }), + ("Scroll", "OnScroll", "PointerEventData", "IScrollHandler", new []{ ("PointerEventData", "eventData") }), + ("Select", "OnSelect", "BaseEventData", "ISelectHandler", new []{ ("BaseEventData", "eventData") }), + ("Submit", "OnSubmit", "BaseEventData", "ISubmitHandler", new []{ ("BaseEventData", "eventData") }), + ("UpdateSelected", "OnUpdateSelected", "BaseEventData", "IUpdateSelectedHandler", new []{ ("BaseEventData", "eventData") }), + + // 2019.3 + ("ParticleUpdateJobScheduled", "OnParticleUpdateJobScheduled", "UnityEngine.ParticleSystemJobs.ParticleSystemJobData", null, new[]{("UnityEngine.ParticleSystemJobs.ParticleSystemJobData", "particles")}), + + // Oneshot + // Awake, Start, Destroy + }; + + triggers = triggers.OrderBy(x => x.handlerInterface != null).ThenBy(x => x.handlerInterface != null ? x.handlerInterface : x.methodName).ToArray(); + + Func ToInterfaceName = x => $"IAsync{x}Handler"; + Func ToUniTaskName = x => x == "AsyncUnit" ? "UniTask" : $"UniTask<{x}>"; + Func ToCastUniTasSourceType = x => x == "AsyncUnit" ? "IUniTaskSource" : $"IUniTaskSource<{x}>"; + Func OnInvokeSuffix = x => x == "AsyncUnit" ? ".AsUniTask()" : $""; + Func<(string argType, string argName)[], string> BuildMethodArgument = x => string.Join(", ", x.Select(y => y.argType + " " + y.argName)); + Func<(string argType, string argName)[], string> BuildResultParameter = x => x.Length == 0 ? "AsyncUnit.Default" : "(" + string.Join(", ", x.Select(y => y.argName)) + ")"; + + Func IsParticleSystem = x => x == "ParticleUpdateJobScheduled"; + Func IsMouseTrigger = x => x.StartsWith("Mouse"); + Func RequirePhysicsModule = x => (x.StartsWith("Collision") || x.StartsWith("Collider") || x.StartsWith("ControllerCollider") || x.StartsWith("Joint") || x.StartsWith("Trigger")) + ? (x.Contains("2D") ? "UNITASK_PHYSICS2D_SUPPORT" : "UNITASK_PHYSICS_SUPPORT") + : null; + Func IsUguiSystem = x => x != null; +#> +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System.Threading; +using UnityEngine; +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT +using UnityEngine.EventSystems; +#endif + +namespace Cysharp.Threading.Tasks.Triggers +{ +<# foreach(var t in triggers) { #> +#region <#= t.triggerName #> +<# if(IsUguiSystem(t.handlerInterface)) { #> +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT +<# } #> +<# if(IsParticleSystem(t.triggerName)) { #> +#if UNITY_2019_3_OR_NEWER && (!UNITY_2019_1_OR_NEWER || UNITASK_PARTICLESYSTEM_SUPPORT) +<# } #> +<# if(IsMouseTrigger(t.triggerName)) { #> +#if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO) +<# } #> +<# if(RequirePhysicsModule(t.triggerName) != null) { #> +#if !UNITY_2019_1_OR_NEWER || <#= RequirePhysicsModule(t.triggerName) #> +<# } #> + + public interface <#= ToInterfaceName(t.methodName) #> + { + <#= ToUniTaskName(t.returnType) #> <#= t.methodName #>Async(); + } + + public partial class AsyncTriggerHandler : <#= ToInterfaceName(t.methodName) #> + { + <#= ToUniTaskName(t.returnType) #> <#= ToInterfaceName(t.methodName) #>.<#= t.methodName #>Async() + { + core.Reset(); + return new <#= ToUniTaskName(t.returnType) #>((<#= ToCastUniTasSourceType(t.returnType) #>)(object)this, core.Version); + } + } + + public static partial class AsyncTriggerExtensions + { + public static Async<#= t.triggerName #>Trigger GetAsync<#= t.triggerName #>Trigger(this GameObject gameObject) + { + return GetOrAddComponentTrigger>(gameObject); + } + + public static Async<#= t.triggerName #>Trigger GetAsync<#= t.triggerName #>Trigger(this Component component) + { + return component.gameObject.GetAsync<#= t.triggerName #>Trigger(); + } + } + + [DisallowMultipleComponent] + public sealed class Async<#= t.triggerName #>Trigger : AsyncTriggerBase<<#= t.returnType #>><#= (t.handlerInterface == null) ? "" : $", {t.handlerInterface}" #> + { + void <#= (t.handlerInterface == null) ? "" : $"{t.handlerInterface}." #><#= t.methodName #>(<#= BuildMethodArgument(t.arguments) #>) + { + RaiseEvent(<#= BuildResultParameter(t.arguments) #>); + } + + public <#= ToInterfaceName(t.methodName) #> Get<#= t.methodName #>AsyncHandler() + { + return new AsyncTriggerHandler<<#= t.returnType #>>(this, false); + } + + public <#= ToInterfaceName(t.methodName) #> Get<#= t.methodName #>AsyncHandler(CancellationToken cancellationToken) + { + return new AsyncTriggerHandler<<#= t.returnType #>>(this, cancellationToken, false); + } + + public <#= ToUniTaskName(t.returnType) #> <#= t.methodName #>Async() + { + return ((<#= ToInterfaceName(t.methodName) #>)new AsyncTriggerHandler<<#= t.returnType #>>(this, true)).<#= t.methodName #>Async(); + } + + public <#= ToUniTaskName(t.returnType) #> <#= t.methodName #>Async(CancellationToken cancellationToken) + { + return ((<#= ToInterfaceName(t.methodName) #>)new AsyncTriggerHandler<<#= t.returnType #>>(this, cancellationToken, true)).<#= t.methodName #>Async(); + } + } +<# if(IsUguiSystem(t.handlerInterface)) { #> +#endif +<# } #> +<# if(RequirePhysicsModule(t.triggerName) != null) { #> +#endif +<# } #> +<# if(IsParticleSystem(t.triggerName) || IsMouseTrigger(t.triggerName)) { #> +#endif +<# } #> +#endregion + +<# } #> +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/MonoBehaviourMessagesTriggers.tt.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/MonoBehaviourMessagesTriggers.tt.meta new file mode 100644 index 0000000..40a4e9e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/Triggers/MonoBehaviourMessagesTriggers.tt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 3ca26d0cd9373354c8cd147490f32c8e +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Bridge.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Bridge.cs new file mode 100644 index 0000000..c904299 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Bridge.cs @@ -0,0 +1,18 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Collections; + +namespace Cysharp.Threading.Tasks +{ + // UnityEngine Bridges. + + public partial struct UniTask + { + public static IEnumerator ToCoroutine(Func taskFactory) + { + return taskFactory().ToCoroutine(); + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Bridge.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Bridge.cs.meta new file mode 100644 index 0000000..6f8da80 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Bridge.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bd6beac8e0ebd264e9ba246c39429c72 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Delay.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Delay.cs new file mode 100644 index 0000000..7e922f4 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Delay.cs @@ -0,0 +1,963 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections; +using System.Runtime.CompilerServices; +using System.Threading; +using UnityEngine; + +namespace Cysharp.Threading.Tasks +{ + public enum DelayType + { + /// use Time.deltaTime. + DeltaTime, + /// Ignore timescale, use Time.unscaledDeltaTime. + UnscaledDeltaTime, + /// use Stopwatch.GetTimestamp(). + Realtime + } + + public partial struct UniTask + { + public static YieldAwaitable Yield() + { + // optimized for single continuation + return new YieldAwaitable(PlayerLoopTiming.Update); + } + + public static YieldAwaitable Yield(PlayerLoopTiming timing) + { + // optimized for single continuation + return new YieldAwaitable(timing); + } + + public static UniTask Yield(CancellationToken cancellationToken) + { + return new UniTask(YieldPromise.Create(PlayerLoopTiming.Update, cancellationToken, out var token), token); + } + + public static UniTask Yield(PlayerLoopTiming timing, CancellationToken cancellationToken) + { + return new UniTask(YieldPromise.Create(timing, cancellationToken, out var token), token); + } + + /// + /// Similar as UniTask.Yield but guaranteed run on next frame. + /// + public static UniTask NextFrame() + { + return new UniTask(NextFramePromise.Create(PlayerLoopTiming.Update, CancellationToken.None, out var token), token); + } + + /// + /// Similar as UniTask.Yield but guaranteed run on next frame. + /// + public static UniTask NextFrame(PlayerLoopTiming timing) + { + return new UniTask(NextFramePromise.Create(timing, CancellationToken.None, out var token), token); + } + + /// + /// Similar as UniTask.Yield but guaranteed run on next frame. + /// + public static UniTask NextFrame(CancellationToken cancellationToken) + { + return new UniTask(NextFramePromise.Create(PlayerLoopTiming.Update, cancellationToken, out var token), token); + } + + /// + /// Similar as UniTask.Yield but guaranteed run on next frame. + /// + public static UniTask NextFrame(PlayerLoopTiming timing, CancellationToken cancellationToken) + { + return new UniTask(NextFramePromise.Create(timing, cancellationToken, out var token), token); + } + + [Obsolete("Use WaitForEndOfFrame(MonoBehaviour) instead or UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate). Equivalent for coroutine's WaitForEndOfFrame requires MonoBehaviour(runner of Coroutine).")] + public static YieldAwaitable WaitForEndOfFrame() + { + return UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate); + } + + [Obsolete("Use WaitForEndOfFrame(MonoBehaviour) instead or UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate). Equivalent for coroutine's WaitForEndOfFrame requires MonoBehaviour(runner of Coroutine).")] + public static UniTask WaitForEndOfFrame(CancellationToken cancellationToken) + { + return UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate, cancellationToken); + } + + public static UniTask WaitForEndOfFrame(MonoBehaviour coroutineRunner, CancellationToken cancellationToken = default) + { + var source = WaitForEndOfFramePromise.Create(coroutineRunner, cancellationToken, out var token); + return new UniTask(source, token); + } + + /// + /// Same as UniTask.Yield(PlayerLoopTiming.FixedUpdate). + /// + public static YieldAwaitable WaitForFixedUpdate() + { + return UniTask.Yield(PlayerLoopTiming.FixedUpdate); + } + + /// + /// Same as UniTask.Yield(PlayerLoopTiming.FixedUpdate, cancellationToken). + /// + public static UniTask WaitForFixedUpdate(CancellationToken cancellationToken) + { + return UniTask.Yield(PlayerLoopTiming.FixedUpdate, cancellationToken); + } + + public static UniTask DelayFrame(int delayFrameCount, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + if (delayFrameCount < 0) + { + throw new ArgumentOutOfRangeException("Delay does not allow minus delayFrameCount. delayFrameCount:" + delayFrameCount); + } + + return new UniTask(DelayFramePromise.Create(delayFrameCount, delayTiming, cancellationToken, out var token), token); + } + + public static UniTask Delay(int millisecondsDelay, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + var delayTimeSpan = TimeSpan.FromMilliseconds(millisecondsDelay); + return Delay(delayTimeSpan, ignoreTimeScale, delayTiming, cancellationToken); + } + + public static UniTask Delay(TimeSpan delayTimeSpan, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + var delayType = ignoreTimeScale ? DelayType.UnscaledDeltaTime : DelayType.DeltaTime; + return Delay(delayTimeSpan, delayType, delayTiming, cancellationToken); + } + + public static UniTask Delay(int millisecondsDelay, DelayType delayType, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + var delayTimeSpan = TimeSpan.FromMilliseconds(millisecondsDelay); + return Delay(delayTimeSpan, delayType, delayTiming, cancellationToken); + } + + public static UniTask Delay(TimeSpan delayTimeSpan, DelayType delayType, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + if (delayTimeSpan < TimeSpan.Zero) + { + throw new ArgumentOutOfRangeException("Delay does not allow minus delayTimeSpan. delayTimeSpan:" + delayTimeSpan); + } + +#if UNITY_EDITOR + // force use Realtime. + if (PlayerLoopHelper.IsMainThread && !UnityEditor.EditorApplication.isPlaying) + { + delayType = DelayType.Realtime; + } +#endif + + switch (delayType) + { + case DelayType.UnscaledDeltaTime: + { + return new UniTask(DelayIgnoreTimeScalePromise.Create(delayTimeSpan, delayTiming, cancellationToken, out var token), token); + } + case DelayType.Realtime: + { + return new UniTask(DelayRealtimePromise.Create(delayTimeSpan, delayTiming, cancellationToken, out var token), token); + } + case DelayType.DeltaTime: + default: + { + return new UniTask(DelayPromise.Create(delayTimeSpan, delayTiming, cancellationToken, out var token), token); + } + } + } + + sealed class YieldPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + YieldPromise nextNode; + public ref YieldPromise NextNode => ref nextNode; + + static YieldPromise() + { + TaskPool.RegisterSizeGetter(typeof(YieldPromise), () => pool.Size); + } + + CancellationToken cancellationToken; + UniTaskCompletionSourceCore core; + + YieldPromise() + { + } + + public static IUniTaskSource Create(PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new YieldPromise(); + } + + + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public void GetResult(short token) + { + try + { + core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + return false; + } + + core.TrySetResult(null); + return false; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + cancellationToken = default; + return pool.TryPush(this); + } + } + + sealed class NextFramePromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + NextFramePromise nextNode; + public ref NextFramePromise NextNode => ref nextNode; + + static NextFramePromise() + { + TaskPool.RegisterSizeGetter(typeof(NextFramePromise), () => pool.Size); + } + + int frameCount; + CancellationToken cancellationToken; + UniTaskCompletionSourceCore core; + + NextFramePromise() + { + } + + public static IUniTaskSource Create(PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new NextFramePromise(); + } + + result.frameCount = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1; + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public void GetResult(short token) + { + try + { + core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + return false; + } + + if (frameCount == Time.frameCount) + { + return true; + } + + core.TrySetResult(AsyncUnit.Default); + return false; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + cancellationToken = default; + return pool.TryPush(this); + } + } + + sealed class WaitForEndOfFramePromise : IUniTaskSource, ITaskPoolNode, System.Collections.IEnumerator + { + static TaskPool pool; + WaitForEndOfFramePromise nextNode; + public ref WaitForEndOfFramePromise NextNode => ref nextNode; + + static WaitForEndOfFramePromise() + { + TaskPool.RegisterSizeGetter(typeof(WaitForEndOfFramePromise), () => pool.Size); + } + + CancellationToken cancellationToken; + UniTaskCompletionSourceCore core; + + WaitForEndOfFramePromise() + { + } + + public static IUniTaskSource Create(MonoBehaviour coroutineRunner, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new WaitForEndOfFramePromise(); + } + + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + coroutineRunner.StartCoroutine(result); + + token = result.core.Version; + return result; + } + + public void GetResult(short token) + { + try + { + core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + Reset(); // Reset Enumerator + cancellationToken = default; + return pool.TryPush(this); + } + + // Coroutine Runner implementation + + static readonly WaitForEndOfFrame waitForEndOfFrameYieldInstruction = new WaitForEndOfFrame(); + bool isFirst = true; + + object IEnumerator.Current => waitForEndOfFrameYieldInstruction; + + bool IEnumerator.MoveNext() + { + if (isFirst) + { + isFirst = false; + return true; // start WaitForEndOfFrame + } + + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + return false; + } + + core.TrySetResult(null); + return false; + } + + public void Reset() + { + isFirst = true; + } + } + + sealed class DelayFramePromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + DelayFramePromise nextNode; + public ref DelayFramePromise NextNode => ref nextNode; + + static DelayFramePromise() + { + TaskPool.RegisterSizeGetter(typeof(DelayFramePromise), () => pool.Size); + } + + int initialFrame; + int delayFrameCount; + CancellationToken cancellationToken; + + int currentFrameCount; + UniTaskCompletionSourceCore core; + + DelayFramePromise() + { + } + + public static IUniTaskSource Create(int delayFrameCount, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new DelayFramePromise(); + } + + result.delayFrameCount = delayFrameCount; + result.cancellationToken = cancellationToken; + result.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public void GetResult(short token) + { + try + { + core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + return false; + } + + if (currentFrameCount == 0) + { + if (delayFrameCount == 0) // same as Yield + { + core.TrySetResult(AsyncUnit.Default); + return false; + } + + // skip in initial frame. + if (initialFrame == Time.frameCount) + { +#if UNITY_EDITOR + // force use Realtime. + if (PlayerLoopHelper.IsMainThread && !UnityEditor.EditorApplication.isPlaying) + { + //goto ++currentFrameCount + } + else + { + return true; + } +#else + return true; +#endif + } + } + + if (++currentFrameCount >= delayFrameCount) + { + core.TrySetResult(AsyncUnit.Default); + return false; + } + + return true; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + currentFrameCount = default; + delayFrameCount = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + + sealed class DelayPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + DelayPromise nextNode; + public ref DelayPromise NextNode => ref nextNode; + + static DelayPromise() + { + TaskPool.RegisterSizeGetter(typeof(DelayPromise), () => pool.Size); + } + + int initialFrame; + float delayTimeSpan; + float elapsed; + CancellationToken cancellationToken; + + UniTaskCompletionSourceCore core; + + DelayPromise() + { + } + + public static IUniTaskSource Create(TimeSpan delayTimeSpan, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new DelayPromise(); + } + + result.elapsed = 0.0f; + result.delayTimeSpan = (float)delayTimeSpan.TotalSeconds; + result.cancellationToken = cancellationToken; + result.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public void GetResult(short token) + { + try + { + core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + return false; + } + + if (elapsed == 0.0f) + { + if (initialFrame == Time.frameCount) + { + return true; + } + } + + elapsed += Time.deltaTime; + if (elapsed >= delayTimeSpan) + { + core.TrySetResult(null); + return false; + } + + return true; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + delayTimeSpan = default; + elapsed = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + + sealed class DelayIgnoreTimeScalePromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + DelayIgnoreTimeScalePromise nextNode; + public ref DelayIgnoreTimeScalePromise NextNode => ref nextNode; + + static DelayIgnoreTimeScalePromise() + { + TaskPool.RegisterSizeGetter(typeof(DelayIgnoreTimeScalePromise), () => pool.Size); + } + + float delayFrameTimeSpan; + float elapsed; + int initialFrame; + CancellationToken cancellationToken; + + UniTaskCompletionSourceCore core; + + DelayIgnoreTimeScalePromise() + { + } + + public static IUniTaskSource Create(TimeSpan delayFrameTimeSpan, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new DelayIgnoreTimeScalePromise(); + } + + result.elapsed = 0.0f; + result.delayFrameTimeSpan = (float)delayFrameTimeSpan.TotalSeconds; + result.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1; + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public void GetResult(short token) + { + try + { + core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + return false; + } + + if (elapsed == 0.0f) + { + if (initialFrame == Time.frameCount) + { + return true; + } + } + + elapsed += Time.unscaledDeltaTime; + if (elapsed >= delayFrameTimeSpan) + { + core.TrySetResult(null); + return false; + } + + return true; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + delayFrameTimeSpan = default; + elapsed = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + + sealed class DelayRealtimePromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + DelayRealtimePromise nextNode; + public ref DelayRealtimePromise NextNode => ref nextNode; + + static DelayRealtimePromise() + { + TaskPool.RegisterSizeGetter(typeof(DelayRealtimePromise), () => pool.Size); + } + + long delayTimeSpanTicks; + ValueStopwatch stopwatch; + CancellationToken cancellationToken; + + UniTaskCompletionSourceCore core; + + DelayRealtimePromise() + { + } + + public static IUniTaskSource Create(TimeSpan delayTimeSpan, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new DelayRealtimePromise(); + } + + result.stopwatch = ValueStopwatch.StartNew(); + result.delayTimeSpanTicks = delayTimeSpan.Ticks; + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public void GetResult(short token) + { + try + { + core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + return false; + } + + if (stopwatch.IsInvalid) + { + core.TrySetResult(AsyncUnit.Default); + return false; + } + + if (stopwatch.ElapsedTicks >= delayTimeSpanTicks) + { + core.TrySetResult(AsyncUnit.Default); + return false; + } + + return true; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + stopwatch = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + } + + public readonly struct YieldAwaitable + { + readonly PlayerLoopTiming timing; + + public YieldAwaitable(PlayerLoopTiming timing) + { + this.timing = timing; + } + + public Awaiter GetAwaiter() + { + return new Awaiter(timing); + } + + public UniTask ToUniTask() + { + return UniTask.Yield(timing, CancellationToken.None); + } + + public readonly struct Awaiter : ICriticalNotifyCompletion + { + readonly PlayerLoopTiming timing; + + public Awaiter(PlayerLoopTiming timing) + { + this.timing = timing; + } + + public bool IsCompleted => false; + + public void GetResult() { } + + public void OnCompleted(Action continuation) + { + PlayerLoopHelper.AddContinuation(timing, continuation); + } + + public void UnsafeOnCompleted(Action continuation) + { + PlayerLoopHelper.AddContinuation(timing, continuation); + } + } + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Delay.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Delay.cs.meta new file mode 100644 index 0000000..08ce579 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Delay.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ecff7972251de0848b2c0fa89bbd3489 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Factory.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Factory.cs new file mode 100644 index 0000000..2f6a6a9 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Factory.cs @@ -0,0 +1,499 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Runtime.CompilerServices; +using System.Runtime.ExceptionServices; +using System.Threading; + +namespace Cysharp.Threading.Tasks +{ + public partial struct UniTask + { + static readonly UniTask CanceledUniTask = new Func(() => + { + return new UniTask(new CanceledResultSource(CancellationToken.None), 0); + })(); + + static class CanceledUniTaskCache + { + public static readonly UniTask Task; + + static CanceledUniTaskCache() + { + Task = new UniTask(new CanceledResultSource(CancellationToken.None), 0); + } + } + + public static readonly UniTask CompletedTask = new UniTask(); + + public static UniTask FromException(Exception ex) + { + if (ex is OperationCanceledException oce) + { + return FromCanceled(oce.CancellationToken); + } + + return new UniTask(new ExceptionResultSource(ex), 0); + } + + public static UniTask FromException(Exception ex) + { + if (ex is OperationCanceledException oce) + { + return FromCanceled(oce.CancellationToken); + } + + return new UniTask(new ExceptionResultSource(ex), 0); + } + + public static UniTask FromResult(T value) + { + return new UniTask(value); + } + + public static UniTask FromCanceled(CancellationToken cancellationToken = default) + { + if (cancellationToken == CancellationToken.None) + { + return CanceledUniTask; + } + else + { + return new UniTask(new CanceledResultSource(cancellationToken), 0); + } + } + + public static UniTask FromCanceled(CancellationToken cancellationToken = default) + { + if (cancellationToken == CancellationToken.None) + { + return CanceledUniTaskCache.Task; + } + else + { + return new UniTask(new CanceledResultSource(cancellationToken), 0); + } + } + + public static UniTask Create(Func factory) + { + return factory(); + } + + public static UniTask Create(Func> factory) + { + return factory(); + } + + public static AsyncLazy Lazy(Func factory) + { + return new AsyncLazy(factory); + } + + public static AsyncLazy Lazy(Func> factory) + { + return new AsyncLazy(factory); + } + + /// + /// helper of fire and forget void action. + /// + public static void Void(Func asyncAction) + { + asyncAction().Forget(); + } + + /// + /// helper of fire and forget void action. + /// + public static void Void(Func asyncAction, CancellationToken cancellationToken) + { + asyncAction(cancellationToken).Forget(); + } + + /// + /// helper of fire and forget void action. + /// + public static void Void(Func asyncAction, T state) + { + asyncAction(state).Forget(); + } + + /// + /// helper of create add UniTaskVoid to delegate. + /// For example: FooAction = UniTask.Action(async () => { /* */ }) + /// + public static Action Action(Func asyncAction) + { + return () => asyncAction().Forget(); + } + + /// + /// helper of create add UniTaskVoid to delegate. + /// + public static Action Action(Func asyncAction, CancellationToken cancellationToken) + { + return () => asyncAction(cancellationToken).Forget(); + } + +#if UNITY_2018_3_OR_NEWER + + /// + /// Create async void(UniTaskVoid) UnityAction. + /// For exampe: onClick.AddListener(UniTask.UnityAction(async () => { /* */ } )) + /// + public static UnityEngine.Events.UnityAction UnityAction(Func asyncAction) + { + return () => asyncAction().Forget(); + } + + /// + /// Create async void(UniTaskVoid) UnityAction. + /// For exampe: onClick.AddListener(UniTask.UnityAction(FooAsync, this.GetCancellationTokenOnDestroy())) + /// + public static UnityEngine.Events.UnityAction UnityAction(Func asyncAction, CancellationToken cancellationToken) + { + return () => asyncAction(cancellationToken).Forget(); + } + +#endif + + /// + /// Defer the task creation just before call await. + /// + public static UniTask Defer(Func factory) + { + return new UniTask(new DeferPromise(factory), 0); + } + + /// + /// Defer the task creation just before call await. + /// + public static UniTask Defer(Func> factory) + { + return new UniTask(new DeferPromise(factory), 0); + } + + /// + /// Never complete. + /// + public static UniTask Never(CancellationToken cancellationToken) + { + return new UniTask(new NeverPromise(cancellationToken), 0); + } + + /// + /// Never complete. + /// + public static UniTask Never(CancellationToken cancellationToken) + { + return new UniTask(new NeverPromise(cancellationToken), 0); + } + + sealed class ExceptionResultSource : IUniTaskSource + { + readonly ExceptionDispatchInfo exception; + bool calledGet; + + public ExceptionResultSource(Exception exception) + { + this.exception = ExceptionDispatchInfo.Capture(exception); + } + + public void GetResult(short token) + { + if (!calledGet) + { + calledGet = true; + GC.SuppressFinalize(this); + } + exception.Throw(); + } + + public UniTaskStatus GetStatus(short token) + { + return UniTaskStatus.Faulted; + } + + public UniTaskStatus UnsafeGetStatus() + { + return UniTaskStatus.Faulted; + } + + public void OnCompleted(Action continuation, object state, short token) + { + continuation(state); + } + + ~ExceptionResultSource() + { + if (!calledGet) + { + UniTaskScheduler.PublishUnobservedTaskException(exception.SourceException); + } + } + } + + sealed class ExceptionResultSource : IUniTaskSource + { + readonly ExceptionDispatchInfo exception; + bool calledGet; + + public ExceptionResultSource(Exception exception) + { + this.exception = ExceptionDispatchInfo.Capture(exception); + } + + public T GetResult(short token) + { + if (!calledGet) + { + calledGet = true; + GC.SuppressFinalize(this); + } + exception.Throw(); + return default; + } + + void IUniTaskSource.GetResult(short token) + { + if (!calledGet) + { + calledGet = true; + GC.SuppressFinalize(this); + } + exception.Throw(); + } + + public UniTaskStatus GetStatus(short token) + { + return UniTaskStatus.Faulted; + } + + public UniTaskStatus UnsafeGetStatus() + { + return UniTaskStatus.Faulted; + } + + public void OnCompleted(Action continuation, object state, short token) + { + continuation(state); + } + + ~ExceptionResultSource() + { + if (!calledGet) + { + UniTaskScheduler.PublishUnobservedTaskException(exception.SourceException); + } + } + } + + sealed class CanceledResultSource : IUniTaskSource + { + readonly CancellationToken cancellationToken; + + public CanceledResultSource(CancellationToken cancellationToken) + { + this.cancellationToken = cancellationToken; + } + + public void GetResult(short token) + { + throw new OperationCanceledException(cancellationToken); + } + + public UniTaskStatus GetStatus(short token) + { + return UniTaskStatus.Canceled; + } + + public UniTaskStatus UnsafeGetStatus() + { + return UniTaskStatus.Canceled; + } + + public void OnCompleted(Action continuation, object state, short token) + { + continuation(state); + } + } + + sealed class CanceledResultSource : IUniTaskSource + { + readonly CancellationToken cancellationToken; + + public CanceledResultSource(CancellationToken cancellationToken) + { + this.cancellationToken = cancellationToken; + } + + public T GetResult(short token) + { + throw new OperationCanceledException(cancellationToken); + } + + void IUniTaskSource.GetResult(short token) + { + throw new OperationCanceledException(cancellationToken); + } + + public UniTaskStatus GetStatus(short token) + { + return UniTaskStatus.Canceled; + } + + public UniTaskStatus UnsafeGetStatus() + { + return UniTaskStatus.Canceled; + } + + public void OnCompleted(Action continuation, object state, short token) + { + continuation(state); + } + } + + sealed class DeferPromise : IUniTaskSource + { + Func factory; + UniTask task; + UniTask.Awaiter awaiter; + + public DeferPromise(Func factory) + { + this.factory = factory; + } + + public void GetResult(short token) + { + awaiter.GetResult(); + } + + public UniTaskStatus GetStatus(short token) + { + var f = Interlocked.Exchange(ref factory, null); + if (f != null) + { + task = f(); + awaiter = task.GetAwaiter(); + } + + return task.Status; + } + + public void OnCompleted(Action continuation, object state, short token) + { + awaiter.SourceOnCompleted(continuation, state); + } + + public UniTaskStatus UnsafeGetStatus() + { + return task.Status; + } + } + + sealed class DeferPromise : IUniTaskSource + { + Func> factory; + UniTask task; + UniTask.Awaiter awaiter; + + public DeferPromise(Func> factory) + { + this.factory = factory; + } + + public T GetResult(short token) + { + return awaiter.GetResult(); + } + + void IUniTaskSource.GetResult(short token) + { + awaiter.GetResult(); + } + + public UniTaskStatus GetStatus(short token) + { + var f = Interlocked.Exchange(ref factory, null); + if (f != null) + { + task = f(); + awaiter = task.GetAwaiter(); + } + + return task.Status; + } + + public void OnCompleted(Action continuation, object state, short token) + { + awaiter.SourceOnCompleted(continuation, state); + } + + public UniTaskStatus UnsafeGetStatus() + { + return task.Status; + } + } + + sealed class NeverPromise : IUniTaskSource + { + static readonly Action cancellationCallback = CancellationCallback; + + CancellationToken cancellationToken; + UniTaskCompletionSourceCore core; + + public NeverPromise(CancellationToken cancellationToken) + { + this.cancellationToken = cancellationToken; + if (this.cancellationToken.CanBeCanceled) + { + this.cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this); + } + } + + static void CancellationCallback(object state) + { + var self = (NeverPromise)state; + self.core.TrySetCanceled(self.cancellationToken); + } + + public T GetResult(short token) + { + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + void IUniTaskSource.GetResult(short token) + { + core.GetResult(token); + } + } + } + + internal static class CompletedTasks + { + public static readonly UniTask AsyncUnit = UniTask.FromResult(Cysharp.Threading.Tasks.AsyncUnit.Default); + public static readonly UniTask True = UniTask.FromResult(true); + public static readonly UniTask False = UniTask.FromResult(false); + public static readonly UniTask Zero = UniTask.FromResult(0); + public static readonly UniTask MinusOne = UniTask.FromResult(-1); + public static readonly UniTask One = UniTask.FromResult(1); + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Factory.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Factory.cs.meta new file mode 100644 index 0000000..31bc0c9 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Factory.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4e12b66d6b9bd7845b04a594cbe386b4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Run.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Run.cs new file mode 100644 index 0000000..ac3e795 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Run.cs @@ -0,0 +1,289 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks +{ + public partial struct UniTask + { + #region OBSOLETE_RUN + + [Obsolete("UniTask.Run is similar as Task.Run, it uses ThreadPool. For equivalent behaviour, use UniTask.RunOnThreadPool instead. If you don't want to use ThreadPool, you can use UniTask.Void(async void) or UniTask.Create(async UniTask) too.")] + public static UniTask Run(Action action, bool configureAwait = true, CancellationToken cancellationToken = default) + { + return RunOnThreadPool(action, configureAwait, cancellationToken); + } + + [Obsolete("UniTask.Run is similar as Task.Run, it uses ThreadPool. For equivalent behaviour, use UniTask.RunOnThreadPool instead. If you don't want to use ThreadPool, you can use UniTask.Void(async void) or UniTask.Create(async UniTask) too.")] + public static UniTask Run(Action action, object state, bool configureAwait = true, CancellationToken cancellationToken = default) + { + return RunOnThreadPool(action, state, configureAwait, cancellationToken); + } + + [Obsolete("UniTask.Run is similar as Task.Run, it uses ThreadPool. For equivalent behaviour, use UniTask.RunOnThreadPool instead. If you don't want to use ThreadPool, you can use UniTask.Void(async void) or UniTask.Create(async UniTask) too.")] + public static UniTask Run(Func action, bool configureAwait = true, CancellationToken cancellationToken = default) + { + return RunOnThreadPool(action, configureAwait, cancellationToken); + } + + [Obsolete("UniTask.Run is similar as Task.Run, it uses ThreadPool. For equivalent behaviour, use UniTask.RunOnThreadPool instead. If you don't want to use ThreadPool, you can use UniTask.Void(async void) or UniTask.Create(async UniTask) too.")] + public static UniTask Run(Func action, object state, bool configureAwait = true, CancellationToken cancellationToken = default) + { + return RunOnThreadPool(action, state, configureAwait, cancellationToken); + } + + [Obsolete("UniTask.Run is similar as Task.Run, it uses ThreadPool. For equivalent behaviour, use UniTask.RunOnThreadPool instead. If you don't want to use ThreadPool, you can use UniTask.Void(async void) or UniTask.Create(async UniTask) too.")] + public static UniTask Run(Func func, bool configureAwait = true, CancellationToken cancellationToken = default) + { + return RunOnThreadPool(func, configureAwait, cancellationToken); + } + + [Obsolete("UniTask.Run is similar as Task.Run, it uses ThreadPool. For equivalent behaviour, use UniTask.RunOnThreadPool instead. If you don't want to use ThreadPool, you can use UniTask.Void(async void) or UniTask.Create(async UniTask) too.")] + public static UniTask Run(Func> func, bool configureAwait = true, CancellationToken cancellationToken = default) + { + return RunOnThreadPool(func, configureAwait, cancellationToken); + } + + [Obsolete("UniTask.Run is similar as Task.Run, it uses ThreadPool. For equivalent behaviour, use UniTask.RunOnThreadPool instead. If you don't want to use ThreadPool, you can use UniTask.Void(async void) or UniTask.Create(async UniTask) too.")] + public static UniTask Run(Func func, object state, bool configureAwait = true, CancellationToken cancellationToken = default) + { + return RunOnThreadPool(func, state, configureAwait, cancellationToken); + } + + [Obsolete("UniTask.Run is similar as Task.Run, it uses ThreadPool. For equivalent behaviour, use UniTask.RunOnThreadPool instead. If you don't want to use ThreadPool, you can use UniTask.Void(async void) or UniTask.Create(async UniTask) too.")] + public static UniTask Run(Func> func, object state, bool configureAwait = true, CancellationToken cancellationToken = default) + { + return RunOnThreadPool(func, state, configureAwait, cancellationToken); + } + + #endregion + + /// Run action on the threadPool and return to main thread if configureAwait = true. + public static async UniTask RunOnThreadPool(Action action, bool configureAwait = true, CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + await UniTask.SwitchToThreadPool(); + + cancellationToken.ThrowIfCancellationRequested(); + + if (configureAwait) + { + try + { + action(); + } + finally + { + await UniTask.Yield(); + } + } + else + { + action(); + } + + cancellationToken.ThrowIfCancellationRequested(); + } + + /// Run action on the threadPool and return to main thread if configureAwait = true. + public static async UniTask RunOnThreadPool(Action action, object state, bool configureAwait = true, CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + await UniTask.SwitchToThreadPool(); + + cancellationToken.ThrowIfCancellationRequested(); + + if (configureAwait) + { + try + { + action(state); + } + finally + { + await UniTask.Yield(); + } + } + else + { + action(state); + } + + cancellationToken.ThrowIfCancellationRequested(); + } + + /// Run action on the threadPool and return to main thread if configureAwait = true. + public static async UniTask RunOnThreadPool(Func action, bool configureAwait = true, CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + await UniTask.SwitchToThreadPool(); + + cancellationToken.ThrowIfCancellationRequested(); + + if (configureAwait) + { + try + { + await action(); + } + finally + { + await UniTask.Yield(); + } + } + else + { + await action(); + } + + cancellationToken.ThrowIfCancellationRequested(); + } + + /// Run action on the threadPool and return to main thread if configureAwait = true. + public static async UniTask RunOnThreadPool(Func action, object state, bool configureAwait = true, CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + await UniTask.SwitchToThreadPool(); + + cancellationToken.ThrowIfCancellationRequested(); + + if (configureAwait) + { + try + { + await action(state); + } + finally + { + await UniTask.Yield(); + } + } + else + { + await action(state); + } + + cancellationToken.ThrowIfCancellationRequested(); + } + + /// Run action on the threadPool and return to main thread if configureAwait = true. + public static async UniTask RunOnThreadPool(Func func, bool configureAwait = true, CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + await UniTask.SwitchToThreadPool(); + + cancellationToken.ThrowIfCancellationRequested(); + + if (configureAwait) + { + try + { + return func(); + } + finally + { + await UniTask.Yield(); + cancellationToken.ThrowIfCancellationRequested(); + } + } + else + { + return func(); + } + } + + /// Run action on the threadPool and return to main thread if configureAwait = true. + public static async UniTask RunOnThreadPool(Func> func, bool configureAwait = true, CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + await UniTask.SwitchToThreadPool(); + + cancellationToken.ThrowIfCancellationRequested(); + + if (configureAwait) + { + try + { + return await func(); + } + finally + { + cancellationToken.ThrowIfCancellationRequested(); + await UniTask.Yield(); + cancellationToken.ThrowIfCancellationRequested(); + } + } + else + { + var result = await func(); + cancellationToken.ThrowIfCancellationRequested(); + return result; + } + } + + /// Run action on the threadPool and return to main thread if configureAwait = true. + public static async UniTask RunOnThreadPool(Func func, object state, bool configureAwait = true, CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + await UniTask.SwitchToThreadPool(); + + cancellationToken.ThrowIfCancellationRequested(); + + if (configureAwait) + { + try + { + return func(state); + } + finally + { + await UniTask.Yield(); + cancellationToken.ThrowIfCancellationRequested(); + } + } + else + { + return func(state); + } + } + + /// Run action on the threadPool and return to main thread if configureAwait = true. + public static async UniTask RunOnThreadPool(Func> func, object state, bool configureAwait = true, CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + await UniTask.SwitchToThreadPool(); + + cancellationToken.ThrowIfCancellationRequested(); + + if (configureAwait) + { + try + { + return await func(state); + } + finally + { + cancellationToken.ThrowIfCancellationRequested(); + await UniTask.Yield(); + cancellationToken.ThrowIfCancellationRequested(); + } + } + else + { + var result = await func(state); + cancellationToken.ThrowIfCancellationRequested(); + return result; + } + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Run.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Run.cs.meta new file mode 100644 index 0000000..9a780ae --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Run.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8473162fc285a5f44bcca90f7da073e7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Threading.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Threading.cs new file mode 100644 index 0000000..71d6aec --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Threading.cs @@ -0,0 +1,412 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Runtime.CompilerServices; +using System.Threading; +using System.Threading.Tasks; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks +{ + public partial struct UniTask + { +#if UNITY_2018_3_OR_NEWER + + /// + /// If running on mainthread, do nothing. Otherwise, same as UniTask.Yield(PlayerLoopTiming.Update). + /// + public static SwitchToMainThreadAwaitable SwitchToMainThread(CancellationToken cancellationToken = default) + { + return new SwitchToMainThreadAwaitable(PlayerLoopTiming.Update, cancellationToken); + } + + /// + /// If running on mainthread, do nothing. Otherwise, same as UniTask.Yield(timing). + /// + public static SwitchToMainThreadAwaitable SwitchToMainThread(PlayerLoopTiming timing, CancellationToken cancellationToken = default) + { + return new SwitchToMainThreadAwaitable(timing, cancellationToken); + } + + /// + /// Return to mainthread(same as await SwitchToMainThread) after using scope is closed. + /// + public static ReturnToMainThread ReturnToMainThread(CancellationToken cancellationToken = default) + { + return new ReturnToMainThread(PlayerLoopTiming.Update, cancellationToken); + } + + /// + /// Return to mainthread(same as await SwitchToMainThread) after using scope is closed. + /// + public static ReturnToMainThread ReturnToMainThread(PlayerLoopTiming timing, CancellationToken cancellationToken = default) + { + return new ReturnToMainThread(timing, cancellationToken); + } + + /// + /// Queue the action to PlayerLoop. + /// + public static void Post(Action action, PlayerLoopTiming timing = PlayerLoopTiming.Update) + { + PlayerLoopHelper.AddContinuation(timing, action); + } + +#endif + + public static SwitchToThreadPoolAwaitable SwitchToThreadPool() + { + return new SwitchToThreadPoolAwaitable(); + } + + /// + /// Note: use SwitchToThreadPool is recommended. + /// + public static SwitchToTaskPoolAwaitable SwitchToTaskPool() + { + return new SwitchToTaskPoolAwaitable(); + } + + public static SwitchToSynchronizationContextAwaitable SwitchToSynchronizationContext(SynchronizationContext synchronizationContext, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(synchronizationContext, nameof(synchronizationContext)); + return new SwitchToSynchronizationContextAwaitable(synchronizationContext, cancellationToken); + } + + public static ReturnToSynchronizationContext ReturnToSynchronizationContext(SynchronizationContext synchronizationContext, CancellationToken cancellationToken = default) + { + return new ReturnToSynchronizationContext(synchronizationContext, false, cancellationToken); + } + + public static ReturnToSynchronizationContext ReturnToCurrentSynchronizationContext(bool dontPostWhenSameContext = true, CancellationToken cancellationToken = default) + { + return new ReturnToSynchronizationContext(SynchronizationContext.Current, dontPostWhenSameContext, cancellationToken); + } + } + +#if UNITY_2018_3_OR_NEWER + + public struct SwitchToMainThreadAwaitable + { + readonly PlayerLoopTiming playerLoopTiming; + readonly CancellationToken cancellationToken; + + public SwitchToMainThreadAwaitable(PlayerLoopTiming playerLoopTiming, CancellationToken cancellationToken) + { + this.playerLoopTiming = playerLoopTiming; + this.cancellationToken = cancellationToken; + } + + public Awaiter GetAwaiter() => new Awaiter(playerLoopTiming, cancellationToken); + + public struct Awaiter : ICriticalNotifyCompletion + { + readonly PlayerLoopTiming playerLoopTiming; + readonly CancellationToken cancellationToken; + + public Awaiter(PlayerLoopTiming playerLoopTiming, CancellationToken cancellationToken) + { + this.playerLoopTiming = playerLoopTiming; + this.cancellationToken = cancellationToken; + } + + public bool IsCompleted + { + get + { + var currentThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId; + if (PlayerLoopHelper.MainThreadId == currentThreadId) + { + return true; // run immediate. + } + else + { + return false; // register continuation. + } + } + } + + public void GetResult() { cancellationToken.ThrowIfCancellationRequested(); } + + public void OnCompleted(Action continuation) + { + PlayerLoopHelper.AddContinuation(playerLoopTiming, continuation); + } + + public void UnsafeOnCompleted(Action continuation) + { + PlayerLoopHelper.AddContinuation(playerLoopTiming, continuation); + } + } + } + + public struct ReturnToMainThread + { + readonly PlayerLoopTiming playerLoopTiming; + readonly CancellationToken cancellationToken; + + public ReturnToMainThread(PlayerLoopTiming playerLoopTiming, CancellationToken cancellationToken) + { + this.playerLoopTiming = playerLoopTiming; + this.cancellationToken = cancellationToken; + } + + public Awaiter DisposeAsync() + { + return new Awaiter(playerLoopTiming, cancellationToken); // run immediate. + } + + public readonly struct Awaiter : ICriticalNotifyCompletion + { + readonly PlayerLoopTiming timing; + readonly CancellationToken cancellationToken; + + public Awaiter(PlayerLoopTiming timing, CancellationToken cancellationToken) + { + this.timing = timing; + this.cancellationToken = cancellationToken; + } + + public Awaiter GetAwaiter() => this; + + public bool IsCompleted => PlayerLoopHelper.MainThreadId == System.Threading.Thread.CurrentThread.ManagedThreadId; + + public void GetResult() { cancellationToken.ThrowIfCancellationRequested(); } + + public void OnCompleted(Action continuation) + { + PlayerLoopHelper.AddContinuation(timing, continuation); + } + + public void UnsafeOnCompleted(Action continuation) + { + PlayerLoopHelper.AddContinuation(timing, continuation); + } + } + } + +#endif + + public struct SwitchToThreadPoolAwaitable + { + public Awaiter GetAwaiter() => new Awaiter(); + + public struct Awaiter : ICriticalNotifyCompletion + { + static readonly WaitCallback switchToCallback = Callback; + + public bool IsCompleted => false; + public void GetResult() { } + + public void OnCompleted(Action continuation) + { + ThreadPool.QueueUserWorkItem(switchToCallback, continuation); + } + + public void UnsafeOnCompleted(Action continuation) + { +#if NETCOREAPP3_1 + ThreadPool.UnsafeQueueUserWorkItem(ThreadPoolWorkItem.Create(continuation), false); +#else + ThreadPool.UnsafeQueueUserWorkItem(switchToCallback, continuation); +#endif + } + + static void Callback(object state) + { + var continuation = (Action)state; + continuation(); + } + } + +#if NETCOREAPP3_1 + + sealed class ThreadPoolWorkItem : IThreadPoolWorkItem, ITaskPoolNode + { + static TaskPool pool; + ThreadPoolWorkItem nextNode; + public ref ThreadPoolWorkItem NextNode => ref nextNode; + + static ThreadPoolWorkItem() + { + TaskPool.RegisterSizeGetter(typeof(ThreadPoolWorkItem), () => pool.Size); + } + + Action continuation; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ThreadPoolWorkItem Create(Action continuation) + { + if (!pool.TryPop(out var item)) + { + item = new ThreadPoolWorkItem(); + } + + item.continuation = continuation; + return item; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Execute() + { + var call = continuation; + continuation = null; + if (call != null) + { + pool.TryPush(this); + call.Invoke(); + } + } + } + +#endif + } + + public struct SwitchToTaskPoolAwaitable + { + public Awaiter GetAwaiter() => new Awaiter(); + + public struct Awaiter : ICriticalNotifyCompletion + { + static readonly Action switchToCallback = Callback; + + public bool IsCompleted => false; + public void GetResult() { } + + public void OnCompleted(Action continuation) + { + Task.Factory.StartNew(switchToCallback, continuation, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + } + + public void UnsafeOnCompleted(Action continuation) + { + Task.Factory.StartNew(switchToCallback, continuation, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + } + + static void Callback(object state) + { + var continuation = (Action)state; + continuation(); + } + } + } + + public struct SwitchToSynchronizationContextAwaitable + { + readonly SynchronizationContext synchronizationContext; + readonly CancellationToken cancellationToken; + + public SwitchToSynchronizationContextAwaitable(SynchronizationContext synchronizationContext, CancellationToken cancellationToken) + { + this.synchronizationContext = synchronizationContext; + this.cancellationToken = cancellationToken; + } + + public Awaiter GetAwaiter() => new Awaiter(synchronizationContext, cancellationToken); + + public struct Awaiter : ICriticalNotifyCompletion + { + static readonly SendOrPostCallback switchToCallback = Callback; + readonly SynchronizationContext synchronizationContext; + readonly CancellationToken cancellationToken; + + public Awaiter(SynchronizationContext synchronizationContext, CancellationToken cancellationToken) + { + this.synchronizationContext = synchronizationContext; + this.cancellationToken = cancellationToken; + } + + public bool IsCompleted => false; + public void GetResult() { cancellationToken.ThrowIfCancellationRequested(); } + + public void OnCompleted(Action continuation) + { + synchronizationContext.Post(switchToCallback, continuation); + } + + public void UnsafeOnCompleted(Action continuation) + { + synchronizationContext.Post(switchToCallback, continuation); + } + + static void Callback(object state) + { + var continuation = (Action)state; + continuation(); + } + } + } + + public struct ReturnToSynchronizationContext + { + readonly SynchronizationContext syncContext; + readonly bool dontPostWhenSameContext; + readonly CancellationToken cancellationToken; + + public ReturnToSynchronizationContext(SynchronizationContext syncContext, bool dontPostWhenSameContext, CancellationToken cancellationToken) + { + this.syncContext = syncContext; + this.dontPostWhenSameContext = dontPostWhenSameContext; + this.cancellationToken = cancellationToken; + } + + public Awaiter DisposeAsync() + { + return new Awaiter(syncContext, dontPostWhenSameContext, cancellationToken); + } + + public struct Awaiter : ICriticalNotifyCompletion + { + static readonly SendOrPostCallback switchToCallback = Callback; + + readonly SynchronizationContext synchronizationContext; + readonly bool dontPostWhenSameContext; + readonly CancellationToken cancellationToken; + + public Awaiter(SynchronizationContext synchronizationContext, bool dontPostWhenSameContext, CancellationToken cancellationToken) + { + this.synchronizationContext = synchronizationContext; + this.dontPostWhenSameContext = dontPostWhenSameContext; + this.cancellationToken = cancellationToken; + } + + public Awaiter GetAwaiter() => this; + + public bool IsCompleted + { + get + { + if (!dontPostWhenSameContext) return false; + + var current = SynchronizationContext.Current; + if (current == synchronizationContext) + { + return true; + } + else + { + return false; + } + } + } + + public void GetResult() { cancellationToken.ThrowIfCancellationRequested(); } + + public void OnCompleted(Action continuation) + { + synchronizationContext.Post(switchToCallback, continuation); + } + + public void UnsafeOnCompleted(Action continuation) + { + synchronizationContext.Post(switchToCallback, continuation); + } + + static void Callback(object state) + { + var continuation = (Action)state; + continuation(); + } + } + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Threading.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Threading.cs.meta new file mode 100644 index 0000000..fa512b8 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.Threading.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4132ea600454134439fa2c7eb931b5e6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WaitUntil.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WaitUntil.cs new file mode 100644 index 0000000..0a09fe0 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WaitUntil.cs @@ -0,0 +1,582 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Collections.Generic; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks +{ + public partial struct UniTask + { + public static UniTask WaitUntil(Func predicate, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + return new UniTask(WaitUntilPromise.Create(predicate, timing, cancellationToken, out var token), token); + } + + public static UniTask WaitWhile(Func predicate, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + return new UniTask(WaitWhilePromise.Create(predicate, timing, cancellationToken, out var token), token); + } + + public static UniTask WaitUntilCanceled(CancellationToken cancellationToken, PlayerLoopTiming timing = PlayerLoopTiming.Update) + { + return new UniTask(WaitUntilCanceledPromise.Create(cancellationToken, timing, out var token), token); + } + + public static UniTask WaitUntilValueChanged(T target, Func monitorFunction, PlayerLoopTiming monitorTiming = PlayerLoopTiming.Update, IEqualityComparer equalityComparer = null, CancellationToken cancellationToken = default(CancellationToken)) + where T : class + { + var unityObject = target as UnityEngine.Object; + var isUnityObject = target is UnityEngine.Object; // don't use (unityObject == null) + + return new UniTask(isUnityObject + ? WaitUntilValueChangedUnityObjectPromise.Create(target, monitorFunction, equalityComparer, monitorTiming, cancellationToken, out var token) + : WaitUntilValueChangedStandardObjectPromise.Create(target, monitorFunction, equalityComparer, monitorTiming, cancellationToken, out token), token); + } + + sealed class WaitUntilPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + WaitUntilPromise nextNode; + public ref WaitUntilPromise NextNode => ref nextNode; + + static WaitUntilPromise() + { + TaskPool.RegisterSizeGetter(typeof(WaitUntilPromise), () => pool.Size); + } + + Func predicate; + CancellationToken cancellationToken; + + UniTaskCompletionSourceCore core; + + WaitUntilPromise() + { + } + + public static IUniTaskSource Create(Func predicate, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new WaitUntilPromise(); + } + + result.predicate = predicate; + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public void GetResult(short token) + { + try + { + core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + return false; + } + + try + { + if (!predicate()) + { + return true; + } + } + catch (Exception ex) + { + core.TrySetException(ex); + return false; + } + + core.TrySetResult(null); + return false; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + predicate = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + + sealed class WaitWhilePromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + WaitWhilePromise nextNode; + public ref WaitWhilePromise NextNode => ref nextNode; + + static WaitWhilePromise() + { + TaskPool.RegisterSizeGetter(typeof(WaitWhilePromise), () => pool.Size); + } + + Func predicate; + CancellationToken cancellationToken; + + UniTaskCompletionSourceCore core; + + WaitWhilePromise() + { + } + + public static IUniTaskSource Create(Func predicate, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new WaitWhilePromise(); + } + + result.predicate = predicate; + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public void GetResult(short token) + { + try + { + core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + return false; + } + + try + { + if (predicate()) + { + return true; + } + } + catch (Exception ex) + { + core.TrySetException(ex); + return false; + } + + core.TrySetResult(null); + return false; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + predicate = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + + sealed class WaitUntilCanceledPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + WaitUntilCanceledPromise nextNode; + public ref WaitUntilCanceledPromise NextNode => ref nextNode; + + static WaitUntilCanceledPromise() + { + TaskPool.RegisterSizeGetter(typeof(WaitUntilCanceledPromise), () => pool.Size); + } + + CancellationToken cancellationToken; + + UniTaskCompletionSourceCore core; + + WaitUntilCanceledPromise() + { + } + + public static IUniTaskSource Create(CancellationToken cancellationToken, PlayerLoopTiming timing, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new WaitUntilCanceledPromise(); + } + + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public void GetResult(short token) + { + try + { + core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { + core.TrySetResult(null); + return false; + } + + return true; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + cancellationToken = default; + return pool.TryPush(this); + } + } + + // where T : UnityEngine.Object, can not add constraint + sealed class WaitUntilValueChangedUnityObjectPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode> + { + static TaskPool> pool; + WaitUntilValueChangedUnityObjectPromise nextNode; + public ref WaitUntilValueChangedUnityObjectPromise NextNode => ref nextNode; + + static WaitUntilValueChangedUnityObjectPromise() + { + TaskPool.RegisterSizeGetter(typeof(WaitUntilValueChangedUnityObjectPromise), () => pool.Size); + } + + T target; + UnityEngine.Object targetAsUnityObject; + U currentValue; + Func monitorFunction; + IEqualityComparer equalityComparer; + CancellationToken cancellationToken; + + UniTaskCompletionSourceCore core; + + WaitUntilValueChangedUnityObjectPromise() + { + } + + public static IUniTaskSource Create(T target, Func monitorFunction, IEqualityComparer equalityComparer, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new WaitUntilValueChangedUnityObjectPromise(); + } + + result.target = target; + result.targetAsUnityObject = target as UnityEngine.Object; + result.monitorFunction = monitorFunction; + result.currentValue = monitorFunction(target); + result.equalityComparer = equalityComparer ?? UnityEqualityComparer.GetDefault(); + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public U GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested || targetAsUnityObject == null) // destroyed = cancel. + { + core.TrySetCanceled(cancellationToken); + return false; + } + + U nextValue = default(U); + try + { + nextValue = monitorFunction(target); + if (equalityComparer.Equals(currentValue, nextValue)) + { + return true; + } + } + catch (Exception ex) + { + core.TrySetException(ex); + return false; + } + + core.TrySetResult(nextValue); + return false; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + target = default; + currentValue = default; + monitorFunction = default; + equalityComparer = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + + sealed class WaitUntilValueChangedStandardObjectPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode> + where T : class + { + static TaskPool> pool; + WaitUntilValueChangedStandardObjectPromise nextNode; + public ref WaitUntilValueChangedStandardObjectPromise NextNode => ref nextNode; + + static WaitUntilValueChangedStandardObjectPromise() + { + TaskPool.RegisterSizeGetter(typeof(WaitUntilValueChangedStandardObjectPromise), () => pool.Size); + } + + WeakReference target; + U currentValue; + Func monitorFunction; + IEqualityComparer equalityComparer; + CancellationToken cancellationToken; + + UniTaskCompletionSourceCore core; + + WaitUntilValueChangedStandardObjectPromise() + { + } + + public static IUniTaskSource Create(T target, Func monitorFunction, IEqualityComparer equalityComparer, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new WaitUntilValueChangedStandardObjectPromise(); + } + + result.target = new WeakReference(target, false); // wrap in WeakReference. + result.monitorFunction = monitorFunction; + result.currentValue = monitorFunction(target); + result.equalityComparer = equalityComparer ?? UnityEqualityComparer.GetDefault(); + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public U GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested || !target.TryGetTarget(out var t)) // doesn't find = cancel. + { + core.TrySetCanceled(cancellationToken); + return false; + } + + U nextValue = default(U); + try + { + nextValue = monitorFunction(t); + if (equalityComparer.Equals(currentValue, nextValue)) + { + return true; + } + } + catch (Exception ex) + { + core.TrySetException(ex); + return false; + } + + core.TrySetResult(nextValue); + return false; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + target = default; + currentValue = default; + monitorFunction = default; + equalityComparer = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WaitUntil.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WaitUntil.cs.meta new file mode 100644 index 0000000..6e64dc7 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WaitUntil.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 87c9c533491903a4288536b5ac173db8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.Generated.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.Generated.cs new file mode 100644 index 0000000..9ef07d6 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.Generated.cs @@ -0,0 +1,5011 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member +using System; +using System.Runtime.CompilerServices; +using System.Runtime.ExceptionServices; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks +{ + public partial struct UniTask + { + + public static UniTask<(T1, T2)> WhenAll(UniTask task1, UniTask task2) + { + if (task1.Status.IsCompletedSuccessfully() && task2.Status.IsCompletedSuccessfully()) + { + return new UniTask<(T1, T2)>((task1.GetAwaiter().GetResult(), task2.GetAwaiter().GetResult())); + } + + return new UniTask<(T1, T2)>(new WhenAllPromise(task1, task2), 0); + } + + sealed class WhenAllPromise : IUniTaskSource<(T1, T2)> + { + T1 t1 = default; + T2 t2 = default; + int completedCount; + UniTaskCompletionSourceCore<(T1, T2)> core; + + public WhenAllPromise(UniTask task1, UniTask task2) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t1 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 2) + { + self.core.TrySetResult((self.t1, self.t2)); + } + } + + static void TryInvokeContinuationT2(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t2 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 2) + { + self.core.TrySetResult((self.t1, self.t2)); + } + } + + + public (T1, T2) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + public static UniTask<(T1, T2, T3)> WhenAll(UniTask task1, UniTask task2, UniTask task3) + { + if (task1.Status.IsCompletedSuccessfully() && task2.Status.IsCompletedSuccessfully() && task3.Status.IsCompletedSuccessfully()) + { + return new UniTask<(T1, T2, T3)>((task1.GetAwaiter().GetResult(), task2.GetAwaiter().GetResult(), task3.GetAwaiter().GetResult())); + } + + return new UniTask<(T1, T2, T3)>(new WhenAllPromise(task1, task2, task3), 0); + } + + sealed class WhenAllPromise : IUniTaskSource<(T1, T2, T3)> + { + T1 t1 = default; + T2 t2 = default; + T3 t3 = default; + int completedCount; + UniTaskCompletionSourceCore<(T1, T2, T3)> core; + + public WhenAllPromise(UniTask task1, UniTask task2, UniTask task3) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t1 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 3) + { + self.core.TrySetResult((self.t1, self.t2, self.t3)); + } + } + + static void TryInvokeContinuationT2(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t2 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 3) + { + self.core.TrySetResult((self.t1, self.t2, self.t3)); + } + } + + static void TryInvokeContinuationT3(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t3 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 3) + { + self.core.TrySetResult((self.t1, self.t2, self.t3)); + } + } + + + public (T1, T2, T3) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + public static UniTask<(T1, T2, T3, T4)> WhenAll(UniTask task1, UniTask task2, UniTask task3, UniTask task4) + { + if (task1.Status.IsCompletedSuccessfully() && task2.Status.IsCompletedSuccessfully() && task3.Status.IsCompletedSuccessfully() && task4.Status.IsCompletedSuccessfully()) + { + return new UniTask<(T1, T2, T3, T4)>((task1.GetAwaiter().GetResult(), task2.GetAwaiter().GetResult(), task3.GetAwaiter().GetResult(), task4.GetAwaiter().GetResult())); + } + + return new UniTask<(T1, T2, T3, T4)>(new WhenAllPromise(task1, task2, task3, task4), 0); + } + + sealed class WhenAllPromise : IUniTaskSource<(T1, T2, T3, T4)> + { + T1 t1 = default; + T2 t2 = default; + T3 t3 = default; + T4 t4 = default; + int completedCount; + UniTaskCompletionSourceCore<(T1, T2, T3, T4)> core; + + public WhenAllPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t1 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 4) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4)); + } + } + + static void TryInvokeContinuationT2(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t2 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 4) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4)); + } + } + + static void TryInvokeContinuationT3(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t3 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 4) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4)); + } + } + + static void TryInvokeContinuationT4(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t4 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 4) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4)); + } + } + + + public (T1, T2, T3, T4) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + public static UniTask<(T1, T2, T3, T4, T5)> WhenAll(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5) + { + if (task1.Status.IsCompletedSuccessfully() && task2.Status.IsCompletedSuccessfully() && task3.Status.IsCompletedSuccessfully() && task4.Status.IsCompletedSuccessfully() && task5.Status.IsCompletedSuccessfully()) + { + return new UniTask<(T1, T2, T3, T4, T5)>((task1.GetAwaiter().GetResult(), task2.GetAwaiter().GetResult(), task3.GetAwaiter().GetResult(), task4.GetAwaiter().GetResult(), task5.GetAwaiter().GetResult())); + } + + return new UniTask<(T1, T2, T3, T4, T5)>(new WhenAllPromise(task1, task2, task3, task4, task5), 0); + } + + sealed class WhenAllPromise : IUniTaskSource<(T1, T2, T3, T4, T5)> + { + T1 t1 = default; + T2 t2 = default; + T3 t3 = default; + T4 t4 = default; + T5 t5 = default; + int completedCount; + UniTaskCompletionSourceCore<(T1, T2, T3, T4, T5)> core; + + public WhenAllPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t1 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 5) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5)); + } + } + + static void TryInvokeContinuationT2(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t2 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 5) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5)); + } + } + + static void TryInvokeContinuationT3(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t3 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 5) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5)); + } + } + + static void TryInvokeContinuationT4(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t4 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 5) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5)); + } + } + + static void TryInvokeContinuationT5(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t5 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 5) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5)); + } + } + + + public (T1, T2, T3, T4, T5) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + public static UniTask<(T1, T2, T3, T4, T5, T6)> WhenAll(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6) + { + if (task1.Status.IsCompletedSuccessfully() && task2.Status.IsCompletedSuccessfully() && task3.Status.IsCompletedSuccessfully() && task4.Status.IsCompletedSuccessfully() && task5.Status.IsCompletedSuccessfully() && task6.Status.IsCompletedSuccessfully()) + { + return new UniTask<(T1, T2, T3, T4, T5, T6)>((task1.GetAwaiter().GetResult(), task2.GetAwaiter().GetResult(), task3.GetAwaiter().GetResult(), task4.GetAwaiter().GetResult(), task5.GetAwaiter().GetResult(), task6.GetAwaiter().GetResult())); + } + + return new UniTask<(T1, T2, T3, T4, T5, T6)>(new WhenAllPromise(task1, task2, task3, task4, task5, task6), 0); + } + + sealed class WhenAllPromise : IUniTaskSource<(T1, T2, T3, T4, T5, T6)> + { + T1 t1 = default; + T2 t2 = default; + T3 t3 = default; + T4 t4 = default; + T5 t5 = default; + T6 t6 = default; + int completedCount; + UniTaskCompletionSourceCore<(T1, T2, T3, T4, T5, T6)> core; + + public WhenAllPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t1 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 6) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6)); + } + } + + static void TryInvokeContinuationT2(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t2 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 6) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6)); + } + } + + static void TryInvokeContinuationT3(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t3 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 6) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6)); + } + } + + static void TryInvokeContinuationT4(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t4 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 6) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6)); + } + } + + static void TryInvokeContinuationT5(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t5 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 6) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6)); + } + } + + static void TryInvokeContinuationT6(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t6 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 6) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6)); + } + } + + + public (T1, T2, T3, T4, T5, T6) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7)> WhenAll(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7) + { + if (task1.Status.IsCompletedSuccessfully() && task2.Status.IsCompletedSuccessfully() && task3.Status.IsCompletedSuccessfully() && task4.Status.IsCompletedSuccessfully() && task5.Status.IsCompletedSuccessfully() && task6.Status.IsCompletedSuccessfully() && task7.Status.IsCompletedSuccessfully()) + { + return new UniTask<(T1, T2, T3, T4, T5, T6, T7)>((task1.GetAwaiter().GetResult(), task2.GetAwaiter().GetResult(), task3.GetAwaiter().GetResult(), task4.GetAwaiter().GetResult(), task5.GetAwaiter().GetResult(), task6.GetAwaiter().GetResult(), task7.GetAwaiter().GetResult())); + } + + return new UniTask<(T1, T2, T3, T4, T5, T6, T7)>(new WhenAllPromise(task1, task2, task3, task4, task5, task6, task7), 0); + } + + sealed class WhenAllPromise : IUniTaskSource<(T1, T2, T3, T4, T5, T6, T7)> + { + T1 t1 = default; + T2 t2 = default; + T3 t3 = default; + T4 t4 = default; + T5 t5 = default; + T6 t6 = default; + T7 t7 = default; + int completedCount; + UniTaskCompletionSourceCore<(T1, T2, T3, T4, T5, T6, T7)> core; + + public WhenAllPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t1 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 7) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7)); + } + } + + static void TryInvokeContinuationT2(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t2 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 7) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7)); + } + } + + static void TryInvokeContinuationT3(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t3 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 7) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7)); + } + } + + static void TryInvokeContinuationT4(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t4 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 7) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7)); + } + } + + static void TryInvokeContinuationT5(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t5 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 7) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7)); + } + } + + static void TryInvokeContinuationT6(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t6 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 7) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7)); + } + } + + static void TryInvokeContinuationT7(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t7 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 7) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7)); + } + } + + + public (T1, T2, T3, T4, T5, T6, T7) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8)> WhenAll(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8) + { + if (task1.Status.IsCompletedSuccessfully() && task2.Status.IsCompletedSuccessfully() && task3.Status.IsCompletedSuccessfully() && task4.Status.IsCompletedSuccessfully() && task5.Status.IsCompletedSuccessfully() && task6.Status.IsCompletedSuccessfully() && task7.Status.IsCompletedSuccessfully() && task8.Status.IsCompletedSuccessfully()) + { + return new UniTask<(T1, T2, T3, T4, T5, T6, T7, T8)>((task1.GetAwaiter().GetResult(), task2.GetAwaiter().GetResult(), task3.GetAwaiter().GetResult(), task4.GetAwaiter().GetResult(), task5.GetAwaiter().GetResult(), task6.GetAwaiter().GetResult(), task7.GetAwaiter().GetResult(), task8.GetAwaiter().GetResult())); + } + + return new UniTask<(T1, T2, T3, T4, T5, T6, T7, T8)>(new WhenAllPromise(task1, task2, task3, task4, task5, task6, task7, task8), 0); + } + + sealed class WhenAllPromise : IUniTaskSource<(T1, T2, T3, T4, T5, T6, T7, T8)> + { + T1 t1 = default; + T2 t2 = default; + T3 t3 = default; + T4 t4 = default; + T5 t5 = default; + T6 t6 = default; + T7 t7 = default; + T8 t8 = default; + int completedCount; + UniTaskCompletionSourceCore<(T1, T2, T3, T4, T5, T6, T7, T8)> core; + + public WhenAllPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task8.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT8(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT8(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t1 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 8) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8)); + } + } + + static void TryInvokeContinuationT2(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t2 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 8) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8)); + } + } + + static void TryInvokeContinuationT3(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t3 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 8) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8)); + } + } + + static void TryInvokeContinuationT4(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t4 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 8) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8)); + } + } + + static void TryInvokeContinuationT5(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t5 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 8) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8)); + } + } + + static void TryInvokeContinuationT6(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t6 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 8) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8)); + } + } + + static void TryInvokeContinuationT7(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t7 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 8) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8)); + } + } + + static void TryInvokeContinuationT8(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t8 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 8) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8)); + } + } + + + public (T1, T2, T3, T4, T5, T6, T7, T8) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> WhenAll(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9) + { + if (task1.Status.IsCompletedSuccessfully() && task2.Status.IsCompletedSuccessfully() && task3.Status.IsCompletedSuccessfully() && task4.Status.IsCompletedSuccessfully() && task5.Status.IsCompletedSuccessfully() && task6.Status.IsCompletedSuccessfully() && task7.Status.IsCompletedSuccessfully() && task8.Status.IsCompletedSuccessfully() && task9.Status.IsCompletedSuccessfully()) + { + return new UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9)>((task1.GetAwaiter().GetResult(), task2.GetAwaiter().GetResult(), task3.GetAwaiter().GetResult(), task4.GetAwaiter().GetResult(), task5.GetAwaiter().GetResult(), task6.GetAwaiter().GetResult(), task7.GetAwaiter().GetResult(), task8.GetAwaiter().GetResult(), task9.GetAwaiter().GetResult())); + } + + return new UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9)>(new WhenAllPromise(task1, task2, task3, task4, task5, task6, task7, task8, task9), 0); + } + + sealed class WhenAllPromise : IUniTaskSource<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> + { + T1 t1 = default; + T2 t2 = default; + T3 t3 = default; + T4 t4 = default; + T5 t5 = default; + T6 t6 = default; + T7 t7 = default; + T8 t8 = default; + T9 t9 = default; + int completedCount; + UniTaskCompletionSourceCore<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> core; + + public WhenAllPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task8.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT8(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT8(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task9.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT9(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT9(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t1 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 9) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9)); + } + } + + static void TryInvokeContinuationT2(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t2 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 9) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9)); + } + } + + static void TryInvokeContinuationT3(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t3 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 9) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9)); + } + } + + static void TryInvokeContinuationT4(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t4 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 9) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9)); + } + } + + static void TryInvokeContinuationT5(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t5 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 9) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9)); + } + } + + static void TryInvokeContinuationT6(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t6 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 9) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9)); + } + } + + static void TryInvokeContinuationT7(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t7 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 9) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9)); + } + } + + static void TryInvokeContinuationT8(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t8 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 9) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9)); + } + } + + static void TryInvokeContinuationT9(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t9 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 9) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9)); + } + } + + + public (T1, T2, T3, T4, T5, T6, T7, T8, T9) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)> WhenAll(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10) + { + if (task1.Status.IsCompletedSuccessfully() && task2.Status.IsCompletedSuccessfully() && task3.Status.IsCompletedSuccessfully() && task4.Status.IsCompletedSuccessfully() && task5.Status.IsCompletedSuccessfully() && task6.Status.IsCompletedSuccessfully() && task7.Status.IsCompletedSuccessfully() && task8.Status.IsCompletedSuccessfully() && task9.Status.IsCompletedSuccessfully() && task10.Status.IsCompletedSuccessfully()) + { + return new UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)>((task1.GetAwaiter().GetResult(), task2.GetAwaiter().GetResult(), task3.GetAwaiter().GetResult(), task4.GetAwaiter().GetResult(), task5.GetAwaiter().GetResult(), task6.GetAwaiter().GetResult(), task7.GetAwaiter().GetResult(), task8.GetAwaiter().GetResult(), task9.GetAwaiter().GetResult(), task10.GetAwaiter().GetResult())); + } + + return new UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)>(new WhenAllPromise(task1, task2, task3, task4, task5, task6, task7, task8, task9, task10), 0); + } + + sealed class WhenAllPromise : IUniTaskSource<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)> + { + T1 t1 = default; + T2 t2 = default; + T3 t3 = default; + T4 t4 = default; + T5 t5 = default; + T6 t6 = default; + T7 t7 = default; + T8 t8 = default; + T9 t9 = default; + T10 t10 = default; + int completedCount; + UniTaskCompletionSourceCore<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)> core; + + public WhenAllPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task8.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT8(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT8(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task9.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT9(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT9(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task10.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT10(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT10(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t1 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 10) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10)); + } + } + + static void TryInvokeContinuationT2(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t2 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 10) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10)); + } + } + + static void TryInvokeContinuationT3(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t3 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 10) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10)); + } + } + + static void TryInvokeContinuationT4(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t4 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 10) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10)); + } + } + + static void TryInvokeContinuationT5(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t5 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 10) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10)); + } + } + + static void TryInvokeContinuationT6(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t6 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 10) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10)); + } + } + + static void TryInvokeContinuationT7(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t7 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 10) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10)); + } + } + + static void TryInvokeContinuationT8(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t8 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 10) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10)); + } + } + + static void TryInvokeContinuationT9(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t9 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 10) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10)); + } + } + + static void TryInvokeContinuationT10(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t10 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 10) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10)); + } + } + + + public (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)> WhenAll(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11) + { + if (task1.Status.IsCompletedSuccessfully() && task2.Status.IsCompletedSuccessfully() && task3.Status.IsCompletedSuccessfully() && task4.Status.IsCompletedSuccessfully() && task5.Status.IsCompletedSuccessfully() && task6.Status.IsCompletedSuccessfully() && task7.Status.IsCompletedSuccessfully() && task8.Status.IsCompletedSuccessfully() && task9.Status.IsCompletedSuccessfully() && task10.Status.IsCompletedSuccessfully() && task11.Status.IsCompletedSuccessfully()) + { + return new UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)>((task1.GetAwaiter().GetResult(), task2.GetAwaiter().GetResult(), task3.GetAwaiter().GetResult(), task4.GetAwaiter().GetResult(), task5.GetAwaiter().GetResult(), task6.GetAwaiter().GetResult(), task7.GetAwaiter().GetResult(), task8.GetAwaiter().GetResult(), task9.GetAwaiter().GetResult(), task10.GetAwaiter().GetResult(), task11.GetAwaiter().GetResult())); + } + + return new UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)>(new WhenAllPromise(task1, task2, task3, task4, task5, task6, task7, task8, task9, task10, task11), 0); + } + + sealed class WhenAllPromise : IUniTaskSource<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)> + { + T1 t1 = default; + T2 t2 = default; + T3 t3 = default; + T4 t4 = default; + T5 t5 = default; + T6 t6 = default; + T7 t7 = default; + T8 t8 = default; + T9 t9 = default; + T10 t10 = default; + T11 t11 = default; + int completedCount; + UniTaskCompletionSourceCore<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)> core; + + public WhenAllPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task8.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT8(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT8(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task9.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT9(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT9(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task10.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT10(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT10(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task11.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT11(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT11(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t1 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 11) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11)); + } + } + + static void TryInvokeContinuationT2(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t2 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 11) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11)); + } + } + + static void TryInvokeContinuationT3(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t3 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 11) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11)); + } + } + + static void TryInvokeContinuationT4(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t4 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 11) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11)); + } + } + + static void TryInvokeContinuationT5(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t5 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 11) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11)); + } + } + + static void TryInvokeContinuationT6(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t6 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 11) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11)); + } + } + + static void TryInvokeContinuationT7(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t7 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 11) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11)); + } + } + + static void TryInvokeContinuationT8(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t8 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 11) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11)); + } + } + + static void TryInvokeContinuationT9(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t9 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 11) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11)); + } + } + + static void TryInvokeContinuationT10(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t10 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 11) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11)); + } + } + + static void TryInvokeContinuationT11(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t11 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 11) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11)); + } + } + + + public (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)> WhenAll(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12) + { + if (task1.Status.IsCompletedSuccessfully() && task2.Status.IsCompletedSuccessfully() && task3.Status.IsCompletedSuccessfully() && task4.Status.IsCompletedSuccessfully() && task5.Status.IsCompletedSuccessfully() && task6.Status.IsCompletedSuccessfully() && task7.Status.IsCompletedSuccessfully() && task8.Status.IsCompletedSuccessfully() && task9.Status.IsCompletedSuccessfully() && task10.Status.IsCompletedSuccessfully() && task11.Status.IsCompletedSuccessfully() && task12.Status.IsCompletedSuccessfully()) + { + return new UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)>((task1.GetAwaiter().GetResult(), task2.GetAwaiter().GetResult(), task3.GetAwaiter().GetResult(), task4.GetAwaiter().GetResult(), task5.GetAwaiter().GetResult(), task6.GetAwaiter().GetResult(), task7.GetAwaiter().GetResult(), task8.GetAwaiter().GetResult(), task9.GetAwaiter().GetResult(), task10.GetAwaiter().GetResult(), task11.GetAwaiter().GetResult(), task12.GetAwaiter().GetResult())); + } + + return new UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)>(new WhenAllPromise(task1, task2, task3, task4, task5, task6, task7, task8, task9, task10, task11, task12), 0); + } + + sealed class WhenAllPromise : IUniTaskSource<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)> + { + T1 t1 = default; + T2 t2 = default; + T3 t3 = default; + T4 t4 = default; + T5 t5 = default; + T6 t6 = default; + T7 t7 = default; + T8 t8 = default; + T9 t9 = default; + T10 t10 = default; + T11 t11 = default; + T12 t12 = default; + int completedCount; + UniTaskCompletionSourceCore<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)> core; + + public WhenAllPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task8.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT8(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT8(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task9.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT9(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT9(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task10.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT10(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT10(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task11.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT11(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT11(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task12.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT12(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT12(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t1 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 12) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12)); + } + } + + static void TryInvokeContinuationT2(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t2 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 12) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12)); + } + } + + static void TryInvokeContinuationT3(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t3 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 12) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12)); + } + } + + static void TryInvokeContinuationT4(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t4 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 12) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12)); + } + } + + static void TryInvokeContinuationT5(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t5 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 12) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12)); + } + } + + static void TryInvokeContinuationT6(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t6 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 12) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12)); + } + } + + static void TryInvokeContinuationT7(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t7 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 12) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12)); + } + } + + static void TryInvokeContinuationT8(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t8 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 12) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12)); + } + } + + static void TryInvokeContinuationT9(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t9 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 12) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12)); + } + } + + static void TryInvokeContinuationT10(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t10 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 12) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12)); + } + } + + static void TryInvokeContinuationT11(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t11 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 12) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12)); + } + } + + static void TryInvokeContinuationT12(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t12 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 12) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12)); + } + } + + + public (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)> WhenAll(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13) + { + if (task1.Status.IsCompletedSuccessfully() && task2.Status.IsCompletedSuccessfully() && task3.Status.IsCompletedSuccessfully() && task4.Status.IsCompletedSuccessfully() && task5.Status.IsCompletedSuccessfully() && task6.Status.IsCompletedSuccessfully() && task7.Status.IsCompletedSuccessfully() && task8.Status.IsCompletedSuccessfully() && task9.Status.IsCompletedSuccessfully() && task10.Status.IsCompletedSuccessfully() && task11.Status.IsCompletedSuccessfully() && task12.Status.IsCompletedSuccessfully() && task13.Status.IsCompletedSuccessfully()) + { + return new UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)>((task1.GetAwaiter().GetResult(), task2.GetAwaiter().GetResult(), task3.GetAwaiter().GetResult(), task4.GetAwaiter().GetResult(), task5.GetAwaiter().GetResult(), task6.GetAwaiter().GetResult(), task7.GetAwaiter().GetResult(), task8.GetAwaiter().GetResult(), task9.GetAwaiter().GetResult(), task10.GetAwaiter().GetResult(), task11.GetAwaiter().GetResult(), task12.GetAwaiter().GetResult(), task13.GetAwaiter().GetResult())); + } + + return new UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)>(new WhenAllPromise(task1, task2, task3, task4, task5, task6, task7, task8, task9, task10, task11, task12, task13), 0); + } + + sealed class WhenAllPromise : IUniTaskSource<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)> + { + T1 t1 = default; + T2 t2 = default; + T3 t3 = default; + T4 t4 = default; + T5 t5 = default; + T6 t6 = default; + T7 t7 = default; + T8 t8 = default; + T9 t9 = default; + T10 t10 = default; + T11 t11 = default; + T12 t12 = default; + T13 t13 = default; + int completedCount; + UniTaskCompletionSourceCore<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)> core; + + public WhenAllPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task8.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT8(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT8(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task9.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT9(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT9(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task10.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT10(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT10(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task11.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT11(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT11(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task12.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT12(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT12(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task13.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT13(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT13(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t1 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 13) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13)); + } + } + + static void TryInvokeContinuationT2(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t2 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 13) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13)); + } + } + + static void TryInvokeContinuationT3(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t3 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 13) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13)); + } + } + + static void TryInvokeContinuationT4(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t4 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 13) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13)); + } + } + + static void TryInvokeContinuationT5(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t5 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 13) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13)); + } + } + + static void TryInvokeContinuationT6(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t6 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 13) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13)); + } + } + + static void TryInvokeContinuationT7(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t7 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 13) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13)); + } + } + + static void TryInvokeContinuationT8(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t8 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 13) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13)); + } + } + + static void TryInvokeContinuationT9(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t9 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 13) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13)); + } + } + + static void TryInvokeContinuationT10(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t10 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 13) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13)); + } + } + + static void TryInvokeContinuationT11(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t11 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 13) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13)); + } + } + + static void TryInvokeContinuationT12(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t12 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 13) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13)); + } + } + + static void TryInvokeContinuationT13(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t13 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 13) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13)); + } + } + + + public (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)> WhenAll(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13, UniTask task14) + { + if (task1.Status.IsCompletedSuccessfully() && task2.Status.IsCompletedSuccessfully() && task3.Status.IsCompletedSuccessfully() && task4.Status.IsCompletedSuccessfully() && task5.Status.IsCompletedSuccessfully() && task6.Status.IsCompletedSuccessfully() && task7.Status.IsCompletedSuccessfully() && task8.Status.IsCompletedSuccessfully() && task9.Status.IsCompletedSuccessfully() && task10.Status.IsCompletedSuccessfully() && task11.Status.IsCompletedSuccessfully() && task12.Status.IsCompletedSuccessfully() && task13.Status.IsCompletedSuccessfully() && task14.Status.IsCompletedSuccessfully()) + { + return new UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)>((task1.GetAwaiter().GetResult(), task2.GetAwaiter().GetResult(), task3.GetAwaiter().GetResult(), task4.GetAwaiter().GetResult(), task5.GetAwaiter().GetResult(), task6.GetAwaiter().GetResult(), task7.GetAwaiter().GetResult(), task8.GetAwaiter().GetResult(), task9.GetAwaiter().GetResult(), task10.GetAwaiter().GetResult(), task11.GetAwaiter().GetResult(), task12.GetAwaiter().GetResult(), task13.GetAwaiter().GetResult(), task14.GetAwaiter().GetResult())); + } + + return new UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)>(new WhenAllPromise(task1, task2, task3, task4, task5, task6, task7, task8, task9, task10, task11, task12, task13, task14), 0); + } + + sealed class WhenAllPromise : IUniTaskSource<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)> + { + T1 t1 = default; + T2 t2 = default; + T3 t3 = default; + T4 t4 = default; + T5 t5 = default; + T6 t6 = default; + T7 t7 = default; + T8 t8 = default; + T9 t9 = default; + T10 t10 = default; + T11 t11 = default; + T12 t12 = default; + T13 t13 = default; + T14 t14 = default; + int completedCount; + UniTaskCompletionSourceCore<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)> core; + + public WhenAllPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13, UniTask task14) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task8.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT8(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT8(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task9.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT9(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT9(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task10.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT10(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT10(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task11.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT11(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT11(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task12.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT12(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT12(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task13.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT13(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT13(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task14.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT14(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT14(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t1 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 14) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14)); + } + } + + static void TryInvokeContinuationT2(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t2 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 14) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14)); + } + } + + static void TryInvokeContinuationT3(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t3 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 14) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14)); + } + } + + static void TryInvokeContinuationT4(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t4 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 14) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14)); + } + } + + static void TryInvokeContinuationT5(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t5 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 14) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14)); + } + } + + static void TryInvokeContinuationT6(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t6 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 14) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14)); + } + } + + static void TryInvokeContinuationT7(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t7 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 14) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14)); + } + } + + static void TryInvokeContinuationT8(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t8 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 14) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14)); + } + } + + static void TryInvokeContinuationT9(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t9 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 14) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14)); + } + } + + static void TryInvokeContinuationT10(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t10 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 14) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14)); + } + } + + static void TryInvokeContinuationT11(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t11 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 14) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14)); + } + } + + static void TryInvokeContinuationT12(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t12 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 14) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14)); + } + } + + static void TryInvokeContinuationT13(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t13 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 14) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14)); + } + } + + static void TryInvokeContinuationT14(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t14 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 14) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14)); + } + } + + + public (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)> WhenAll(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13, UniTask task14, UniTask task15) + { + if (task1.Status.IsCompletedSuccessfully() && task2.Status.IsCompletedSuccessfully() && task3.Status.IsCompletedSuccessfully() && task4.Status.IsCompletedSuccessfully() && task5.Status.IsCompletedSuccessfully() && task6.Status.IsCompletedSuccessfully() && task7.Status.IsCompletedSuccessfully() && task8.Status.IsCompletedSuccessfully() && task9.Status.IsCompletedSuccessfully() && task10.Status.IsCompletedSuccessfully() && task11.Status.IsCompletedSuccessfully() && task12.Status.IsCompletedSuccessfully() && task13.Status.IsCompletedSuccessfully() && task14.Status.IsCompletedSuccessfully() && task15.Status.IsCompletedSuccessfully()) + { + return new UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)>((task1.GetAwaiter().GetResult(), task2.GetAwaiter().GetResult(), task3.GetAwaiter().GetResult(), task4.GetAwaiter().GetResult(), task5.GetAwaiter().GetResult(), task6.GetAwaiter().GetResult(), task7.GetAwaiter().GetResult(), task8.GetAwaiter().GetResult(), task9.GetAwaiter().GetResult(), task10.GetAwaiter().GetResult(), task11.GetAwaiter().GetResult(), task12.GetAwaiter().GetResult(), task13.GetAwaiter().GetResult(), task14.GetAwaiter().GetResult(), task15.GetAwaiter().GetResult())); + } + + return new UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)>(new WhenAllPromise(task1, task2, task3, task4, task5, task6, task7, task8, task9, task10, task11, task12, task13, task14, task15), 0); + } + + sealed class WhenAllPromise : IUniTaskSource<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)> + { + T1 t1 = default; + T2 t2 = default; + T3 t3 = default; + T4 t4 = default; + T5 t5 = default; + T6 t6 = default; + T7 t7 = default; + T8 t8 = default; + T9 t9 = default; + T10 t10 = default; + T11 t11 = default; + T12 t12 = default; + T13 t13 = default; + T14 t14 = default; + T15 t15 = default; + int completedCount; + UniTaskCompletionSourceCore<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)> core; + + public WhenAllPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13, UniTask task14, UniTask task15) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task8.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT8(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT8(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task9.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT9(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT9(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task10.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT10(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT10(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task11.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT11(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT11(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task12.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT12(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT12(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task13.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT13(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT13(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task14.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT14(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT14(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task15.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT15(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT15(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t1 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 15) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14, self.t15)); + } + } + + static void TryInvokeContinuationT2(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t2 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 15) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14, self.t15)); + } + } + + static void TryInvokeContinuationT3(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t3 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 15) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14, self.t15)); + } + } + + static void TryInvokeContinuationT4(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t4 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 15) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14, self.t15)); + } + } + + static void TryInvokeContinuationT5(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t5 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 15) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14, self.t15)); + } + } + + static void TryInvokeContinuationT6(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t6 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 15) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14, self.t15)); + } + } + + static void TryInvokeContinuationT7(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t7 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 15) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14, self.t15)); + } + } + + static void TryInvokeContinuationT8(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t8 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 15) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14, self.t15)); + } + } + + static void TryInvokeContinuationT9(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t9 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 15) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14, self.t15)); + } + } + + static void TryInvokeContinuationT10(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t10 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 15) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14, self.t15)); + } + } + + static void TryInvokeContinuationT11(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t11 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 15) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14, self.t15)); + } + } + + static void TryInvokeContinuationT12(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t12 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 15) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14, self.t15)); + } + } + + static void TryInvokeContinuationT13(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t13 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 15) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14, self.t15)); + } + } + + static void TryInvokeContinuationT14(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t14 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 15) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14, self.t15)); + } + } + + static void TryInvokeContinuationT15(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + self.t15 = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 15) + { + self.core.TrySetResult((self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14, self.t15)); + } + } + + + public (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.Generated.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.Generated.cs.meta new file mode 100644 index 0000000..40ed46c --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.Generated.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5110117231c8a6d4095fd0cbd3f4c142 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.Generated.tt b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.Generated.tt new file mode 100644 index 0000000..f14cb4c --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.Generated.tt @@ -0,0 +1,122 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".cs" #> +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member +using System; +using System.Runtime.CompilerServices; +using System.Runtime.ExceptionServices; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks +{ + public partial struct UniTask + { +<# for(var i = 2; i <= 15; i++ ) { + var range = Enumerable.Range(1, i); + var t = string.Join(", ", range.Select(x => "T" + x)); + var args = string.Join(", ", range.Select(x => $"UniTask task{x}")); + var targs = string.Join(", ", range.Select(x => $"task{x}")); + var tresult = string.Join(", ", range.Select(x => $"task{x}.GetAwaiter().GetResult()")); + var completedSuccessfullyAnd = string.Join(" && ", range.Select(x => $"task{x}.Status.IsCompletedSuccessfully()")); + var tfield = string.Join(", ", range.Select(x => $"self.t{x}")); +#> + + public static UniTask<(<#= t #>)> WhenAll<<#= t #>>(<#= args #>) + { + if (<#= completedSuccessfullyAnd #>) + { + return new UniTask<(<#= t #>)>((<#= tresult #>)); + } + + return new UniTask<(<#= t #>)>(new WhenAllPromise<<#= t #>>(<#= targs #>), 0); + } + + sealed class WhenAllPromise<<#= t #>> : IUniTaskSource<(<#= t #>)> + { +<# for(var j = 1; j <= i; j++) { #> + T<#= j #> t<#= j #> = default; +<# } #> + int completedCount; + UniTaskCompletionSourceCore<(<#= t #>)> core; + + public WhenAllPromise(<#= args #>) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; +<# for(var j = 1; j <= i; j++) { #> + { + var awaiter = task<#= j #>.GetAwaiter(); + if (awaiter.IsCompleted) + { + TryInvokeContinuationT<#= j #>(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple>, UniTask>.Awaiter>)state) + { + TryInvokeContinuationT<#= j #>(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } +<# } #> + } + +<# for(var j = 1; j <= i; j++) { #> + static void TryInvokeContinuationT<#= j #>(WhenAllPromise<<#= t #>> self, in UniTask>.Awaiter awaiter) + { + try + { + self.t<#= j #> = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == <#= i #>) + { + self.core.TrySetResult((<#= tfield #>)); + } + } + +<# } #> + + public (<#= t #>) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } +<# } #> + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.Generated.tt.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.Generated.tt.meta new file mode 100644 index 0000000..f64cefd --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.Generated.tt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 2c2ecfd98ee1b9a4c9fae1b398c32d75 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.cs new file mode 100644 index 0000000..39f6a9a --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.cs @@ -0,0 +1,237 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Collections.Generic; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks +{ + public partial struct UniTask + { + public static UniTask WhenAll(params UniTask[] tasks) + { + if (tasks.Length == 0) + { + return UniTask.FromResult(Array.Empty()); + } + + return new UniTask(new WhenAllPromise(tasks, tasks.Length), 0); + } + + public static UniTask WhenAll(IEnumerable> tasks) + { + using (var span = ArrayPoolUtil.Materialize(tasks)) + { + var promise = new WhenAllPromise(span.Array, span.Length); // consumed array in constructor. + return new UniTask(promise, 0); + } + } + + public static UniTask WhenAll(params UniTask[] tasks) + { + if (tasks.Length == 0) + { + return UniTask.CompletedTask; + } + + return new UniTask(new WhenAllPromise(tasks, tasks.Length), 0); + } + + public static UniTask WhenAll(IEnumerable tasks) + { + using (var span = ArrayPoolUtil.Materialize(tasks)) + { + var promise = new WhenAllPromise(span.Array, span.Length); // consumed array in constructor. + return new UniTask(promise, 0); + } + } + + sealed class WhenAllPromise : IUniTaskSource + { + T[] result; + int completeCount; + UniTaskCompletionSourceCore core; // don't reset(called after GetResult, will invoke TrySetException.) + + public WhenAllPromise(UniTask[] tasks, int tasksLength) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completeCount = 0; + + if (tasksLength == 0) + { + this.result = Array.Empty(); + core.TrySetResult(result); + return; + } + + this.result = new T[tasksLength]; + + for (int i = 0; i < tasksLength; i++) + { + UniTask.Awaiter awaiter; + try + { + awaiter = tasks[i].GetAwaiter(); + } + catch (Exception ex) + { + core.TrySetException(ex); + continue; + } + + if (awaiter.IsCompleted) + { + TryInvokeContinuation(this, awaiter, i); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter, int>)state) + { + TryInvokeContinuation(t.Item1, t.Item2, t.Item3); + } + }, StateTuple.Create(this, awaiter, i)); + } + } + } + + static void TryInvokeContinuation(WhenAllPromise self, in UniTask.Awaiter awaiter, int i) + { + try + { + self.result[i] = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completeCount) == self.result.Length) + { + self.core.TrySetResult(self.result); + } + } + + public T[] GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + sealed class WhenAllPromise : IUniTaskSource + { + int completeCount; + int tasksLength; + UniTaskCompletionSourceCore core; // don't reset(called after GetResult, will invoke TrySetException.) + + public WhenAllPromise(UniTask[] tasks, int tasksLength) + { + TaskTracker.TrackActiveTask(this, 3); + + this.tasksLength = tasksLength; + this.completeCount = 0; + + if (tasksLength == 0) + { + core.TrySetResult(AsyncUnit.Default); + return; + } + + for (int i = 0; i < tasksLength; i++) + { + UniTask.Awaiter awaiter; + try + { + awaiter = tasks[i].GetAwaiter(); + } + catch (Exception ex) + { + core.TrySetException(ex); + continue; + } + + if (awaiter.IsCompleted) + { + TryInvokeContinuation(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple)state) + { + TryInvokeContinuation(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuation(WhenAllPromise self, in UniTask.Awaiter awaiter) + { + try + { + awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completeCount) == self.tasksLength) + { + self.core.TrySetResult(AsyncUnit.Default); + } + } + + public void GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.cs.meta new file mode 100644 index 0000000..0366aa8 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAll.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 355997a305ba64248822eec34998a1a0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.Generated.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.Generated.cs new file mode 100644 index 0000000..09b98e6 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.Generated.cs @@ -0,0 +1,5060 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member +using System; +using System.Runtime.CompilerServices; +using System.Runtime.ExceptionServices; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks +{ + public partial struct UniTask + { + public static UniTask<(int winArgumentIndex, T1 result1, T2 result2)> WhenAny(UniTask task1, UniTask task2) + { + return new UniTask<(int winArgumentIndex, T1 result1, T2 result2)>(new WhenAnyPromise(task1, task2), 0); + } + + sealed class WhenAnyPromise : IUniTaskSource<(int, T1 result1, T2 result2)> + { + int completedCount; + UniTaskCompletionSourceCore<(int, T1 result1, T2 result2)> core; + + public WhenAnyPromise(UniTask task1, UniTask task2) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T1 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((0, result, default)); + } + } + + static void TryInvokeContinuationT2(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T2 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((1, default, result)); + } + } + + + public (int, T1 result1, T2 result2) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + public static UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3)> WhenAny(UniTask task1, UniTask task2, UniTask task3) + { + return new UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3)>(new WhenAnyPromise(task1, task2, task3), 0); + } + + sealed class WhenAnyPromise : IUniTaskSource<(int, T1 result1, T2 result2, T3 result3)> + { + int completedCount; + UniTaskCompletionSourceCore<(int, T1 result1, T2 result2, T3 result3)> core; + + public WhenAnyPromise(UniTask task1, UniTask task2, UniTask task3) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T1 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((0, result, default, default)); + } + } + + static void TryInvokeContinuationT2(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T2 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((1, default, result, default)); + } + } + + static void TryInvokeContinuationT3(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T3 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((2, default, default, result)); + } + } + + + public (int, T1 result1, T2 result2, T3 result3) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + public static UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4)> WhenAny(UniTask task1, UniTask task2, UniTask task3, UniTask task4) + { + return new UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4)>(new WhenAnyPromise(task1, task2, task3, task4), 0); + } + + sealed class WhenAnyPromise : IUniTaskSource<(int, T1 result1, T2 result2, T3 result3, T4 result4)> + { + int completedCount; + UniTaskCompletionSourceCore<(int, T1 result1, T2 result2, T3 result3, T4 result4)> core; + + public WhenAnyPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T1 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((0, result, default, default, default)); + } + } + + static void TryInvokeContinuationT2(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T2 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((1, default, result, default, default)); + } + } + + static void TryInvokeContinuationT3(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T3 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((2, default, default, result, default)); + } + } + + static void TryInvokeContinuationT4(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T4 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((3, default, default, default, result)); + } + } + + + public (int, T1 result1, T2 result2, T3 result3, T4 result4) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + public static UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5)> WhenAny(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5) + { + return new UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5)>(new WhenAnyPromise(task1, task2, task3, task4, task5), 0); + } + + sealed class WhenAnyPromise : IUniTaskSource<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5)> + { + int completedCount; + UniTaskCompletionSourceCore<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5)> core; + + public WhenAnyPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T1 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((0, result, default, default, default, default)); + } + } + + static void TryInvokeContinuationT2(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T2 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((1, default, result, default, default, default)); + } + } + + static void TryInvokeContinuationT3(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T3 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((2, default, default, result, default, default)); + } + } + + static void TryInvokeContinuationT4(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T4 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((3, default, default, default, result, default)); + } + } + + static void TryInvokeContinuationT5(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T5 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((4, default, default, default, default, result)); + } + } + + + public (int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + public static UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6)> WhenAny(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6) + { + return new UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6)>(new WhenAnyPromise(task1, task2, task3, task4, task5, task6), 0); + } + + sealed class WhenAnyPromise : IUniTaskSource<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6)> + { + int completedCount; + UniTaskCompletionSourceCore<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6)> core; + + public WhenAnyPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T1 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((0, result, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT2(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T2 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((1, default, result, default, default, default, default)); + } + } + + static void TryInvokeContinuationT3(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T3 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((2, default, default, result, default, default, default)); + } + } + + static void TryInvokeContinuationT4(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T4 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((3, default, default, default, result, default, default)); + } + } + + static void TryInvokeContinuationT5(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T5 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((4, default, default, default, default, result, default)); + } + } + + static void TryInvokeContinuationT6(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T6 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((5, default, default, default, default, default, result)); + } + } + + + public (int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + public static UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7)> WhenAny(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7) + { + return new UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7)>(new WhenAnyPromise(task1, task2, task3, task4, task5, task6, task7), 0); + } + + sealed class WhenAnyPromise : IUniTaskSource<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7)> + { + int completedCount; + UniTaskCompletionSourceCore<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7)> core; + + public WhenAnyPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T1 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((0, result, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT2(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T2 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((1, default, result, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT3(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T3 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((2, default, default, result, default, default, default, default)); + } + } + + static void TryInvokeContinuationT4(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T4 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((3, default, default, default, result, default, default, default)); + } + } + + static void TryInvokeContinuationT5(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T5 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((4, default, default, default, default, result, default, default)); + } + } + + static void TryInvokeContinuationT6(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T6 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((5, default, default, default, default, default, result, default)); + } + } + + static void TryInvokeContinuationT7(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T7 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((6, default, default, default, default, default, default, result)); + } + } + + + public (int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + public static UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8)> WhenAny(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8) + { + return new UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8)>(new WhenAnyPromise(task1, task2, task3, task4, task5, task6, task7, task8), 0); + } + + sealed class WhenAnyPromise : IUniTaskSource<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8)> + { + int completedCount; + UniTaskCompletionSourceCore<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8)> core; + + public WhenAnyPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task8.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT8(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT8(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T1 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((0, result, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT2(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T2 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((1, default, result, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT3(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T3 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((2, default, default, result, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT4(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T4 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((3, default, default, default, result, default, default, default, default)); + } + } + + static void TryInvokeContinuationT5(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T5 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((4, default, default, default, default, result, default, default, default)); + } + } + + static void TryInvokeContinuationT6(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T6 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((5, default, default, default, default, default, result, default, default)); + } + } + + static void TryInvokeContinuationT7(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T7 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((6, default, default, default, default, default, default, result, default)); + } + } + + static void TryInvokeContinuationT8(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T8 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((7, default, default, default, default, default, default, default, result)); + } + } + + + public (int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + public static UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9)> WhenAny(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9) + { + return new UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9)>(new WhenAnyPromise(task1, task2, task3, task4, task5, task6, task7, task8, task9), 0); + } + + sealed class WhenAnyPromise : IUniTaskSource<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9)> + { + int completedCount; + UniTaskCompletionSourceCore<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9)> core; + + public WhenAnyPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task8.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT8(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT8(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task9.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT9(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT9(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T1 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((0, result, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT2(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T2 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((1, default, result, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT3(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T3 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((2, default, default, result, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT4(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T4 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((3, default, default, default, result, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT5(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T5 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((4, default, default, default, default, result, default, default, default, default)); + } + } + + static void TryInvokeContinuationT6(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T6 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((5, default, default, default, default, default, result, default, default, default)); + } + } + + static void TryInvokeContinuationT7(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T7 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((6, default, default, default, default, default, default, result, default, default)); + } + } + + static void TryInvokeContinuationT8(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T8 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((7, default, default, default, default, default, default, default, result, default)); + } + } + + static void TryInvokeContinuationT9(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T9 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((8, default, default, default, default, default, default, default, default, result)); + } + } + + + public (int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + public static UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10)> WhenAny(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10) + { + return new UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10)>(new WhenAnyPromise(task1, task2, task3, task4, task5, task6, task7, task8, task9, task10), 0); + } + + sealed class WhenAnyPromise : IUniTaskSource<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10)> + { + int completedCount; + UniTaskCompletionSourceCore<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10)> core; + + public WhenAnyPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task8.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT8(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT8(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task9.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT9(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT9(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task10.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT10(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT10(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T1 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((0, result, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT2(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T2 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((1, default, result, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT3(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T3 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((2, default, default, result, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT4(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T4 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((3, default, default, default, result, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT5(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T5 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((4, default, default, default, default, result, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT6(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T6 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((5, default, default, default, default, default, result, default, default, default, default)); + } + } + + static void TryInvokeContinuationT7(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T7 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((6, default, default, default, default, default, default, result, default, default, default)); + } + } + + static void TryInvokeContinuationT8(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T8 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((7, default, default, default, default, default, default, default, result, default, default)); + } + } + + static void TryInvokeContinuationT9(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T9 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((8, default, default, default, default, default, default, default, default, result, default)); + } + } + + static void TryInvokeContinuationT10(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T10 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((9, default, default, default, default, default, default, default, default, default, result)); + } + } + + + public (int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + public static UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11)> WhenAny(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11) + { + return new UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11)>(new WhenAnyPromise(task1, task2, task3, task4, task5, task6, task7, task8, task9, task10, task11), 0); + } + + sealed class WhenAnyPromise : IUniTaskSource<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11)> + { + int completedCount; + UniTaskCompletionSourceCore<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11)> core; + + public WhenAnyPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task8.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT8(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT8(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task9.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT9(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT9(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task10.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT10(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT10(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task11.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT11(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT11(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T1 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((0, result, default, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT2(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T2 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((1, default, result, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT3(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T3 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((2, default, default, result, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT4(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T4 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((3, default, default, default, result, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT5(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T5 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((4, default, default, default, default, result, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT6(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T6 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((5, default, default, default, default, default, result, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT7(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T7 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((6, default, default, default, default, default, default, result, default, default, default, default)); + } + } + + static void TryInvokeContinuationT8(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T8 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((7, default, default, default, default, default, default, default, result, default, default, default)); + } + } + + static void TryInvokeContinuationT9(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T9 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((8, default, default, default, default, default, default, default, default, result, default, default)); + } + } + + static void TryInvokeContinuationT10(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T10 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((9, default, default, default, default, default, default, default, default, default, result, default)); + } + } + + static void TryInvokeContinuationT11(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T11 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((10, default, default, default, default, default, default, default, default, default, default, result)); + } + } + + + public (int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + public static UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12)> WhenAny(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12) + { + return new UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12)>(new WhenAnyPromise(task1, task2, task3, task4, task5, task6, task7, task8, task9, task10, task11, task12), 0); + } + + sealed class WhenAnyPromise : IUniTaskSource<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12)> + { + int completedCount; + UniTaskCompletionSourceCore<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12)> core; + + public WhenAnyPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task8.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT8(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT8(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task9.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT9(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT9(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task10.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT10(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT10(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task11.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT11(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT11(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task12.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT12(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT12(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T1 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((0, result, default, default, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT2(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T2 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((1, default, result, default, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT3(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T3 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((2, default, default, result, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT4(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T4 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((3, default, default, default, result, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT5(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T5 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((4, default, default, default, default, result, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT6(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T6 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((5, default, default, default, default, default, result, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT7(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T7 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((6, default, default, default, default, default, default, result, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT8(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T8 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((7, default, default, default, default, default, default, default, result, default, default, default, default)); + } + } + + static void TryInvokeContinuationT9(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T9 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((8, default, default, default, default, default, default, default, default, result, default, default, default)); + } + } + + static void TryInvokeContinuationT10(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T10 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((9, default, default, default, default, default, default, default, default, default, result, default, default)); + } + } + + static void TryInvokeContinuationT11(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T11 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((10, default, default, default, default, default, default, default, default, default, default, result, default)); + } + } + + static void TryInvokeContinuationT12(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T12 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((11, default, default, default, default, default, default, default, default, default, default, default, result)); + } + } + + + public (int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + public static UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12, T13 result13)> WhenAny(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13) + { + return new UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12, T13 result13)>(new WhenAnyPromise(task1, task2, task3, task4, task5, task6, task7, task8, task9, task10, task11, task12, task13), 0); + } + + sealed class WhenAnyPromise : IUniTaskSource<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12, T13 result13)> + { + int completedCount; + UniTaskCompletionSourceCore<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12, T13 result13)> core; + + public WhenAnyPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task8.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT8(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT8(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task9.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT9(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT9(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task10.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT10(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT10(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task11.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT11(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT11(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task12.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT12(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT12(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task13.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT13(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT13(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T1 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((0, result, default, default, default, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT2(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T2 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((1, default, result, default, default, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT3(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T3 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((2, default, default, result, default, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT4(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T4 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((3, default, default, default, result, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT5(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T5 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((4, default, default, default, default, result, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT6(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T6 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((5, default, default, default, default, default, result, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT7(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T7 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((6, default, default, default, default, default, default, result, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT8(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T8 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((7, default, default, default, default, default, default, default, result, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT9(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T9 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((8, default, default, default, default, default, default, default, default, result, default, default, default, default)); + } + } + + static void TryInvokeContinuationT10(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T10 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((9, default, default, default, default, default, default, default, default, default, result, default, default, default)); + } + } + + static void TryInvokeContinuationT11(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T11 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((10, default, default, default, default, default, default, default, default, default, default, result, default, default)); + } + } + + static void TryInvokeContinuationT12(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T12 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((11, default, default, default, default, default, default, default, default, default, default, default, result, default)); + } + } + + static void TryInvokeContinuationT13(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T13 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((12, default, default, default, default, default, default, default, default, default, default, default, default, result)); + } + } + + + public (int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12, T13 result13) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + public static UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12, T13 result13, T14 result14)> WhenAny(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13, UniTask task14) + { + return new UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12, T13 result13, T14 result14)>(new WhenAnyPromise(task1, task2, task3, task4, task5, task6, task7, task8, task9, task10, task11, task12, task13, task14), 0); + } + + sealed class WhenAnyPromise : IUniTaskSource<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12, T13 result13, T14 result14)> + { + int completedCount; + UniTaskCompletionSourceCore<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12, T13 result13, T14 result14)> core; + + public WhenAnyPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13, UniTask task14) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task8.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT8(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT8(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task9.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT9(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT9(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task10.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT10(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT10(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task11.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT11(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT11(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task12.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT12(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT12(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task13.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT13(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT13(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task14.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT14(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT14(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T1 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((0, result, default, default, default, default, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT2(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T2 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((1, default, result, default, default, default, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT3(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T3 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((2, default, default, result, default, default, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT4(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T4 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((3, default, default, default, result, default, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT5(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T5 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((4, default, default, default, default, result, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT6(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T6 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((5, default, default, default, default, default, result, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT7(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T7 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((6, default, default, default, default, default, default, result, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT8(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T8 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((7, default, default, default, default, default, default, default, result, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT9(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T9 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((8, default, default, default, default, default, default, default, default, result, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT10(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T10 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((9, default, default, default, default, default, default, default, default, default, result, default, default, default, default)); + } + } + + static void TryInvokeContinuationT11(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T11 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((10, default, default, default, default, default, default, default, default, default, default, result, default, default, default)); + } + } + + static void TryInvokeContinuationT12(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T12 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((11, default, default, default, default, default, default, default, default, default, default, default, result, default, default)); + } + } + + static void TryInvokeContinuationT13(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T13 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((12, default, default, default, default, default, default, default, default, default, default, default, default, result, default)); + } + } + + static void TryInvokeContinuationT14(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T14 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((13, default, default, default, default, default, default, default, default, default, default, default, default, default, result)); + } + } + + + public (int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12, T13 result13, T14 result14) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + public static UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12, T13 result13, T14 result14, T15 result15)> WhenAny(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13, UniTask task14, UniTask task15) + { + return new UniTask<(int winArgumentIndex, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12, T13 result13, T14 result14, T15 result15)>(new WhenAnyPromise(task1, task2, task3, task4, task5, task6, task7, task8, task9, task10, task11, task12, task13, task14, task15), 0); + } + + sealed class WhenAnyPromise : IUniTaskSource<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12, T13 result13, T14 result14, T15 result15)> + { + int completedCount; + UniTaskCompletionSourceCore<(int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12, T13 result13, T14 result14, T15 result15)> core; + + public WhenAnyPromise(UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13, UniTask task14, UniTask task15) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; + { + var awaiter = task1.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT1(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT1(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task2.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT2(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT2(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task3.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT3(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT3(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task4.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT4(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT4(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task5.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT5(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT5(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task6.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT6(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT6(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task7.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT7(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT7(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task8.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT8(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT8(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task9.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT9(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT9(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task10.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT10(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT10(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task11.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT11(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT11(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task12.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT12(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT12(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task13.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT13(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT13(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task14.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT14(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT14(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + { + var awaiter = task15.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT15(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryInvokeContinuationT15(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryInvokeContinuationT1(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T1 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((0, result, default, default, default, default, default, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT2(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T2 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((1, default, result, default, default, default, default, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT3(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T3 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((2, default, default, result, default, default, default, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT4(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T4 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((3, default, default, default, result, default, default, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT5(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T5 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((4, default, default, default, default, result, default, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT6(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T6 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((5, default, default, default, default, default, result, default, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT7(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T7 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((6, default, default, default, default, default, default, result, default, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT8(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T8 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((7, default, default, default, default, default, default, default, result, default, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT9(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T9 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((8, default, default, default, default, default, default, default, default, result, default, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT10(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T10 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((9, default, default, default, default, default, default, default, default, default, result, default, default, default, default, default)); + } + } + + static void TryInvokeContinuationT11(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T11 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((10, default, default, default, default, default, default, default, default, default, default, result, default, default, default, default)); + } + } + + static void TryInvokeContinuationT12(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T12 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((11, default, default, default, default, default, default, default, default, default, default, default, result, default, default, default)); + } + } + + static void TryInvokeContinuationT13(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T13 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((12, default, default, default, default, default, default, default, default, default, default, default, default, result, default, default)); + } + } + + static void TryInvokeContinuationT14(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T14 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((13, default, default, default, default, default, default, default, default, default, default, default, default, default, result, default)); + } + } + + static void TryInvokeContinuationT15(WhenAnyPromise self, in UniTask.Awaiter awaiter) + { + T15 result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((14, default, default, default, default, default, default, default, default, default, default, default, default, default, default, result)); + } + } + + + public (int, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12, T13 result13, T14 result14, T15 result15) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.Generated.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.Generated.cs.meta new file mode 100644 index 0000000..49a2c3f --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.Generated.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 13d604ac281570c4eac9962429f19ca9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.Generated.tt b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.Generated.tt new file mode 100644 index 0000000..b1b6a03 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.Generated.tt @@ -0,0 +1,117 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".cs" #> +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member +using System; +using System.Runtime.CompilerServices; +using System.Runtime.ExceptionServices; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks +{ + public partial struct UniTask + { +<# for(var i = 2; i <= 15; i++ ) { + var range = Enumerable.Range(1, i); + var t = string.Join(", ", range.Select(x => "T" + x)); + var args = string.Join(", ", range.Select(x => $"UniTask task{x}")); + var targs = string.Join(", ", range.Select(x => $"task{x}")); + var tresult = string.Join(", ", range.Select(x => $"task{x}.GetAwaiter().GetResult()")); + var tBool = string.Join(", ", range.Select(x => $"T{x} result{x}")); + var tfield = string.Join(", ", range.Select(x => $"self.t{x}")); + Func getResult = j => string.Join(", ", range.Select(x => (x == j) ? "result" : "default")); +#> + public static UniTask<(int winArgumentIndex, <#= tBool #>)> WhenAny<<#= t #>>(<#= args #>) + { + return new UniTask<(int winArgumentIndex, <#= tBool #>)>(new WhenAnyPromise<<#= t #>>(<#= targs #>), 0); + } + + sealed class WhenAnyPromise<<#= t #>> : IUniTaskSource<(int, <#= tBool #>)> + { + int completedCount; + UniTaskCompletionSourceCore<(int, <#= tBool #>)> core; + + public WhenAnyPromise(<#= args #>) + { + TaskTracker.TrackActiveTask(this, 3); + + this.completedCount = 0; +<# for(var j = 1; j <= i; j++) { #> + { + var awaiter = task<#= j #>.GetAwaiter(); + + if (awaiter.IsCompleted) + { + TryInvokeContinuationT<#= j #>(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple>, UniTask>.Awaiter>)state) + { + TryInvokeContinuationT<#= j #>(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } +<# } #> + } + +<# for(var j = 1; j <= i; j++) { #> + static void TryInvokeContinuationT<#= j #>(WhenAnyPromise<<#= t #>> self, in UniTask>.Awaiter awaiter) + { + T<#= j #> result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((<#= j - 1 #>, <#= getResult(j) #>)); + } + } + +<# } #> + + public (int, <#= tBool #>) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + +<# } #> + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.Generated.tt.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.Generated.tt.meta new file mode 100644 index 0000000..389cfd2 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.Generated.tt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 71c2c0bce7543454c8ef545083e18170 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.cs new file mode 100644 index 0000000..09eb32d --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.cs @@ -0,0 +1,359 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Collections.Generic; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks +{ + public partial struct UniTask + { + public static UniTask<(bool hasResultLeft, T result)> WhenAny(UniTask leftTask, UniTask rightTask) + { + return new UniTask<(bool, T)>(new WhenAnyLRPromise(leftTask, rightTask), 0); + } + + public static UniTask<(int winArgumentIndex, T result)> WhenAny(params UniTask[] tasks) + { + return new UniTask<(int, T)>(new WhenAnyPromise(tasks, tasks.Length), 0); + } + + public static UniTask<(int winArgumentIndex, T result)> WhenAny(IEnumerable> tasks) + { + using (var span = ArrayPoolUtil.Materialize(tasks)) + { + return new UniTask<(int, T)>(new WhenAnyPromise(span.Array, span.Length), 0); + } + } + + /// Return value is winArgumentIndex + public static UniTask WhenAny(params UniTask[] tasks) + { + return new UniTask(new WhenAnyPromise(tasks, tasks.Length), 0); + } + + /// Return value is winArgumentIndex + public static UniTask WhenAny(IEnumerable tasks) + { + using (var span = ArrayPoolUtil.Materialize(tasks)) + { + return new UniTask(new WhenAnyPromise(span.Array, span.Length), 0); + } + } + + sealed class WhenAnyLRPromise : IUniTaskSource<(bool, T)> + { + int completedCount; + UniTaskCompletionSourceCore<(bool, T)> core; + + public WhenAnyLRPromise(UniTask leftTask, UniTask rightTask) + { + TaskTracker.TrackActiveTask(this, 3); + + { + UniTask.Awaiter awaiter; + try + { + awaiter = leftTask.GetAwaiter(); + } + catch (Exception ex) + { + core.TrySetException(ex); + goto RIGHT; + } + + if (awaiter.IsCompleted) + { + TryLeftInvokeContinuation(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryLeftInvokeContinuation(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + RIGHT: + { + UniTask.Awaiter awaiter; + try + { + awaiter = rightTask.GetAwaiter(); + } + catch (Exception ex) + { + core.TrySetException(ex); + return; + } + + if (awaiter.IsCompleted) + { + TryRightInvokeContinuation(this, awaiter); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter>)state) + { + TryRightInvokeContinuation(t.Item1, t.Item2); + } + }, StateTuple.Create(this, awaiter)); + } + } + } + + static void TryLeftInvokeContinuation(WhenAnyLRPromise self, in UniTask.Awaiter awaiter) + { + T result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((true, result)); + } + } + + static void TryRightInvokeContinuation(WhenAnyLRPromise self, in UniTask.Awaiter awaiter) + { + try + { + awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((false, default)); + } + } + + public (bool, T) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + + sealed class WhenAnyPromise : IUniTaskSource<(int, T)> + { + int completedCount; + UniTaskCompletionSourceCore<(int, T)> core; + + public WhenAnyPromise(UniTask[] tasks, int tasksLength) + { + if (tasksLength == 0) + { + throw new ArgumentException("The tasks argument contains no tasks."); + } + + TaskTracker.TrackActiveTask(this, 3); + + for (int i = 0; i < tasksLength; i++) + { + UniTask.Awaiter awaiter; + try + { + awaiter = tasks[i].GetAwaiter(); + } + catch (Exception ex) + { + core.TrySetException(ex); + continue; // consume others. + } + + if (awaiter.IsCompleted) + { + TryInvokeContinuation(this, awaiter, i); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple, UniTask.Awaiter, int>)state) + { + TryInvokeContinuation(t.Item1, t.Item2, t.Item3); + } + }, StateTuple.Create(this, awaiter, i)); + } + } + } + + static void TryInvokeContinuation(WhenAnyPromise self, in UniTask.Awaiter awaiter, int i) + { + T result; + try + { + result = awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult((i, result)); + } + } + + public (int, T) GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + sealed class WhenAnyPromise : IUniTaskSource + { + int completedCount; + UniTaskCompletionSourceCore core; + + public WhenAnyPromise(UniTask[] tasks, int tasksLength) + { + if (tasksLength == 0) + { + throw new ArgumentException("The tasks argument contains no tasks."); + } + + TaskTracker.TrackActiveTask(this, 3); + + for (int i = 0; i < tasksLength; i++) + { + UniTask.Awaiter awaiter; + try + { + awaiter = tasks[i].GetAwaiter(); + } + catch (Exception ex) + { + core.TrySetException(ex); + continue; // consume others. + } + + if (awaiter.IsCompleted) + { + TryInvokeContinuation(this, awaiter, i); + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple)state) + { + TryInvokeContinuation(t.Item1, t.Item2, t.Item3); + } + }, StateTuple.Create(this, awaiter, i)); + } + } + } + + static void TryInvokeContinuation(WhenAnyPromise self, in UniTask.Awaiter awaiter, int i) + { + try + { + awaiter.GetResult(); + } + catch (Exception ex) + { + self.core.TrySetException(ex); + return; + } + + if (Interlocked.Increment(ref self.completedCount) == 1) + { + self.core.TrySetResult(i); + } + } + + public int GetResult(short token) + { + TaskTracker.RemoveTracking(this); + GC.SuppressFinalize(this); + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.cs.meta new file mode 100644 index 0000000..c10f762 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.WhenAny.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c32578978c37eaf41bdd90e1b034637d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.asmdef b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.asmdef new file mode 100644 index 0000000..a5c594d --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.asmdef @@ -0,0 +1,45 @@ +{ + "name": "UniTask", + "rootNamespace": "", + "references": [], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [ + { + "name": "com.unity.modules.assetbundle", + "expression": "", + "define": "UNITASK_ASSETBUNDLE_SUPPORT" + }, + { + "name": "com.unity.modules.physics", + "expression": "", + "define": "UNITASK_PHYSICS_SUPPORT" + }, + { + "name": "com.unity.modules.physics2d", + "expression": "", + "define": "UNITASK_PHYSICS2D_SUPPORT" + }, + { + "name": "com.unity.modules.particlesystem", + "expression": "", + "define": "UNITASK_PARTICLESYSTEM_SUPPORT" + }, + { + "name": "com.unity.ugui", + "expression": "", + "define": "UNITASK_UGUI_SUPPORT" + }, + { + "name": "com.unity.modules.unitywebrequest", + "expression": "", + "define": "UNITASK_WEBREQUEST_SUPPORT" + } + ], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.asmdef.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.asmdef.meta new file mode 100644 index 0000000..e497045 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: f51ebe6a0ceec4240a699833d6309b23 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.cs new file mode 100644 index 0000000..8eb2087 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.cs @@ -0,0 +1,707 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member +#pragma warning disable CS0436 + +using Cysharp.Threading.Tasks.CompilerServices; +using System; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Runtime.ExceptionServices; +using System.Runtime.InteropServices; + +namespace Cysharp.Threading.Tasks +{ + internal static class AwaiterActions + { + internal static readonly Action InvokeContinuationDelegate = Continuation; + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static void Continuation(object state) + { + ((Action)state).Invoke(); + } + } + + /// + /// Lightweight unity specified task-like object. + /// + [AsyncMethodBuilder(typeof(AsyncUniTaskMethodBuilder))] + [StructLayout(LayoutKind.Auto)] + public readonly partial struct UniTask + { + readonly IUniTaskSource source; + readonly short token; + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public UniTask(IUniTaskSource source, short token) + { + this.source = source; + this.token = token; + } + + public UniTaskStatus Status + { + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + if (source == null) return UniTaskStatus.Succeeded; + return source.GetStatus(token); + } + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Awaiter GetAwaiter() + { + return new Awaiter(this); + } + + /// + /// returns (bool IsCanceled) instead of throws OperationCanceledException. + /// + public UniTask SuppressCancellationThrow() + { + var status = Status; + if (status == UniTaskStatus.Succeeded) return CompletedTasks.False; + if (status == UniTaskStatus.Canceled) return CompletedTasks.True; + return new UniTask(new IsCanceledSource(source), token); + } + +#if !UNITY_2018_3_OR_NEWER + + public static implicit operator System.Threading.Tasks.ValueTask(in UniTask self) + { + if (self.source == null) + { + return default; + } + +#if NETSTANDARD2_0 + return self.AsValueTask(); +#else + return new System.Threading.Tasks.ValueTask(self.source, self.token); +#endif + } + +#endif + + public override string ToString() + { + if (source == null) return "()"; + return "(" + source.UnsafeGetStatus() + ")"; + } + + /// + /// Memoizing inner IValueTaskSource. The result UniTask can await multiple. + /// + public UniTask Preserve() + { + if (source == null) + { + return this; + } + else + { + return new UniTask(new MemoizeSource(source), token); + } + } + + public UniTask AsAsyncUnitUniTask() + { + if (this.source == null) return CompletedTasks.AsyncUnit; + + var status = this.source.GetStatus(this.token); + if (status.IsCompletedSuccessfully()) + { + this.source.GetResult(this.token); + return CompletedTasks.AsyncUnit; + } + else if(this.source is IUniTaskSource asyncUnitSource) + { + return new UniTask(asyncUnitSource, this.token); + } + + return new UniTask(new AsyncUnitSource(this.source), this.token); + } + + sealed class AsyncUnitSource : IUniTaskSource + { + readonly IUniTaskSource source; + + public AsyncUnitSource(IUniTaskSource source) + { + this.source = source; + } + + public AsyncUnit GetResult(short token) + { + source.GetResult(token); + return AsyncUnit.Default; + } + + public UniTaskStatus GetStatus(short token) + { + return source.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + source.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return source.UnsafeGetStatus(); + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + } + + sealed class IsCanceledSource : IUniTaskSource + { + readonly IUniTaskSource source; + + public IsCanceledSource(IUniTaskSource source) + { + this.source = source; + } + + public bool GetResult(short token) + { + if (source.GetStatus(token) == UniTaskStatus.Canceled) + { + return true; + } + + source.GetResult(token); + return false; + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return source.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return source.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + source.OnCompleted(continuation, state, token); + } + } + + sealed class MemoizeSource : IUniTaskSource + { + IUniTaskSource source; + ExceptionDispatchInfo exception; + UniTaskStatus status; + + public MemoizeSource(IUniTaskSource source) + { + this.source = source; + } + + public void GetResult(short token) + { + if (source == null) + { + if (exception != null) + { + exception.Throw(); + } + } + else + { + try + { + source.GetResult(token); + status = UniTaskStatus.Succeeded; + } + catch (Exception ex) + { + exception = ExceptionDispatchInfo.Capture(ex); + if (ex is OperationCanceledException) + { + status = UniTaskStatus.Canceled; + } + else + { + status = UniTaskStatus.Faulted; + } + throw; + } + finally + { + source = null; + } + } + } + + public UniTaskStatus GetStatus(short token) + { + if (source == null) + { + return status; + } + + return source.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + if (source == null) + { + continuation(state); + } + else + { + source.OnCompleted(continuation, state, token); + } + } + + public UniTaskStatus UnsafeGetStatus() + { + if (source == null) + { + return status; + } + + return source.UnsafeGetStatus(); + } + } + + public readonly struct Awaiter : ICriticalNotifyCompletion + { + readonly UniTask task; + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Awaiter(in UniTask task) + { + this.task = task; + } + + public bool IsCompleted + { + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + return task.Status.IsCompleted(); + } + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void GetResult() + { + if (task.source == null) return; + task.source.GetResult(task.token); + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void OnCompleted(Action continuation) + { + if (task.source == null) + { + continuation(); + } + else + { + task.source.OnCompleted(AwaiterActions.InvokeContinuationDelegate, continuation, task.token); + } + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void UnsafeOnCompleted(Action continuation) + { + if (task.source == null) + { + continuation(); + } + else + { + task.source.OnCompleted(AwaiterActions.InvokeContinuationDelegate, continuation, task.token); + } + } + + /// + /// If register manually continuation, you can use it instead of for compiler OnCompleted methods. + /// + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SourceOnCompleted(Action continuation, object state) + { + if (task.source == null) + { + continuation(state); + } + else + { + task.source.OnCompleted(continuation, state, task.token); + } + } + } + } + + /// + /// Lightweight unity specified task-like object. + /// + [AsyncMethodBuilder(typeof(AsyncUniTaskMethodBuilder<>))] + [StructLayout(LayoutKind.Auto)] + public readonly struct UniTask + { + readonly IUniTaskSource source; + readonly T result; + readonly short token; + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public UniTask(T result) + { + this.source = default; + this.token = default; + this.result = result; + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public UniTask(IUniTaskSource source, short token) + { + this.source = source; + this.token = token; + this.result = default; + } + + public UniTaskStatus Status + { + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + return (source == null) ? UniTaskStatus.Succeeded : source.GetStatus(token); + } + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Awaiter GetAwaiter() + { + return new Awaiter(this); + } + + /// + /// Memoizing inner IValueTaskSource. The result UniTask can await multiple. + /// + public UniTask Preserve() + { + if (source == null) + { + return this; + } + else + { + return new UniTask(new MemoizeSource(source), token); + } + } + + public UniTask AsUniTask() + { + if (this.source == null) return UniTask.CompletedTask; + + var status = this.source.GetStatus(this.token); + if (status.IsCompletedSuccessfully()) + { + this.source.GetResult(this.token); + return UniTask.CompletedTask; + } + + // Converting UniTask -> UniTask is zero overhead. + return new UniTask(this.source, this.token); + } + + public static implicit operator UniTask(UniTask self) + { + return self.AsUniTask(); + } + +#if !UNITY_2018_3_OR_NEWER + + public static implicit operator System.Threading.Tasks.ValueTask(in UniTask self) + { + if (self.source == null) + { + return new System.Threading.Tasks.ValueTask(self.result); + } + +#if NETSTANDARD2_0 + return self.AsValueTask(); +#else + return new System.Threading.Tasks.ValueTask(self.source, self.token); +#endif + } + +#endif + + /// + /// returns (bool IsCanceled, T Result) instead of throws OperationCanceledException. + /// + public UniTask<(bool IsCanceled, T Result)> SuppressCancellationThrow() + { + if (source == null) + { + return new UniTask<(bool IsCanceled, T Result)>((false, result)); + } + + return new UniTask<(bool, T)>(new IsCanceledSource(source), token); + } + + public override string ToString() + { + return (this.source == null) ? result?.ToString() + : "(" + this.source.UnsafeGetStatus() + ")"; + } + + sealed class IsCanceledSource : IUniTaskSource<(bool, T)> + { + readonly IUniTaskSource source; + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public IsCanceledSource(IUniTaskSource source) + { + this.source = source; + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public (bool, T) GetResult(short token) + { + if (source.GetStatus(token) == UniTaskStatus.Canceled) + { + return (true, default); + } + + var result = source.GetResult(token); + return (false, result); + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public UniTaskStatus GetStatus(short token) + { + return source.GetStatus(token); + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public UniTaskStatus UnsafeGetStatus() + { + return source.UnsafeGetStatus(); + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void OnCompleted(Action continuation, object state, short token) + { + source.OnCompleted(continuation, state, token); + } + } + + sealed class MemoizeSource : IUniTaskSource + { + IUniTaskSource source; + T result; + ExceptionDispatchInfo exception; + UniTaskStatus status; + + public MemoizeSource(IUniTaskSource source) + { + this.source = source; + } + + public T GetResult(short token) + { + if (source == null) + { + if (exception != null) + { + exception.Throw(); + } + return result; + } + else + { + try + { + result = source.GetResult(token); + status = UniTaskStatus.Succeeded; + return result; + } + catch (Exception ex) + { + exception = ExceptionDispatchInfo.Capture(ex); + if (ex is OperationCanceledException) + { + status = UniTaskStatus.Canceled; + } + else + { + status = UniTaskStatus.Faulted; + } + throw; + } + finally + { + source = null; + } + } + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + if (source == null) + { + return status; + } + + return source.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + if (source == null) + { + continuation(state); + } + else + { + source.OnCompleted(continuation, state, token); + } + } + + public UniTaskStatus UnsafeGetStatus() + { + if (source == null) + { + return status; + } + + return source.UnsafeGetStatus(); + } + } + + public readonly struct Awaiter : ICriticalNotifyCompletion + { + readonly UniTask task; + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Awaiter(in UniTask task) + { + this.task = task; + } + + public bool IsCompleted + { + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + return task.Status.IsCompleted(); + } + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public T GetResult() + { + var s = task.source; + if (s == null) + { + return task.result; + } + else + { + return s.GetResult(task.token); + } + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void OnCompleted(Action continuation) + { + var s = task.source; + if (s == null) + { + continuation(); + } + else + { + s.OnCompleted(AwaiterActions.InvokeContinuationDelegate, continuation, task.token); + } + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void UnsafeOnCompleted(Action continuation) + { + var s = task.source; + if (s == null) + { + continuation(); + } + else + { + s.OnCompleted(AwaiterActions.InvokeContinuationDelegate, continuation, task.token); + } + } + + /// + /// If register manually continuation, you can use it instead of for compiler OnCompleted methods. + /// + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SourceOnCompleted(Action continuation, object state) + { + var s = task.source; + if (s == null) + { + continuation(state); + } + else + { + s.OnCompleted(continuation, state, task.token); + } + } + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.cs.meta new file mode 100644 index 0000000..04eb6b6 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTask.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8947adf23181ff04db73829df217ca94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskCompletionSource.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskCompletionSource.cs new file mode 100644 index 0000000..67b882e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskCompletionSource.cs @@ -0,0 +1,941 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Runtime.ExceptionServices; +using System.Runtime.InteropServices; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks +{ + public interface IResolvePromise + { + bool TrySetResult(); + } + + public interface IResolvePromise + { + bool TrySetResult(T value); + } + + public interface IRejectPromise + { + bool TrySetException(Exception exception); + } + + public interface ICancelPromise + { + bool TrySetCanceled(CancellationToken cancellationToken = default); + } + + public interface IPromise : IResolvePromise, IRejectPromise, ICancelPromise + { + } + + public interface IPromise : IResolvePromise, IRejectPromise, ICancelPromise + { + } + + internal class ExceptionHolder + { + ExceptionDispatchInfo exception; + bool calledGet = false; + + public ExceptionHolder(ExceptionDispatchInfo exception) + { + this.exception = exception; + } + + public ExceptionDispatchInfo GetException() + { + if (!calledGet) + { + calledGet = true; + GC.SuppressFinalize(this); + } + return exception; + } + + ~ExceptionHolder() + { + if (!calledGet) + { + UniTaskScheduler.PublishUnobservedTaskException(exception.SourceException); + } + } + } + + [StructLayout(LayoutKind.Auto)] + public struct UniTaskCompletionSourceCore + { + // Struct Size: TResult + (8 + 2 + 1 + 1 + 8 + 8) + + TResult result; + object error; // ExceptionHolder or OperationCanceledException + short version; + bool hasUnhandledError; + int completedCount; // 0: completed == false + Action continuation; + object continuationState; + + [DebuggerHidden] + public void Reset() + { + ReportUnhandledError(); + + unchecked + { + version += 1; // incr version. + } + completedCount = 0; + result = default; + error = null; + hasUnhandledError = false; + continuation = null; + continuationState = null; + } + + void ReportUnhandledError() + { + if (hasUnhandledError) + { + try + { + if (error is OperationCanceledException oc) + { + UniTaskScheduler.PublishUnobservedTaskException(oc); + } + else if (error is ExceptionHolder e) + { + UniTaskScheduler.PublishUnobservedTaskException(e.GetException().SourceException); + } + } + catch + { + } + } + } + + internal void MarkHandled() + { + hasUnhandledError = false; + } + + /// Completes with a successful result. + /// The result. + [DebuggerHidden] + public bool TrySetResult(TResult result) + { + if (Interlocked.Increment(ref completedCount) == 1) + { + // setup result + this.result = result; + + if (continuation != null || Interlocked.CompareExchange(ref this.continuation, UniTaskCompletionSourceCoreShared.s_sentinel, null) != null) + { + continuation(continuationState); + return true; + } + } + + return false; + } + + /// Completes with an error. + /// The exception. + [DebuggerHidden] + public bool TrySetException(Exception error) + { + if (Interlocked.Increment(ref completedCount) == 1) + { + // setup result + this.hasUnhandledError = true; + if (error is OperationCanceledException) + { + this.error = error; + } + else + { + this.error = new ExceptionHolder(ExceptionDispatchInfo.Capture(error)); + } + + if (continuation != null || Interlocked.CompareExchange(ref this.continuation, UniTaskCompletionSourceCoreShared.s_sentinel, null) != null) + { + continuation(continuationState); + return true; + } + } + + return false; + } + + [DebuggerHidden] + public bool TrySetCanceled(CancellationToken cancellationToken = default) + { + if (Interlocked.Increment(ref completedCount) == 1) + { + // setup result + this.hasUnhandledError = true; + this.error = new OperationCanceledException(cancellationToken); + + if (continuation != null || Interlocked.CompareExchange(ref this.continuation, UniTaskCompletionSourceCoreShared.s_sentinel, null) != null) + { + continuation(continuationState); + return true; + } + } + + return false; + } + + /// Gets the operation version. + [DebuggerHidden] + public short Version => version; + + /// Gets the status of the operation. + /// Opaque value that was provided to the 's constructor. + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public UniTaskStatus GetStatus(short token) + { + ValidateToken(token); + return (continuation == null || (completedCount == 0)) ? UniTaskStatus.Pending + : (error == null) ? UniTaskStatus.Succeeded + : (error is OperationCanceledException) ? UniTaskStatus.Canceled + : UniTaskStatus.Faulted; + } + + /// Gets the status of the operation without token validation. + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public UniTaskStatus UnsafeGetStatus() + { + return (continuation == null || (completedCount == 0)) ? UniTaskStatus.Pending + : (error == null) ? UniTaskStatus.Succeeded + : (error is OperationCanceledException) ? UniTaskStatus.Canceled + : UniTaskStatus.Faulted; + } + + /// Gets the result of the operation. + /// Opaque value that was provided to the 's constructor. + // [StackTraceHidden] + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public TResult GetResult(short token) + { + ValidateToken(token); + if (completedCount == 0) + { + throw new InvalidOperationException("Not yet completed, UniTask only allow to use await."); + } + + if (error != null) + { + hasUnhandledError = false; + if (error is OperationCanceledException oce) + { + throw oce; + } + else if (error is ExceptionHolder eh) + { + eh.GetException().Throw(); + } + + throw new InvalidOperationException("Critical: invalid exception type was held."); + } + + return result; + } + + /// Schedules the continuation action for this operation. + /// The continuation to invoke when the operation has completed. + /// The state object to pass to when it's invoked. + /// Opaque value that was provided to the 's constructor. + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void OnCompleted(Action continuation, object state, short token /*, ValueTaskSourceOnCompletedFlags flags */) + { + if (continuation == null) + { + throw new ArgumentNullException(nameof(continuation)); + } + ValidateToken(token); + + /* no use ValueTaskSourceOnCOmpletedFlags, always no capture ExecutionContext and SynchronizationContext. */ + + /* + PatternA: GetStatus=Pending => OnCompleted => TrySet*** => GetResult + PatternB: TrySet*** => GetStatus=!Pending => GetResult + PatternC: GetStatus=Pending => TrySet/OnCompleted(race condition) => GetResult + C.1: win OnCompleted -> TrySet invoke saved continuation + C.2: win TrySet -> should invoke continuation here. + */ + + // not set continuation yet. + object oldContinuation = this.continuation; + if (oldContinuation == null) + { + continuationState = state; + oldContinuation = Interlocked.CompareExchange(ref this.continuation, continuation, null); + } + + if (oldContinuation != null) + { + // already running continuation in TrySet. + // It will cause call OnCompleted multiple time, invalid. + if (!ReferenceEquals(oldContinuation, UniTaskCompletionSourceCoreShared.s_sentinel)) + { + throw new InvalidOperationException("Already continuation registered, can not await twice or get Status after await."); + } + + continuation(state); + } + } + + [DebuggerHidden] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void ValidateToken(short token) + { + if (token != version) + { + throw new InvalidOperationException("Token version is not matched, can not await twice or get Status after await."); + } + } + } + + internal static class UniTaskCompletionSourceCoreShared // separated out of generic to avoid unnecessary duplication + { + internal static readonly Action s_sentinel = CompletionSentinel; + + private static void CompletionSentinel(object _) // named method to aid debugging + { + throw new InvalidOperationException("The sentinel delegate should never be invoked."); + } + } + + public class AutoResetUniTaskCompletionSource : IUniTaskSource, ITaskPoolNode, IPromise + { + static TaskPool pool; + AutoResetUniTaskCompletionSource nextNode; + public ref AutoResetUniTaskCompletionSource NextNode => ref nextNode; + + static AutoResetUniTaskCompletionSource() + { + TaskPool.RegisterSizeGetter(typeof(AutoResetUniTaskCompletionSource), () => pool.Size); + } + + UniTaskCompletionSourceCore core; + + AutoResetUniTaskCompletionSource() + { + } + + [DebuggerHidden] + public static AutoResetUniTaskCompletionSource Create() + { + if (!pool.TryPop(out var result)) + { + result = new AutoResetUniTaskCompletionSource(); + } + TaskTracker.TrackActiveTask(result, 2); + return result; + } + + [DebuggerHidden] + public static AutoResetUniTaskCompletionSource CreateFromCanceled(CancellationToken cancellationToken, out short token) + { + var source = Create(); + source.TrySetCanceled(cancellationToken); + token = source.core.Version; + return source; + } + + [DebuggerHidden] + public static AutoResetUniTaskCompletionSource CreateFromException(Exception exception, out short token) + { + var source = Create(); + source.TrySetException(exception); + token = source.core.Version; + return source; + } + + [DebuggerHidden] + public static AutoResetUniTaskCompletionSource CreateCompleted(out short token) + { + var source = Create(); + source.TrySetResult(); + token = source.core.Version; + return source; + } + + public UniTask Task + { + [DebuggerHidden] + get + { + return new UniTask(this, core.Version); + } + } + + [DebuggerHidden] + public bool TrySetResult() + { + return core.TrySetResult(AsyncUnit.Default); + } + + [DebuggerHidden] + public bool TrySetCanceled(CancellationToken cancellationToken = default) + { + return core.TrySetCanceled(cancellationToken); + } + + [DebuggerHidden] + public bool TrySetException(Exception exception) + { + return core.TrySetException(exception); + } + + [DebuggerHidden] + public void GetResult(short token) + { + try + { + core.GetResult(token); + } + finally + { + TryReturn(); + } + + } + + [DebuggerHidden] + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + [DebuggerHidden] + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + [DebuggerHidden] + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + [DebuggerHidden] + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + return pool.TryPush(this); + } + } + + public class AutoResetUniTaskCompletionSource : IUniTaskSource, ITaskPoolNode>, IPromise + { + static TaskPool> pool; + AutoResetUniTaskCompletionSource nextNode; + public ref AutoResetUniTaskCompletionSource NextNode => ref nextNode; + + static AutoResetUniTaskCompletionSource() + { + TaskPool.RegisterSizeGetter(typeof(AutoResetUniTaskCompletionSource), () => pool.Size); + } + + UniTaskCompletionSourceCore core; + + AutoResetUniTaskCompletionSource() + { + } + + [DebuggerHidden] + public static AutoResetUniTaskCompletionSource Create() + { + if (!pool.TryPop(out var result)) + { + result = new AutoResetUniTaskCompletionSource(); + } + TaskTracker.TrackActiveTask(result, 2); + return result; + } + + [DebuggerHidden] + public static AutoResetUniTaskCompletionSource CreateFromCanceled(CancellationToken cancellationToken, out short token) + { + var source = Create(); + source.TrySetCanceled(cancellationToken); + token = source.core.Version; + return source; + } + + [DebuggerHidden] + public static AutoResetUniTaskCompletionSource CreateFromException(Exception exception, out short token) + { + var source = Create(); + source.TrySetException(exception); + token = source.core.Version; + return source; + } + + [DebuggerHidden] + public static AutoResetUniTaskCompletionSource CreateFromResult(T result, out short token) + { + var source = Create(); + source.TrySetResult(result); + token = source.core.Version; + return source; + } + + public UniTask Task + { + [DebuggerHidden] + get + { + return new UniTask(this, core.Version); + } + } + + [DebuggerHidden] + public bool TrySetResult(T result) + { + return core.TrySetResult(result); + } + + [DebuggerHidden] + public bool TrySetCanceled(CancellationToken cancellationToken = default) + { + return core.TrySetCanceled(cancellationToken); + } + + [DebuggerHidden] + public bool TrySetException(Exception exception) + { + return core.TrySetException(exception); + } + + [DebuggerHidden] + public T GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + [DebuggerHidden] + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + [DebuggerHidden] + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + [DebuggerHidden] + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + [DebuggerHidden] + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + [DebuggerHidden] + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + return pool.TryPush(this); + } + } + + public class UniTaskCompletionSource : IUniTaskSource, IPromise + { + CancellationToken cancellationToken; + ExceptionHolder exception; + object gate; + Action singleContinuation; + object singleState; + List<(Action, object)> secondaryContinuationList; + + int intStatus; // UniTaskStatus + bool handled = false; + + public UniTaskCompletionSource() + { + TaskTracker.TrackActiveTask(this, 2); + } + + [DebuggerHidden] + internal void MarkHandled() + { + if (!handled) + { + handled = true; + TaskTracker.RemoveTracking(this); + } + } + + public UniTask Task + { + [DebuggerHidden] + get + { + return new UniTask(this, 0); + } + } + + [DebuggerHidden] + public bool TrySetResult() + { + return TrySignalCompletion(UniTaskStatus.Succeeded); + } + + [DebuggerHidden] + public bool TrySetCanceled(CancellationToken cancellationToken = default) + { + if (UnsafeGetStatus() != UniTaskStatus.Pending) return false; + + this.cancellationToken = cancellationToken; + return TrySignalCompletion(UniTaskStatus.Canceled); + } + + [DebuggerHidden] + public bool TrySetException(Exception exception) + { + if (exception is OperationCanceledException oce) + { + return TrySetCanceled(oce.CancellationToken); + } + + if (UnsafeGetStatus() != UniTaskStatus.Pending) return false; + + this.exception = new ExceptionHolder(ExceptionDispatchInfo.Capture(exception)); + return TrySignalCompletion(UniTaskStatus.Faulted); + } + + [DebuggerHidden] + public void GetResult(short token) + { + MarkHandled(); + + var status = (UniTaskStatus)intStatus; + switch (status) + { + case UniTaskStatus.Succeeded: + return; + case UniTaskStatus.Faulted: + exception.GetException().Throw(); + return; + case UniTaskStatus.Canceled: + throw new OperationCanceledException(cancellationToken); + default: + case UniTaskStatus.Pending: + throw new InvalidOperationException("not yet completed."); + } + } + + [DebuggerHidden] + public UniTaskStatus GetStatus(short token) + { + return (UniTaskStatus)intStatus; + } + + [DebuggerHidden] + public UniTaskStatus UnsafeGetStatus() + { + return (UniTaskStatus)intStatus; + } + + [DebuggerHidden] + public void OnCompleted(Action continuation, object state, short token) + { + if (gate == null) + { + Interlocked.CompareExchange(ref gate, new object(), null); + } + + var lockGate = Thread.VolatileRead(ref gate); + lock (lockGate) // wait TrySignalCompletion, after status is not pending. + { + if ((UniTaskStatus)intStatus != UniTaskStatus.Pending) + { + continuation(state); + return; + } + + if (singleContinuation == null) + { + singleContinuation = continuation; + singleState = state; + } + else + { + if (secondaryContinuationList == null) + { + secondaryContinuationList = new List<(Action, object)>(); + } + secondaryContinuationList.Add((continuation, state)); + } + } + } + + [DebuggerHidden] + bool TrySignalCompletion(UniTaskStatus status) + { + if (Interlocked.CompareExchange(ref intStatus, (int)status, (int)UniTaskStatus.Pending) == (int)UniTaskStatus.Pending) + { + if (gate == null) + { + Interlocked.CompareExchange(ref gate, new object(), null); + } + + var lockGate = Thread.VolatileRead(ref gate); + lock (lockGate) // wait OnCompleted. + { + if (singleContinuation != null) + { + try + { + singleContinuation(singleState); + } + catch (Exception ex) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + } + } + + if (secondaryContinuationList != null) + { + foreach (var (c, state) in secondaryContinuationList) + { + try + { + c(state); + } + catch (Exception ex) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + } + } + } + + singleContinuation = null; + singleState = null; + secondaryContinuationList = null; + } + return true; + } + return false; + } + } + + public class UniTaskCompletionSource : IUniTaskSource, IPromise + { + CancellationToken cancellationToken; + T result; + ExceptionHolder exception; + object gate; + Action singleContinuation; + object singleState; + List<(Action, object)> secondaryContinuationList; + + int intStatus; // UniTaskStatus + bool handled = false; + + public UniTaskCompletionSource() + { + TaskTracker.TrackActiveTask(this, 2); + } + + [DebuggerHidden] + internal void MarkHandled() + { + if (!handled) + { + handled = true; + TaskTracker.RemoveTracking(this); + } + } + + public UniTask Task + { + [DebuggerHidden] + get + { + return new UniTask(this, 0); + } + } + + [DebuggerHidden] + public bool TrySetResult(T result) + { + if (UnsafeGetStatus() != UniTaskStatus.Pending) return false; + + this.result = result; + return TrySignalCompletion(UniTaskStatus.Succeeded); + } + + [DebuggerHidden] + public bool TrySetCanceled(CancellationToken cancellationToken = default) + { + if (UnsafeGetStatus() != UniTaskStatus.Pending) return false; + + this.cancellationToken = cancellationToken; + return TrySignalCompletion(UniTaskStatus.Canceled); + } + + [DebuggerHidden] + public bool TrySetException(Exception exception) + { + if (exception is OperationCanceledException oce) + { + return TrySetCanceled(oce.CancellationToken); + } + + if (UnsafeGetStatus() != UniTaskStatus.Pending) return false; + + this.exception = new ExceptionHolder(ExceptionDispatchInfo.Capture(exception)); + return TrySignalCompletion(UniTaskStatus.Faulted); + } + + [DebuggerHidden] + public T GetResult(short token) + { + MarkHandled(); + + var status = (UniTaskStatus)intStatus; + switch (status) + { + case UniTaskStatus.Succeeded: + return result; + case UniTaskStatus.Faulted: + exception.GetException().Throw(); + return default; + case UniTaskStatus.Canceled: + throw new OperationCanceledException(cancellationToken); + default: + case UniTaskStatus.Pending: + throw new InvalidOperationException("not yet completed."); + } + } + + [DebuggerHidden] + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + [DebuggerHidden] + public UniTaskStatus GetStatus(short token) + { + return (UniTaskStatus)intStatus; + } + + [DebuggerHidden] + public UniTaskStatus UnsafeGetStatus() + { + return (UniTaskStatus)intStatus; + } + + [DebuggerHidden] + public void OnCompleted(Action continuation, object state, short token) + { + if (gate == null) + { + Interlocked.CompareExchange(ref gate, new object(), null); + } + + var lockGate = Thread.VolatileRead(ref gate); + lock (lockGate) // wait TrySignalCompletion, after status is not pending. + { + if ((UniTaskStatus)intStatus != UniTaskStatus.Pending) + { + continuation(state); + return; + } + + if (singleContinuation == null) + { + singleContinuation = continuation; + singleState = state; + } + else + { + if (secondaryContinuationList == null) + { + secondaryContinuationList = new List<(Action, object)>(); + } + secondaryContinuationList.Add((continuation, state)); + } + } + } + + [DebuggerHidden] + bool TrySignalCompletion(UniTaskStatus status) + { + if (Interlocked.CompareExchange(ref intStatus, (int)status, (int)UniTaskStatus.Pending) == (int)UniTaskStatus.Pending) + { + if (gate == null) + { + Interlocked.CompareExchange(ref gate, new object(), null); + } + + var lockGate = Thread.VolatileRead(ref gate); + lock (lockGate) // wait OnCompleted. + { + if (singleContinuation != null) + { + try + { + singleContinuation(singleState); + } + catch (Exception ex) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + } + } + + if (secondaryContinuationList != null) + { + foreach (var (c, state) in secondaryContinuationList) + { + try + { + c(state); + } + catch (Exception ex) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + } + } + } + + singleContinuation = null; + singleState = null; + secondaryContinuationList = null; + } + return true; + } + return false; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskCompletionSource.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskCompletionSource.cs.meta new file mode 100644 index 0000000..2ae5ee3 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskCompletionSource.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ed03524d09e7eb24a9fb9137198feb84 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.Shorthand.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.Shorthand.cs new file mode 100644 index 0000000..0e51a45 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.Shorthand.cs @@ -0,0 +1,187 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member +using System.Collections.Generic; + +namespace Cysharp.Threading.Tasks +{ + public static partial class UniTaskExtensions + { + // shorthand of WhenAll + + public static UniTask.Awaiter GetAwaiter(this UniTask[] tasks) + { + return UniTask.WhenAll(tasks).GetAwaiter(); + } + + public static UniTask.Awaiter GetAwaiter(this IEnumerable tasks) + { + return UniTask.WhenAll(tasks).GetAwaiter(); + } + + public static UniTask.Awaiter GetAwaiter(this UniTask[] tasks) + { + return UniTask.WhenAll(tasks).GetAwaiter(); + } + + public static UniTask.Awaiter GetAwaiter(this IEnumerable> tasks) + { + return UniTask.WhenAll(tasks).GetAwaiter(); + } + + public static UniTask<(T1, T2)>.Awaiter GetAwaiter(this (UniTask task1, UniTask task2) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2).GetAwaiter(); + } + + public static UniTask<(T1, T2, T3)>.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3).GetAwaiter(); + } + + public static UniTask<(T1, T2, T3, T4)>.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4).GetAwaiter(); + } + + public static UniTask<(T1, T2, T3, T4, T5)>.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5).GetAwaiter(); + } + + public static UniTask<(T1, T2, T3, T4, T5, T6)>.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6).GetAwaiter(); + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7)>.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7).GetAwaiter(); + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8)>.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8).GetAwaiter(); + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9)>.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9).GetAwaiter(); + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)>.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10).GetAwaiter(); + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)>.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11).GetAwaiter(); + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)>.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11, tasks.Item12).GetAwaiter(); + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)>.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11, tasks.Item12, tasks.Item13).GetAwaiter(); + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)>.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13, UniTask task14) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11, tasks.Item12, tasks.Item13, tasks.Item14).GetAwaiter(); + } + + public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)>.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13, UniTask task14, UniTask task15) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11, tasks.Item12, tasks.Item13, tasks.Item14, tasks.Item15).GetAwaiter(); + } + + + + public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2).GetAwaiter(); + } + + + public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3).GetAwaiter(); + } + + + public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4).GetAwaiter(); + } + + + public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5).GetAwaiter(); + } + + + public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6).GetAwaiter(); + } + + + public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7).GetAwaiter(); + } + + + public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8).GetAwaiter(); + } + + + public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9).GetAwaiter(); + } + + + public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10).GetAwaiter(); + } + + + public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11).GetAwaiter(); + } + + + public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11, tasks.Item12).GetAwaiter(); + } + + + public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11, tasks.Item12, tasks.Item13).GetAwaiter(); + } + + + public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13, UniTask task14) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11, tasks.Item12, tasks.Item13, tasks.Item14).GetAwaiter(); + } + + + public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13, UniTask task14, UniTask task15) tasks) + { + return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11, tasks.Item12, tasks.Item13, tasks.Item14, tasks.Item15).GetAwaiter(); + } + + + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.Shorthand.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.Shorthand.cs.meta new file mode 100644 index 0000000..e2dcc14 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.Shorthand.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4b4ff020f73dc6d4b8ebd4760d61fb43 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.Shorthand.tt b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.Shorthand.tt new file mode 100644 index 0000000..94f6b9f --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.Shorthand.tt @@ -0,0 +1,63 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".cs" #> +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member +using System.Collections.Generic; + +namespace Cysharp.Threading.Tasks +{ + public static partial class UniTaskExtensions + { + // shorthand of WhenAll + + public static UniTask.Awaiter GetAwaiter(this UniTask[] tasks) + { + return UniTask.WhenAll(tasks).GetAwaiter(); + } + + public static UniTask.Awaiter GetAwaiter(this IEnumerable tasks) + { + return UniTask.WhenAll(tasks).GetAwaiter(); + } + + public static UniTask.Awaiter GetAwaiter(this UniTask[] tasks) + { + return UniTask.WhenAll(tasks).GetAwaiter(); + } + + public static UniTask.Awaiter GetAwaiter(this IEnumerable> tasks) + { + return UniTask.WhenAll(tasks).GetAwaiter(); + } + +<# for(var i = 2; i <= 15; i++ ) { + var range = Enumerable.Range(1, i); + var t = string.Join(", ", range.Select(x => "T" + x)); + var args = string.Join(", ", range.Select(x => $"UniTask task{x}")); + var titems = string.Join(", ", range.Select(x => $"tasks.Item{x}")); +#> + public static UniTask<(<#= t #>)>.Awaiter GetAwaiter<<#= t #>>(this (<#= args #>) tasks) + { + return UniTask.WhenAll(<#= titems #>).GetAwaiter(); + } + +<# } #> + +<# for(var i = 2; i <= 15; i++ ) { + var range = Enumerable.Range(1, i); + var args = string.Join(", ", range.Select(x => $"UniTask task{x}")); + var titems = string.Join(", ", range.Select(x => $"tasks.Item{x}")); +#> + + public static UniTask.Awaiter GetAwaiter(this (<#= args #>) tasks) + { + return UniTask.WhenAll(<#= titems #>).GetAwaiter(); + } + +<# } #> + + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.Shorthand.tt.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.Shorthand.tt.meta new file mode 100644 index 0000000..4b7ff9e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.Shorthand.tt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 9a75b5d7e55a5a34fb6476586df37c72 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.cs new file mode 100644 index 0000000..d330109 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.cs @@ -0,0 +1,921 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Collections; +using System.Runtime.ExceptionServices; +using System.Threading; +using System.Threading.Tasks; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks +{ + public static partial class UniTaskExtensions + { + /// + /// Convert Task[T] -> UniTask[T]. + /// + public static UniTask AsUniTask(this Task task, bool useCurrentSynchronizationContext = true) + { + var promise = new UniTaskCompletionSource(); + + task.ContinueWith((x, state) => + { + var p = (UniTaskCompletionSource)state; + + switch (x.Status) + { + case TaskStatus.Canceled: + p.TrySetCanceled(); + break; + case TaskStatus.Faulted: + p.TrySetException(x.Exception); + break; + case TaskStatus.RanToCompletion: + p.TrySetResult(x.Result); + break; + default: + throw new NotSupportedException(); + } + }, promise, useCurrentSynchronizationContext ? TaskScheduler.FromCurrentSynchronizationContext() : TaskScheduler.Current); + + return promise.Task; + } + + /// + /// Convert Task -> UniTask. + /// + public static UniTask AsUniTask(this Task task, bool useCurrentSynchronizationContext = true) + { + var promise = new UniTaskCompletionSource(); + + task.ContinueWith((x, state) => + { + var p = (UniTaskCompletionSource)state; + + switch (x.Status) + { + case TaskStatus.Canceled: + p.TrySetCanceled(); + break; + case TaskStatus.Faulted: + p.TrySetException(x.Exception); + break; + case TaskStatus.RanToCompletion: + p.TrySetResult(); + break; + default: + throw new NotSupportedException(); + } + }, promise, useCurrentSynchronizationContext ? TaskScheduler.FromCurrentSynchronizationContext() : TaskScheduler.Current); + + return promise.Task; + } + + public static Task AsTask(this UniTask task) + { + try + { + UniTask.Awaiter awaiter; + try + { + awaiter = task.GetAwaiter(); + } + catch (Exception ex) + { + return Task.FromException(ex); + } + + if (awaiter.IsCompleted) + { + try + { + var result = awaiter.GetResult(); + return Task.FromResult(result); + } + catch (Exception ex) + { + return Task.FromException(ex); + } + } + + var tcs = new TaskCompletionSource(); + + awaiter.SourceOnCompleted(state => + { + using (var tuple = (StateTuple, UniTask.Awaiter>)state) + { + var (inTcs, inAwaiter) = tuple; + try + { + var result = inAwaiter.GetResult(); + inTcs.SetResult(result); + } + catch (Exception ex) + { + inTcs.SetException(ex); + } + } + }, StateTuple.Create(tcs, awaiter)); + + return tcs.Task; + } + catch (Exception ex) + { + return Task.FromException(ex); + } + } + + public static Task AsTask(this UniTask task) + { + try + { + UniTask.Awaiter awaiter; + try + { + awaiter = task.GetAwaiter(); + } + catch (Exception ex) + { + return Task.FromException(ex); + } + + if (awaiter.IsCompleted) + { + try + { + awaiter.GetResult(); // check token valid on Succeeded + return Task.CompletedTask; + } + catch (Exception ex) + { + return Task.FromException(ex); + } + } + + var tcs = new TaskCompletionSource(); + + awaiter.SourceOnCompleted(state => + { + using (var tuple = (StateTuple, UniTask.Awaiter>)state) + { + var (inTcs, inAwaiter) = tuple; + try + { + inAwaiter.GetResult(); + inTcs.SetResult(null); + } + catch (Exception ex) + { + inTcs.SetException(ex); + } + } + }, StateTuple.Create(tcs, awaiter)); + + return tcs.Task; + } + catch (Exception ex) + { + return Task.FromException(ex); + } + } + + public static AsyncLazy ToAsyncLazy(this UniTask task) + { + return new AsyncLazy(task); + } + + public static AsyncLazy ToAsyncLazy(this UniTask task) + { + return new AsyncLazy(task); + } + + /// + /// Ignore task result when cancel raised first. + /// + public static UniTask AttachExternalCancellation(this UniTask task, CancellationToken cancellationToken) + { + if (!cancellationToken.CanBeCanceled) + { + return task; + } + + if (cancellationToken.IsCancellationRequested) + { + return UniTask.FromCanceled(cancellationToken); + } + + if (task.Status.IsCompleted()) + { + return task; + } + + return new UniTask(new AttachExternalCancellationSource(task, cancellationToken), 0); + } + + /// + /// Ignore task result when cancel raised first. + /// + public static UniTask AttachExternalCancellation(this UniTask task, CancellationToken cancellationToken) + { + if (!cancellationToken.CanBeCanceled) + { + return task; + } + + if (cancellationToken.IsCancellationRequested) + { + return UniTask.FromCanceled(cancellationToken); + } + + if (task.Status.IsCompleted()) + { + return task; + } + + return new UniTask(new AttachExternalCancellationSource(task, cancellationToken), 0); + } + + sealed class AttachExternalCancellationSource : IUniTaskSource + { + static readonly Action cancellationCallbackDelegate = CancellationCallback; + + CancellationToken cancellationToken; + CancellationTokenRegistration tokenRegistration; + UniTaskCompletionSourceCore core; + + public AttachExternalCancellationSource(UniTask task, CancellationToken cancellationToken) + { + this.cancellationToken = cancellationToken; + this.tokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallbackDelegate, this); + RunTask(task).Forget(); + } + + async UniTaskVoid RunTask(UniTask task) + { + try + { + await task; + core.TrySetResult(AsyncUnit.Default); + } + catch (Exception ex) + { + core.TrySetException(ex); + } + finally + { + tokenRegistration.Dispose(); + } + } + + static void CancellationCallback(object state) + { + var self = (AttachExternalCancellationSource)state; + self.core.TrySetCanceled(self.cancellationToken); + } + + public void GetResult(short token) + { + core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + } + + sealed class AttachExternalCancellationSource : IUniTaskSource + { + static readonly Action cancellationCallbackDelegate = CancellationCallback; + + CancellationToken cancellationToken; + CancellationTokenRegistration tokenRegistration; + UniTaskCompletionSourceCore core; + + public AttachExternalCancellationSource(UniTask task, CancellationToken cancellationToken) + { + this.cancellationToken = cancellationToken; + this.tokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallbackDelegate, this); + RunTask(task).Forget(); + } + + async UniTaskVoid RunTask(UniTask task) + { + try + { + core.TrySetResult(await task); + } + catch (Exception ex) + { + core.TrySetException(ex); + } + finally + { + tokenRegistration.Dispose(); + } + } + + static void CancellationCallback(object state) + { + var self = (AttachExternalCancellationSource)state; + self.core.TrySetCanceled(self.cancellationToken); + } + + void IUniTaskSource.GetResult(short token) + { + core.GetResult(token); + } + + public T GetResult(short token) + { + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + } + +#if UNITY_2018_3_OR_NEWER + + public static IEnumerator ToCoroutine(this UniTask task, Action resultHandler = null, Action exceptionHandler = null) + { + return new ToCoroutineEnumerator(task, resultHandler, exceptionHandler); + } + + public static IEnumerator ToCoroutine(this UniTask task, Action exceptionHandler = null) + { + return new ToCoroutineEnumerator(task, exceptionHandler); + } + + public static async UniTask Timeout(this UniTask task, TimeSpan timeout, DelayType delayType = DelayType.DeltaTime, PlayerLoopTiming timeoutCheckTiming = PlayerLoopTiming.Update, CancellationTokenSource taskCancellationTokenSource = null) + { + var delayCancellationTokenSource = new CancellationTokenSource(); + var timeoutTask = UniTask.Delay(timeout, delayType, timeoutCheckTiming, delayCancellationTokenSource.Token).SuppressCancellationThrow(); + + int winArgIndex; + bool taskResultIsCanceled; + try + { + (winArgIndex, taskResultIsCanceled, _) = await UniTask.WhenAny(task.SuppressCancellationThrow(), timeoutTask); + } + catch + { + delayCancellationTokenSource.Cancel(); + delayCancellationTokenSource.Dispose(); + throw; + } + + // timeout + if (winArgIndex == 1) + { + if (taskCancellationTokenSource != null) + { + taskCancellationTokenSource.Cancel(); + taskCancellationTokenSource.Dispose(); + } + + throw new TimeoutException("Exceed Timeout:" + timeout); + } + else + { + delayCancellationTokenSource.Cancel(); + delayCancellationTokenSource.Dispose(); + } + + if (taskResultIsCanceled) + { + Error.ThrowOperationCanceledException(); + } + } + + public static async UniTask Timeout(this UniTask task, TimeSpan timeout, DelayType delayType = DelayType.DeltaTime, PlayerLoopTiming timeoutCheckTiming = PlayerLoopTiming.Update, CancellationTokenSource taskCancellationTokenSource = null) + { + var delayCancellationTokenSource = new CancellationTokenSource(); + var timeoutTask = UniTask.Delay(timeout, delayType, timeoutCheckTiming, delayCancellationTokenSource.Token).SuppressCancellationThrow(); + + int winArgIndex; + (bool IsCanceled, T Result) taskResult; + try + { + (winArgIndex, taskResult, _) = await UniTask.WhenAny(task.SuppressCancellationThrow(), timeoutTask); + } + catch + { + delayCancellationTokenSource.Cancel(); + delayCancellationTokenSource.Dispose(); + throw; + } + + // timeout + if (winArgIndex == 1) + { + if (taskCancellationTokenSource != null) + { + taskCancellationTokenSource.Cancel(); + taskCancellationTokenSource.Dispose(); + } + + throw new TimeoutException("Exceed Timeout:" + timeout); + } + else + { + delayCancellationTokenSource.Cancel(); + delayCancellationTokenSource.Dispose(); + } + + if (taskResult.IsCanceled) + { + Error.ThrowOperationCanceledException(); + } + + return taskResult.Result; + } + + /// + /// Timeout with suppress OperationCanceledException. Returns (bool, IsCacneled). + /// + public static async UniTask TimeoutWithoutException(this UniTask task, TimeSpan timeout, DelayType delayType = DelayType.DeltaTime, PlayerLoopTiming timeoutCheckTiming = PlayerLoopTiming.Update, CancellationTokenSource taskCancellationTokenSource = null) + { + var delayCancellationTokenSource = new CancellationTokenSource(); + var timeoutTask = UniTask.Delay(timeout, delayType, timeoutCheckTiming, delayCancellationTokenSource.Token).SuppressCancellationThrow(); + + int winArgIndex; + bool taskResultIsCanceled; + try + { + (winArgIndex, taskResultIsCanceled, _) = await UniTask.WhenAny(task.SuppressCancellationThrow(), timeoutTask); + } + catch + { + delayCancellationTokenSource.Cancel(); + delayCancellationTokenSource.Dispose(); + return true; + } + + // timeout + if (winArgIndex == 1) + { + if (taskCancellationTokenSource != null) + { + taskCancellationTokenSource.Cancel(); + taskCancellationTokenSource.Dispose(); + } + + return true; + } + else + { + delayCancellationTokenSource.Cancel(); + delayCancellationTokenSource.Dispose(); + } + + if (taskResultIsCanceled) + { + return true; + } + + return false; + } + + /// + /// Timeout with suppress OperationCanceledException. Returns (bool IsTimeout, T Result). + /// + public static async UniTask<(bool IsTimeout, T Result)> TimeoutWithoutException(this UniTask task, TimeSpan timeout, DelayType delayType = DelayType.DeltaTime, PlayerLoopTiming timeoutCheckTiming = PlayerLoopTiming.Update, CancellationTokenSource taskCancellationTokenSource = null) + { + var delayCancellationTokenSource = new CancellationTokenSource(); + var timeoutTask = UniTask.Delay(timeout, delayType, timeoutCheckTiming, delayCancellationTokenSource.Token).SuppressCancellationThrow(); + + int winArgIndex; + (bool IsCanceled, T Result) taskResult; + try + { + (winArgIndex, taskResult, _) = await UniTask.WhenAny(task.SuppressCancellationThrow(), timeoutTask); + } + catch + { + delayCancellationTokenSource.Cancel(); + delayCancellationTokenSource.Dispose(); + return (true, default); + } + + // timeout + if (winArgIndex == 1) + { + if (taskCancellationTokenSource != null) + { + taskCancellationTokenSource.Cancel(); + taskCancellationTokenSource.Dispose(); + } + + return (true, default); + } + else + { + delayCancellationTokenSource.Cancel(); + delayCancellationTokenSource.Dispose(); + } + + if (taskResult.IsCanceled) + { + return (true, default); + } + + return (false, taskResult.Result); + } + +#endif + + public static void Forget(this UniTask task) + { + var awaiter = task.GetAwaiter(); + if (awaiter.IsCompleted) + { + try + { + awaiter.GetResult(); + } + catch (Exception ex) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + } + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple)state) + { + try + { + t.Item1.GetResult(); + } + catch (Exception ex) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + } + } + }, StateTuple.Create(awaiter)); + } + } + + public static void Forget(this UniTask task, Action exceptionHandler, bool handleExceptionOnMainThread = true) + { + if (exceptionHandler == null) + { + Forget(task); + } + else + { + ForgetCoreWithCatch(task, exceptionHandler, handleExceptionOnMainThread).Forget(); + } + } + + static async UniTaskVoid ForgetCoreWithCatch(UniTask task, Action exceptionHandler, bool handleExceptionOnMainThread) + { + try + { + await task; + } + catch (Exception ex) + { + try + { + if (handleExceptionOnMainThread) + { +#if UNITY_2018_3_OR_NEWER + await UniTask.SwitchToMainThread(); +#endif + } + exceptionHandler(ex); + } + catch (Exception ex2) + { + UniTaskScheduler.PublishUnobservedTaskException(ex2); + } + } + } + + public static void Forget(this UniTask task) + { + var awaiter = task.GetAwaiter(); + if (awaiter.IsCompleted) + { + try + { + awaiter.GetResult(); + } + catch (Exception ex) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + } + } + else + { + awaiter.SourceOnCompleted(state => + { + using (var t = (StateTuple.Awaiter>)state) + { + try + { + t.Item1.GetResult(); + } + catch (Exception ex) + { + UniTaskScheduler.PublishUnobservedTaskException(ex); + } + } + }, StateTuple.Create(awaiter)); + } + } + + public static void Forget(this UniTask task, Action exceptionHandler, bool handleExceptionOnMainThread = true) + { + if (exceptionHandler == null) + { + task.Forget(); + } + else + { + ForgetCoreWithCatch(task, exceptionHandler, handleExceptionOnMainThread).Forget(); + } + } + + static async UniTaskVoid ForgetCoreWithCatch(UniTask task, Action exceptionHandler, bool handleExceptionOnMainThread) + { + try + { + await task; + } + catch (Exception ex) + { + try + { + if (handleExceptionOnMainThread) + { +#if UNITY_2018_3_OR_NEWER + await UniTask.SwitchToMainThread(); +#endif + } + exceptionHandler(ex); + } + catch (Exception ex2) + { + UniTaskScheduler.PublishUnobservedTaskException(ex2); + } + } + } + + public static async UniTask ContinueWith(this UniTask task, Action continuationFunction) + { + continuationFunction(await task); + } + + public static async UniTask ContinueWith(this UniTask task, Func continuationFunction) + { + await continuationFunction(await task); + } + + public static async UniTask ContinueWith(this UniTask task, Func continuationFunction) + { + return continuationFunction(await task); + } + + public static async UniTask ContinueWith(this UniTask task, Func> continuationFunction) + { + return await continuationFunction(await task); + } + + public static async UniTask ContinueWith(this UniTask task, Action continuationFunction) + { + await task; + continuationFunction(); + } + + public static async UniTask ContinueWith(this UniTask task, Func continuationFunction) + { + await task; + await continuationFunction(); + } + + public static async UniTask ContinueWith(this UniTask task, Func continuationFunction) + { + await task; + return continuationFunction(); + } + + public static async UniTask ContinueWith(this UniTask task, Func> continuationFunction) + { + await task; + return await continuationFunction(); + } + + public static async UniTask Unwrap(this UniTask> task) + { + return await await task; + } + + public static async UniTask Unwrap(this UniTask task) + { + await await task; + } + + public static async UniTask Unwrap(this Task> task) + { + return await await task; + } + + public static async UniTask Unwrap(this Task> task, bool continueOnCapturedContext) + { + return await await task.ConfigureAwait(continueOnCapturedContext); + } + + public static async UniTask Unwrap(this Task task) + { + await await task; + } + + public static async UniTask Unwrap(this Task task, bool continueOnCapturedContext) + { + await await task.ConfigureAwait(continueOnCapturedContext); + } + + public static async UniTask Unwrap(this UniTask> task) + { + return await await task; + } + + public static async UniTask Unwrap(this UniTask> task, bool continueOnCapturedContext) + { + return await (await task).ConfigureAwait(continueOnCapturedContext); + } + + public static async UniTask Unwrap(this UniTask task) + { + await await task; + } + + public static async UniTask Unwrap(this UniTask task, bool continueOnCapturedContext) + { + await (await task).ConfigureAwait(continueOnCapturedContext); + } + +#if UNITY_2018_3_OR_NEWER + + sealed class ToCoroutineEnumerator : IEnumerator + { + bool completed; + UniTask task; + Action exceptionHandler = null; + bool isStarted = false; + ExceptionDispatchInfo exception; + + public ToCoroutineEnumerator(UniTask task, Action exceptionHandler) + { + completed = false; + this.exceptionHandler = exceptionHandler; + this.task = task; + } + + async UniTaskVoid RunTask(UniTask task) + { + try + { + await task; + } + catch (Exception ex) + { + if (exceptionHandler != null) + { + exceptionHandler(ex); + } + else + { + this.exception = ExceptionDispatchInfo.Capture(ex); + } + } + finally + { + completed = true; + } + } + + public object Current => null; + + public bool MoveNext() + { + if (!isStarted) + { + isStarted = true; + RunTask(task).Forget(); + } + + if (exception != null) + { + exception.Throw(); + return false; + } + + return !completed; + } + + void IEnumerator.Reset() + { + } + } + + sealed class ToCoroutineEnumerator : IEnumerator + { + bool completed; + Action resultHandler = null; + Action exceptionHandler = null; + bool isStarted = false; + UniTask task; + object current = null; + ExceptionDispatchInfo exception; + + public ToCoroutineEnumerator(UniTask task, Action resultHandler, Action exceptionHandler) + { + completed = false; + this.task = task; + this.resultHandler = resultHandler; + this.exceptionHandler = exceptionHandler; + } + + async UniTaskVoid RunTask(UniTask task) + { + try + { + var value = await task; + current = value; // boxed if T is struct... + if (resultHandler != null) + { + resultHandler(value); + } + } + catch (Exception ex) + { + if (exceptionHandler != null) + { + exceptionHandler(ex); + } + else + { + this.exception = ExceptionDispatchInfo.Capture(ex); + } + } + finally + { + completed = true; + } + } + + public object Current => current; + + public bool MoveNext() + { + if (!isStarted) + { + isStarted = true; + RunTask(task).Forget(); + } + + if (exception != null) + { + exception.Throw(); + return false; + } + + return !completed; + } + + void IEnumerator.Reset() + { + } + } + +#endif + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.cs.meta new file mode 100644 index 0000000..0d22946 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 05460c617dae1e440861a7438535389f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskObservableExtensions.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskObservableExtensions.cs new file mode 100644 index 0000000..d54a5fd --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskObservableExtensions.cs @@ -0,0 +1,750 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Runtime.ExceptionServices; +using System.Threading; +using Cysharp.Threading.Tasks.Internal; + +namespace Cysharp.Threading.Tasks +{ + public static class UniTaskObservableExtensions + { + public static UniTask ToUniTask(this IObservable source, bool useFirstValue = false, CancellationToken cancellationToken = default) + { + var promise = new UniTaskCompletionSource(); + var disposable = new SingleAssignmentDisposable(); + + var observer = useFirstValue + ? (IObserver)new FirstValueToUniTaskObserver(promise, disposable, cancellationToken) + : (IObserver)new ToUniTaskObserver(promise, disposable, cancellationToken); + + try + { + disposable.Disposable = source.Subscribe(observer); + } + catch (Exception ex) + { + promise.TrySetException(ex); + } + + return promise.Task; + } + + public static IObservable ToObservable(this UniTask task) + { + if (task.Status.IsCompleted()) + { + try + { + return new ReturnObservable(task.GetAwaiter().GetResult()); + } + catch (Exception ex) + { + return new ThrowObservable(ex); + } + } + + var subject = new AsyncSubject(); + Fire(subject, task).Forget(); + return subject; + } + + /// + /// Ideally returns IObservabl[Unit] is best but Cysharp.Threading.Tasks does not have Unit so return AsyncUnit instead. + /// + public static IObservable ToObservable(this UniTask task) + { + if (task.Status.IsCompleted()) + { + try + { + task.GetAwaiter().GetResult(); + return new ReturnObservable(AsyncUnit.Default); + } + catch (Exception ex) + { + return new ThrowObservable(ex); + } + } + + var subject = new AsyncSubject(); + Fire(subject, task).Forget(); + return subject; + } + + static async UniTaskVoid Fire(AsyncSubject subject, UniTask task) + { + T value; + try + { + value = await task; + } + catch (Exception ex) + { + subject.OnError(ex); + return; + } + + subject.OnNext(value); + subject.OnCompleted(); + } + + static async UniTaskVoid Fire(AsyncSubject subject, UniTask task) + { + try + { + await task; + } + catch (Exception ex) + { + subject.OnError(ex); + return; + } + + subject.OnNext(AsyncUnit.Default); + subject.OnCompleted(); + } + + class ToUniTaskObserver : IObserver + { + static readonly Action callback = OnCanceled; + + readonly UniTaskCompletionSource promise; + readonly SingleAssignmentDisposable disposable; + readonly CancellationToken cancellationToken; + readonly CancellationTokenRegistration registration; + + bool hasValue; + T latestValue; + + public ToUniTaskObserver(UniTaskCompletionSource promise, SingleAssignmentDisposable disposable, CancellationToken cancellationToken) + { + this.promise = promise; + this.disposable = disposable; + this.cancellationToken = cancellationToken; + + if (this.cancellationToken.CanBeCanceled) + { + this.registration = this.cancellationToken.RegisterWithoutCaptureExecutionContext(callback, this); + } + } + + static void OnCanceled(object state) + { + var self = (ToUniTaskObserver)state; + self.disposable.Dispose(); + self.promise.TrySetCanceled(); + } + + public void OnNext(T value) + { + hasValue = true; + latestValue = value; + } + + public void OnError(Exception error) + { + try + { + promise.TrySetException(error); + } + finally + { + registration.Dispose(); + disposable.Dispose(); + } + } + + public void OnCompleted() + { + try + { + if (hasValue) + { + promise.TrySetResult(latestValue); + } + else + { + promise.TrySetException(new InvalidOperationException("Sequence has no elements")); + } + } + finally + { + registration.Dispose(); + disposable.Dispose(); + } + } + } + + class FirstValueToUniTaskObserver : IObserver + { + static readonly Action callback = OnCanceled; + + readonly UniTaskCompletionSource promise; + readonly SingleAssignmentDisposable disposable; + readonly CancellationToken cancellationToken; + readonly CancellationTokenRegistration registration; + + bool hasValue; + + public FirstValueToUniTaskObserver(UniTaskCompletionSource promise, SingleAssignmentDisposable disposable, CancellationToken cancellationToken) + { + this.promise = promise; + this.disposable = disposable; + this.cancellationToken = cancellationToken; + + if (this.cancellationToken.CanBeCanceled) + { + this.registration = this.cancellationToken.RegisterWithoutCaptureExecutionContext(callback, this); + } + } + + static void OnCanceled(object state) + { + var self = (FirstValueToUniTaskObserver)state; + self.disposable.Dispose(); + self.promise.TrySetCanceled(); + } + + public void OnNext(T value) + { + hasValue = true; + try + { + promise.TrySetResult(value); + } + finally + { + registration.Dispose(); + disposable.Dispose(); + } + } + + public void OnError(Exception error) + { + try + { + promise.TrySetException(error); + } + finally + { + registration.Dispose(); + disposable.Dispose(); + } + } + + public void OnCompleted() + { + try + { + if (!hasValue) + { + promise.TrySetException(new InvalidOperationException("Sequence has no elements")); + } + } + finally + { + registration.Dispose(); + disposable.Dispose(); + } + } + } + + class ReturnObservable : IObservable + { + readonly T value; + + public ReturnObservable(T value) + { + this.value = value; + } + + public IDisposable Subscribe(IObserver observer) + { + observer.OnNext(value); + observer.OnCompleted(); + return EmptyDisposable.Instance; + } + } + + class ThrowObservable : IObservable + { + readonly Exception value; + + public ThrowObservable(Exception value) + { + this.value = value; + } + + public IDisposable Subscribe(IObserver observer) + { + observer.OnError(value); + return EmptyDisposable.Instance; + } + } + } +} + +namespace Cysharp.Threading.Tasks.Internal +{ + // Bridges for Rx. + + internal class EmptyDisposable : IDisposable + { + public static EmptyDisposable Instance = new EmptyDisposable(); + + EmptyDisposable() + { + + } + + public void Dispose() + { + } + } + + internal sealed class SingleAssignmentDisposable : IDisposable + { + readonly object gate = new object(); + IDisposable current; + bool disposed; + + public bool IsDisposed { get { lock (gate) { return disposed; } } } + + public IDisposable Disposable + { + get + { + return current; + } + set + { + var old = default(IDisposable); + bool alreadyDisposed; + lock (gate) + { + alreadyDisposed = disposed; + old = current; + if (!alreadyDisposed) + { + if (value == null) return; + current = value; + } + } + + if (alreadyDisposed && value != null) + { + value.Dispose(); + return; + } + + if (old != null) throw new InvalidOperationException("Disposable is already set"); + } + } + + + public void Dispose() + { + IDisposable old = null; + + lock (gate) + { + if (!disposed) + { + disposed = true; + old = current; + current = null; + } + } + + if (old != null) old.Dispose(); + } + } + + internal sealed class AsyncSubject : IObservable, IObserver + { + object observerLock = new object(); + + T lastValue; + bool hasValue; + bool isStopped; + bool isDisposed; + Exception lastError; + IObserver outObserver = EmptyObserver.Instance; + + public T Value + { + get + { + ThrowIfDisposed(); + if (!isStopped) throw new InvalidOperationException("AsyncSubject is not completed yet"); + if (lastError != null) ExceptionDispatchInfo.Capture(lastError).Throw(); + return lastValue; + } + } + + public bool HasObservers + { + get + { + return !(outObserver is EmptyObserver) && !isStopped && !isDisposed; + } + } + + public bool IsCompleted { get { return isStopped; } } + + public void OnCompleted() + { + IObserver old; + T v; + bool hv; + lock (observerLock) + { + ThrowIfDisposed(); + if (isStopped) return; + + old = outObserver; + outObserver = EmptyObserver.Instance; + isStopped = true; + v = lastValue; + hv = hasValue; + } + + if (hv) + { + old.OnNext(v); + old.OnCompleted(); + } + else + { + old.OnCompleted(); + } + } + + public void OnError(Exception error) + { + if (error == null) throw new ArgumentNullException("error"); + + IObserver old; + lock (observerLock) + { + ThrowIfDisposed(); + if (isStopped) return; + + old = outObserver; + outObserver = EmptyObserver.Instance; + isStopped = true; + lastError = error; + } + + old.OnError(error); + } + + public void OnNext(T value) + { + lock (observerLock) + { + ThrowIfDisposed(); + if (isStopped) return; + + this.hasValue = true; + this.lastValue = value; + } + } + + public IDisposable Subscribe(IObserver observer) + { + if (observer == null) throw new ArgumentNullException("observer"); + + var ex = default(Exception); + var v = default(T); + var hv = false; + + lock (observerLock) + { + ThrowIfDisposed(); + if (!isStopped) + { + var listObserver = outObserver as ListObserver; + if (listObserver != null) + { + outObserver = listObserver.Add(observer); + } + else + { + var current = outObserver; + if (current is EmptyObserver) + { + outObserver = observer; + } + else + { + outObserver = new ListObserver(new ImmutableList>(new[] { current, observer })); + } + } + + return new Subscription(this, observer); + } + + ex = lastError; + v = lastValue; + hv = hasValue; + } + + if (ex != null) + { + observer.OnError(ex); + } + else if (hv) + { + observer.OnNext(v); + observer.OnCompleted(); + } + else + { + observer.OnCompleted(); + } + + return EmptyDisposable.Instance; + } + + public void Dispose() + { + lock (observerLock) + { + isDisposed = true; + outObserver = DisposedObserver.Instance; + lastError = null; + lastValue = default(T); + } + } + + void ThrowIfDisposed() + { + if (isDisposed) throw new ObjectDisposedException(""); + } + + class Subscription : IDisposable + { + readonly object gate = new object(); + AsyncSubject parent; + IObserver unsubscribeTarget; + + public Subscription(AsyncSubject parent, IObserver unsubscribeTarget) + { + this.parent = parent; + this.unsubscribeTarget = unsubscribeTarget; + } + + public void Dispose() + { + lock (gate) + { + if (parent != null) + { + lock (parent.observerLock) + { + var listObserver = parent.outObserver as ListObserver; + if (listObserver != null) + { + parent.outObserver = listObserver.Remove(unsubscribeTarget); + } + else + { + parent.outObserver = EmptyObserver.Instance; + } + + unsubscribeTarget = null; + parent = null; + } + } + } + } + } + } + + internal class ListObserver : IObserver + { + private readonly ImmutableList> _observers; + + public ListObserver(ImmutableList> observers) + { + _observers = observers; + } + + public void OnCompleted() + { + var targetObservers = _observers.Data; + for (int i = 0; i < targetObservers.Length; i++) + { + targetObservers[i].OnCompleted(); + } + } + + public void OnError(Exception error) + { + var targetObservers = _observers.Data; + for (int i = 0; i < targetObservers.Length; i++) + { + targetObservers[i].OnError(error); + } + } + + public void OnNext(T value) + { + var targetObservers = _observers.Data; + for (int i = 0; i < targetObservers.Length; i++) + { + targetObservers[i].OnNext(value); + } + } + + internal IObserver Add(IObserver observer) + { + return new ListObserver(_observers.Add(observer)); + } + + internal IObserver Remove(IObserver observer) + { + var i = Array.IndexOf(_observers.Data, observer); + if (i < 0) + return this; + + if (_observers.Data.Length == 2) + { + return _observers.Data[1 - i]; + } + else + { + return new ListObserver(_observers.Remove(observer)); + } + } + } + + internal class EmptyObserver : IObserver + { + public static readonly EmptyObserver Instance = new EmptyObserver(); + + EmptyObserver() + { + + } + + public void OnCompleted() + { + } + + public void OnError(Exception error) + { + } + + public void OnNext(T value) + { + } + } + + internal class ThrowObserver : IObserver + { + public static readonly ThrowObserver Instance = new ThrowObserver(); + + ThrowObserver() + { + + } + + public void OnCompleted() + { + } + + public void OnError(Exception error) + { + ExceptionDispatchInfo.Capture(error).Throw(); + } + + public void OnNext(T value) + { + } + } + + internal class DisposedObserver : IObserver + { + public static readonly DisposedObserver Instance = new DisposedObserver(); + + DisposedObserver() + { + + } + + public void OnCompleted() + { + throw new ObjectDisposedException(""); + } + + public void OnError(Exception error) + { + throw new ObjectDisposedException(""); + } + + public void OnNext(T value) + { + throw new ObjectDisposedException(""); + } + } + + internal class ImmutableList + { + public static readonly ImmutableList Empty = new ImmutableList(); + + T[] data; + + public T[] Data + { + get { return data; } + } + + ImmutableList() + { + data = new T[0]; + } + + public ImmutableList(T[] data) + { + this.data = data; + } + + public ImmutableList Add(T value) + { + var newData = new T[data.Length + 1]; + Array.Copy(data, newData, data.Length); + newData[data.Length] = value; + return new ImmutableList(newData); + } + + public ImmutableList Remove(T value) + { + var i = IndexOf(value); + if (i < 0) return this; + + var length = data.Length; + if (length == 1) return Empty; + + var newData = new T[length - 1]; + + Array.Copy(data, 0, newData, 0, i); + Array.Copy(data, i + 1, newData, i, length - i - 1); + + return new ImmutableList(newData); + } + + public int IndexOf(T value) + { + for (var i = 0; i < data.Length; ++i) + { + // ImmutableList only use for IObserver(no worry for boxed) + if (object.Equals(data[i], value)) return i; + } + return -1; + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskObservableExtensions.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskObservableExtensions.cs.meta new file mode 100644 index 0000000..527a49f --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskObservableExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eaea262a5ad393d419c15b3b2901d664 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskScheduler.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskScheduler.cs new file mode 100644 index 0000000..2f91f2a --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskScheduler.cs @@ -0,0 +1,103 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks +{ + // UniTask has no scheduler like TaskScheduler. + // Only handle unobserved exception. + + public static class UniTaskScheduler + { + public static event Action UnobservedTaskException; + + /// + /// Propagate OperationCanceledException to UnobservedTaskException when true. Default is false. + /// + public static bool PropagateOperationCanceledException = false; + +#if UNITY_2018_3_OR_NEWER + + /// + /// Write log type when catch unobserved exception and not registered UnobservedTaskException. Default is Exception. + /// + public static UnityEngine.LogType UnobservedExceptionWriteLogType = UnityEngine.LogType.Exception; + + /// + /// Dispatch exception event to Unity MainThread. Default is true. + /// + public static bool DispatchUnityMainThread = true; + + // cache delegate. + static readonly SendOrPostCallback handleExceptionInvoke = InvokeUnobservedTaskException; + + static void InvokeUnobservedTaskException(object state) + { + UnobservedTaskException((Exception)state); + } +#endif + + internal static void PublishUnobservedTaskException(Exception ex) + { + if (ex != null) + { + if (!PropagateOperationCanceledException && ex is OperationCanceledException) + { + return; + } + + if (UnobservedTaskException != null) + { +#if UNITY_2018_3_OR_NEWER + if (!DispatchUnityMainThread || Thread.CurrentThread.ManagedThreadId == PlayerLoopHelper.MainThreadId) + { + // allows inlining call. + UnobservedTaskException.Invoke(ex); + } + else + { + // Post to MainThread. + PlayerLoopHelper.UnitySynchronizationContext.Post(handleExceptionInvoke, ex); + } +#else + UnobservedTaskException.Invoke(ex); +#endif + } + else + { +#if UNITY_2018_3_OR_NEWER + string msg = null; + if (UnobservedExceptionWriteLogType != UnityEngine.LogType.Exception) + { + msg = "UnobservedTaskException: " + ex.ToString(); + } + switch (UnobservedExceptionWriteLogType) + { + case UnityEngine.LogType.Error: + UnityEngine.Debug.LogError(msg); + break; + case UnityEngine.LogType.Assert: + UnityEngine.Debug.LogAssertion(msg); + break; + case UnityEngine.LogType.Warning: + UnityEngine.Debug.LogWarning(msg); + break; + case UnityEngine.LogType.Log: + UnityEngine.Debug.Log(msg); + break; + case UnityEngine.LogType.Exception: + UnityEngine.Debug.LogException(ex); + break; + default: + break; + } +#else + Console.WriteLine("UnobservedTaskException: " + ex.ToString()); +#endif + } + } + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskScheduler.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskScheduler.cs.meta new file mode 100644 index 0000000..5e29191 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskScheduler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d6cad69921702d5488d96b5ef30df1b0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskSynchronizationContext.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskSynchronizationContext.cs new file mode 100644 index 0000000..450e019 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskSynchronizationContext.cs @@ -0,0 +1,158 @@ +using System; +using System.Runtime.InteropServices; +using System.Threading; + +namespace Cysharp.Threading.Tasks +{ + public class UniTaskSynchronizationContext : SynchronizationContext + { + const int MaxArrayLength = 0X7FEFFFFF; + const int InitialSize = 16; + + static SpinLock gate = new SpinLock(false); + static bool dequing = false; + + static int actionListCount = 0; + static Callback[] actionList = new Callback[InitialSize]; + + static int waitingListCount = 0; + static Callback[] waitingList = new Callback[InitialSize]; + + static int opCount; + + public override void Send(SendOrPostCallback d, object state) + { + d(state); + } + + public override void Post(SendOrPostCallback d, object state) + { + bool lockTaken = false; + try + { + gate.Enter(ref lockTaken); + + if (dequing) + { + // Ensure Capacity + if (waitingList.Length == waitingListCount) + { + var newLength = waitingListCount * 2; + if ((uint)newLength > MaxArrayLength) newLength = MaxArrayLength; + + var newArray = new Callback[newLength]; + Array.Copy(waitingList, newArray, waitingListCount); + waitingList = newArray; + } + waitingList[waitingListCount] = new Callback(d, state); + waitingListCount++; + } + else + { + // Ensure Capacity + if (actionList.Length == actionListCount) + { + var newLength = actionListCount * 2; + if ((uint)newLength > MaxArrayLength) newLength = MaxArrayLength; + + var newArray = new Callback[newLength]; + Array.Copy(actionList, newArray, actionListCount); + actionList = newArray; + } + actionList[actionListCount] = new Callback(d, state); + actionListCount++; + } + } + finally + { + if (lockTaken) gate.Exit(false); + } + } + + public override void OperationStarted() + { + Interlocked.Increment(ref opCount); + } + + public override void OperationCompleted() + { + Interlocked.Decrement(ref opCount); + } + + public override SynchronizationContext CreateCopy() + { + return this; + } + + // delegate entrypoint. + internal static void Run() + { + { + bool lockTaken = false; + try + { + gate.Enter(ref lockTaken); + if (actionListCount == 0) return; + dequing = true; + } + finally + { + if (lockTaken) gate.Exit(false); + } + } + + for (int i = 0; i < actionListCount; i++) + { + var action = actionList[i]; + actionList[i] = default; + action.Invoke(); + } + + { + bool lockTaken = false; + try + { + gate.Enter(ref lockTaken); + dequing = false; + + var swapTempActionList = actionList; + + actionListCount = waitingListCount; + actionList = waitingList; + + waitingListCount = 0; + waitingList = swapTempActionList; + } + finally + { + if (lockTaken) gate.Exit(false); + } + } + } + + [StructLayout(LayoutKind.Auto)] + readonly struct Callback + { + readonly SendOrPostCallback callback; + readonly object state; + + public Callback(SendOrPostCallback callback, object state) + { + this.callback = callback; + this.state = state; + } + + public void Invoke() + { + try + { + callback(state); + } + catch (Exception ex) + { + UnityEngine.Debug.LogException(ex); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskSynchronizationContext.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskSynchronizationContext.cs.meta new file mode 100644 index 0000000..9828c89 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskSynchronizationContext.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: abf3aae9813db2849bce518f8596e920 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskVoid.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskVoid.cs new file mode 100644 index 0000000..c7e9ed9 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskVoid.cs @@ -0,0 +1,19 @@ +#pragma warning disable CS1591 +#pragma warning disable CS0436 + +using System; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using Cysharp.Threading.Tasks.CompilerServices; + +namespace Cysharp.Threading.Tasks +{ + [AsyncMethodBuilder(typeof(AsyncUniTaskVoidMethodBuilder))] + public readonly struct UniTaskVoid + { + public void Forget() + { + } + } +} + diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskVoid.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskVoid.cs.meta new file mode 100644 index 0000000..01f7156 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UniTaskVoid.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e9f28cd922179634d863011548f89ae7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs new file mode 100644 index 0000000..90889f3 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs @@ -0,0 +1,198 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +#if UNITY_2018_4 || UNITY_2019_4_OR_NEWER +#if UNITASK_ASSETBUNDLE_SUPPORT + +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Runtime.CompilerServices; +using System.Threading; +using UnityEngine; + +namespace Cysharp.Threading.Tasks +{ + public static partial class UnityAsyncExtensions + { + public static AssetBundleRequestAllAssetsAwaiter AwaitForAllAssets(this AssetBundleRequest asyncOperation) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + return new AssetBundleRequestAllAssetsAwaiter(asyncOperation); + } + + public static UniTask AwaitForAllAssets(this AssetBundleRequest asyncOperation, CancellationToken cancellationToken) + { + return AwaitForAllAssets(asyncOperation, cancellationToken: cancellationToken); + } + + public static UniTask AwaitForAllAssets(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); + if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.allAssets); + return new UniTask(AssetBundleRequestAllAssetsConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + } + + public struct AssetBundleRequestAllAssetsAwaiter : ICriticalNotifyCompletion + { + AssetBundleRequest asyncOperation; + Action continuationAction; + + public AssetBundleRequestAllAssetsAwaiter(AssetBundleRequest asyncOperation) + { + this.asyncOperation = asyncOperation; + this.continuationAction = null; + } + + public AssetBundleRequestAllAssetsAwaiter GetAwaiter() + { + return this; + } + + public bool IsCompleted => asyncOperation.isDone; + + public UnityEngine.Object[] GetResult() + { + if (continuationAction != null) + { + asyncOperation.completed -= continuationAction; + continuationAction = null; + var result = asyncOperation.allAssets; + asyncOperation = null; + return result; + } + else + { + var result = asyncOperation.allAssets; + asyncOperation = null; + return result; + } + } + + public void OnCompleted(Action continuation) + { + UnsafeOnCompleted(continuation); + } + + public void UnsafeOnCompleted(Action continuation) + { + Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction); + continuationAction = PooledDelegate.Create(continuation); + asyncOperation.completed += continuationAction; + } + } + + sealed class AssetBundleRequestAllAssetsConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + AssetBundleRequestAllAssetsConfiguredSource nextNode; + public ref AssetBundleRequestAllAssetsConfiguredSource NextNode => ref nextNode; + + static AssetBundleRequestAllAssetsConfiguredSource() + { + TaskPool.RegisterSizeGetter(typeof(AssetBundleRequestAllAssetsConfiguredSource), () => pool.Size); + } + + AssetBundleRequest asyncOperation; + IProgress progress; + CancellationToken cancellationToken; + + UniTaskCompletionSourceCore core; + + AssetBundleRequestAllAssetsConfiguredSource() + { + + } + + public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new AssetBundleRequestAllAssetsConfiguredSource(); + } + + result.asyncOperation = asyncOperation; + result.progress = progress; + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public UnityEngine.Object[] GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + return false; + } + + if (progress != null) + { + progress.Report(asyncOperation.progress); + } + + if (asyncOperation.isDone) + { + core.TrySetResult(asyncOperation.allAssets); + return false; + } + + return true; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + asyncOperation = default; + progress = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + } +} + +#endif +#endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs.meta new file mode 100644 index 0000000..79be923 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e9147caba40da434da95b39709c13784 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.AsyncGPUReadback.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.AsyncGPUReadback.cs new file mode 100644 index 0000000..5805dbb --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.AsyncGPUReadback.cs @@ -0,0 +1,140 @@ + #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Threading; +using UnityEngine.Rendering; + +namespace Cysharp.Threading.Tasks +{ + public static partial class UnityAsyncExtensions + { + #region AsyncGPUReadbackRequest + + public static UniTask.Awaiter GetAwaiter(this AsyncGPUReadbackRequest asyncOperation) + { + return ToUniTask(asyncOperation).GetAwaiter(); + } + + public static UniTask WithCancellation(this AsyncGPUReadbackRequest asyncOperation, CancellationToken cancellationToken) + { + return ToUniTask(asyncOperation, cancellationToken: cancellationToken); + } + + public static UniTask ToUniTask(this AsyncGPUReadbackRequest asyncOperation, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + if (asyncOperation.done) return UniTask.FromResult(asyncOperation); + return new UniTask(AsyncGPUReadbackRequestAwaiterConfiguredSource.Create(asyncOperation, timing, cancellationToken, out var token), token); + } + + sealed class AsyncGPUReadbackRequestAwaiterConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + AsyncGPUReadbackRequestAwaiterConfiguredSource nextNode; + public ref AsyncGPUReadbackRequestAwaiterConfiguredSource NextNode => ref nextNode; + + static AsyncGPUReadbackRequestAwaiterConfiguredSource() + { + TaskPool.RegisterSizeGetter(typeof(AsyncGPUReadbackRequestAwaiterConfiguredSource), () => pool.Size); + } + + AsyncGPUReadbackRequest asyncOperation; + CancellationToken cancellationToken; + + UniTaskCompletionSourceCore core; + + AsyncGPUReadbackRequestAwaiterConfiguredSource() + { + + } + + public static IUniTaskSource Create(AsyncGPUReadbackRequest asyncOperation, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new AsyncGPUReadbackRequestAwaiterConfiguredSource(); + } + + result.asyncOperation = asyncOperation; + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public AsyncGPUReadbackRequest GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + return false; + } + + if (asyncOperation.hasError) + { + core.TrySetException(new Exception("AsyncGPUReadbackRequest.hasError = true")); + return false; + } + + if (asyncOperation.done) + { + core.TrySetResult(asyncOperation); + return false; + } + + return true; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + asyncOperation = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + + #endregion + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.AsyncGPUReadback.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.AsyncGPUReadback.cs.meta new file mode 100644 index 0000000..510c49e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.AsyncGPUReadback.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 98f5fedb44749ab4688674d79126b46a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.Jobs.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.Jobs.cs new file mode 100644 index 0000000..db0a892 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.Jobs.cs @@ -0,0 +1,102 @@ +#if ENABLE_MANAGED_JOBS +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Threading; +using Unity.Jobs; +using UnityEngine; + +namespace Cysharp.Threading.Tasks +{ + public static partial class UnityAsyncExtensions + { + public static async UniTask WaitAsync(this JobHandle jobHandle, PlayerLoopTiming waitTiming, CancellationToken cancellationToken = default) + { + await UniTask.Yield(waitTiming); + jobHandle.Complete(); + cancellationToken.ThrowIfCancellationRequested(); // call cancel after Complete. + } + + public static UniTask.Awaiter GetAwaiter(this JobHandle jobHandle) + { + var handler = JobHandlePromise.Create(jobHandle, out var token); + { + PlayerLoopHelper.AddAction(PlayerLoopTiming.EarlyUpdate, handler); + PlayerLoopHelper.AddAction(PlayerLoopTiming.PreUpdate, handler); + PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, handler); + PlayerLoopHelper.AddAction(PlayerLoopTiming.PreLateUpdate, handler); + PlayerLoopHelper.AddAction(PlayerLoopTiming.PostLateUpdate, handler); + } + + return new UniTask(handler, token).GetAwaiter(); + } + + // can not pass CancellationToken because can't handle JobHandle's Complete and NativeArray.Dispose. + + public static UniTask ToUniTask(this JobHandle jobHandle, PlayerLoopTiming waitTiming) + { + var handler = JobHandlePromise.Create(jobHandle, out var token); + { + PlayerLoopHelper.AddAction(waitTiming, handler); + } + + return new UniTask(handler, token); + } + + sealed class JobHandlePromise : IUniTaskSource, IPlayerLoopItem + { + JobHandle jobHandle; + + UniTaskCompletionSourceCore core; + + // Cancellation is not supported. + public static JobHandlePromise Create(JobHandle jobHandle, out short token) + { + // not use pool. + var result = new JobHandlePromise(); + + result.jobHandle = jobHandle; + + TaskTracker.TrackActiveTask(result, 3); + + token = result.core.Version; + return result; + } + + public void GetResult(short token) + { + TaskTracker.RemoveTracking(this); + core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (jobHandle.IsCompleted | PlayerLoopHelper.IsEditorApplicationQuitting) + { + jobHandle.Complete(); + core.TrySetResult(AsyncUnit.Default); + return false; + } + + return true; + } + } + } +} + +#endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.Jobs.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.Jobs.cs.meta new file mode 100644 index 0000000..c07df0b --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.Jobs.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 30979a768fbd4b94f8694eee8a305c99 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.MonoBehaviour.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.MonoBehaviour.cs new file mode 100644 index 0000000..fdfe55c --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.MonoBehaviour.cs @@ -0,0 +1,14 @@ +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks +{ + public static partial class UnityAsyncExtensions + { + public static UniTask StartAsyncCoroutine(this UnityEngine.MonoBehaviour monoBehaviour, Func asyncCoroutine) + { + var token = monoBehaviour.GetCancellationTokenOnDestroy(); + return asyncCoroutine(token); + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.MonoBehaviour.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.MonoBehaviour.cs.meta new file mode 100644 index 0000000..6e45863 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.MonoBehaviour.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2edd588bb09eb0a4695d039d6a1f02b2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.cs new file mode 100644 index 0000000..8f8e3a1 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.cs @@ -0,0 +1,928 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Runtime.CompilerServices; +using System.Threading; +using UnityEngine; +using Cysharp.Threading.Tasks.Internal; +#if ENABLE_UNITYWEBREQUEST && (!UNITY_2019_1_OR_NEWER || UNITASK_WEBREQUEST_SUPPORT) +using UnityEngine.Networking; +#endif + +namespace Cysharp.Threading.Tasks +{ + public static partial class UnityAsyncExtensions + { + #region AsyncOperation + + public static AsyncOperationAwaiter GetAwaiter(this AsyncOperation asyncOperation) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + return new AsyncOperationAwaiter(asyncOperation); + } + + public static UniTask WithCancellation(this AsyncOperation asyncOperation, CancellationToken cancellationToken) + { + return ToUniTask(asyncOperation, cancellationToken: cancellationToken); + } + + public static UniTask ToUniTask(this AsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); + if (asyncOperation.isDone) return UniTask.CompletedTask; + return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + } + + public struct AsyncOperationAwaiter : ICriticalNotifyCompletion + { + AsyncOperation asyncOperation; + Action continuationAction; + + public AsyncOperationAwaiter(AsyncOperation asyncOperation) + { + this.asyncOperation = asyncOperation; + this.continuationAction = null; + } + + public bool IsCompleted => asyncOperation.isDone; + + public void GetResult() + { + if (continuationAction != null) + { + asyncOperation.completed -= continuationAction; + continuationAction = null; + asyncOperation = null; + } + else + { + asyncOperation = null; + } + } + + public void OnCompleted(Action continuation) + { + UnsafeOnCompleted(continuation); + } + + public void UnsafeOnCompleted(Action continuation) + { + Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction); + continuationAction = PooledDelegate.Create(continuation); + asyncOperation.completed += continuationAction; + } + } + + sealed class AsyncOperationConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + AsyncOperationConfiguredSource nextNode; + public ref AsyncOperationConfiguredSource NextNode => ref nextNode; + + static AsyncOperationConfiguredSource() + { + TaskPool.RegisterSizeGetter(typeof(AsyncOperationConfiguredSource), () => pool.Size); + } + + AsyncOperation asyncOperation; + IProgress progress; + CancellationToken cancellationToken; + + UniTaskCompletionSourceCore core; + + AsyncOperationConfiguredSource() + { + + } + + public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new AsyncOperationConfiguredSource(); + } + + result.asyncOperation = asyncOperation; + result.progress = progress; + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public void GetResult(short token) + { + try + { + core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + return false; + } + + if (progress != null) + { + progress.Report(asyncOperation.progress); + } + + if (asyncOperation.isDone) + { + core.TrySetResult(AsyncUnit.Default); + return false; + } + + return true; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + asyncOperation = default; + progress = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + + #endregion + + #region ResourceRequest + + public static ResourceRequestAwaiter GetAwaiter(this ResourceRequest asyncOperation) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + return new ResourceRequestAwaiter(asyncOperation); + } + + public static UniTask WithCancellation(this ResourceRequest asyncOperation, CancellationToken cancellationToken) + { + return ToUniTask(asyncOperation, cancellationToken: cancellationToken); + } + + public static UniTask ToUniTask(this ResourceRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); + if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); + return new UniTask(ResourceRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + } + + public struct ResourceRequestAwaiter : ICriticalNotifyCompletion + { + ResourceRequest asyncOperation; + Action continuationAction; + + public ResourceRequestAwaiter(ResourceRequest asyncOperation) + { + this.asyncOperation = asyncOperation; + this.continuationAction = null; + } + + public bool IsCompleted => asyncOperation.isDone; + + public UnityEngine.Object GetResult() + { + if (continuationAction != null) + { + asyncOperation.completed -= continuationAction; + continuationAction = null; + var result = asyncOperation.asset; + asyncOperation = null; + return result; + } + else + { + var result = asyncOperation.asset; + asyncOperation = null; + return result; + } + } + + public void OnCompleted(Action continuation) + { + UnsafeOnCompleted(continuation); + } + + public void UnsafeOnCompleted(Action continuation) + { + Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction); + continuationAction = PooledDelegate.Create(continuation); + asyncOperation.completed += continuationAction; + } + } + + sealed class ResourceRequestConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + ResourceRequestConfiguredSource nextNode; + public ref ResourceRequestConfiguredSource NextNode => ref nextNode; + + static ResourceRequestConfiguredSource() + { + TaskPool.RegisterSizeGetter(typeof(ResourceRequestConfiguredSource), () => pool.Size); + } + + ResourceRequest asyncOperation; + IProgress progress; + CancellationToken cancellationToken; + + UniTaskCompletionSourceCore core; + + ResourceRequestConfiguredSource() + { + + } + + public static IUniTaskSource Create(ResourceRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new ResourceRequestConfiguredSource(); + } + + result.asyncOperation = asyncOperation; + result.progress = progress; + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public UnityEngine.Object GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + return false; + } + + if (progress != null) + { + progress.Report(asyncOperation.progress); + } + + if (asyncOperation.isDone) + { + core.TrySetResult(asyncOperation.asset); + return false; + } + + return true; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + asyncOperation = default; + progress = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + + #endregion + +#if UNITASK_ASSETBUNDLE_SUPPORT + #region AssetBundleRequest + + public static AssetBundleRequestAwaiter GetAwaiter(this AssetBundleRequest asyncOperation) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + return new AssetBundleRequestAwaiter(asyncOperation); + } + + public static UniTask WithCancellation(this AssetBundleRequest asyncOperation, CancellationToken cancellationToken) + { + return ToUniTask(asyncOperation, cancellationToken: cancellationToken); + } + + public static UniTask ToUniTask(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); + if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); + return new UniTask(AssetBundleRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + } + + public struct AssetBundleRequestAwaiter : ICriticalNotifyCompletion + { + AssetBundleRequest asyncOperation; + Action continuationAction; + + public AssetBundleRequestAwaiter(AssetBundleRequest asyncOperation) + { + this.asyncOperation = asyncOperation; + this.continuationAction = null; + } + + public bool IsCompleted => asyncOperation.isDone; + + public UnityEngine.Object GetResult() + { + if (continuationAction != null) + { + asyncOperation.completed -= continuationAction; + continuationAction = null; + var result = asyncOperation.asset; + asyncOperation = null; + return result; + } + else + { + var result = asyncOperation.asset; + asyncOperation = null; + return result; + } + } + + public void OnCompleted(Action continuation) + { + UnsafeOnCompleted(continuation); + } + + public void UnsafeOnCompleted(Action continuation) + { + Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction); + continuationAction = PooledDelegate.Create(continuation); + asyncOperation.completed += continuationAction; + } + } + + sealed class AssetBundleRequestConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + AssetBundleRequestConfiguredSource nextNode; + public ref AssetBundleRequestConfiguredSource NextNode => ref nextNode; + + static AssetBundleRequestConfiguredSource() + { + TaskPool.RegisterSizeGetter(typeof(AssetBundleRequestConfiguredSource), () => pool.Size); + } + + AssetBundleRequest asyncOperation; + IProgress progress; + CancellationToken cancellationToken; + + UniTaskCompletionSourceCore core; + + AssetBundleRequestConfiguredSource() + { + + } + + public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new AssetBundleRequestConfiguredSource(); + } + + result.asyncOperation = asyncOperation; + result.progress = progress; + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public UnityEngine.Object GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + return false; + } + + if (progress != null) + { + progress.Report(asyncOperation.progress); + } + + if (asyncOperation.isDone) + { + core.TrySetResult(asyncOperation.asset); + return false; + } + + return true; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + asyncOperation = default; + progress = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + + #endregion +#endif + +#if UNITASK_ASSETBUNDLE_SUPPORT + #region AssetBundleCreateRequest + + public static AssetBundleCreateRequestAwaiter GetAwaiter(this AssetBundleCreateRequest asyncOperation) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + return new AssetBundleCreateRequestAwaiter(asyncOperation); + } + + public static UniTask WithCancellation(this AssetBundleCreateRequest asyncOperation, CancellationToken cancellationToken) + { + return ToUniTask(asyncOperation, cancellationToken: cancellationToken); + } + + public static UniTask ToUniTask(this AssetBundleCreateRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); + if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.assetBundle); + return new UniTask(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + } + + public struct AssetBundleCreateRequestAwaiter : ICriticalNotifyCompletion + { + AssetBundleCreateRequest asyncOperation; + Action continuationAction; + + public AssetBundleCreateRequestAwaiter(AssetBundleCreateRequest asyncOperation) + { + this.asyncOperation = asyncOperation; + this.continuationAction = null; + } + + public bool IsCompleted => asyncOperation.isDone; + + public AssetBundle GetResult() + { + if (continuationAction != null) + { + asyncOperation.completed -= continuationAction; + continuationAction = null; + var result = asyncOperation.assetBundle; + asyncOperation = null; + return result; + } + else + { + var result = asyncOperation.assetBundle; + asyncOperation = null; + return result; + } + } + + public void OnCompleted(Action continuation) + { + UnsafeOnCompleted(continuation); + } + + public void UnsafeOnCompleted(Action continuation) + { + Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction); + continuationAction = PooledDelegate.Create(continuation); + asyncOperation.completed += continuationAction; + } + } + + sealed class AssetBundleCreateRequestConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + AssetBundleCreateRequestConfiguredSource nextNode; + public ref AssetBundleCreateRequestConfiguredSource NextNode => ref nextNode; + + static AssetBundleCreateRequestConfiguredSource() + { + TaskPool.RegisterSizeGetter(typeof(AssetBundleCreateRequestConfiguredSource), () => pool.Size); + } + + AssetBundleCreateRequest asyncOperation; + IProgress progress; + CancellationToken cancellationToken; + + UniTaskCompletionSourceCore core; + + AssetBundleCreateRequestConfiguredSource() + { + + } + + public static IUniTaskSource Create(AssetBundleCreateRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new AssetBundleCreateRequestConfiguredSource(); + } + + result.asyncOperation = asyncOperation; + result.progress = progress; + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public AssetBundle GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + return false; + } + + if (progress != null) + { + progress.Report(asyncOperation.progress); + } + + if (asyncOperation.isDone) + { + core.TrySetResult(asyncOperation.assetBundle); + return false; + } + + return true; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + asyncOperation = default; + progress = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + + #endregion +#endif + +#if ENABLE_UNITYWEBREQUEST && (!UNITY_2019_1_OR_NEWER || UNITASK_WEBREQUEST_SUPPORT) + #region UnityWebRequestAsyncOperation + + public static UnityWebRequestAsyncOperationAwaiter GetAwaiter(this UnityWebRequestAsyncOperation asyncOperation) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + return new UnityWebRequestAsyncOperationAwaiter(asyncOperation); + } + + public static UniTask WithCancellation(this UnityWebRequestAsyncOperation asyncOperation, CancellationToken cancellationToken) + { + return ToUniTask(asyncOperation, cancellationToken: cancellationToken); + } + + public static UniTask ToUniTask(this UnityWebRequestAsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); + if (asyncOperation.isDone) + { + if (asyncOperation.webRequest.IsError()) + { + return UniTask.FromException(new UnityWebRequestException(asyncOperation.webRequest)); + } + return UniTask.FromResult(asyncOperation.webRequest); + } + return new UniTask(UnityWebRequestAsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + } + + public struct UnityWebRequestAsyncOperationAwaiter : ICriticalNotifyCompletion + { + UnityWebRequestAsyncOperation asyncOperation; + Action continuationAction; + + public UnityWebRequestAsyncOperationAwaiter(UnityWebRequestAsyncOperation asyncOperation) + { + this.asyncOperation = asyncOperation; + this.continuationAction = null; + } + + public bool IsCompleted => asyncOperation.isDone; + + public UnityWebRequest GetResult() + { + if (continuationAction != null) + { + asyncOperation.completed -= continuationAction; + continuationAction = null; + var result = asyncOperation.webRequest; + asyncOperation = null; + if (result.IsError()) + { + throw new UnityWebRequestException(result); + } + return result; + } + else + { + var result = asyncOperation.webRequest; + asyncOperation = null; + if (result.IsError()) + { + throw new UnityWebRequestException(result); + } + return result; + } + } + + public void OnCompleted(Action continuation) + { + UnsafeOnCompleted(continuation); + } + + public void UnsafeOnCompleted(Action continuation) + { + Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction); + continuationAction = PooledDelegate.Create(continuation); + asyncOperation.completed += continuationAction; + } + } + + sealed class UnityWebRequestAsyncOperationConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode + { + static TaskPool pool; + UnityWebRequestAsyncOperationConfiguredSource nextNode; + public ref UnityWebRequestAsyncOperationConfiguredSource NextNode => ref nextNode; + + static UnityWebRequestAsyncOperationConfiguredSource() + { + TaskPool.RegisterSizeGetter(typeof(UnityWebRequestAsyncOperationConfiguredSource), () => pool.Size); + } + + UnityWebRequestAsyncOperation asyncOperation; + IProgress progress; + CancellationToken cancellationToken; + + UniTaskCompletionSourceCore core; + + UnityWebRequestAsyncOperationConfiguredSource() + { + + } + + public static IUniTaskSource Create(UnityWebRequestAsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new UnityWebRequestAsyncOperationConfiguredSource(); + } + + result.asyncOperation = asyncOperation; + result.progress = progress; + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public UnityWebRequest GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { + asyncOperation.webRequest.Abort(); + core.TrySetCanceled(cancellationToken); + return false; + } + + if (progress != null) + { + progress.Report(asyncOperation.progress); + } + + if (asyncOperation.isDone) + { + if (asyncOperation.webRequest.IsError()) + { + core.TrySetException(new UnityWebRequestException(asyncOperation.webRequest)); + } + else + { + core.TrySetResult(asyncOperation.webRequest); + } + return false; + } + + return true; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + asyncOperation = default; + progress = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + + #endregion +#endif + + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.cs.meta new file mode 100644 index 0000000..6dfab81 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8cc7fd65dd1433e419be4764aeb51391 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.tt b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.tt new file mode 100644 index 0000000..65dac9e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.tt @@ -0,0 +1,278 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".cs" #> +<# + var types = new (string typeName, string returnType, string returnField)[] + { + ("AsyncOperation", "void", null), + ("ResourceRequest", "UnityEngine.Object", "asset"), + ("AssetBundleRequest", "UnityEngine.Object", "asset"), // allAssets? + ("AssetBundleCreateRequest", "AssetBundle", "assetBundle"), + ("UnityWebRequestAsyncOperation", "UnityWebRequest", "webRequest") // -> #if ENABLE_UNITYWEBREQUEST && (!UNITY_2019_1_OR_NEWER || UNITASK_WEBREQUEST_SUPPORT) + }; + + Func ToUniTaskReturnType = x => (x == "void") ? "UniTask" : $"UniTask<{x}>"; + Func ToIUniTaskSourceReturnType = x => (x == "void") ? "IUniTaskSource" : $"IUniTaskSource<{x}>"; + Func<(string typeName, string returnType, string returnField), bool> IsUnityWebRequest = x => x.returnType == "UnityWebRequest"; + Func<(string typeName, string returnType, string returnField), bool> IsAssetBundleModule = x => x.typeName == "AssetBundleRequest" || x.typeName == "AssetBundleCreateRequest"; + Func<(string typeName, string returnType, string returnField), bool> IsVoid = x => x.returnType == "void"; +#> +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +using System; +using System.Runtime.CompilerServices; +using System.Threading; +using UnityEngine; +using Cysharp.Threading.Tasks.Internal; +#if ENABLE_UNITYWEBREQUEST && (!UNITY_2019_1_OR_NEWER || UNITASK_WEBREQUEST_SUPPORT) +using UnityEngine.Networking; +#endif + +namespace Cysharp.Threading.Tasks +{ + public static partial class UnityAsyncExtensions + { +<# foreach(var t in types) { #> +<# if(IsUnityWebRequest(t)) { #> +#if ENABLE_UNITYWEBREQUEST && (!UNITY_2019_1_OR_NEWER || UNITASK_WEBREQUEST_SUPPORT) +<# } else if(IsAssetBundleModule(t)) { #> +#if UNITASK_ASSETBUNDLE_SUPPORT +<# } #> + #region <#= t.typeName #> + + public static <#= t.typeName #>Awaiter GetAwaiter(this <#= t.typeName #> asyncOperation) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + return new <#= t.typeName #>Awaiter(asyncOperation); + } + + public static <#= ToUniTaskReturnType(t.returnType) #> WithCancellation(this <#= t.typeName #> asyncOperation, CancellationToken cancellationToken) + { + return ToUniTask(asyncOperation, cancellationToken: cancellationToken); + } + + public static <#= ToUniTaskReturnType(t.returnType) #> ToUniTask(this <#= t.typeName #> asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<#= IsVoid(t) ? "" : "<" + t.returnType + ">" #>(cancellationToken); +<# if(IsUnityWebRequest(t)) { #> + if (asyncOperation.isDone) + { + if (asyncOperation.webRequest.IsError()) + { + return UniTask.FromException(new UnityWebRequestException(asyncOperation.webRequest)); + } + return UniTask.FromResult(asyncOperation.webRequest); + } +<# } else { #> + if (asyncOperation.isDone) return <#= IsVoid(t) ? "UniTask.CompletedTask" : $"UniTask.FromResult(asyncOperation.{t.returnField})" #>; +<# } #> + return new <#= ToUniTaskReturnType(t.returnType) #>(<#= t.typeName #>ConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + } + + public struct <#= t.typeName #>Awaiter : ICriticalNotifyCompletion + { + <#= t.typeName #> asyncOperation; + Action continuationAction; + + public <#= t.typeName #>Awaiter(<#= t.typeName #> asyncOperation) + { + this.asyncOperation = asyncOperation; + this.continuationAction = null; + } + + public bool IsCompleted => asyncOperation.isDone; + + public <#= t.returnType #> GetResult() + { + if (continuationAction != null) + { + asyncOperation.completed -= continuationAction; + continuationAction = null; +<# if (!IsVoid(t)) { #> + var result = <#= $"asyncOperation.{t.returnField}" #>; + asyncOperation = null; +<# if(IsUnityWebRequest(t)) { #> + if (result.IsError()) + { + throw new UnityWebRequestException(result); + } +<# } #> + return result; +<# } else { #> + asyncOperation = null; +<# } #> + } + else + { +<# if (!IsVoid(t)) { #> + var result = <#= $"asyncOperation.{t.returnField}" #>; + asyncOperation = null; +<# if(IsUnityWebRequest(t)) { #> + if (result.IsError()) + { + throw new UnityWebRequestException(result); + } +<# } #> + return result; +<# } else { #> + asyncOperation = null; +<# } #> + } + } + + public void OnCompleted(Action continuation) + { + UnsafeOnCompleted(continuation); + } + + public void UnsafeOnCompleted(Action continuation) + { + Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction); + continuationAction = PooledDelegate.Create(continuation); + asyncOperation.completed += continuationAction; + } + } + + sealed class <#= t.typeName #>ConfiguredSource : <#= ToIUniTaskSourceReturnType(t.returnType) #>, IPlayerLoopItem, ITaskPoolNode<<#= t.typeName #>ConfiguredSource> + { + static TaskPool<<#= t.typeName #>ConfiguredSource> pool; + <#= t.typeName #>ConfiguredSource nextNode; + public ref <#= t.typeName #>ConfiguredSource NextNode => ref nextNode; + + static <#= t.typeName #>ConfiguredSource() + { + TaskPool.RegisterSizeGetter(typeof(<#= t.typeName #>ConfiguredSource), () => pool.Size); + } + + <#= t.typeName #> asyncOperation; + IProgress progress; + CancellationToken cancellationToken; + + UniTaskCompletionSourceCore<<#= IsVoid(t) ? "AsyncUnit" : t.returnType #>> core; + + <#= t.typeName #>ConfiguredSource() + { + + } + + public static <#= ToIUniTaskSourceReturnType(t.returnType) #> Create(<#= t.typeName #> asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource<#= IsVoid(t) ? "" : $"<{t.returnType}>" #>.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new <#= t.typeName #>ConfiguredSource(); + } + + result.asyncOperation = asyncOperation; + result.progress = progress; + result.cancellationToken = cancellationToken; + + TaskTracker.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; + } + + public <#= t.returnType #> GetResult(short token) + { + try + { +<# if (!IsVoid(t)) { #> + return core.GetResult(token); +<# } else { #> + core.GetResult(token); +<# } #> + } + finally + { + TryReturn(); + } + } + +<# if (!IsVoid(t)) { #> + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } +<# } #> + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public bool MoveNext() + { + if (cancellationToken.IsCancellationRequested) + { +<# if(IsUnityWebRequest(t)) { #> + asyncOperation.webRequest.Abort(); +<# } #> + core.TrySetCanceled(cancellationToken); + return false; + } + + if (progress != null) + { + progress.Report(asyncOperation.progress); + } + + if (asyncOperation.isDone) + { +<# if(IsUnityWebRequest(t)) { #> + if (asyncOperation.webRequest.IsError()) + { + core.TrySetException(new UnityWebRequestException(asyncOperation.webRequest)); + } + else + { + core.TrySetResult(asyncOperation.webRequest); + } +<# } else { #> + core.TrySetResult(<#= IsVoid(t) ? "AsyncUnit.Default" : $"asyncOperation.{t.returnField}" #>); +<# } #> + return false; + } + + return true; + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + asyncOperation = default; + progress = default; + cancellationToken = default; + return pool.TryPush(this); + } + } + + #endregion +<# if(IsUnityWebRequest(t) || IsAssetBundleModule(t)) { #> +#endif +<# } #> + +<# } #> + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.tt.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.tt.meta new file mode 100644 index 0000000..24dc439 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.tt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: b1053c85b3f0794488b10e6de53e9c02 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.uGUI.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.uGUI.cs new file mode 100644 index 0000000..11ebd63 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.uGUI.cs @@ -0,0 +1,826 @@ +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT +using System; +using System.Threading; +using UnityEngine; +using UnityEngine.Events; +using UnityEngine.UI; + +namespace Cysharp.Threading.Tasks +{ + public static partial class UnityAsyncExtensions + { + public static AsyncUnityEventHandler GetAsyncEventHandler(this UnityEvent unityEvent, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(unityEvent, cancellationToken, false); + } + + public static UniTask OnInvokeAsync(this UnityEvent unityEvent, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(unityEvent, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnInvokeAsAsyncEnumerable(this UnityEvent unityEvent, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(unityEvent, cancellationToken); + } + + public static AsyncUnityEventHandler GetAsyncEventHandler(this UnityEvent unityEvent, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(unityEvent, cancellationToken, false); + } + + public static UniTask OnInvokeAsync(this UnityEvent unityEvent, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(unityEvent, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnInvokeAsAsyncEnumerable(this UnityEvent unityEvent, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(unityEvent, cancellationToken); + } + + public static IAsyncClickEventHandler GetAsyncClickEventHandler(this Button button) + { + return new AsyncUnityEventHandler(button.onClick, button.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncClickEventHandler GetAsyncClickEventHandler(this Button button, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(button.onClick, cancellationToken, false); + } + + public static UniTask OnClickAsync(this Button button) + { + return new AsyncUnityEventHandler(button.onClick, button.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask OnClickAsync(this Button button, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(button.onClick, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnClickAsAsyncEnumerable(this Button button) + { + return new UnityEventHandlerAsyncEnumerable(button.onClick, button.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable OnClickAsAsyncEnumerable(this Button button, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(button.onClick, cancellationToken); + } + + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this Toggle toggle) + { + return new AsyncUnityEventHandler(toggle.onValueChanged, toggle.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this Toggle toggle, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(toggle.onValueChanged, cancellationToken, false); + } + + public static UniTask OnValueChangedAsync(this Toggle toggle) + { + return new AsyncUnityEventHandler(toggle.onValueChanged, toggle.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask OnValueChangedAsync(this Toggle toggle, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(toggle.onValueChanged, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this Toggle toggle) + { + return new UnityEventHandlerAsyncEnumerable(toggle.onValueChanged, toggle.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this Toggle toggle, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(toggle.onValueChanged, cancellationToken); + } + + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this Scrollbar scrollbar) + { + return new AsyncUnityEventHandler(scrollbar.onValueChanged, scrollbar.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this Scrollbar scrollbar, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(scrollbar.onValueChanged, cancellationToken, false); + } + + public static UniTask OnValueChangedAsync(this Scrollbar scrollbar) + { + return new AsyncUnityEventHandler(scrollbar.onValueChanged, scrollbar.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask OnValueChangedAsync(this Scrollbar scrollbar, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(scrollbar.onValueChanged, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this Scrollbar scrollbar) + { + return new UnityEventHandlerAsyncEnumerable(scrollbar.onValueChanged, scrollbar.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this Scrollbar scrollbar, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(scrollbar.onValueChanged, cancellationToken); + } + + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this ScrollRect scrollRect) + { + return new AsyncUnityEventHandler(scrollRect.onValueChanged, scrollRect.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this ScrollRect scrollRect, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(scrollRect.onValueChanged, cancellationToken, false); + } + + public static UniTask OnValueChangedAsync(this ScrollRect scrollRect) + { + return new AsyncUnityEventHandler(scrollRect.onValueChanged, scrollRect.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask OnValueChangedAsync(this ScrollRect scrollRect, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(scrollRect.onValueChanged, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this ScrollRect scrollRect) + { + return new UnityEventHandlerAsyncEnumerable(scrollRect.onValueChanged, scrollRect.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this ScrollRect scrollRect, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(scrollRect.onValueChanged, cancellationToken); + } + + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this Slider slider) + { + return new AsyncUnityEventHandler(slider.onValueChanged, slider.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this Slider slider, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(slider.onValueChanged, cancellationToken, false); + } + + public static UniTask OnValueChangedAsync(this Slider slider) + { + return new AsyncUnityEventHandler(slider.onValueChanged, slider.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask OnValueChangedAsync(this Slider slider, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(slider.onValueChanged, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this Slider slider) + { + return new UnityEventHandlerAsyncEnumerable(slider.onValueChanged, slider.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this Slider slider, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(slider.onValueChanged, cancellationToken); + } + + public static IAsyncEndEditEventHandler GetAsyncEndEditEventHandler(this InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onEndEdit, inputField.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncEndEditEventHandler GetAsyncEndEditEventHandler(this InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onEndEdit, cancellationToken, false); + } + + public static UniTask OnEndEditAsync(this InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onEndEdit, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask OnEndEditAsync(this InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onEndEdit, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnEndEditAsAsyncEnumerable(this InputField inputField) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onEndEdit, inputField.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable OnEndEditAsAsyncEnumerable(this InputField inputField, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onEndEdit, cancellationToken); + } + + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onValueChanged, cancellationToken, false); + } + + public static UniTask OnValueChangedAsync(this InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask OnValueChangedAsync(this InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onValueChanged, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this InputField inputField) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this InputField inputField, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onValueChanged, cancellationToken); + } + + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this Dropdown dropdown) + { + return new AsyncUnityEventHandler(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this Dropdown dropdown, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(dropdown.onValueChanged, cancellationToken, false); + } + + public static UniTask OnValueChangedAsync(this Dropdown dropdown) + { + return new AsyncUnityEventHandler(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask OnValueChangedAsync(this Dropdown dropdown, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(dropdown.onValueChanged, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this Dropdown dropdown) + { + return new UnityEventHandlerAsyncEnumerable(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this Dropdown dropdown, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(dropdown.onValueChanged, cancellationToken); + } + } + + public interface IAsyncClickEventHandler : IDisposable + { + UniTask OnClickAsync(); + } + + public interface IAsyncValueChangedEventHandler : IDisposable + { + UniTask OnValueChangedAsync(); + } + + public interface IAsyncEndEditEventHandler : IDisposable + { + UniTask OnEndEditAsync(); + } + + // for TMP_PRO + + public interface IAsyncEndTextSelectionEventHandler : IDisposable + { + UniTask OnEndTextSelectionAsync(); + } + + public interface IAsyncTextSelectionEventHandler : IDisposable + { + UniTask OnTextSelectionAsync(); + } + + public interface IAsyncDeselectEventHandler : IDisposable + { + UniTask OnDeselectAsync(); + } + + public interface IAsyncSelectEventHandler : IDisposable + { + UniTask OnSelectAsync(); + } + + public interface IAsyncSubmitEventHandler : IDisposable + { + UniTask OnSubmitAsync(); + } + + internal class TextSelectionEventConverter : UnityEvent<(string, int, int)>, IDisposable + { + readonly UnityEvent innerEvent; + readonly UnityAction invokeDelegate; + + + public TextSelectionEventConverter(UnityEvent unityEvent) + { + this.innerEvent = unityEvent; + this.invokeDelegate = InvokeCore; + + innerEvent.AddListener(invokeDelegate); + } + + void InvokeCore(string item1, int item2, int item3) + { + innerEvent.Invoke(item1, item2, item3); + } + + public void Dispose() + { + innerEvent.RemoveListener(invokeDelegate); + } + } + + public class AsyncUnityEventHandler : IUniTaskSource, IDisposable, IAsyncClickEventHandler + { + static Action cancellationCallback = CancellationCallback; + + readonly UnityAction action; + readonly UnityEvent unityEvent; + + CancellationToken cancellationToken; + CancellationTokenRegistration registration; + bool isDisposed; + bool callOnce; + + UniTaskCompletionSourceCore core; + + public AsyncUnityEventHandler(UnityEvent unityEvent, CancellationToken cancellationToken, bool callOnce) + { + this.cancellationToken = cancellationToken; + if (cancellationToken.IsCancellationRequested) + { + isDisposed = true; + return; + } + + this.action = Invoke; + this.unityEvent = unityEvent; + this.callOnce = callOnce; + + unityEvent.AddListener(action); + + if (cancellationToken.CanBeCanceled) + { + registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this); + } + + TaskTracker.TrackActiveTask(this, 3); + } + + public UniTask OnInvokeAsync() + { + core.Reset(); + if (isDisposed) + { + core.TrySetCanceled(this.cancellationToken); + } + return new UniTask(this, core.Version); + } + + void Invoke() + { + core.TrySetResult(AsyncUnit.Default); + } + + static void CancellationCallback(object state) + { + var self = (AsyncUnityEventHandler)state; + self.Dispose(); + } + + public void Dispose() + { + if (!isDisposed) + { + isDisposed = true; + TaskTracker.RemoveTracking(this); + registration.Dispose(); + if (unityEvent != null) + { + unityEvent.RemoveListener(action); + } + core.TrySetCanceled(cancellationToken); + } + } + + UniTask IAsyncClickEventHandler.OnClickAsync() + { + return OnInvokeAsync(); + } + + void IUniTaskSource.GetResult(short token) + { + try + { + core.GetResult(token); + } + finally + { + if (callOnce) + { + Dispose(); + } + } + } + + UniTaskStatus IUniTaskSource.GetStatus(short token) + { + return core.GetStatus(token); + } + + UniTaskStatus IUniTaskSource.UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + public class AsyncUnityEventHandler : IUniTaskSource, IDisposable, IAsyncValueChangedEventHandler, IAsyncEndEditEventHandler + , IAsyncEndTextSelectionEventHandler, IAsyncTextSelectionEventHandler, IAsyncDeselectEventHandler, IAsyncSelectEventHandler, IAsyncSubmitEventHandler + { + static Action cancellationCallback = CancellationCallback; + + readonly UnityAction action; + readonly UnityEvent unityEvent; + + CancellationToken cancellationToken; + CancellationTokenRegistration registration; + bool isDisposed; + bool callOnce; + + UniTaskCompletionSourceCore core; + + public AsyncUnityEventHandler(UnityEvent unityEvent, CancellationToken cancellationToken, bool callOnce) + { + this.cancellationToken = cancellationToken; + if (cancellationToken.IsCancellationRequested) + { + isDisposed = true; + return; + } + + this.action = Invoke; + this.unityEvent = unityEvent; + this.callOnce = callOnce; + + unityEvent.AddListener(action); + + if (cancellationToken.CanBeCanceled) + { + registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this); + } + + TaskTracker.TrackActiveTask(this, 3); + } + + public UniTask OnInvokeAsync() + { + core.Reset(); + if (isDisposed) + { + core.TrySetCanceled(this.cancellationToken); + } + return new UniTask(this, core.Version); + } + + void Invoke(T result) + { + core.TrySetResult(result); + } + + static void CancellationCallback(object state) + { + var self = (AsyncUnityEventHandler)state; + self.Dispose(); + } + + public void Dispose() + { + if (!isDisposed) + { + isDisposed = true; + TaskTracker.RemoveTracking(this); + registration.Dispose(); + if (unityEvent != null) + { + // Dispose inner delegate for TextSelectionEventConverter + if (unityEvent is IDisposable disp) + { + disp.Dispose(); + } + + unityEvent.RemoveListener(action); + } + + core.TrySetCanceled(); + } + } + + UniTask IAsyncValueChangedEventHandler.OnValueChangedAsync() + { + return OnInvokeAsync(); + } + + UniTask IAsyncEndEditEventHandler.OnEndEditAsync() + { + return OnInvokeAsync(); + } + + UniTask IAsyncEndTextSelectionEventHandler.OnEndTextSelectionAsync() + { + return OnInvokeAsync(); + } + + UniTask IAsyncTextSelectionEventHandler.OnTextSelectionAsync() + { + return OnInvokeAsync(); + } + + UniTask IAsyncDeselectEventHandler.OnDeselectAsync() + { + return OnInvokeAsync(); + } + + UniTask IAsyncSelectEventHandler.OnSelectAsync() + { + return OnInvokeAsync(); + } + + UniTask IAsyncSubmitEventHandler.OnSubmitAsync() + { + return OnInvokeAsync(); + } + + T IUniTaskSource.GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + if (callOnce) + { + Dispose(); + } + } + } + + void IUniTaskSource.GetResult(short token) + { + ((IUniTaskSource)this).GetResult(token); + } + + UniTaskStatus IUniTaskSource.GetStatus(short token) + { + return core.GetStatus(token); + } + + UniTaskStatus IUniTaskSource.UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + void IUniTaskSource.OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + } + + public class UnityEventHandlerAsyncEnumerable : IUniTaskAsyncEnumerable + { + readonly UnityEvent unityEvent; + readonly CancellationToken cancellationToken1; + + public UnityEventHandlerAsyncEnumerable(UnityEvent unityEvent, CancellationToken cancellationToken) + { + this.unityEvent = unityEvent; + this.cancellationToken1 = cancellationToken; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + if (this.cancellationToken1 == cancellationToken) + { + return new UnityEventHandlerAsyncEnumerator(unityEvent, this.cancellationToken1, CancellationToken.None); + } + else + { + return new UnityEventHandlerAsyncEnumerator(unityEvent, this.cancellationToken1, cancellationToken); + } + } + + class UnityEventHandlerAsyncEnumerator : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action cancel1 = OnCanceled1; + static readonly Action cancel2 = OnCanceled2; + + readonly UnityEvent unityEvent; + CancellationToken cancellationToken1; + CancellationToken cancellationToken2; + + UnityAction unityAction; + CancellationTokenRegistration registration1; + CancellationTokenRegistration registration2; + bool isDisposed; + + public UnityEventHandlerAsyncEnumerator(UnityEvent unityEvent, CancellationToken cancellationToken1, CancellationToken cancellationToken2) + { + this.unityEvent = unityEvent; + this.cancellationToken1 = cancellationToken1; + this.cancellationToken2 = cancellationToken2; + } + + public AsyncUnit Current => default; + + public UniTask MoveNextAsync() + { + cancellationToken1.ThrowIfCancellationRequested(); + cancellationToken2.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (unityAction == null) + { + unityAction = Invoke; + + TaskTracker.TrackActiveTask(this, 3); + unityEvent.AddListener(unityAction); + if (cancellationToken1.CanBeCanceled) + { + registration1 = cancellationToken1.RegisterWithoutCaptureExecutionContext(cancel1, this); + } + if (cancellationToken2.CanBeCanceled) + { + registration2 = cancellationToken1.RegisterWithoutCaptureExecutionContext(cancel2, this); + } + } + + return new UniTask(this, completionSource.Version); + } + + void Invoke() + { + completionSource.TrySetResult(true); + } + + static void OnCanceled1(object state) + { + var self = (UnityEventHandlerAsyncEnumerator)state; + self.DisposeAsync().Forget(); + } + + static void OnCanceled2(object state) + { + var self = (UnityEventHandlerAsyncEnumerator)state; + self.DisposeAsync().Forget(); + } + + public UniTask DisposeAsync() + { + if (!isDisposed) + { + isDisposed = true; + TaskTracker.RemoveTracking(this); + registration1.Dispose(); + registration2.Dispose(); + unityEvent.RemoveListener(unityAction); + } + + return default; + } + } + } + + public class UnityEventHandlerAsyncEnumerable : IUniTaskAsyncEnumerable + { + readonly UnityEvent unityEvent; + readonly CancellationToken cancellationToken1; + + public UnityEventHandlerAsyncEnumerable(UnityEvent unityEvent, CancellationToken cancellationToken) + { + this.unityEvent = unityEvent; + this.cancellationToken1 = cancellationToken; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + if (this.cancellationToken1 == cancellationToken) + { + return new UnityEventHandlerAsyncEnumerator(unityEvent, this.cancellationToken1, CancellationToken.None); + } + else + { + return new UnityEventHandlerAsyncEnumerator(unityEvent, this.cancellationToken1, cancellationToken); + } + } + + class UnityEventHandlerAsyncEnumerator : MoveNextSource, IUniTaskAsyncEnumerator + { + static readonly Action cancel1 = OnCanceled1; + static readonly Action cancel2 = OnCanceled2; + + readonly UnityEvent unityEvent; + CancellationToken cancellationToken1; + CancellationToken cancellationToken2; + + UnityAction unityAction; + CancellationTokenRegistration registration1; + CancellationTokenRegistration registration2; + bool isDisposed; + + public UnityEventHandlerAsyncEnumerator(UnityEvent unityEvent, CancellationToken cancellationToken1, CancellationToken cancellationToken2) + { + this.unityEvent = unityEvent; + this.cancellationToken1 = cancellationToken1; + this.cancellationToken2 = cancellationToken2; + } + + public T Current { get; private set; } + + public UniTask MoveNextAsync() + { + cancellationToken1.ThrowIfCancellationRequested(); + cancellationToken2.ThrowIfCancellationRequested(); + completionSource.Reset(); + + if (unityAction == null) + { + unityAction = Invoke; + + TaskTracker.TrackActiveTask(this, 3); + unityEvent.AddListener(unityAction); + if (cancellationToken1.CanBeCanceled) + { + registration1 = cancellationToken1.RegisterWithoutCaptureExecutionContext(cancel1, this); + } + if (cancellationToken2.CanBeCanceled) + { + registration2 = cancellationToken1.RegisterWithoutCaptureExecutionContext(cancel2, this); + } + } + + return new UniTask(this, completionSource.Version); + } + + void Invoke(T value) + { + Current = value; + completionSource.TrySetResult(true); + } + + static void OnCanceled1(object state) + { + var self = (UnityEventHandlerAsyncEnumerator)state; + self.DisposeAsync().Forget(); + } + + static void OnCanceled2(object state) + { + var self = (UnityEventHandlerAsyncEnumerator)state; + self.DisposeAsync().Forget(); + } + + public UniTask DisposeAsync() + { + if (!isDisposed) + { + isDisposed = true; + TaskTracker.RemoveTracking(this); + registration1.Dispose(); + registration2.Dispose(); + if (unityEvent is IDisposable disp) + { + disp.Dispose(); + } + unityEvent.RemoveListener(unityAction); + } + + return default; + } + } + } +} + +#endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.uGUI.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.uGUI.cs.meta new file mode 100644 index 0000000..90c5d51 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityAsyncExtensions.uGUI.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6804799fba2376d4099561d176101aff +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityBindingExtensions.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityBindingExtensions.cs new file mode 100644 index 0000000..749f02a --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityBindingExtensions.cs @@ -0,0 +1,245 @@ +using System; +using System.Threading; +using UnityEngine; +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT +using UnityEngine.UI; +#endif + +namespace Cysharp.Threading.Tasks +{ + public static class UnityBindingExtensions + { +#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT + // -> Text + + public static void BindTo(this IUniTaskAsyncEnumerable source, Text text, bool rebindOnError = true) + { + BindToCore(source, text, text.GetCancellationTokenOnDestroy(), rebindOnError).Forget(); + } + + public static void BindTo(this IUniTaskAsyncEnumerable source, Text text, CancellationToken cancellationToken, bool rebindOnError = true) + { + BindToCore(source, text, cancellationToken, rebindOnError).Forget(); + } + + static async UniTaskVoid BindToCore(IUniTaskAsyncEnumerable source, Text text, CancellationToken cancellationToken, bool rebindOnError) + { + var repeat = false; + BIND_AGAIN: + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (true) + { + bool moveNext; + try + { + moveNext = await e.MoveNextAsync(); + repeat = false; + } + catch (Exception ex) + { + if (ex is OperationCanceledException) return; + + if (rebindOnError && !repeat) + { + repeat = true; + goto BIND_AGAIN; + } + else + { + throw; + } + } + + if (!moveNext) return; + + text.text = e.Current; + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + // -> Text + + public static void BindTo(this IUniTaskAsyncEnumerable source, Text text, bool rebindOnError = true) + { + BindToCore(source, text, text.GetCancellationTokenOnDestroy(), rebindOnError).Forget(); + } + + public static void BindTo(this IUniTaskAsyncEnumerable source, Text text, CancellationToken cancellationToken, bool rebindOnError = true) + { + BindToCore(source, text, cancellationToken, rebindOnError).Forget(); + } + + public static void BindTo(this AsyncReactiveProperty source, Text text, bool rebindOnError = true) + { + BindToCore(source, text, text.GetCancellationTokenOnDestroy(), rebindOnError).Forget(); + } + + static async UniTaskVoid BindToCore(IUniTaskAsyncEnumerable source, Text text, CancellationToken cancellationToken, bool rebindOnError) + { + var repeat = false; + BIND_AGAIN: + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (true) + { + bool moveNext; + try + { + moveNext = await e.MoveNextAsync(); + repeat = false; + } + catch (Exception ex) + { + if (ex is OperationCanceledException) return; + + if (rebindOnError && !repeat) + { + repeat = true; + goto BIND_AGAIN; + } + else + { + throw; + } + } + + if (!moveNext) return; + + text.text = e.Current.ToString(); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + + // -> Selectable + + public static void BindTo(this IUniTaskAsyncEnumerable source, Selectable selectable, bool rebindOnError = true) + { + BindToCore(source, selectable, selectable.GetCancellationTokenOnDestroy(), rebindOnError).Forget(); + } + + public static void BindTo(this IUniTaskAsyncEnumerable source, Selectable selectable, CancellationToken cancellationToken, bool rebindOnError = true) + { + BindToCore(source, selectable, cancellationToken, rebindOnError).Forget(); + } + + static async UniTaskVoid BindToCore(IUniTaskAsyncEnumerable source, Selectable selectable, CancellationToken cancellationToken, bool rebindOnError) + { + var repeat = false; + BIND_AGAIN: + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (true) + { + bool moveNext; + try + { + moveNext = await e.MoveNextAsync(); + repeat = false; + } + catch (Exception ex) + { + if (ex is OperationCanceledException) return; + + if (rebindOnError && !repeat) + { + repeat = true; + goto BIND_AGAIN; + } + else + { + throw; + } + } + + if (!moveNext) return; + + + selectable.interactable = e.Current; + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } +#endif + + // -> Action + + public static void BindTo(this IUniTaskAsyncEnumerable source, TObject monoBehaviour, Action bindAction, bool rebindOnError = true) + where TObject : MonoBehaviour + { + BindToCore(source, monoBehaviour, bindAction, monoBehaviour.GetCancellationTokenOnDestroy(), rebindOnError).Forget(); + } + + public static void BindTo(this IUniTaskAsyncEnumerable source, TObject bindTarget, Action bindAction, CancellationToken cancellationToken, bool rebindOnError = true) + { + BindToCore(source, bindTarget, bindAction, cancellationToken, rebindOnError).Forget(); + } + + static async UniTaskVoid BindToCore(IUniTaskAsyncEnumerable source, TObject bindTarget, Action bindAction, CancellationToken cancellationToken, bool rebindOnError) + { + var repeat = false; + BIND_AGAIN: + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (true) + { + bool moveNext; + try + { + moveNext = await e.MoveNextAsync(); + repeat = false; + } + catch (Exception ex) + { + if (ex is OperationCanceledException) return; + + if (rebindOnError && !repeat) + { + repeat = true; + goto BIND_AGAIN; + } + else + { + throw; + } + } + + if (!moveNext) return; + + bindAction(bindTarget, e.Current); + } + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + } +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityBindingExtensions.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityBindingExtensions.cs.meta new file mode 100644 index 0000000..3fae798 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityBindingExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 090b20e3528552b4a8d751f7df525c2b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityWebRequestException.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityWebRequestException.cs new file mode 100644 index 0000000..9585769 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityWebRequestException.cs @@ -0,0 +1,67 @@ +#if ENABLE_UNITYWEBREQUEST && (!UNITY_2019_1_OR_NEWER || UNITASK_WEBREQUEST_SUPPORT) + +using System; +using System.Collections.Generic; +using UnityEngine.Networking; + +namespace Cysharp.Threading.Tasks +{ + public class UnityWebRequestException : Exception + { + public UnityWebRequest UnityWebRequest { get; } +#if UNITY_2020_2_OR_NEWER + public UnityWebRequest.Result Result { get; } +#else + public bool IsNetworkError { get; } + public bool IsHttpError { get; } +#endif + public string Error { get; } + public string Text { get; } + public long ResponseCode { get; } + public Dictionary ResponseHeaders { get; } + + string msg; + + public UnityWebRequestException(UnityWebRequest unityWebRequest) + { + this.UnityWebRequest = unityWebRequest; +#if UNITY_2020_2_OR_NEWER + this.Result = unityWebRequest.result; +#else + this.IsNetworkError = unityWebRequest.isNetworkError; + this.IsHttpError = unityWebRequest.isHttpError; +#endif + this.Error = unityWebRequest.error; + this.ResponseCode = unityWebRequest.responseCode; + if (UnityWebRequest.downloadHandler != null) + { + if (unityWebRequest.downloadHandler is DownloadHandlerBuffer dhb) + { + this.Text = dhb.text; + } + } + this.ResponseHeaders = unityWebRequest.GetResponseHeaders(); + } + + public override string Message + { + get + { + if (msg == null) + { + if(!string.IsNullOrWhiteSpace(Text)) + { + msg = Error + Environment.NewLine + Text; + } + else + { + msg = Error; + } + } + return msg; + } + } + } +} + +#endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityWebRequestException.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityWebRequestException.cs.meta new file mode 100644 index 0000000..50c475e --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/UnityWebRequestException.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 013a499e522703a42962a779b4d9850c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/_InternalVisibleTo.cs b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/_InternalVisibleTo.cs new file mode 100644 index 0000000..cd4db41 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/_InternalVisibleTo.cs @@ -0,0 +1,7 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("UniTask.Linq")] +[assembly: InternalsVisibleTo("UniTask.Addressables")] +[assembly: InternalsVisibleTo("UniTask.DOTween")] +[assembly: InternalsVisibleTo("UniTask.TextMeshPro")] +[assembly: InternalsVisibleTo("UniTask.YooAsset")] \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/_InternalVisibleTo.cs.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/_InternalVisibleTo.cs.meta new file mode 100644 index 0000000..2ec6cd3 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/Runtime/_InternalVisibleTo.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8507e97eb606fad4b99c6edf92e19cb8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/package.json b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/package.json new file mode 100644 index 0000000..f2e0b59 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/package.json @@ -0,0 +1,12 @@ +{ + "name": "com.cysharp.unitask", + "displayName": "UniTask", + "author": { "name": "Cysharp, Inc.", "url": "https://cysharp.co.jp/en/" }, + "version": "2.3.1", + "unity": "2018.4", + "description": "Provides an efficient async/await integration to Unity.", + "keywords": [ "async/await", "async", "Task", "UniTask" ], + "license": "MIT", + "category": "Task", + "dependencies": {} +} diff --git a/Assets/YooAsset/Samples~/UniTask Sample/UniTask/package.json.meta b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/package.json.meta new file mode 100644 index 0000000..65439e6 --- /dev/null +++ b/Assets/YooAsset/Samples~/UniTask Sample/UniTask/package.json.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: d1a9a71f68bb0d04db91ddaa3329abf9 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: