Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 33 additions & 22 deletions Stardrop/Utilities/NXMProtocol.cs
Original file line number Diff line number Diff line change
@@ -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\"");
}
Expand All @@ -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;
}
}
}
9 changes: 9 additions & 0 deletions Stardrop/Views/SettingsWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
1 change: 1 addition & 0 deletions Stardrop/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
1 change: 1 addition & 0 deletions Stardrop/i18n/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
Loading