Skip to content

Commit

Permalink
添加测试代码
Browse files Browse the repository at this point in the history
  • Loading branch information
JusterZhu committed Nov 13, 2024
1 parent 5e9018c commit 2b8a7d1
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 39 deletions.
12 changes: 7 additions & 5 deletions src/c#/GeneralUpdate.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ private static void Main(string[] args)
await DifferentialCore.Instance?.Dirty(source, patch);
});*/

Task.Run(() =>
Task.Run(async () =>
{
Console.WriteLine("主程序启动辣!!!!");
await Task.Delay(3000);

var configinfo = new Configinfo();
configinfo.UpdateLogUrl = "https://www.baidu.com";
//configinfo.UpdateLogUrl = "https://www.baidu.com";
configinfo.ReportUrl = "http://127.0.0.1:5008/Upgrade/Report";
configinfo.UpdateUrl = "http://127.0.0.1:5008/Upgrade/Verification";

configinfo.AppName = "GeneralUpdate.Upgrade.exe";
configinfo.AppName = "GeneralUpdate.Upgrad.exe";
configinfo.MainAppName = "GeneralUpdate.Client.exe";
configinfo.InstallPath = Thread.GetDomain().BaseDirectory;

Expand Down Expand Up @@ -59,9 +62,8 @@ private static void Main(string[] args)
.AddListenerException(OnException)
.SetConfig(configinfo)
.Option(UpdateOption.DownloadTimeOut, 60)
.Option(UpdateOption.Encoding, Encoding.Default)
.Option(UpdateOption.Encoding, Encoding.UTF8)
.Option(UpdateOption.Format, Format.ZIP)
.Option(UpdateOption.Drive, false)
.LaunchAsync();
});

Expand Down
71 changes: 51 additions & 20 deletions src/c#/GeneralUpdate.ClientCore/GeneralClientBootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,17 @@ private async Task ExecuteWorkflowAsync()
_configinfo.Encoding = GetOption(UpdateOption.Encoding) ?? Encoding.Default;
_configinfo.Format = GetOption(UpdateOption.Format) ?? ".zip";
_configinfo.DownloadTimeOut = GetOption(UpdateOption.DownloadTimeOut) == 0 ? 60 : GetOption(UpdateOption.DownloadTimeOut);
_configinfo.DriveEnabled = GetOption(UpdateOption.Drive);
_configinfo.DriveEnabled = GetOption(UpdateOption.Drive) ?? false;
_configinfo.TempPath = GeneralFileManager.GetTempDirectory("main_temp");

if (_configinfo.IsMainUpdate)
{
_configinfo.UpdateVersions = mainResp.Body.OrderBy(x => x.ReleaseDate).ToList();
_configinfo.LastVersion = _configinfo.UpdateVersions.Last().Version;

//var failed = CheckFail(_configinfo.LastVersion);
//if (failed) return;

//Initialize the process transfer parameter object.
var processInfo = new ProcessInfo(_configinfo.MainAppName
, _configinfo.InstallPath
Expand Down Expand Up @@ -220,6 +223,31 @@ private async Task Download()
await manager.LaunchTasksAsync();
}

/// <summary>
/// Check if there has been a recent update failure.
/// </summary>
/// <param name="version"></param>
/// <returns></returns>
private bool CheckFail(string version)
{
/*
Read the version number of the last failed upgrade from the system environment variables, then compare it with the version number of the current request.
If it is less than or equal to the failed version number, do not perform the update.
*/
var fail = Environment.GetEnvironmentVariable("UpgradeFail", EnvironmentVariableTarget.User);
if (string.IsNullOrEmpty(fail) || string.IsNullOrEmpty(version))
return false;

var failVersion = new Version(fail);
var lastVersion = new Version(version);
return failVersion >= lastVersion;
}

/// <summary>
/// Determine whether the current version verification result indicates that an update is needed.
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
private bool CheckUpgrade(VersionRespDTO? response)
{
if (response == null)
Expand All @@ -235,6 +263,27 @@ private bool CheckUpgrade(VersionRespDTO? response)
return false;
}

/// <summary>
/// During the iteration process, if any version requires a mandatory update, all the update content from this request should be updated.
/// </summary>
/// <param name="versions"></param>
/// <returns></returns>
private bool CheckForcibly(List<VersionBodyDTO>? versions)
{
if (versions == null)
return false;

foreach (var item in versions)
{
if (item.IsForcibly == true)
{
return true;
}
}

return false;
}

/// <summary>
/// User decides if update is required.
/// </summary>
Expand Down Expand Up @@ -274,28 +323,10 @@ private void ClearEnvironmentVariable()
}
catch (Exception ex)
{
EventManager.Instance.Dispatch(this,
new ExceptionEventArgs(ex,
"Error: An unknown error occurred while deleting the environment variable."));
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(ex, "Error: An unknown error occurred while deleting the environment variable."));
}
}

private bool CheckForcibly(List<VersionBodyDTO>? versions)
{
if (versions == null)
return false;

foreach (var item in versions)
{
if (item.IsForcibly == true)
{
return true;
}
}

return false;
}

protected override void ExecuteStrategy()=> throw new NotImplementedException();

protected override Task ExecuteStrategyAsync()=> throw new NotImplementedException();
Expand Down
7 changes: 6 additions & 1 deletion src/c#/GeneralUpdate.ClientCore/Strategys/WindowsStrategy.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using GeneralUpdate.ClientCore.Pipeline;
using GeneralUpdate.Common.FileBasic;
Expand Down Expand Up @@ -88,7 +89,11 @@ public override void StartApp()
if (File.Exists(appPath))
{
Environment.SetEnvironmentVariable("ProcessInfo", _configinfo.ProcessInfo, EnvironmentVariableTarget.User);
Process.Start(appPath);
Process.Start(new ProcessStartInfo
{
UseShellExecute = true,
FileName = appPath
});
}
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading.Tasks;
using GeneralUpdate.Common.Internal.Strategy;
Expand Down Expand Up @@ -48,10 +49,17 @@ public TBootstrap Option<T>(UpdateOption<T> option, T value)

protected T? GetOption<T>(UpdateOption<T>? option)
{
Debug.Assert(option != null && _options.Count != 0);
var val = _options[option];
if (val != null) return (T)val.GetValue();
return default;
try
{
Debug.Assert(option != null && _options.Count != 0);
var val = _options[option];
if (val != null) return (T)val.GetValue();
return default;
}
catch
{
return default;
}
}
}
}
10 changes: 8 additions & 2 deletions src/c#/GeneralUpdate.Core/Strategys/WindowsStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,14 @@ public override void StartApp()
{
var appPath = Path.Combine(_configinfo.InstallPath, _configinfo.MainAppName);
if (File.Exists(appPath))
Process.Start(appPath);

{
Process.Start(new ProcessStartInfo
{
FileName = appPath,
UseShellExecute = true
});
}

Environment.SetEnvironmentVariable("ProcessInfo", null, EnvironmentVariableTarget.User);
}
catch (Exception e)
Expand Down
2 changes: 0 additions & 2 deletions src/c#/GeneralUpdate.Differential/DifferentialCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ private async Task DirtyPatch(string appPath, string patchPath)
try
{
if (!File.Exists(appPath) || !File.Exists(patchPath))
{
return;
}

var newPath = Path.Combine(Path.GetDirectoryName(appPath)!, $"{Path.GetRandomFileName()}_{Path.GetFileName(appPath)}");
await new BinaryHandler().Dirty(appPath, newPath, patchPath);
Expand Down
11 changes: 7 additions & 4 deletions src/c#/GeneralUpdate.Upgrad/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ private static void Main(string[] args)
processor.AddCommand(new InstallDriverCommand(information));
processor.ProcessCommands();*/

Task.Run(() =>
Task.Run(async () =>
{
var jsonPath = @"D:\packet\test.json";
var json = File.ReadAllText(jsonPath);
Environment.SetEnvironmentVariable("ProcessInfo", json, EnvironmentVariableTarget.User);
Console.WriteLine("升级程序启动辣!!!!");
await Task.Delay(3000);

//var jsonPath = @"D:\packet\test.json";
//var json = File.ReadAllText(jsonPath);
//Environment.SetEnvironmentVariable("ProcessInfo", json, EnvironmentVariableTarget.User);

_ = new GeneralUpdateBootstrap() //单个或多个更新包下载通知事件
.AddListenerMultiDownloadProgress(OnMultiDownloadProgressChanged)
Expand Down

0 comments on commit 2b8a7d1

Please sign in to comment.