Skip to content

Commit

Permalink
Merge #4002 Fix crash with DLC disabled by Steam
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Jan 18, 2024
2 parents 1264e20 + 375ba31 commit 324b5f6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file.
- [GUI] Suppress admin user check for URL handler registration (#3996 by: HebaruSan)
- [Netkan] Fix null reference exception in swinfo transformer (#3999 by: HebaruSan)
- [GUI] Refactor Contents tab refreshing (#4001 by: HebaruSan)
- [Core] Fix crash with DLC disabled by Steam (#4002 by: HebaruSan)

### Internal

Expand Down
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;
}
}

0 comments on commit 324b5f6

Please sign in to comment.