|
| 1 | +using SingleInstanceCore; |
| 2 | + |
1 | 3 | namespace ChuChartManager; |
2 | 4 |
|
3 | 5 | public static class Program |
4 | 6 | { |
5 | 7 | [STAThread] |
6 | 8 | public static void Main() |
7 | 9 | { |
8 | | - ApplicationConfiguration.Initialize(); |
9 | | - |
10 | | - Directory.CreateDirectory(StaticSettings.AppDataDir); |
11 | | - StaticSettings.Config = Config.Load(); |
| 10 | + var app = new AppMain(); |
| 11 | + var isFirstInstance = app.InitializeAsFirstInstance("ChuChartManager"); |
| 12 | + if (!isFirstInstance) return; |
12 | 13 |
|
13 | | - var hasGamePath = !string.IsNullOrEmpty(StaticSettings.Config.GamePath) |
14 | | - && Directory.Exists(StaticSettings.Config.GamePath); |
15 | | - |
16 | | - if (hasGamePath) |
| 14 | + try |
17 | 15 | { |
18 | | - StaticSettings.GamePath = StaticSettings.Config.GamePath; |
19 | | - StaticSettings.ReadGameVersion(); |
20 | | - var scanner = new MusicScanner(StaticSettings.GamePath); |
21 | | - scanner.ScanAll(); |
22 | | - StaticSettings.Scanner = scanner; |
23 | | - |
24 | | - AppMain.BrowserWin = new Browser(); |
25 | | - AppMain.BrowserWin.Show(); |
| 16 | + ApplicationConfiguration.Initialize(); |
| 17 | + |
| 18 | + Directory.CreateDirectory(StaticSettings.AppDataDir); |
| 19 | + StaticSettings.Config = Config.Load(); |
| 20 | + |
| 21 | + var hasGamePath = !string.IsNullOrEmpty(StaticSettings.Config.GamePath) |
| 22 | + && Directory.Exists(StaticSettings.Config.GamePath); |
| 23 | + |
| 24 | + if (hasGamePath) |
| 25 | + { |
| 26 | + StaticSettings.GamePath = StaticSettings.Config.GamePath; |
| 27 | + StaticSettings.ReadGameVersion(); |
| 28 | + var scanner = new MusicScanner(StaticSettings.GamePath); |
| 29 | + scanner.ScanAll(); |
| 30 | + StaticSettings.Scanner = scanner; |
| 31 | + |
| 32 | + AppMain.BrowserWin = new Browser(); |
| 33 | + AppMain.BrowserWin.Show(); |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + AppMain.OobeBrowserWin = new OobeBrowser(); |
| 38 | + AppMain.OobeBrowserWin.Show(); |
| 39 | + } |
| 40 | + |
| 41 | + // Form.Show() 后 SynchronizationContext 才可用 |
| 42 | + AppMain.UiContext = SynchronizationContext.Current; |
| 43 | + |
| 44 | + ServerManager.StartApp(StaticSettings.Config.IsExport, url => |
| 45 | + { |
| 46 | + if (AppMain.BrowserWin is { IsDisposed: false } browser) |
| 47 | + browser.Invoke(() => browser.InjectBackendUrl(url)); |
| 48 | + if (AppMain.OobeBrowserWin is { IsDisposed: false } oobe) |
| 49 | + oobe.Invoke(() => oobe.InjectBackendUrl(url)); |
| 50 | + }); |
| 51 | + |
| 52 | + Application.Run(); |
26 | 53 | } |
27 | | - else |
| 54 | + finally |
28 | 55 | { |
29 | | - AppMain.OobeBrowserWin = new OobeBrowser(); |
30 | | - AppMain.OobeBrowserWin.Show(); |
| 56 | + SingleInstance.Cleanup(); |
31 | 57 | } |
32 | | - |
33 | | - // Form.Show() 后 SynchronizationContext 才可用 |
34 | | - AppMain.UiContext = SynchronizationContext.Current; |
35 | | - |
36 | | - ServerManager.StartApp(StaticSettings.Config.IsExport, url => |
37 | | - { |
38 | | - if (AppMain.BrowserWin is { IsDisposed: false } browser) |
39 | | - browser.Invoke(() => browser.InjectBackendUrl(url)); |
40 | | - if (AppMain.OobeBrowserWin is { IsDisposed: false } oobe) |
41 | | - oobe.Invoke(() => oobe.InjectBackendUrl(url)); |
42 | | - }); |
43 | | - |
44 | | - Application.Run(); |
45 | 58 | } |
46 | 59 | } |
0 commit comments