From c72adcd119d9e96f47f93a2a7d5beb4974dffc0b Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 19 Feb 2017 01:28:00 -0500 Subject: use more robust crossplatform path checks in installer --- .../InteractiveInstaller.cs | 62 +++++++++++----------- 1 file changed, 32 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/StardewModdingAPI.Installer/InteractiveInstaller.cs b/src/StardewModdingAPI.Installer/InteractiveInstaller.cs index 1496bedd..7f59ed2a 100644 --- a/src/StardewModdingAPI.Installer/InteractiveInstaller.cs +++ b/src/StardewModdingAPI.Installer/InteractiveInstaller.cs @@ -4,9 +4,7 @@ using System.IO; using System.Linq; using System.Reflection; using System.Threading; -#if SMAPI_FOR_WINDOWS using Microsoft.Win32; -#endif using StardewModdingApi.Installer.Enums; namespace StardewModdingApi.Installer @@ -18,37 +16,43 @@ namespace StardewModdingApi.Installer ** Properties *********/ /// The default file paths where Stardew Valley can be installed. + /// The target platform. /// Derived from the crossplatform mod config: https://github.com/Pathoschild/Stardew.ModBuildConfig. - private IEnumerable DefaultInstallPaths + private IEnumerable GetDefaultInstallPaths(Platform platform) { - get + switch (platform) { - // Linux - yield return $"{Environment.GetEnvironmentVariable("HOME")}/GOG Games/Stardew Valley/game"; - yield return $"{Environment.GetEnvironmentVariable("HOME")}/.local/share/Steam/steamapps/common/Stardew Valley"; + case Platform.Mono: + // Linux + yield return $"{Environment.GetEnvironmentVariable("HOME")}/GOG Games/Stardew Valley/game"; + yield return $"{Environment.GetEnvironmentVariable("HOME")}/.local/share/Steam/steamapps/common/Stardew Valley"; + + // Mac + yield return "/Applications/Stardew Valley.app/Contents/MacOS"; + yield return $"{Environment.GetEnvironmentVariable("HOME")}/Library/Application Support/Steam/steamapps/common/Stardew Valley/Contents/MacOS"; + break; - // Mac - yield return "/Applications/Stardew Valley.app/Contents/MacOS"; - yield return $"{Environment.GetEnvironmentVariable("HOME")}/Library/Application Support/Steam/steamapps/common/Stardew Valley/Contents/MacOS"; + case Platform.Windows: + // Windows + yield return @"C:\Program Files (x86)\GalaxyClient\Games\Stardew Valley"; + yield return @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley"; - // Windows - yield return @"C:\Program Files (x86)\GalaxyClient\Games\Stardew Valley"; - yield return @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley"; + // Windows registry + IDictionary registryKeys = new Dictionary + { + [@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 413150"] = "InstallLocation", // Steam + [@"SOFTWARE\WOW6432Node\GOG.com\Games\1453375253"] = "PATH", // GOG on 64-bit Windows + }; + foreach (var pair in registryKeys) + { + string path = this.GetLocalMachineRegistryValue(pair.Key, pair.Value); + if (!string.IsNullOrWhiteSpace(path)) + yield return path; + } + break; - // Windows registry -#if SMAPI_FOR_WINDOWS - IDictionary registryKeys = new Dictionary - { - [@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 413150"] = "InstallLocation", // Steam - [@"SOFTWARE\WOW6432Node\GOG.com\Games\1453375253"] = "PATH", // GOG on 64-bit Windows - }; - foreach (var pair in registryKeys) - { - string path = this.GetLocalMachineRegistryValue(pair.Key, pair.Value); - if (!string.IsNullOrWhiteSpace(path)) - yield return path; - } -#endif + default: + throw new InvalidOperationException($"Unknown platform '{platform}'."); } } @@ -307,7 +311,6 @@ namespace StardewModdingApi.Installer } } -#if SMAPI_FOR_WINDOWS /// Get the value of a key in the Windows registry. /// The full path of the registry key relative to HKLM. /// The name of the value. @@ -320,7 +323,6 @@ namespace StardewModdingApi.Installer using (openKey) return (string)openKey.GetValue(name); } -#endif /// Print a debug message. /// The text to print. @@ -441,7 +443,7 @@ namespace StardewModdingApi.Installer : "StardewValley.exe"; // try default paths - foreach (string defaultPath in this.DefaultInstallPaths) + foreach (string defaultPath in this.GetDefaultInstallPaths(platform)) { DirectoryInfo dir = new DirectoryInfo(defaultPath); if (dir.Exists && dir.EnumerateFiles(executableFilename).Any()) -- cgit