-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathProgram.cs
More file actions
119 lines (103 loc) · 4.24 KB
/
Copy pathProgram.cs
File metadata and controls
119 lines (103 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using GeneralUpdate.Core;
using GeneralUpdate.Core.Configuration;
using GeneralUpdate.Core.Download;
using GeneralUpdate.Core.Download.Reporting;
using GeneralUpdate.Core.Event;
using GeneralUpdate.Core.Hooks;
try
{
Console.WriteLine("=== GeneralUpdate Client Test ===");
Console.WriteLine($"Started at {DateTime.Now}");
Console.WriteLine($"Running from: {AppDomain.CurrentDomain.BaseDirectory}");
// Secrets come from code — never from files.
var updateUrl = "http://localhost:5000/Upgrade/Verification";
var reportUrl = "http://localhost:5000/Upgrade/Report";
var appSecretKey = Environment.GetEnvironmentVariable("APP_SECRET_KEY") ?? "dfeb5833-975e-4afb-88f1-6278ee9aeff6";
Console.WriteLine($"UpdateUrl: {updateUrl}");
Console.WriteLine();
// Identity metadata (MainAppName, ClientVersion, UpdateAppName, UpdatePath, …)
// is auto-discovered from generalupdate.manifest.json (generated by GeneralUpdate.Tools).
await new GeneralUpdateBootstrap()
.SetSource(updateUrl, appSecretKey, reportUrl)
.Option(UpdateOptions.AppType, AppType.Client)
.Hooks<ClientTestHooks>()
.AddListenerMultiDownloadStatistics(OnDownloadStatistics)
.AddListenerMultiDownloadCompleted(OnDownloadCompleted)
.AddListenerMultiAllDownloadCompleted(OnAllDownloadCompleted)
.AddListenerMultiDownloadError(OnDownloadError)
.AddListenerException(OnException)
.AddListenerUpdateInfo(OnUpdateInfo)
.LaunchAsync();
Console.WriteLine("Client test completed.");
}
catch (Exception ex)
{
Console.WriteLine($"FATAL: {ex}");
Environment.Exit(1);
}
static void OnDownloadStatistics(object sender, MultiDownloadStatisticsEventArgs e)
{
var v = e.Version as VersionInfo;
Console.WriteLine($"[Download] {v?.Version}: {e.ProgressPercentage}% | {e.Speed} | ETA: {e.Remaining}");
}
static void OnDownloadCompleted(object sender, MultiDownloadCompletedEventArgs e)
{
var v = e.Version as VersionInfo;
Console.WriteLine($"[Download] {v?.Version}: {(e.IsCompleted ? "SUCCESS" : "FAILED")}");
}
static void OnAllDownloadCompleted(object sender, MultiAllDownloadCompletedEventArgs e)
{
Console.WriteLine(e.IsAllDownloadCompleted
? "[Download] All downloads completed."
: $"[Download] Downloads finished with {e.FailedVersions.Count} failure(s).");
}
static void OnDownloadError(object sender, MultiDownloadErrorEventArgs e)
{
var v = e.Version as VersionInfo;
Console.WriteLine($"[Download] Error @ {v?.Version}: {e.Exception.Message}");
}
static void OnException(object sender, ExceptionEventArgs e)
{
Console.WriteLine($"[Error] {e.Exception}");
}
static void OnUpdateInfo(object sender, UpdateInfoEventArgs e)
{
Console.WriteLine($"[UpdateInfo] Code={e.Info?.Code}, Message={e.Info?.Message}");
if (e.Info?.Body is { Count: > 0 })
{
foreach (var vi in e.Info.Body)
Console.WriteLine($" - {vi.Version} ({vi.Name}) [{vi.Size} bytes] {(vi.IsForcibly == true ? "(forced)" : "")}");
}
else
{
Console.WriteLine(" No updates available.");
}
}
sealed class ClientTestHooks : IUpdateHooks
{
public async Task<bool> OnBeforeUpdateAsync(UpdateContext ctx)
{
Console.WriteLine($"[Hook] OnBeforeUpdate: {ctx.CurrentVersion} -> {ctx.TargetVersion}");
return await Task.FromResult(true);
}
public async Task OnDownloadCompletedAsync(DownloadContext ctx)
{
Console.WriteLine($"[Hook] OnDownloadCompleted: {ctx.AssetName} v{ctx.Version} ({ctx.TotalBytes} bytes, {ctx.Duration}) {(ctx.Success ? "OK" : "FAIL")}");
await Task.CompletedTask;
}
public async Task OnAfterUpdateAsync(UpdateContext ctx)
{
Console.WriteLine($"[Hook] OnAfterUpdate: {ctx.CurrentVersion} -> {ctx.TargetVersion}");
await Task.CompletedTask;
}
public async Task OnUpdateErrorAsync(UpdateContext ctx, Exception ex)
{
Console.WriteLine($"[Hook] OnUpdateError: {ctx.CurrentVersion} -> {ctx.TargetVersion} | {ex.Message}");
await Task.CompletedTask;
}
public async Task OnBeforeStartAppAsync(UpdateContext ctx)
{
Console.WriteLine($"[Hook] OnBeforeStartApp: {ctx.UpdateAppName} @ {ctx.InstallPath}");
await Task.CompletedTask;
}
}