Browse Source

UpdateInstaller rollback

flabbet 2 years ago
parent
commit
4d49f56b3e

+ 2 - 8
src/PixiEditor.UpdateModule/UpdateChannel.cs

@@ -1,10 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace PixiEditor.UpdateModule;
+namespace PixiEditor.UpdateModule;
 
 public class UpdateChannel
 {
@@ -22,4 +16,4 @@ public class UpdateChannel
         ApiUrl = $"https://api.github.com/repos/{repositoryOwner}/{repositoryName}/releases/latest";
         IncompatibleFileApiUrl = "https://raw.githubusercontent.com/" + $"{repositoryOwner}/{repositoryName}/" + "{0}/incompatible.json";
     }
-}
+}

+ 20 - 17
src/PixiEditor.UpdateModule/UpdateChecker.cs

@@ -18,7 +18,20 @@ public class UpdateChecker
 
     public ReleaseInfo LatestReleaseInfo { get; private set; }
 
-    public UpdateChannel Channel { get; set; }
+    private UpdateChannel _channel;
+    public UpdateChannel Channel 
+    {
+        get => _channel;
+        set
+        {
+            bool changed = _channel != value;
+            if (changed)
+            {
+                _channel = value;
+                LatestReleaseInfo = null;
+            }
+        }
+    }
 
     public string CurrentVersionTag { get; }
 
@@ -28,19 +41,9 @@ public class UpdateChecker
     /// <param name="originalVer">Version to compare.</param>
     /// <param name="newVer">Version to compare with.</param>
     /// <returns>True if semantic version is higher.</returns>
-    public static bool VersionBigger(string originalVer, string newVer)
+    public static bool VersionDifferent(string originalVer, string newVer)
     {
-        if (!ParseVersionString(originalVer, out float ver1))
-        {
-            return false;
-        }
-
-        if (ParseVersionString(newVer, out float ver2))
-        {
-            return ver2 > ver1;
-        }
-
-        return false;
+        return NormalizeVersionString(originalVer) != NormalizeVersionString(newVer);
     }
 
     public async Task<bool> CheckUpdateAvailable()
@@ -51,7 +54,7 @@ public class UpdateChecker
 
     public bool CheckUpdateAvailable(ReleaseInfo latestRelease)
     {
-        return latestRelease.WasDataFetchSuccessful && VersionBigger(CurrentVersionTag, latestRelease.TagName);
+        return latestRelease.WasDataFetchSuccessful && VersionDifferent(CurrentVersionTag, latestRelease.TagName);
     }
 
     public bool IsUpdateCompatible(string[] incompatibleVersions)
@@ -97,8 +100,8 @@ public class UpdateChecker
         return new ReleaseInfo(false);
     }
 
-    private static bool ParseVersionString(string versionString, out float version)
+    private static string NormalizeVersionString(string versionString)
     {
-        return float.TryParse(versionString[..7].Replace(".", string.Empty).Insert(1, "."), NumberStyles.Any, CultureInfo.InvariantCulture, out version);
+        return versionString[..7];
     }
-}
+}

+ 20 - 8
src/PixiEditor/ViewModels/SubViewModels/Main/UpdateViewModel.cs

@@ -52,7 +52,15 @@ internal class UpdateViewModel : SubViewModel<ViewModelMain>
         : base(owner)
     {
         Owner.OnStartupEvent += Owner_OnStartupEvent;
-        IPreferences.Current.AddCallback<string>("UpdateChannel", (val) => UpdateChecker.Channel = GetUpdateChannel(val));
+        IPreferences.Current.AddCallback<string>("UpdateChannel", val =>
+        {
+            string prevChannel = UpdateChecker.Channel.ApiUrl;
+            UpdateChecker.Channel = GetUpdateChannel(val);
+            if (prevChannel != UpdateChecker.Channel.ApiUrl)
+            {
+                ConditionalUPDATE();
+            }
+        });
         InitUpdateChecker();
     }
 
@@ -66,6 +74,7 @@ internal class UpdateViewModel : SubViewModel<ViewModelMain>
             Path.Join(UpdateDownloader.DownloadLocation, $"update-{UpdateChecker.LatestReleaseInfo.TagName}.exe"));
         if (updateAvailable && updateFileDoesNotExists && updateExeDoesNotExists)
         {
+            UpdateReadyToInstall = false;
             VersionText = "Downloading update...";
             if (updateCompatible)
             {
@@ -83,32 +92,35 @@ internal class UpdateViewModel : SubViewModel<ViewModelMain>
         return false;
     }
 
-    private static void AskToInstall()
+    private void AskToInstall()
     {
 #if RELEASE || DEV_RELEASE
             if (IPreferences.Current.GetPreference("CheckUpdatesOnStartup", true))
             {
                 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;
+                bool updateFileExists = File.Exists(
+                    Path.Join(UpdateDownloader.DownloadLocation, $"update-{UpdateChecker.LatestReleaseInfo.TagName}.zip"));
+                string exePath = Path.Join(UpdateDownloader.DownloadLocation,
+                    $"update-{UpdateChecker.LatestReleaseInfo.TagName}.exe");
 
+                bool updateExeExists = File.Exists(exePath);
+                
                 string updaterPath = Path.Join(dir, "PixiEditor.UpdateInstaller.exe");
 
-                if (updateZipExists || updateExeExists)
+                if (updateFileExists || updateExeExists)
                 {
                     ViewModelMain.Current.UpdateSubViewModel.UpdateReadyToInstall = true;
                     var result = ConfirmationDialog.Show("Update is ready to be installed. Do you want to install it now?", "New update");
                     if (result == Models.Enums.ConfirmationType.Yes)
                     {
-                        if (updateZipExists && File.Exists(updaterPath))
+                        if (updateFileExists && File.Exists(updaterPath))
                         {
                             InstallHeadless(updaterPath);
                         }
                         else if (updateExeExists)
                         {
-                            OpenExeInstaller(updateExeFiles[0]);
+                            OpenExeInstaller(exePath);
                         }
                     }
                 }