-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathClientUpdateStrategy.cs
More file actions
292 lines (248 loc) · 10.7 KB
/
Copy pathClientUpdateStrategy.cs
File metadata and controls
292 lines (248 loc) · 10.7 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using GeneralUpdate.Core.Configuration;
using GeneralUpdate.Core.Download;
using GeneralUpdate.Core.Event;
using GeneralUpdate.Core.FileSystem;
using GeneralUpdate.Core.JsonContext;
using GeneralUpdate.Core.Network;
namespace GeneralUpdate.Core.Strategy;
/// <summary>
/// Client-side update strategy. Validates versions against server,
/// downloads update packages, creates process info for the upgrade side,
/// and starts the upgrade process.
/// </summary>
/// <remarks>
/// This is the AppType.ClientApp role strategy. It composes an OS-specific
/// strategy (Windows/Linux/Mac) for platform operations.
/// </remarks>
public class ClientUpdateStrategy : IStrategy
{
private GlobalConfigInfo? _configInfo;
private IStrategy? _osStrategy;
private Func<UpdateInfoEventArgs, bool>? _updatePrecheck;
private readonly List<Func<bool>> _customOptions = new();
private readonly Download.Abstractions.IDownloadOrchestrator? _orchestrator;
private readonly DiffMode _diffMode = DiffMode.Serial;
public ClientUpdateStrategy(Download.Abstractions.IDownloadOrchestrator? orchestrator = null) { _orchestrator = orchestrator; }
public void Create(GlobalConfigInfo parameter)
{
_configInfo = parameter ?? throw new ArgumentNullException(nameof(parameter));
_osStrategy = ResolveOsStrategy();
}
public async Task ExecuteAsync()
{
if (_configInfo == null) throw new InvalidOperationException("ClientUpdateStrategy not configured.");
try
{
GeneralTracer.Debug("ClientUpdateStrategy.ExecuteAsync start.");
await CallSmallBowlHomeAsync(_configInfo.Bowl);
ExecuteCustomOptions();
await ExecuteWorkflowAsync();
}
catch (Exception ex)
{
GeneralTracer.Error("ClientUpdateStrategy.ExecuteAsync failed.", ex);
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(ex, ex.Message));
}
}
public void Execute()
{
ExecuteAsync().GetAwaiter().GetResult();
}
public void StartApp()
{
_osStrategy?.StartApp();
}
/// <summary>Register a precheck callback invoked when update info is available.</summary>
public ClientUpdateStrategy UseUpdatePrecheck(Func<UpdateInfoEventArgs, bool> func)
{
_updatePrecheck = func ?? throw new ArgumentNullException(nameof(func));
return this;
}
/// <summary>Register custom pre-update operations.</summary>
public ClientUpdateStrategy UseCustomOption(Func<bool> func)
{
_customOptions.Add(func);
return this;
}
#region Workflow
private async Task ExecuteWorkflowAsync()
{
// Silent mode ¡ª delegate to SilentUpdateMode
// (encoding/format/timeout are read from _configInfo)
var defaultEncoding = Encoding.UTF8;
var defaultTimeout = 60;
if (true /* silent check would read from options */)
{
// Standard mode
await ExecuteStandardWorkflowAsync(defaultEncoding, defaultTimeout);
}
}
private async Task ExecuteStandardWorkflowAsync(Encoding encoding, int timeout)
{
GeneralTracer.Info($"ClientUpdateStrategy: validating client={_configInfo!.ClientVersion}, upgrade={_configInfo.UpgradeClientVersion}");
var mainResp = await VersionService.Validate(_configInfo.UpdateUrl,
_configInfo.ClientVersion, AppType.ClientApp, _configInfo.AppSecretKey,
GetPlatform(), _configInfo.ProductId, _configInfo.Scheme, _configInfo.Token);
var upgradeResp = await VersionService.Validate(_configInfo.UpdateUrl,
_configInfo.UpgradeClientVersion, AppType.UpgradeApp, _configInfo.AppSecretKey,
GetPlatform(), _configInfo.ProductId, _configInfo.Scheme, _configInfo.Token);
_configInfo.IsUpgradeUpdate = CheckUpgrade(upgradeResp);
_configInfo.IsMainUpdate = CheckUpgrade(mainResp);
GeneralTracer.Info($"ClientUpdateStrategy: IsMainUpdate={_configInfo.IsMainUpdate}, IsUpgradeUpdate={_configInfo.IsUpgradeUpdate}");
var updateInfoArgs = new UpdateInfoEventArgs(mainResp);
EventManager.Instance.Dispatch(this, updateInfoArgs);
var isForcibly = CheckForcibly(mainResp.Body) || CheckForcibly(upgradeResp.Body);
if (CanSkip(isForcibly, updateInfoArgs))
{
GeneralTracer.Info("ClientUpdateStrategy: update skipped.");
return;
}
InitBlackList();
ApplyRuntimeOptions(encoding, timeout);
_configInfo.TempPath = StorageManager.GetTempDirectory("main_temp");
_configInfo.BackupDirectory = Path.Combine(_configInfo.InstallPath,
$"{StorageManager.DirectoryName}{_configInfo.ClientVersion}");
_configInfo.UpdateVersions = _configInfo.IsUpgradeUpdate
? upgradeResp.Body.OrderBy(x => x.ReleaseDate).ToList()
: new List<VersionInfo>();
if (_configInfo.IsMainUpdate)
{
_configInfo.LastVersion = mainResp.Body.OrderBy(x => x.ReleaseDate).Last().Version;
GeneralTracer.Info($"ClientUpdateStrategy: main update LastVersion={_configInfo.LastVersion}");
if (CheckFail(_configInfo.LastVersion))
{
GeneralTracer.Warn($"ClientUpdateStrategy: version {_configInfo.LastVersion} matches known-failed upgrade.");
return;
}
var processInfo = ConfigurationMapper.MapToProcessInfo(
_configInfo, mainResp.Body,
BlackListManager.Instance.BlackFormats.ToList(),
BlackListManager.Instance.BlackFiles.ToList(),
BlackListManager.Instance.SkipDirectorys.ToList());
_configInfo.ProcessInfo = JsonSerializer.Serialize(
processInfo, ProcessInfoJsonContext.Default.ProcessInfo);
}
// Backup
Backup();
_osStrategy!.Create(_configInfo);
GeneralTracer.Info($"ClientUpdateStrategy: IsUpgradeUpdate={_configInfo.IsUpgradeUpdate}, IsMainUpdate={_configInfo.IsMainUpdate}");
switch (_configInfo.IsUpgradeUpdate)
{
case true when _configInfo.IsMainUpdate:
GeneralTracer.Info("ClientUpdateStrategy: both upgrade+main ¡ª downloading and executing.");
await DownloadAsync();
await _osStrategy.ExecuteAsync();
_osStrategy.StartApp();
break;
case true when !_configInfo.IsMainUpdate:
GeneralTracer.Info("ClientUpdateStrategy: upgrade-only ¡ª downloading and executing.");
await DownloadAsync();
await _osStrategy.ExecuteAsync();
break;
case false when _configInfo.IsMainUpdate:
GeneralTracer.Info("ClientUpdateStrategy: main-only ¡ª starting updater.");
_osStrategy.StartApp();
break;
}
}
#endregion
#region Helpers
private static IStrategy ResolveOsStrategy()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return new WindowsStrategy();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
return new LinuxStrategy();
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return new MacStrategy();
throw new PlatformNotSupportedException("The current operating system is not supported!");
}
private void ApplyRuntimeOptions(Encoding encoding, int timeout)
{
_configInfo!.Encoding = encoding;
_configInfo.Format = Format.ZIP;
_configInfo.DownloadTimeOut = timeout;
}
private void InitBlackList()
{
BlackListManager.Instance.AddBlackFiles(_configInfo!.BlackFiles);
BlackListManager.Instance.AddBlackFormats(_configInfo.BlackFormats);
BlackListManager.Instance.AddSkipDirectorys(_configInfo.SkipDirectorys);
}
private void Backup()
{
GeneralTracer.Info($"ClientUpdateStrategy: backing up {_configInfo!.InstallPath} -> {_configInfo.BackupDirectory}");
StorageManager.Backup(_configInfo.InstallPath, _configInfo.BackupDirectory,
BlackListManager.Instance.SkipDirectorys);
}
private async Task DownloadAsync()
{
var manager = new DownloadManager(
_configInfo!.TempPath, _configInfo.Format, _configInfo.DownloadTimeOut);
foreach (var version in _configInfo.UpdateVersions)
manager.Add(new DownloadTask(manager, version));
await manager.LaunchTasksAsync();
}
private bool CanSkip(bool isForcibly, UpdateInfoEventArgs updateInfo)
{
if (isForcibly) return false;
return _updatePrecheck?.Invoke(updateInfo) == true;
}
private bool CheckFail(string version)
{
var fail = Environments.GetEnvironmentVariable("UpgradeFail");
if (string.IsNullOrEmpty(fail) || string.IsNullOrEmpty(version))
return false;
return new Version(fail) >= new Version(version);
}
private static bool CheckUpgrade(VersionRespDTO? response)
=> response?.Code == 200 && response.Body?.Count > 0;
private static bool CheckForcibly(IEnumerable<VersionInfo>? versions)
=> versions?.Any(v => v.IsForcibly == true) == true;
private static int GetPlatform()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return PlatformType.Windows;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) return PlatformType.Linux;
return -1;
}
private async Task CallSmallBowlHomeAsync(string processName)
{
if (string.IsNullOrWhiteSpace(processName)) return;
try
{
var processes = Process.GetProcessesByName(processName);
if (processes.Length == 0) return;
foreach (var process in processes)
{
GeneralTracer.Info($"Shutting down process {process.ProcessName} (ID: {process.Id})");
await GracefulExit.ShutdownAsync(process).ConfigureAwait(false);
}
}
catch (Exception ex)
{
GeneralTracer.Error("CallSmallBowlHomeAsync failed.", ex);
}
}
private void ExecuteCustomOptions()
{
foreach (var option in _customOptions)
{
if (!option.Invoke())
{
var ex = new Exception("Custom option execution failed.");
GeneralTracer.Error("ExecuteCustomOptions failed.", ex);
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(ex, ex.Message));
}
}
}
#endregion
}