-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScriptArgument.cs
302 lines (202 loc) · 12 KB
/
ScriptArgument.cs
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
namespace Int19h.Bannerlord.CSharp.Scripting {
public class ScriptArgument {
protected readonly object?[] Values;
protected ScriptArgument(object?[] values) {
Trace.Assert(values.Length > 0);
Values = values;
}
private static IEnumerable<object?> Flatten(object value) {
switch (value) {
case IEnumerable<object?> xs:
foreach (var x in xs) {
yield return x;
}
break;
case ITuple tuple:
for (int i = 0; i < tuple.Length; ++i) {
foreach (var x in Flatten(tuple[i])) {
yield return x;
}
}
break;
default:
yield return value;
break;
}
}
public static object? Wrap(object? value) {
if (value is null or All or IDataStore or IGameStarter) {
return value;
}
var items = Flatten(value).ToArray();
dynamic arg =
items.Length == 1 && items[0] is var x and not null ?
new UnrestrictedScriptArgument.Scalar(x) :
new UnrestrictedScriptArgument(items);
foreach (dynamic? item in items) {
arg = arg.RestrictBy(item);
}
return arg;
}
private static ValueTuple<TTarget>? Convert<TTarget>(object value) => null;
private static ValueTuple<TTarget>? Convert<TTarget>(TTarget value) => ValueTuple.Create(value);
protected static TTarget? LookUp<TTarget>(object? value) {
if (value is null) {
return (TTarget?)value;
} else if (Convert((dynamic)value) is ValueTuple<TTarget> result) {
return result.Item1;
}
if (LookupTables.Get<TTarget>() is var table and not null) {
switch (value) {
case string s:
return table[s];
case Lookup lookup:
return table[lookup];
}
}
throw new InvalidOperationException($"Invalid {typeof(TTarget).Name} lookup: {value}");
}
protected TTarget?[] LookUp<TTarget>()
=> Values.Select(value => LookUp<TTarget>(value)).ToArray();
internal dynamic RestrictBy(object? o) => new ScriptArgument(Values);
internal dynamic RestrictBy(string s) => this;
internal dynamic RestrictBy(Lookup lookup) => this;
public class Scalar : ScriptArgument {
internal Scalar(object value) : base(new[] { value }) { }
internal object Value => Values[0]!;
}
}
public class ScriptArgument<T1> : ScriptArgument {
internal ScriptArgument(object?[] values) : base(values) { }
public static implicit operator T1?[](ScriptArgument<T1> arg) => arg.LookUp<T1>();
internal new dynamic RestrictBy(string s) => this;
internal new dynamic RestrictBy(Lookup lookup) => this;
internal dynamic RestrictBy(T1 item) => new ScriptArgument<T1>(Values);
public new class Scalar : ScriptArgument.Scalar {
public Scalar(object value) : base(value) { }
public static implicit operator T1?(ScriptArgument<T1>.Scalar arg) => LookUp<T1>(arg.Value);
public static implicit operator T1?[](ScriptArgument<T1>.Scalar arg) => arg.LookUp<T1>();
}
}
public class ScriptArgument<T1, T2> : ScriptArgument<T1> {
internal ScriptArgument(object?[] values) : base(values) { }
public static implicit operator T2?[](ScriptArgument<T1, T2> arg) => arg.LookUp<T2>();
internal new dynamic RestrictBy(string s) => this;
internal new dynamic RestrictBy(Lookup lookup) => this;
internal dynamic RestrictBy(T2 item) => new ScriptArgument<T2>(Values);
public new class Scalar : ScriptArgument<T1>.Scalar {
public Scalar(object value) : base(value) { }
public static implicit operator T2?(ScriptArgument<T1, T2>.Scalar arg) => LookUp<T2>(arg.Value);
public static implicit operator T2?[](ScriptArgument<T1, T2>.Scalar arg) => arg.LookUp<T2>();
}
}
public class ScriptArgument<T1, T2, T3> : ScriptArgument<T1, T2> {
internal ScriptArgument(object?[] values) : base(values) { }
public static implicit operator T3?[](ScriptArgument<T1, T2, T3> arg) => arg.LookUp<T3>();
internal new dynamic RestrictBy(string s) => this;
internal new dynamic RestrictBy(Lookup lookup) => this;
internal dynamic RestrictBy(T3 item) => new ScriptArgument<T3>(Values);
public new class Scalar : ScriptArgument<T1, T2>.Scalar {
public Scalar(object value) : base(value) { }
public static implicit operator T3?(ScriptArgument<T1, T2, T3>.Scalar arg) => LookUp<T3>(arg.Value);
public static implicit operator T3?[](ScriptArgument<T1, T2, T3>.Scalar arg) => arg.LookUp<T3>();
}
}
public class ScriptArgument<T1, T2, T3, T4> : ScriptArgument<T1, T2, T3> {
internal ScriptArgument(object?[] values) : base(values) { }
public static implicit operator T4?[](ScriptArgument<T1, T2, T3, T4> arg) => arg.LookUp<T4>();
internal new dynamic RestrictBy(string s) => this;
internal new dynamic RestrictBy(Lookup lookup) => this;
internal dynamic RestrictBy(T4 item) => new ScriptArgument<T4>(Values);
public new class Scalar : ScriptArgument<T1, T2, T3>.Scalar {
public Scalar(object value) : base(value) { }
public static implicit operator T4?(ScriptArgument<T1, T2, T3, T4>.Scalar arg) => LookUp<T4>(arg.Value);
public static implicit operator T4?[](ScriptArgument<T1, T2, T3, T4>.Scalar arg) => arg.LookUp<T4>();
}
}
public class ScriptArgument<T1, T2, T3, T4, T5> : ScriptArgument<T1, T2, T3, T4> {
internal ScriptArgument(object?[] values) : base(values) { }
public static implicit operator T5?[](ScriptArgument<T1, T2, T3, T4, T5> arg) => arg.LookUp<T5>();
internal new dynamic RestrictBy(string s) => this;
internal new dynamic RestrictBy(Lookup lookup) => this;
internal dynamic RestrictBy(T5 item) => new ScriptArgument<T5>(Values);
public new class Scalar : ScriptArgument<T1, T2, T3, T4>.Scalar {
public Scalar(object value) : base(value) { }
public static implicit operator T5?(ScriptArgument<T1, T2, T3, T4, T5>.Scalar arg) => LookUp<T5>(arg.Value);
public static implicit operator T5?[](ScriptArgument<T1, T2, T3, T4, T5>.Scalar arg) => arg.LookUp<T5>();
}
}
public class ScriptArgument<T1, T2, T3, T4, T5, T6> : ScriptArgument<T1, T2, T3, T4, T5> {
internal ScriptArgument(object?[] values) : base(values) { }
public static implicit operator T6?[](ScriptArgument<T1, T2, T3, T4, T5, T6> arg) => arg.LookUp<T6>();
internal new dynamic RestrictBy(string s) => this;
internal new dynamic RestrictBy(Lookup lookup) => this;
internal dynamic RestrictBy(T6 item) => new ScriptArgument<T6>(Values);
public new class Scalar : ScriptArgument<T1, T2, T3, T4, T5>.Scalar {
public Scalar(object value) : base(value) { }
public static implicit operator T6?(ScriptArgument<T1, T2, T3, T4, T5, T6>.Scalar arg) => LookUp<T6>(arg.Value);
public static implicit operator T6?[](ScriptArgument<T1, T2, T3, T4, T5, T6>.Scalar arg) => arg.LookUp<T6>();
}
}
public class ScriptArgument<T1, T2, T3, T4, T5, T6, T7> : ScriptArgument<T1, T2, T3, T4, T5, T6> {
internal ScriptArgument(object?[] values) : base(values) { }
public static implicit operator T7?[](ScriptArgument<T1, T2, T3, T4, T5, T6, T7> arg) => arg.LookUp<T7>();
internal new dynamic RestrictBy(string s) => this;
internal new dynamic RestrictBy(Lookup lookup) => this;
internal dynamic RestrictBy(T7 item) => new ScriptArgument<T7>(Values);
public new class Scalar : ScriptArgument<T1, T2, T3, T4, T5, T6>.Scalar {
public Scalar(object value) : base(value) { }
public static implicit operator T7?(ScriptArgument<T1, T2, T3, T4, T5, T6, T7>.Scalar arg) => LookUp<T7>(arg.Value);
public static implicit operator T7?[](ScriptArgument<T1, T2, T3, T4, T5, T6, T7>.Scalar arg) => arg.LookUp<T7>();
}
}
public class ScriptArgument<T1, T2, T3, T4, T5, T6, T7, T8> : ScriptArgument<T1, T2, T3, T4, T5, T6, T7> {
internal ScriptArgument(object?[] values) : base(values) { }
public static implicit operator T8?[](ScriptArgument<T1, T2, T3, T4, T5, T6, T7, T8> arg) => arg.LookUp<T8>();
internal new dynamic RestrictBy(string s) => this;
internal new dynamic RestrictBy(Lookup lookup) => this;
internal dynamic RestrictBy(T8 item) => new ScriptArgument<T8>(Values);
public new class Scalar : ScriptArgument<T1, T2, T3, T4, T5, T6, T7>.Scalar {
public Scalar(object value) : base(value) { }
public static implicit operator T8?(ScriptArgument<T1, T2, T3, T4, T5, T6, T7, T8>.Scalar arg) => LookUp<T8>(arg.Value);
public static implicit operator T8?[](ScriptArgument<T1, T2, T3, T4, T5, T6, T7, T8>.Scalar arg) => arg.LookUp<T8>();
}
}
public class ScriptArgument<T1, T2, T3, T4, T5, T6, T7, T8, T9> : ScriptArgument<T1, T2, T3, T4, T5, T6, T7, T8> {
internal ScriptArgument(object?[] values) : base(values) { }
public static implicit operator T9?[](ScriptArgument<T1, T2, T3, T4, T5, T6, T7, T8, T9> arg) => arg.LookUp<T9>();
internal new dynamic RestrictBy(string s) => this;
internal new dynamic RestrictBy(Lookup lookup) => this;
internal dynamic RestrictBy(T9 item) => new ScriptArgument<T9>(Values);
public new class Scalar : ScriptArgument<T1, T2, T3, T4, T5, T6, T7, T8>.Scalar {
public Scalar(object value) : base(value) { }
public static implicit operator T9?(ScriptArgument<T1, T2, T3, T4, T5, T6, T7, T8, T9>.Scalar arg) => LookUp<T9>(arg.Value);
public static implicit operator T9?[](ScriptArgument<T1, T2, T3, T4, T5, T6, T7, T8, T9>.Scalar arg) => arg.LookUp<T9>();
}
}
internal class UnrestrictedScriptArgument : ScriptArgument<Kingdom, Clan, Hero, Settlement, Town, Village, MobileParty, ItemObject> {
internal UnrestrictedScriptArgument(object?[] values) : base(values) { }
private dynamic Unrestricted<T>() => new ScriptArgument<T, Kingdom, Clan, Hero, Town, Village, MobileParty, ItemObject>(Values);
internal new dynamic RestrictBy(string s) => Unrestricted<string>();
internal dynamic RestrictBy(NameLookup lookup) => Unrestricted<NameLookup>();
internal dynamic RestrictBy(IdLookup lookup) => Unrestricted<IdLookup>();
internal dynamic RestrictBy<T>(T value) => new ScriptArgument<T>(Values);
public new class Scalar : ScriptArgument<Kingdom, Clan, Hero, Settlement, Town, Village, MobileParty, ItemObject>.Scalar {
public Scalar(object value) : base(value) { }
private dynamic Unrestricted<T>() => new ScriptArgument<T, Kingdom, Clan, Hero, Settlement, Town, Village, MobileParty, ItemObject>.Scalar(Value);
internal new dynamic RestrictBy(string s) => Unrestricted<string>();
internal dynamic RestrictBy(NameLookup lookup) => Unrestricted<NameLookup>();
internal dynamic RestrictBy(IdLookup lookup) => Unrestricted<IdLookup>();
internal dynamic RestrictBy<T>(T value) => new ScriptArgument<T>.Scalar(Value);
}
}
}