Browse Source

Associate .pixi file with PixiEditor on Steam build

Krzysztof Krysiński 2 years ago
parent
commit
639ccb23bc

+ 1 - 0
src/PixiEditor/Data/Localization/Languages/en.json

@@ -549,6 +549,7 @@
   "ERROR_WHILE_SAVING": "An internal error occured while saving. Please try again.",
   "ERROR_WHILE_SAVING": "An internal error occured while saving. Please try again.",
   "UNKNOWN_ERROR_SAVING": "An error occured while saving.",
   "UNKNOWN_ERROR_SAVING": "An error occured while saving.",
   "FAILED_ASSOCIATE_LOSPEC": "Failed to associate Lospec Palette protocol.",
   "FAILED_ASSOCIATE_LOSPEC": "Failed to associate Lospec Palette protocol.",
+  "FAILED_ASSOCIATE_PIXI": "Failed to associate .pixi file with PixiEditor.",
   "SHORTCUTS_CORRUPTED_TITLE": "Corrupted shortcuts file",
   "SHORTCUTS_CORRUPTED_TITLE": "Corrupted shortcuts file",
   "SHORTCUTS_CORRUPTED": "Shortcuts file was corrupted, resetting to default.",
   "SHORTCUTS_CORRUPTED": "Shortcuts file was corrupted, resetting to default.",
   "FAILED_DOWNLOAD_PALETTE": "Failed to download palette",
   "FAILED_DOWNLOAD_PALETTE": "Failed to download palette",

+ 39 - 0
src/PixiEditor/Helpers/RegistryHelpers.cs

@@ -0,0 +1,39 @@
+using System.Diagnostics;
+using System.Security.AccessControl;
+using System.Windows;
+using Microsoft.Win32;
+using PixiEditor.Localization;
+using PixiEditor.Models.Dialogs;
+
+namespace PixiEditor.Helpers;
+
+public static class RegistryHelpers
+{
+    public static bool IsKeyPresentInRoot(string keyName)
+    {
+        using var key = Registry.ClassesRoot.OpenSubKey(keyName, RegistryRights.ReadKey);
+        return key != null;
+    }
+
+    public static bool TryAssociate(Action associationMethod, LocalizedString errorMessage)
+    {
+        try
+        {
+            if (!ProcessHelper.IsRunningAsAdministrator())
+            {
+                ProcessHelper.RunAsAdmin(Process.GetCurrentProcess().MainModule?.FileName);
+                Application.Current.Shutdown();
+            }
+            else
+            {
+                associationMethod();
+            }
+        }
+        catch
+        {
+            NoticeDialog.Show(errorMessage, "ERROR");
+        }
+
+        return false;
+    }
+}

+ 22 - 16
src/PixiEditor/ViewModels/SubViewModels/Main/RegistryViewModel.cs

@@ -21,27 +21,35 @@ internal class RegistryViewModel : SubViewModel<ViewModelMain>
         if (!LospecPaletteIsAssociated())
         if (!LospecPaletteIsAssociated())
         {
         {
             // Associate lospec-palette URL protocol
             // Associate lospec-palette URL protocol
-            AssociateLospecPalette();
+            RegistryHelpers.TryAssociate(AssociateLospecPaletteInRegistry, "FAILED_ASSOCIATE_LOSPEC");
         }
         }
+
+#if STEAM // Only associate .pixi file if it's a steam version, other versions handle it during installation
+        if(!PixiFileIsAssociated())
+        {
+            RegistryHelpers.TryAssociate(AssociatePixiFileInRegistry, "FAILED_ASSOCIATE_PIXI");
+        }
+#endif
     }
     }
 
 
-    private void AssociateLospecPalette()
+    private bool PixiFileIsAssociated()
+    {
+        // Check if HKEY_CLASSES_ROOT\.pixi is present
+        return RegistryHelpers.IsKeyPresentInRoot(".pixi");
+    }
+
+    private void AssociatePixiFileInRegistry()
     {
     {
         try
         try
         {
         {
-            if (!ProcessHelper.IsRunningAsAdministrator())
-            {
-                ProcessHelper.RunAsAdmin(Process.GetCurrentProcess().MainModule?.FileName);
-                Application.Current.Shutdown();
-            }
-            else
-            {
-                AssociateLospecPaletteInRegistry();
-            }
+            using RegistryKey key = Registry.ClassesRoot.CreateSubKey(".pixi");
+            key.SetValue("", "PixiEditor");
+
+            using RegistryKey shellKey = key.CreateSubKey("OpenWithProgids");
         }
         }
         catch
         catch
         {
         {
-            NoticeDialog.Show("FAILED_ASSOCIATE_LOSPEC", "ERROR");
+            NoticeDialog.Show("FAILED_ASSOCIATE_PIXI", "ERROR");
         }
         }
     }
     }
 
 
@@ -65,15 +73,13 @@ internal class RegistryViewModel : SubViewModel<ViewModelMain>
         }
         }
         catch
         catch
         {
         {
-            NoticeDialog.Show("FAILED_ASSOCIATE_LOSPEC", "ERROR");
+            NoticeDialog.Show("Failed to associate lospec-palette protocol", "Error");
         }
         }
     }
     }
 
 
     private bool LospecPaletteIsAssociated()
     private bool LospecPaletteIsAssociated()
     {
     {
         // Check if HKEY_CLASSES_ROOT\lospec-palette is present
         // Check if HKEY_CLASSES_ROOT\lospec-palette is present
-
-        RegistryKey lospecPaletteKey = Registry.ClassesRoot.OpenSubKey("lospec-palette", RegistryRights.ReadKey);
-        return lospecPaletteKey != null;
+        return RegistryHelpers.IsKeyPresentInRoot("lospec-palette");
     }
     }
 }
 }