Skip to content

Commit

Permalink
适配AOT
Browse files Browse the repository at this point in the history
  • Loading branch information
JusterZhu committed Nov 15, 2024
1 parent 438315e commit 37b2070
Show file tree
Hide file tree
Showing 21 changed files with 110 additions and 238 deletions.
6 changes: 5 additions & 1 deletion src/c#/GeneralUpdate.Bowl/Bowl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.InteropServices;
using System.Text.Json;
using GeneralUpdate.Bowl.Strategys;
using GeneralUpdate.Common.AOT.JsonContext;
using GeneralUpdate.Common.Shared.Object;

namespace GeneralUpdate.Bowl;
Expand Down Expand Up @@ -45,7 +46,10 @@ private static MonitorParameter CreateParameter()
if(string.IsNullOrWhiteSpace(json))
throw new ArgumentNullException("ProcessInfo environment variable not set !");

var processInfo = JsonSerializer.Deserialize<ProcessInfo>(json);
var processInfo = JsonSerializer.Deserialize<ProcessInfo>(json, ProcessInfoJsonContext.Default.ProcessInfo);
if(processInfo == null)
throw new ArgumentNullException("ProcessInfo json deserialize fail!");

return new MonitorParameter
{
ProcessNameOrId = processInfo.AppName,
Expand Down
23 changes: 13 additions & 10 deletions src/c#/GeneralUpdate.ClientCore/GeneralClientBootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text.Json;
using System.Threading.Tasks;
using GeneralUpdate.ClientCore.Strategys;
using GeneralUpdate.Common.AOT.JsonContext;
using GeneralUpdate.Common.FileBasic;
using GeneralUpdate.Common.Download;
using GeneralUpdate.Common.Internal;
Expand Down Expand Up @@ -141,17 +142,19 @@ private async Task ExecuteWorkflowAsync()
//Request the upgrade information needed by the client and upgrade end, and determine if an upgrade is necessary.
var mainResp = await VersionService.Validate(_configinfo.UpdateUrl
, _configinfo.ClientVersion
,1
, AppType.ClientApp
,_configinfo.AppSecretKey
,_configinfo.Platform
,_configinfo.ProductId);
,_configinfo.ProductId
, VersionRespJsonContext.Default.VersionRespDTO);

var upgradeResp = await VersionService.Validate(_configinfo.UpdateUrl
, _configinfo.UpgradeClientVersion
,2
, AppType.UpgradeApp
,_configinfo.AppSecretKey
,_configinfo.Platform
,_configinfo.ProductId);
,_configinfo.ProductId
, VersionRespJsonContext.Default.VersionRespDTO);

_configinfo.IsUpgradeUpdate = CheckUpgrade(upgradeResp);
_configinfo.IsMainUpdate = CheckUpgrade(mainResp);
Expand All @@ -161,7 +164,7 @@ private async Task ExecuteWorkflowAsync()
if (CanSkip(isForcibly)) return;

_configinfo.Encoding = GetOption(UpdateOption.Encoding) ?? Encoding.Default;
_configinfo.Format = GetOption(UpdateOption.Format) ?? ".zip";
_configinfo.Format = GetOption(UpdateOption.Format) ?? Format.ZIP;
_configinfo.DownloadTimeOut = GetOption(UpdateOption.DownloadTimeOut) == 0 ? 60 : GetOption(UpdateOption.DownloadTimeOut);
_configinfo.DriveEnabled = GetOption(UpdateOption.Drive) ?? false;
_configinfo.TempPath = GeneralFileManager.GetTempDirectory("main_temp");
Expand Down Expand Up @@ -190,7 +193,7 @@ private async Task ExecuteWorkflowAsync()
, _configinfo.BackupDirectory
, _configinfo.Bowl);

_configinfo.ProcessInfo = JsonSerializer.Serialize(processInfo);
_configinfo.ProcessInfo = JsonSerializer.Serialize(processInfo,ProcessInfoJsonContext.Default.ProcessInfo);
}

GeneralFileManager.Backup(_configinfo.InstallPath, _configinfo.BackupDirectory, _notBackupDirectory);
Expand Down Expand Up @@ -263,7 +266,7 @@ private bool CheckUpgrade(VersionRespDTO? response)
return false;
}

if (response.Code == HttpStatus.OK)
if (response.Code == 200)
{
return response.Body.Count > 0;
}
Expand All @@ -276,7 +279,7 @@ private bool CheckUpgrade(VersionRespDTO? response)
/// </summary>
/// <param name="versions"></param>
/// <returns></returns>
private bool CheckForcibly(List<VersionBodyDTO>? versions)
private bool CheckForcibly(List<VersionInfo>? versions)
{
if (versions == null)
return false;
Expand Down Expand Up @@ -314,8 +317,8 @@ private void ExecuteCustomOptions()
{
if (!option.Invoke())
{
EventManager.Instance.Dispatch(this,
new ExceptionEventArgs(null, $"{nameof(option)}Execution failure!"));
var args = new ExceptionEventArgs(null, $"{nameof(option)}Execution failure!");
EventManager.Instance.Dispatch(this,args);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/c#/GeneralUpdate.ClientCore/Pipeline/ZipMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using GeneralUpdate.Common.Internal;
using GeneralUpdate.Common.Internal.Event;
using GeneralUpdate.Common.Internal.Pipeline;
using GeneralUpdate.Common.Shared.Object;
using GeneralUpdate.Zip;
using GeneralUpdate.Zip.Factory;

Expand Down Expand Up @@ -40,8 +41,8 @@ private static OperationType MatchType(string extensionName)
{
var type = extensionName switch
{
".zip" => OperationType.GZip,
".7z" => OperationType.G7z,
Format.ZIP => OperationType.GZip,
Format.SEVENZIP => OperationType.G7z,
_ => OperationType.None
};
return type;
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(ProcessInfo))]
public partial class ProcessInfoJsonContext : JsonSerializerContext;
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(VersionRespDTO))]
public partial class VersionRespJsonContext : JsonSerializerContext;
1 change: 0 additions & 1 deletion src/c#/GeneralUpdate.Common/Download/DownloadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public async Task LaunchTasksAsync()
}
catch (Exception ex)
{
_failedVersions.Add((null, ex.Message));
MultiAllDownloadCompleted?.Invoke(this, new MultiAllDownloadCompletedEventArgs(false, _failedVersions));
throw new Exception($"Download manager error: {ex.Message}", ex);
}
Expand Down
11 changes: 6 additions & 5 deletions src/c#/GeneralUpdate.Common/Download/DownloadTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ public class DownloadTask

private readonly HttpClient _httpClient;
private readonly DownloadManager _manager;
private readonly VersionBodyDTO _version;
private readonly VersionInfo? _version;
private const int DEFAULT_DELTA = 1048576; // 1024*1024
private long _beforBytes;
private long _receivedBytes;
private long _totalBytes;
private Timer _speedTimer;
private Timer? _speedTimer;
private DateTime _startTime;

#endregion Private Members

#region Constructors

public DownloadTask(DownloadManager manager, VersionBodyDTO version)
public DownloadTask(DownloadManager manager, VersionInfo version)
{
_manager = manager;
_version = version;
Expand All @@ -53,7 +53,7 @@ public async Task LaunchAsync()
InitStatisticsEvent();
InitProgressEvent();
InitCompletedEvent();
var path = Path.Combine(_manager.Path, $"{_version.Name}{_manager.Format}");
var path = Path.Combine(_manager.Path, $"{_version?.Name}{_manager.Format}");
await DownloadFileRangeAsync(_version.Url, path);
}
catch (Exception ex)
Expand Down Expand Up @@ -139,7 +139,8 @@ private async Task WriteFileAsync(string tempPath, byte[] chunk, long totalBytes

private void InitStatisticsEvent()
{
if (_speedTimer != null) return;
if (_speedTimer != null)
return;

_speedTimer = new Timer(_ =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ namespace GeneralUpdate.Common.Download
{
public class MultiAllDownloadCompletedEventArgs : EventArgs
{
public MultiAllDownloadCompletedEventArgs()
{ }

public MultiAllDownloadCompletedEventArgs(bool isAllDownloadCompleted, IList<(object, string)> failedVersions)
{
IsAllDownloadCompleted = isAllDownloadCompleted;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using GeneralUpdate.Common.Shared.Object.Enum;

namespace GeneralUpdate.Common.Download
{
Expand Down

This file was deleted.

5 changes: 3 additions & 2 deletions src/c#/GeneralUpdate.Common/FileBasic/BlackListManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using GeneralUpdate.Common.Shared.Object;

namespace GeneralUpdate.Common.FileBasic;

Expand All @@ -12,8 +13,8 @@ public class BlackListManager
private static readonly List<string> _blackFileFormats =
[
".patch",
".7z",
".zip",
Format.SEVENZIP,
Format.ZIP,
".rar",
".tar",
".json",
Expand Down
50 changes: 3 additions & 47 deletions src/c#/GeneralUpdate.Common/Shared/Object/DTO/VersionRespDTO.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using System.Collections.Generic;

namespace GeneralUpdate.Common.Shared.Object
{
public class VersionRespDTO : BaseResponseDTO<List<VersionBodyDTO>>
{ }

public class VersionBodyDTO
{
[JsonPropertyName("recordId")]
public int RecordId { get; set; }

[JsonPropertyName("name")]
public string? Name { get; set; }

[JsonPropertyName("hash")]
public string? Hash { get; set; }

[JsonPropertyName("releaseDate")]
public DateTime? ReleaseDate { get; set; }

[JsonPropertyName("url")]
public string? Url { get; set; }

[JsonPropertyName("version")]
public string? Version { get; set; }

[JsonPropertyName("appType")]
public int? AppType { get; set; }

[JsonPropertyName("platform")]
public int? Platform { get; set; }

[JsonPropertyName("productId")]
public string? ProductId { get; set; }

[JsonPropertyName("isForcibly")]
public bool? IsForcibly { get; set; }

[JsonPropertyName("format")]
public string Format { get; set; }

[JsonPropertyName("size")]
public long? Size { get; set; }
}
}
namespace GeneralUpdate.Common.Shared.Object;
public class VersionRespDTO : BaseResponseDTO<List<VersionInfo>>;
12 changes: 0 additions & 12 deletions src/c#/GeneralUpdate.Common/Shared/Object/Enum/HttpStatus.cs

This file was deleted.

48 changes: 0 additions & 48 deletions src/c#/GeneralUpdate.Common/Shared/Object/Enum/PlatformType.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class GlobalConfigInfo
/// <summary>
/// Version information that needs to be updated.
/// </summary>
public List<VersionBodyDTO> UpdateVersions { get; set; }
public List<VersionInfo> UpdateVersions { get; set; }

/// <summary>
/// The encoding format for file operations.
Expand Down
Loading

0 comments on commit 37b2070

Please sign in to comment.