-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
169 lines (143 loc) · 5.59 KB
/
Program.cs
File metadata and controls
169 lines (143 loc) · 5.59 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
using VoicemeeterWindowsVolume.Controllers;
using VoicemeeterWindowsVolume.Models;
using VoicemeeterWindowsVolume.Views;
namespace VoicemeeterWindowsVolume;
/// <summary>
/// Application entry point. Wires up MVC components and starts the message loop.
/// </summary>
internal static class Program
{
private static readonly string DataDir =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "VMWV");
private static readonly string SettingsPath =
Path.Combine(DataDir, "settings.json");
private static readonly string LogPath =
Path.Combine(DataDir, "vmwv-crash.log");
private static readonly string AppLogPath =
Path.Combine(DataDir, "vmwv.log");
[STAThread]
static void Main()
{
// Catch all unhandled exceptions and log them before exiting
AppDomain.CurrentDomain.UnhandledException += (_, e) =>
LogFatal(e.ExceptionObject?.ToString() ?? "Unknown error");
TaskScheduler.UnobservedTaskException += (_, e) =>
{
LogFatal($"Unobserved task exception: {e.Exception}");
e.SetObserved();
};
try
{
Directory.CreateDirectory(DataDir);
// Redirect Console.WriteLine to vmwv.log (timestamped, auto-flush)
var logWriter = new TimestampedFileWriter(AppLogPath);
Console.SetOut(logWriter);
ApplicationConfiguration.Initialize();
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += (_, e) =>
{
LogFatal($"UI thread exception: {e.Exception}");
MessageBox.Show(e.Exception.Message, AppStrings.FriendlyName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
};
System.Console.WriteLine(
$"Voicemeeter Windows Volume started, Process ID: {Environment.ProcessId}");
// Detect system theme for icon color
string iconColor = GetSystemColor();
// Initialize View (must be on STA thread before Application.Run)
TrayViewController.Instance.Initialize(iconColor);
// Load settings, apply to UI, then start audio sync
SettingsController.Instance.LoadSettings(
settingsPath: SettingsPath,
defaults: new AppSettings(),
callback: () =>
{
TrayViewController.Instance.ApplySavedToggles();
System.Console.WriteLine("Starting audio synchronization");
AudioSyncController.Instance.StartAudioSync();
}
);
// ApplicationContext keeps the WinForms message pump alive without a main form.
// Application.Run() with no args exits immediately — we need the context.
var ctx = new TrayApplicationContext();
Application.Run(ctx);
}
catch (Exception ex)
{
LogFatal(ex.ToString());
MessageBox.Show(ex.Message, AppStrings.FriendlyName + " - Fatal Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static void LogFatal(string message)
{
try
{
Directory.CreateDirectory(DataDir);
File.AppendAllText(LogPath,
$"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}{Environment.NewLine}");
}
catch { /* can't log — ignore */ }
System.Console.WriteLine(message);
}
private static string GetSystemColor()
{
try
{
using var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize");
if (key?.GetValue("AppsUseLightTheme") is int val)
return val == 0 ? "dark" : "light";
}
catch { /* fall through */ }
return "default";
}
}
/// <summary>
/// Custom ApplicationContext that keeps the WinForms message pump running
/// for a tray-only app (no main view). Handles graceful shutdown.
/// </summary>
internal sealed class TrayApplicationContext : ApplicationContext
{
public TrayApplicationContext()
{
Application.ApplicationExit += (_, _) => Cleanup();
}
private static void Cleanup()
{
try
{
AudioSyncController.Instance.Disconnect();
PowerShellRunner.StopAllWorkers();
TrayViewController.Instance.Dispose();
System.Console.WriteLine("clean exit");
}
catch { /* best-effort cleanup */ }
}
}
/// <summary>
/// TextWriter that prepends a timestamp to every line and writes to a file with auto-flush.
/// Replaces Console.Out so all Console.WriteLine calls are captured to vmwv.log.
/// TODO: disable logging maybe? or log level control idk
/// </summary>
internal sealed class TimestampedFileWriter : TextWriter
{
private readonly StreamWriter _writer;
public TimestampedFileWriter(string path)
{
_writer = new StreamWriter(path, append: false, encoding: System.Text.Encoding.UTF8)
{
AutoFlush = true,
};
}
public override System.Text.Encoding Encoding => System.Text.Encoding.UTF8;
public override void WriteLine(string? value)
=> _writer.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {value}");
public override void Write(string? value)
=> _writer.Write(value);
protected override void Dispose(bool disposing)
{
if (disposing) _writer.Dispose();
base.Dispose(disposing);
}
}