Skip to content

Commit

Permalink
OSS AOT适配
Browse files Browse the repository at this point in the history
  • Loading branch information
JusterZhu committed Nov 15, 2024
1 parent df6e2b9 commit 92bdf9d
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 138 deletions.
2 changes: 2 additions & 0 deletions src/c#/GeneralUpdate.Client/GeneralUpdate.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ApplicationManifest></ApplicationManifest>
<PublishAot>true</PublishAot>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>

<ItemGroup>
Expand Down
15 changes: 10 additions & 5 deletions src/c#/GeneralUpdate.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace GeneralUpdate.Client
{
internal class Progra
internal class Program
{
private static void Main(string[] args)
{
Expand Down Expand Up @@ -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();
}
Expand All @@ -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)
Expand Down
26 changes: 8 additions & 18 deletions src/c#/GeneralUpdate.ClientCore/GeneralClientOSS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,37 @@ private GeneralClientOSS() { }
/// <summary>
/// Starting an OSS update for windows platform.
/// </summary>
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(() =>
{
try
{
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<List<VersionPO>>(versionsFilePath);
var versions = GeneralFileManager.GetJson<List<VersionOSS>>(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
{
Expand Down Expand Up @@ -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<object, ExceptionEventArgs> callbackAction)
=> AddListener(callbackAction);

private static void AddListener<TArgs>(Action<object, TArgs> callbackAction) where TArgs : EventArgs
{
Debug.Assert(callbackAction != null);
EventManager.Instance.AddListener(callbackAction);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.0-rc.1.24452.1"/>
<PackageReference Include="System.Collections.Immutable" Version="9.0.0-rc.1.24431.7"/>
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.0"/>
<PackageReference Include="System.Collections.Immutable" Version="9.0.0"/>
<PackageReference Include="System.Net.Requests" Version="4.3.0"/>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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<VersionOSS>))]
public partial class VersionOSSJsonContext : JsonSerializerContext;
7 changes: 6 additions & 1 deletion src/c#/GeneralUpdate.Common/FileBasic/GeneralFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -60,11 +61,15 @@ public static void CreateJson<T>(string targetPath, T obj) where T : class
File.WriteAllText(targetPath, jsonString);
}

public static T? GetJson<T>(string path) where T : class
public static T? GetJson<T>(string path, JsonTypeInfo<T>? 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<T>(json);
}

Expand Down
11 changes: 3 additions & 8 deletions src/c#/GeneralUpdate.Common/GeneralUpdate.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.0-rc.1.24431.7" />
<PackageReference Include="System.Collections.Immutable" Version="9.0.0-rc.1.24431.7" />
<PackageReference Include="System.Text.Json" Version="9.0.0-rc.1.24431.7" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.0" />
<PackageReference Include="System.Collections.Immutable" Version="9.0.0" />
<PackageReference Include="System.Text.Json" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
<None Remove="GeneralUpdate.Common.csproj.DotSettings" />
</ItemGroup>

</Project>

This file was deleted.

10 changes: 5 additions & 5 deletions src/c#/GeneralUpdate.Common/Internal/Event/EventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type, Delegate> _dicDelegates = new Dictionary<Type, Delegate>();
private bool disposed = false;
private Dictionary<Type, Delegate> _dicDelegates = new();
private bool _disposed = false;

private EventManager() { }

Expand Down Expand Up @@ -67,10 +67,10 @@ public void Dispatch<TEventArgs>(object sender, TEventArgs eventArgs) where TEve

public void Dispose()
{
if (!this.disposed)
if (!this._disposed)
{
_dicDelegates.Clear();
disposed = true;
_disposed = true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace GeneralUpdate.Common.Shared.Object
{
public class ParamsOSS
public class GlobalConfigInfoOSS
{
[JsonPropertyName("Url")]
public string Url { get; set; }
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace GeneralUpdate.Common.Shared.Object
/// <summary>
/// Version data persistence.
/// </summary>
public class VersionPO
public class VersionOSS
{
/// <summary>
/// Update package release time.
Expand Down
62 changes: 17 additions & 45 deletions src/c#/GeneralUpdate.Core/GeneralUpdateOSS.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -29,53 +24,30 @@ private GeneralUpdateOSS() { }
/// <returns></returns>
public static async Task Start()=> await BaseStart();

public static void AddListenerMultiAllDownloadCompleted(Action<object, MultiAllDownloadCompletedEventArgs> callbackAction)
=> AddListener(callbackAction);

public static void AddListenerMultiDownloadProgress(Action<object, MultiDownloadProgressChangedEventArgs> callbackAction)
=> AddListener(callbackAction);

public static void AddListenerMultiDownloadCompleted(Action<object, MultiDownloadCompletedEventArgs> callbackAction)
=> AddListener(callbackAction);

public static void AddListenerMultiDownloadError(Action<object, MultiDownloadErrorEventArgs> callbackAction)
=> AddListener(callbackAction);

public static void AddListenerMultiDownloadStatistics(Action<object, MultiDownloadStatisticsEventArgs> callbackAction)
=> AddListener(callbackAction);

public static void AddListenerException(Action<object, ExceptionEventArgs> callbackAction)
=> AddListener(callbackAction);

public static void AddListenerDownloadConfigProcess(Action<object, OSSDownloadArgs> callbackAction)
=> AddListener(callbackAction);

#endregion Public Methods

#region Private Methods

private static void AddListener<TArgs>(Action<object, TArgs> callbackAction) where TArgs : EventArgs
{
Debug.Assert(callbackAction != null);
EventManager.Instance.AddListener(callbackAction);
}


/// <summary>
/// The underlying update method.
/// </summary>
/// <typeparam name="T">The class that needs to be injected with the corresponding platform update policy or inherits the abstract update policy.</typeparam>
/// <param name="args">List of parameter.</param>
/// <returns></returns>
private static async Task BaseStart()
{
var json = Environment.GetEnvironmentVariable("ParamsOSS", EnvironmentVariableTarget.User);
if (string.IsNullOrWhiteSpace(json))
return;

var parameter = JsonSerializer.Deserialize<ParamsOSS>(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<GlobalConfigInfoOSS>(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
Expand Down
Loading

0 comments on commit 92bdf9d

Please sign in to comment.