三国卡牌客户端基础资源仓库
yyl
2025-08-25 214fe94eaf7f09741a7857775dfffe8c3b83c75c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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<T{x}> 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<WhenAllPromise<<#= t #>>, UniTask<T<#= j #>>.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<T<#= j #>>.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<object> continuation, object state, short token)
            {
                core.OnCompleted(continuation, state, token);
            }
        }
<# } #>
    }
}