This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.xaml.cs
655 lines (590 loc) · 25.1 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
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
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Web.WebView2.Core;
using Windows.Storage;
using Windows.System;
namespace Miiverse_PC
{
/// <summary>The main window of the app.</summary>
public sealed partial class MainWindow : Window
{
/// <summary>The default options for JSON serialization.</summary>
private static readonly JsonSerializerOptions defaultSerializerOptions = new()
{
WriteIndented = true,
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters = {
new JsonStringEnumConverter()
},
ReadCommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,
NumberHandling = JsonNumberHandling.AllowReadingFromString,
};
/// <summary>The path where account data is stored as JSON.</summary>
private readonly string accountDataJsonPath;
/// <summary>The current window's window handle.</summary>
private readonly IntPtr hwnd;
/// <summary>The JavaScript code that is run on NavigationCompleted.</summary>
private readonly string javascriptCode;
/// <summary>The path where settings data is stored as JSON.</summary>
private readonly string settingsDataJsonPath;
/// <summary>
/// The settings dialog used to display and edit account settings.
/// </summary>
private readonly SettingsDialog settingsDialog = new();
/// <summary>The currently logged-in account.</summary>
private Account? currentAccount;
/// <summary>If the WebView is navigating to a page.</summary>
private bool isWebViewNavigating = false;
/// <summary>
/// Code that runs on initialization of the <see cref="MainWindow" /> class.
/// </summary>
public MainWindow()
{
InitializeComponent();
hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
Title = "Miiverse PC Client";
javascriptCode = ReadJavascriptFile(@"js\portal.js");
// Set up and restore stored data
try
{
accountDataJsonPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "accountData.json");
}
catch (InvalidOperationException)
{
// The app is running unpackaged, so LocalFolder cannot be used
// Use the program's folder instead or fall back to the current
// working directory
var currentAssembly = System.Reflection.Assembly.GetEntryAssembly();
string? currentDirectory = Path.GetDirectoryName(currentAssembly?.Location);
accountDataJsonPath = currentDirectory is not null
? Path.Combine(currentDirectory, "accountData.json")
: Path.Combine(Environment.CurrentDirectory, "accountData.json");
}
settingsDataJsonPath = Path.Combine(Path.GetDirectoryName(accountDataJsonPath)!, "settings.json");
// Load account and settings data
_ = LoadAccountDataAsync();
_ = LoadSettingsDataAsync();
}
/// <summary>
/// Reads JavaScript code from a file path relative to the current
/// assembly and handles exceptions by returning a JavaScript alert
/// containing the exception text.
/// </summary>
/// <param name="filePath">The relative file path of the code.</param>
/// <returns>
/// A string containing the JavaScript code in the file or an alert
/// containing exception info.
/// </returns>
private static string ReadJavascriptFile(string filePath)
{
try
{
var currentAssembly = System.Reflection.Assembly.GetEntryAssembly();
string? currentDirectory = Path.GetDirectoryName(currentAssembly?.Location);
return currentDirectory is null
? $"alert(\"Failed to get the current application directory.\\n Assembly: {currentAssembly}\\n Directory: {currentDirectory}\")"
: File.ReadAllText(Path.Combine(currentDirectory, filePath));
}
catch (Exception ex)
{
// Basic string replacements that escape new lines, quotes, and
// backslashes inside the exception text to avoid syntax errors
string escapedEx = ex.ToString()
.Replace("\\", "\\\\")
.Replace(Environment.NewLine, "\\n\\n")
.Replace("'", "\\'")
.Replace("\"", "\\\"");
return $"alert(\"There was an error loading the JavaScript code at {filePath}: {escapedEx}\"); console.error(\"{escapedEx}\");";
}
}
/// <summary>Clears the stored account data.</summary>
private async Task DeleteAccountDataAsync()
{
try
{
File.Delete(accountDataJsonPath);
}
catch (Exception ex)
{
await ShowErrorDialogAsync("Failed to delete account data", ex.ToString()).ConfigureAwait(false);
}
}
/// <summary>Clears the stored settings data.</summary>
private async Task DeleteSettingsDataAsync()
{
settingsDialog.ResetSettings();
try
{
File.Delete(settingsDataJsonPath);
}
catch (Exception ex)
{
await ShowErrorDialogAsync("Failed to delete settings data", ex.ToString()).ConfigureAwait(false);
}
}
/// <summary>
/// Downloads the current account's profile data asynchronously and
/// prompts the user to save it.
/// </summary>
private async void DownloadUserProfileDataAsync(object sender, RoutedEventArgs e)
{
if (currentAccount is null || !currentAccount.IsSignedIn)
{
await ShowErrorDialogAsync("Not signed in", "You must be signed in to download user profile data.").ConfigureAwait(false);
return;
}
var savePicker = new Windows.Storage.Pickers.FileSavePicker
{
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Downloads,
SuggestedFileName = "ProfileData"
};
savePicker.FileTypeChoices.Add("XML data", new List<string>() { ".xml" });
// Initialize the file picker with the current window handle. This
// prevents the file picker from throwing a COM exception
WinRT.Interop.InitializeWithWindow.Initialize(savePicker, hwnd);
StorageFile dataFile = await savePicker.PickSaveFileAsync();
if (dataFile is not null)
{
try
{
string profileData = await currentAccount.GetUserProfileXmlAsync();
var profileXml = System.Xml.Linq.XDocument.Parse(profileData);
await FileIO.WriteTextAsync(dataFile, profileXml.ToString());
}
catch (Exception ex)
{
await ShowErrorDialogAsync("Failed to save profile data", ex.ToString()).ConfigureAwait(false);
}
}
}
/// <summary>Goes back in the WebView.</summary>
private void GoBack(object sender, RoutedEventArgs e)
{
if (webView.CanGoBack)
{
webView.GoBack();
}
}
/// <summary>Goes forward in the WebView.</summary>
private void GoForward(object sender, RoutedEventArgs e)
{
if (webView.CanGoForward)
{
webView.GoForward();
}
}
/// <summary>Handles HistoryChanged events from the WebView.</summary>
private void HistoryChangedHandler(CoreWebView2 sender, object args)
{
backButton.IsEnabled = sender.CanGoBack;
forwardButton.IsEnabled = sender.CanGoForward;
addressBar.Text = sender.Source;
}
/// <summary>Loads the stored account data asynchronously.</summary>
private async Task LoadAccountDataAsync()
{
try
{
if (!File.Exists(accountDataJsonPath))
{
// If the file does not exist, there is nothing to load
return;
}
string jsonData = await File.ReadAllTextAsync(accountDataJsonPath);
if (string.IsNullOrWhiteSpace(jsonData))
{
// Or if the file is empty
return;
}
var accountNode = JsonNode.Parse(jsonData)!;
username.Text = accountNode["username"]?.ToString();
password.Password = accountNode["passwordHash"]?.ToString();
saveLoginInfo.IsChecked = true;
}
catch (Exception ex)
{
// Need to wait until the main window is loaded before showing
// the error due to Content.XamlRoot being null
while (Content.XamlRoot is null)
{
await Task.Delay(100);
}
await ShowErrorDialogAsync
(
"Failed to load the saved account data",
"The saved data will be automatically deleted.\n\n" + ex.ToString()
).ConfigureAwait(false);
await DeleteAccountDataAsync().ConfigureAwait(false);
}
}
/// <summary>Loads the stored settings data asynchronously.</summary>
private async Task LoadSettingsDataAsync()
{
try
{
if (!File.Exists(settingsDataJsonPath))
{
// If the file does not exist, there is nothing to load
return;
}
string jsonData = await File.ReadAllTextAsync(settingsDataJsonPath);
if (string.IsNullOrWhiteSpace(jsonData))
{
// Or if the file is empty
return;
}
Settings? storedSettings = JsonSerializer.Deserialize<Settings>(jsonData, defaultSerializerOptions);
// Use default settings if deserialization returns null
storedSettings ??= new();
settingsDialog.CurrentSettings = storedSettings;
settingsDialog.LoadSettings();
}
catch (Exception ex)
{
// Need to wait until the main window is loaded before showing
// the error due to Content.XamlRoot being null
while (Content.XamlRoot is null)
{
await Task.Delay(100);
}
await ShowErrorDialogAsync
(
"Failed to load the saved settings",
"The settings will be automatically reset.\n\n" + ex.ToString()
).ConfigureAwait(false);
await DeleteSettingsDataAsync().ConfigureAwait(false);
}
}
/// <summary>Logs in as a PNID with a username and password hash.</summary>
private async void LoginAsync(object sender, RoutedEventArgs e)
{
// Check if inputs are valid
if (string.IsNullOrWhiteSpace(username.Text) || string.IsNullOrWhiteSpace(password.Password))
{
await ShowErrorDialogAsync
(
"Username or password is empty",
"Please fill out both the username and password hash text boxes."
).ConfigureAwait(false);
return;
}
// Start login process
UpdateLoginStatus(true);
// Create the account and account status string
currentAccount = new(username.Text, password.Password, settingsDialog.CurrentSettings);
string currentError = "(No error)";
string currentStatus = "Starting login process";
try
{
// Hash the password if necessary
await currentAccount.HashPnidPasswordAsync();
if (string.IsNullOrEmpty(currentAccount.PnidPasswordHash))
{
currentError = "Login failed (password hash)";
currentStatus = $"The PNID username {currentAccount.PnidUsername} does not exist on the server.";
return;
}
// Login with password hash and receive OAuth2 token
currentStatus = await currentAccount.CreateOauth2TokenAsync();
if (currentAccount.OauthTokenIsNull)
{
currentError = "Login failed (OAuth2 token)";
return;
}
// Login with OAuth2 token and receive Miiverse service token
currentStatus = await currentAccount.CreateMiiverseTokenAsync();
if (string.IsNullOrEmpty(currentAccount.MiiverseToken))
{
currentError = "Login failed (Miiverse service token)";
return;
}
// Login with the Miiverse token and receive portal server
currentStatus = await currentAccount.GetMiiversePortalServerAsync();
if (string.IsNullOrEmpty(currentAccount.MiiversePortalServer))
{
currentError = "Login failed (Miiverse portal server)";
return;
}
// Create the ParamPack data using the selected language and country
currentAccount.CreateParamPack();
// At this point, account login has succeeded
currentStatus = "Successfully logged in.";
}
catch (HttpRequestException ex)
{
currentError = "Login failed (network error)";
currentStatus = "The login failed because of a network error: " + ex.ToString();
}
catch (System.Xml.XmlException ex)
{
currentError = "Login failed (parse error)";
currentStatus = "The login failed because the account server's response could not be parsed: " + ex.ToString();
}
catch (Exception ex)
{
currentError = "Login failed (unknown error)";
currentStatus = "The login failed because of an unknown error: " + ex.ToString();
}
finally
{
UpdateLoginStatus();
// Show error dialog if the login failed
if (!currentAccount.IsSignedIn)
{
await ShowErrorDialogAsync(currentError, currentStatus).ConfigureAwait(false);
}
}
if (!currentAccount.IsSignedIn)
{
// Also return if the login failed without an exception
return;
}
// Save or clear the stored login info
if (saveLoginInfo.IsChecked == true)
{
await SaveAccountDataAsync();
}
else
{
await DeleteAccountDataAsync();
}
// Set up the WebView and save login info if the login succeeded and
// no exceptions were thrown
if (currentAccount.IsSignedIn)
{
webView.CoreWebView2.AddWebResourceRequestedFilter($"{currentAccount.MiiversePortalServer}/*", CoreWebView2WebResourceContext.All);
try
{
webView.Source = new(currentAccount.MiiversePortalServer);
}
catch (UriFormatException ex)
{
await ShowErrorDialogAsync
(
"Invalid Miiverse portal server",
$"The Miiverse portal server ({currentAccount.MiiversePortalServer}) is not a valid URL.\n{ex}"
).ConfigureAwait(false);
}
}
}
/// <summary>Logs in when enter is pressed.</summary>
private void LoginOnEnter(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter)
{
// Call LoginAsync without awaiting and use an empty sender
LoginAsync(new object(), new RoutedEventArgs());
}
}
/// <summary>
/// Handles WebResourceRequested events for the Miiverse portal from
/// the WebView.
/// </summary>
private void MiiverseWebResourceRequestedHandler(CoreWebView2 sender, CoreWebView2WebResourceRequestedEventArgs e)
{
if (currentAccount is not null && currentAccount.IsSignedIn)
{
e.Request.Headers.SetHeader("X-Nintendo-ServiceToken", currentAccount.MiiverseToken);
e.Request.Headers.SetHeader("X-Nintendo-ParamPack", currentAccount.ParamPackData);
}
}
/// <summary>Moves focus to the to password box on enter.</summary>
private void MoveToPasswordOnEnter(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter)
{
password.Focus(FocusState.Programmatic);
}
}
/// <summary>
/// Navigates the WebView to different Miiverse pages when a button is
/// pressed, depending on the button's name.
/// </summary>
private void NavigateToPage(object sender, RoutedEventArgs e)
{
if (currentAccount is not null && currentAccount.IsSignedIn)
{
string page = ((Button)sender).Name switch
{
"userPage" => "/users/me",
"userMenuPage" => "/users/menu",
"activityFeedPage" => "/activity-feed",
"communitiesPage" => "/communities",
"messagesPage" => "/messages",
"notificationsPage" => "/news",
_ => "",
};
page = currentAccount.MiiversePortalServer + page;
try
{
webView.Source = new(page);
}
catch (UriFormatException ex)
{
_ = ShowErrorDialogAsync
(
"Invalid Miiverse portal server",
$"The Miiverse portal server (${page}) is not a valid URL.\n{ex}"
).ConfigureAwait(false);
}
}
}
/// <summary>Opens the settings dialog.</summary>
private async void OpenSettingsDialogAsync(object sender, RoutedEventArgs e)
{
settingsDialog.XamlRoot ??= Content.XamlRoot;
var result = await settingsDialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
// Save button clicked
settingsDialog.SaveSettings();
// Save the settings data as JSON
await SaveSettingsDataAsync().ConfigureAwait(false);
}
else if (result == ContentDialogResult.Secondary)
{
// Reset button clicked
ContentDialog confirmationDialog = new()
{
Title = "Reset confirmation",
Content = "Are you sure that you want to reset the settings?",
PrimaryButtonText = "Reset",
CloseButtonText = "Cancel",
XamlRoot = Content.XamlRoot,
};
var confirmation = await confirmationDialog.ShowAsync();
if (confirmation == ContentDialogResult.Primary)
{
settingsDialog.ResetSettings();
// Delete the stored settings data
await DeleteSettingsDataAsync().ConfigureAwait(false);
}
}
else
{
// Dialog was closed, do not save settings
settingsDialog.LoadSettings();
}
}
/// <summary>Reload or stops loading the WebView.</summary>
private void ReloadOrStop(object sender, RoutedEventArgs e)
{
try
{
if (isWebViewNavigating)
{
webView.CoreWebView2.Stop();
}
else
{
webView.Reload();
}
}
catch (InvalidOperationException ex)
{
// The WebView is not initialized, so the reload failed
_ = ShowErrorDialogAsync("Error: WebView not initialized", ex.ToString());
}
}
/// <summary>Saves the current account's login info as JSON.</summary>
private async Task SaveAccountDataAsync()
{
try
{
JsonObject accountDataObject = new()
{
["username"] = currentAccount?.PnidUsername,
["passwordHash"] = currentAccount?.PnidPasswordHash,
};
string jsonData = accountDataObject.ToJsonString(defaultSerializerOptions);
await File.WriteAllTextAsync(accountDataJsonPath, jsonData);
}
catch (Exception ex)
{
await ShowErrorDialogAsync("Failed to save account data", ex.ToString());
}
}
/// <summary>Saves the current settings as JSON.</summary>
private async Task SaveSettingsDataAsync()
{
try
{
string settingsData = JsonSerializer.Serialize(settingsDialog.CurrentSettings, defaultSerializerOptions);
await File.WriteAllTextAsync(settingsDataJsonPath, settingsData);
}
catch (Exception ex)
{
await ShowErrorDialogAsync("Failed to save new settings", ex.ToString());
}
}
/// <summary>
/// Sets up the WebView event handlers asynchronously after it loads.
/// </summary>
private async void SetupWebViewHandlersAsync(object sender, RoutedEventArgs e)
{
await webView.EnsureCoreWebView2Async();
webView.NavigationStarting += (sender, e) =>
{
reloadButton.Content = "Stop";
isWebViewNavigating = true;
};
webView.NavigationCompleted += (sender, e) =>
{
reloadButton.Content = "Reload";
isWebViewNavigating = false;
};
webView.CoreWebView2.HistoryChanged += HistoryChangedHandler;
webView.CoreWebView2.WebResourceRequested += MiiverseWebResourceRequestedHandler;
webView.NavigationCompleted += (sender, e) => _ = webView.ExecuteScriptAsync(javascriptCode);
}
/// <summary>Shows an error dialog over the window.</summary>
/// <param name="title">The error dialog's title.</param>
/// <param name="message">The error dialog's body.</param>
/// <returns>A Task object.</returns>
private async Task ShowErrorDialogAsync(string title, string message)
{
ContentDialog errorDialog = new()
{
Title = title,
Content = message,
CloseButtonText = "Close",
XamlRoot = Content.XamlRoot
};
_ = await errorDialog.ShowAsync().AsTask().ConfigureAwait(false);
}
/// <summary>Updates the login status text and button.</summary>
/// <param name="isLoggingIn">
/// Whether an account is currently being logged in to (if true,
/// disables the login button).
/// </param>
private void UpdateLoginStatus(bool isLoggingIn = false)
{
if (isLoggingIn)
{
loginStatus.Text = "Logging in...";
loginButton.IsEnabled = false;
}
else
{
loginStatus.Text = currentAccount is null
|| !currentAccount.IsSignedIn
? "Not logged in"
: $"Logged in as \"{currentAccount.PnidUsername}\"";
loginButton.IsEnabled = true;
}
}
/// <summary>
/// Executes JavaScript to click on the hidden image input, opening a
/// file explorer window to select am image to upload.
/// </summary>
private void UploadImage(object sender, RoutedEventArgs e)
{
_ = webView.ExecuteScriptAsync("document.getElementById('hidden-image-file-input').click();");
}
}
}