From a6741cce9bb408c44b94f9a8d814856a9cfc806f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 8 Jun 2018 19:10:42 -0400 Subject: [PATCH] detect game install path via Steam library path (#512) Thanks to InkyQuill! --- src/SMAPI.Installer/InteractiveInstaller.cs | 26 ++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/SMAPI.Installer/InteractiveInstaller.cs b/src/SMAPI.Installer/InteractiveInstaller.cs index 02dd6891..30cb10b4 100644 --- a/src/SMAPI.Installer/InteractiveInstaller.cs +++ b/src/SMAPI.Installer/InteractiveInstaller.cs @@ -66,6 +66,11 @@ namespace StardewModdingApi.Installer if (!string.IsNullOrWhiteSpace(path)) yield return path; } + + // via Steam library path + string steampath = this.GetCurrentUserRegistryValue(@"Software\Valve\Steam", "SteamPath"); + if (steampath != null) + yield return Path.Combine(steampath.Replace('/', '\\'), @"steamapps\common\Stardew Valley"); } break; @@ -133,11 +138,11 @@ namespace StardewModdingApi.Installer /// Initialisation flow: /// 1. Collect information (mainly OS and install path) and validate it. /// 2. Ask the user whether to install or uninstall. - /// + /// /// Uninstall logic: /// 1. On Linux/Mac: if a backup of the launcher exists, delete the launcher and restore the backup. /// 2. Delete all files and folders in the game directory matching one of the values returned by . - /// + /// /// Install flow: /// 1. Run the uninstall flow. /// 2. Copy the SMAPI files from package/Windows or package/Mono into the game directory. @@ -431,7 +436,7 @@ namespace StardewModdingApi.Installer return str; } - /// Get the value of a key in the Windows registry. + /// Get the value of a key in the Windows HKLM registry. /// The full path of the registry key relative to HKLM. /// The name of the value. private string GetLocalMachineRegistryValue(string key, string name) @@ -444,6 +449,19 @@ namespace StardewModdingApi.Installer return (string)openKey.GetValue(name); } + /// Get the value of a key in the Windows HKCU registry. + /// The full path of the registry key relative to HKCU. + /// The name of the value. + private string GetCurrentUserRegistryValue(string key, string name) + { + RegistryKey currentuser = Environment.Is64BitOperatingSystem ? RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64) : Registry.CurrentUser; + RegistryKey openKey = currentuser.OpenSubKey(key); + if (openKey == null) + return null; + using (openKey) + return (string)openKey.GetValue(name); + } + /// Print a debug message. /// The text to print. private void PrintDebug(string text) => this.ConsoleWriter.WriteLine(text, ConsoleLogLevel.Debug); @@ -602,6 +620,8 @@ namespace StardewModdingApi.Installer where dir.Exists && dir.EnumerateFiles(executableFilename).Any() select dir ) + .GroupBy(p => p.FullName, StringComparer.InvariantCultureIgnoreCase) // ignore duplicate paths + .Select(p => p.First()) .ToArray(); // choose where to install