update installer for VdfConverter & rework avoid custom models

This commit is contained in:
Jesse Plamondon-Willard 2022-10-08 21:32:10 -04:00
parent c6b3446e9c
commit ab66266b4b
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
4 changed files with 30 additions and 65 deletions

View File

@ -154,7 +154,7 @@ foreach ($folder in $folders) {
cp -Recurse "$smapiBin/i18n" "$bundlePath/smapi-internal" cp -Recurse "$smapiBin/i18n" "$bundlePath/smapi-internal"
# bundle smapi-internal # bundle smapi-internal
foreach ($name in @("0Harmony.dll", "0Harmony.xml", "Mono.Cecil.dll", "Mono.Cecil.Mdb.dll", "Mono.Cecil.Pdb.dll", "MonoMod.Common.dll", "Newtonsoft.Json.dll", "Pathoschild.Http.Client.dll", "Pintail.dll", "TMXTile.dll", "SMAPI.Toolkit.dll", "SMAPI.Toolkit.pdb", "SMAPI.Toolkit.xml", "SMAPI.Toolkit.CoreInterfaces.dll", "SMAPI.Toolkit.CoreInterfaces.pdb", "SMAPI.Toolkit.CoreInterfaces.xml", "System.Net.Http.Formatting.dll")) { foreach ($name in @("0Harmony.dll", "0Harmony.xml", "Mono.Cecil.dll", "Mono.Cecil.Mdb.dll", "Mono.Cecil.Pdb.dll", "MonoMod.Common.dll", "Newtonsoft.Json.dll", "Pathoschild.Http.Client.dll", "Pintail.dll", "TMXTile.dll", "SMAPI.Toolkit.dll", "SMAPI.Toolkit.pdb", "SMAPI.Toolkit.xml", "SMAPI.Toolkit.CoreInterfaces.dll", "SMAPI.Toolkit.CoreInterfaces.pdb", "SMAPI.Toolkit.CoreInterfaces.xml", "System.Net.Http.Formatting.dll", "VdfConverter.dll")) {
cp "$smapiBin/$name" "$bundlePath/smapi-internal" cp "$smapiBin/$name" "$bundlePath/smapi-internal"
} }

View File

@ -24,6 +24,9 @@ namespace StardewModdingAPI.Toolkit.Framework.GameScanning
/// <summary>The current OS.</summary> /// <summary>The current OS.</summary>
private readonly Platform Platform; private readonly Platform Platform;
/// <summary>The Steam app ID for Stardew Valley.</summary>
private const string SteamAppId = "413150";
/********* /*********
** Public methods ** Public methods
@ -146,7 +149,7 @@ namespace StardewModdingAPI.Toolkit.Framework.GameScanning
#if SMAPI_FOR_WINDOWS #if SMAPI_FOR_WINDOWS
IDictionary<string, string> registryKeys = new Dictionary<string, string> IDictionary<string, string> registryKeys = new Dictionary<string, string>
{ {
[@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 413150"] = "InstallLocation", // Steam [@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App " + GameScanner.SteamAppId] = "InstallLocation", // Steam
[@"SOFTWARE\WOW6432Node\GOG.com\Games\1453375253"] = "PATH", // GOG on 64-bit Windows [@"SOFTWARE\WOW6432Node\GOG.com\Games\1453375253"] = "PATH", // GOG on 64-bit Windows
}; };
foreach (var pair in registryKeys) foreach (var pair in registryKeys)
@ -160,9 +163,10 @@ namespace StardewModdingAPI.Toolkit.Framework.GameScanning
string? steamPath = this.GetCurrentUserRegistryValue(@"Software\Valve\Steam", "SteamPath"); string? steamPath = this.GetCurrentUserRegistryValue(@"Software\Valve\Steam", "SteamPath");
if (steamPath != null) if (steamPath != null)
{ {
// conventional path
yield return Path.Combine(steamPath.Replace('/', '\\'), @"steamapps\common\Stardew Valley"); yield return Path.Combine(steamPath.Replace('/', '\\'), @"steamapps\common\Stardew Valley");
// Check for Steam libraries in other locations // from Steam's .vdf file
string? path = this.GetPathFromSteamLibrary(steamPath); string? path = this.GetPathFromSteamLibrary(steamPath);
if (!string.IsNullOrWhiteSpace(path)) if (!string.IsNullOrWhiteSpace(path))
yield return path; yield return path;
@ -257,26 +261,34 @@ namespace StardewModdingAPI.Toolkit.Framework.GameScanning
/// <returns>The game directory, if found.</returns> /// <returns>The game directory, if found.</returns>
private string? GetPathFromSteamLibrary(string? steamPath) private string? GetPathFromSteamLibrary(string? steamPath)
{ {
string stardewAppId = "413150"; if (steamPath == null)
if (steamPath != null) return null;
// get raw .vdf data
string libraryFoldersPath = Path.Combine(steamPath.Replace('/', '\\'), "steamapps\\libraryfolders.vdf");
using FileStream fileStream = File.OpenRead(libraryFoldersPath);
VdfDeserializer deserializer = new();
dynamic libraries = deserializer.Deserialize(fileStream);
if (libraries?.libraryfolders is null)
return null;
// get path from Stardew Valley app (if any)
foreach (dynamic pair in libraries.libraryfolders)
{ {
string? libraryFoldersPath = Path.Combine(steamPath.Replace('/', '\\'), "steamapps\\libraryfolders.vdf"); dynamic library = pair.Value;
using FileStream fs = File.OpenRead(libraryFoldersPath);
VdfDeserializer deserializer = new VdfDeserializer(); foreach (dynamic app in library.apps)
SteamLibraryCollection libraries = deserializer.Deserialize<SteamLibraryCollection>(fs);
if (libraries.libraryfolders != null)
{ {
var stardewLibrary = libraries.libraryfolders.FirstOrDefault(f => string key = app.Key;
if (key == GameScanner.SteamAppId)
{ {
var apps = f.Value?.apps; string path = library.path;
return apps != null && apps.Any(a => a.Key.Equals(stardewAppId));
}); return Path.Combine(path.Replace("\\\\", "\\"), "steamapps", "common", "Stardew Valley");
if (stardewLibrary.Value?.path != null)
{
return Path.Combine(stardewLibrary.Value.path.Replace("\\\\", "\\"), @"steamapps\common\Stardew Valley");
} }
} }
} }
return null; return null;
} }
#endif #endif

View File

@ -1,47 +0,0 @@
#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.
/// <summary>Model for Steam's libraryfolders.vdf.</summary>
public class SteamLibraryCollection
{
/// <summary>Each entry identifies a different location that part of the Steam games library is installed to.</summary>
public LibraryFolders<int, LibraryFolder> libraryfolders { get; set; }
}
/// <summary>A collection of LibraryFolders. Like a dictionary, but has contentstatsid used as an index also.</summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
#pragma warning disable CS8714 // Required for model.
public class LibraryFolders<TKey, TValue> : Dictionary<TKey, TValue>
#pragma warning restore CS8714
{
/// <summary>Index of the library, starting from "0".</summary>
public string contentstatsid { get; set; }
}
/// <summary>A Steam library folder, containing information on the location and size of games installed there.</summary>
public class LibraryFolder
{
/// <summary>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.</summary>
public string path { get; set; }
/// <summary>Label for the library, or ""</summary>
public string label { get; set; }
/// <summary>~19-digit identifier.</summary>
public string contentid { get; set; }
/// <summary>Size of the library in bytes. May show 0 when size is non-zero.</summary>
public string totalsize { get; set; }
/// <summary>Used for downloads.</summary>
public string update_clean_bytes_tally { get; set; }
/// <summary>Normally "0".</summary>
public string time_last_update_corruption { get; set; }
/// <summary>List of Steam app IDs, and their current size in bytes.</summary>
public Dictionary<string, string> apps { get; set; }
}
#pragma warning restore IDE1006
#pragma warning restore CS8618
}
#endif

View File

@ -14,7 +14,7 @@
<PackageReference Include="Pathoschild.Http.FluentClient" Version="4.1.1" /> <PackageReference Include="Pathoschild.Http.FluentClient" Version="4.1.1" />
<PackageReference Include="System.Management" Version="5.0.0" Condition="'$(OS)' == 'Windows_NT'" /> <PackageReference Include="System.Management" Version="5.0.0" Condition="'$(OS)' == 'Windows_NT'" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" Condition="'$(OS)' == 'Windows_NT'" /> <PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" Condition="'$(OS)' == 'Windows_NT'" />
<PackageReference Include="VdfConverter" Version="1.0.3" /> <PackageReference Include="VdfConverter" Version="1.0.3" Condition="'$(OS)' == 'Windows_NT'" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>