|
1 | | -using System.Text; |
| 1 | +using System.Diagnostics; |
2 | 2 | using GeneralUpdate.ClientCore; |
3 | 3 | using GeneralUpdate.Common.Download; |
4 | 4 | using GeneralUpdate.Common.Internal; |
5 | 5 | using GeneralUpdate.Common.Internal.Bootstrap; |
6 | 6 | using GeneralUpdate.Common.Shared.Object; |
7 | 7 |
|
8 | | -try |
9 | | -{ |
10 | | - Console.WriteLine($"主程序初始化,{DateTime.Now}!"); |
11 | | - Console.WriteLine("当前运行目录:" + Thread.GetDomain().BaseDirectory); |
12 | | - var configinfo = new Configinfo |
13 | | - { |
14 | | - //UpdateLogUrl = "https://www.baidu.com", |
15 | | - ReportUrl = "http://127.0.0.1:5000/Upgrade/Report", |
16 | | - UpdateUrl = "http://127.0.0.1:5000/Upgrade/Verification", |
17 | | - AppName = "UpgradeSample.exe", |
18 | | - MainAppName = "ClientSample.exe", |
19 | | - InstallPath = Thread.GetDomain().BaseDirectory, |
20 | | - //Bowl = "Generalupdate.CatBowl.exe", |
21 | | - //当前客户端的版本号 |
22 | | - ClientVersion = "1.0.0.0", |
23 | | - //当前升级端的版本号 |
24 | | - UpgradeClientVersion = "1.0.0.0", |
25 | | - //产品id |
26 | | - ProductId = "2d974e2a-31e6-4887-9bb1-b4689e98c77a", |
27 | | - //应用密钥 |
28 | | - AppSecretKey = "dfeb5833-975e-4afb-88f1-6278ee9aeff6", |
29 | | - //BlackFiles = new List<string> { "123.exe" }, |
30 | | - //BlackFormats = new List<string> { "123.dll" }, |
31 | | - //SkipDirectorys = new List<string> { "logs" }, |
32 | | - //Scheme = "Bearer", |
33 | | - //Token = "..." |
34 | | - }; |
35 | | - _ = await new GeneralClientBootstrap() |
36 | | - //单个或多个更新包下载速度、剩余下载事件、当前下载版本信息通知事件 |
37 | | - .AddListenerMultiDownloadStatistics(OnMultiDownloadStatistics) |
38 | | - //单个或多个更新包下载完成 |
39 | | - .AddListenerMultiDownloadCompleted(OnMultiDownloadCompleted) |
40 | | - //完成所有的下载任务通知 |
41 | | - .AddListenerMultiAllDownloadCompleted(OnMultiAllDownloadCompleted) |
42 | | - //下载过程出现的异常通知 |
43 | | - .AddListenerMultiDownloadError(OnMultiDownloadError) |
44 | | - //整个更新过程出现的任何问题都会通过这个事件通知 |
45 | | - .AddListenerException(OnException) |
46 | | - //服务端返回更新信息后的通知(可用于显示更新日志、版本信息等) |
47 | | - .AddListenerUpdateInfo(OnUpdateInfo) |
48 | | - //更新预检回调:返回 true 跳过本次更新,返回 false 继续自动更新 |
49 | | - //(强制更新版本会忽略此回调) |
50 | | - .AddListenerUpdatePrecheck(OnUpdatePrecheck) |
51 | | - .SetConfig(configinfo) |
52 | | - .Option(UpdateOption.DownloadTimeOut, 60) |
53 | | - .Option(UpdateOption.Encoding, Encoding.Default) |
54 | | - .LaunchAsync(); |
55 | | - Console.WriteLine($"主程序已启动,{DateTime.Now}!"); |
56 | | - await Task.Delay(2000); |
57 | | -} |
58 | | -catch (Exception e) |
59 | | -{ |
60 | | - Console.WriteLine(e.Message + "\n" + e.StackTrace); |
61 | | -} |
| 8 | +// Parse command-line arguments |
| 9 | +var cliArgs = ParseArgs(args); |
62 | 10 |
|
63 | | -void OnMultiDownloadError(object arg1, MultiDownloadErrorEventArgs arg2) |
64 | | -{ |
65 | | - var version = arg2.Version as VersionInfo; |
66 | | - Console.WriteLine($"{version?.Version} {arg2.Exception}"); |
67 | | -} |
| 11 | +Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Client started"); |
| 12 | +Console.WriteLine($"Install path: {cliArgs.InstallPath}"); |
68 | 13 |
|
69 | | -void OnMultiAllDownloadCompleted(object arg1, MultiAllDownloadCompletedEventArgs arg2) |
| 14 | +var configinfo = new Configinfo |
70 | 15 | { |
71 | | - Console.WriteLine(arg2.IsAllDownloadCompleted ? "所有的下载任务已完成!" : $"下载任务已失败!{arg2.FailedVersions.Count}"); |
72 | | -} |
| 16 | + ReportUrl = $"{cliArgs.ServerUrl}/Upgrade/Report", |
| 17 | + UpdateUrl = $"{cliArgs.ServerUrl}/Upgrade/Verification", |
| 18 | + AppName = cliArgs.AppName, |
| 19 | + MainAppName = AppDomain.CurrentDomain.FriendlyName, |
| 20 | + InstallPath = cliArgs.InstallPath, |
| 21 | + ClientVersion = cliArgs.CurrentVersion, |
| 22 | + UpgradeClientVersion = "1.0.0.0", |
| 23 | + ProductId = cliArgs.ProductId, |
| 24 | + AppSecretKey = cliArgs.AppSecret, |
| 25 | +}; |
73 | 26 |
|
74 | | -void OnMultiDownloadCompleted(object arg1, MultiDownloadCompletedEventArgs arg2) |
| 27 | +try |
75 | 28 | { |
76 | | - var version = arg2.Version as VersionInfo; |
77 | | - Console.WriteLine(arg2.IsComplated ? $"当前下载版本:{version?.Version}, 下载完成!" : $"当前下载版本:{version?.Version}, 下载失败!"); |
78 | | -} |
| 29 | + await new GeneralClientBootstrap() |
| 30 | + .AddListenerMultiDownloadStatistics((_, e) => |
| 31 | + { |
| 32 | + var v = e.Version as VersionInfo; |
| 33 | + Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Download: {v?.Version} {e.ProgressPercentage}%"); |
| 34 | + }) |
| 35 | + .AddListenerMultiAllDownloadCompleted((_, e) => |
| 36 | + { |
| 37 | + Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] All downloads: {(e.IsAllDownloadCompleted ? "completed" : $"failed ({e.FailedVersions.Count})")}"); |
| 38 | + }) |
| 39 | + .AddListenerMultiDownloadCompleted((_, e) => |
| 40 | + { |
| 41 | + var v = e.Version as VersionInfo; |
| 42 | + Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Download {v?.Version}: {(e.IsComplated ? "done" : "failed")}"); |
| 43 | + }) |
| 44 | + .AddListenerException((_, e) => |
| 45 | + { |
| 46 | + Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] ERROR: {e.Exception}"); |
| 47 | + }) |
| 48 | + .AddListenerUpdateInfo((_, e) => |
| 49 | + { |
| 50 | + Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Update info: Code={e.Info.Code}, Versions={e.Info.Body?.Count ?? 0}"); |
| 51 | + }) |
| 52 | + .SetConfig(configinfo) |
| 53 | + .Option(UpdateOption.DownloadTimeOut, 60) |
| 54 | + .LaunchAsync(); |
79 | 55 |
|
80 | | -void OnMultiDownloadStatistics(object arg1, MultiDownloadStatisticsEventArgs arg2) |
81 | | -{ |
82 | | - var version = arg2.Version as VersionInfo; |
83 | | - Console.WriteLine( |
84 | | - $"当前下载版本:{version?.Version},下载速度:{arg2.Speed},剩余下载时间:{arg2.Remaining},已下载大小:{arg2.BytesReceived},总大小:{arg2.TotalBytesToReceive}, 进度百分比:{arg2.ProgressPercentage}%"); |
| 56 | + Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Client completed successfully"); |
85 | 57 | } |
86 | | - |
87 | | -void OnException(object arg1, ExceptionEventArgs arg2) |
| 58 | +catch (Exception ex) |
88 | 59 | { |
89 | | - Console.WriteLine($"{arg2.Exception}"); |
| 60 | + Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] FATAL: {ex.Message}"); |
| 61 | + Environment.Exit(1); |
90 | 62 | } |
91 | 63 |
|
92 | | -void OnUpdateInfo(object arg1, UpdateInfoEventArgs arg2) |
| 64 | +static ClientArgs ParseArgs(string[] argv) |
93 | 65 | { |
94 | | - // arg2.Info 包含服务端返回的完整版本信息(VersionRespDTO) |
95 | | - Console.WriteLine($"服务端返回更新信息:Code={arg2.Info.Code}, 版本数量={arg2.Info.Body?.Count ?? 0}"); |
| 66 | + var a = new ClientArgs(); |
| 67 | + for (int i = 0; i < argv.Length; i++) |
| 68 | + { |
| 69 | + switch (argv[i]) |
| 70 | + { |
| 71 | + case "--server-url" when i + 1 < argv.Length: a.ServerUrl = argv[++i]; break; |
| 72 | + case "--install-path" when i + 1 < argv.Length: a.InstallPath = argv[++i]; break; |
| 73 | + case "--current-version" when i + 1 < argv.Length: a.CurrentVersion = argv[++i]; break; |
| 74 | + case "--app-secret" when i + 1 < argv.Length: a.AppSecret = argv[++i]; break; |
| 75 | + case "--product-id" when i + 1 < argv.Length: a.ProductId = argv[++i]; break; |
| 76 | + case "--app-name" when i + 1 < argv.Length: a.AppName = argv[++i]; break; |
| 77 | + } |
| 78 | + } |
| 79 | + return a; |
96 | 80 | } |
97 | 81 |
|
98 | | -bool OnUpdatePrecheck(UpdateInfoEventArgs arg) |
| 82 | +class ClientArgs |
99 | 83 | { |
100 | | - // 返回 true:跳过本次更新;返回 false:继续执行自动更新 |
101 | | - // 可在此处添加自定义判断逻辑,例如检查磁盘空间、询问用户是否立即更新等 |
102 | | - Console.WriteLine($"更新预检:发现 {arg.Info.Body?.Count ?? 0} 个可用版本,继续更新..."); |
103 | | - return false; // false = 继续更新 |
| 84 | + public string ServerUrl { get; set; } = "http://127.0.0.1:5000"; |
| 85 | + public string InstallPath { get; set; } = AppDomain.CurrentDomain.BaseDirectory; |
| 86 | + public string CurrentVersion { get; set; } = "1.0.0.0"; |
| 87 | + public string AppSecret { get; set; } = "dfeb5833-975e-4afb-88f1-6278ee9aeff6"; |
| 88 | + public string ProductId { get; set; } = "2d974e2a-31e6-4887-9bb1-b4689e98c77a"; |
| 89 | + public string AppName { get; set; } = "Upgrade.exe"; |
104 | 90 | } |
0 commit comments