|
| 1 | +using GeneralUpdate.Core; |
| 2 | +using GeneralUpdate.Core.Configuration; |
| 3 | +using GeneralUpdate.Core.Download; |
| 4 | +using GeneralUpdate.Core.Event; |
| 5 | + |
| 6 | +namespace Hub.Samples; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// 标准更新流程 — 最简化的生产级集成方式。 |
| 10 | +/// 展示从配置到执行的完整链路,不包含模拟数据或文件操作。 |
| 11 | +/// </summary> |
| 12 | +/// <remarks> |
| 13 | +/// 相比 <see cref="CompleteUpdateSample"/>,本示例省略了模拟应用的创建和 |
| 14 | +/// 更新后的文件遍历比对,聚焦于 GeneralUpdateBootstrap 的核心配置模式。 |
| 15 | +/// </remarks> |
| 16 | +public class StandardUpdateSample : ISample |
| 17 | +{ |
| 18 | + public int Index => 1; |
| 19 | + public string Name => "标准更新流程 — Bootstrap·配置·执行"; |
| 20 | + public bool RequiresServer => true; |
| 21 | + public string[] CleanPaths => Array.Empty<string>(); |
| 22 | + |
| 23 | + public async Task RunAsync(AppConfig config, CancellationToken ct) |
| 24 | + { |
| 25 | + Console.WriteLine(); |
| 26 | + Console.WriteLine("══ 标准更新流程 (v10.5.0-beta.6) ══"); |
| 27 | + |
| 28 | + // ════════════════════════════════════════════════════════════════ |
| 29 | + // 1. 组装请求参数 |
| 30 | + // ════════════════════════════════════════════════════════════════ |
| 31 | + var request = new UpdateRequest |
| 32 | + { |
| 33 | + UpdateUrl = $"{config.ServerUrl}/Upgrade/Verification", |
| 34 | + ReportUrl = $"{config.ServerUrl}/Upgrade/Report", |
| 35 | + AppSecretKey = config.AppSecretKey, |
| 36 | + InstallPath = AppDomain.CurrentDomain.BaseDirectory, |
| 37 | + UpdatePath = AppDomain.CurrentDomain.BaseDirectory, |
| 38 | + ClientVersion = config.ClientVersion, |
| 39 | + MainAppName = config.MainAppName, |
| 40 | + UpdateAppName = config.UpgradeAppName, |
| 41 | + ProductId = config.ProductId |
| 42 | + }; |
| 43 | + |
| 44 | + Console.WriteLine(); |
| 45 | + Console.WriteLine("══ 配置 ══"); |
| 46 | + Console.WriteLine($" Server: {request.UpdateUrl}"); |
| 47 | + Console.WriteLine($" Version: {request.ClientVersion}"); |
| 48 | + Console.WriteLine($" AppKey: {request.AppSecretKey?[..Math.Min(8, request.AppSecretKey?.Length ?? 0)]}..."); |
| 49 | + |
| 50 | + // ════════════════════════════════════════════════════════════════ |
| 51 | + // 2. 构建 Bootstrap |
| 52 | + // ════════════════════════════════════════════════════════════════ |
| 53 | + Console.WriteLine(); |
| 54 | + Console.WriteLine("══ 初始化 GeneralUpdateBootstrap ══"); |
| 55 | + |
| 56 | + var bootstrap = new GeneralUpdateBootstrap() |
| 57 | + |
| 58 | + // — 必要:请求配置 — |
| 59 | + .SetConfig(request) |
| 60 | + |
| 61 | + // — 必要:指定应用类型(Client=主应用, Upgrade=升级器) — |
| 62 | + .SetOption(Option.AppType, AppType.Client) |
| 63 | + |
| 64 | + // — 必要:版本发现事件 — |
| 65 | + .AddListenerUpdateInfo((_, e) => |
| 66 | + { |
| 67 | + var count = e.Info?.Body?.Count ?? 0; |
| 68 | + // 服务端可能在多个版本中返回 full + chain 包供客户端选择 |
| 69 | + // 客户端会按 PackageType 自动排序:full 优先 |
| 70 | + Console.WriteLine($" [版本发现] 找到 {count} 个可用更新"); |
| 71 | + }) |
| 72 | + |
| 73 | + // — 可选:下载统计(多文件并行) — |
| 74 | + .AddListenerMultiDownloadStatistics((_, e) => |
| 75 | + { |
| 76 | + Console.Write($"\r [下载] {e.BytesReceived}/{e.TotalBytesToReceive} bytes ({e.ProgressPercentage:F0}%) {e.Speed}"); |
| 77 | + }) |
| 78 | + |
| 79 | + // — 可选:下载完成 — |
| 80 | + .AddListenerMultiDownloadCompleted((_, e) => |
| 81 | + { |
| 82 | + Console.WriteLine(); |
| 83 | + Console.WriteLine($" [下载完成] {(e.IsCompleted ? "✓" : "✗ 存在失败")}"); |
| 84 | + }) |
| 85 | + |
| 86 | + // — 可选:单文件进度 — |
| 87 | + .AddListenerProgress((_, e) => |
| 88 | + { |
| 89 | + if (e.Progress is { } p) |
| 90 | + Console.WriteLine($" {p.AssetName}: {p.Percentage:F0}% ({p.Status})"); |
| 91 | + }) |
| 92 | + |
| 93 | + // — 可选:异常通知 — |
| 94 | + .AddListenerException((_, e) => |
| 95 | + { |
| 96 | + Console.WriteLine($" [异常] {e.Message}"); |
| 97 | + }); |
| 98 | + |
| 99 | + // ════════════════════════════════════════════════════════════════ |
| 100 | + // 3. 执行更新 |
| 101 | + // ════════════════════════════════════════════════════════════════ |
| 102 | + Console.WriteLine(); |
| 103 | + Console.WriteLine("══ 启动更新 (LaunchAsync) ══"); |
| 104 | + Console.WriteLine(" 流程: 版本发现 → 下载 → 解压 → 备份旧版本 → 应用新版本"); |
| 105 | + |
| 106 | + try |
| 107 | + { |
| 108 | + await bootstrap.LaunchAsync(); |
| 109 | + Console.WriteLine(); |
| 110 | + Console.WriteLine("══ 更新完成 ✓ ══"); |
| 111 | + } |
| 112 | + catch (Exception ex) |
| 113 | + { |
| 114 | + Console.WriteLine(); |
| 115 | + Console.WriteLine($"══ 更新失败: {ex.Message} ══"); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments