-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
442 lines (393 loc) · 15.7 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
442 lines (393 loc) · 15.7 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
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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;
using Microsoft.Win32;
using SC4ModMigrationAssistant.Models;
using SC4ModMigrationAssistant.Services;
namespace SC4ModMigrationAssistant;
public partial class MainWindow : Window
{
private readonly DbpfScanService _scanService = new();
private readonly DuplicateMoverService _moverService = new();
private readonly Sc4pacLookupService _catalogLookupService;
private readonly ObservableCollection<LogEntryView> _logEntries = new();
private readonly ObservableCollection<Sc4pacMatch> _catalogMatches = new();
private readonly ConcurrentQueue<LogMessage> _pendingLog = new();
private readonly DispatcherTimer _logFlushTimer;
// Synthwave neon palette used for log lines, matching the brushes defined in MainWindow.xaml.
private static readonly Brush BrushPlugins = new SolidColorBrush(Color.FromRgb(0xF1, 0xEA, 0xFB)); // soft white/lavender
private static readonly Brush BrushOverride = new SolidColorBrush(Color.FromRgb(0x22, 0xD3, 0xEE)); // neon cyan
private static readonly Brush BrushDuplicate = new SolidColorBrush(Color.FromRgb(0xFF, 0x3C, 0xAC)); // neon pink
private static readonly Brush BrushWarning = new SolidColorBrush(Color.FromRgb(0xFF, 0xB8, 0x6C)); // neon amber
private static readonly Brush BrushStatus = new SolidColorBrush(Color.FromRgb(0x9B, 0x8F, 0xC0)); // muted lavender-gray
private string? _pluginsRoot;
private ScanResult? _lastScanResult;
private CancellationTokenSource? _cts;
public MainWindow()
{
InitializeComponent();
_catalogLookupService = new Sc4pacLookupService(_scanService);
LogListBox.ItemsSource = _logEntries;
CatalogResultsListBox.ItemsSource = _catalogMatches;
TryEnableDarkTitleBar();
// Log lines are enqueued from the background scan thread (cheap, thread-safe, no UI
// marshaling) and flushed to the UI in small batches on a timer. This is what keeps
// the window responsive even with tens of thousands of files: without batching, a
// WPF UI update per file is what causes the "freezing" during scans.
_logFlushTimer = new DispatcherTimer(DispatcherPriority.Background)
{
Interval = TimeSpan.FromMilliseconds(150)
};
_logFlushTimer.Tick += (_, _) => FlushLogQueue();
_logFlushTimer.Start();
}
private void BtnBrowse_Click(object sender, RoutedEventArgs e)
{
var dialog = new OpenFolderDialog
{
Title = "Select the Plugins folder"
};
if (dialog.ShowDialog() == true)
{
_pluginsRoot = dialog.FolderName;
TxtPluginsPath.Text = _pluginsRoot;
BtnScan.IsEnabled = true;
BtnMoveDuplicates.IsEnabled = false;
BtnCheckCatalog.IsEnabled = true;
_lastScanResult = null;
_logEntries.Clear();
_catalogMatches.Clear();
CatalogResultsPanel.Visibility = Visibility.Collapsed;
ProgressBarScan.Value = 0;
TxtProgressCount.Text = string.Empty;
ResetCompareProgress();
}
}
private async void BtnScan_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(_pluginsRoot))
{
MessageBox.Show(this, "Please select the Plugins folder first.", "Warning",
MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
_logEntries.Clear();
ProgressBarScan.IsIndeterminate = true;
ProgressBarScan.Value = 0;
TxtProgressCount.Text = string.Empty;
ResetCompareProgress();
_cts = new CancellationTokenSource();
SetBusy(true, "Scanning...");
Action<LogMessage> log = msg => _pendingLog.Enqueue(msg);
var scanProgress = new Progress<ScanProgress>(OnScanProgress);
var compareProgress = new Progress<ScanProgress>(OnCompareProgress);
try
{
ScanResult result = await Task.Run(() =>
_scanService.ScanPlugins(_pluginsRoot!, log, scanProgress, compareProgress, _cts.Token), _cts.Token);
FlushLogQueue();
_lastScanResult = result;
BtnMoveDuplicates.IsEnabled = result.Duplicates.Count > 0;
TxtStatus.Text = $"Files read: {result.TotalFilesScanned} - Duplicates found: {result.Duplicates.Count}";
}
catch (OperationCanceledException)
{
FlushLogQueue();
AppendLogImmediate(new LogMessage("Scan cancelled by the user.", LogColor.Orange));
TxtStatus.Text = "Scan cancelled.";
}
catch (Exception ex)
{
FlushLogQueue();
AppendLogImmediate(new LogMessage($"Error during scan: {ex.Message}", LogColor.Orange));
MessageBox.Show(this, ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
ProgressBarScan.IsIndeterminate = false;
CompareProgressPanel.Visibility = Visibility.Collapsed;
SetBusy(false, TxtStatus.Text);
_cts?.Dispose();
_cts = null;
}
}
private async void BtnMoveDuplicates_Click(object sender, RoutedEventArgs e)
{
if (_lastScanResult == null || _lastScanResult.Duplicates.Count == 0)
{
MessageBox.Show(this, "No duplicates to move. Run a scan first.", "Warning",
MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
var dialog = new OpenFolderDialog
{
Title = "Select (or create) the folder to move duplicates to"
};
if (dialog.ShowDialog() != true)
{
return;
}
string destination = dialog.FolderName;
var confirm = MessageBox.Show(this,
$"{_lastScanResult.Duplicates.Count} duplicate file(s) will be moved to:\n{destination}\n\nContinue?",
"Confirm move", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (confirm != MessageBoxResult.Yes)
{
return;
}
_cts = new CancellationTokenSource();
SetBusy(true, "Moving duplicates...");
Action<LogMessage> log = msg => _pendingLog.Enqueue(msg);
List<ScannedFile> duplicates = _lastScanResult.Duplicates;
try
{
await Task.Run(() => _moverService.MoveDuplicates(duplicates, destination, log));
FlushLogQueue();
TxtStatus.Text = $"Move complete: {duplicates.Count} file(s) moved to {destination}";
BtnMoveDuplicates.IsEnabled = false;
}
catch (Exception ex)
{
FlushLogQueue();
AppendLogImmediate(new LogMessage($"Error while moving files: {ex.Message}", LogColor.Orange));
MessageBox.Show(this, ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
SetBusy(false, TxtStatus.Text);
_cts?.Dispose();
_cts = null;
}
}
private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
_cts?.Cancel();
BtnCancel.IsEnabled = false;
}
private async void BtnCheckCatalog_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(_pluginsRoot))
{
MessageBox.Show(this, "Please select the Plugins folder first.", "Warning",
MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
_logEntries.Clear();
_catalogMatches.Clear();
CatalogResultsPanel.Visibility = Visibility.Collapsed;
ProgressBarScan.IsIndeterminate = true;
ProgressBarScan.Value = 0;
TxtProgressCount.Text = string.Empty;
ResetCompareProgress();
_cts = new CancellationTokenSource();
SetBusy(true, "Checking sc4pac catalog...");
Action<LogMessage> log = msg => _pendingLog.Enqueue(msg);
var scanProgress = new Progress<ScanProgress>(OnScanProgress);
try
{
List<Sc4pacMatch> matches = await _catalogLookupService.CheckCatalogAsync(_pluginsRoot!, log, scanProgress, _cts.Token);
FlushLogQueue();
foreach (Sc4pacMatch match in matches)
{
_catalogMatches.Add(match);
}
CatalogResultsPanel.Visibility = matches.Count > 0 ? Visibility.Visible : Visibility.Collapsed;
TxtStatus.Text = matches.Count > 0
? $"{matches.Count} sc4pac package(s) found for your override files."
: "No matching sc4pac packages found.";
}
catch (OperationCanceledException)
{
FlushLogQueue();
AppendLogImmediate(new LogMessage("Catalog check cancelled by the user.", LogColor.Orange));
TxtStatus.Text = "Catalog check cancelled.";
}
catch (Exception ex)
{
FlushLogQueue();
AppendLogImmediate(new LogMessage($"Error during catalog check: {ex.Message}", LogColor.Orange));
MessageBox.Show(this, ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
ProgressBarScan.IsIndeterminate = false;
SetBusy(false, TxtStatus.Text);
_cts?.Dispose();
_cts = null;
}
}
private void BtnCopyPackageId_Click(object sender, RoutedEventArgs e)
{
if (sender is Button { Tag: Sc4pacMatch match })
{
TryCopyToClipboard(match.PackageId);
TxtStatus.Text = $"Copied package ID: {match.PackageId}";
}
}
private void BtnCopyAddCommand_Click(object sender, RoutedEventArgs e)
{
if (sender is Button { Tag: Sc4pacMatch match })
{
TryCopyToClipboard(match.Sc4pacAddCommand);
TxtStatus.Text = $"Copied: {match.Sc4pacAddCommand}";
}
}
private void BtnOpenPage_Click(object sender, RoutedEventArgs e)
{
if (sender is not Button { Tag: Sc4pacMatch { PageUrl: { Length: > 0 } url } })
{
return;
}
try
{
// UseShellExecute=true opens the URL with the OS's default browser, same as
// double-clicking a link - this project never launches a specific browser directly.
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
}
catch (Exception ex)
{
MessageBox.Show(this, $"Could not open the page:\n{ex.Message}", "Error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
/// <summary>
/// Clipboard.SetText can intermittently throw COMException if another process briefly holds
/// the clipboard (common on Windows) - retrying once after a short delay, as recommended
/// practice, instead of letting a transient failure surface as an error to the user.
/// </summary>
private static void TryCopyToClipboard(string text)
{
try
{
Clipboard.SetText(text);
}
catch (Exception)
{
try
{
System.Threading.Thread.Sleep(100);
Clipboard.SetText(text);
}
catch
{
// Give up silently - copying to the clipboard is a convenience, not essential.
}
}
}
private void OnScanProgress(ScanProgress p)
{
ProgressBarScan.IsIndeterminate = false;
ProgressBarScan.Maximum = Math.Max(p.Total, 1);
ProgressBarScan.Value = p.Processed;
TxtProgressCount.Text = p.Total > 0 ? $"{p.Processed} / {p.Total} files" : string.Empty;
}
/// <summary>
/// Shows/updates the dedicated comparison progress bar. It only becomes visible once the
/// comparison phase actually starts, and is hidden again as soon as a new scan begins.
/// </summary>
private void OnCompareProgress(ScanProgress p)
{
if (CompareProgressPanel.Visibility != Visibility.Visible)
{
CompareProgressPanel.Visibility = Visibility.Visible;
}
ProgressBarCompare.Maximum = Math.Max(p.Total, 1);
ProgressBarCompare.Value = p.Processed;
TxtCompareCount.Text = p.Total > 0 ? $"Analyzing duplicates: {p.Processed} / {p.Total}" : "Analyzing duplicates";
}
private void ResetCompareProgress()
{
CompareProgressPanel.Visibility = Visibility.Collapsed;
ProgressBarCompare.Value = 0;
TxtCompareCount.Text = string.Empty;
}
/// <summary>
/// Drains all currently queued log messages and adds them to the bound collection in one
/// batch. Runs on the UI thread (called from the DispatcherTimer tick or right after an
/// awaited background task completes).
/// </summary>
private void FlushLogQueue()
{
if (_pendingLog.IsEmpty)
{
return;
}
while (_pendingLog.TryDequeue(out LogMessage? msg))
{
_logEntries.Add(ToView(msg));
}
if (_logEntries.Count > 0)
{
LogListBox.ScrollIntoView(_logEntries[^1]);
}
}
private void AppendLogImmediate(LogMessage message)
{
_logEntries.Add(ToView(message));
if (_logEntries.Count > 0)
{
LogListBox.ScrollIntoView(_logEntries[^1]);
}
}
private static LogEntryView ToView(LogMessage message)
{
Brush brush = message.Color switch
{
LogColor.Black => BrushPlugins,
LogColor.Blue => BrushOverride,
LogColor.Red => BrushDuplicate,
LogColor.Orange => BrushWarning,
LogColor.Gray => BrushStatus,
_ => BrushPlugins
};
return new LogEntryView { Text = message.Text, Brush = brush };
}
private void SetBusy(bool busy, string status)
{
BtnBrowse.IsEnabled = !busy;
BtnScan.IsEnabled = !busy && !string.IsNullOrWhiteSpace(_pluginsRoot);
BtnMoveDuplicates.IsEnabled = !busy && _lastScanResult is { } r && r.Duplicates.Count > 0;
BtnCheckCatalog.IsEnabled = !busy && !string.IsNullOrWhiteSpace(_pluginsRoot);
BtnCancel.IsEnabled = busy;
TxtStatus.Text = status;
Mouse.OverrideCursor = busy ? Cursors.Arrow : null;
}
/// <summary>
/// Best-effort attempt to make the native window title bar follow the dark theme too
/// (Windows 10 2004+ / Windows 11). Silently does nothing on older systems.
/// </summary>
private void TryEnableDarkTitleBar()
{
SourceInitialized += (_, _) =>
{
try
{
var hwnd = new WindowInteropHelper(this).Handle;
int useImmersiveDarkMode = 1;
// 20 = DWMWA_USE_IMMERSIVE_DARK_MODE (Windows 10 20H1+ / 11). Falls back to the
// older attribute id (19) used on early Windows 10 20H1 builds if that fails.
if (DwmSetWindowAttribute(hwnd, 20, ref useImmersiveDarkMode, sizeof(int)) != 0)
{
DwmSetWindowAttribute(hwnd, 19, ref useImmersiveDarkMode, sizeof(int));
}
}
catch
{
// Not critical - the rest of the UI is already themed regardless of the title bar.
}
};
}
[DllImport("dwmapi.dll", PreserveSig = true)]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attribute, ref int value, int size);
}