-
-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathProgram.cs
869 lines (801 loc) · 37.2 KB
/
Program.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
/*
ViVeTool - Windows feature configuration tool
Copyright (C) 2019-2025 @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 System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Albacore.ViVe;
using Albacore.ViVe.Exceptions;
using Albacore.ViVe.NativeEnums;
using Albacore.ViVe.NativeStructs;
namespace Albacore.ViVeTool
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Properties.Resources.Branding);
if (args.Length < 1)
{
PrintHelp();
return;
}
if (Environment.OSVersion.Version.Build < 18963)
{
// An attempt at stopping people from mistaking 18363 for 18963 by showing them both target & current numbers
Console.WriteLine(Properties.Resources.IncompatibleBuild, Environment.OSVersion.Version.Build);
return;
}
ProcessArgs(args);
if (Debugger.IsAttached)
Console.ReadKey();
}
static void PrintHelp()
{
Console.WriteLine(Properties.Resources.Help_Commands);
}
static void ProcessArgs(string[] args)
{
var mainCmd = args[0].ToLowerInvariant();
switch (mainCmd)
{
#region Current commands
case "/query":
ArgumentBlock.Initialize(args, ArgumentBlockFlags.Store | ArgumentBlockFlags.Identifiers | ArgumentBlockFlags.Priority);
HandleQuery();
break;
case "/enable":
ArgumentBlock.Initialize(args, ArgumentBlockFlags.Store | ArgumentBlockFlags.Identifiers | ArgumentBlockFlags.FeatureConfigurationProperties | ArgumentBlockFlags.AllowBothStoresArgument);
HandleSet(RTL_FEATURE_ENABLED_STATE.Enabled);
break;
case "/disable":
ArgumentBlock.Initialize(args, ArgumentBlockFlags.Store | ArgumentBlockFlags.Identifiers | ArgumentBlockFlags.FeatureConfigurationProperties | ArgumentBlockFlags.AllowBothStoresArgument);
HandleSet(RTL_FEATURE_ENABLED_STATE.Disabled);
break;
case "/reset":
ArgumentBlock.Initialize(args, ArgumentBlockFlags.Store | ArgumentBlockFlags.Identifiers | ArgumentBlockFlags.Priority | ArgumentBlockFlags.AllowBothStoresArgument);
HandleReset();
break;
case "/fullreset":
ArgumentBlock.Initialize(args, ArgumentBlockFlags.Store | ArgumentBlockFlags.AllowBothStoresArgument | ArgumentBlockFlags.Priority);
HandleFullReset();
break;
case "/changestamp":
HandleChangeStamp();
break;
case "/querysubs":
HandleQuerySubs();
break;
case "/addsub":
ArgumentBlock.Initialize(args, ArgumentBlockFlags.Identifiers | ArgumentBlockFlags.SubscriptionProperties);
HandleSetSubs(false);
break;
case "/delsub":
ArgumentBlock.Initialize(args, ArgumentBlockFlags.Identifiers | ArgumentBlockFlags.ReportingKind | ArgumentBlockFlags.ReportingTarget);
HandleSetSubs(true);
break;
case "/notifyusage":
ArgumentBlock.Initialize(args, ArgumentBlockFlags.Identifiers | ArgumentBlockFlags.ReportingKind | ArgumentBlockFlags.ReportingOptions);
HandleNotifyUsage();
break;
case "/lkgstatus":
ArgumentBlock.Initialize(args, 0);
HandleLKGStatus();
break;
#if SET_LKG_COMMAND
case "/setlkg":
ArgumentBlock.Initialize(args, ArgumentBlockFlags.LKGStatus);
HandleSetLKG();
break;
#endif
case "/export":
ArgumentBlock.Initialize(args, ArgumentBlockFlags.Export);
HandleExport();
break;
case "/import":
ArgumentBlock.Initialize(args, ArgumentBlockFlags.Export | ArgumentBlockFlags.ImportReplace);
HandleImport();
break;
case "/fixlkg":
ArgumentBlock.Initialize(args, 0);
HandleFixLKG();
break;
case "/fixpriority":
HandleFixPriority();
break;
case "/appupdate":
HandleAppUpdate();
break;
case "/dictupdate":
HandleDictUpdate();
break;
case "/?":
case "/help":
PrintHelp();
break;
#endregion
#region Migration tips
case "queryconfig":
CommandMigrationInfoTip(mainCmd, "/query");
break;
case "addconfig":
CommandMigrationInfoTip(mainCmd, "/enable' & '/disable");
break;
case "delconfig":
CommandMigrationInfoTip(mainCmd, "/reset");
break;
case "querysubs":
CommandMigrationInfoTip(mainCmd, "/querysubs");
break;
case "addsub":
CommandMigrationInfoTip(mainCmd, "/addsub");
break;
case "delsub":
CommandMigrationInfoTip(mainCmd, "/delsub");
break;
case "notifyusage":
CommandMigrationInfoTip(mainCmd, "/notifyusage");
break;
case "changestamp":
CommandMigrationInfoTip(mainCmd, "/changestamp");
break;
#endregion
default:
ConsoleEx.WriteWarnLine(Properties.Resources.UnrecognizedCommand, mainCmd);
PrintHelp();
break;
}
}
#region Main handlers
static void HandleQuery()
{
if (ArgumentBlock.HelpMode)
{
Console.WriteLine(Properties.Resources.Help_Query);
return;
}
var priorityClamp = ArgumentBlock.FeatureConfigurationProperties.Priority;
var storeToUse = ArgumentBlock.Store.HasValue ? (RTL_FEATURE_CONFIGURATION_TYPE)ArgumentBlock.Store.Value : RTL_FEATURE_CONFIGURATION_TYPE.Runtime;
if (ArgumentBlock.IdList == null || ArgumentBlock.IdList.Count == 0)
{
var retrievedConfigs = FeatureManager.QueryAllFeatureConfigurations(storeToUse);
if (retrievedConfigs != null)
{
var namesAll = FeatureNaming.FindNamesForFeatures(retrievedConfigs.Select(x => x.FeatureId));
foreach (var config in retrievedConfigs)
{
if (priorityClamp.HasValue && config.Priority != priorityClamp.Value)
continue;
string name = null;
if (namesAll != null)
try { name = namesAll[config.FeatureId]; } catch { }
PrintFeatureConfig(config, name);
}
}
else
ConsoleEx.WriteErrorLine(Properties.Resources.QueryFailed);
return;
}
var namesSpecific = FeatureNaming.FindNamesForFeatures(ArgumentBlock.IdList);
foreach (var id in ArgumentBlock.IdList)
{
var config = FeatureManager.QueryFeatureConfiguration(id, storeToUse);
if (config != null)
{
if (priorityClamp.HasValue && config.Value.Priority != priorityClamp.Value)
continue;
string name = null;
if (namesSpecific != null)
try { name = namesSpecific[id]; } catch { }
PrintFeatureConfig(config.Value, name);
}
else
{
ConsoleEx.WriteErrorLine(Properties.Resources.SingleQueryFailed, id, storeToUse);
if (storeToUse == RTL_FEATURE_CONFIGURATION_TYPE.Boot)
ConsoleEx.WriteWarnLine(Properties.Resources.BootStoreRebootTip);
}
}
}
static void HandleSet(RTL_FEATURE_ENABLED_STATE state)
{
if (ArgumentBlock.HelpMode)
{
Console.WriteLine(Properties.Resources.Help_Set, state == RTL_FEATURE_ENABLED_STATE.Enabled ? "/enable" : "/disable");
return;
}
if (ArgumentBlock.IdList == null || ArgumentBlock.IdList.Count == 0)
{
ConsoleEx.WriteErrorLine(Properties.Resources.NoFeaturesSpecified);
return;
}
var fcp = ArgumentBlock.FeatureConfigurationProperties;
var updates = new RTL_FEATURE_CONFIGURATION_UPDATE[ArgumentBlock.IdList.Count];
try
{
for (int i = 0; i < updates.Length; i++)
{
updates[i] = new RTL_FEATURE_CONFIGURATION_UPDATE()
{
FeatureId = ArgumentBlock.IdList[i],
EnabledState = state,
EnabledStateOptions = fcp.EnabledStateOptions,
Priority = fcp.Priority ?? RTL_FEATURE_CONFIGURATION_PRIORITY.User,
Variant = fcp.Variant,
VariantPayloadKind = fcp.VariantPayloadKind,
VariantPayload = fcp.VariantPayload,
Operation = RTL_FEATURE_CONFIGURATION_OPERATION.FeatureState | RTL_FEATURE_CONFIGURATION_OPERATION.VariantState
};
}
}
catch (FeaturePropertyOverflowException fpoe) { ConsoleEx.WriteErrorLine(fpoe.Message); return; }
FinalizeSet(updates, false);
}
static void HandleReset()
{
if (ArgumentBlock.HelpMode)
{
Console.WriteLine(Properties.Resources.Help_Reset, Properties.Resources.ImmutablePropertiesInfo);
return;
}
if (ArgumentBlock.IdList == null || ArgumentBlock.IdList.Count == 0)
{
ConsoleEx.WriteErrorLine(Properties.Resources.NoFeaturesSpecified);
return;
}
RTL_FEATURE_CONFIGURATION_UPDATE[] updates;
var fcp = ArgumentBlock.FeatureConfigurationProperties;
if (fcp.Priority.HasValue)
{
var priority = fcp.Priority.Value;
updates = new RTL_FEATURE_CONFIGURATION_UPDATE[ArgumentBlock.IdList.Count];
for (int i = 0; i < updates.Length; i++)
{
updates[i] = new RTL_FEATURE_CONFIGURATION_UPDATE()
{
FeatureId = ArgumentBlock.IdList[i],
Priority = priority,
Operation = RTL_FEATURE_CONFIGURATION_OPERATION.ResetState
};
}
}
else
updates = FindResettables(false);
FinalizeSet(updates, true);
}
static void HandleFullReset()
{
if (ArgumentBlock.HelpMode)
{
Console.WriteLine(Properties.Resources.Help_FullReset, Properties.Resources.ImmutablePropertiesInfo);
return;
}
var userConsent = ConsoleEx.UserQuestion(Properties.Resources.FullResetPrompt);
if (!userConsent)
{
Console.WriteLine(Properties.Resources.FullResetCanceled);
return;
}
var updates = FindResettables(true);
FinalizeSet(updates, true);
}
static void HandleChangeStamp()
{
Console.WriteLine(Properties.Resources.ChangestampDisplay, FeatureManager.QueryFeatureConfigurationChangeStamp());
}
static void HandleQuerySubs()
{
var retrievedSubs = FeatureManager.QueryFeatureUsageSubscriptions();
if (retrievedSubs != null)
{
var names = FeatureNaming.FindNamesForFeatures(retrievedSubs.Select(x => x.FeatureId));
foreach (var sub in retrievedSubs)
{
string name = null;
if (names != null)
try { name = names[sub.FeatureId]; } catch { }
PrintSubscription(sub, name);
}
}
else
ConsoleEx.WriteErrorLine(Properties.Resources.QuerySubsFailed);
}
// No individual Store management is supported due to queries effectively being Runtime only
static void HandleSetSubs(bool delete)
{
if (ArgumentBlock.HelpMode)
{
Console.WriteLine(Properties.Resources.Help_SetSubs,
delete ? "/delsub" : "/addsub",
delete ? "" : " [/reportingoptions:<0-65535>]",
delete ? Properties.Resources.Help_SetSubs_Delete : Properties.Resources.Help_SetSubs_Add);
return;
}
var sp = ArgumentBlock.SubscriptionProperties;
if (ArgumentBlock.IdList == null || ArgumentBlock.IdList.Count == 0)
{
ConsoleEx.WriteErrorLine(Properties.Resources.NoFeaturesSpecified);
return;
}
else if (sp.ReportingTarget == 0)
{
ConsoleEx.WriteErrorLine(Properties.Resources.NoReportingTargetSpecified);
return;
}
var subs = new RTL_FEATURE_USAGE_SUBSCRIPTION_DETAILS[ArgumentBlock.IdList.Count];
for (int i = 0; i < subs.Length; i++)
{
subs[i] = new RTL_FEATURE_USAGE_SUBSCRIPTION_DETAILS()
{
FeatureId = ArgumentBlock.IdList[i],
ReportingKind = sp.ReportingKind,
ReportingOptions = sp.ReportingOptions,
ReportingTarget = sp.ReportingTarget
};
}
int result;
if (delete)
result = FeatureManager.RemoveFeatureUsageSubscriptions(subs);
else
result = FeatureManager.AddFeatureUsageSubscriptions(subs);
if (result != 0)
{
ConsoleEx.WriteErrorLine(Properties.Resources.SetSubsRuntimeFailed,
GetHumanErrorDescription(result));
return;
}
if (delete)
result = FeatureManager.RemoveFeatureUsageSubscriptionsFromRegistry(subs);
else
result = FeatureManager.AddFeatureUsageSubscriptionsToRegistry(subs);
if (result != 0)
ConsoleEx.WriteErrorLine(Properties.Resources.SetSubsBootFailed,
GetHumanErrorDescription(result, true));
else
Console.WriteLine(Properties.Resources.SetSubsSuccess);
}
static void HandleNotifyUsage()
{
if (ArgumentBlock.HelpMode)
{
Console.WriteLine(Properties.Resources.Help_NotifyUsage);
return;
}
if (ArgumentBlock.IdList == null || ArgumentBlock.IdList.Count == 0)
{
ConsoleEx.WriteErrorLine(Properties.Resources.NoFeaturesSpecified);
return;
}
var sp = ArgumentBlock.SubscriptionProperties;
foreach (var id in ArgumentBlock.IdList)
{
var report = new RTL_FEATURE_USAGE_REPORT()
{
FeatureId = id,
ReportingKind = sp.ReportingKind,
ReportingOptions = sp.ReportingOptions
};
int result = FeatureManager.NotifyFeatureUsage(ref report);
if (result != 0)
ConsoleEx.WriteErrorLine(Properties.Resources.NotifyUsageFailed,
id,
GetHumanErrorDescription(result));
else
Console.WriteLine(Properties.Resources.NotifyUsageSuccess, id);
}
}
static void HandleLKGStatus()
{
if (ArgumentBlock.HelpMode)
{
Console.WriteLine(Properties.Resources.Help_LKGStatus);
return;
}
var result = FeatureManager.GetBootFeatureConfigurationState(out BSD_FEATURE_CONFIGURATION_STATE state);
if (result != 0)
ConsoleEx.WriteErrorLine(Properties.Resources.LKGQueryFailed,
GetHumanErrorDescription(result));
else
Console.WriteLine(Properties.Resources.LKGStatusDisplay, state);
}
#if SET_LKG_COMMAND
static void HandleSetLKG()
{
UpdateLKGStatus(ArgumentBlock.LKGStatus.Value);
}
#endif
static void HandleExport()
{
if (ArgumentBlock.HelpMode)
{
Console.WriteLine(Properties.Resources.Help_Export);
return;
}
if (string.IsNullOrEmpty(ArgumentBlock.FileName))
{
ConsoleEx.WriteErrorLine(Properties.Resources.NoFileNameSpecified);
return;
}
var useBothStores = ArgumentBlock.ShouldUseBothStores;
RTL_FEATURE_CONFIGURATION[] runtimeFeatures = null;
RTL_FEATURE_CONFIGURATION[] bootFeatures = null;
if (useBothStores || ArgumentBlock.Store.Value == FeatureConfigurationTypeEx.Runtime)
runtimeFeatures = FeatureManager.QueryAllFeatureConfigurations(RTL_FEATURE_CONFIGURATION_TYPE.Runtime);
if (useBothStores || ArgumentBlock.Store.Value == FeatureConfigurationTypeEx.Boot)
bootFeatures = FeatureManager.QueryAllFeatureConfigurations(RTL_FEATURE_CONFIGURATION_TYPE.Boot);
using (var fs = new FileStream(ArgumentBlock.FileName, FileMode.Create))
using (var bw = new BinaryWriter(fs))
{
SerializeConfigsToStream(bw, runtimeFeatures);
SerializeConfigsToStream(bw, bootFeatures);
}
Console.WriteLine(Properties.Resources.ExportSuccess, runtimeFeatures?.Length ?? 0, bootFeatures?.Length ?? 0, ArgumentBlock.FileName);
}
static void HandleImport()
{
if (ArgumentBlock.HelpMode)
{
Console.WriteLine(Properties.Resources.Help_Import, Properties.Resources.ImmutablePropertiesInfo);
return;
}
if (string.IsNullOrEmpty(ArgumentBlock.FileName))
{
ConsoleEx.WriteErrorLine(Properties.Resources.NoFileNameSpecified);
return;
}
List<RTL_FEATURE_CONFIGURATION> runtimeFeatures, bootFeatures;
using (var fs = new FileStream(ArgumentBlock.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var br = new BinaryReader(fs))
{
runtimeFeatures = DeserializeConfigsFromStream(br);
bootFeatures = DeserializeConfigsFromStream(br);
}
Console.WriteLine(Properties.Resources.ImportBreakdown, runtimeFeatures.Count, bootFeatures.Count, ArgumentBlock.FileName);
if (ArgumentBlock.ImportReplace)
HandleFullReset();
var useBothStores = ArgumentBlock.ShouldUseBothStores;
if ((useBothStores || ArgumentBlock.Store.Value == FeatureConfigurationTypeEx.Runtime) && runtimeFeatures.Count > 0)
{
Console.WriteLine(Properties.Resources.ImportProcessing, runtimeFeatures.Count, FeatureConfigurationTypeEx.Runtime);
var updates = ConvertConfigsToUpdates(runtimeFeatures);
var prevValue = ArgumentBlock.Store;
ArgumentBlock.Store = FeatureConfigurationTypeEx.Runtime;
FinalizeSet(updates, false);
ArgumentBlock.Store = prevValue;
}
if ((useBothStores || ArgumentBlock.Store.Value == FeatureConfigurationTypeEx.Boot) && bootFeatures.Count > 0)
{
Console.WriteLine(Properties.Resources.ImportProcessing, runtimeFeatures.Count, FeatureConfigurationTypeEx.Boot);
var updates = ConvertConfigsToUpdates(bootFeatures);
var prevValue = ArgumentBlock.Store;
ArgumentBlock.Store = FeatureConfigurationTypeEx.Boot;
FinalizeSet(updates, false);
ArgumentBlock.Store = prevValue;
Console.WriteLine(Properties.Resources.RebootRecommended);
}
}
static void HandleFixLKG()
{
if (ArgumentBlock.HelpMode)
{
Console.WriteLine(Properties.Resources.Help_FixLKG);
return;
}
var fixPerformed = FeatureManager.FixLKGStore();
if (fixPerformed)
Console.WriteLine(Properties.Resources.FixLKGPerformed);
else
Console.WriteLine(Properties.Resources.FixLKGNotNeeded);
}
static void HandleFixPriority()
{
var success = FixPriorityInternal(RTL_FEATURE_CONFIGURATION_TYPE.Runtime);
if (!success)
return;
FixPriorityInternal(RTL_FEATURE_CONFIGURATION_TYPE.Boot);
}
static void HandleAppUpdate()
{
Console.WriteLine(Properties.Resources.CheckingAppUpdates);
var lri = UpdateCheck.GetLatestReleaseInfo();
if (lri == null || !UpdateCheck.IsAppOutdated(lri.tag_name))
Console.WriteLine(Properties.Resources.NoNewerVersionFound);
else
Console.WriteLine(Properties.Resources.NewAppUpdateDisplay, lri.name, lri.published_at, lri.body, lri.html_url);
}
static void HandleDictUpdate()
{
Console.WriteLine(Properties.Resources.CheckingDictUpdates);
var ldi = UpdateCheck.GetLatestDictionaryInfo();
if (ldi == null || !UpdateCheck.IsDictOutdated(ldi.sha))
Console.WriteLine(Properties.Resources.NoNewerVersionFound);
else
{
Console.WriteLine(Properties.Resources.NewDictUpdateDisplay, ldi.sha);
var dlNew = !File.Exists(FeatureNaming.DictFilePath) || ConsoleEx.UserQuestion(Properties.Resources.DictUpdateConsent);
if (dlNew)
{
UpdateCheck.ReplaceDict(ldi.download_url);
Console.WriteLine(Properties.Resources.DictUpdateFinished);
}
else
Console.WriteLine(Properties.Resources.DictUpdateCanceled);
}
}
#endregion
#region Helpers
static RTL_FEATURE_CONFIGURATION_UPDATE[] FindResettables(bool fullReset)
{
var useBothStores = ArgumentBlock.ShouldUseBothStores;
var dupeCheckHs = new HashSet<string>();
var updateList = new List<RTL_FEATURE_CONFIGURATION_UPDATE>();
if (useBothStores || ArgumentBlock.Store.Value == FeatureConfigurationTypeEx.Runtime)
FindResettablesInternal(RTL_FEATURE_CONFIGURATION_TYPE.Runtime, fullReset, updateList, dupeCheckHs);
if (useBothStores || ArgumentBlock.Store.Value == FeatureConfigurationTypeEx.Boot)
FindResettablesInternal(RTL_FEATURE_CONFIGURATION_TYPE.Boot, fullReset, updateList, dupeCheckHs);
return updateList.ToArray();
}
static void FindResettablesInternal(RTL_FEATURE_CONFIGURATION_TYPE type, bool fullReset, List<RTL_FEATURE_CONFIGURATION_UPDATE> targetList, HashSet<string> alreadyFoundSet)
{
var configs = FeatureManager.QueryAllFeatureConfigurations(type);
var priorityClamp = ArgumentBlock.FeatureConfigurationProperties?.Priority;
foreach (var cfg in configs)
{
if (FeatureManager.ImmutablePriorities.Contains(cfg.Priority) ||
(priorityClamp.HasValue && cfg.Priority != priorityClamp.Value))
continue;
if (fullReset || ArgumentBlock.IdList.Contains(cfg.FeatureId))
{
if (alreadyFoundSet != null)
{
var preCount = alreadyFoundSet.Count;
alreadyFoundSet.Add($"{cfg.FeatureId}-{(uint)cfg.Priority}");
if (alreadyFoundSet.Count == preCount)
continue;
}
targetList.Add(new RTL_FEATURE_CONFIGURATION_UPDATE()
{
FeatureId = cfg.FeatureId,
Priority = cfg.Priority,
Operation = RTL_FEATURE_CONFIGURATION_OPERATION.ResetState
});
}
}
}
static bool FinalizeSet(RTL_FEATURE_CONFIGURATION_UPDATE[] updates, bool isReset)
{
var useBothStores = ArgumentBlock.ShouldUseBothStores;
if (useBothStores || ArgumentBlock.Store == FeatureConfigurationTypeEx.Runtime)
{
int result;
try { result = FeatureManager.SetFeatureConfigurations(updates, RTL_FEATURE_CONFIGURATION_TYPE.Runtime); }
catch (ArgumentException ae) { ConsoleEx.WriteErrorLine(ae.Message); return false; }
if (result != 0)
{
ConsoleEx.WriteErrorLine(isReset ? Properties.Resources.ResetRuntimeFailed : Properties.Resources.SetRuntimeFailed,
GetHumanErrorDescription(result));
return false;
}
}
if (useBothStores || ArgumentBlock.Store == FeatureConfigurationTypeEx.Boot)
{
int result;
try { result = FeatureManager.SetFeatureConfigurations(updates, RTL_FEATURE_CONFIGURATION_TYPE.Boot); }
catch (ArgumentException ae) { ConsoleEx.WriteErrorLine(ae.Message); return false; }
if (result != 0)
{
ConsoleEx.WriteErrorLine(isReset ? Properties.Resources.ResetBootFailed : Properties.Resources.SetBootFailed,
GetHumanErrorDescription(result));
return false;
}
UpdateLKGStatus(BSD_FEATURE_CONFIGURATION_STATE.BootPending);
}
Console.WriteLine(isReset ? Properties.Resources.ResetSuccess : Properties.Resources.SetSuccess);
return true;
}
static void UpdateLKGStatus(BSD_FEATURE_CONFIGURATION_STATE newStatus)
{
var result = FeatureManager.GetBootFeatureConfigurationState(out BSD_FEATURE_CONFIGURATION_STATE currentStatus);
if (result != 0)
{
// STATUS_OBJECT_NAME_NOT_FOUND check
if ((uint)result == 0xC0000034)
{
result = FeatureManager.InitializeBootStatusDataFile();
if (result != 0)
{
ConsoleEx.WriteWarnLine(Properties.Resources.BootStatInitFailed,
GetHumanErrorDescription(result));
return;
}
}
else
{
ConsoleEx.WriteWarnLine(Properties.Resources.LKGQueryFailed,
GetHumanErrorDescription(result));
}
}
if (currentStatus != newStatus)
{
result = FeatureManager.SetBootFeatureConfigurationState(newStatus);
if (result != 0)
ConsoleEx.WriteWarnLine(Properties.Resources.LKGUpdateFailed,
GetHumanErrorDescription(result),
currentStatus);
}
}
static void SerializeConfigsToStream(BinaryWriter bw, RTL_FEATURE_CONFIGURATION[] configurations)
{
if (configurations != null)
{
bw.Write(configurations.Length);
foreach (var feature in configurations)
{
bw.Write(feature.FeatureId);
bw.Write(feature.CompactState);
bw.Write(feature.VariantPayload);
}
}
else
bw.Write(0);
}
static List<RTL_FEATURE_CONFIGURATION> DeserializeConfigsFromStream(BinaryReader br)
{
var count = br.ReadInt32();
var configurations = new List<RTL_FEATURE_CONFIGURATION>();
for (int i = 0; i < count; i++)
{
var config = new RTL_FEATURE_CONFIGURATION
{
FeatureId = br.ReadUInt32(),
CompactState = br.ReadUInt32(),
VariantPayload = br.ReadUInt32()
};
// Ignore these as they can't be written to the Runtime store and should be purely CBS managed
if (FeatureManager.ImmutablePriorities.Contains(config.Priority))
continue;
configurations.Add(config);
}
return configurations;
}
static RTL_FEATURE_CONFIGURATION_UPDATE[] ConvertConfigsToUpdates(List<RTL_FEATURE_CONFIGURATION> configurations)
{
var updates = new RTL_FEATURE_CONFIGURATION_UPDATE[configurations.Count];
for (int i = 0; i < updates.Length; i++)
{
var config = configurations[i];
var update = new RTL_FEATURE_CONFIGURATION_UPDATE()
{
FeatureId = config.FeatureId,
Priority = config.Priority,
EnabledState = config.EnabledState,
EnabledStateOptions = config.IsWexpConfiguration ? RTL_FEATURE_ENABLED_STATE_OPTIONS.WexpConfig : RTL_FEATURE_ENABLED_STATE_OPTIONS.None,
Variant = config.Variant,
VariantPayloadKind = config.VariantPayloadKind,
VariantPayload = config.VariantPayload,
Operation = RTL_FEATURE_CONFIGURATION_OPERATION.FeatureState | RTL_FEATURE_CONFIGURATION_OPERATION.VariantState
};
updates[i] = update;
}
return updates;
}
static bool FixPriorityInternal(RTL_FEATURE_CONFIGURATION_TYPE configurationType)
{
Console.WriteLine(Properties.Resources.FixPriorityProcessing, configurationType);
var features = FeatureManager.QueryAllFeatureConfigurations(configurationType);
var fixUpdates = MakePriorityFixUpdates(features);
if (fixUpdates == null)
{
Console.WriteLine(Properties.Resources.FixPriorityNotNeeded);
return true;
}
ArgumentBlock.Store = (FeatureConfigurationTypeEx)configurationType;
var success = FinalizeSet(fixUpdates, false);
if (configurationType == RTL_FEATURE_CONFIGURATION_TYPE.Boot)
Console.WriteLine(Properties.Resources.RebootRecommended);
return success;
}
static RTL_FEATURE_CONFIGURATION_UPDATE[] MakePriorityFixUpdates(RTL_FEATURE_CONFIGURATION[] configurations)
{
var configsToFix = configurations.Where(x => x.Priority == RTL_FEATURE_CONFIGURATION_PRIORITY.Service && !x.IsWexpConfiguration);
if (!configsToFix.Any())
return null;
var priorityFixUpdates = new RTL_FEATURE_CONFIGURATION_UPDATE[configsToFix.Count() * 2];
var updatesCreated = 0;
foreach (var cfg in configsToFix)
{
priorityFixUpdates[updatesCreated] = new RTL_FEATURE_CONFIGURATION_UPDATE()
{
FeatureId = cfg.FeatureId,
Priority = cfg.Priority,
Operation = RTL_FEATURE_CONFIGURATION_OPERATION.ResetState
};
priorityFixUpdates[updatesCreated + 1] = new RTL_FEATURE_CONFIGURATION_UPDATE()
{
FeatureId = cfg.FeatureId,
Priority = RTL_FEATURE_CONFIGURATION_PRIORITY.User,
EnabledState = cfg.EnabledState,
Variant = cfg.Variant,
VariantPayloadKind = cfg.VariantPayloadKind,
VariantPayload = cfg.VariantPayload,
Operation = RTL_FEATURE_CONFIGURATION_OPERATION.FeatureState | RTL_FEATURE_CONFIGURATION_OPERATION.VariantState
};
updatesCreated += 2;
}
return priorityFixUpdates;
}
static void CommandMigrationInfoTip(string oldCommand, string newCommand)
{
ConsoleEx.WriteWarnLine(Properties.Resources.CommandMigrationNote, oldCommand, newCommand);
}
static string GetHumanErrorDescription(int ntStatus, bool noTranslate = false)
{
var hResult = 0;
if (!noTranslate)
hResult = NativeMethods.RtlNtStatusToDosError(ntStatus);
if (noTranslate || hResult == 0x13D) //ERROR_MR_MID_NOT_FOUND
hResult = ntStatus;
var w32ex = new Win32Exception(hResult);
return w32ex.Message;
}
#endregion
#region Console printing
static void PrintFeatureConfig(RTL_FEATURE_CONFIGURATION config, string name = null)
{
var defaultFg = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("[{0}]", config.FeatureId);
if (!string.IsNullOrEmpty(name))
Console.Write(" ({0})", name);
Console.WriteLine();
Console.ForegroundColor = defaultFg;
if (Enum.IsDefined(typeof(RTL_FEATURE_CONFIGURATION_PRIORITY), config.Priority))
Console.WriteLine(Properties.Resources.FeatureDisplay_Priority + " ({1})", config.Priority, (uint)config.Priority);
else
Console.WriteLine(Properties.Resources.FeatureDisplay_Priority, config.Priority, (uint)config.Priority);
Console.WriteLine(Properties.Resources.FeatureDisplay_State, config.EnabledState, (uint)config.EnabledState);
Console.WriteLine(Properties.Resources.FeatureDisplay_Type,
config.IsWexpConfiguration ? Properties.Resources.FeatureType_Experiment : Properties.Resources.FeatureType_Override,
config.IsWexpConfiguration ? 1 : 0);
if (config.HasSubscriptions)
Console.WriteLine(Properties.Resources.FeatureDisplay_HasSubscriptions, config.HasSubscriptions);
if (config.Variant != 0)
Console.WriteLine(Properties.Resources.FeatureDisplay_Variant, config.Variant);
var vpkDefined = config.VariantPayloadKind != 0;
if (vpkDefined)
Console.WriteLine(Properties.Resources.FeatureDisplay_PayloadKind, config.VariantPayloadKind, (uint)config.VariantPayloadKind);
if (vpkDefined || config.VariantPayload != 0)
Console.WriteLine(Properties.Resources.FeatureDisplay_Payload, config.VariantPayload);
Console.WriteLine();
}
static void PrintSubscription(RTL_FEATURE_USAGE_SUBSCRIPTION_DETAILS sub, string name = null)
{
var defaultFg = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("[{0}]", sub.FeatureId);
if (!string.IsNullOrEmpty(name))
Console.Write(" ({0})", name);
Console.WriteLine();
Console.ForegroundColor = defaultFg;
Console.WriteLine(Properties.Resources.SubscriptionDisplay_ReportingKind, sub.ReportingKind);
Console.WriteLine(Properties.Resources.SubscriptionDisplay_ReportingOptions, sub.ReportingOptions);
Console.WriteLine(Properties.Resources.SubscriptionDisplay_ReportingTarget, sub.ReportingTarget);
Console.WriteLine();
}
#endregion
}
}