-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainWindow.xaml.cs
497 lines (445 loc) · 19.5 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
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
// <copyright file="MainWindow.xaml.cs" company="nocorp">
// Copyright (c) realies. No rights reserved.
// </copyright>
namespace ChipsetAutoUpdater
{
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using Microsoft.Win32;
using Microsoft.Win32.TaskScheduler;
using Application = System.Windows.Application;
using Cursors = System.Windows.Input.Cursors;
using MessageBox = System.Windows.MessageBox;
using SystemTask = System.Threading.Tasks.Task;
using TaskSchedulerTask = Microsoft.Win32.TaskScheduler.Task;
/// <summary>
/// Interaction logic for MainWindow.xaml.
/// </summary>
public partial class MainWindow : Window
{
private const string TaskName = "ChipsetAutoUpdaterAutoStart";
private const int UpdateCheckIntervalHours = 6;
private NotifyIcon notifyIcon;
private string detectedChipsetString;
private string installedVersionString;
private string latestVersionReleasePageUrl;
private string latestVersionDownloadFileUrl;
private string latestVersionString;
private CancellationTokenSource cancellationTokenSource;
private HttpClient client;
private bool isInitializing = true;
private System.Windows.Threading.DispatcherTimer updateTimer;
private bool autoUpdateEnabled = false;
/// <summary>
/// Initializes a new instance of the <see cref="MainWindow"/> class.
/// </summary>
public MainWindow()
{
this.InitializeComponent();
this.InitializeNotifyIcon();
this.StateChanged += this.MainWindow_StateChanged;
this.Loaded += this.Window_Loaded;
string[] args = Environment.GetCommandLineArgs();
if (args.Contains("/min"))
{
this.WindowState = WindowState.Minimized;
this.Hide();
}
if (args.Contains("/autoupdate"))
{
this.autoUpdateEnabled = true;
}
this.Title = "CAU " + (Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false).OfType<AssemblyFileVersionAttribute>().FirstOrDefault()?.Version?.TrimEnd(".0".ToCharArray()) + " " ?? " ");
this.client = new HttpClient();
this.client.Timeout = TimeSpan.FromSeconds(1);
this.client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; AcmeInc/1.0)");
this.client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));
this.client.DefaultRequestHeaders.Referrer = new Uri("https://www.amd.com/");
this.InitializeUpdateTimer();
_ = this.RenderView();
}
/// <summary>
/// Performs cleanup operations when the window is closed.
/// Disposes of the notify icon and calls the base OnClosed method.
/// </summary>
/// <param name="e">A System.EventArgs that contains the event data.</param>
protected override void OnClosed(EventArgs e)
{
this.notifyIcon.Dispose();
base.OnClosed(e);
}
private void InitializeUpdateTimer()
{
this.updateTimer = new System.Windows.Threading.DispatcherTimer();
this.updateTimer.Tick += async (sender, e) => await this.CheckForUpdates();
this.updateTimer.Interval = TimeSpan.FromHours(UpdateCheckIntervalHours);
this.updateTimer.Start();
}
private async SystemTask CheckForUpdates()
{
await this.RenderView();
if (this.latestVersionString != null && this.latestVersionString != this.installedVersionString)
{
this.Show();
this.WindowState = WindowState.Normal;
this.Activate();
if (this.autoUpdateEnabled)
{
this.InstallDrivers_Click(this, new RoutedEventArgs());
}
}
}
private void AutoStartCheckBox_Checked(object sender, RoutedEventArgs e)
{
if (this.isInitializing)
{
return;
}
if (this.AutoStartCheckBox.IsChecked == true)
{
this.CreateStartupTask();
this.AutoUpdateCheckBox.IsEnabled = true;
}
else
{
this.RemoveStartupTask();
this.AutoUpdateCheckBox.IsChecked = false;
this.AutoUpdateCheckBox.IsEnabled = false;
this.autoUpdateEnabled = false;
}
}
private void AutoUpdateCheckBox_Checked(object sender, RoutedEventArgs e)
{
if (this.isInitializing)
{
return;
}
this.autoUpdateEnabled = this.AutoUpdateCheckBox.IsChecked == true;
this.CreateStartupTask();
}
private void CreateStartupTask()
{
using (TaskService ts = new TaskService())
{
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Start ChipsetAutoUpdater at system startup";
td.Triggers.Add(new LogonTrigger());
string arguments = "/min";
if (this.AutoUpdateCheckBox.IsChecked == true)
{
arguments += " /autoupdate";
}
td.Actions.Add(new ExecAction(Assembly.GetExecutingAssembly().Location, arguments));
td.Principal.RunLevel = TaskRunLevel.Highest;
ts.RootFolder.RegisterTaskDefinition(TaskName, td);
}
}
private void RemoveStartupTask()
{
using (TaskService ts = new TaskService())
{
ts.RootFolder.DeleteTask(TaskName, false);
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
using (TaskService ts = new TaskService())
{
TaskSchedulerTask task = ts.GetTask(TaskName);
this.AutoStartCheckBox.IsChecked = task != null;
this.AutoUpdateCheckBox.IsChecked = task != null && task.Definition.Actions.OfType<ExecAction>().Any(a => a.Arguments.Contains("/autoupdate"));
}
this.AutoUpdateCheckBox.IsEnabled = this.AutoStartCheckBox.IsChecked == true;
this.autoUpdateEnabled = this.AutoUpdateCheckBox.IsChecked == true;
this.isInitializing = false;
}
private void InitializeNotifyIcon()
{
this.notifyIcon = new NotifyIcon();
this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
this.notifyIcon.Visible = true;
this.notifyIcon.Text = "Chipset Auto Updater";
// Create context menu
ContextMenuStrip contextMenu = new ContextMenuStrip();
contextMenu.Items.Add("Open", null, this.OnOpenClick);
contextMenu.Items.Add("Exit", null, this.OnExitClick);
this.notifyIcon.ContextMenuStrip = contextMenu;
this.notifyIcon.DoubleClick += this.NotifyIcon_DoubleClick;
}
private async void MainWindow_StateChanged(object sender, EventArgs e)
{
if (this.WindowState == WindowState.Minimized)
{
this.Hide();
}
else if (this.WindowState == WindowState.Normal)
{
await this.CheckForUpdates();
}
}
private void NotifyIcon_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.WindowState = WindowState.Normal;
}
private void OnOpenClick(object sender, EventArgs e)
{
this.Show();
this.WindowState = WindowState.Normal;
}
private void OnExitClick(object sender, EventArgs e)
{
Application.Current.Shutdown();
}
private async SystemTask RenderView()
{
this.detectedChipsetString = this.ChipsetModelMatcher();
this.ChipsetModelText.Text = this.detectedChipsetString ?? "Not Detected";
this.SetInstalledVersion();
if (this.detectedChipsetString != null)
{
var versionData = await this.FetchLatestVersionData(this.detectedChipsetString);
this.latestVersionReleasePageUrl = versionData.Item1;
this.latestVersionDownloadFileUrl = versionData.Item2;
this.latestVersionString = versionData.Item3;
}
if (this.latestVersionString != null)
{
this.LatestVersionText.Text = this.latestVersionString;
this.LatestVersionText.TextDecorations = TextDecorations.Underline;
this.LatestVersionText.Cursor = Cursors.Hand;
this.LatestVersionText.MouseLeftButtonDown += (s, e) =>
{
if (this.latestVersionReleasePageUrl != null)
{
Process.Start(new ProcessStartInfo(this.latestVersionReleasePageUrl) { UseShellExecute = true });
}
};
this.InstallDriversButton.IsEnabled = true;
}
else
{
this.LatestVersionText.Text = "Error fetching";
this.LatestVersionText.TextDecorations = null;
this.LatestVersionText.Cursor = Cursors.Arrow;
this.LatestVersionText.MouseLeftButtonDown -= (s, e) => { };
this.InstallDriversButton.IsEnabled = false;
}
}
private string ChipsetModelMatcher()
{
string registryPath = @"HARDWARE\DESCRIPTION\System\BIOS";
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registryPath))
{
if (key != null)
{
string product = key.GetValue("BaseBoardProduct")?.ToString().ToUpper() ?? string.Empty;
string pattern = @"[ABX]\d{3}E?"; // Define the regex pattern to match chipset models like A/B/X followed by three digits and optional E
Match match = Regex.Match(product, pattern);
if (match.Success)
{
return match.Value;
}
}
}
return null;
}
private string GetInstalledAMDChipsetVersion()
{
string registryPath = @"Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registryPath))
{
if (key != null)
{
foreach (string subkeyName in key.GetSubKeyNames())
{
using (RegistryKey subkey = key.OpenSubKey(subkeyName))
{
if (subkey != null)
{
string publisher = subkey.GetValue("Publisher") as string;
string displayName = subkey.GetValue("DisplayName") as string;
if (publisher == "Advanced Micro Devices, Inc." && displayName == "AMD Chipset Software")
{
return subkey.GetValue("DisplayVersion") as string;
}
}
}
}
}
}
return null;
}
private async Task<Tuple<string, string, string>> FetchLatestVersionData(string chipset)
{
try
{
string driversPageUrl = "https://www.amd.com/en/support/download/drivers.html";
string driversPageContent = await this.client.GetStringAsync(driversPageUrl);
string driversPagePattern = $@"https://[^""&]+{Regex.Escape(chipset)}\.html";
Match driversUrlMatch = Regex.Match(driversPageContent, driversPagePattern, RegexOptions.IgnoreCase);
if (driversUrlMatch.Success)
{
string chipsetPageContent = await this.client.GetStringAsync(driversUrlMatch.Value);
string chipsetDownloadFileUrlPattern = $@"https://[^""]+\.exe";
Match chipsetDownloadFileUrlMatch = Regex.Match(chipsetPageContent, chipsetDownloadFileUrlPattern, RegexOptions.IgnoreCase);
string chispetReleasePageUrlPattrern = $@"/en/resources/support-articles/release-notes/.*chipset[^""]+";
Match chipsetRelasePageUrlMatch = Regex.Match(chipsetPageContent, chispetReleasePageUrlPattrern, RegexOptions.IgnoreCase);
if (chipsetDownloadFileUrlMatch.Success && chipsetRelasePageUrlMatch.Success)
{
return Tuple.Create($@"https://www.amd.com{chipsetRelasePageUrlMatch.Value}", chipsetDownloadFileUrlMatch.Value, chipsetDownloadFileUrlMatch.Value.Split('_').Last().Replace(".exe", string.Empty));
}
}
}
catch (Exception)
{
}
return Tuple.Create<string, string, string>(null, null, null);
}
private async void InstallDrivers_Click(object sender, RoutedEventArgs e)
{
string localFilePath = Path.Combine(Path.GetTempPath(), $"amd_chipset_software_{this.latestVersionString}.exe");
try
{
this.InstallDriversButton.IsEnabled = false;
this.InstallDriversButton.Visibility = Visibility.Collapsed;
this.DownloadProgressBar.Visibility = Visibility.Visible;
this.CancelButton.Visibility = Visibility.Visible;
this.cancellationTokenSource = new CancellationTokenSource();
await this.DownloadFileAsync(this.latestVersionDownloadFileUrl, localFilePath, this.cancellationTokenSource.Token);
if (this.cancellationTokenSource.Token.IsCancellationRequested)
{
return; // Do not start the process if the download was cancelled
}
this.CancelButton.IsEnabled = false;
this.CancelButton.Content = "Installing...";
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = localFilePath,
Arguments = "-INSTALL",
Verb = "runas",
UseShellExecute = true,
CreateNoWindow = true,
};
Process process = Process.Start(startInfo);
await SystemTask.Run(() => this.MonitorProcess(process));
await SystemTask.Run(() => process.WaitForExit());
}
catch (Exception ex)
{
_ = MessageBox.Show($"Error downloading or installing drivers: {ex.Message}");
}
finally
{
this.InstallDriversButton.IsEnabled = true;
this.InstallDriversButton.Visibility = Visibility.Visible;
this.DownloadProgressBar.Value = 0;
this.CancelButton.Visibility = Visibility.Collapsed;
this.CancelButton.Content = "Cancel Download";
this.CleanUpFile(localFilePath);
}
}
private async SystemTask DownloadFileAsync(string requestUri, string destinationFilePath, CancellationToken cancellationToken)
{
try
{
using (var response = await this.client.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead, cancellationToken))
{
_ = response.EnsureSuccessStatusCode();
var totalBytes = response.Content.Headers.ContentLength ?? -1L;
var canReportProgress = totalBytes != -1;
using (var contentStream = await response.Content.ReadAsStreamAsync())
using (var fileStream = new FileStream(destinationFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true))
{
var totalRead = 0L;
var buffer = new byte[8192];
var isMoreToRead = true;
do
{
var read = await contentStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
if (read == 0)
{
isMoreToRead = false; // Download completed
}
else
{
await fileStream.WriteAsync(buffer, 0, read, cancellationToken);
totalRead += read;
if (canReportProgress)
{
var progress = (int)((totalRead * 100) / totalBytes);
this.DownloadProgressBar.Value = progress;
}
}
}
while (isMoreToRead);
}
}
}
catch (TaskCanceledException)
{
if (File.Exists(destinationFilePath))
{
try
{
File.Delete(destinationFilePath);
}
catch (Exception ex)
{
_ = MessageBox.Show($"Error deleting file: {ex.Message}");
}
}
}
catch (HttpRequestException httpRequestEx)
{
_ = MessageBox.Show($"HTTP request error: {httpRequestEx.Message}");
}
catch (Exception ex)
{
_ = MessageBox.Show($"Error during download: {ex.Message}");
this.CleanUpFile(destinationFilePath);
}
}
private void CancelDownload_Click(object sender, RoutedEventArgs e)
{
this.cancellationTokenSource?.Cancel();
}
private async SystemTask MonitorProcess(Process process)
{
while (!process.HasExited)
{
await this.Dispatcher.InvokeAsync(() => this.SetInstalledVersion());
await SystemTask.Delay(1000);
}
}
private void SetInstalledVersion()
{
this.installedVersionString = this.GetInstalledAMDChipsetVersion();
this.InstalledVersionText.Text = this.installedVersionString ?? "Not Installed";
}
private void CleanUpFile(string destinationFilePath)
{
if (File.Exists(destinationFilePath))
{
try
{
File.Delete(destinationFilePath);
}
catch (Exception deleteEx)
{
_ = MessageBox.Show($"Error deleting file: {deleteEx.Message}");
}
}
}
}
}