-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
383 lines (347 loc) · 15 KB
/
Program.cs
File metadata and controls
383 lines (347 loc) · 15 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
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
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2026 itsnateai
using EQSwitch.Config;
using EQSwitch.Core;
using EQSwitch.UI;
namespace EQSwitch;
static class Program
{
// Single-instance mutex to prevent multiple copies running
private static Mutex? _mutex;
[STAThread]
static void Main(string[] args)
{
// --test-migrate <input-json> — run ConfigVersionMigrator on the file and write
// <input>.migrated.json next to it. Exits without showing UI. Used by scripted
// migration test fixtures under _tests/migration/.
if (args.Length >= 2 && args[0] == "--test-migrate")
{
try
{
var inputPath = args[1];
var inputJson = File.ReadAllText(inputPath);
var (migratedJson, didMigrate) = ConfigVersionMigrator.MigrateIfNeeded(inputJson);
var outputPath = inputPath + ".migrated.json";
File.WriteAllText(outputPath, migratedJson);
File.WriteAllText(inputPath + ".test-result.txt",
$"input={inputPath}\noutput={outputPath}\nmigrated={didMigrate}\n");
}
catch (Exception ex)
{
File.WriteAllText(args[1] + ".test-result.txt", $"ERROR: {ex.GetType().Name}: {ex.Message}\n{ex.StackTrace}\n");
}
return;
}
// --test-split <input-v3-json> — deserialize accounts[] into LoginAccount[],
// run LoginAccountSplitter, and write the split result to <input>.split.json.
// The fixture harness asserts this output matches the migrator's accountsV4 /
// charactersV4 keys so the two code paths can't drift silently.
if (args.Length >= 2 && args[0] == "--test-split")
{
try
{
var inputPath = args[1];
var inputJson = File.ReadAllText(inputPath);
var options = new System.Text.Json.JsonSerializerOptions
{
PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase,
};
var root = System.Text.Json.Nodes.JsonNode.Parse(inputJson)?.AsObject();
var accountsArray = root?["accounts"]?.ToJsonString() ?? "[]";
var legacyAccounts = System.Text.Json.JsonSerializer.Deserialize<List<EQSwitch.Models.LoginAccount>>(
accountsArray, options) ?? new List<EQSwitch.Models.LoginAccount>();
var (v4Accounts, v4Characters) = EQSwitch.Config.LoginAccountSplitter.Split(legacyAccounts);
var splitOutput = new System.Text.Json.Nodes.JsonObject
{
["accounts"] = System.Text.Json.Nodes.JsonNode.Parse(
System.Text.Json.JsonSerializer.Serialize(v4Accounts, options)),
["characters"] = System.Text.Json.Nodes.JsonNode.Parse(
System.Text.Json.JsonSerializer.Serialize(v4Characters, options)),
};
File.WriteAllText(inputPath + ".split.json", splitOutput.ToJsonString(
new System.Text.Json.JsonSerializerOptions { WriteIndented = true }));
}
catch (Exception ex)
{
File.WriteAllText(args[1] + ".test-result.txt", $"ERROR (split): {ex.GetType().Name}: {ex.Message}\n{ex.StackTrace}\n");
}
return;
}
// --test-character-selector — run Core/CharacterSelectorTests.RunAll() and
// exit with its return code. Used to gate Phase 5b's pure decision helper.
// Uses Environment.Exit (vs. the sibling --test-* flags' file-side-effect
// pattern) because the test-runner's pass/fail status IS the return value.
// try/catch matches sibling flags so CLR unhandled-exception crashes get a
// distinct exit code (2) vs. assertion-failure exit code (1).
if (args.Length >= 1 && args[0] == "--test-character-selector")
{
int exitCode;
try
{
exitCode = Core.CharacterSelectorTests.RunAll();
}
catch (Exception ex)
{
Console.Error.WriteLine($"CharacterSelectorTests CRASHED: {ex.GetType().Name}: {ex.Message}");
Console.Error.WriteLine(ex.StackTrace);
exitCode = 2;
}
Environment.Exit(exitCode);
return;
}
// --test-config-validate — run Core/AppConfigValidateTests.RunAll() and
// exit with its return code. Covers AppConfig.Validate()'s defense-in-
// depth resync blocks (Phase 5b's CharacterAliases/LegacyCharacterProfiles
// mirror in particular). Same exit-code and try/catch contract as the
// sibling --test-character-selector flag.
if (args.Length >= 1 && args[0] == "--test-config-validate")
{
int exitCode;
try
{
exitCode = Core.AppConfigValidateTests.RunAll();
}
catch (Exception ex)
{
Console.Error.WriteLine($"AppConfigValidateTests CRASHED: {ex.GetType().Name}: {ex.Message}");
Console.Error.WriteLine(ex.StackTrace);
exitCode = 2;
}
Environment.Exit(exitCode);
return;
}
// --test-key-input-writer — run Core/KeyInputWriterTests.RunAll() and
// exit with its return code. Guards the hotfix v3 MMF write-order contract.
if (args.Length >= 1 && args[0] == "--test-key-input-writer")
{
int exitCode;
try
{
exitCode = Core.KeyInputWriterTests.RunAll();
}
catch (Exception ex)
{
Console.Error.WriteLine($"KeyInputWriterTests CRASHED: {ex.GetType().Name}: {ex.Message}");
Console.Error.WriteLine(ex.StackTrace);
exitCode = 2;
}
Environment.Exit(exitCode);
return;
}
// --test-shm-layout — verify C# SharedKeyState struct layout matches
// Native/key_shm.h. Fails fast if a refactor drifts either side.
if (args.Length >= 1 && args[0] == "--test-shm-layout")
{
int exitCode;
try
{
exitCode = Core.ShmLayoutTests.RunAll();
}
catch (Exception ex)
{
Console.Error.WriteLine($"ShmLayoutTests CRASHED: {ex.GetType().Name}: {ex.Message}");
Console.Error.WriteLine(ex.StackTrace);
exitCode = 2;
}
Environment.Exit(exitCode);
return;
}
// Enforce single instance
const string mutexName = "EQSwitch_SingleInstance_SoD";
_mutex = new Mutex(true, mutexName, out bool createdNew);
// After self-update, the old instance may still be shutting down
if (!createdNew && args.Contains("--after-update"))
{
for (int i = 0; i < 10 && !createdNew; i++)
{
Thread.Sleep(500);
_mutex.Dispose();
_mutex = new Mutex(true, mutexName, out createdNew);
}
}
if (!createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FloatingTooltip.Show("EQSwitch is already running. Check your system tray.", 4000);
// Brief delay so tooltip is visible before process exits
Thread.Sleep(4200);
return;
}
bool isAfterUpdate = args.Contains("--after-update");
FileLogger.Initialize();
CleanupUpdateArtifacts();
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Set an app-managed default font. Without this, controls use the
// static Control.DefaultFont (SystemFonts.DefaultFont) which gets
// invalidated during runtime — possibly by display change events,
// DPI context switches, or GDI+ cleanup in this tray-only app.
// Controls then throw "Parameter is not valid" on construction.
Application.SetDefaultFont(new Font("Segoe UI", 9f));
// Catch UI thread exceptions BEFORE WinForms tries to show ThreadExceptionDialog
// (which itself crashes due to GDI+ font corruption, hiding the real error)
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += (_, e) =>
{
FileLogger.Error("UI thread exception (original)", e.Exception);
try
{
MessageBox.Show(
$"EQSwitch encountered an error:\n\n{e.Exception.GetType().Name}: {e.Exception.Message}\n\n{e.Exception.StackTrace}",
"EQSwitch Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
catch
{
// If even MessageBox fails (GDI+ corruption), at least we logged it
}
};
try
{
var config = ConfigManager.Load();
// First-run: try migrating from AHK config, then show EQ path picker
bool isNewUser = false;
if (config.IsFirstRun)
{
var migrated = ConfigMigration.TryImportFromAhk();
if (migrated != null)
{
config = migrated;
isNewUser = true;
MessageBox.Show(
"Imported settings from eqswitch.cfg (AHK version).\nCheck Settings to verify everything looks right.",
"EQSwitch — Migration",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
using var dialog = new FirstRunDialog();
if (dialog.ShowDialog() != DialogResult.OK)
return; // User cancelled — don't start
config.EQPath = dialog.SelectedEQPath;
config.IsFirstRun = false;
isNewUser = true;
}
// Seed EQ client settings from actual ini so AppConfig reflects reality
// instead of hardcoded defaults — prevents silent overwrites on first Save
if (!string.IsNullOrEmpty(config.EQPath))
{
var iniPath = Path.Combine(config.EQPath, "eqclient.ini");
config.EQClientIni = EQClientIniConfig.SeedFromIni(iniPath);
}
ConfigManager.Save(config);
ConfigManager.FlushSave();
}
var processManager = new ProcessManager(config);
var trayApp = new TrayManager(config, processManager);
// Ensure cleanup on any exit path (not just tray menu Exit)
Application.ApplicationExit += (_, _) => trayApp.Dispose();
trayApp.Initialize();
// Auto-open Settings on first run so new users can configure
if (isNewUser)
trayApp.OpenSettingsAfterDelay();
// Show confirmation after successful self-update (delayed so tray is ready)
if (isAfterUpdate)
{
var postUpdateTimer = new System.Windows.Forms.Timer { Interval = 1500 };
postUpdateTimer.Tick += (_, _) =>
{
postUpdateTimer.Stop();
postUpdateTimer.Dispose();
var version = System.Reflection.Assembly.GetExecutingAssembly()
.GetName().Version?.ToString(3) ?? "?";
FloatingTooltip.Show($"✅ EQSwitch updated to v{version}!", 5000);
};
postUpdateTimer.Start();
}
// --test-update: simulate update flow without hitting GitHub
#if DEBUG
if (args.Contains("--test-update"))
{
UpdateDialog.TestMode = true;
var timer = new System.Windows.Forms.Timer { Interval = 500 };
timer.Tick += (_, _) =>
{
timer.Stop();
timer.Dispose();
using var dlg = new UpdateDialog();
dlg.ShowDialog();
};
timer.Start();
}
#endif
Application.Run();
}
catch (Exception ex)
{
FileLogger.Error("Fatal error", ex);
MessageBox.Show(
$"EQSwitch encountered a fatal error:\n\n{ex.Message}",
"EQSwitch Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
finally
{
ConfigManager.FlushSave();
ConfigManager.Shutdown();
FileLogger.Shutdown();
if (createdNew) _mutex?.ReleaseMutex();
_mutex?.Dispose();
}
}
private static void CleanupUpdateArtifacts()
{
var dir = AppDomain.CurrentDomain.BaseDirectory;
var exePath = Path.Combine(dir, "EQSwitch.exe");
// Torn-state recovery: if exe is missing but .old exists, restore it
if (!File.Exists(exePath))
{
var oldPath = exePath + ".old";
if (File.Exists(oldPath))
{
try
{
File.Move(oldPath, exePath);
FileLogger.Warn("Recovered EQSwitch.exe from .old after interrupted update.");
}
catch (Exception ex)
{
FileLogger.Error($"Failed to recover EQSwitch.exe from .old: {ex.Message}");
}
}
return;
}
// Clean up each artifact independently so one locked file doesn't block the rest.
// Retry exe.old — the old process may still be releasing the memory-mapped file.
foreach (var pattern in new[] { "EQSwitch.exe.old", "EQSwitch.exe.new",
"eqswitch-hook.dll.old", "eqswitch-hook.dll.new",
"eqswitch-di8.dll.old", "eqswitch-di8.dll.new",
"update.zip" })
{
var path = Path.Combine(dir, pattern);
if (!File.Exists(path)) continue;
for (int attempt = 0; attempt < 3; attempt++)
{
try
{
File.Delete(path);
FileLogger.Info($"Cleaned up update artifact: {pattern}");
break;
}
catch (Exception) when (attempt < 2)
{
Thread.Sleep(500);
}
catch (Exception ex)
{
FileLogger.Warn($"Failed to clean up {pattern}: {ex.Message}");
}
}
}
}
}