11using Microsoft . Extensions . DependencyInjection ;
22using PenumbraModForwarder . Common . Interfaces ;
3- using PenumbraModForwarder . Common . Services ;
43using PenumbraModForwarder . UI . Interfaces ;
54using PenumbraModForwarder . UI . Views ;
65using Serilog ;
@@ -9,58 +8,80 @@ namespace PenumbraModForwarder.UI;
98
109static class Program
1110{
12- /// <summary>
13- /// The main entry point for the application.
14- /// </summary>
11+ private static IServiceProvider _serviceProvider ;
12+
13+ public static bool IsExiting { get ; private set ; } = false ;
14+
1515 [ STAThread ]
1616 static void Main ( string [ ] args )
1717 {
18- var serviceProvider = Extensions . ServiceExtensions . Configuration ( ) ;
19-
18+ _serviceProvider = Extensions . ServiceExtensions . Configuration ( ) ;
19+
2020 Application . EnableVisualStyles ( ) ;
2121 Application . SetCompatibleTextRenderingDefault ( false ) ;
22-
22+
2323 if ( args . Length > 0 )
2424 {
2525 var filePath = args [ 0 ] ;
26- HandleFileArgs ( serviceProvider , filePath ) ;
26+ HandleFileArgs ( filePath ) ;
2727 return ;
2828 }
29-
30- IsProgramAlreadyRunning ( serviceProvider ) ;
31- CheckForUpdates ( serviceProvider ) ;
32- MigrateOldConfigIfExists ( serviceProvider ) ;
33- CreateStartMenuShortcut ( serviceProvider ) ;
34- SetTexToolsPath ( serviceProvider ) ;
29+
3530 Application . ApplicationExit += OnApplicationExit ;
36- Application . Run ( serviceProvider . GetRequiredService < MainWindow > ( ) ) ;
31+
32+ IsProgramAlreadyRunning ( ) ;
33+
34+ CheckForUpdates ( ) ;
35+
36+ if ( IsExiting )
37+ {
38+ return ;
39+ }
40+
41+ MigrateOldConfigIfExists ( ) ;
42+ CreateStartMenuShortcut ( ) ;
43+ SetTexToolsPath ( ) ;
44+ Application . Run ( _serviceProvider . GetRequiredService < MainWindow > ( ) ) ;
3745 }
38-
46+
3947 private static void OnApplicationExit ( object sender , EventArgs e )
4048 {
49+ // Optional: Additional cleanup if needed
50+ }
51+
52+ public static void ExitApplication ( )
53+ {
54+ IsExiting = true ;
55+
56+ Log . Information ( "Application is exiting." ) ;
4157 Log . CloseAndFlush ( ) ;
42-
43- // Clean up temp files
44- var serviceProvider = Extensions . ServiceExtensions . Configuration ( ) ;
45- var fileHandlerService = serviceProvider . GetRequiredService < IFileHandlerService > ( ) ;
58+
59+ var fileHandlerService = _serviceProvider . GetRequiredService < IFileHandlerService > ( ) ;
4660 fileHandlerService . CleanUpTempFiles ( ) ;
61+
62+ if ( _serviceProvider is IDisposable disposable )
63+ {
64+ disposable . Dispose ( ) ;
65+ }
66+
67+ Application . Exit ( ) ;
4768 }
48-
49- private static void HandleFileArgs ( IServiceProvider serviceProvider , string filePath )
69+
70+ private static void HandleFileArgs ( string filePath )
5071 {
5172 try
5273 {
5374 Log . Information ( $ "Running application with file: { filePath } ") ;
54-
75+
5576 var allowedExtensions = new [ ] { ".pmp" , ".ttmp2" , ".ttmp" } ;
5677 if ( ! allowedExtensions . Contains ( Path . GetExtension ( filePath ) ) )
5778 {
5879 Log . Error ( $ "File '{ filePath } ' is not a valid mod file. Aborting.") ;
5980 return ;
6081 }
6182
62- var installService = serviceProvider . GetRequiredService < IPenumbraInstallerService > ( ) ;
63-
83+ var installService = _serviceProvider . GetRequiredService < IPenumbraInstallerService > ( ) ;
84+
6485 Log . Information ( "Starting mod installation..." ) ;
6586 var result = installService . InstallMod ( filePath ) ;
6687
@@ -69,53 +90,54 @@ private static void HandleFileArgs(IServiceProvider serviceProvider, string file
6990 Log . Error ( "Mod installation failed." ) ;
7091 return ;
7192 }
72-
93+
7394 Log . Information ( "Mod installed successfully." ) ;
74-
75- var systemTrayService = serviceProvider . GetRequiredService < ISystemTrayManager > ( ) ;
95+
96+ var systemTrayService = _serviceProvider . GetRequiredService < ISystemTrayManager > ( ) ;
7697 Log . Information ( "Triggering exit process..." ) ;
7798 systemTrayService . TriggerExit ( ) ;
78-
99+
79100 Log . Information ( "Exiting application..." ) ;
80- Environment . Exit ( 0 ) ;
101+ ExitApplication ( ) ;
81102 }
82103 catch ( Exception ex )
83104 {
84105 Log . Error ( ex , "An error occurred during the file handling process." ) ;
85106 MessageBox . Show ( $ "An unexpected error occurred: { ex . Message } ", "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
107+ ExitApplication ( ) ;
86108 }
87109 }
88-
89- private static void MigrateOldConfigIfExists ( IServiceProvider serviceProvider )
110+
111+ private static void MigrateOldConfigIfExists ( )
90112 {
91- var configurationService = serviceProvider . GetRequiredService < IConfigurationService > ( ) ;
113+ var configurationService = _serviceProvider . GetRequiredService < IConfigurationService > ( ) ;
92114 configurationService . MigrateOldConfig ( ) ;
93115 }
94-
95- private static void CheckForUpdates ( IServiceProvider serviceProvider )
116+
117+ private static void CheckForUpdates ( )
96118 {
97- var updateService = serviceProvider . GetRequiredService < IUpdateService > ( ) ;
119+ var updateService = _serviceProvider . GetRequiredService < IUpdateService > ( ) ;
98120 updateService . CheckForUpdates ( ) ;
99121 }
100-
101- private static void CreateStartMenuShortcut ( IServiceProvider serviceProvider )
122+
123+ private static void CreateStartMenuShortcut ( )
102124 {
103- var shortcutService = serviceProvider . GetRequiredService < IShortcutService > ( ) ;
125+ var shortcutService = _serviceProvider . GetRequiredService < IShortcutService > ( ) ;
104126 shortcutService . CreateShortcutInStartMenus ( ) ;
105127 }
106-
107- private static void SetTexToolsPath ( IServiceProvider serviceProvider )
128+
129+ private static void SetTexToolsPath ( )
108130 {
109- var texToolsHelper = serviceProvider . GetRequiredService < ITexToolsHelper > ( ) ;
131+ var texToolsHelper = _serviceProvider . GetRequiredService < ITexToolsHelper > ( ) ;
110132 texToolsHelper . SetTexToolsConsolePath ( ) ;
111133 }
112134
113- private static void IsProgramAlreadyRunning ( IServiceProvider serviceProvider )
135+ private static void IsProgramAlreadyRunning ( )
114136 {
115- var processHelperService = serviceProvider . GetRequiredService < IProcessHelperService > ( ) ;
137+ var processHelperService = _serviceProvider . GetRequiredService < IProcessHelperService > ( ) ;
116138 var result = processHelperService . IsApplicationAlreadyOpen ( ) ;
117139 if ( ! result ) return ;
118140 MessageBox . Show ( "An instance of Penumbra Mod Forwarder is already running." , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
119- Environment . Exit ( 0 ) ;
141+ ExitApplication ( ) ;
120142 }
121143}
0 commit comments