Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 4 additions & 89 deletions src/c#/GeneralUpdate.Core/Bootstrap/GeneralUpdateBootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ namespace GeneralUpdate.Core;
/// <list type="bullet">
/// <item><see cref="AppType.Client"/> — validate versions, download, start upgrade process</item>
/// <item><see cref="AppType.Upgrade"/> — receive ProcessInfo, apply updates, start main app</item>
/// <item><see cref="AppType.OSS"/> — OSS-based cloud storage update</item>
/// <item><see cref="AppType.OSSClient"/> — OSS client: download version config, start upgrade process</item>
/// <item><see cref="AppType.OSSUpgrade"/> — OSS upgrade: download packages from cloud, start main app</item>
/// </list>
/// </summary>
/// <remarks>
Expand Down Expand Up @@ -71,7 +72,8 @@ public override async Task<GeneralUpdateBootstrap> LaunchAsync()
{
AppType.Client => await LaunchWithStrategy(new ClientUpdateStrategy()),
AppType.Upgrade => await LaunchWithStrategy(new UpgradeUpdateStrategy()),
AppType.OSS => await LaunchOssAsync(),
AppType.OSSClient => await LaunchWithStrategy(new OSSUpdateStrategy(AppType.OSSClient)),
AppType.OSSUpgrade => await LaunchWithStrategy(new OSSUpdateStrategy(AppType.OSSUpgrade)),
_ => await LaunchWithStrategy(new ClientUpdateStrategy())
};
}
Expand Down Expand Up @@ -153,73 +155,6 @@ private async Task<GeneralUpdateBootstrap> LaunchWithStrategy(IStrategy roleStra
return this;
}

/// <summary>OSS workflow: download packages from cloud storage, apply updates.</summary>
private async Task<GeneralUpdateBootstrap> LaunchOssAsync()
{
try
{
GeneralTracer.Debug("LaunchOssAsync start.");

var json = Environments.GetEnvironmentVariable("GlobalConfigInfoOSS");
if (!string.IsNullOrWhiteSpace(json))
{
var strategy = new OSSUpdateStrategy();
strategy.Create(_configInfo);
await strategy.ExecuteAsync();
return this;
}

// Client-side OSS
var basePath = AppDomain.CurrentDomain.BaseDirectory;
var versionFileName = $"{_configInfo.MainAppName ?? _configInfo.AppName}_versions.json";
var versionsFilePath = Path.Combine(basePath, versionFileName);

DownloadOssFile(_configInfo.UpdateUrl, versionsFilePath);
if (!File.Exists(versionsFilePath)) return this;

var versions = StorageManager.GetJson<List<VersionOSS>>(versionsFilePath,
VersionOSSJsonContext.Default.ListVersionOSS);
if (versions == null || versions.Count == 0) return this;

versions = versions.OrderByDescending(x => x.PubTime).ToList();
var newVersion = versions.First();

if (!IsOssUpgrade(_configInfo.ClientVersion, newVersion.Version))
{
GeneralTracer.Info("LaunchOssAsync: no upgrade needed.");
return this;
}

// Use user-configured AppName, fall back to default updater name
var upgradeAppName = !string.IsNullOrWhiteSpace(_configInfo.AppName) && _configInfo.AppName != "Update.exe"
? _configInfo.AppName
: "GeneralUpdate.Upgrade.exe";
var appPath = Path.Combine(basePath, upgradeAppName);
if (!File.Exists(appPath))
throw new Exception($"Upgrade application not found: {upgradeAppName}");

var ossConfig = new GlobalConfigInfoOSS
{
AppName = _configInfo.MainAppName ?? _configInfo.AppName,
CurrentVersion = _configInfo.ClientVersion,
VersionFileName = versionFileName,
Encoding = (_configInfo.Encoding?.CodePage ?? Encoding.UTF8.CodePage).ToString(),
Url = _configInfo.UpdateUrl
};

var serialized = JsonSerializer.Serialize(ossConfig,
GlobalConfigInfoOSSJsonContext.Default.GlobalConfigInfoOSS);
Environments.SetEnvironmentVariable("GlobalConfigInfoOSS", serialized);
Process.Start(appPath);
await GracefulExit.CurrentProcessAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
GeneralTracer.Error("LaunchOssAsync failed.", ex);
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(ex, ex.Message));
}
return this;
}

// ════════════════════════════════════════════════════════════════
// Configuration
Expand Down Expand Up @@ -373,26 +308,6 @@ private async Task CallSmallBowlHomeAsync(string processName)
}
}

private static void DownloadOssFile(string url, string path)
{
if (File.Exists(path))
{
File.SetAttributes(path, FileAttributes.Normal);
File.Delete(path);
}
using var webClient = new System.Net.WebClient();
webClient.DownloadFile(new Uri(url), path);
}

private static bool IsOssUpgrade(string clientVersion, string serverVersion)
{
if (string.IsNullOrWhiteSpace(clientVersion) || string.IsNullOrWhiteSpace(serverVersion))
return false;
return Version.TryParse(clientVersion, out var cv)
&& Version.TryParse(serverVersion, out var sv)
&& cv < sv;
}

// ════════════════════════════════════════════════════════════════
// Strategy & Events
// ════════════════════════════════════════════════════════════════
Expand Down
7 changes: 5 additions & 2 deletions src/c#/GeneralUpdate.Core/Configuration/AppType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public enum AppType
/// <summary>Upgrade application — applies downloaded update packages, starts main app.</summary>
Upgrade = 2,

/// <summary>OSS (Object Storage Service) update mode — downloads from cloud storage.</summary>
OSS = 3
/// <summary>OSS client mode — checks version config, starts upgrade process.</summary>
OSSClient = 3,

/// <summary>OSS upgrade mode — downloads packages from OSS, deploys to client.</summary>
OSSUpgrade = 4
}
Loading
Loading