Skip to content

Commit 4829443

Browse files
committed
Linux适配
1 parent 4bfaf2e commit 4829443

File tree

18 files changed

+394
-168
lines changed

18 files changed

+394
-168
lines changed

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

Lines changed: 66 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using GeneralUpdate.Common.Internal;
77
using GeneralUpdate.Common.Internal.Bootstrap;
88
using GeneralUpdate.Common.Shared.Object;
9+
using GeneralUpdate.Common.Shared.Object.Enum;
910

1011
namespace GeneralUpdate.Client
1112
{
@@ -23,51 +24,57 @@ private static void Main(string[] args)
2324
await DifferentialCore.Instance?.Dirty(source, patch);
2425
});*/
2526

26-
/*Task.Run(() =>
27+
Task.Run(async () =>
2728
{
28-
//Console.WriteLine("主程序启动辣!!!!");
29-
//await Task.Delay(3000);
30-
31-
var configinfo = new Configinfo();
32-
//configinfo.UpdateLogUrl = "https://www.baidu.com";
33-
configinfo.ReportUrl = "http://127.0.0.1:5008/Upgrade/Report";
34-
configinfo.UpdateUrl = "http://127.0.0.1:5008/Upgrade/Verification";
35-
36-
configinfo.AppName = "GeneralUpdate.Upgrad.exe";
37-
configinfo.MainAppName = "GeneralUpdate.Client.exe";
38-
configinfo.InstallPath = Thread.GetDomain().BaseDirectory;
39-
configinfo.Bowl = "Generalupdate.CatBowl.exe";
40-
41-
//当前客户端的版本号
42-
configinfo.ClientVersion = "1.0.0.0";
43-
//当前升级端的版本号
44-
configinfo.UpgradeClientVersion = "1.0.0.0";
45-
46-
//平台
47-
configinfo.Platform = 1;
48-
//产品id
49-
configinfo.ProductId = "a77c9df5-45f8-4ee9-b3ad-b9431ce0b51c";
50-
//应用密钥
51-
configinfo.AppSecretKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
52-
53-
_ = new GeneralClientBootstrap()//单个或多个更新包下载通知事件
54-
.AddListenerMultiDownloadProgress(OnMultiDownloadProgressChanged)
55-
//单个或多个更新包下载速度、剩余下载事件、当前下载版本信息通知事件
56-
.AddListenerMultiDownloadStatistics(OnMultiDownloadStatistics)
57-
//单个或多个更新包下载完成
58-
.AddListenerMultiDownloadCompleted(OnMultiDownloadCompleted)
59-
//完成所有的下载任务通知
60-
.AddListenerMultiAllDownloadCompleted(OnMultiAllDownloadCompleted)
61-
//下载过程出现的异常通知
62-
.AddListenerMultiDownloadError(OnMultiDownloadError)
63-
//整个更新过程出现的任何问题都会通过这个事件通知
64-
.AddListenerException(OnException)
65-
.SetConfig(configinfo)
66-
.Option(UpdateOption.DownloadTimeOut, 60)
67-
.Option(UpdateOption.Encoding, Encoding.UTF8)
68-
.Option(UpdateOption.Format, Format.ZIP)
69-
.LaunchAsync();
70-
});*/
29+
try
30+
{
31+
Console.WriteLine("主程序启动辣!!!!");
32+
await Task.Delay(3000);
33+
34+
var configinfo = new Configinfo
35+
{
36+
//configinfo.UpdateLogUrl = "https://www.baidu.com";
37+
ReportUrl = "http://127.0.0.1:5000/Upgrade/Report",
38+
UpdateUrl = "http://127.0.0.1:5000/Upgrade/Verification",
39+
AppName = "GeneralUpdate.Upgrad.exe",
40+
MainAppName = "GeneralUpdate.Client.exe",
41+
InstallPath = @"D:\迅雷下载\Client", //Thread.GetDomain().BaseDirectory,
42+
//configinfo.Bowl = "Generalupdate.CatBowl.exe";
43+
//当前客户端的版本号
44+
ClientVersion = "1.0.0.0",
45+
//当前升级端的版本号
46+
UpgradeClientVersion = "1.0.0.0",
47+
//平台
48+
Platform = PlatformType.Windwos,
49+
//产品id
50+
ProductId = "a77c9df5-45f8-4ee9-b3ad-b9431ce0b51c",
51+
//应用密钥
52+
AppSecretKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
53+
};
54+
55+
_ = new GeneralClientBootstrap() //单个或多个更新包下载通知事件
56+
.AddListenerMultiDownloadProgress(OnMultiDownloadProgressChanged)
57+
//单个或多个更新包下载速度、剩余下载事件、当前下载版本信息通知事件
58+
.AddListenerMultiDownloadStatistics(OnMultiDownloadStatistics)
59+
//单个或多个更新包下载完成
60+
.AddListenerMultiDownloadCompleted(OnMultiDownloadCompleted)
61+
//完成所有的下载任务通知
62+
.AddListenerMultiAllDownloadCompleted(OnMultiAllDownloadCompleted)
63+
//下载过程出现的异常通知
64+
.AddListenerMultiDownloadError(OnMultiDownloadError)
65+
//整个更新过程出现的任何问题都会通过这个事件通知
66+
.AddListenerException(OnException)
67+
.SetConfig(configinfo)
68+
.Option(UpdateOption.DownloadTimeOut, 60)
69+
.Option(UpdateOption.Encoding, Encoding.UTF8)
70+
.Option(UpdateOption.Format, Format.ZIP)
71+
.LaunchAsync();
72+
}
73+
catch (Exception e)
74+
{
75+
Console.WriteLine(e.Message + "\n" + e.StackTrace);
76+
}
77+
});
7178

7279
/*var paramsOSS = new GlobalConfigInfoOSS();
7380
paramsOSS.Url = "http://192.168.50.203/versions.json";
@@ -82,43 +89,45 @@ private static void Main(string[] args)
8289
hub.AddListenerReceive(Receive);
8390
hub.StartAsync().Wait();*/
8491

85-
Console.Read();
86-
}
87-
88-
private static void Receive(string arg1, string arg2)
89-
{
90-
throw new NotImplementedException();
92+
while (true)
93+
{
94+
var input = Console.ReadLine();
95+
if (input == "exit")
96+
{
97+
break;
98+
}
99+
}
91100
}
92101

93-
94102
private static void OnMultiDownloadError(object arg1, MultiDownloadErrorEventArgs arg2)
95103
{
96-
Debug.WriteLine(arg2.Exception);
104+
var version = arg2.Version as VersionInfo;
105+
Console.WriteLine($"{version.Version} {arg2.Exception}");
97106
}
98107

99108
private static void OnMultiAllDownloadCompleted(object arg1, MultiAllDownloadCompletedEventArgs arg2)
100109
{
101-
Debug.WriteLine(arg2.IsAllDownloadCompleted);
110+
Console.WriteLine(arg2.IsAllDownloadCompleted);
102111
}
103112

104113
private static void OnMultiDownloadCompleted(object arg1, MultiDownloadCompletedEventArgs arg2)
105114
{
106-
var v = arg2.Version;
115+
Console.WriteLine(arg2.Error.ToString());
107116
}
108117

109118
private static void OnMultiDownloadStatistics(object arg1, MultiDownloadStatisticsEventArgs arg2)
110119
{
111-
Debug.WriteLine(arg2.Speed);
120+
Console.WriteLine($"{arg2.Speed}, {arg2.Remaining}");
112121
}
113122

114123
private static void OnMultiDownloadProgressChanged(object arg1, MultiDownloadProgressChangedEventArgs arg2)
115124
{
116-
Debug.WriteLine(arg2.ProgressValue);
125+
Console.WriteLine($"{arg2.TotalBytesToReceive}, {arg2.ProgressValue}");
117126
}
118127

119128
private static void OnException(object arg1, ExceptionEventArgs arg2)
120129
{
121-
Debug.WriteLine(arg2.Exception);
130+
Console.WriteLine($"{arg2.Exception}");
122131
}
123132
}
124133
}

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

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ public class GeneralClientBootstrap : AbstractBootstrap<GeneralClientBootstrap,
3232
private IStrategy? _strategy;
3333
private Func<bool>? _customSkipOption;
3434
private readonly List<Func<bool>> _customOptions = new();
35-
private const string DirectoryName = "app-";
36-
private readonly List<string> _notBackupDirectory = ["fail", DirectoryName];
3735

3836
#region Public Methods
3937

@@ -168,7 +166,7 @@ private async Task ExecuteWorkflowAsync()
168166
_configinfo.DownloadTimeOut = GetOption(UpdateOption.DownloadTimeOut) == 0 ? 60 : GetOption(UpdateOption.DownloadTimeOut);
169167
_configinfo.DriveEnabled = GetOption(UpdateOption.Drive) ?? false;
170168
_configinfo.TempPath = GeneralFileManager.GetTempDirectory("main_temp");
171-
_configinfo.BackupDirectory = Path.Combine(_configinfo.InstallPath, $"{DirectoryName}{_configinfo.ClientVersion}");
169+
_configinfo.BackupDirectory = Path.Combine(_configinfo.InstallPath, $"{GeneralFileManager.DirectoryName}{_configinfo.ClientVersion}");
172170

173171
if (_configinfo.IsMainUpdate)
174172
{
@@ -196,7 +194,7 @@ private async Task ExecuteWorkflowAsync()
196194
_configinfo.ProcessInfo = JsonSerializer.Serialize(processInfo,ProcessInfoJsonContext.Default.ProcessInfo);
197195
}
198196

199-
GeneralFileManager.Backup(_configinfo.InstallPath, _configinfo.BackupDirectory, _notBackupDirectory);
197+
GeneralFileManager.Backup(_configinfo.InstallPath, _configinfo.BackupDirectory, GeneralFileManager.SkipDirectorys);
200198
StrategyFactory();
201199

202200
switch (_configinfo.IsUpgradeUpdate)
@@ -235,7 +233,7 @@ private async Task Download()
235233
}
236234

237235
/// <summary>
238-
/// Check if there has been a recent update failure.
236+
/// Check if there has been a recent update failure.(only windows)
239237
/// </summary>
240238
/// <param name="version"></param>
241239
/// <returns></returns>
@@ -262,14 +260,10 @@ private bool CheckFail(string version)
262260
private bool CheckUpgrade(VersionRespDTO? response)
263261
{
264262
if (response == null)
265-
{
266263
return false;
267-
}
268264

269265
if (response.Code == 200)
270-
{
271266
return response.Body.Count > 0;
272-
}
273267

274268
return false;
275269
}
@@ -305,12 +299,39 @@ private bool CanSkip(bool isForcibly)
305299
return _customSkipOption?.Invoke() == true;
306300
}
307301

302+
private void CallSmallBowlHome(string processName)
303+
{
304+
if(string.IsNullOrWhiteSpace(processName))
305+
return;
306+
307+
try
308+
{
309+
var processes = Process.GetProcessesByName(processName);
310+
if (processes.Length == 0)
311+
{
312+
Debug.WriteLine($"No process named {processName} found.");
313+
return;
314+
}
315+
316+
foreach (var process in processes)
317+
{
318+
Debug.WriteLine($"Killing process {process.ProcessName} (ID: {process.Id})");
319+
process.Kill();
320+
}
321+
}
322+
catch (Exception ex)
323+
{
324+
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(ex, ex.Message));
325+
}
326+
}
327+
308328
/// <summary>
309329
/// Performs all injected custom operations.
310330
/// </summary>
311331
/// <returns></returns>
312332
private void ExecuteCustomOptions()
313333
{
334+
CallSmallBowlHome(_configinfo.Bowl);
314335
if (!_customOptions.Any()) return;
315336

316337
foreach (var option in _customOptions)
@@ -330,7 +351,15 @@ private void ClearEnvironmentVariable()
330351
{
331352
try
332353
{
333-
Environment.SetEnvironmentVariable("ProcessInfo", null, EnvironmentVariableTarget.User);
354+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
355+
{
356+
Environment.SetEnvironmentVariable("ProcessInfo", null, EnvironmentVariableTarget.User);
357+
}
358+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
359+
{
360+
if (File.Exists("ProcessInfo.json"))
361+
File.Delete("ProcessInfo.json");
362+
}
334363
}
335364
catch (Exception ex)
336365
{

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Text.Json;
1010
using System.Threading;
1111
using System.Threading.Tasks;
12+
using GeneralUpdate.Common.AOT.JsonContext;
1213
using GeneralUpdate.Common.FileBasic;
1314
using GeneralUpdate.Common.Internal;
1415
using GeneralUpdate.Common.Internal.Event;
@@ -34,7 +35,7 @@ await Task.Run(() =>
3435
var versionsFilePath = Path.Combine(basePath, configGlobalConfigInfo.VersionFileName);
3536
DownloadFile(configGlobalConfigInfo.Url, versionsFilePath);
3637
if (!File.Exists(versionsFilePath)) return;
37-
var versions = GeneralFileManager.GetJson<List<VersionOSS>>(versionsFilePath);
38+
var versions = GeneralFileManager.GetJson<List<VersionOSS>>(versionsFilePath, VersionOSSJsonContext.Default.ListVersionOSS);
3839
if (versions == null || versions.Count == 0) return;
3940
versions = versions.OrderByDescending(x => x.PubTime).ToList();
4041
var newVersion = versions.First();

0 commit comments

Comments
 (0)