-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathModule.cs
More file actions
264 lines (227 loc) · 9.32 KB
/
Module.cs
File metadata and controls
264 lines (227 loc) · 9.32 KB
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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Ultz.Extensions.Commands.Attributes.Checks;
using Ultz.Extensions.Commands.Builders;
using Ultz.Extensions.Commands.Context;
using Ultz.Extensions.Commands.Parsing.ArgumentParsers;
using Ultz.Extensions.Commands.Results;
using Ultz.Extensions.Commands.Results.Failed.Checks;
using Ultz.Extensions.Commands.Results.Successful;
using Ultz.Extensions.Commands.Results.User;
namespace Ultz.Extensions.Commands.Built
{
/// <summary>
/// Represents a module built using the <see cref="CommandService" />.
/// </summary>
public sealed class Module
{
private bool _isEnabled;
internal Module(CommandService service, ModuleBuilder builder, Module parent)
{
Parent = parent;
Type = builder.Type;
Service = service;
Description = builder.Description;
Remarks = builder.Remarks;
RunMode = builder.RunMode ?? Parent?.RunMode ?? Service.DefaultRunMode;
IgnoresExtraArguments = builder.IgnoresExtraArguments ??
Parent?.IgnoresExtraArguments ?? Service.IgnoresExtraArguments;
CustomArgumentParserType = builder.CustomArgumentParserType ?? Parent?.CustomArgumentParserType;
var aliases = builder.Aliases.ToImmutableArray();
Aliases = aliases;
var fullAliases = ImmutableArray.CreateBuilder<string>();
if (Parent == null || Parent.FullAliases.Count == 0)
{
fullAliases.AddRange(aliases);
}
else if (aliases.Length == 0)
{
fullAliases.AddRange((ImmutableArray<string>) Parent.FullAliases);
}
else
{
for (var i = 0; i < Parent.FullAliases.Count; i++)
{
var parentAlias = Parent.FullAliases[i];
var absolute = parentAlias.Length == 0;
for (var j = 0; j < aliases.Length; j++)
{
var alias = aliases[j];
if (alias.Length == 0)
{
if (absolute)
{
continue;
}
fullAliases.Add(parentAlias);
}
else if (absolute)
{
fullAliases.Add(alias);
}
else
{
fullAliases.Add(string.Concat(parentAlias, Service.Separator, alias));
}
}
}
}
FullAliases = fullAliases.TryMoveToImmutable();
Name = builder.Name ?? Type?.Name;
for (var i = 0; i < builder.Checks.Count; i++)
{
builder.Checks[i].Module = this;
}
Checks = builder.Checks.ToImmutableArray();
Attributes = builder.Attributes.ToImmutableArray();
var modules = ImmutableArray.CreateBuilder<Module>(builder.Submodules.Count);
for (var i = 0; i < builder.Submodules.Count; i++)
{
modules.Add(builder.Submodules[i].Build(service, this));
}
Submodules = modules.TryMoveToImmutable();
var commands = ImmutableArray.CreateBuilder<Command>(builder.Commands.Count);
for (var i = 0; i < builder.Commands.Count; i++)
{
commands.Add(builder.Commands[i].Build(this));
}
Commands = commands.TryMoveToImmutable();
_isEnabled = builder.IsEnabled;
}
/// <summary>
/// Gets the name of this <see cref="Module" />.
/// </summary>
public string Name { get; }
/// <summary>
/// Gets the description of this <see cref="Module" />.
/// </summary>
public string Description { get; }
/// <summary>
/// Gets the remarks of this <see cref="Module" />.
/// </summary>
public string Remarks { get; }
/// <summary>
/// Gets the <see cref="Extensions.Commands.RunMode" /> of this <see cref="Module" />.
/// </summary>
public RunMode RunMode { get; }
/// <summary>
/// Gets whether this <see cref="Module" />'s commands ignore extra arguments or not.
/// </summary>
public bool IgnoresExtraArguments { get; }
/// <summary>
/// Gets or sets the <see cref="Type" /> of a custom <see cref="IArgumentParser" />.
/// </summary>
public Type CustomArgumentParserType { get; }
/// <summary>
/// Gets the aliases of this <see cref="Module" />.
/// </summary>
public IReadOnlyList<string> Aliases { get; }
/// <summary>
/// Gets the full aliases of this <see cref="Module" />.
/// </summary>
/// <remarks>
/// Aliases of parent <see cref="Module" />s and this <see cref="Module" /> concatenated using the
/// <see cref="CommandService.Separator" />.
/// </remarks>
public IReadOnlyList<string> FullAliases { get; }
/// <summary>
/// Gets the checks of this <see cref="Module" />.
/// </summary>
public IReadOnlyList<CheckAttribute> Checks { get; }
/// <summary>
/// Gets the attributes of this <see cref="Module" />.
/// </summary>
public IReadOnlyList<Attribute> Attributes { get; }
/// <summary>
/// Gets the submodules of this <see cref="Module" />.
/// </summary>
public IReadOnlyList<Module> Submodules { get; }
/// <summary>
/// Gets the commands of this <see cref="Module" />.
/// </summary>
public IReadOnlyList<Command> Commands { get; }
/// <summary>
/// Gets whether this <see cref="Module" /> is enabled or not.
/// </summary>
public bool IsEnabled => Parent != null
? Parent.IsEnabled && Volatile.Read(ref _isEnabled)
: Volatile.Read(ref _isEnabled);
/// <summary>
/// Gets the parent <see cref="Module" /> of this <see cref="Module" />.
/// </summary>
public Module Parent { get; }
/// <summary>
/// Gets the <see cref="System.Type" /> this <see cref="Module" /> was built from.
/// <see langword="null" /> if it was built using a <see cref="ModuleBuilder" />.
/// </summary>
public Type Type { get; }
/// <summary>
/// Gets the <see cref="CommandService" /> of this <see cref="Module" />.
/// </summary>
public CommandService Service { get; }
/// <summary>
/// Runs checks on parent <see cref="Module" />s and this <see cref="Module" />.
/// </summary>
/// <param name="context"> The <see cref="CommandContext" /> used for execution. </param>
/// <returns>
/// A <see cref="SuccessfulResult" /> if all of the checks pass, otherwise a <see cref="ChecksFailedResult" />.
/// </returns>
public async Task<IResult> RunChecksAsync(CommandContext context)
{
if (Parent != null)
{
var result = await Parent.RunChecksAsync(context).ConfigureAwait(false);
if (!result.IsSuccessful)
{
return result;
}
}
if (Checks.Count > 0)
{
async Task<(CheckAttribute Check, CheckResult Result)> RunCheckAsync(CheckAttribute check)
{
var checkResult = await check.CheckAsync(context).ConfigureAwait(false);
return (check, checkResult);
}
var checkResults = await Task.WhenAll(Checks.Select(RunCheckAsync)).ConfigureAwait(false);
var failedGroups = checkResults.GroupBy(x => x.Check.Group)
.Where(x => x.Key == null ? x.Any(y => !y.Result.IsSuccessful) : x.All(y => !y.Result.IsSuccessful))
.ToImmutableArray();
if (failedGroups.Length > 0)
{
return new ChecksFailedResult(this,
failedGroups.SelectMany(x => x).Where(x => !x.Result.IsSuccessful).ToImmutableArray());
}
}
return new SuccessfulResult();
}
/// <summary>
/// Enables this <see cref="Module" />.
/// </summary>
public void Enable()
{
Volatile.Write(ref _isEnabled, true);
}
/// <summary>
/// Disables this <see cref="Module" />.
/// </summary>
public void Disable()
{
Volatile.Write(ref _isEnabled, false);
}
/// <summary>
/// Returns <see cref="Name" /> or calls <see cref="object.ToString" /> if it is <see langword="null" />.
/// </summary>
/// <returns>
/// A <see cref="string" /> representing this <see cref="Module" />.
/// </returns>
public override string ToString()
{
return Name ?? base.ToString();
}
}
}