-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonApp.cs
113 lines (89 loc) · 4.57 KB
/
CommonApp.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
#nullable enable
using Dark.Net;
using Dark.Net.Wpf;
using Gma.System.MouseKeyHook;
using McMaster.Extensions.CommandLineUtils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using TrainerCommon.Cheats;
using TrainerCommon.Games;
using TrainerCommon.Trainer;
namespace TrainerCommon.App;
public abstract class CommonApp: Application {
private IKeyboardMouseEvents? keyboardShortcuts;
private TrainerService? trainerService;
protected abstract Game game { get; }
protected override void OnStartup(StartupEventArgs e) {
base.OnStartup(e);
DarkNet.Instance.SetCurrentProcessTheme(Theme.Auto);
new SkinManager().RegisterSkins(
getResourceUri("TrainerCommon", "App/Skins/Skin.Light.xaml"),
getResourceUri("TrainerCommon", "App/Skins/Skin.Dark.xaml"));
keyboardShortcuts = Hook.GlobalEvents();
keyboardShortcuts.OnCombination(game.cheats.Select(cheat =>
new KeyValuePair<Combination, Action>(cheat.keyboardShortcut, () => cheat.isEnabled.Value ^= true)));
trainerService = new TrainerServiceImpl();
MainWindowViewModel viewModel = new(game, trainerService);
if (e.Args.Any(arg => arg.ToLower() is "-?" or "-h" or "--help" or "/?" or "/h" or "/help" or "help")) {
string executableName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
MessageBox.Show($"""
{executableName}
Trainer window is shown, and no cheats are enabled by default.
{executableName} --enable-cheat "{game.cheats.First().name}"
Trainer window is shown, and the specified cheat is enabled automatically.
You can pass --enable-cheat more than once to enable multiple cheats at startup.
""", "Usage", MessageBoxButton.OK, MessageBoxImage.Information);
Current.Shutdown(0);
}
try {
foreach (Cheat cheat in getCheatsToEnableOnStartup(e.Args)) {
cheat.isEnabled.Value = true;
}
} catch (ApplicationException exception) {
MessageBox.Show(exception.Message, viewModel.windowTitle, MessageBoxButton.OK, MessageBoxImage.Error);
Current.Shutdown(1);
}
MainWindow = new MainWindow(viewModel);
DarkNet.Instance.SetWindowThemeWpf(MainWindow, Theme.Auto);
MainWindow!.Show();
trainerService.attachToGame(game);
}
/// <exception cref="IOException">if the resource dictionary XAML file can't be found in the executing assembly</exception>
private static Uri getResourceUri(string unpackedAssemblyName, string resourceDictionaryXamlPath) {
resourceDictionaryXamlPath = Uri.EscapeUriString(resourceDictionaryXamlPath.TrimStart('/'));
unpackedAssemblyName = Uri.EscapeUriString(unpackedAssemblyName);
string executingAssembly = Uri.EscapeUriString(Assembly.GetExecutingAssembly().GetName().Name);
Uri uri;
try {
uri = new Uri($"pack://application:,,,/{executingAssembly};component/{unpackedAssemblyName}/{resourceDictionaryXamlPath}", UriKind.Absolute);
GetResourceStream(uri)?.Stream.Dispose();
return uri;
} catch (IOException) {
uri = new Uri($"pack://application:,,,/{unpackedAssemblyName};component/{resourceDictionaryXamlPath}", UriKind.Absolute);
GetResourceStream(uri)?.Stream.Dispose();
return uri;
}
}
private IEnumerable<Cheat> getCheatsToEnableOnStartup(string[] args) {
CommandLineApplication argParser = new();
argParser.Conventions.UseDefaultConventions();
CommandOption<string> enableCheatOption = argParser.Option<string>("--enable-cheat", "Cheat to automatically enable on startup", CommandOptionType.MultipleValue);
argParser.Parse(args);
return enableCheatOption.ParsedValues.Select(cheatName => {
try {
return game.cheats.First(cheat => cheat.name.Equals(cheatName, StringComparison.CurrentCultureIgnoreCase));
} catch (InvalidOperationException) {
throw new ApplicationException($"No cheat found with name \"{cheatName}\".");
}
});
}
protected override void OnExit(ExitEventArgs e) {
keyboardShortcuts?.Dispose();
trainerService?.Dispose();
base.OnExit(e);
}
}