diff --git a/src/c#/GeneralUpdate.Client/GeneralUpdate.Client.csproj b/src/c#/GeneralUpdate.Client/GeneralUpdate.Client.csproj index 94ea81d5..72e8d98f 100644 --- a/src/c#/GeneralUpdate.Client/GeneralUpdate.Client.csproj +++ b/src/c#/GeneralUpdate.Client/GeneralUpdate.Client.csproj @@ -6,6 +6,8 @@ enable enable + true + win-x64 diff --git a/src/c#/GeneralUpdate.Client/Program.cs b/src/c#/GeneralUpdate.Client/Program.cs index 360f6cd3..b807e4f5 100644 --- a/src/c#/GeneralUpdate.Client/Program.cs +++ b/src/c#/GeneralUpdate.Client/Program.cs @@ -8,7 +8,7 @@ namespace GeneralUpdate.Client { - internal class Progra + internal class Program { private static void Main(string[] args) { @@ -67,8 +67,14 @@ private static void Main(string[] args) .Option(UpdateOption.Format, Format.ZIP) .LaunchAsync(); });*/ - - GeneralClientOSS.Start(new ParamsOSS("http://192.168.50.203/versions.json", "GeneralUpdate.Client.exe","1.0.0.0", "versions.json")); + + var paramsOSS = new GlobalConfigInfoOSS(); + paramsOSS.Url = "http://192.168.50.203/versions.json"; + paramsOSS.CurrentVersion = "1.0.0.0"; + paramsOSS.VersionFileName = "versions.json"; + paramsOSS.AppName = "GeneralUpdate.Client.exe"; + paramsOSS.Encoding = Encoding.UTF8.WebName; + GeneralClientOSS.Start(paramsOSS); Console.Read(); } @@ -85,8 +91,7 @@ private static void OnMultiAllDownloadCompleted(object arg1, MultiAllDownloadCom private static void OnMultiDownloadCompleted(object arg1, MultiDownloadCompletedEventArgs arg2) { - var v = arg2.Version as VersionInfo; - Debug.WriteLine(v.Version); + var v = arg2.Version; } private static void OnMultiDownloadStatistics(object arg1, MultiDownloadStatisticsEventArgs arg2) diff --git a/src/c#/GeneralUpdate.ClientCore/GeneralClientOSS.cs b/src/c#/GeneralUpdate.ClientCore/GeneralClientOSS.cs index 4d2a127f..8c7db0b9 100644 --- a/src/c#/GeneralUpdate.ClientCore/GeneralClientOSS.cs +++ b/src/c#/GeneralUpdate.ClientCore/GeneralClientOSS.cs @@ -23,7 +23,7 @@ private GeneralClientOSS() { } /// /// Starting an OSS update for windows platform. /// - public static async Task Start(ParamsOSS configParams, string upgradeAppName = "GeneralUpdate.Upgrade.exe") + public static async Task Start(GlobalConfigInfoOSS configGlobalConfigInfo, string upgradeAppName = "GeneralUpdate.Upgrade.exe") { await Task.Run(() => { @@ -31,30 +31,29 @@ await Task.Run(() => { var basePath = Thread.GetDomain().BaseDirectory; //Download the version information file from OSS to be updated.(JSON) - var versionsFilePath = Path.Combine(basePath, configParams.VersionFileName); - DownloadFile(configParams.Url, versionsFilePath); + var versionsFilePath = Path.Combine(basePath, configGlobalConfigInfo.VersionFileName); + DownloadFile(configGlobalConfigInfo.Url, versionsFilePath); if (!File.Exists(versionsFilePath)) return; - var versions = GeneralFileManager.GetJson>(versionsFilePath); + var versions = GeneralFileManager.GetJson>(versionsFilePath); if (versions == null || versions.Count == 0) return; versions = versions.OrderByDescending(x => x.PubTime).ToList(); var newVersion = versions.First(); //Determine whether the current client version needs to be upgraded. - if (!IsUpgrade(configParams.CurrentVersion, newVersion.Version)) + if (!IsUpgrade(configGlobalConfigInfo.CurrentVersion, newVersion.Version)) return; - var json = JsonSerializer.Serialize(configParams); - Environment.SetEnvironmentVariable("ParamsOSS", json, EnvironmentVariableTarget.User); - //If you confirm that an update is required, start the upgrade application. var appPath = Path.Combine(basePath, $"{upgradeAppName}"); if (!File.Exists(appPath)) throw new Exception($"The application does not exist {upgradeAppName} !"); + var json = JsonSerializer.Serialize(configGlobalConfigInfo); + Environment.SetEnvironmentVariable("GlobalConfigInfoOSS", json, EnvironmentVariableTarget.User); Process.Start(appPath); } catch (Exception ex) { - EventManager.Instance.Dispatch(new GeneralClientOSS(), new ExceptionEventArgs(ex)); + throw new Exception(ex.Message + "\n" + ex.StackTrace); } finally { @@ -86,13 +85,4 @@ private static void DownloadFile(string url, string path) using var webClient = new WebClient(); webClient.DownloadFile(new Uri(url), path); } - - public static void AddListenerException(Action callbackAction) - => AddListener(callbackAction); - - private static void AddListener(Action callbackAction) where TArgs : EventArgs - { - Debug.Assert(callbackAction != null); - EventManager.Instance.AddListener(callbackAction); - } } \ No newline at end of file diff --git a/src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj b/src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj index a39dacbe..894bc2e0 100644 --- a/src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj +++ b/src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj @@ -18,8 +18,8 @@ - - + + diff --git a/src/c#/GeneralUpdate.Common/AOT/JsonContext/GlobalConfigInfoOSSJsonContext.cs b/src/c#/GeneralUpdate.Common/AOT/JsonContext/GlobalConfigInfoOSSJsonContext.cs new file mode 100644 index 00000000..cca843e7 --- /dev/null +++ b/src/c#/GeneralUpdate.Common/AOT/JsonContext/GlobalConfigInfoOSSJsonContext.cs @@ -0,0 +1,7 @@ +using System.Text.Json.Serialization; +using GeneralUpdate.Common.Shared.Object; + +namespace GeneralUpdate.Common.AOT.JsonContext; + +[JsonSerializable(typeof(GlobalConfigInfoOSS))] +public partial class GlobalConfigInfoOSSJsonContext : JsonSerializerContext; \ No newline at end of file diff --git a/src/c#/GeneralUpdate.Common/AOT/JsonContext/VersionOSSJsonContext.cs b/src/c#/GeneralUpdate.Common/AOT/JsonContext/VersionOSSJsonContext.cs new file mode 100644 index 00000000..f90d5ef8 --- /dev/null +++ b/src/c#/GeneralUpdate.Common/AOT/JsonContext/VersionOSSJsonContext.cs @@ -0,0 +1,8 @@ +using System.Collections.Generic; +using System.Text.Json.Serialization; +using GeneralUpdate.Common.Shared.Object; + +namespace GeneralUpdate.Common.AOT.JsonContext; + +[JsonSerializable(typeof(List))] +public partial class VersionOSSJsonContext : JsonSerializerContext; \ No newline at end of file diff --git a/src/c#/GeneralUpdate.Common/FileBasic/GeneralFileManager.cs b/src/c#/GeneralUpdate.Common/FileBasic/GeneralFileManager.cs index b366cddd..414d16a2 100644 --- a/src/c#/GeneralUpdate.Common/FileBasic/GeneralFileManager.cs +++ b/src/c#/GeneralUpdate.Common/FileBasic/GeneralFileManager.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Text.Json; +using System.Text.Json.Serialization.Metadata; using System.Threading; using GeneralUpdate.Common.HashAlgorithms; @@ -60,11 +61,15 @@ public static void CreateJson(string targetPath, T obj) where T : class File.WriteAllText(targetPath, jsonString); } - public static T? GetJson(string path) where T : class + public static T? GetJson(string path, JsonTypeInfo? typeInfo = null) where T : class { if (File.Exists(path)) { var json = File.ReadAllText(path); + if (typeInfo != null) + { + return JsonSerializer.Deserialize(json, typeInfo); + } return JsonSerializer.Deserialize(json); } diff --git a/src/c#/GeneralUpdate.Common/GeneralUpdate.Common.csproj b/src/c#/GeneralUpdate.Common/GeneralUpdate.Common.csproj index f842ca31..d4cfe5ca 100644 --- a/src/c#/GeneralUpdate.Common/GeneralUpdate.Common.csproj +++ b/src/c#/GeneralUpdate.Common/GeneralUpdate.Common.csproj @@ -7,13 +7,8 @@ - - - + + + - - - - - diff --git a/src/c#/GeneralUpdate.Common/GeneralUpdate.Common.csproj.DotSettings b/src/c#/GeneralUpdate.Common/GeneralUpdate.Common.csproj.DotSettings deleted file mode 100644 index 632c2681..00000000 --- a/src/c#/GeneralUpdate.Common/GeneralUpdate.Common.csproj.DotSettings +++ /dev/null @@ -1,2 +0,0 @@ - - CSharp120 \ No newline at end of file diff --git a/src/c#/GeneralUpdate.Common/Internal/Event/EventManager.cs b/src/c#/GeneralUpdate.Common/Internal/Event/EventManager.cs index 4308aab0..24f8a367 100644 --- a/src/c#/GeneralUpdate.Common/Internal/Event/EventManager.cs +++ b/src/c#/GeneralUpdate.Common/Internal/Event/EventManager.cs @@ -5,10 +5,10 @@ namespace GeneralUpdate.Common.Internal.Event { public class EventManager : IDisposable { - private static readonly object _lockObj = new object(); + private static readonly object _lockObj = new(); private static EventManager _instance; - private Dictionary _dicDelegates = new Dictionary(); - private bool disposed = false; + private Dictionary _dicDelegates = new(); + private bool _disposed = false; private EventManager() { } @@ -67,10 +67,10 @@ public void Dispatch(object sender, TEventArgs eventArgs) where TEve public void Dispose() { - if (!this.disposed) + if (!this._disposed) { _dicDelegates.Clear(); - disposed = true; + _disposed = true; } } } diff --git a/src/c#/GeneralUpdate.Common/Shared/Object/ParamsOSS.cs b/src/c#/GeneralUpdate.Common/Shared/Object/GlobalConfigInfoOSS.cs similarity index 75% rename from src/c#/GeneralUpdate.Common/Shared/Object/ParamsOSS.cs rename to src/c#/GeneralUpdate.Common/Shared/Object/GlobalConfigInfoOSS.cs index 46053bd9..1bd152ba 100644 --- a/src/c#/GeneralUpdate.Common/Shared/Object/ParamsOSS.cs +++ b/src/c#/GeneralUpdate.Common/Shared/Object/GlobalConfigInfoOSS.cs @@ -3,7 +3,7 @@ namespace GeneralUpdate.Common.Shared.Object { - public class ParamsOSS + public class GlobalConfigInfoOSS { [JsonPropertyName("Url")] public string Url { get; set; } @@ -16,12 +16,15 @@ public class ParamsOSS [JsonPropertyName("VersionFileName")] public string VersionFileName { get; set; } + + [JsonPropertyName("Encoding")] + public string Encoding { get; set; } - public ParamsOSS() + public GlobalConfigInfoOSS() { } - public ParamsOSS(string url, string appName, string currentVersion, string versionFileName) + public GlobalConfigInfoOSS(string url, string appName, string currentVersion, string versionFileName) { Url = url ?? throw new ArgumentNullException(nameof(url)); AppName = appName ?? throw new ArgumentNullException(nameof(appName)); diff --git a/src/c#/GeneralUpdate.Common/Shared/Object/VersionPO.cs b/src/c#/GeneralUpdate.Common/Shared/Object/VersionOSS.cs similarity index 97% rename from src/c#/GeneralUpdate.Common/Shared/Object/VersionPO.cs rename to src/c#/GeneralUpdate.Common/Shared/Object/VersionOSS.cs index 902dcfb8..6fbcf120 100644 --- a/src/c#/GeneralUpdate.Common/Shared/Object/VersionPO.cs +++ b/src/c#/GeneralUpdate.Common/Shared/Object/VersionOSS.cs @@ -6,7 +6,7 @@ namespace GeneralUpdate.Common.Shared.Object /// /// Version data persistence. /// - public class VersionPO + public class VersionOSS { /// /// Update package release time. diff --git a/src/c#/GeneralUpdate.Core/GeneralUpdateOSS.cs b/src/c#/GeneralUpdate.Core/GeneralUpdateOSS.cs index cfbd0cc0..d30b167d 100644 --- a/src/c#/GeneralUpdate.Core/GeneralUpdateOSS.cs +++ b/src/c#/GeneralUpdate.Core/GeneralUpdateOSS.cs @@ -1,16 +1,11 @@ using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Diagnostics.Contracts; -using System.IO; -using System.Runtime.InteropServices; using System.Text.Json; using System.Threading.Tasks; +using GeneralUpdate.Common.AOT.JsonContext; using GeneralUpdate.Common.Download; -using GeneralUpdate.Common.FileBasic; using GeneralUpdate.Common.Internal; using GeneralUpdate.Common.Internal.Event; -using GeneralUpdate.Common.Internal.Strategy; using GeneralUpdate.Common.Shared.Object; using GeneralUpdate.Core.Internal; using GeneralUpdate.Core.Strategys; @@ -29,53 +24,30 @@ private GeneralUpdateOSS() { } /// public static async Task Start()=> await BaseStart(); - public static void AddListenerMultiAllDownloadCompleted(Action callbackAction) - => AddListener(callbackAction); - - public static void AddListenerMultiDownloadProgress(Action callbackAction) - => AddListener(callbackAction); - - public static void AddListenerMultiDownloadCompleted(Action callbackAction) - => AddListener(callbackAction); - - public static void AddListenerMultiDownloadError(Action callbackAction) - => AddListener(callbackAction); - - public static void AddListenerMultiDownloadStatistics(Action callbackAction) - => AddListener(callbackAction); - - public static void AddListenerException(Action callbackAction) - => AddListener(callbackAction); - - public static void AddListenerDownloadConfigProcess(Action callbackAction) - => AddListener(callbackAction); - #endregion Public Methods #region Private Methods - - private static void AddListener(Action callbackAction) where TArgs : EventArgs - { - Debug.Assert(callbackAction != null); - EventManager.Instance.AddListener(callbackAction); - } - + /// /// The underlying update method. /// - /// The class that needs to be injected with the corresponding platform update policy or inherits the abstract update policy. - /// List of parameter. - /// private static async Task BaseStart() { - var json = Environment.GetEnvironmentVariable("ParamsOSS", EnvironmentVariableTarget.User); - if (string.IsNullOrWhiteSpace(json)) - return; - - var parameter = JsonSerializer.Deserialize(json); - var strategy = new OSSStrategy(); - strategy.Create(parameter); - await strategy.ExecuteAsync(); + try + { + var json = Environment.GetEnvironmentVariable("GlobalConfigInfoOSS", EnvironmentVariableTarget.User); + if (string.IsNullOrWhiteSpace(json)) + return; + + var parameter = JsonSerializer.Deserialize(json, GlobalConfigInfoOSSJsonContext.Default.GlobalConfigInfoOSS); + var strategy = new OSSStrategy(); + strategy.Create(parameter); + await strategy.ExecuteAsync(); + } + catch (Exception exception) + { + throw new Exception(exception.Message + "\n" + exception.StackTrace); + } } #endregion Private Methods diff --git a/src/c#/GeneralUpdate.Core/Strategys/OSSStrategy.cs b/src/c#/GeneralUpdate.Core/Strategys/OSSStrategy.cs index 1dcf3671..715e48fc 100644 --- a/src/c#/GeneralUpdate.Core/Strategys/OSSStrategy.cs +++ b/src/c#/GeneralUpdate.Core/Strategys/OSSStrategy.cs @@ -7,10 +7,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using GeneralUpdate.Common.AOT.JsonContext; using GeneralUpdate.Common.FileBasic; using GeneralUpdate.Common.Download; -using GeneralUpdate.Common.Internal; -using GeneralUpdate.Common.Internal.Event; using GeneralUpdate.Common.Shared.Object; namespace GeneralUpdate.Core.Strategys @@ -20,16 +19,15 @@ public class OSSStrategy #region Private Members private readonly string _appPath = AppDomain.CurrentDomain.BaseDirectory; - private const string _format = ".zip"; - private const int _timeOut = 60; - private ParamsOSS _parameter; - private Encoding _encoding; + private const string Format = ".zip"; + private const int TimeOut = 60; + private GlobalConfigInfoOSS? _parameter; #endregion Private Members #region Public Methods - public void Create(ParamsOSS parameter) + public void Create(GlobalConfigInfoOSS parameter) => _parameter = parameter; public async Task ExecuteAsync() @@ -38,15 +36,15 @@ public async Task ExecuteAsync() { //1.Download the JSON version configuration file. var jsonPath = Path.Combine(_appPath, _parameter.VersionFileName); - if (!File.Exists(jsonPath)) throw new FileNotFoundException(jsonPath); + if (!File.Exists(jsonPath)) + throw new FileNotFoundException(jsonPath); //2.Parse the JSON version configuration file content. - var versions = GeneralFileManager.GetJson>(jsonPath); + var versions = GeneralFileManager.GetJson>(jsonPath, VersionOSSJsonContext.Default.ListVersionOSS); if (versions == null) throw new NullReferenceException(nameof(versions)); versions = versions.OrderBy(v => v.PubTime).ToList(); - //3.Download version by version according to the version of the configuration file. await DownloadVersions(versions); UnZip(versions); @@ -56,7 +54,7 @@ public async Task ExecuteAsync() } catch (Exception ex) { - EventManager.Instance.Dispatch(this, new ExceptionEventArgs(ex)); + throw new Exception(ex.Message + "\n" + ex.StackTrace); } finally { @@ -72,22 +70,30 @@ public async Task ExecuteAsync() /// Download all updated versions version by version. /// /// The collection of version information to be updated as described in the configuration file. - private async Task DownloadVersions(List versions) + private async Task DownloadVersions(List versions) { - var manager = new DownloadManager(_appPath, _format, _timeOut); - foreach (var versionInfo in versions) + try { - var version = new VersionBodyDTO + var manager = new DownloadManager(_appPath, Format, TimeOut); + foreach (var versionInfo in versions) { - Name = versionInfo.PacketName, - Version = versionInfo.Version, - Url = versionInfo.Url, - Format = _format, - Hash = versionInfo.Hash - }; - manager.Add(new DownloadTask(manager, version)); + var version = new VersionBodyDTO + { + Name = versionInfo.PacketName, + Version = versionInfo.Version, + Url = versionInfo.Url, + Format = Format, + Hash = versionInfo.Hash + }; + manager.Add(new DownloadTask(manager, version)); + } + + await manager.LaunchTasksAsync(); + } + catch (Exception e) + { + throw new Exception(e.Message + "\n" + e.StackTrace); } - await manager.LaunchTasksAsync(); } /// @@ -101,31 +107,20 @@ private void LaunchApp() Process.Start(appPath); } - private bool UnZip(List versions) + private void UnZip(List versions) { - try + var encoding = Encoding.GetEncoding(_parameter.Encoding); + foreach (var version in versions) { - bool isCompleted = true; - foreach (var version in versions) + var zipFilePath = Path.Combine(_appPath, $"{version.PacketName}{Format}"); + var zipFactory = new GeneralZipFactory(); + zipFactory.Completed += (sender, e) => { - var zipFilePath = Path.Combine(_appPath, $"{version.PacketName}.zip"); - var zipFactory = new GeneralZipFactory(); - zipFactory.UnZipProgress += (sender, e) => - EventManager.Instance.Dispatch(this, new MultiDownloadProgressChangedEventArgs(version, ProgressType.Updatefile, "Updating file...")); - zipFactory.Completed += (sender, e) => - { - isCompleted = e.IsCompleted; - if (File.Exists(zipFilePath)) File.Delete(zipFilePath); - }; - zipFactory.CreateOperate(OperationType.GZip, version.PacketName, zipFilePath, _appPath, false, _encoding); - zipFactory.UnZip(); - } - return isCompleted; - } - catch (Exception exception) - { - EventManager.Instance.Dispatch(this, new ExceptionEventArgs(exception)); - return false; + if (File.Exists(zipFilePath)) + File.Delete(zipFilePath); + }; + zipFactory.CreateOperate(OperationType.GZip, version.PacketName, zipFilePath, _appPath, false, encoding); + zipFactory.UnZip(); } } diff --git a/src/c#/GeneralUpdate.Upgrad/GeneralUpdate.Upgrad.csproj b/src/c#/GeneralUpdate.Upgrad/GeneralUpdate.Upgrad.csproj index 0f8bd882..0ecae939 100644 --- a/src/c#/GeneralUpdate.Upgrad/GeneralUpdate.Upgrad.csproj +++ b/src/c#/GeneralUpdate.Upgrad/GeneralUpdate.Upgrad.csproj @@ -2,13 +2,20 @@ Exe - net8.0 + net9.0 enable enable + true + win-x64 + + + all + + diff --git a/src/c#/GeneralUpdate.Upgrad/Program.cs b/src/c#/GeneralUpdate.Upgrad/Program.cs index 3846bdba..dce6bb79 100644 --- a/src/c#/GeneralUpdate.Upgrad/Program.cs +++ b/src/c#/GeneralUpdate.Upgrad/Program.cs @@ -80,9 +80,7 @@ private static void Main(string[] args) .LaunchAsync(); });*/ - GeneralUpdateOSS.Start(); - Console.Read(); }