-
-
Notifications
You must be signed in to change notification settings - Fork 348
/
StandardDlcDetectorBase.cs
105 lines (87 loc) · 3.89 KB
/
StandardDlcDetectorBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using CKAN.DLC;
using CKAN.Versioning;
namespace CKAN.Games.KerbalSpaceProgram.DLC
{
/// <summary>
/// Base class for DLC Detectors that follow standard conventions.
/// </summary>
/// <remarks>
/// "Standard conventions" is defined as detecting installation by the presence of directory with the name
/// DirectoryBaseName in the [GameData]/SquadExpansion directory, detecting version by parsing a version line in
/// a readme.txt file in the same directory, and having an identifier of IdentifierBaseName-DLC.
/// </remarks>
public abstract class StandardDlcDetectorBase : IDlcDetector
{
public GameVersion ReleaseGameVersion { get; }
public string IdentifierBaseName { get; }
private readonly string DirectoryBaseName;
private readonly Dictionary<string, string> CanonicalVersions;
private readonly IGame game;
private static readonly Regex VersionPattern = new Regex(
@"^Version\s+(?<version>\S+)",
RegexOptions.Compiled | RegexOptions.IgnoreCase
);
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)
{
if (string.IsNullOrWhiteSpace(identifierBaseName))
{
throw new ArgumentException("Value must be provided.", nameof(identifierBaseName));
}
if (string.IsNullOrWhiteSpace(directoryBaseName))
{
throw new ArgumentException("Value must be provided.", nameof(directoryBaseName));
}
this.game = game;
IdentifierBaseName = identifierBaseName;
DirectoryBaseName = directoryBaseName;
ReleaseGameVersion = releaseGameVersion;
CanonicalVersions = canonicalVersions ?? new Dictionary<string, 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 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;
}
}
}
return true;
}
else
{
return false;
}
}
public virtual string InstallPath()
{
return Path.Combine("GameData", "SquadExpansion", DirectoryBaseName);
}
public bool AllowedOnBaseVersion(GameVersion baseVersion)
{
return baseVersion >= ReleaseGameVersion;
}
}
}