Skip to content

Commit 95f9d03

Browse files
committed
Update Program.cs
1 parent c09767f commit 95f9d03

1 file changed

Lines changed: 45 additions & 74 deletions

File tree

test_app/Upgrade/Program.cs

Lines changed: 45 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,60 @@
1-
using System.Text.Json;
1+
using GeneralUpdate.Common.Download;
2+
using GeneralUpdate.Common.Internal;
3+
using GeneralUpdate.Common.Shared.Object;
24
using GeneralUpdate.Core;
35

4-
// Parse command-line args
5-
var cliArgs = ParseArgs(Environment.GetCommandLineArgs()[1..]);
6-
7-
// Write ProcessInfo as env var for GeneralUpdateBootstrap
8-
var processInfoJson = JsonSerializer.Serialize(new Dictionary<string, object>
9-
{
10-
["AppName"] = "Client.exe",
11-
["InstallPath"] = cliArgs.InstallPath,
12-
["CurrentVersion"] = cliArgs.CurrentVersion,
13-
["LastVersion"] = cliArgs.LastVersion,
14-
["CompressEncoding"] = "utf-8",
15-
["CompressFormat"] = ".zip",
16-
["DownloadTimeOut"] = 60,
17-
["AppSecretKey"] = cliArgs.AppSecret,
18-
["UpdateVersions"] = new[]
19-
{
20-
new Dictionary<string, object?>
21-
{
22-
["Name"] = cliArgs.PatchName,
23-
["Version"] = cliArgs.TargetVersion,
24-
["Hash"] = cliArgs.Hash,
25-
["AppType"] = 1,
26-
["IsForcibly"] = false
27-
}
28-
}
29-
});
30-
31-
Environment.SetEnvironmentVariable("ProcessInfo", processInfoJson);
32-
33-
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Upgrade started");
34-
Console.WriteLine($"Install path: {cliArgs.InstallPath}");
35-
366
try
377
{
38-
await new GeneralUpdateBootstrap()
39-
.AddListenerMultiDownloadStatistics((_, e) =>
40-
{
41-
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Download: {e.ProgressPercentage}%");
42-
})
43-
.AddListenerMultiAllDownloadCompleted((_, e) =>
44-
{
45-
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Downloads: {(e.IsAllDownloadCompleted ? "done" : "failed")}");
46-
})
47-
.AddListenerException((_, e) =>
48-
{
49-
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] ERROR: {e.Exception}");
50-
})
8+
Console.WriteLine($"升级程序初始化,{DateTime.Now}!");
9+
Console.WriteLine("当前运行目录:" + Thread.GetDomain().BaseDirectory);
10+
_ = await new GeneralUpdateBootstrap()
11+
//单个或多个更新包下载速度、剩余下载事件、当前下载版本信息通知事件
12+
.AddListenerMultiDownloadStatistics(OnMultiDownloadStatistics)
13+
//单个或多个更新包下载完成
14+
.AddListenerMultiDownloadCompleted(OnMultiDownloadCompleted)
15+
//完成所有的下载任务通知
16+
.AddListenerMultiAllDownloadCompleted(OnMultiAllDownloadCompleted)
17+
//下载过程出现的异常通知
18+
.AddListenerMultiDownloadError(OnMultiDownloadError)
19+
//整个更新过程出现的任何问题都会通过这个事件通知
20+
.AddListenerException(OnException)
21+
//设置字段映射表,用于解析所有驱动包的信息的字符串
22+
//.SetFieldMappings(fieldMappingsCN)
23+
//是否开启驱动更新
24+
//.Option(UpdateOption.Drive, true)
5125
.LaunchAsync();
26+
Console.WriteLine($"升级程序已启动,{DateTime.Now}!");
27+
await Task.Delay(2000);
28+
}
29+
catch (Exception e)
30+
{
31+
Console.WriteLine(e.Message + "\n" + e.StackTrace);
32+
}
5233

53-
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Upgrade completed");
34+
void OnMultiDownloadError(object arg1, MultiDownloadErrorEventArgs arg2)
35+
{
36+
var version = arg2.Version as VersionInfo;
37+
Console.WriteLine($"{version?.Version} {arg2.Exception}");
5438
}
55-
catch (Exception ex)
39+
40+
void OnMultiAllDownloadCompleted(object arg1, MultiAllDownloadCompletedEventArgs arg2)
5641
{
57-
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] FATAL: {ex.Message}");
58-
Environment.Exit(1);
42+
Console.WriteLine(arg2.IsAllDownloadCompleted ? "所有的下载任务已完成!" : $"下载任务已失败!{arg2.FailedVersions.Count}");
5943
}
6044

61-
static UpgradeArgs ParseArgs(string[] argv)
45+
void OnMultiDownloadCompleted(object arg1, MultiDownloadCompletedEventArgs arg2)
6246
{
63-
var a = new UpgradeArgs();
64-
for (int i = 0; i < argv.Length; i++)
65-
{
66-
switch (argv[i])
67-
{
68-
case "--install-path" when i + 1 < argv.Length: a.InstallPath = argv[++i]; break;
69-
case "--current-version" when i + 1 < argv.Length: a.CurrentVersion = argv[++i]; break;
70-
case "--target-version" when i + 1 < argv.Length: a.TargetVersion = argv[++i]; break;
71-
case "--last-version" when i + 1 < argv.Length: a.LastVersion = argv[++i]; break;
72-
case "--app-secret" when i + 1 < argv.Length: a.AppSecret = argv[++i]; break;
73-
case "--patch-name" when i + 1 < argv.Length: a.PatchName = argv[++i]; break;
74-
case "--hash" when i + 1 < argv.Length: a.Hash = argv[++i]; break;
75-
}
76-
}
77-
return a;
47+
var version = arg2.Version as VersionInfo;
48+
Console.WriteLine(arg2.IsComplated ? $"当前下载版本:{version?.Version}, 下载完成!" : $"当前下载版本:{version?.Version}, 下载失败!");
7849
}
7950

80-
class UpgradeArgs
51+
void OnMultiDownloadStatistics(object arg1, MultiDownloadStatisticsEventArgs arg2)
8152
{
82-
public string InstallPath { get; set; } = AppDomain.CurrentDomain.BaseDirectory;
83-
public string CurrentVersion { get; set; } = "1.0.0.0";
84-
public string TargetVersion { get; set; } = "2.0.0.0";
85-
public string LastVersion { get; set; } = "1.0.0.0";
86-
public string AppSecret { get; set; } = "";
87-
public string PatchName { get; set; } = "";
88-
public string Hash { get; set; } = "";
53+
var version = arg2.Version as VersionInfo;
54+
Console.WriteLine($"当前下载版本:{version?.Version},下载速度:{arg2.Speed},剩余下载时间:{arg2.Remaining},已下载大小:{arg2.BytesReceived},总大小:{arg2.TotalBytesToReceive}, 进度百分比:{arg2.ProgressPercentage}%");
8955
}
56+
57+
void OnException(object arg1, ExceptionEventArgs arg2)
58+
{
59+
Console.WriteLine($"{arg2.Exception}");
60+
}

0 commit comments

Comments
 (0)