1+ using PCL . Core . App . Updates . Sources ;
2+ using PCL . Core . UI ;
3+ using System ;
4+ using System . IO ;
5+ using System . Threading . Tasks ;
6+
7+ namespace PCL . Core . App . Updates ;
8+
9+ [ LifecycleService ( LifecycleState . Running ) ]
10+ [ LifecycleScope ( "check-update" , "检查更新" ) ]
11+ public sealed partial class CheckUpdateService
12+ {
13+ private static readonly SourceController _SourceController = new ( [
14+ new UpdateMinioSource ( "https://s3.pysio.online/pcl2-ce/" , "Pysio" ) ,
15+ new UpdateMinioSource ( "https://staticassets.naids.com/resources/pclce/" , "Naids" )
16+ ] ) ;
17+
18+ public static VersionData ? LatestVersion { get ; private set ; }
19+
20+ public static bool IsUpdateDownloaded { get ; private set ; }
21+
22+ [ LifecycleStart ]
23+ private static async Task _Start ( )
24+ {
25+ if ( Config . System . Update . UpdateMode == 3 )
26+ {
27+ Context . Info ( "更新模式为禁用,跳过检查" ) ;
28+ return ;
29+ }
30+
31+ Context . Info ( "检查更新中..." ) ;
32+ if ( ! await TryCheckUpdate ( ) || LatestVersion is null ) return ;
33+
34+ if ( ! LatestVersion . IsAvailable )
35+ {
36+ Context . Info ( "已经是最新版本,跳过更新" ) ;
37+ return ;
38+ }
39+
40+ Context . Info ( $ "发现新版本: { LatestVersion . Version . Code } , 准备更新") ;
41+
42+ if ( Config . System . Update . UpdateMode == 2 && ! _PromptUpdate ( ) ) return ;
43+
44+ if ( ! await TryDownloadUpdate ( ) ) return ;
45+
46+ if ( Config . System . Update . UpdateMode == 1 && ! _PromptInstall ( ) ) return ;
47+
48+ Context . Info ( "准备重启并安装..." ) ;
49+ UpdateHelper . Restart ( true , true ) ;
50+ }
51+
52+ #region Public Methods
53+
54+ public static async Task < bool > TryCheckUpdate ( )
55+ {
56+ try
57+ {
58+ LatestVersion = await _SourceController . CheckUpdateAsync ( ) . ConfigureAwait ( false ) ;
59+ return true ;
60+ }
61+ catch ( InvalidOperationException ex )
62+ {
63+ if ( ex . Message . Contains ( "不可用" ) )
64+ {
65+ Context . Warn ( "所有更新源均不可用" , ex ) ;
66+ HintWrapper . Show ( "所有更新源均不可用,可能是网络问题" , HintTheme . Error ) ;
67+ }
68+ else
69+ {
70+ Context . Warn ( "检查更新时发生未知异常" , ex ) ;
71+ HintWrapper . Show ( "检查更新时发生未知异常,可能是网络问题" , HintTheme . Error ) ;
72+ }
73+ }
74+ catch ( Exception ex )
75+ {
76+ Context . Warn ( "检查更新时发生未知异常" , ex ) ;
77+ HintWrapper . Show ( "检查更新时发生未知异常,可能是网络问题" , HintTheme . Error ) ;
78+ }
79+ return false ;
80+ }
81+
82+ public static async Task < bool > TryDownloadUpdate ( )
83+ {
84+ Context . Info ( "下载更新包中..." ) ;
85+ try
86+ {
87+ var outputPath = Path . Combine (
88+ Basics . ExecutableDirectory ,
89+ "PCL" ,
90+ "Plain Craft Launcher Community Edition.exe" ) ;
91+ if ( LatestVersion == null ) return false ;
92+ await _SourceController . DownloadAsync ( outputPath ) . ConfigureAwait ( false ) ;
93+ Context . Info ( "更新包下载完成" ) ;
94+ IsUpdateDownloaded = true ;
95+ return true ;
96+ }
97+ catch ( InvalidOperationException ex )
98+ {
99+ if ( ex . Message . Contains ( "不可用" ) )
100+ {
101+ Context . Warn ( "所有更新源均不可用" , ex ) ;
102+ HintWrapper . Show ( "所有更新源均不可用,可能是网络问题" , HintTheme . Error ) ;
103+ }
104+ else
105+ {
106+ Context . Warn ( "下载更新包时发生未知异常" , ex ) ;
107+ HintWrapper . Show ( "下载更新包时发生未知异常,可能是网络问题" , HintTheme . Error ) ;
108+ }
109+ }
110+ catch ( Exception ex )
111+ {
112+ Context . Warn ( "下载更新包时发生未知异常" , ex ) ;
113+ HintWrapper . Show ( "下载更新包时发生未知异常,可能是网络问题" , HintTheme . Error ) ;
114+ }
115+ return false ;
116+ }
117+
118+ #endregion
119+
120+ #region Prompt Wrappers
121+
122+ private static bool _PromptUpdate ( )
123+ {
124+ if ( LatestVersion == null ) return false ;
125+
126+ if ( MsgBoxWrapper . Show (
127+ $ "启动器有新版本可用 ({ Basics . VersionName } -> { LatestVersion . Version . Name } )\r \n " +
128+ $ "是否立即下载并安装?\r \n " +
129+ "你也可以稍后在 设置 -> 检查更新 界面中更新。" ,
130+ "发现新版本" , MsgBoxTheme . Info , true , "立刻更新" , "以后再说" ) == 1 ) return true ;
131+
132+ Context . Info ( "用户取消更新" ) ;
133+ return false ;
134+ }
135+
136+ private static bool _PromptInstall ( )
137+ {
138+ if ( LatestVersion == null ) return false ;
139+ if ( ! IsUpdateDownloaded ) return false ;
140+
141+ if ( MsgBoxWrapper . Show (
142+ $ "启动器有新版本可用 ({ Basics . VersionName } -> { LatestVersion . Version . Name } )\r \n " +
143+ $ "已自动下载,是否立即安装?\r \n " +
144+ "你也可以稍后在 设置 -> 检查更新 界面中安装。" ,
145+ "发现新版本" , MsgBoxTheme . Info , true , "立刻更新" , "以后再说" ) == 1 ) return true ;
146+
147+ Context . Info ( "用户取消安装" ) ;
148+ return false ;
149+ }
150+
151+ #endregion
152+ }
0 commit comments