11using System . Text ;
22using GeneralUpdate . ClientCore ;
3- using GeneralUpdate . Core . Bootstrap ;
4- using GeneralUpdate . Core . Domain . Entity ;
5- using GeneralUpdate . Core . Domain . Enum ;
6- using GeneralUpdate . Core . Events . CommonArgs ;
7- using GeneralUpdate . Core . Events . MultiEventArgs ;
8- using GeneralUpdate . Core . Strategys . PlatformWindows ;
3+ using GeneralUpdate . Common . Download ;
4+ using GeneralUpdate . Common . Internal ;
5+ using GeneralUpdate . Common . Internal . Bootstrap ;
6+ using GeneralUpdate . Common . Shared . Object ;
97
10- //ClientStrategy该更新策略将完成1.自动升级组件自更新 2.启动更新组件 3.配置好ClientParameter无需再像之前的版本写args数组进程通讯了。
11- //generalClientBootstrap.Config(baseUrl, "B8A7FADD-386C-46B0-B283-C9F963420C7C").
12-
13- var baseUrl = "127.0.0.1" ;
14- var configinfo = GetWindowsConfigInfo ( ) ;
15- var generalClientBootstrap = await new GeneralClientBootstrap ( )
16- //单个或多个更新包下载通知事件
17- . AddListenerMultiDownloadProgress ( OnMultiDownloadProgressChanged )
18- //单个或多个更新包下载速度、剩余下载事件、当前下载版本信息通知事件
19- . AddListenerMultiDownloadStatistics ( OnMultiDownloadStatistics )
20- //单个或多个更新包下载完成
21- . AddListenerMultiDownloadCompleted ( OnMultiDownloadCompleted )
22- //完成所有的下载任务通知
23- . AddListenerMultiAllDownloadCompleted ( OnMultiAllDownloadCompleted )
24- //下载过程出现的异常通知
25- . AddListenerMultiDownloadError ( OnMultiDownloadError )
26- //整个更新过程出现的任何问题都会通过这个事件通知
27- . AddListenerException ( OnException )
28- . Config ( configinfo )
29- . Option ( UpdateOption . DownloadTimeOut , 60 )
30- . Option ( UpdateOption . Encoding , Encoding . Default )
31- . Option ( UpdateOption . Format , Format . ZIP )
32- //开启驱动更新
33- //.Option(UpdateOption.Drive, true)
34- //开启遗言功能,需要部署GeneralUpdate.SystemService Windows服务。
35- //.Option(UpdateOption.WillMessage, true)
36- . Strategy < WindowsStrategy > ( )
37- //注入一个func让用户决定是否跳过本次更新,如果是强制更新则不生效
38- //.SetCustomSkipOption(ShowCustomOption)
39- //注入一个自定义方法集合,该集合会在更新启动前执行。执行自定义方法列表如果出现任何异常,将通过异常订阅通知。(推荐在更新之前检查当前软件环境)
40- //.AddCustomOption(new List<Func<bool>>() { () => Check1(), () => Check2() })
41- //默认黑名单文件: { "Newtonsoft.Json.dll" } 默认黑名单文件扩展名: { ".patch", ".7z", ".zip", ".rar", ".tar" , ".json" }
42- //如果不需要扩展,需要重新传入黑名单集合来覆盖。
43- //.SetBlacklist(GetBlackFiles(), GetBlackFormats())
44- . LaunchTaskAsync ( ) ;
45-
46- List < string > GetBlackFiles ( )
47- {
48- var blackFiles = new List < string > ( ) ;
49- blackFiles . Add ( "MainApp" ) ;
50- return blackFiles ;
51- }
52-
53- List < string > GetBlackFormats ( )
54- {
55- var blackFormats = new List < string > ( ) ;
56- blackFormats . Add ( ".zip" ) ;
57- return blackFormats ;
58- }
59-
60- /// <summary>
61- /// 获取Windows平台所需的配置参数
62- /// </summary>
63- /// <returns></returns>
64- Configinfo GetWindowsConfigInfo ( )
8+ try
659{
66- //该对象用于主程序客户端与更新组件进程之间交互用的对象
67- var config = new Configinfo ( ) ;
68- //本机的客户端程序应用地址
69- config . InstallPath = @"D:\packet\source" ;
70- //更新公告网页
71- config . UpdateLogUrl = "https://www.justerzhu.cn/" ;
72- //客户端当前版本号
73- config . ClientVersion = "1.1.1.1" ;
74- //客户端类型:1.主程序客户端 2.更新组件
75- config . AppType = AppType . UpgradeApp ;
76- //指定应用密钥,用于区分客户端应用
77- config . AppSecretKey = "B8A7FADD-386C-46B0-B283-C9F963420C7C" ;
78- //更新组件更新包下载地址
79- config . UpdateUrl = $ "{ baseUrl } /versions/{ config . AppType } /{ config . ClientVersion } /{ config . AppSecretKey } ";
80- //更新程序exe名称
81- config . AppName = "GeneralUpdate.Core" ;
82- //主程序客户端exe名称
83- config . MainAppName = "GeneralUpdate.ClientCore" ;
84- //主程序信息
85- var mainVersion = "1.1.1.1" ;
86- //主程序客户端更新包下载地址
87- config . MainUpdateUrl = $ "{ baseUrl } /versions/{ AppType . ClientApp } /{ mainVersion } /{ config . AppSecretKey } ";
88- return config ;
10+ Console . WriteLine ( $ "主程序初始化,{ DateTime . Now } !") ;
11+ Console . WriteLine ( "当前运行目录:" + Thread . GetDomain ( ) . BaseDirectory ) ;
12+ await Task . Delay ( 2000 ) ;
13+ var configinfo = new Configinfo
14+ {
15+ //configinfo.UpdateLogUrl = "https://www.baidu.com";
16+ ReportUrl = "http://127.0.0.1:5000/Report" ,
17+ UpdateUrl = "http://127.0.0.1:5000/Verification" ,
18+ AppName = "GeneralUpdate.Upgrad.exe" ,
19+ MainAppName = "GeneralUpdate.Client.exe" ,
20+ InstallPath = Thread . GetDomain ( ) . BaseDirectory ,
21+ //configinfo.Bowl = "Generalupdate.CatBowl.exe";
22+ //当前客户端的版本号
23+ ClientVersion = "1.0.0.0" ,
24+ //当前升级端的版本号
25+ UpgradeClientVersion = "1.0.0.0" ,
26+ //产品id
27+ ProductId = "2d974e2a-31e6-4887-9bb1-b4689e98c77a" ,
28+ //应用密钥
29+ AppSecretKey = "dfeb5833-975e-4afb-88f1-6278ee9aeff6"
30+ } ;
31+ _ = await new GeneralClientBootstrap ( ) //单个或多个更新包下载通知事件
32+ //单个或多个更新包下载速度、剩余下载事件、当前下载版本信息通知事件
33+ . AddListenerMultiDownloadStatistics ( OnMultiDownloadStatistics )
34+ //单个或多个更新包下载完成
35+ . AddListenerMultiDownloadCompleted ( OnMultiDownloadCompleted )
36+ //完成所有的下载任务通知
37+ . AddListenerMultiAllDownloadCompleted ( OnMultiAllDownloadCompleted )
38+ //下载过程出现的异常通知
39+ . AddListenerMultiDownloadError ( OnMultiDownloadError )
40+ //整个更新过程出现的任何问题都会通过这个事件通知
41+ . AddListenerException ( OnException )
42+ . SetConfig ( configinfo )
43+ . Option ( UpdateOption . DownloadTimeOut , 60 )
44+ . Option ( UpdateOption . Encoding , Encoding . Default )
45+ . LaunchAsync ( ) ;
46+ Console . WriteLine ( $ "主程序已启动,{ DateTime . Now } !") ;
8947}
90-
91- /// <summary>
92- /// 获取Android平台所需要的参数
93- /// </summary>
94- /// <returns></returns>
95- Configinfo GetAndroidConfigInfo ( )
96- {
97- var config = new Configinfo ( ) ;
98- config . InstallPath = System . Threading . Thread . GetDomain ( ) . BaseDirectory ;
99- //主程序客户端当前版本号
100- config . ClientVersion = "1.0.0.0" ; //VersionTracking.Default.CurrentVersion.ToString();
101- config . AppType = AppType . ClientApp ;
102- config . AppSecretKey = "41A54379-C7D6-4920-8768-21A3468572E5" ;
103- //主程序客户端exe名称
104- config . MainAppName = "GeneralUpdate.ClientCore" ;
105- //主程序信息
106- var mainVersion = "1.1.1.1" ;
107- config . MainUpdateUrl = $ "{ baseUrl } /versions/{ AppType . ClientApp } /{ mainVersion } /{ config . AppSecretKey } ";
108- return config ;
109- }
110-
111- /// <summary>
112- /// 让用户决定是否跳过本次更新
113- /// </summary>
114- /// <returns></returns>
115- async Task < bool > ShowCustomOption ( )
48+ catch ( Exception e )
11649{
117- return await Task . FromResult ( true ) ;
50+ Console . WriteLine ( e . Message + " \n " + e . StackTrace ) ;
11851}
11952
120- void OnMultiDownloadStatistics ( object sender , MultiDownloadStatisticsEventArgs e )
53+ void OnMultiDownloadError ( object arg1 , MultiDownloadErrorEventArgs arg2 )
12154{
122- //e.Remaining 剩余下载时间
123- //e.Speed 下载速度
124- //e.Version 当前下载的版本信息
125- Console . WriteLine ( $ "{ e . Version } download speed { e . Speed } bytes/s. Remaining { e . Remaining } ") ;
55+ var version = arg2 . Version as VersionInfo ;
56+ Console . WriteLine ( $ "{ version . Version } { arg2 . Exception } ") ;
12657}
12758
128- void OnMultiDownloadProgressChanged ( object sender , MultiDownloadProgressChangedEventArgs e )
59+ void OnMultiAllDownloadCompleted ( object arg1 , MultiAllDownloadCompletedEventArgs arg2 )
12960{
130- //e.TotalBytesToReceive 当前更新包需要下载的总大小
131- //e.ProgressValue 当前进度值
132- //e.ProgressPercentage 当前进度的百分比
133- //e.Version 当前下载的版本信息
134- //e.Type 当前正在执行的操作 1.ProgressType.Check 检查版本信息中 2.ProgressType.Donwload 正在下载当前版本 3. ProgressType.Updatefile 更新当前版本 4. ProgressType.Done更新完成 5.ProgressType.Fail 更新失败
135- //e.BytesReceived 已下载大小
136- Console . WriteLine ( $ "{ e . ProgressPercentage } %") ;
61+ Console . WriteLine ( arg2 . IsAllDownloadCompleted ? "所有的下载任务已完成!" : $ "下载任务已失败!{ arg2 . FailedVersions . Count } ") ;
13762}
13863
139- void OnException ( object sender , ExceptionEventArgs e )
64+ void OnMultiDownloadCompleted ( object arg1 , MultiDownloadCompletedEventArgs arg2 )
14065{
141- Console . WriteLine ( e . Exception . Message ) ;
66+ var version = arg2 . Version as VersionInfo ;
67+ Console . WriteLine ( arg2 . IsComplated ? $ "当前下载版本:{ version . Version } , 下载完成!" : $ "当前下载版本:{ version . Version } , 下载失败!") ;
14268}
14369
144- void OnMultiAllDownloadCompleted ( object sender , MultiAllDownloadCompletedEventArgs e )
70+ void OnMultiDownloadStatistics ( object arg1 , MultiDownloadStatisticsEventArgs arg2 )
14571{
146- //e.FailedVersions; 如果出现下载失败则会把下载错误的版本、错误原因统计到该集合当中。
147- Console . WriteLine ( $ "Is all download completed { e . IsAllDownloadCompleted } .") ;
72+ var version = arg2 . Version as VersionInfo ;
73+ Console . WriteLine (
74+ $ "当前下载版本:{ version . Version } ,下载速度:{ arg2 . Speed } ,剩余下载时间:{ arg2 . Remaining } ,已下载大小:{ arg2 . BytesReceived } ,总大小:{ arg2 . TotalBytesToReceive } , 进度百分比:{ arg2 . ProgressPercentage } %") ;
14875}
14976
150- void OnMultiDownloadCompleted ( object sender , MultiDownloadCompletedEventArgs e )
77+ void OnException ( object arg1 , ExceptionEventArgs arg2 )
15178{
152- var info = e . Version as VersionInfo ;
153- Console . WriteLine ( $ "{ info . Name } download completed.") ;
154- }
155-
156- void OnMultiDownloadError ( object sender , MultiDownloadErrorEventArgs e )
157- {
158- var info = e . Version as VersionInfo ;
159- Console . WriteLine ( $ "{ info . Name } error!") ;
160- }
79+ Console . WriteLine ( $ "{ arg2 . Exception } ") ;
80+ }
0 commit comments