-
-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathArgumentBlock.cs
276 lines (259 loc) · 12 KB
/
ArgumentBlock.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
/*
ViVe - Windows feature configuration library
Copyright (C) 2019-2023 @thebookisclosed
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using Albacore.ViVe.NativeEnums;
using System;
using System.Collections.Generic;
namespace Albacore.ViVeTool
{
static class ArgumentBlock
{
internal static FeatureConfigurationTypeEx? Store;
internal static List<uint> IdList;
internal static FeatureConfigurationProperties FeatureConfigurationProperties;
internal static SubscriptionProperties SubscriptionProperties;
#if SET_LKG_COMMAND
internal static BSD_FEATURE_CONFIGURATION_STATE? LKGStatus;
#endif
internal static string FileName;
internal static bool ImportReplace;
internal static bool HelpMode;
internal static bool ShouldUseBothStores { get { return !Store.HasValue || Store.Value == FeatureConfigurationTypeEx.Both; } }
internal static void Initialize(string[] args, ArgumentBlockFlags flags)
{
// Fast help path
if (args.Length == 2 && (args[1] == "/?" || string.Compare(args[1], "/help", true) == 0))
{
HelpMode = true;
return;
}
if (flags != 0 &&
(flags.HasFlag(ArgumentBlockFlags.EnabledStateOptions) ||
flags.HasFlag(ArgumentBlockFlags.Variant) ||
flags.HasFlag(ArgumentBlockFlags.VariantPayloadKind) ||
flags.HasFlag(ArgumentBlockFlags.VariantPayload) ||
flags.HasFlag(ArgumentBlockFlags.Priority)))
FeatureConfigurationProperties = new FeatureConfigurationProperties();
if (flags != 0 &&
(flags.HasFlag(ArgumentBlockFlags.ReportingKind) ||
flags.HasFlag(ArgumentBlockFlags.ReportingOptions) ||
flags.HasFlag(ArgumentBlockFlags.ReportingTarget)))
SubscriptionProperties = new SubscriptionProperties();
var bothStoresArgAllowed = flags.HasFlag(ArgumentBlockFlags.AllowBothStoresArgument);
for (int i = 1; i < args.Length && flags != 0; i++)
{
var firstSc = args[i].IndexOf(':');
var hasValue = firstSc != -1;
var lower = args[i].ToLowerInvariant();
var key = hasValue ? lower.Substring(0, firstSc) : lower;
var value = hasValue ? args[i].Substring(firstSc + 1) : string.Empty;
if (flags.HasFlag(ArgumentBlockFlags.Store) && key == "/store")
{
if (Enum.TryParse(value, true, out FeatureConfigurationTypeEx parsedStore))
{
if (!bothStoresArgAllowed && parsedStore == FeatureConfigurationTypeEx.Both)
{
ConsoleEx.WriteErrorLine(Properties.Resources.InvalidEnumSpecInScenario, value, "Store");
HelpMode = true;
return;
}
Store = parsedStore;
}
else
{
ConsoleEx.WriteErrorLine(Properties.Resources.InvalidEnumSpec, value, "Store");
HelpMode = true;
return;
}
flags &= ~ArgumentBlockFlags.Store;
}
else if (flags.HasFlag(ArgumentBlockFlags.IdList) && key == "/id")
{
if (IdList == null)
IdList = new List<uint>();
foreach (var strId in value.Split(','))
{
if (TryParseDecHexUint(strId, out uint parsedId))
IdList.Add(parsedId);
else
Console.WriteLine("Unable to parse feature ID: {0}", strId);
}
flags &= ~ArgumentBlockFlags.IdList;
}
else if (flags.HasFlag(ArgumentBlockFlags.NameList) && key == "/name")
{
if (IdList == null)
IdList = new List<uint>();
var foundIds = FeatureNaming.FindIdsForNames(value.Split(','));
if (foundIds != null)
IdList.AddRange(foundIds);
flags &= ~ArgumentBlockFlags.NameList;
}
else if (flags.HasFlag(ArgumentBlockFlags.EnabledStateOptions) && key == "/experiment")
{
FeatureConfigurationProperties.EnabledStateOptions = RTL_FEATURE_ENABLED_STATE_OPTIONS.WexpConfig;
flags &= ~ArgumentBlockFlags.EnabledStateOptions;
}
else if (flags.HasFlag(ArgumentBlockFlags.Variant) && key == "/variant")
{
if (TryParseDecHexUint(value, out uint parsedVariant))
FeatureConfigurationProperties.Variant = parsedVariant;
flags &= ~ArgumentBlockFlags.Variant;
}
else if (flags.HasFlag(ArgumentBlockFlags.VariantPayloadKind) && key == "/variantpayloadkind")
{
if (!Enum.TryParse(value, true, out RTL_FEATURE_VARIANT_PAYLOAD_KIND parsedKind))
{
ConsoleEx.WriteErrorLine(Properties.Resources.InvalidEnumSpec, value, "Variant Payload Kind");
HelpMode = true;
return;
}
FeatureConfigurationProperties.VariantPayloadKind = parsedKind;
flags &= ~ArgumentBlockFlags.VariantPayloadKind;
}
else if (flags.HasFlag(ArgumentBlockFlags.VariantPayload) && key == "/variantpayload")
{
if (TryParseDecHexUint(value, out uint parsedPayload))
FeatureConfigurationProperties.VariantPayload = parsedPayload;
flags &= ~ArgumentBlockFlags.VariantPayload;
}
else if (flags.HasFlag(ArgumentBlockFlags.Priority) && key == "/priority")
{
if (!Enum.TryParse(value, true, out RTL_FEATURE_CONFIGURATION_PRIORITY parsedPriority) ||
(uint)parsedPriority > 15)
{
ConsoleEx.WriteErrorLine(Properties.Resources.InvalidEnumSpec, value, "Priority");
HelpMode = true;
return;
}
FeatureConfigurationProperties.Priority = parsedPriority;
flags &= ~ArgumentBlockFlags.Priority;
}
else if (flags.HasFlag(ArgumentBlockFlags.ReportingKind) && key == "/reportingkind")
{
if (TryParseDecHexUshort(value, out ushort parsedKind))
SubscriptionProperties.ReportingKind = parsedKind;
flags &= ~ArgumentBlockFlags.ReportingKind;
}
else if (flags.HasFlag(ArgumentBlockFlags.ReportingOptions) && key == "/reportingoptions")
{
if (TryParseDecHexUshort(value, out ushort parsedOptions))
SubscriptionProperties.ReportingOptions = parsedOptions;
flags &= ~ArgumentBlockFlags.ReportingOptions;
}
else if (flags.HasFlag(ArgumentBlockFlags.ReportingTarget) && key == "/reportingtarget")
{
if (ulong.TryParse(value, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out ulong parsedTarget))
SubscriptionProperties.ReportingTarget = parsedTarget;
flags &= ~ArgumentBlockFlags.ReportingTarget;
}
#if SET_LKG_COMMAND
else if (flags.HasFlag(ArgumentBlockFlags.LKGStatus) && key == "/status")
{
if (!Enum.TryParse(value, true, out BSD_FEATURE_CONFIGURATION_STATE parsedStatus))
{
ConsoleEx.WriteErrorLine(Properties.Resources.InvalidEnumSpec, value, "LKG Status");
HelpMode = true;
return;
}
LKGStatus = parsedStatus;
flags &= ~ArgumentBlockFlags.LKGStatus;
}
#endif
else if (flags.HasFlag(ArgumentBlockFlags.FileName) && key == "/filename")
{
FileName = value;
flags &= ~ArgumentBlockFlags.FileName;
}
else if (flags.HasFlag(ArgumentBlockFlags.ImportReplace) && key == "/replace")
{
ImportReplace = true;
flags &= ~ArgumentBlockFlags.ImportReplace;
}
else if (key == "/?" || string.Compare(key, "/help", true) == 0)
{
HelpMode = true;
return;
}
else
{
ConsoleEx.WriteWarnLine(Properties.Resources.UnrecognizedParameter, key);
}
}
}
private static bool TryParseDecHexUint(string input, out uint output)
{
bool success;
if (input.StartsWith("0x"))
success = uint.TryParse(input.Substring(2), System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out output);
else
success = uint.TryParse(input, out output);
return success;
}
private static bool TryParseDecHexUshort(string input, out ushort output)
{
bool success;
if (input.StartsWith("0x"))
success = ushort.TryParse(input.Substring(2), System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out output);
else
success = ushort.TryParse(input, out output);
return success;
}
}
internal class FeatureConfigurationProperties
{
internal RTL_FEATURE_ENABLED_STATE_OPTIONS EnabledStateOptions;
internal uint Variant;
internal RTL_FEATURE_VARIANT_PAYLOAD_KIND VariantPayloadKind;
internal uint VariantPayload;
internal RTL_FEATURE_CONFIGURATION_PRIORITY? Priority;
}
internal class SubscriptionProperties
{
internal ushort ReportingKind;
internal ushort ReportingOptions;
internal ulong ReportingTarget;
}
[Flags]
enum ArgumentBlockFlags
{
Store = 1,
IdList = 2,
NameList = 4,
EnabledStateOptions = 8,
Variant = 16,
VariantPayloadKind = 32,
VariantPayload = 64,
Priority = 128,
ReportingKind = 256,
ReportingOptions = 512,
ReportingTarget = 1024,
AllowBothStoresArgument = 2048,
#if SET_LKG_COMMAND
LKGStatus = 4096,
#endif
FileName = 8192,
ImportReplace = 16384,
Identifiers = IdList | NameList,
FeatureConfigurationProperties = EnabledStateOptions | Variant | VariantPayloadKind | VariantPayload | Priority,
SubscriptionProperties = ReportingKind | ReportingOptions | ReportingTarget,
Export = Store | AllowBothStoresArgument | FileName
}
enum FeatureConfigurationTypeEx : uint
{
Boot = 0,
Runtime = 1,
Both = 2
}
}