LANCommander/LANCommander.Launcher.Services/UpdateService.cs

93 lines
3.3 KiB
C#
Raw Permalink Normal View History

using LANCommander.SDK.Models;
2024-09-11 00:24:34 -05:00
using Microsoft.Extensions.Logging;
using Semver;
using System.Diagnostics;
2024-10-03 23:40:55 -05:00
using System.IO.Compression;
2024-08-01 00:30:54 -05:00
using System.Reflection;
2025-10-08 19:51:44 -05:00
using LANCommander.SDK.Extensions;
namespace LANCommander.Launcher.Services
{
public class UpdateService(
ILogger<UpdateService> logger,
SDK.Client client) : BaseService(logger)
{
public delegate Task OnUpdateAvailableHandler(CheckForUpdateResponse response);
public event OnUpdateAvailableHandler OnUpdateAvailable;
public async Task<CheckForUpdateResponse> CheckForUpdateAsync()
{
2025-10-08 19:51:44 -05:00
using (var op = Logger.BeginOperation("Checking for updates"))
{
var response = await client.Launcher.CheckForUpdateAsync();
2025-10-08 19:51:44 -05:00
if (response != null && response.UpdateAvailable)
OnUpdateAvailable?.Invoke(response);
op.Complete();
2025-10-08 19:51:44 -05:00
return response;
}
}
public async Task UpdateAsync(SemVersion version)
{
2025-10-08 19:51:44 -05:00
using (var op = Logger.BeginDebugOperation("Updating launcher"))
{
op.Enrich("Version", version);
Logger?.LogInformation("Downloading launcher v{Version}", version);
2025-10-08 19:51:44 -05:00
string path = Path.Combine(client.Settings.CurrentValue.Updates.StoragePath, $"{version}.zip");
2024-10-03 23:40:55 -05:00
2025-10-08 19:51:44 -05:00
await client.Launcher.DownloadAsync(path);
2024-10-03 23:40:55 -05:00
2025-10-08 19:51:44 -05:00
Logger?.LogInformation("Update version {Version} has been downloaded", version);
2024-10-03 23:40:55 -05:00
2025-10-08 19:51:44 -05:00
string processExecutable = "LANCommander.AutoUpdater.exe";
2025-10-08 19:51:44 -05:00
if (File.Exists("LANCommander.AutoUpdater"))
processExecutable = "LANCommander.AutoUpdater";
2025-10-08 19:51:44 -05:00
Logger?.LogInformation("New autoupdater is being extracted");
2025-10-08 19:51:44 -05:00
using (ZipArchive archive = ZipFile.OpenRead(path))
2024-10-03 23:40:55 -05:00
{
2025-10-08 19:51:44 -05:00
Logger?.LogTrace("Looping entries");
foreach (ZipArchiveEntry entry in archive.Entries.Where(e => e.FullName == processExecutable))
{
entry.ExtractToFile(entry.FullName, true);
Logger?.LogTrace("Extracted {Entry}", entry);
}
Logger?.LogTrace("Entries loop over");
2024-10-03 23:40:55 -05:00
}
2025-10-08 19:51:44 -05:00
Logger?.LogInformation("Sarting Autoupdater");
2025-10-08 19:51:44 -05:00
var process = new ProcessStartInfo();
2025-10-08 19:51:44 -05:00
process.FileName = processExecutable;
process.Arguments = $"-Version {version} -Path \"{client.Settings.CurrentValue.Updates.StoragePath}\" -Executable {Process.GetCurrentProcess().MainModule.FileName}";
process.UseShellExecute = true;
2025-10-08 19:51:44 -05:00
Process.Start(process);
2025-10-08 19:51:44 -05:00
client.Settings.Update(s => s.Launcher.LaunchCount = 0);
2024-07-28 18:33:50 -05:00
2025-10-08 19:51:44 -05:00
Logger?.LogInformation("Shutting down to get out of the way");
op.Complete();
2025-10-08 19:51:44 -05:00
Environment.Exit(0);
}
}
2024-08-01 00:30:54 -05:00
public static SemVersion GetCurrentVersion()
{
var productVersion = FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location).ProductVersion;
return SemVersion.Parse(productVersion);
2024-08-01 00:30:54 -05:00
}
}
}