diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-12-05 19:10:39 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-12-05 19:10:39 -0500 |
commit | 8a2618d812e4e2f36161907af5af5c484eb37abf (patch) | |
tree | c164325cf57fb43e4375e94dc584fbc3ab3ceeb5 /src/SMAPI.Toolkit/Framework | |
parent | 42e878e77d58569be81e34ca206a78e0ebe0604d (diff) | |
parent | 5b5dd47c22a1332a4c432d6a1cd414b5c83388d7 (diff) | |
download | SMAPI-8a2618d812e4e2f36161907af5af5c484eb37abf.tar.gz SMAPI-8a2618d812e4e2f36161907af5af5c484eb37abf.tar.bz2 SMAPI-8a2618d812e4e2f36161907af5af5c484eb37abf.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI.Toolkit/Framework')
-rw-r--r-- | src/SMAPI.Toolkit/Framework/GameScanning/GameFolderType.cs | 21 | ||||
-rw-r--r-- | src/SMAPI.Toolkit/Framework/GameScanning/GameScanner.cs | 55 |
2 files changed, 73 insertions, 3 deletions
diff --git a/src/SMAPI.Toolkit/Framework/GameScanning/GameFolderType.cs b/src/SMAPI.Toolkit/Framework/GameScanning/GameFolderType.cs new file mode 100644 index 00000000..d18af59b --- /dev/null +++ b/src/SMAPI.Toolkit/Framework/GameScanning/GameFolderType.cs @@ -0,0 +1,21 @@ +namespace StardewModdingAPI.Toolkit.Framework.GameScanning +{ + /// <summary>The detected validity for a Stardew Valley game folder based on file structure heuristics.</summary> + public enum GameFolderType + { + /// <summary>The folder seems to contain a valid Stardew Valley 1.5.5+ install.</summary> + Valid, + + /// <summary>The folder doesn't contain Stardew Valley.</summary> + NoGameFound, + + /// <summary>The folder contains Stardew Valley 1.5.4 or earlier. This version uses XNA Framework and 32-bit .NET Framework 4.5.2 on Windows and Mono on Linux/macOS, and isn't compatible with current versions of SMAPI.</summary> + Legacy154OrEarlier, + + /// <summary>The folder contains Stardew Valley from the game's legacy compatibility branch, which backports newer changes to the <see cref="Legacy154OrEarlier"/> format.</summary> + LegacyCompatibilityBranch, + + /// <summary>The folder seems to contain Stardew Valley files, but they failed to load for unknown reasons (e.g. corrupted executable).</summary> + InvalidUnknown + } +} diff --git a/src/SMAPI.Toolkit/Framework/GameScanning/GameScanner.cs b/src/SMAPI.Toolkit/Framework/GameScanning/GameScanner.cs index 7553c07f..37e4f263 100644 --- a/src/SMAPI.Toolkit/Framework/GameScanning/GameScanner.cs +++ b/src/SMAPI.Toolkit/Framework/GameScanning/GameScanner.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Xml.Linq; using System.Xml.XPath; using StardewModdingAPI.Toolkit.Utilities; +using System.Reflection; #if SMAPI_FOR_WINDOWS using Microsoft.Win32; #endif @@ -54,11 +55,59 @@ namespace StardewModdingAPI.Toolkit.Framework.GameScanning /// <param name="dir">The folder to check.</param> public bool LooksLikeGameFolder(DirectoryInfo dir) { - return - dir.Exists - && dir.EnumerateFiles("Stardew Valley.dll").Any(); + return this.GetGameFolderType(dir) == GameFolderType.Valid; } + /// <summary>Detect the validity of a game folder based on file structure heuristics.</summary> + /// <param name="dir">The folder to check.</param> + public GameFolderType GetGameFolderType(DirectoryInfo dir) + { + // no such folder + if (!dir.Exists) + return GameFolderType.NoGameFound; + + // apparently valid + if (dir.EnumerateFiles("Stardew Valley.dll").Any()) + return GameFolderType.Valid; + + // doesn't contain any version of Stardew Valley + FileInfo executable = new(Path.Combine(dir.FullName, "Stardew Valley.exe")); + if (!executable.Exists) + executable = new(Path.Combine(dir.FullName, "StardewValley.exe")); // pre-1.5.5 Linux/macOS executable + if (!executable.Exists) + return GameFolderType.NoGameFound; + + // get assembly version + Version version; + try + { + version = AssemblyName.GetAssemblyName(executable.FullName).Version; + } + catch + { + // The executable exists but it doesn't seem to be a valid assembly. This would + // happen with Stardew Valley 1.5.5+, but that should have been flagged as a valid + // folder before this point. + return GameFolderType.InvalidUnknown; + } + + // ignore Stardew Valley 1.5.5+ at this point + if (version.Major == 1 && version.Minor == 3 && version.Build == 37) + return GameFolderType.InvalidUnknown; + + // incompatible version + if (version.Major == 1 && version.Minor < 4) + { + // Stardew Valley 1.5.4 and earlier have assembly versions <= 1.3.7853.31734 + if (version.Minor < 3 || version.Build <= 7853) + return GameFolderType.Legacy154OrEarlier; + + // Stardew Valley 1.5.5+ legacy compatibility branch + return GameFolderType.LegacyCompatibilityBranch; + } + + return GameFolderType.InvalidUnknown; + } /********* ** Private methods |