Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
314 lines (276 loc) · 13.3 KB
/
Copy pathProgram.cs
File metadata and controls
314 lines (276 loc) · 13.3 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
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows.Forms;
using Velopack;
namespace SteelSeriesDiscordStreamFix
{
internal static class Program
{
private const string ShowSettingsEventName = "SteelSeriesDiscordStreamFix_ShowSettingsRequest_Event";
// The installer/launcher sets this to ask a running copy to quit cleanly (un-mute Discord, restore
// the Sonar devices we hid) before it replaces the files with a newer version.
private const string ExitRequestEventName = "SteelSeriesDiscordStreamFix_ExitRequest_Event";
private static DateTime _lastErrorDialogUtc = DateTime.MinValue;
/// <summary>
/// Set by MicMuterContext: the same cleanup the normal exit path runs (un-mute Discord, restore the
/// Sonar devices WE hid). Lets a crash leave Windows in a clean state too.
/// </summary>
internal static Action EmergencyCleanup;
/// <summary>
/// 0 for a normal start. 1..MaxCrashRestarts when this process was started by the crash handler of the
/// previous process (argument --crash-restart=N). The app then tells the user it restarted itself.
/// </summary>
internal static int CrashRestartCount { get; private set; }
// A crash loop must never spin forever: after this many quick crashes in a row the app stays down.
private const int MaxCrashRestarts = 3;
// A process that ran at least this long before crashing is a new problem, not part of a crash loop.
private static readonly TimeSpan StableUptime = TimeSpan.FromMinutes(10);
[STAThread]
static void Main()
{
ParseCrashRestartArg();
AppDomain.CurrentDomain.UnhandledException += (s, e) =>
{
Logger.Log("FATAL UnhandledException: " +
((e.ExceptionObject as Exception)?.ToString() ?? e.ExceptionObject?.ToString()));
try
{
var cleanup = EmergencyCleanup;
if (cleanup != null) cleanup();
else if (AppSettings.WeHidDevices) SteelSeriesToggle.SetChatAndGamingVisible(true);
}
catch { }
// The app has no tray icon, so a crash would silently leave Discord unprotected: start a fresh copy.
if (e.IsTerminating) TryRestartAfterCrash();
};
Application.ThreadException += (s, e) =>
{
Logger.Log("FATAL ThreadException: " + e.Exception);
// Show the dialog at most once per minute so a repeating timer error can't spam the user.
var nowUtc = DateTime.UtcNow;
if (nowUtc - _lastErrorDialogUtc < TimeSpan.FromMinutes(1)) return;
_lastErrorDialogUtc = nowUtc;
ThemedMessageBox.Show("An unexpected error occurred. Details were written to the log file.",
"SteelSeries Discord Stream Fix", MessageBoxButtons.OK, MessageBoxIcon.Error);
};
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// .NET Framework 4.7.2 can fall back to old protocols on older Windows builds; GitHub needs TLS 1.2+.
try { ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; } catch { }
VelopackApp.Build()
.OnBeforeUninstallFastCallback(v => UninstallCleanup())
.OnBeforeUpdateFastCallback(v => BeforeUpdateMarkClean())
.Run();
TryEnableDpiAwareness();
Logger.LogHeader();
Logger.Log($"Main() launched. exe={Application.ExecutablePath}");
bool isAutoStart = Array.Exists(Environment.GetCommandLineArgs(),
a => string.Equals(a, "--autostart", StringComparison.OrdinalIgnoreCase));
if (isAutoStart)
{
Logger.Log("Detected --autostart launch (Windows startup) — Settings will not auto-open.");
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (var singleInstance = new Mutex(false, "SteelSeriesDiscordStreamFix_SingleInstance_Mutex"))
{
bool ownsMutex;
try
{
ownsMutex = singleInstance.WaitOne(TimeSpan.FromMilliseconds(200));
}
catch (AbandonedMutexException)
{
ownsMutex = true;
}
if (!ownsMutex)
{
// A Windows-startup launch must never pop the Settings window of an instance that already runs.
if (!isAutoStart && TrySignalRunningInstance())
{
return;
}
Logger.Log("Could not signal a running instance right away — retrying mutex acquisition briefly in case the previous instance is still exiting.");
// After a crash the dying process may need a little longer (Windows error reporting) to let go.
TimeSpan retryWait = CrashRestartCount > 0 ? TimeSpan.FromSeconds(15) : TimeSpan.FromSeconds(3);
try
{
ownsMutex = singleInstance.WaitOne(retryWait);
}
catch (AbandonedMutexException)
{
ownsMutex = true;
}
if (!ownsMutex)
{
if (!isAutoStart && !TrySignalRunningInstance())
{
ThemedMessageBox.Show("SteelSeries Discord Stream Fix is already running in the background.",
"SteelSeries Discord Stream Fix", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return;
}
}
try
{
using (var showSettingsEvent = new EventWaitHandle(false, EventResetMode.AutoReset, ShowSettingsEventName))
using (var exitRequestEvent = new EventWaitHandle(false, EventResetMode.AutoReset, ExitRequestEventName))
{
Application.Run(new MicMuterContext(showSettingsEvent, exitRequestEvent, isAutoStart));
}
}
catch (Exception ex)
{
Logger.Log("FATAL top-level exception in Main(): " + ex);
}
finally
{
try { singleInstance.ReleaseMutex(); } catch { }
}
}
}
/// <summary>
/// Runs when Velopack uninstalls the app from ANY place (Windows Settings > Apps, Update.exe, ...),
/// before the files are removed. The in-app Uninstall button does the same cleanup, but without this
/// hook a normal Windows uninstall would leave the Sonar Chat/Gaming devices hidden and the
/// startup entry behind. Velopack exits right after this hook, so keep it quick and never throw.
/// </summary>
private static void UninstallCleanup()
{
try
{
Logger.Log("Velopack uninstall hook: restoring Sonar devices, removing startup entry and settings.");
// Read this BEFORE the settings key is deleted below.
if (AppSettings.WeHidDevices)
{
try { SteelSeriesToggle.SetChatAndGamingVisible(true); }
catch (Exception ex) { Logger.Log("Uninstall hook: restoring devices failed: " + ex.Message); }
}
try { AppSettings.StartWithWindows = false; }
catch (Exception ex) { Logger.Log("Uninstall hook: removing startup entry failed: " + ex.Message); }
try
{
Microsoft.Win32.Registry.CurrentUser.DeleteSubKeyTree(
@"SOFTWARE\SteelSeriesDiscordStreamFix", throwOnMissingSubKey: false);
}
catch (Exception ex) { Logger.Log("Uninstall hook: registry cleanup failed: " + ex.Message); }
// Logs live outside the install folder, so remove them here (last, nothing logs after this).
Logger.DeleteLogFolder();
}
catch
{
// A hook must never throw.
}
}
private static void ParseCrashRestartArg()
{
try
{
const string prefix = "--crash-restart=";
foreach (string a in Environment.GetCommandLineArgs())
{
int n;
if (a.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) &&
int.TryParse(a.Substring(prefix.Length), out n) && n > 0)
{
CrashRestartCount = Math.Min(n, MaxCrashRestarts);
return;
}
}
}
catch { }
}
/// <summary>
/// Called from the crash handler after the cleanup ran: starts a new copy in the background, at most
/// MaxCrashRestarts times in a row, and only if the user had confirmed their settings.
/// </summary>
private static void TryRestartAfterCrash()
{
try
{
if (!AppSettings.HasConfirmedSettings) return; // never set up (or already uninstalled): nothing to protect
TimeSpan uptime;
using (Process me = Process.GetCurrentProcess())
uptime = DateTime.Now - me.StartTime;
int next = CrashRestartPolicy.NextAttempt(uptime, CrashRestartCount, MaxCrashRestarts, StableUptime);
if (next == 0)
{
Logger.Log("Crash handler: " + MaxCrashRestarts + " quick crashes in a row — NOT restarting again.");
return;
}
string exe = Application.ExecutablePath;
Process.Start(new ProcessStartInfo
{
FileName = exe,
Arguments = "--autostart --crash-restart=" + next,
WorkingDirectory = Path.GetDirectoryName(exe),
UseShellExecute = false
});
Logger.Log("Crash handler: started a new copy (attempt " + next + " of " + MaxCrashRestarts + ").");
// Leave right away, otherwise the Windows "has stopped working" dialog can keep this process (and the
// single-instance mutex) alive and the new copy would have to wait for it.
Environment.Exit(1);
}
catch (Exception ex)
{
Logger.Log("Crash handler: restart failed: " + ex.Message);
}
}
/// <summary>
/// Runs (in the OLD version, in its own short process) right before Velopack replaces the files. If the
/// updater has to kill a running copy that did not answer the exit request, that is not a crash: mark the
/// run as clean so the next start does not show a false "closed abnormally" notice. Never throws.
/// </summary>
private static void BeforeUpdateMarkClean()
{
try
{
Logger.Log("Velopack update hook: an update is about to be applied — marking this run as clean.");
AppSettings.RunState = "clean";
}
catch
{
// A hook must never throw.
}
}
private static bool TrySignalRunningInstance()
{
try
{
// This process was just started by the user, so it may bring a window to the front. Let the
// running instance do it too, otherwise its Settings window can open BEHIND other windows.
try { AllowSetForegroundWindow(-1 /* ASFW_ANY */); } catch { }
using (var showEvent = EventWaitHandle.OpenExisting(ShowSettingsEventName))
{
showEvent.Set();
}
Logger.Log("Signaled running instance to show Settings.");
return true;
}
catch (Exception ex)
{
Logger.Log("TrySignalRunningInstance failed: " + ex.Message);
return false;
}
}
private static void TryEnableDpiAwareness()
{
try
{
SetProcessDpiAwarenessContext(new IntPtr(-4) /* PER_MONITOR_AWARE_V2 */);
}
catch
{
try { SetProcessDPIAware(); } catch { /* pre-Vista, irrelevant */ }
}
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool AllowSetForegroundWindow(int dwProcessId);
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
private static extern bool SetProcessDpiAwarenessContext(IntPtr dpiContext);
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
private static extern bool SetProcessDPIAware();
}
}