-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathProgram.cs
More file actions
229 lines (198 loc) · 9.53 KB
/
Copy pathProgram.cs
File metadata and controls
229 lines (198 loc) · 9.53 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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
{
await RunOssClientAsync();
/*var isOssMode = args.Length > 0 && args[0] == "--oss";
if (isOssMode)
{
await RunOssClientAsync();
}
else
{
await RunStandardClientAsync();
}*/
}
catch (Exception ex)
{
Console.WriteLine($"FATAL: {ex}");
Console.WriteLine("Press Enter to exit...");
Console.ReadLine();
Environment.Exit(1);
}
Console.WriteLine("Press Enter to exit...");
Console.ReadLine();
// ═══════════════════════════════════════════════════════════════════
// OSS Client mode — version JSON download → version compare → launch upgrade
// ═══════════════════════════════════════════════════════════════════
static async Task RunOssClientAsync()
{
Console.WriteLine("=== GeneralUpdate OSS Client Test ===");
Console.WriteLine($"Started at {DateTime.Now}");
Console.WriteLine($"Running from: {AppDomain.CurrentDomain.BaseDirectory}");
// Only secrets are supplied in code. Identity fields (MainAppName,
// ClientVersion, UpdateAppName, UpdatePath) are read from
// generalupdate.manifest.json by OssStrategy via
// AppMetadataDiscoverer.Discover() — same as the standard flow.
var updateUrl = "http://localhost:5000/packages/versions.json";
var appSecretKey = "dfeb5833-975e-4afb-88f1-6278ee9aeff6";
Console.WriteLine($"UpdateUrl: {updateUrl}");
Console.WriteLine();
await new GeneralUpdateBootstrap()
.SetSource(updateUrl, appSecretKey)
.SetOption(Option.AppType, AppType.OssClient)
.Hooks<ClientTestHooks>()
.AddListenerMultiDownloadStatistics(OnDownloadStatistics)
.AddListenerMultiDownloadCompleted(OnDownloadCompleted)
.AddListenerMultiAllDownloadCompleted(OnAllDownloadCompleted)
.AddListenerMultiDownloadError(OnDownloadError)
.AddListenerException(OnException)
.AddListenerUpdateInfo(OnUpdateInfo)
.LaunchAsync();
Console.WriteLine("OSS Client test completed.");
}
// ═══════════════════════════════════════════════════════════════════
// Standard Client mode — silent poll with IPC handoff to Upgrade
// ═══════════════════════════════════════════════════════════════════
static async Task RunStandardClientAsync()
{
Console.WriteLine("=== GeneralUpdate Client Test (Silent Mode) ===");
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($"Silent mode: ENABLED (poll every 1 minute)");
Console.WriteLine();
// Silent mode: polls server in background, prepares update, launches Upgrade on exit.
var bootstrap = await new GeneralUpdateBootstrap()
.SetSource(updateUrl, appSecretKey, reportUrl)
.SetOption(Option.AppType, AppType.Client)
.SetOption(Option.Silent, true)
.SetOption(Option.SilentPollIntervalMinutes, 1)
.Hooks<ClientTestHooks>()
.AddListenerMultiDownloadStatistics(OnDownloadStatistics)
.AddListenerMultiDownloadCompleted(OnDownloadCompleted)
.AddListenerMultiAllDownloadCompleted(OnAllDownloadCompleted)
.AddListenerMultiDownloadError(OnDownloadError)
.AddListenerException(OnException)
.AddListenerUpdateInfo(OnUpdateInfo)
.LaunchAsync();
var orchestrator = bootstrap.SilentOrchestrator;
Console.WriteLine();
Console.WriteLine("╔════════════════════════════════════════════╗");
Console.WriteLine("║ Silent poll running in background. ║");
Console.WriteLine("║ Press Ctrl+C or Enter to exit. ║");
Console.WriteLine("║ On exit, Upgrade process will be launched ║");
Console.WriteLine("║ if an update has been prepared. ║");
Console.WriteLine("╚════════════════════════════════════════════╝");
Console.WriteLine();
// Keep the process alive so the background poll loop can work.
var cts = new CancellationTokenSource();
Console.CancelKeyPress += (_, e) =>
{
Console.WriteLine();
Console.WriteLine("[Shutdown] Ctrl+C pressed. Exiting...");
e.Cancel = true;
cts.Cancel();
};
try
{
await Task.Delay(Timeout.Infinite, cts.Token);
}
catch (OperationCanceledException)
{
// Expected on Ctrl+C — graceful shutdown
}
Console.WriteLine("[Shutdown] Launching upgrade process...");
if (orchestrator != null && orchestrator.HasPreparedUpdate)
{
var launched = orchestrator.TryLaunchUpgrade();
Console.WriteLine(launched
? "[Shutdown] Upgrade process launched successfully."
: "[Shutdown] No update prepared or upgrade already launched.");
}
else
{
Console.WriteLine("[Shutdown] No orchestrator or no update prepared.");
}
Console.WriteLine("[Shutdown] Client test exiting gracefully.");
}
// ═══════════════════════════════════════════════════════════════════
// Event handlers (shared across both modes)
// ═══════════════════════════════════════════════════════════════════
static void OnDownloadStatistics(object sender, MultiDownloadStatisticsEventArgs e)
{
var v = e.Version as VersionEntry;
Console.WriteLine($"[Download] {v?.Version}: {e.ProgressPercentage}% | {e.Speed} | ETA: {e.Remaining}");
}
static void OnDownloadCompleted(object sender, MultiDownloadCompletedEventArgs e)
{
var v = e.Version as VersionEntry;
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 VersionEntry;
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.");
}
}
// ═══════════════════════════════════════════════════════════════════
// Hooks (shared across both modes)
// ═══════════════════════════════════════════════════════════════════
sealed class ClientTestHooks : IUpdateHooks
{
public async Task<bool> OnBeforeUpdateAsync(HookContext 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(HookContext ctx)
{
Console.WriteLine($"[Hook] OnAfterUpdate: {ctx.CurrentVersion} -> {ctx.TargetVersion}");
await Task.CompletedTask;
}
public async Task OnUpdateErrorAsync(HookContext ctx, Exception ex)
{
Console.WriteLine($"[Hook] OnUpdateError: {ctx.CurrentVersion} -> {ctx.TargetVersion} | {ex.Message}");
await Task.CompletedTask;
}
public async Task OnBeforeStartAppAsync(HookContext ctx)
{
Console.WriteLine($"[Hook] OnBeforeStartApp: {ctx.UpdateAppName} @ {ctx.InstallPath}");
await Task.CompletedTask;
}
}