-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
188 lines (143 loc) · 6.33 KB
/
MainWindow.xaml.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Win32;
using SettingsManager.Converters;
using SettingsManager.Settings;
namespace SettingsManager;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow {
public MainWindow() {
if (RegistryUtils.Vint == null && RegistryUtils.RTX == null) {
MessageBox.Show(Localization.GetString("NoGameInstallationFound"), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Application.Current.Shutdown();
return;
}
InitializeComponent();
if (Environment.GetCommandLineArgs().Contains("show-all")) {
ItemCollection items = Tabs.Items;
SettingsGroupTab tab = new() { Group = SettingsGroup.Other };
LocalizationProvider.SetPath(tab, "OtherGroup");
items.Insert(items.Count - 1, tab);
}
MessageBoxResult result = MessageBox.Show(Localization.GetString("BackupWarning"),
"Warning",
MessageBoxButton.YesNo,
MessageBoxImage.Exclamation,
MessageBoxResult.Yes);
if (result == MessageBoxResult.Yes)
ShowSaveBackupDialog();
CreateSettings();
}
void CreateSettings() {
SettingDescription[] descriptions = Json.Deserialize<SettingDescription[]>(ResourcesUtils.GetStream("Settings/descriptions.json")!, true)!;
SettingsGroupTab[] settingsTabs = Tabs.Items.OfType<SettingsGroupTab>().ToArray();
List<Setting> collected = new(descriptions.Length);
foreach (SettingDescription description in descriptions) {
object? vintValue = RegistryUtils.Vint?.GetValue(description.NameWithHash);
object? rtxValue = RegistryUtils.RTX?.GetValue(description.NameWithHash);
Setting setting = new(description.FriendlyName,
description.NameWithoutHash,
description.NameWithHash,
vintValue,
rtxValue,
description.ValueConverter,
description.Tip);
settingsTabs.First(tab => tab.Group == description.Group).AddSetting(setting);
collected.Add(setting);
}
SettingsGroupTab? otherTab = settingsTabs.FirstOrDefault(tab => tab.Group == SettingsGroup.Other);
string[] vintNames = RegistryUtils.Vint?.GetValueNames() ?? [];
string[] rtxNames = RegistryUtils.RTX?.GetValueNames() ?? [];
string[] valueNames = vintNames
.Concat(rtxNames)
.Except(collected.Select(setting => setting.RegistryKey))
.OrderBy(x => x)
.ToArray();
foreach (string name in valueNames) {
string nameWithoutHash = name.Substring(0, name.IndexOf("_h", StringComparison.Ordinal));
object? vintValue = RegistryUtils.Vint?.GetValue(name);
object? rtxValue = RegistryUtils.RTX?.GetValue(name);
Setting setting = new(nameWithoutHash, nameWithoutHash, name, vintValue, rtxValue, new SimpleValueConverter());
otherTab?.AddSetting(setting);
collected.Add(setting);
}
foreach (SettingDescription description in descriptions) {
Setting parent = collected.First(setting => setting.RegistryKeyWithoutHash == description.NameWithoutHash);
Setting[] dependencies = description.Dependencies
.Select(name => collected.First(setting => setting.RegistryKeyWithoutHash == name))
.ToArray();
parent.InitDependencies(dependencies);
}
}
static void ShowSaveBackupDialog() {
SaveFileDialog backupDialog = new() {
FileName = "Backup.reg",
Filter = $"{Localization.GetString("RegistryFilesFilter")}|*.reg",
AddExtension = true,
DefaultExt = "reg",
OverwritePrompt = true
};
backupDialog.FileOk += BackupDialogSubmitted;
backupDialog.ShowDialog();
}
static void BackupDialogSubmitted(object sender, CancelEventArgs e) {
SaveFileDialog dialog = (SaveFileDialog)sender;
Stream? vintBackup = PrepareBackup(RegistryUtils.Vint?.Export());
Stream? rtxBackup = PrepareBackup(RegistryUtils.RTX?.Export());
Stream source;
if (vintBackup != null && rtxBackup != null) {
source = MergeBackups(vintBackup, rtxBackup);
vintBackup.Dispose();
rtxBackup.Dispose();
} else if (vintBackup != null)
source = vintBackup;
else if (rtxBackup != null)
source = rtxBackup;
else
throw new InvalidOperationException();
using Stream output = dialog.OpenFile();
source.CopyTo(output);
source.Dispose();
}
static Stream? PrepareBackup(Stream? stream) {
if (stream == null)
return null;
List<string> content = ReadAllLines(stream).ToList();
string partition = content[2];
if (!partition.StartsWith("[") || !partition.EndsWith("]"))
throw new ArgumentException();
partition = partition.Insert(1, "-");
content.Insert(2, partition);
return new MemoryStream(Encoding.Default.GetBytes(string.Join(Environment.NewLine, content)));
}
static Stream MergeBackups(Stream first, Stream second) {
string[] firstContent = ReadAllLines(first);
string[] secondContent = ReadAllLines(second);
string result = string.Join(Environment.NewLine, firstContent.Concat(secondContent.Skip(1)));
return new MemoryStream(Encoding.Default.GetBytes(result));
}
static string[] ReadAllLines(Stream stream) {
List<string> stringList = [];
using (StreamReader streamReader = new(stream)) {
while (streamReader.ReadLine() is { } str)
stringList.Add(str);
}
return stringList.ToArray();
}
protected override void OnSourceInitialized(EventArgs e) {
base.OnSourceInitialized(e);
IconHelper.RemoveIcon(this);
}
void Hyperlink_OnRequestNavigate(object sender, RequestNavigateEventArgs e) =>
Process.Start(e.Uri.ToString());
}