22// LGPL-3.0-or-later (see file COPYING and COPYING.LESSER)
33
44using System . Windows ;
5- using CommunityToolkit . Mvvm . Messaging ;
65using Microsoft . Extensions . DependencyInjection ;
76using Microsoft . Extensions . Hosting ;
87using NLog ;
98using StreamDeckSimHub . Plugin . ActionEditor ;
109using StreamDeckSimHub . Plugin . Actions . GenericButton . Model ;
1110using StreamDeckSimHub . Plugin . SimHub ;
1211using StreamDeckSimHub . Plugin . Tools ;
12+ using StreamDeckSimHub . Plugin . Tools . AutoUpdate ;
1313
1414namespace StreamDeckSimHub . Plugin ;
1515
@@ -25,7 +25,38 @@ public App()
2525 var localDevMode = Environment . GetCommandLineArgs ( ) . Length == 2 && Environment . GetCommandLineArgs ( ) [ 1 ] == "dev" ;
2626 _host = Program . CreateHost ( localDevMode ) ;
2727 LogManager . GetCurrentClassLogger ( ) . Info ( "Starting StreamDeckSimHub plugin {version}" , ThisAssembly . AssemblyFileVersion ) ;
28+ }
29+
30+ private async void Application_Startup ( object sender , StartupEventArgs e )
31+ {
32+ try
33+ {
34+ var simHubConnection = _host . Services . GetRequiredService < ISimHubConnection > ( ) ;
35+ if ( simHubConnection is SimHubConnection shc )
36+ {
37+ shc . Run ( ) ;
38+ }
39+ await _host . StartAsync ( ) ;
40+ }
41+ catch ( Exception ex )
42+ {
43+ LogManager . GetCurrentClassLogger ( ) . Error ( ex , "Error during application startup" ) ;
44+ }
45+
46+
47+ var localDevMode = Environment . GetCommandLineArgs ( ) . Length == 2 && Environment . GetCommandLineArgs ( ) [ 1 ] == "dev" ;
48+
49+ if ( localDevMode )
50+ {
51+ UpdateStatus . LatestVersion = new Version ( "99.99.1" ) ;
52+ }
53+ else
54+ {
55+ // Run version check in the background. We use it only in the GenericButtonEditor, which is not yet opened.
56+ _ = GetLatestVersionAsync ( ) ;
57+ }
2858
59+ // Developer mode: Open a Generic Button Editor directly for testing
2960 if ( localDevMode )
3061 {
3162 var settings = new Settings
@@ -47,29 +78,28 @@ public App()
4778 }
4879 }
4980
50- private async void Application_Startup ( object sender , StartupEventArgs e )
81+ private async void Application_Exit ( object sender , ExitEventArgs e )
5182 {
52- try
53- {
54- var simHubConnection = _host . Services . GetRequiredService < ISimHubConnection > ( ) ;
55- if ( simHubConnection is SimHubConnection shc )
56- {
57- shc . Run ( ) ;
58- }
59- WeakReferenceMessenger . Default . RegisterAll ( this ) ;
60- await _host . StartAsync ( ) ;
61- }
62- catch ( Exception ex )
83+ using ( _host )
6384 {
64- LogManager . GetCurrentClassLogger ( ) . Error ( ex , "Error during application startup" ) ;
85+ await _host . StopAsync ( ) ;
6586 }
6687 }
6788
68- private async void Application_Exit ( object sender , ExitEventArgs e )
89+ private async Task GetLatestVersionAsync ( )
6990 {
70- using ( _host )
91+ try
7192 {
72- await _host . StopAsync ( ) ;
93+ var updater = _host . Services . GetRequiredService < AutoUpdater > ( ) ;
94+ var versionInfo = await updater . GetLatestVersion ( ) ;
95+ UpdateStatus . LatestVersion = new Version ( versionInfo . TagName ) ;
96+ UpdateStatus . LatestVersionException = null ;
97+ }
98+ catch ( Exception ex )
99+ {
100+ LogManager . GetCurrentClassLogger ( ) . Error ( $ "Error checking for new version: { ex . Message } ") ;
101+ UpdateStatus . LatestVersion = null ;
102+ UpdateStatus . LatestVersionException = ex ;
73103 }
74104 }
75105}
0 commit comments