diff --git a/src/PicView.Avalonia/StartUp/StartUpHelper.cs b/src/PicView.Avalonia/StartUp/StartUpHelper.cs index f74fbf59..bab53729 100644 --- a/src/PicView.Avalonia/StartUp/StartUpHelper.cs +++ b/src/PicView.Avalonia/StartUp/StartUpHelper.cs @@ -7,6 +7,7 @@ using PicView.Avalonia.Navigation; using PicView.Avalonia.SettingsManagement; using PicView.Avalonia.UI; +using PicView.Avalonia.Update; using PicView.Avalonia.ViewModels; using PicView.Avalonia.Views; using PicView.Avalonia.WindowBehavior; @@ -34,6 +35,11 @@ public static void Start(MainViewModel vm, bool settingsExists, IClassicDesktopS { HandleMultipleInstances(args); } + else if (args.Length > 1 && args[1].Equals("update", StringComparison.InvariantCultureIgnoreCase)) + { + Task.Run(async () => await UpdateManager.UpdateCurrentVersion(vm)); + return; + } } vm.IsLoading = true; diff --git a/src/PicView.Avalonia/Update/UpdateManager.cs b/src/PicView.Avalonia/Update/UpdateManager.cs index f04b34f8..bca71c91 100644 --- a/src/PicView.Avalonia/Update/UpdateManager.cs +++ b/src/PicView.Avalonia/Update/UpdateManager.cs @@ -16,14 +16,43 @@ public partial class UpdateSourceGenerationContext : JsonSerializerContext; public static class UpdateManager { - public static void CheckForUpdates() - { - } - public static async Task UpdateCurrentVersion(MainViewModel vm) { var currentDirectory = new DirectoryInfo(Environment.ProcessPath); - var currentVersion = VersionHelper.GetCurrentVersionAsVersion(); + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + bool isAdmin; + try + { + isAdmin = currentDirectory.GetAccessControl().AreAccessRulesProtected; + } + catch (Exception) + { + isAdmin = false; + } + + if (isAdmin) + { + // Restart the application as admin + var process = new Process + { + StartInfo = new ProcessStartInfo + { + Verb = "runas", + UseShellExecute = true, + FileName = "PicView.exe", + Arguments = "update", + WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory + } + }; + process.Start(); + Environment.Exit(0); + } + } + + // ReSharper disable once RedundantAssignment + var currentVersion = VersionHelper.GetAssemblyVersion(); var url = "https://picview.org/update.json"; var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + "PicView"); Directory.CreateDirectory(tempPath); @@ -78,16 +107,6 @@ public static async Task UpdateCurrentVersion(MainViewModel vm) if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - var isAdmin = false; - try - { - isAdmin = currentDirectory.GetAccessControl().AreAccessRulesProtected; - } - catch (Exception) - { - isAdmin = false; - } - var isInstalled = CheckIfIsInstalled(); var architecture = RuntimeInformation.ProcessArchitecture switch @@ -105,70 +124,49 @@ public static async Task UpdateCurrentVersion(MainViewModel vm) return; } - if (isAdmin) + switch (architecture) { - // Restart the application as admin - var process = new Process - { - StartInfo = new ProcessStartInfo + case InstalledArchitecture.Arm64Install: + var fileName = Path.GetFileName(updateInfo.X64Install); + var tempFileDownloadPath = Path.Combine(tempPath, fileName); + await StartFileDownloader(vm, updateInfo.Arm64Install, tempFileDownloadPath); + var process = new Process { - Verb = "runas", - UseShellExecute = true, - FileName = "PicView.exe", - Arguments = $"update,{architecture},{currentVersion},{tempPath}", - WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory - } - }; - process.Start(); - Environment.Exit(0); - } - else - { - switch (architecture) - { - case InstalledArchitecture.Arm64Install: - var fileName = Path.GetFileName(updateInfo.X64Install); - var tempFileDownloadPath = Path.Combine(tempPath, fileName); - await StartFileDownloader(vm, updateInfo.Arm64Install, tempFileDownloadPath); - var process = new Process + StartInfo = new ProcessStartInfo { - StartInfo = new ProcessStartInfo - { - Verb = "runas", - UseShellExecute = true, - FileName = tempFileDownloadPath - } - }; - process.Start(); - return; - case InstalledArchitecture.Arm64Portable: - fileName = Path.GetFileName(updateInfo.X64Portable); - tempFileDownloadPath = Path.Combine(tempPath, fileName); - await StartFileDownloader(vm, updateInfo.Arm64Portable, tempFileDownloadPath); - vm.PlatformService.LocateOnDisk(tempFileDownloadPath); - return; - case InstalledArchitecture.X64Install: - fileName = Path.GetFileName(updateInfo.X64Install); - tempFileDownloadPath = Path.Combine(tempPath, fileName); - await StartFileDownloader(vm, updateInfo.X64Install, tempFileDownloadPath); - process = new Process + Verb = "runas", + UseShellExecute = true, + FileName = tempFileDownloadPath + } + }; + process.Start(); + return; + case InstalledArchitecture.Arm64Portable: + fileName = Path.GetFileName(updateInfo.X64Portable); + tempFileDownloadPath = Path.Combine(tempPath, fileName); + await StartFileDownloader(vm, updateInfo.Arm64Portable, tempFileDownloadPath); + vm.PlatformService.LocateOnDisk(tempFileDownloadPath); + return; + case InstalledArchitecture.X64Install: + fileName = Path.GetFileName(updateInfo.X64Install); + tempFileDownloadPath = Path.Combine(tempPath, fileName); + await StartFileDownloader(vm, updateInfo.X64Install, tempFileDownloadPath); + process = new Process + { + StartInfo = new ProcessStartInfo { - StartInfo = new ProcessStartInfo - { - Verb = "runas", - UseShellExecute = true, - FileName = tempFileDownloadPath - } - }; - process.Start(); - return; - case InstalledArchitecture.X64Portable: - fileName = Path.GetFileName(updateInfo.X64Portable); - tempFileDownloadPath = Path.Combine(tempPath, fileName); - await StartFileDownloader(vm, updateInfo.X64Portable, tempFileDownloadPath); - vm.PlatformService.LocateOnDisk(tempFileDownloadPath); - return; - } + UseShellExecute = true, + FileName = tempFileDownloadPath + } + }; + process.Start(); + return; + case InstalledArchitecture.X64Portable: + fileName = Path.GetFileName(updateInfo.X64Portable); + tempFileDownloadPath = Path.Combine(tempPath, fileName); + await StartFileDownloader(vm, updateInfo.X64Portable, tempFileDownloadPath); + vm.PlatformService.LocateOnDisk(tempFileDownloadPath); + return; } } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) @@ -250,12 +248,14 @@ private static async Task StartFileDownloader(MainViewModel vm, string downloadU } } - private static void ProgressChanged(MainViewModel vm, long? totalfilesize, long? totalbytesdownloaded, double? progresspercentage) + private static void ProgressChanged(MainViewModel vm, long? totalfilesize, long? totalbytesdownloaded, + double? progresspercentage) { if (totalfilesize is null || totalbytesdownloaded is null || progresspercentage is null) { return; } + vm.PlatformService.SetTaskbarProgress((ulong)totalbytesdownloaded, (ulong)totalfilesize); } diff --git a/src/PicView.Core/Config/VersionHelper.cs b/src/PicView.Core/Config/VersionHelper.cs index 7c044632..f45d42eb 100644 --- a/src/PicView.Core/Config/VersionHelper.cs +++ b/src/PicView.Core/Config/VersionHelper.cs @@ -23,7 +23,7 @@ public static class VersionHelper } } - public static Version? GetCurrentVersionAsVersion() + public static Version? GetAssemblyVersion() { try {