1- using SingleInstanceCore ;
1+ using System . Diagnostics ;
2+ using SingleInstanceCore ;
23using System . Text . Json ;
4+ using Windows . ApplicationModel ;
5+ using Windows . ApplicationModel . Activation ;
36using MaiChartManager . Controllers . Music ;
7+ using Microsoft . Web . WebView2 . Core ;
48using Xabe . FFmpeg ;
59
610namespace MaiChartManager ;
@@ -49,7 +53,12 @@ public void Run()
4953 {
5054 try
5155 {
52- StaticSettings . Config = JsonSerializer . Deserialize < Config > ( File . ReadAllText ( Path . Combine ( StaticSettings . appData , "config.json" ) ) ) ;
56+ var cfg = JsonSerializer . Deserialize < Config > ( File . ReadAllText ( Path . Combine ( StaticSettings . appData , "config.json" ) ) ) ;
57+ if ( cfg == null )
58+ {
59+ throw new Exception ( "config.json is null" ) ;
60+ }
61+ StaticSettings . Config = cfg ;
5362 }
5463 catch ( Exception e )
5564 {
@@ -59,6 +68,23 @@ public void Run()
5968 }
6069 }
6170
71+ string ? availableVersion = null ;
72+ try
73+ {
74+ availableVersion = CoreWebView2Environment . GetAvailableBrowserVersionString ( ) ;
75+ }
76+ catch ( WebView2RuntimeNotFoundException ) { }
77+
78+ if ( availableVersion == null && ! IsFromStartup )
79+ {
80+ var answer = MessageBox . Show ( "WebView2 运行时未安装,在启动之后可能会白屏…\n \n 如果你觉得你已经安装了 WebView2 运行时,请尝试重启电脑。\n \n 要尝试安装一下 WebView2 吗?" , "WebView2 未安装" , MessageBoxButtons . YesNo ,
81+ MessageBoxIcon . Warning ) ;
82+ if ( answer == DialogResult . Yes )
83+ {
84+ Process . Start ( new ProcessStartInfo ( Path . Combine ( StaticSettings . exeDir , "MicrosoftEdgeWebview2Setup.exe" ) ) { UseShellExecute = true } ) ;
85+ }
86+ }
87+
6288 IapManager . Init ( ) ;
6389
6490 _launcher = new Launcher ( ) ;
@@ -73,6 +99,31 @@ public void Run()
7399 }
74100 }
75101
102+ private static bool ? _isFromStartup ;
103+
104+ public static bool IsFromStartup
105+ {
106+ get
107+ {
108+ if ( _isFromStartup . HasValue )
109+ return _isFromStartup . Value ;
110+ try
111+ {
112+ var aeArgs = AppInstance . GetActivatedEventArgs ( ) ;
113+ _isFromStartup = aeArgs ? . Kind == ActivationKind . StartupTask ;
114+ return _isFromStartup . Value ;
115+ }
116+ catch ( Exception e )
117+ {
118+ Console . WriteLine ( e ) ;
119+ SentrySdk . CaptureException ( e ) ;
120+ }
121+
122+ _isFromStartup = false ;
123+ return false ;
124+ }
125+ }
126+
76127 public void OnInstanceInvoked ( string [ ] args )
77128 {
78129 _launcher . ShowWindow ( ) ;
0 commit comments