diff --git a/Stardrop/Utilities/NXMProtocol.cs b/Stardrop/Utilities/NXMProtocol.cs index 7dfbaf4..757d039 100644 --- a/Stardrop/Utilities/NXMProtocol.cs +++ b/Stardrop/Utilities/NXMProtocol.cs @@ -1,23 +1,30 @@ using Microsoft.Win32; using System; using System.Runtime.InteropServices; +using System.Runtime.Versioning; namespace Stardrop.Utilities { internal static class NXMProtocol { + [SupportedOSPlatform("windows")] public static bool Register(string applicationPath) { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) is false) + { + Program.helper.Log($"Attempted to modify registery keys for NXM protocol on a non-Windows system!"); + return false; + } try { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) is false) + using var software = Registry.CurrentUser.OpenSubKey("Software", writable: true); + using var classes = software?.OpenSubKey("Classes", writable: true); + if (classes is null) { - Program.helper.Log($"Attempted to modify registery keys for NXM protocol on a non-Windows system!"); return false; } - - var keyTest = Registry.CurrentUser.OpenSubKey("Software", true).OpenSubKey("Classes", true); - RegistryKey key = keyTest.CreateSubKey("nxm"); + + using RegistryKey key = classes.CreateSubKey("nxm"); key.SetValue("URL Protocol", "nxm"); key.CreateSubKey(@"shell\open\command").SetValue("", "\"" + applicationPath + "\" --nxm \"%1\""); } @@ -29,35 +36,39 @@ public static bool Register(string applicationPath) return true; } - + [SupportedOSPlatform("windows")] public static bool Validate(string applicationPath) { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) is false) + { + Program.helper.Log($"Attempted to modify registery keys for NXM protocol on a non-Windows system!"); + return false; + } try { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) is false) - { - Program.helper.Log($"Attempted to modify registery keys for NXM protocol on a non-Windows system!"); - return false; - } - var baseKeyTest = Registry.CurrentUser.OpenSubKey("Software", true).OpenSubKey("Classes", true).OpenSubKey("nxm", true); - if (baseKeyTest is null || baseKeyTest.GetValue("URL Protocol").ToString() != "nxm") - { + using var software = Registry.CurrentUser.OpenSubKey("Software", writable: true); + using var classes = software?.OpenSubKey("Classes", writable: true); + using var baseKey = classes?.OpenSubKey("nxm", writable: true); + + if(baseKey is null) return false; - } - - var actualKeyTest = Registry.CurrentUser.OpenSubKey("Software", true).OpenSubKey("Classes", true).OpenSubKey(@"nxm\shell\open\command", true); - if (actualKeyTest.GetValue(String.Empty).ToString() != "\"" + applicationPath + "\" --nxm \"%1\"") - { + + if (!string.Equals(baseKey.GetValue("URL Protocol")?.ToString(), "nxm", StringComparison.Ordinal)) return false; - } + + using var commandKey = classes?.OpenSubKey(@"nxm\shell\open\command", writable: true); + + return string.Equals( + commandKey?.GetValue(string.Empty)?.ToString(), + $"\"{applicationPath}\" --nxm \"%1\"", + StringComparison.Ordinal); + } catch (Exception ex) { return false; } - - return true; } } } diff --git a/Stardrop/Views/SettingsWindow.axaml.cs b/Stardrop/Views/SettingsWindow.axaml.cs index be9362c..06860d3 100644 --- a/Stardrop/Views/SettingsWindow.axaml.cs +++ b/Stardrop/Views/SettingsWindow.axaml.cs @@ -176,6 +176,15 @@ public SettingsWindow(double parentWindowHeight) : this() private async void RegisterNXMButton_Click(object? sender, RoutedEventArgs e) { + if (!OperatingSystem.IsWindows()) + { + await new WarningWindow( + Program.translation.Get("ui.warning.unsupported_platform"), + Program.translation.Get("internal.ok")) + .ShowDialog(this); + return; + } + if (NXMProtocol.Validate(Program.executablePath) is false) { var requestWindow = new MessageWindow(Program.translation.Get("ui.message.confirm_nxm_association")); diff --git a/Stardrop/i18n/de.json b/Stardrop/i18n/de.json index 03946d7..523cf90 100644 --- a/Stardrop/i18n/de.json +++ b/Stardrop/i18n/de.json @@ -107,6 +107,7 @@ "ui.warning.unable_to_read_log": "Die SMAPI-Logdatei konnte nicht gelesen werden, um die Spielversion zu ermitteln.\n\nMods werden nicht auf Updates geprüft.", "ui.warning.no_manifest": "Keine manifest.json in \"{0}\" gefunden.", "ui.warning.unable_to_load_mod": "Die Datei unter \"{0}\" konnte nicht geladen werden.\n\n Weitere Informationen findest du in der Logdatei.", + "ui.warning.unsupported_platform": "NXM Verknüpfung ist nur auf Windows supported", // Messages "ui.message.confirm_mod_deletion": "Bist du sicher, dass du {0} löschen möchtest? Dies kann nicht rückgängig gemacht werden.", diff --git a/Stardrop/i18n/default.json b/Stardrop/i18n/default.json index 900ee25..0df98b2 100644 --- a/Stardrop/i18n/default.json +++ b/Stardrop/i18n/default.json @@ -217,6 +217,7 @@ "ui.warning.failed_to_get_download_link": "Failed to get the download url from Nexus Mods via the NXM link: {0}", "ui.warning.failed_to_set_association": "Failed to register the NXM protocol with Stardrop.\n\nSee log for details.", "ui.warning.already_associated": "Stardrop is already associated with the NXM protocol.", + "ui.warning.unsupported_platform": "NXM Connection is currently only supported on Windows", // Messages "ui.message.confirm_mod_deletion": "Are you sure you'd like to delete {0}? This cannot be undone.",