-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathPipeServiceHandler.cs
809 lines (683 loc) · 28.5 KB
/
PipeServiceHandler.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
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Security.Principal;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using gamevault.Helper;
using gamevault.Models;
using gamevault.UserControls;
using gamevault.ViewModels;
using Windows.Devices.Sms;
namespace gamevault
{
/// <summary>
/// Provides a client-side API to & from the main running instance of the application
/// </summary>
public class PipeServiceHandler
{
/// <summary>
/// The URI scheme used for communication
/// </summary>
public const string GAMEVAULT_URI_SCHEME = "gamevault";
/// <summary>
/// The friendly name of the URI
/// </summary>
private const string GAMEVAULT_URI_NAME = "GameVault App";
/// <summary>
/// The name of the named pipe
/// </summary>
private const string GAMEVAULT_PIPE_NAME = "GameVault";
/// <summary>
/// The singleton instance of the handler, which is not null after the application has started
/// </summary>
public static PipeServiceHandler? Instance { get; private set; }
private TaskCompletionSource isReadyForCommandsTCS = new TaskCompletionSource();
private bool _isReadyForCommands = false;
/// <summary>
/// Whether or not the handler is ready for commands. If set to false (the default) then all commands sent to the main instance will be queued until ready.
/// </summary>
public bool IsReadyForCommands
{
get => _isReadyForCommands;
set
{
if (_isReadyForCommands == value)
return;
if (value)
{
isReadyForCommandsTCS?.TrySetResult();
}
else
{
// If we somehow have an existing tcs, cancel it before making a new one
isReadyForCommandsTCS?.TrySetCanceled();
isReadyForCommandsTCS = new TaskCompletionSource();
}
_isReadyForCommands = value;
}
}
private PipeServiceHandler()
{
}
/// <summary>
/// Starts the handler
/// </summary>
public static void StartInstance()
{
if (Instance != null)
return;
Instance = new PipeServiceHandler();
try
{
Instance.RegisterUriScheme();
}
catch { } //
try
{
Instance.StartNamedPipeServer();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message, "An error while startin the internal pipe server", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
}
}
/// <summary>
/// Registers the URI scheme which lets other applications communicate with the application
/// </summary>
private void RegisterUriScheme()
{
string? executablePath = null;
// winexe does not easily support getting our .exe file
//executablePath = System.Reflection.Assembly.GetEntryAssembly()?.Location;
// so get it from the process
executablePath = System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName;
if (string.IsNullOrEmpty(executablePath))
return;
#if WINDOWS
if (!App.IsWindowsPackage)
{
var view = Microsoft.Win32.RegistryView.Registry32;
if (Environment.Is64BitOperatingSystem)
view = Microsoft.Win32.RegistryView.Registry64;
using var root = Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, view);
using var classes = root.OpenSubKey(@"Software\Classes", true)!;
var openString = $"\"{executablePath}\" --uridata \"%1\"";
using var existing = classes.OpenSubKey(@$"{GAMEVAULT_URI_SCHEME}\shell\open\command");
if (existing != null && existing.GetValue("")?.ToString() == openString)
return;
using var newEntry = classes.CreateSubKey(GAMEVAULT_URI_SCHEME);
newEntry.SetValue("", $"URL:{GAMEVAULT_URI_NAME}");
newEntry.SetValue("URL Protocol", "");
using var command = newEntry.CreateSubKey(@"shell\open\command");
command.SetValue("", openString);
}
else
{
//Add MC Store Code if necessary
}
#endif
}
/// <summary>
/// Starts the named pipe server which accepts connections.
/// The named pipe will run on a background task (which should be a separate thread).
/// </summary>
private void StartNamedPipeServer()
{
_ = Task.Factory.StartNew(async () =>
{
var pipeSecurity = new PipeSecurity();
// Allow all users on the current machine to connect
pipeSecurity.AddAccessRule(new PipeAccessRule(
new SecurityIdentifier(WellKnownSidType.WorldSid, null),
PipeAccessRights.ReadWrite | PipeAccessRights.CreateNewInstance,
System.Security.AccessControl.AccessControlType.Allow));
while (true)
{
// Each time we accept a connection, we create a new named pipe
try
{
var server = NamedPipeServerStreamAcl.Create(GAMEVAULT_PIPE_NAME, PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances,
PipeTransmissionMode.Byte, PipeOptions.None, 0, 0, pipeSecurity);
await server.WaitForConnectionAsync().ConfigureAwait(false);
// Handle the pipe in the background, so we don't block our thread and can handle the next connection asap
_ = HandlePipeConnection(server).ConfigureAwait(false);
}
catch (Exception) { }
}
}, TaskCreationOptions.LongRunning).ConfigureAwait(false);
}
/// <summary>
/// Handles a single pipe connection
/// </summary>
private async Task HandlePipeConnection(NamedPipeServerStream server)
{
// There is no loop inside this method since we only accept a SINGLE message and then close the connection
// It might be possible to have a persistent connection in the future, in which case this would need to keep the pipe open
StreamWriter? writer = null;
StreamReader? reader = null;
try
{
if (!IsReadyForCommands)
await isReadyForCommandsTCS.Task;
string? message = null;
reader = new StreamReader(server, leaveOpen: true);
message = await reader.ReadLineAsync().ConfigureAwait(false);
if (!string.IsNullOrEmpty(message))
{
var response = await HandleMessage(message).ConfigureAwait(false);
if (!string.IsNullOrEmpty(response))
{
// It's possible we get a response to send back (query), so do that
writer = new StreamWriter(server, leaveOpen: true) { AutoFlush = true };
await writer.WriteLineAsync(response).ConfigureAwait(false);
}
}
}
finally
{
// Safely dispose anything we created
SafeDispose(writer);
SafeDispose(reader);
SafeDispose(server);
}
}
/// <summary>
/// Sends a single message to the main running instance
/// </summary>
/// <param name="message">The message to send (which should be an uri form)</param>
/// <param name="expectsResult">True if we expect a response (such as from a Query)</param>
/// <returns></returns>
public static async Task<string?> SendMessage(string message, bool expectsResult = false)
{
string? result = null;
var client = new NamedPipeClientStream(GAMEVAULT_PIPE_NAME);
StreamWriter? writer = null;
StreamReader? reader = null;
try
{
await client.ConnectAsync();
writer = new StreamWriter(client, leaveOpen: true) { AutoFlush = true };
await writer.WriteLineAsync(message);
if (expectsResult)
{
reader = new StreamReader(client, leaveOpen: true);
result = await reader.ReadLineAsync();
}
}
finally
{
SafeDispose(writer);
SafeDispose(reader);
SafeDispose(client);
}
return result;
}
/// <summary>
/// Safely dispose anything without throwing an error if it's already disposed or closed or whatever
/// </summary>
/// <param name="disposable">The object to dispose</param>
private static void SafeDispose(IDisposable? disposable)
{
if (disposable == null)
return;
try
{
disposable.Dispose();
}
catch (Exception) { }
}
/// <summary>
/// Handles a single message sent to this instance
/// </summary>
/// <param name="message">The message to handle</param>
/// <returns>A response if required (such as from a Query)</returns>
private async Task<string?> HandleMessage(string message)
{
if (string.IsNullOrEmpty(message))
return null;
CommandOptions opts;
if (message.Equals("ShowMainWindow", StringComparison.OrdinalIgnoreCase))
{
// ShowMainWindow is a legacy message which we don't pass around anymore
// But it's possible (unlikely) that other applications are sending it
// Remove in the future if we don't need it
opts = new CommandOptions() { Action = CommandOptions.ActionEnum.Show };
}
else
{
// Parse the message
opts = CommandOptions.CreateFromUri(message);
}
return await HandleCommand(opts);
}
/// <summary>
/// Handles a command (which could be potentially our command line options)
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task<string?> HandleCommand(CommandOptions? options)
{
if (options == null)
return null;
if ((options.Action == CommandOptions.ActionEnum.Install || options.Action == CommandOptions.ActionEnum.Uninstall) && !SettingsViewModel.Instance.License.IsActive())
{
try
{
string url = "https://phalco.de/products/gamevault-plus/checkout?hit_paywall=true";
if (SettingsViewModel.Instance.DevTargetPhalcodeTestBackend)
{
url = "https://test.phalco.de/products/gamevault-plus/checkout?hit_paywall=true";
}
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
return null;
}
catch { return null; }
}
if (!IsReadyForCommands)
await isReadyForCommandsTCS.Task;
bool showMainWindow = false;
bool isGameInstalled = false;
Task? task = null;
if (options.GameId.HasValue)
{
// There's a gameid and most of our actions want to know if it's already installed
isGameInstalled = await GetInstalledGame(options.GameId.Value) != null;
}
// do stuff
switch (options.Action)
{
case CommandOptions.ActionEnum.Show:
showMainWindow = true;
if (options.JumpListCommand.HasValue)
{
task = ExecuteJumpListCommand(options.JumpListCommand.Value);
}
if (options.GameId.HasValue)
{
task = ShowGame(options.GameId.Value);
}
break;
case CommandOptions.ActionEnum.Install:
if (options.GameId.HasValue)
{
showMainWindow = true;
if (!isGameInstalled)
task = InstallGame(options.GameId.Value);
else
task = ShowGame(options.GameId.Value);
}
break;
case CommandOptions.ActionEnum.Uninstall:
showMainWindow = isGameInstalled;
if (options.GameId.HasValue)
{
showMainWindow = true;
if (isGameInstalled)
task = UninstallGame(options.GameId.Value);
else
task = ShowGame(options.GameId.Value);
}
break;
case CommandOptions.ActionEnum.Start:
if (options.GameId.HasValue)
{
if (!isGameInstalled)
{
showMainWindow = true;
if (options.AutoInstall == true && SettingsViewModel.Instance.License.IsActive())
task = InstallGame(options.GameId.Value);
else
task = ShowGame(options.GameId.Value);
}
else
{
showMainWindow = false;
task = StartGame(options.GameId.Value);
}
}
break;
case CommandOptions.ActionEnum.Stop:
showMainWindow = false;
task = StopGame(options.GameId);
break;
case CommandOptions.ActionEnum.Query:
// Shortcut the return since we absolutely never want to show the UI if we're doing a query
return await HandleQuery(options);
default:
// You should really implement new actions that you add
throw new NotImplementedException($"Action {options.Action} not implemented");
}
if (options.Minimized.HasValue)
{
// If we're provided a Minimized value then we can explicitly use that for whether or not to be shown
showMainWindow = !options.Minimized.Value;
}
if (showMainWindow)
{
// Dispatch is used to ensure that we're on the UI thread
await Dispatch(() =>
{
var mainWindow = System.Windows.Application.Current.MainWindow;
if (mainWindow != null)
{
// Make visible
mainWindow.Show();
// Restore
if (mainWindow.WindowState == System.Windows.WindowState.Minimized)
mainWindow.WindowState = System.Windows.WindowState.Normal;
// Bring to foreground
mainWindow.Activate();
}
});
if (task != null)
await task;
}
return null;
}
/// <summary>
/// Get an installed game
/// </summary>
/// <param name="id">The game id</param>
/// <returns>The game or null if not installed</returns>
private async Task<Game?> GetInstalledGame(int id)
{
if (!InstallViewModel.Instance.InstalledGames.Any())
{
await MainWindowViewModel.Instance.Library.GetGameInstalls().RestoreInstalledGames();
}
return InstallViewModel.Instance.InstalledGames.Where(g => g.Key.ID == id).Select(g => g.Key).FirstOrDefault();
}
/// <summary>
/// Get a game from the server or offline cache
/// </summary>
/// <param name="id">The game id</param>
/// <returns>The game or null if not found</returns>
private async Task<Game?> GetServerGame(int id)
{
Game? game = null;
if (LoginManager.Instance.IsLoggedIn())
{
try
{
string result = await WebHelper.GetRequestAsync(@$"{SettingsViewModel.Instance.ServerUrl}/api/games/{id}");
game = JsonSerializer.Deserialize<Game>(result);
}
catch (Exception)
{
}
}
if (game == null)
{
try
{
string compressedStringObject = Preferences.Get(id.ToString(), AppFilePath.OfflineCache);
if (!string.IsNullOrEmpty(compressedStringObject))
{
string decompressedObject = StringCompressor.DecompressString(compressedStringObject);
Game? deserializedObject = JsonSerializer.Deserialize<Game>(decompressedObject);
game = deserializedObject;
}
}
catch (Exception) { }
}
return game;
}
/// <summary>
/// Start a game
/// </summary>
private async Task StartGame(int id)
{
var game = await GetInstalledGame(id);
if (game == null)
return;
// Ensure we're on the UI thread
await Dispatch(() =>
{
var gameViewUserControl = new UserControls.GameViewUserControl(game, LoginManager.Instance.IsLoggedIn());
// Set the correct UI regardless of if it's visible to let the user manage it
MainWindowViewModel.Instance.SetActiveControl(gameViewUserControl);
InstallUserControl.PlayGame(game.ID);
});
}
/// <summary>
/// Stop a game
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private async Task StopGame(int? id)
{
if (id.HasValue)
{
var game = await GetInstalledGame(id.Value);
if (game == null)
return;
}
// Can't implement this yet since we don't actually track games that are running or stopped
throw new NotImplementedException();
}
/// <summary>
/// Uninstall a game
/// </summary>
private async Task UninstallGame(int id)
{
var game = await GetInstalledGame(id);
if (game == null)
return;
// Ensure we're on the UI thread
await Dispatch(async () =>
{
var gameSettingsUserControl = new UserControls.GameSettingsUserControl(game) { Width = 1200, Height = 800, Margin = new Thickness(50) };
// Would be nice to have this all in the background but there's potentially multiple popups that could have multiple options
MainWindowViewModel.Instance.OpenPopup(gameSettingsUserControl);
await gameSettingsUserControl.UninstallGame();
});
}
/// <summary>
/// Install a game
/// </summary>
private async Task InstallGame(int id)
{
var game = await GetInstalledGame(id);
// Check if the game is already installed
if (game == null)
{
game = await GetServerGame(id);
if (game == null)
{
MessageBox.Show($"Game with ID {id} not found", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
// Ensure we're on the UI thread
await Dispatch(async () =>
{
await MainWindowViewModel.Instance.Downloads.TryStartDownload(game);
MainWindowViewModel.Instance.SetActiveControl(MainWindowViewModel.Instance.Downloads);
});
// Let user hit the Install button since the UI is complicated and doesn't support auto installing anyway
}
}
/// <summary>
/// Show a specific game
/// </summary>
private async Task ShowGame(int id)
{
var game = await GetInstalledGame(id);
if (game == null)
game = await GetServerGame(id);
if (game != null)
{
// Ensure we're on the UI thread
await Dispatch(() =>
{
var gameViewUserControl = new UserControls.GameViewUserControl(game, LoginManager.Instance.IsLoggedIn());
MainWindowViewModel.Instance.SetActiveControl(gameViewUserControl);
});
}
else
{
MessageBox.Show($"Game with ID {id} not found", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private async Task ExecuteJumpListCommand(int id)
{
// Ensure we're on the UI thread
await Dispatch(async () =>
{
switch (id)
{
case 15:
{
await App.Instance.ExitApp();
break;
}
default:
{
if (Enum.TryParse(id.ToString(), out MainControl mainControl))
{
MainWindowViewModel.Instance.SetActiveControl(mainControl);
}
break;
}
}
});
}
public enum ActionQueryEnum
{
/// <summary>
/// Returns true/false depending on if the game exists (installed or not)
/// </summary>
Exists,
/// <summary>
/// Returns true/false depending on if the game is downloaded
/// </summary>
Downloaded,
/// <summary>
/// Returns true/false depending on if the game is installed
/// </summary>
Installed,
/// <summary>
/// Returns the name of the game
/// </summary>
GetName,
/// <summary>
/// Returns the install directory of the game
/// </summary>
GetInstallDirectory,
/// <summary>
/// Returns the version of the app
/// </summary>
GetAppVersion,
/// <summary>
/// Returns the server url used by the app
/// </summary>
GetServerUrl,
/// <summary>
/// Returns true/false depending on if the user is logged in
/// </summary>
IsLoggedIn,
}
/// <summary>
/// Handles a query (<see cref="CommandOptions.ActionEnum.Query"/>) with a <see cref="CommandOptions.Query"/> of the enum type <see cref="ActionQueryEnum"/>
/// </summary>
/// <returns>The result of the query OR a string prefixed with "error: " if there was an error</returns>
/// <exception cref="NotImplementedException">If the relevant <see cref="ActionQueryEnum"/> is not implemented</exception>
private async Task<string> HandleQuery(CommandOptions options)
{
if (!Enum.TryParse<ActionQueryEnum>(options.Query, true, out var query))
return "error: invalid query";
switch (query)
{
case ActionQueryEnum.Exists:
{
if (!options.GameId.HasValue)
return "error: no game id";
var game = await GetInstalledGame(options.GameId.Value);
if (game == null)
game = await GetServerGame(options.GameId.Value);
return (game != null).ToString();
}
case ActionQueryEnum.Downloaded:
{
if (!options.GameId.HasValue)
return "error: no game id";
var downloaded = false;
var game = await GetInstalledGame(options.GameId.Value);
if (game == null)
downloaded = true;
else if (DownloadsViewModel.Instance.DownloadedGames.Any(g => g.GetGameId() == options.GameId.Value))
downloaded = true;
return downloaded.ToString();
}
case ActionQueryEnum.Installed:
{
if (!options.GameId.HasValue)
return "error: no game id";
var game = await GetInstalledGame(options.GameId.Value);
return (game != null).ToString();
}
case ActionQueryEnum.GetName:
{
if (!options.GameId.HasValue)
return "error: no game id";
var game = await GetInstalledGame(options.GameId.Value);
if (game == null)
game = await GetServerGame(options.GameId.Value);
return game?.Title ?? "error: game not found";
}
case ActionQueryEnum.GetInstallDirectory:
{
if (!options.GameId.HasValue)
return "error: no game id";
var game = await GetInstalledGame(options.GameId.Value);
if (game == null)
return "";
var installDirectory = InstallViewModel.Instance.InstalledGames.Where(g => g.Key.ID == game.ID).Select(g => g.Value).FirstOrDefault();
return installDirectory ?? "";
}
case ActionQueryEnum.GetServerUrl:
return SettingsViewModel.Instance.ServerUrl;
case ActionQueryEnum.GetAppVersion:
return SettingsViewModel.Instance.Version;
case ActionQueryEnum.IsLoggedIn:
return LoginManager.Instance.IsLoggedIn().ToString();
default:
// You should really implement new Action Queries
throw new NotImplementedException($"Query {query} not implemented");
}
}
/// <summary>
/// Dispatches an action on the UI thread (if available)
/// </summary>
private async Task Dispatch(Action action)
{
var dispatcher = System.Windows.Application.Current.Dispatcher;
if (dispatcher != null)
{
await dispatcher.InvokeAsync(action);
}
else
{
action.Invoke();
}
}
/// <summary>
/// Dispatches an async action on the UI thread (if available)
/// </summary>
private async Task Dispatch(Func<Task> action)
{
var dispatcher = System.Windows.Application.Current.Dispatcher;
if (dispatcher != null)
{
await dispatcher.InvokeAsync(action);
}
else
{
await action.Invoke();
}
}
}
}