Skip to content

Commit 5e9018c

Browse files
committed
更新最新C# 语法
1 parent 855ce67 commit 5e9018c

File tree

19 files changed

+349
-333
lines changed

19 files changed

+349
-333
lines changed

src/c#/GeneralUpdate.Client/Program.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,48 @@
1-
using System.Text;
1+
using System.Diagnostics;
2+
using System.Text;
23
using GeneralUpdate.ClientCore;
34
using GeneralUpdate.Common.Download;
45
using GeneralUpdate.Common.Internal;
56
using GeneralUpdate.Common.Internal.Bootstrap;
67
using GeneralUpdate.Common.Shared.Object;
7-
using GeneralUpdate.Differential;
88

99
namespace GeneralUpdate.Client
1010
{
1111
internal class Progra
1212
{
1313
private static void Main(string[] args)
1414
{
15-
Task.Run(async () =>
15+
/*Task.Run(async () =>
1616
{
1717
var source = @"D:\packet\app";
1818
var target = @"D:\packet\release";
1919
var patch = @"D:\packet\patch";
2020
2121
await DifferentialCore.Instance?.Clean(source, target, patch);
2222
await DifferentialCore.Instance?.Dirty(source, patch);
23-
});
23+
});*/
2424

25-
/*Task.Run(() =>
25+
Task.Run(() =>
2626
{
2727
var configinfo = new Configinfo();
2828
configinfo.UpdateLogUrl = "https://www.baidu.com";
2929
configinfo.ReportUrl = "http://127.0.0.1:5008/Upgrade/Report";
3030
configinfo.UpdateUrl = "http://127.0.0.1:5008/Upgrade/Verification";
3131

32-
configinfo.AppName = "GeneralUpdate.Upgrade";
33-
configinfo.MainAppName = "GeneralUpdate.Client";
32+
configinfo.AppName = "GeneralUpdate.Upgrade.exe";
33+
configinfo.MainAppName = "GeneralUpdate.Client.exe";
3434
configinfo.InstallPath = Thread.GetDomain().BaseDirectory;
3535

36+
//当前客户端的版本号
3637
configinfo.ClientVersion = "1.0.0.0";
38+
//当前升级端的版本号
3739
configinfo.UpgradeClientVersion = "1.0.0.0";
3840

41+
//平台
3942
configinfo.Platform = 1;
40-
configinfo.ProductId = "9999";
43+
//产品id
44+
configinfo.ProductId = "a77c9df5-45f8-4ee9-b3ad-b9431ce0b51c";
45+
//应用密钥
4146
configinfo.AppSecretKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
4247

4348
_ = new GeneralClientBootstrap()//单个或多个更新包下载通知事件
@@ -58,33 +63,40 @@ private static void Main(string[] args)
5863
.Option(UpdateOption.Format, Format.ZIP)
5964
.Option(UpdateOption.Drive, false)
6065
.LaunchAsync();
61-
});*/
66+
});
6267

6368
Console.Read();
6469
}
6570

6671
private static void OnMultiDownloadError(object arg1, MultiDownloadErrorEventArgs arg2)
6772
{
73+
Debug.WriteLine(arg2.Exception);
6874
}
6975

7076
private static void OnMultiAllDownloadCompleted(object arg1, MultiAllDownloadCompletedEventArgs arg2)
7177
{
78+
Debug.WriteLine(arg2.IsAllDownloadCompleted);
7279
}
7380

7481
private static void OnMultiDownloadCompleted(object arg1, MultiDownloadCompletedEventArgs arg2)
7582
{
83+
var v = arg2.Version as VersionInfo;
84+
Debug.WriteLine(v.Version);
7685
}
7786

7887
private static void OnMultiDownloadStatistics(object arg1, MultiDownloadStatisticsEventArgs arg2)
7988
{
89+
Debug.WriteLine(arg2.Speed);
8090
}
8191

8292
private static void OnMultiDownloadProgressChanged(object arg1, MultiDownloadProgressChangedEventArgs arg2)
8393
{
94+
Debug.WriteLine(arg2.ProgressValue);
8495
}
8596

8697
private static void OnException(object arg1, ExceptionEventArgs arg2)
8798
{
99+
Debug.WriteLine(arg2.Exception);
88100
}
89101
}
90102
}

src/c#/GeneralUpdate.ClientCore/GeneralClientBootstrap.cs

Lines changed: 101 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
namespace GeneralUpdate.ClientCore;
2020

2121
/// <summary>
22-
/// This component is used only for client application bootstrapping classes.
22+
/// This component is used only for client application bootstrapping classes.
2323
/// </summary>
2424
public class GeneralClientBootstrap : AbstractBootstrap<GeneralClientBootstrap, IStrategy>
2525
{
@@ -41,19 +41,7 @@ public override async Task<GeneralClientBootstrap> LaunchAsync()
4141
{
4242
ExecuteCustomOptions();
4343
ClearEnvironmentVariable();
44-
await InitializeDataAsync();
45-
46-
var manager = new DownloadManager(_configinfo.TempPath, _configinfo.Format, _configinfo.DownloadTimeOut);
47-
manager.MultiAllDownloadCompleted += OnMultiAllDownloadCompleted;
48-
manager.MultiDownloadCompleted += OnMultiDownloadCompleted;
49-
manager.MultiDownloadError += OnMultiDownloadError;
50-
manager.MultiDownloadProgressChanged += OnMultiDownloadProgressChanged;
51-
manager.MultiDownloadStatistics += OnMultiDownloadStatistics;
52-
foreach (var versionInfo in _configinfo.UpdateVersions)
53-
{
54-
manager.Add(new DownloadTask(manager, versionInfo));
55-
}
56-
await manager.LaunchTasksAsync();
44+
await ExecuteWorkflowAsync();
5745
return this;
5846
}
5947

@@ -144,7 +132,7 @@ public GeneralClientBootstrap AddListenerException(Action<object, ExceptionEvent
144132

145133
#region Private Methods
146134

147-
private async Task InitializeDataAsync()
135+
private async Task ExecuteWorkflowAsync()
148136
{
149137
//Request the upgrade information needed by the client and upgrade end, and determine if an upgrade is necessary.
150138
var mainResp = await VersionService.Validate(_configinfo.UpdateUrl
@@ -160,40 +148,95 @@ private async Task InitializeDataAsync()
160148
,_configinfo.AppSecretKey
161149
,_configinfo.Platform
162150
,_configinfo.ProductId);
163-
164-
_configinfo.IsUpgradeUpdate = upgradeResp.Body.Count > 0;
165-
_configinfo.IsMainUpdate = mainResp.Body.Count > 0;
166-
//No need to update, return directly.
167-
if (!_configinfo.IsMainUpdate && !_configinfo.IsUpgradeUpdate) return;
168-
151+
152+
_configinfo.IsUpgradeUpdate = CheckUpgrade(upgradeResp);
153+
_configinfo.IsMainUpdate = CheckUpgrade(mainResp);
154+
169155
//If the main program needs to be forced to update, the skip will not take effect.
170156
var isForcibly = CheckForcibly(mainResp.Body) || CheckForcibly(upgradeResp.Body);
171157
if (CanSkip(isForcibly)) return;
172158

173-
_configinfo.UpdateVersions = upgradeResp.Body.OrderBy(x => x.ReleaseDate).ToList();
174-
_configinfo.LastVersion = _configinfo.UpdateVersions.Last().Version;
175159
_configinfo.Encoding = GetOption(UpdateOption.Encoding) ?? Encoding.Default;
176-
_configinfo.Format = GetOption(UpdateOption.Format)?? "zip";
160+
_configinfo.Format = GetOption(UpdateOption.Format) ?? ".zip";
177161
_configinfo.DownloadTimeOut = GetOption(UpdateOption.DownloadTimeOut) == 0 ? 60 : GetOption(UpdateOption.DownloadTimeOut);
178162
_configinfo.DriveEnabled = GetOption(UpdateOption.Drive);
179-
_configinfo.TempPath = GeneralFileManager.GetTempDirectory(_configinfo.LastVersion);
163+
_configinfo.TempPath = GeneralFileManager.GetTempDirectory("main_temp");
164+
165+
if (_configinfo.IsMainUpdate)
166+
{
167+
_configinfo.UpdateVersions = mainResp.Body.OrderBy(x => x.ReleaseDate).ToList();
168+
_configinfo.LastVersion = _configinfo.UpdateVersions.Last().Version;
169+
170+
//Initialize the process transfer parameter object.
171+
var processInfo = new ProcessInfo(_configinfo.MainAppName
172+
, _configinfo.InstallPath
173+
, _configinfo.ClientVersion
174+
, _configinfo.LastVersion
175+
, _configinfo.UpdateLogUrl
176+
, _configinfo.Encoding
177+
, _configinfo.Format
178+
, _configinfo.DownloadTimeOut
179+
, _configinfo.AppSecretKey
180+
, mainResp.Body
181+
, _configinfo.ReportUrl);
182+
183+
_configinfo.ProcessInfo = JsonSerializer.Serialize(processInfo);
184+
}
185+
186+
StrategyFactory();
180187

181-
//Initialize the process transfer parameter object.
182-
var processInfo = new ProcessInfo(_configinfo.MainAppName
183-
, _configinfo.InstallPath
184-
, _configinfo.ClientVersion
185-
, _configinfo.LastVersion
186-
, _configinfo.UpdateLogUrl
187-
, _configinfo.Encoding
188-
, _configinfo.Format
189-
, _configinfo.DownloadTimeOut
190-
, _configinfo.AppSecretKey
191-
, mainResp.Body);
192-
_configinfo.ProcessInfo = JsonSerializer.Serialize(processInfo);
188+
switch (_configinfo.IsUpgradeUpdate)
189+
{
190+
case true when _configinfo.IsMainUpdate:
191+
//Both upgrade and main program update.
192+
await Download();
193+
await _strategy?.ExecuteAsync()!;
194+
_strategy?.StartApp();
195+
break;
196+
case true when !_configinfo.IsMainUpdate:
197+
//Upgrade program update.
198+
await Download();
199+
await _strategy?.ExecuteAsync()!;
200+
break;
201+
case false when _configinfo.IsMainUpdate:
202+
//Main program update.
203+
_strategy?.StartApp();
204+
break;
205+
}
193206
}
207+
208+
private async Task Download()
209+
{
210+
var manager = new DownloadManager(_configinfo.TempPath, _configinfo.Format, _configinfo.DownloadTimeOut);
211+
manager.MultiAllDownloadCompleted += OnMultiAllDownloadCompleted;
212+
manager.MultiDownloadCompleted += OnMultiDownloadCompleted;
213+
manager.MultiDownloadError += OnMultiDownloadError;
214+
manager.MultiDownloadProgressChanged += OnMultiDownloadProgressChanged;
215+
manager.MultiDownloadStatistics += OnMultiDownloadStatistics;
216+
foreach (var versionInfo in _configinfo.UpdateVersions)
217+
{
218+
manager.Add(new DownloadTask(manager, versionInfo));
219+
}
220+
await manager.LaunchTasksAsync();
221+
}
222+
223+
private bool CheckUpgrade(VersionRespDTO? response)
224+
{
225+
if (response == null)
226+
{
227+
return false;
228+
}
194229

230+
if (response.Code == HttpStatus.OK)
231+
{
232+
return response.Body.Count > 0;
233+
}
234+
235+
return false;
236+
}
237+
195238
/// <summary>
196-
/// User decides if update is required.
239+
/// User decides if update is required.
197240
/// </summary>
198241
/// <returns>is false to continue execution.</returns>
199242
private bool CanSkip(bool isForcibly)
@@ -221,7 +264,7 @@ private void ExecuteCustomOptions()
221264
}
222265

223266
/// <summary>
224-
/// Clear the environment variable information needed to start the upgrade assistant process.
267+
/// Clear the environment variable information needed to start the upgrade assistant process.
225268
/// </summary>
226269
private void ClearEnvironmentVariable()
227270
{
@@ -237,11 +280,25 @@ private void ClearEnvironmentVariable()
237280
}
238281
}
239282

240-
protected override void ExecuteStrategy()
283+
private bool CheckForcibly(List<VersionBodyDTO>? versions)
241284
{
242-
_strategy?.Create(_configinfo!);
243-
_strategy?.Execute();
285+
if (versions == null)
286+
return false;
287+
288+
foreach (var item in versions)
289+
{
290+
if (item.IsForcibly == true)
291+
{
292+
return true;
293+
}
294+
}
295+
296+
return false;
244297
}
298+
299+
protected override void ExecuteStrategy()=> throw new NotImplementedException();
300+
301+
protected override Task ExecuteStrategyAsync()=> throw new NotImplementedException();
245302

246303
protected override GeneralClientBootstrap StrategyFactory()
247304
{
@@ -252,22 +309,10 @@ protected override GeneralClientBootstrap StrategyFactory()
252309
else
253310
throw new PlatformNotSupportedException("The current operating system is not supported!");
254311

312+
_strategy?.Create(_configinfo!);
255313
return this;
256314
}
257315

258-
private bool CheckForcibly(List<VersionBodyDTO> versions)
259-
{
260-
foreach (var item in versions)
261-
{
262-
if (item.IsForcibly == true)
263-
{
264-
return true;
265-
}
266-
}
267-
268-
return false;
269-
}
270-
271316
private GeneralClientBootstrap AddListener<TArgs>(Action<object, TArgs> callbackAction) where TArgs : EventArgs
272317
{
273318
Debug.Assert(callbackAction != null);
@@ -288,11 +333,7 @@ private void OnMultiDownloadError(object sender, MultiDownloadErrorEventArgs e)
288333
=> EventManager.Instance.Dispatch(sender, e);
289334

290335
private void OnMultiAllDownloadCompleted(object sender, MultiAllDownloadCompletedEventArgs e)
291-
{
292-
EventManager.Instance.Dispatch(sender, e);
293-
StrategyFactory();
294-
ExecuteStrategy();
295-
}
336+
=> EventManager.Instance.Dispatch(sender, e);
296337

297338
#endregion Private Methods
298339
}

src/c#/GeneralUpdate.ClientCore/Pipeline/ZipMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace GeneralUpdate.ClientCore.Pipeline;
1111

1212
public class ZipMiddleware : IMiddleware
1313
{
14-
public Task InvokeAsync(PipelineContext context)
14+
public Task InvokeAsync(PipelineContext? context)
1515
{
1616
return Task.Run(() =>
1717
{

0 commit comments

Comments
 (0)