-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
257 lines (225 loc) · 12.6 KB
/
Program.cs
File metadata and controls
257 lines (225 loc) · 12.6 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
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SimBlock.Core.Application.Interfaces;
using SimBlock.Core.Application.Services;
using SimBlock.Core.Domain.Interfaces;
using SimBlock.Infrastructure.Windows;
using SimBlock.Infrastructure.Services;
using SimBlock.Presentation.Forms;
using SimBlock.Presentation.Configuration;
using SimBlock.Presentation.Interfaces;
using SimBlock.Presentation.Managers;
using SimBlock.Presentation.Theming;
using SimBlock.Presentation.Services;
using System.Windows.Forms;
using System.Threading;
namespace SimBlock
{
public class Program
{
[STAThread]
public static void Main(string[] args)
{
// Ensure only a single instance runs
bool createdNew;
using var mutex = new Mutex(true, "SimBlockSingletonMutex", out createdNew);
if (!createdNew)
{
MessageBox.Show("SimBlock is already running.", "SimBlock", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
// Enable Windows Forms visual styles
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Build the host - don't use 'using' to prevent disposal
var host = CreateHostBuilder(args).Build();
try
{
// Run initialization and show main form
var splashScreenManager = host.Services.GetRequiredService<ISplashScreenManager>();
var logger = host.Services.GetRequiredService<ILogger<Program>>();
// Ensure settings are loaded early so UI picks up persisted layout/theme
var _ = host.Services.GetRequiredService<ISettingsManager>();
// Ensure macro mapping service is instantiated so it subscribes to hooks
var __ = host.Services.GetRequiredService<IMacroMappingService>();
logger.LogInformation("Starting SimBlock application...");
// Get required services for initialization
var keyboardBlockerService = host.Services.GetRequiredService<IKeyboardBlockerService>();
var mouseBlockerService = host.Services.GetRequiredService<IMouseBlockerService>();
// Create splash form
var splashForm = host.Services.GetRequiredService<SplashForm>();
bool initializationSuccessful = false;
// Set up initialization to run after splash form is shown
splashForm.Shown += async (sender, e) =>
{
try
{
// Create progress reporter and wire it up to splash screen
var progressReporter = new InitializationProgressReporter();
progressReporter.ProgressChanged += (sender2, args) =>
{
if (splashForm.InvokeRequired)
{
splashForm.Invoke(new Action(() =>
{
splashForm.UpdateProgress(args.Percentage, args.Status);
}));
}
else
{
splashForm.UpdateProgress(args.Percentage, args.Status);
}
};
// Initialize services
await keyboardBlockerService.InitializeAsync(progressReporter);
await mouseBlockerService.InitializeAsync(progressReporter);
// Small delay to show completion
await Task.Delay(500);
// Mark initialization as successful
initializationSuccessful = true;
// Switch to loading application state with spinner
splashForm.ShowLoadingApplication();
// Small delay to show the loading state
await Task.Delay(1000);
// Close splash form - this will end the Application.Run() below
splashForm.Close();
}
catch (Exception ex)
{
logger.LogCritical(ex, "Fatal error during initialization");
MessageBox.Show($"Fatal error: {ex.Message}", "SimBlock Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
splashForm.Close();
}
};
// Start the message loop with the splash screen
Application.Run(splashForm);
// Dispose splash form to free resources
splashForm.Dispose();
// If initialization was successful, create and run the main form
if (initializationSuccessful)
{
try
{
// Get all required services for MainForm after successful initialization
var uiSettings = host.Services.GetRequiredService<UISettings>();
var statusBarManager = host.Services.GetRequiredService<IStatusBarManager>();
var logoManager = host.Services.GetRequiredService<ILogoManager>();
var layoutManager = host.Services.GetRequiredService<IUILayoutManager>();
var shortcutManager = host.Services.GetRequiredService<IKeyboardShortcutManager>();
var resourceMonitor = host.Services.GetRequiredService<IResourceMonitor>();
var themeManager = host.Services.GetRequiredService<IThemeManager>();
var keyboardInfoService = host.Services.GetRequiredService<IKeyboardInfoService>();
var mouseInfoService = host.Services.GetRequiredService<IMouseInfoService>();
var visualizationManager = host.Services.GetRequiredService<IBlockingVisualizationManager>();
var mainFormLogger = host.Services.GetRequiredService<ILogger<MainForm>>();
var systemTrayService = host.Services.GetRequiredService<ISystemTrayService>();
// Create MainForm only after successful initialization
var mainForm = new MainForm(
keyboardBlockerService,
mouseBlockerService,
mainFormLogger,
uiSettings,
statusBarManager,
logoManager,
layoutManager,
shortcutManager,
resourceMonitor,
themeManager,
keyboardInfoService,
mouseInfoService,
host.Services,
visualizationManager,
systemTrayService
);
// Run main form
Application.Run(mainForm);
// Shutdown services after form is closed (avoid sync-over-async deadlocks)
keyboardBlockerService.ShutdownAsync().GetAwaiter().GetResult();
mouseBlockerService.ShutdownAsync().GetAwaiter().GetResult();
}
catch (Exception ex)
{
logger.LogError(ex, "Error running main form");
MessageBox.Show($"Failed to start SimBlock: {ex.Message}", "SimBlock Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
catch (Exception ex)
{
var logger = host.Services.GetService<ILogger<Program>>();
logger?.LogCritical(ex, "Fatal error occurred");
MessageBox.Show($"Fatal error: {ex.Message}", "SimBlock Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
// Dispose the host when done
host.Dispose();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
// Register UI configuration first
services.AddSingleton<UISettings>();
// Register system tray service
services.AddSingleton<ISystemTrayService, WindowsSystemTrayService>();
// Register domain services (keyboard first, then mouse with delayed injection)
services.AddSingleton<IKeyboardHookService, WindowsKeyboardHookService>();
services.AddSingleton<IKeyboardInfoService, WindowsKeyboardInfoService>();
services.AddSingleton<IMouseInfoService, WindowsMouseInfoService>();
// Register mouse hook service with custom factory to avoid circular dependency
services.AddSingleton<IMouseHookService>(provider =>
{
var logger = provider.GetRequiredService<ILogger<WindowsMouseHookService>>();
var uiSettings = provider.GetRequiredService<UISettings>();
var keyboardHookService = provider.GetRequiredService<IKeyboardHookService>();
return new WindowsMouseHookService(logger, uiSettings, keyboardHookService);
});
// Register application services
services.AddSingleton<IKeyboardBlockerService, KeyboardBlockerService>();
services.AddSingleton<IMouseBlockerService, MouseBlockerService>();
services.AddSingleton<IMacroService, MacroService>();
services.AddSingleton<IMacroMappingService, MacroMappingService>();
// Register infrastructure services
services.AddSingleton<SimBlock.Presentation.Interfaces.IResourceMonitor, ResourceMonitor>();
// Register UI managers
services.AddSingleton<ILogoManager, LogoManager>();
services.AddSingleton<IStatusBarManager, StatusBarManager>();
services.AddSingleton<IUILayoutManager, UILayoutManager>();
services.AddSingleton<IKeyboardShortcutManager, KeyboardShortcutManager>();
services.AddSingleton<IThemeManager, ThemeManager>();
services.AddSingleton<ISettingsManager, SettingsManager>();
services.AddSingleton<IBlockingVisualizationManager, BlockingVisualizationManager>();
services.AddSingleton<IStartupRegistrationService, WindowsStartupRegistrationService>();
services.AddSingleton<IThemeApplier, ThemeApplier>();
// Register auto-update services
services.AddSingleton<IVersionComparator, VersionComparator>();
services.AddSingleton<IGitHubReleaseService, GitHubReleaseService>();
services.AddSingleton<IAutoUpdateService, AutoUpdateService>();
services.AddSingleton<IAutoUpdateManager, AutoUpdateManager>();
// Register presentation layer
// MainForm is created manually in Main() to avoid disposal issues
services.AddTransient<SimBlock.Presentation.Forms.SettingsForm>();
services.AddTransient<SplashForm>();
services.AddTransient<MacroManagerForm>();
services.AddTransient<MacroMappingForm>();
services.AddTransient<MacroEditorForm>();
services.AddSingleton<SimBlock.Presentation.ViewModels.SettingsViewModel>();
// Register splash screen services
services.AddSingleton<ISplashScreenManager, SplashScreenManager>();
services.AddTransient<InitializationProgressReporter>();
// Configure logging
services.AddLogging(builder =>
{
builder.AddConsole();
builder.AddDebug();
builder.SetMinimumLevel(LogLevel.Information);
});
});
}
}