Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash with DLC disabled by Steam #4002

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Core/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public static bool DictionaryEquals<K, V>(this IDictionary<K, V> 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<V>.Default.Equals(a[k], b[k]));

public static V GetOrDefault<K, V>(this Dictionary<K, V> dict, K key)
{
Expand Down
67 changes: 28 additions & 39 deletions Core/Games/KerbalSpaceProgram/DLC/StandardDlcDetectorBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -31,7 +32,10 @@ public abstract class StandardDlcDetectorBase : IDlcDetector
RegexOptions.Compiled | RegexOptions.IgnoreCase
);

protected StandardDlcDetectorBase(IGame game, string identifierBaseName, GameVersion releaseGameVersion, Dictionary<string, string> canonicalVersions = null)
protected StandardDlcDetectorBase(IGame game,
string identifierBaseName,
GameVersion releaseGameVersion,
Dictionary<string, string> canonicalVersions = null)
: this(game, identifierBaseName, identifierBaseName, releaseGameVersion, canonicalVersions) { }

protected StandardDlcDetectorBase(IGame game, string identifierBaseName, string directoryBaseName, GameVersion releaseGameVersion, Dictionary<string, string> canonicalVersions = null)
Expand All @@ -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;
}
}
Loading