Browse Source

Added registry association

Krzysztof Krysiński 3 years ago
parent
commit
f67995cc35

+ 1 - 1
Installer/installer-setup-x64-light.iss

@@ -392,7 +392,7 @@ Root: HKCR; Subkey: "{#MyAppName}\shell\open\command";  ValueData: """{app}\{#My
 
 // lospec-palette URL protocol association
 Root: HKCR; Subkey: "lospec-palette";                   ValueData: "{#MyAppName}";  Flags: uninsdeletevalue; ValueType: string;  ValueName: ""
-Root: HKCR; Subkey: "lospec-palette";                   ValueData: "URL Protocol";  Flags: uninsdeletekey;   ValueType: string;  ValueName: ""
+Root: HKCR; Subkey: "lospec-palette";                   ValueData: "";  Flags: uninsdeletekey;   ValueType: string;  ValueName: "URL Protocol"
 Root: HKCR; Subkey: "lospec-palette\shell\open\command";  ValueData: """{app}\{#MyAppExeName}"" ""%1""";  ValueType: string;  ValueName: ""
 
 [Code]

+ 1 - 1
Installer/installer-setup-x86-light.iss

@@ -391,7 +391,7 @@ Root: HKCR; Subkey: "{#MyAppName}\shell\open\command";  ValueData: """{app}\{#My
 
 // lospec-palette URL protocol association
 Root: HKCR; Subkey: "lospec-palette";                   ValueData: "{#MyAppName}";  Flags: uninsdeletevalue; ValueType: string;  ValueName: ""
-Root: HKCR; Subkey: "lospec-palette";                   ValueData: "URL Protocol";  Flags: uninsdeletekey;   ValueType: string;  ValueName: ""
+Root: HKCR; Subkey: "lospec-palette";                   ValueData: "";  Flags: uninsdeletekey;   ValueType: string;  ValueName: "URL Protocol"
 Root: HKCR; Subkey: "lospec-palette\shell\open\command";  ValueData: """{app}\{#MyAppExeName}"" ""%1""";  ValueType: string;  ValueName: ""
 
 [Code]

+ 1 - 0
PixiEditor/Helpers/Extensions/ServiceCollectionHelpers.cs

@@ -36,6 +36,7 @@ namespace PixiEditor.Helpers.Extensions
                 .AddSingleton<ColorsViewModel>()
                 .AddSingleton<DocumentViewModel>()
                 .AddSingleton<MiscViewModel>()
+                .AddSingleton<RegistryViewModel>()
                 .AddSingleton(x => new DiscordViewModel(x.GetService<ViewModelMain>(), "764168193685979138"))
                 .AddSingleton<DebugViewModel>()
                 // Controllers

+ 6 - 0
PixiEditor/Models/Processes/ProcessHelper.cs

@@ -1,5 +1,6 @@
 using System.ComponentModel;
 using System.Diagnostics;
+using System.Security.Principal;
 
 namespace PixiEditor.Models.Processes
 {
@@ -22,5 +23,10 @@ namespace PixiEditor.Models.Processes
 
             return proc;
         }
+
+        public static bool IsRunningAsAdministrator()
+        {
+            return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
+        }
     }
 }

+ 80 - 0
PixiEditor/ViewModels/SubViewModels/Main/RegistryViewModel.cs

@@ -0,0 +1,80 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Security.AccessControl;
+using System.Security.Claims;
+using System.Security.Principal;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using Microsoft.Win32;
+using PixiEditor.Models.Dialogs;
+using PixiEditor.Models.Processes;
+
+namespace PixiEditor.ViewModels.SubViewModels.Main
+{
+    public class RegistryViewModel : SubViewModel<ViewModelMain>
+    {
+        public RegistryViewModel(ViewModelMain owner) : base(owner)
+        {
+            Owner.OnStartupEvent += OwnerOnStartupEvent;
+        }
+
+        private void OwnerOnStartupEvent(object? sender, EventArgs e)
+        {
+            // Check if lospec-palette is associated in registry
+
+            if (!LospecPaletteIsAssociated())
+            {
+                // Associate lospec-palette URL protocol
+                AssociateLospecPalette();
+            }
+        }
+
+        private void AssociateLospecPalette()
+        {
+            if (!ProcessHelper.IsRunningAsAdministrator())
+            {
+                ProcessHelper.RunAsAdmin(Process.GetCurrentProcess().MainModule?.FileName);
+                Application.Current.Shutdown();
+            }
+            else
+            {
+                AssociateLospecPaletteInRegistry();
+            }
+        }
+
+        private void AssociateLospecPaletteInRegistry()
+        {
+            try
+            {
+                using RegistryKey key = Registry.ClassesRoot.CreateSubKey("lospec-palette");
+
+                key.SetValue("", "PixiEditor");
+                key.SetValue("URL Protocol", "");
+
+                // Create a new key
+                using RegistryKey shellKey = key.CreateSubKey("shell");
+                // Create a new key
+                using RegistryKey openKey = shellKey.CreateSubKey("open");
+                // Create a new key
+                using RegistryKey commandKey = openKey.CreateSubKey("command");
+                // Set the default value of the key
+                commandKey.SetValue("", $"\"{Process.GetCurrentProcess().MainModule?.FileName}\" \"%1\"");
+            }
+            catch (Exception e)
+            {
+                NoticeDialog.Show("Failed to associate lospec-palette protocol", "Error");
+            }
+        }
+
+        private bool LospecPaletteIsAssociated()
+        {
+            // Check if HKEY_CLASSES_ROOT\lospec-palette is present
+
+            RegistryKey lospecPaletteKey = Registry.ClassesRoot.OpenSubKey("lospec-palette", RegistryRights.ReadKey);
+            return lospecPaletteKey != null;
+        }
+    }
+}

+ 3 - 0
PixiEditor/ViewModels/ViewModelMain.cs

@@ -76,6 +76,8 @@ namespace PixiEditor.ViewModels
 
         public WindowViewModel WindowSubViewModel { get; set; }
 
+        public RegistryViewModel RegistrySubViewModel { get; set; }
+
         public IPreferences Preferences { get; set; }
 
         public string ActionDisplay
@@ -158,6 +160,7 @@ namespace PixiEditor.ViewModels
 
             WindowSubViewModel = services.GetService<WindowViewModel>();
             StylusSubViewModel = services.GetService<StylusViewModel>();
+            RegistrySubViewModel = services.GetService<RegistryViewModel>();
 
             AddDebugOnlyViewModels();
             AddReleaseOnlyViewModels();