Browse Source

Fixed crash when updater could not reach GitHub

CPKreuz 4 years ago
parent
commit
d2855f7373

+ 9 - 1
PixiEditor/ViewModels/SubViewModels/Main/UpdateViewModel.cs

@@ -6,6 +6,7 @@ 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;
@@ -88,7 +89,14 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         {
             if (IPreferences.Current.GetPreference("CheckUpdatesOnStartup", true))
             {
-                await CheckForUpdate();
+                try
+                {
+                    await CheckForUpdate();
+                }
+                catch (System.Net.Http.HttpRequestException)
+                {
+                    NoticeDialog.Show("Could not check if there's an update available");
+                }
             }
         }
 

+ 16 - 6
PixiEditor/ViewModels/ViewModelMain.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
+using System.Diagnostics;
 using System.Linq;
 using System.Windows;
 using System.Windows.Input;
@@ -59,9 +60,7 @@ namespace PixiEditor.ViewModels
 
         public DiscordViewModel DiscordViewModel { get; set; }
 
-#if DEBUG
         public DebugViewModel DebugSubViewModel { get; set; }
-#endif
 
         public BitmapManager BitmapManager { get; set; }
 
@@ -101,7 +100,6 @@ namespace PixiEditor.ViewModels
             CloseWindowCommand = new RelayCommand(CloseWindow);
 
             FileSubViewModel = new FileViewModel(this);
-            UpdateSubViewModel = new UpdateViewModel(this);
             ToolsSubViewModel = new ToolsViewModel(this);
             IoSubViewModel = new IoViewModel(this);
             LayersSubViewModel = new LayersViewModel(this);
@@ -111,9 +109,9 @@ namespace PixiEditor.ViewModels
             ColorsSubViewModel = new ColorsViewModel(this);
             DocumentSubViewModel = new DocumentViewModel(this);
             DiscordViewModel = new DiscordViewModel(this, "764168193685979138");
-#if DEBUG
-            DebugSubViewModel = new DebugViewModel(this);
-#endif
+
+            AddDebugOnlyViewModels();
+            AddReleaseOnlyViewModels();
 
             ShortcutController = new ShortcutController(
                     new ShortcutGroup(
@@ -192,6 +190,18 @@ namespace PixiEditor.ViewModels
             return BitmapManager.ActiveDocument != null;
         }
 
+        [Conditional("DEBUG")]
+        private void AddDebugOnlyViewModels()
+        {
+            DebugSubViewModel = new DebugViewModel(this);
+        }
+
+        [Conditional("RELEASE")]
+        private void AddReleaseOnlyViewModels()
+        {
+            UpdateSubViewModel = new UpdateViewModel(this);
+        }
+
         private Shortcut CreateToolShortcut<T>(Key key, ModifierKeys modifier = ModifierKeys.None)
             where T : Tool
         {

+ 0 - 69
PixiEditor/Views/MainWindow.xaml.cs

@@ -85,75 +85,6 @@ namespace PixiEditor
         private void MainWindow_Initialized(object sender, EventArgs e)
         {
             AppDomain.CurrentDomain.UnhandledException += (sender, e) => Helpers.CrashHelper.SaveCrashInfo((Exception)e.ExceptionObject);
-#if RELEASE
-            CheckForDownloadedUpdates();
-#endif
-        }
-
-        private void CheckForDownloadedUpdates()
-        {
-            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 void InstallHeadless(string updaterPath)
-        {
-            try
-            {
-                ProcessHelper.RunAsAdmin(updaterPath);
-                Close();
-            }
-            catch (Win32Exception)
-            {
-                MessageBox.Show(
-                    "Couldn't update without administrator rights.",
-                    "Insufficient permissions",
-                    MessageBoxButton.OK,
-                    MessageBoxImage.Error);
-            }
-        }
-
-        private void OpenExeInstaller(string updateExeFile)
-        {
-            bool alreadyUpdated = AssemblyHelper.GetCurrentAssemblyVersion() ==
-                    updateExeFile.Split('-')[1].Split(".exe")[0];
-
-            if (!alreadyUpdated)
-            {
-                RestartToUpdate(updateExeFile);
-            }
-            else
-            {
-                File.Delete(updateExeFile);
-            }
-        }
-
-        private void RestartToUpdate(string updateExeFile)
-        {
-            Process.Start(updateExeFile);
-            Close();
         }
     }
 }