JT
1 year ago
17 changed files with 229 additions and 83 deletions
@ -1,6 +1,13 @@
|
||||
namespace StabilityMatrix.Helper; |
||||
using System; |
||||
using System.Threading.Tasks; |
||||
using StabilityMatrix.Models; |
||||
|
||||
namespace StabilityMatrix.Helper; |
||||
|
||||
public interface IUpdateHelper |
||||
{ |
||||
void StartCheckingForUpdates(); |
||||
Task StartCheckingForUpdates(); |
||||
|
||||
Task DownloadUpdate(UpdateInfo updateInfo, |
||||
IProgress<ProgressReport> progress); |
||||
} |
@ -1,51 +1,114 @@
|
||||
using System; |
||||
using System.IO; |
||||
using System.Net.Http; |
||||
using System.Reflection; |
||||
using System.Text.Json; |
||||
using System.Threading.Tasks; |
||||
using System.Windows.Threading; |
||||
using AutoUpdaterDotNET; |
||||
using Microsoft.Extensions.Logging; |
||||
using StabilityMatrix.Models; |
||||
using StabilityMatrix.Services; |
||||
|
||||
namespace StabilityMatrix.Helper; |
||||
|
||||
public class UpdateHelper : IUpdateHelper |
||||
{ |
||||
private readonly ILogger<UpdateHelper> logger; |
||||
private readonly IHttpClientFactory httpClientFactory; |
||||
private readonly IDownloadService downloadService; |
||||
private readonly DispatcherTimer timer = new(); |
||||
|
||||
public UpdateHelper(ILogger<UpdateHelper> logger) |
||||
private static readonly string UpdateFolder = |
||||
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Update"); |
||||
|
||||
public static readonly string ExecutablePath = |
||||
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Update", "StabilityMatrix.exe"); |
||||
|
||||
public UpdateHelper(ILogger<UpdateHelper> logger, IHttpClientFactory httpClientFactory, |
||||
IDownloadService downloadService) |
||||
{ |
||||
this.logger = logger; |
||||
this.httpClientFactory = httpClientFactory; |
||||
this.downloadService = downloadService; |
||||
|
||||
timer.Interval = TimeSpan.FromMinutes(5); |
||||
timer.Tick += (_, _) => |
||||
{ |
||||
CheckForUpdate(); |
||||
}; |
||||
AutoUpdater.CheckForUpdateEvent += AutoUpdaterOnCheckForUpdateEvent; |
||||
timer.Tick += async (_, _) => { await CheckForUpdate(); }; |
||||
} |
||||
|
||||
public void StartCheckingForUpdates() |
||||
public async Task StartCheckingForUpdates() |
||||
{ |
||||
timer.IsEnabled = true; |
||||
timer.Start(); |
||||
CheckForUpdate(); |
||||
await CheckForUpdate(); |
||||
} |
||||
|
||||
private void CheckForUpdate() |
||||
public async Task DownloadUpdate(UpdateInfo updateInfo, |
||||
IProgress<ProgressReport> progress) |
||||
{ |
||||
AutoUpdater.DownloadPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Update"); |
||||
AutoUpdater.ExecutablePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Update", "StabilityMatrix.exe"); |
||||
// TODO: make this github url? |
||||
AutoUpdater.Start("https://cdn.lykos.ai/update.xml"); |
||||
var downloadUrl = updateInfo.DownloadUrl; |
||||
|
||||
Directory.CreateDirectory(UpdateFolder); |
||||
|
||||
// download the file from URL |
||||
await downloadService.DownloadToFileAsync(downloadUrl, ExecutablePath, progress: progress, |
||||
httpClientName: "UpdateClient"); |
||||
} |
||||
|
||||
private void AutoUpdaterOnCheckForUpdateEvent(UpdateInfoEventArgs args) |
||||
private async Task CheckForUpdate() |
||||
{ |
||||
if (args.Error == null && args.IsUpdateAvailable) |
||||
var httpClient = httpClientFactory.CreateClient("UpdateClient"); |
||||
var response = await httpClient.GetAsync("https://cdn.lykos.ai/update.json"); |
||||
if (!response.IsSuccessStatusCode) |
||||
{ |
||||
EventManager.Instance.OnUpdateAvailable(args); |
||||
logger.LogError("Error while checking for update"); |
||||
return; |
||||
} |
||||
else if (args.Error != null) |
||||
|
||||
var updateInfo = |
||||
await JsonSerializer.DeserializeAsync<UpdateInfo>( |
||||
await response.Content.ReadAsStreamAsync()); |
||||
|
||||
if (updateInfo == null) |
||||
{ |
||||
logger.LogError("UpdateInfo is null"); |
||||
return; |
||||
} |
||||
|
||||
if (updateInfo.Version == Utilities.GetAppVersion()) |
||||
{ |
||||
logger.LogError(args.Error, "Error while checking for update"); |
||||
logger.LogInformation("No update available"); |
||||
return; |
||||
} |
||||
|
||||
// check if update is newer |
||||
var updateVersion = updateInfo.Version.Split('.'); |
||||
var currentVersion = Utilities.GetAppVersion().Split('.'); |
||||
if (updateVersion.Length != 4 || currentVersion.Length != 4) |
||||
{ |
||||
logger.LogError("Invalid version format"); |
||||
return; |
||||
} |
||||
|
||||
var updateVersionInt = new int[4]; |
||||
var currentVersionInt = new int[4]; |
||||
for (var i = 0; i < 4; i++) |
||||
{ |
||||
if (int.TryParse(updateVersion[i], out updateVersionInt[i]) && |
||||
int.TryParse(currentVersion[i], out currentVersionInt[i])) continue; |
||||
logger.LogError("Invalid version format"); |
||||
return; |
||||
} |
||||
|
||||
// check if update is newer |
||||
for (var i = 0; i < 4; i++) |
||||
{ |
||||
if (updateVersionInt[i] <= currentVersionInt[i]) continue; |
||||
|
||||
logger.LogInformation("Update available"); |
||||
EventManager.Instance.OnUpdateAvailable(updateInfo); |
||||
return; |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
||||
|
@ -0,0 +1,15 @@
|
||||
using System.Reflection; |
||||
|
||||
namespace StabilityMatrix.Helper; |
||||
|
||||
public static class Utilities |
||||
{ |
||||
public static string GetAppVersion() |
||||
{ |
||||
var assembly = Assembly.GetExecutingAssembly(); |
||||
var version = assembly.GetName().Version; |
||||
return version == null |
||||
? "(Unknown)" |
||||
: $"{version.Major}.{version.Minor}.{version.Build}.{version.Revision}"; |
||||
} |
||||
} |
@ -0,0 +1,15 @@
|
||||
using System.Text.Json.Serialization; |
||||
|
||||
namespace StabilityMatrix.Models; |
||||
|
||||
public class UpdateInfo |
||||
{ |
||||
[JsonPropertyName("version")] |
||||
public string Version { get; set; } |
||||
|
||||
[JsonPropertyName("url")] |
||||
public string DownloadUrl { get; set; } |
||||
|
||||
[JsonPropertyName("changelog")] |
||||
public string ChangelogUrl { get; set; } |
||||
} |
Loading…
Reference in new issue