123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- using System;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.IO;
- using System.Reflection;
- using System.Threading.Tasks;
- using System.Windows;
- using PixiEditor.Helpers;
- using PixiEditor.Models.Dialogs;
- using PixiEditor.Models.Processes;
- using PixiEditor.Models.UserPreferences;
- using PixiEditor.UpdateModule;
- namespace PixiEditor.ViewModels.SubViewModels.Main
- {
- public class UpdateViewModel : SubViewModel<ViewModelMain>
- {
- private bool updateReadyToInstall = false;
- public UpdateChecker UpdateChecker { get; set; }
- public RelayCommand RestartApplicationCommand { get; set; }
- private string versionText;
- public string VersionText
- {
- get => versionText;
- set
- {
- versionText = value;
- RaisePropertyChanged(nameof(VersionText));
- }
- }
- public bool UpdateReadyToInstall
- {
- get => updateReadyToInstall;
- set
- {
- updateReadyToInstall = value;
- RaisePropertyChanged(nameof(UpdateReadyToInstall));
- if (value)
- {
- VersionText = $"to install update (current {UpdateChecker.CurrentVersionTag})"; // Button shows "Restart" before this text
- }
- }
- }
- public UpdateViewModel(ViewModelMain owner)
- : base(owner)
- {
- Owner.OnStartupEvent += Owner_OnStartupEvent;
- RestartApplicationCommand = new RelayCommand(RestartApplication);
- InitUpdateChecker();
- }
- public async Task<bool> CheckForUpdate()
- {
- bool updateAvailable = await UpdateChecker.CheckUpdateAvailable();
- bool updateCompatible = await UpdateChecker.IsUpdateCompatible();
- bool updateFileDoesNotExists = !File.Exists(
- Path.Join(UpdateDownloader.DownloadLocation, $"update-{UpdateChecker.LatestReleaseInfo.TagName}.zip"));
- bool updateExeDoesNotExists = !File.Exists(
- Path.Join(UpdateDownloader.DownloadLocation, $"update-{UpdateChecker.LatestReleaseInfo.TagName}.exe"));
- if (updateAvailable && updateFileDoesNotExists && updateExeDoesNotExists)
- {
- VersionText = "Downloading update...";
- if (updateCompatible)
- {
- await UpdateDownloader.DownloadReleaseZip(UpdateChecker.LatestReleaseInfo);
- }
- else
- {
- await UpdateDownloader.DownloadInstaller(UpdateChecker.LatestReleaseInfo);
- }
- UpdateReadyToInstall = true;
- return true;
- }
- return false;
- }
- private static void AskToInstall()
- {
- string dir = AppDomain.CurrentDomain.BaseDirectory;
- UpdateDownloader.CreateTempDirectory();
- bool updateZipExists = Directory.GetFiles(UpdateDownloader.DownloadLocation, "update-*.zip").Length > 0;
- string[] updateExeFiles = Directory.GetFiles(UpdateDownloader.DownloadLocation, "update-*.exe");
- bool updateExeExists = updateExeFiles.Length > 0;
- string updaterPath = Path.Join(dir, "PixiEditor.UpdateInstaller.exe");
- if (updateZipExists || updateExeExists)
- {
- ViewModelMain.Current.UpdateSubViewModel.UpdateReadyToInstall = true;
- var result = ConfirmationDialog.Show("Update is ready to install. Do you want to install it now?");
- if (result == Models.Enums.ConfirmationType.Yes)
- {
- if (updateZipExists && File.Exists(updaterPath))
- {
- InstallHeadless(updaterPath);
- }
- else if (updateExeExists)
- {
- OpenExeInstaller(updateExeFiles[0]);
- }
- }
- }
- }
- private static void InstallHeadless(string updaterPath)
- {
- try
- {
- ProcessHelper.RunAsAdmin(updaterPath);
- Application.Current.Shutdown();
- }
- catch (Win32Exception)
- {
- MessageBox.Show(
- "Couldn't update without administrator rights.",
- "Insufficient permissions",
- MessageBoxButton.OK,
- MessageBoxImage.Error);
- }
- }
- private static void OpenExeInstaller(string updateExeFile)
- {
- bool alreadyUpdated = AssemblyHelper.GetCurrentAssemblyVersion() ==
- updateExeFile.Split('-')[1].Split(".exe")[0];
- if (!alreadyUpdated)
- {
- RestartToUpdate(updateExeFile);
- }
- else
- {
- File.Delete(updateExeFile);
- }
- }
- private static void RestartToUpdate(string updateExeFile)
- {
- Process.Start(updateExeFile);
- Application.Current.Shutdown();
- }
- private static void RestartApplication(object parameter)
- {
- try
- {
- ProcessHelper.RunAsAdmin(Path.Join(AppDomain.CurrentDomain.BaseDirectory, "PixiEditor.UpdateInstaller.exe"));
- Application.Current.Shutdown();
- }
- catch (Win32Exception)
- {
- NoticeDialog.Show("Couldn't update without administrator rights.", "Insufficient permissions");
- }
- }
- private async void Owner_OnStartupEvent(object sender, EventArgs e)
- {
- if (IPreferences.Current.GetPreference("CheckUpdatesOnStartup", true))
- {
- try
- {
- await CheckForUpdate();
- }
- catch (System.Net.Http.HttpRequestException)
- {
- NoticeDialog.Show("Could not check if there's an update available");
- }
- AskToInstall();
- }
- }
- private void InitUpdateChecker()
- {
- string version = AssemblyHelper.GetCurrentAssemblyVersion();
- UpdateChecker = new UpdateChecker(version);
- VersionText = $"Version {version}";
- }
- }
- }
|