From c6b3446e9cb1d1e02db9db86f143ecfe75e9908c Mon Sep 17 00:00:00 2001 From: pizzaoverhead Date: Thu, 29 Sep 2022 13:33:45 +0100 Subject: [PATCH] Added checking for alternative Steam library install locations when looking for the Stardew Valley install. --- .../Framework/GameScanning/GameScanner.cs | 36 ++++++++++++++ .../GameScanning/SteamLibraryCollection.cs | 47 +++++++++++++++++++ src/SMAPI.Toolkit/SMAPI.Toolkit.csproj | 1 + 3 files changed, 84 insertions(+) create mode 100644 src/SMAPI.Toolkit/Framework/GameScanning/SteamLibraryCollection.cs diff --git a/src/SMAPI.Toolkit/Framework/GameScanning/GameScanner.cs b/src/SMAPI.Toolkit/Framework/GameScanning/GameScanner.cs index 8e1538a5..8e24dcdf 100644 --- a/src/SMAPI.Toolkit/Framework/GameScanning/GameScanner.cs +++ b/src/SMAPI.Toolkit/Framework/GameScanning/GameScanner.cs @@ -9,6 +9,7 @@ using StardewModdingAPI.Toolkit.Utilities; using System.Reflection; #if SMAPI_FOR_WINDOWS using Microsoft.Win32; +using VdfParser; #endif namespace StardewModdingAPI.Toolkit.Framework.GameScanning @@ -158,7 +159,14 @@ namespace StardewModdingAPI.Toolkit.Framework.GameScanning // 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"); + + // Check for Steam libraries in other locations + string? path = this.GetPathFromSteamLibrary(steamPath); + if (!string.IsNullOrWhiteSpace(path)) + yield return path; + } #endif // default GOG/Steam paths @@ -243,6 +251,34 @@ namespace StardewModdingAPI.Toolkit.Framework.GameScanning using (openKey) return (string?)openKey.GetValue(name); } + + /// Get the game directory path from alternative Steam library locations. + /// The full path to the directory containing steam.exe. + /// The game directory, if found. + private string? GetPathFromSteamLibrary(string? steamPath) + { + string stardewAppId = "413150"; + if (steamPath != null) + { + string? libraryFoldersPath = Path.Combine(steamPath.Replace('/', '\\'), "steamapps\\libraryfolders.vdf"); + using FileStream fs = File.OpenRead(libraryFoldersPath); + VdfDeserializer deserializer = new VdfDeserializer(); + SteamLibraryCollection libraries = deserializer.Deserialize(fs); + if (libraries.libraryfolders != null) + { + var stardewLibrary = libraries.libraryfolders.FirstOrDefault(f => + { + var apps = f.Value?.apps; + return apps != null && apps.Any(a => a.Key.Equals(stardewAppId)); + }); + if (stardewLibrary.Value?.path != null) + { + return Path.Combine(stardewLibrary.Value.path.Replace("\\\\", "\\"), @"steamapps\common\Stardew Valley"); + } + } + } + return null; + } #endif } } diff --git a/src/SMAPI.Toolkit/Framework/GameScanning/SteamLibraryCollection.cs b/src/SMAPI.Toolkit/Framework/GameScanning/SteamLibraryCollection.cs new file mode 100644 index 00000000..7a186f69 --- /dev/null +++ b/src/SMAPI.Toolkit/Framework/GameScanning/SteamLibraryCollection.cs @@ -0,0 +1,47 @@ +#if SMAPI_FOR_WINDOWS +using System.Collections.Generic; + +namespace StardewModdingAPI.Toolkit.Framework.GameScanning +{ +#pragma warning disable IDE1006 // Model requires lowercase naming. +#pragma warning disable CS8618 // Required for model. + /// Model for Steam's libraryfolders.vdf. + public class SteamLibraryCollection + { + /// Each entry identifies a different location that part of the Steam games library is installed to. + public LibraryFolders libraryfolders { get; set; } + } + + /// A collection of LibraryFolders. Like a dictionary, but has contentstatsid used as an index also. + /// + /// +#pragma warning disable CS8714 // Required for model. + public class LibraryFolders : Dictionary +#pragma warning restore CS8714 + { + /// Index of the library, starting from "0". + public string contentstatsid { get; set; } + } + + /// A Steam library folder, containing information on the location and size of games installed there. + public class LibraryFolder + { + /// The escaped path to this Steam library folder. There will be a steam.exe here, but this may not be the one the player generally launches. + public string path { get; set; } + /// Label for the library, or "" + public string label { get; set; } + /// ~19-digit identifier. + public string contentid { get; set; } + /// Size of the library in bytes. May show 0 when size is non-zero. + public string totalsize { get; set; } + /// Used for downloads. + public string update_clean_bytes_tally { get; set; } + /// Normally "0". + public string time_last_update_corruption { get; set; } + /// List of Steam app IDs, and their current size in bytes. + public Dictionary apps { get; set; } + } +#pragma warning restore IDE1006 +#pragma warning restore CS8618 +} +#endif diff --git a/src/SMAPI.Toolkit/SMAPI.Toolkit.csproj b/src/SMAPI.Toolkit/SMAPI.Toolkit.csproj index 7b79105f..411fd469 100644 --- a/src/SMAPI.Toolkit/SMAPI.Toolkit.csproj +++ b/src/SMAPI.Toolkit/SMAPI.Toolkit.csproj @@ -14,6 +14,7 @@ +