From 375ba3129a32b2346947dab4446e1c2a63cf42db Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Wed, 17 Jan 2024 19:03:29 -0600 Subject: [PATCH] Fix crash with DLC disabled by Steam --- Core/Extensions/DictionaryExtensions.cs | 3 +- .../DLC/StandardDlcDetectorBase.cs | 67 ++++++++----------- 2 files changed, 30 insertions(+), 40 deletions(-) diff --git a/Core/Extensions/DictionaryExtensions.cs b/Core/Extensions/DictionaryExtensions.cs index 2ab1ae94f1..ec7b932985 100644 --- a/Core/Extensions/DictionaryExtensions.cs +++ b/Core/Extensions/DictionaryExtensions.cs @@ -10,7 +10,8 @@ public static bool DictionaryEquals(this IDictionary a, => a == null ? b == null : b != null && a.Count == b.Count && a.Keys.All(k => b.ContainsKey(k)) - && b.Keys.All(k => a.ContainsKey(k) && a[k].Equals(b[k])); + && b.Keys.All(k => a.ContainsKey(k) + && EqualityComparer.Default.Equals(a[k], b[k])); public static V GetOrDefault(this Dictionary dict, K key) { diff --git a/Core/Games/KerbalSpaceProgram/DLC/StandardDlcDetectorBase.cs b/Core/Games/KerbalSpaceProgram/DLC/StandardDlcDetectorBase.cs index a707d38245..71c695c1eb 100644 --- a/Core/Games/KerbalSpaceProgram/DLC/StandardDlcDetectorBase.cs +++ b/Core/Games/KerbalSpaceProgram/DLC/StandardDlcDetectorBase.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; @@ -31,7 +32,10 @@ public abstract class StandardDlcDetectorBase : IDlcDetector RegexOptions.Compiled | RegexOptions.IgnoreCase ); - protected StandardDlcDetectorBase(IGame game, string identifierBaseName, GameVersion releaseGameVersion, Dictionary canonicalVersions = null) + protected StandardDlcDetectorBase(IGame game, + string identifierBaseName, + GameVersion releaseGameVersion, + Dictionary canonicalVersions = null) : this(game, identifierBaseName, identifierBaseName, releaseGameVersion, canonicalVersions) { } protected StandardDlcDetectorBase(IGame game, string identifierBaseName, string directoryBaseName, GameVersion releaseGameVersion, Dictionary canonicalVersions = null) @@ -55,51 +59,36 @@ protected StandardDlcDetectorBase(IGame game, string identifierBaseName, string public virtual bool IsInstalled(GameInstance ksp, out string identifier, out UnmanagedModuleVersion version) { - identifier = $"{IdentifierBaseName}-DLC"; - version = null; - - var directoryPath = Path.Combine(game.PrimaryModDirectory(ksp), "SquadExpansion", DirectoryBaseName); - if (Directory.Exists(directoryPath)) + var directoryPath = Path.Combine(ksp.GameDir(), InstallPath()); + var readmeFilePath = Path.Combine(directoryPath, "readme.txt"); + // Steam leaves empty folders behind when you "disable" a DLC, + // so only return true if the readme exists + if (Directory.Exists(directoryPath) && File.Exists(readmeFilePath)) { - var readmeFilePath = Path.Combine(directoryPath, "readme.txt"); - - if (File.Exists(readmeFilePath)) - { - foreach (var line in File.ReadAllLines(readmeFilePath)) - { - var match = VersionPattern.Match(line); - - if (match.Success) - { - var versionStr = match.Groups["version"].Value; - - if (CanonicalVersions.ContainsKey(versionStr)) - { - versionStr = CanonicalVersions[versionStr]; - } - - version = new UnmanagedModuleVersion(versionStr); - break; - } - } - } - + identifier = $"{IdentifierBaseName}-DLC"; + version = new UnmanagedModuleVersion( + File.ReadAllLines(readmeFilePath) + .Select(line => VersionPattern.Match(line)) + .Where(match => match.Success) + .Select(match => match.Groups["version"].Value) + .Select(verStr => CanonicalVersions.TryGetValue(verStr, out string overrideVer) + ? overrideVer + : verStr) + // A null string results in UnmanagedModuleVersion with IsUnknownVersion==true + .FirstOrDefault()); return true; } - else - { - return false; - } + identifier = null; + version = null; + return false; } public virtual string InstallPath() - { - return Path.Combine("GameData", "SquadExpansion", DirectoryBaseName); - } + => Path.Combine(game.PrimaryModDirectoryRelative, + "SquadExpansion", + DirectoryBaseName); public bool AllowedOnBaseVersion(GameVersion baseVersion) - { - return baseVersion >= ReleaseGameVersion; - } + => baseVersion >= ReleaseGameVersion; } }