From 3b3423dd9bc0457841ecbb89e403e0a6eae7f957 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Fri, 24 May 2024 01:28:35 +0300 Subject: [PATCH] Clean up platform checks in update service --- .../DiscordChatExporter.Gui.csproj | 2 +- .../Services/UpdateService.cs | 39 +++++++++++-------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/DiscordChatExporter.Gui/DiscordChatExporter.Gui.csproj b/DiscordChatExporter.Gui/DiscordChatExporter.Gui.csproj index 8c3f51be..a4e3f6aa 100644 --- a/DiscordChatExporter.Gui/DiscordChatExporter.Gui.csproj +++ b/DiscordChatExporter.Gui/DiscordChatExporter.Gui.csproj @@ -26,7 +26,7 @@ - + diff --git a/DiscordChatExporter.Gui/Services/UpdateService.cs b/DiscordChatExporter.Gui/Services/UpdateService.cs index ccfd67d7..2fc367da 100644 --- a/DiscordChatExporter.Gui/Services/UpdateService.cs +++ b/DiscordChatExporter.Gui/Services/UpdateService.cs @@ -9,18 +9,20 @@ namespace DiscordChatExporter.Gui.Services; public class UpdateService(SettingsService settingsService) : IDisposable { - private readonly IUpdateManager _updateManager = new UpdateManager( - new GithubPackageResolver( - "Tyrrrz", - "DiscordChatExporter", - // Examples: - // DiscordChatExporter.win-arm64.zip - // DiscordChatExporter.win-x64.zip - // DiscordChatExporter.linux-x64.zip - $"DiscordChatExporter.{RuntimeInformation.RuntimeIdentifier}.zip" - ), - new ZipPackageExtractor() - ); + private readonly IUpdateManager? _updateManager = OperatingSystem.IsWindows() + ? new UpdateManager( + new GithubPackageResolver( + "Tyrrrz", + "DiscordChatExporter", + // Examples: + // DiscordChatExporter.win-arm64.zip + // DiscordChatExporter.win-x64.zip + // DiscordChatExporter.linux-x64.zip + $"DiscordChatExporter.{RuntimeInformation.RuntimeIdentifier}.zip" + ), + new ZipPackageExtractor() + ) + : null; private Version? _updateVersion; private bool _updatePrepared; @@ -28,6 +30,9 @@ public class UpdateService(SettingsService settingsService) : IDisposable public async ValueTask CheckForUpdatesAsync() { + if (_updateManager is null) + return null; + if (!settingsService.IsAutoUpdateEnabled) return null; @@ -37,6 +42,9 @@ public class UpdateService(SettingsService settingsService) : IDisposable public async ValueTask PrepareUpdateAsync(Version version) { + if (_updateManager is null) + return; + if (!settingsService.IsAutoUpdateEnabled) return; @@ -57,11 +65,10 @@ public class UpdateService(SettingsService settingsService) : IDisposable public void FinalizeUpdate(bool needRestart) { - if (!settingsService.IsAutoUpdateEnabled) + if (_updateManager is null) return; - // Onova only works on Windows currently - if (!OperatingSystem.IsWindows()) + if (!settingsService.IsAutoUpdateEnabled) return; if (_updateVersion is null || !_updatePrepared || _updaterLaunched) @@ -82,5 +89,5 @@ public class UpdateService(SettingsService settingsService) : IDisposable } } - public void Dispose() => _updateManager.Dispose(); + public void Dispose() => _updateManager?.Dispose(); }