forked from bloxstraplabs/bloxstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaunchHandler.cs
267 lines (205 loc) · 9.28 KB
/
LaunchHandler.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
using System.Windows;
using Windows.Win32;
using Windows.Win32.Foundation;
using Bloxstrap.UI.Elements.Dialogs;
namespace Bloxstrap
{
public static class LaunchHandler
{
public static void ProcessNextAction(NextAction action, bool isUnfinishedInstall = false)
{
switch (action)
{
case NextAction.LaunchSettings:
LaunchSettings();
break;
case NextAction.LaunchRoblox:
LaunchRoblox(LaunchMode.Player);
break;
default:
App.Terminate(isUnfinishedInstall ? ErrorCode.ERROR_INSTALL_USEREXIT : ErrorCode.ERROR_SUCCESS);
break;
}
}
public static void ProcessLaunchArgs()
{
// this order is specific
if (App.LaunchSettings.UninstallFlag.Active)
LaunchUninstaller();
else if (App.LaunchSettings.MenuFlag.Active)
LaunchSettings();
else if (App.LaunchSettings.WatcherFlag.Active)
LaunchWatcher();
else if (App.LaunchSettings.RobloxLaunchMode != LaunchMode.None)
LaunchRoblox(App.LaunchSettings.RobloxLaunchMode);
else if (!App.LaunchSettings.QuietFlag.Active)
LaunchMenu();
else
App.Terminate();
}
public static void LaunchInstaller()
{
using var interlock = new InterProcessLock("Installer");
if (!interlock.IsAcquired)
{
Frontend.ShowMessageBox(Strings.Dialog_AlreadyRunning_Installer, MessageBoxImage.Stop);
App.Terminate();
return;
}
if (App.LaunchSettings.UninstallFlag.Active)
{
Frontend.ShowMessageBox(Strings.Bootstrapper_FirstRunUninstall, MessageBoxImage.Error);
App.Terminate(ErrorCode.ERROR_INVALID_FUNCTION);
return;
}
if (App.LaunchSettings.QuietFlag.Active)
{
var installer = new Installer();
if (!installer.CheckInstallLocation())
App.Terminate(ErrorCode.ERROR_INSTALL_FAILURE);
installer.DoInstall();
interlock.Dispose();
ProcessLaunchArgs();
}
else
{
#if QA_BUILD
Frontend.ShowMessageBox("You are about to install a QA build of Bloxstrap. The red window border indicates that this is a QA build.\n\nQA builds are handled completely separately of your standard installation, like a virtual environment.", MessageBoxImage.Information);
#endif
new LanguageSelectorDialog().ShowDialog();
var installer = new UI.Elements.Installer.MainWindow();
installer.ShowDialog();
interlock.Dispose();
ProcessNextAction(installer.CloseAction, !installer.Finished);
}
}
public static void LaunchUninstaller()
{
using var interlock = new InterProcessLock("Uninstaller");
if (!interlock.IsAcquired)
{
Frontend.ShowMessageBox(Strings.Dialog_AlreadyRunning_Uninstaller, MessageBoxImage.Stop);
App.Terminate();
return;
}
bool confirmed = false;
bool keepData = true;
if (App.LaunchSettings.QuietFlag.Active)
{
confirmed = true;
}
else
{
var dialog = new UninstallerDialog();
dialog.ShowDialog();
confirmed = dialog.Confirmed;
keepData = dialog.KeepData;
}
if (!confirmed)
{
App.Terminate();
return;
}
Installer.DoUninstall(keepData);
Frontend.ShowMessageBox(Strings.Bootstrapper_SuccessfullyUninstalled, MessageBoxImage.Information);
App.Terminate();
}
public static void LaunchSettings()
{
const string LOG_IDENT = "LaunchHandler::LaunchSettings";
using var interlock = new InterProcessLock("Settings");
if (interlock.IsAcquired)
{
bool showAlreadyRunningWarning = Process.GetProcessesByName(App.ProjectName).Length > 1;
var window = new UI.Elements.Settings.MainWindow(showAlreadyRunningWarning);
// typically we'd use Show(), but we need to block to ensure IPL stays in scope
window.ShowDialog();
}
else
{
App.Logger.WriteLine(LOG_IDENT, "Found an already existing menu window");
var process = Utilities.GetProcessesSafe().Where(x => x.MainWindowTitle == Strings.Menu_Title).FirstOrDefault();
if (process is not null)
PInvoke.SetForegroundWindow((HWND)process.MainWindowHandle);
App.Terminate();
}
}
public static void LaunchMenu()
{
var dialog = new LaunchMenuDialog();
dialog.ShowDialog();
ProcessNextAction(dialog.CloseAction);
}
public static void LaunchRoblox(LaunchMode launchMode)
{
const string LOG_IDENT = "LaunchHandler::LaunchRoblox";
if (launchMode == LaunchMode.None)
throw new InvalidOperationException("No Roblox launch mode set");
if (!File.Exists(Path.Combine(Paths.System, "mfplat.dll")))
{
Frontend.ShowMessageBox(Strings.Bootstrapper_WMFNotFound, MessageBoxImage.Error);
if (!App.LaunchSettings.QuietFlag.Active)
Utilities.ShellExecute("https://support.microsoft.com/en-us/topic/media-feature-pack-list-for-windows-n-editions-c1c6fffa-d052-8338-7a79-a4bb980a700a");
App.Terminate(ErrorCode.ERROR_FILE_NOT_FOUND);
}
if (App.Settings.Prop.ConfirmLaunches && Mutex.TryOpenExisting("ROBLOX_singletonMutex", out var _))
{
// this currently doesn't work very well since it relies on checking the existence of the singleton mutex
// which often hangs around for a few seconds after the window closes
// it would be better to have this rely on the activity tracker when we implement IPC in the planned refactoring
var result = Frontend.ShowMessageBox(Strings.Bootstrapper_ConfirmLaunch, MessageBoxImage.Warning, MessageBoxButton.YesNo);
if (result != MessageBoxResult.Yes)
{
App.Terminate();
return;
}
}
// start bootstrapper and show the bootstrapper modal if we're not running silently
App.Logger.WriteLine(LOG_IDENT, "Initializing bootstrapper");
App.Bootstrapper = new Bootstrapper(launchMode);
IBootstrapperDialog? dialog = null;
if (!App.LaunchSettings.QuietFlag.Active)
{
App.Logger.WriteLine(LOG_IDENT, "Initializing bootstrapper dialog");
dialog = App.Settings.Prop.BootstrapperStyle.GetNew();
App.Bootstrapper.Dialog = dialog;
dialog.Bootstrapper = App.Bootstrapper;
}
Task.Run(App.Bootstrapper.Run).ContinueWith(t =>
{
App.Logger.WriteLine(LOG_IDENT, "Bootstrapper task has finished");
if (t.IsFaulted)
{
App.Logger.WriteLine(LOG_IDENT, "An exception occurred when running the bootstrapper");
if (t.Exception is not null)
App.FinalizeExceptionHandling(t.Exception);
}
App.Terminate();
});
dialog?.ShowBootstrapper();
}
public static void LaunchWatcher()
{
const string LOG_IDENT = "LaunchHandler::LaunchWatcher";
// this whole topology is a bit confusing, bear with me:
// main thread: strictly UI only, handles showing of the notification area icon, context menu, server details dialog
// - server information task: queries server location, invoked if either the explorer notification is shown or the server details dialog is opened
// - discord rpc thread: handles rpc connection with discord
// - discord rich presence tasks: handles querying and displaying of game information, invoked on activity watcher events
// - watcher task: runs activity watcher + waiting for roblox to close, terminates when it has
var watcher = new Watcher();
Task.Run(watcher.Run).ContinueWith(t =>
{
App.Logger.WriteLine(LOG_IDENT, "Watcher task has finished");
watcher.Dispose();
if (t.IsFaulted)
{
App.Logger.WriteLine(LOG_IDENT, "An exception occurred when running the watcher");
if (t.Exception is not null)
App.FinalizeExceptionHandling(t.Exception);
}
App.Terminate();
});
}
}
}