Skip to content

Commit

Permalink
Support detecting real MakingHistory DLC
Browse files Browse the repository at this point in the history
  • Loading branch information
dbent committed Mar 13, 2018
1 parent 2a68d7a commit ca28830
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CKAN.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=InvertIf/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=RedundantIfElseBlock/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=LocalFunctions/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/UnitTesting/ShadowCopy/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/PatternsAndTemplates/Todo/TodoPatterns/=0A4664ADE151C349B62FB87D8470A5D9/Name/@EntryValue">Not Implemented</s:String>
Expand Down
2 changes: 2 additions & 0 deletions Core/CKAN-core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
<Compile Include="Converters\JsonOldResourceUrlConverter.cs" />
<Compile Include="Converters\JsonSimpleStringConverter.cs" />
<Compile Include="Converters\JsonSingleOrArrayConverter.cs" />
<Compile Include="DLC\IDlcDetector.cs" />
<Compile Include="DLC\MakingHistoryDlcDetector.cs" />
<Compile Include="Exporters\BbCodeExporter.cs" />
<Compile Include="Exporters\DelimeterSeperatedValueExporter.cs" />
<Compile Include="Exporters\Exporter.cs" />
Expand Down
39 changes: 39 additions & 0 deletions Core/DLC/IDlcDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using CKAN.Versioning;

namespace CKAN.DLC
{
/// <summary>
/// Represents an object that can detect the presence of official DLC in a KSP installation.
/// </summary>
public interface IDlcDetector
{
/// <summary>
/// Checks if the relevant DLC is installed in the specific KSP installation.
/// </summary>
/// <param name="ksp">The KSP installation to check.</param>
/// <param name="identifier">
/// The identifier to use for the DLC's psuedo-module. Value is undefined if this method is undefined.
/// </param>
/// <param name="version">
/// The version of the installed DLC. May be <c>null</c> if it is not possible to get the specific version of
/// the DLC. Value is undefined if this method returns <c>false</c>.
/// </param>
/// <returns>
/// <list type="bullet">
/// <item>
/// <term><c>true</c></term>
/// <description>
/// When the relevant DLC is installed in the installation specified by <paramref name="ksp"/>.
/// </description>
/// </item>
/// <item>
/// <term><c>false</c></term>
/// <description>
/// When the relevant DLC is not installed in the installation specified by <paramref name="ksp"/>.
/// </description>
/// </item>
/// </list>
/// </returns>
bool IsInstalled(KSP ksp, out string identifier, out UnmanagedModuleVersion version);
}
}
62 changes: 62 additions & 0 deletions Core/DLC/MakingHistoryDlcDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using CKAN.Versioning;

namespace CKAN.DLC
{
/// <summary>
/// Represents an object that can detect the presence of the official Making History DLC in a KSP installation.
/// </summary>
public sealed class MakingHistoryDlcDetector : IDlcDetector
{
private const string Identifier = "MakingHistory-DLC";

private static readonly Dictionary<string, string> CanonicalVersions = new Dictionary<string, string>()
{
{ "1.0", "1.0.0" }
};

private static readonly Regex VersionPattern = new Regex(
@"^Version\s+(?<version>\S+)$",
RegexOptions.Compiled | RegexOptions.IgnoreCase
);

public bool IsInstalled(KSP ksp, out string identifier, out UnmanagedModuleVersion version)
{
identifier = Identifier;
version = null;

var directoryPath = Path.Combine(ksp.GameData(), "SquadExpansion", "MakingHistory");
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;
}
}
}
}
38 changes: 35 additions & 3 deletions Core/Registry/RegistryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Text;
using ChinhDo.Transactions;
using CKAN.DLC;
using CKAN.Versioning;
using log4net;
using Newtonsoft.Json;
Expand Down Expand Up @@ -446,6 +447,24 @@ public void ExportInstalled(string path, bool recommends, bool with_versions)
}

public void ScanDlc()
{
var testDlc = TestDlcScan();
var wellKnownDlc = WellKnownDlcScan();

registry.ClearDlc();

foreach (var i in testDlc)
{
registry.RegisterDlc(i.Key, i.Value);
}

foreach (var i in wellKnownDlc)
{
registry.RegisterDlc(i.Key, i.Value);
}
}

private Dictionary<string, UnmanagedModuleVersion> TestDlcScan()
{
var dlc = new Dictionary<string, UnmanagedModuleVersion>();

Expand All @@ -461,11 +480,24 @@ public void ScanDlc()
}
}

registry.ClearDlc();
foreach (var i in dlc)
return dlc;
}

private Dictionary<string, UnmanagedModuleVersion> WellKnownDlcScan()
{
var dlc = new Dictionary<string, UnmanagedModuleVersion>();

var detectors = new IDlcDetector[] { new MakingHistoryDlcDetector() };

foreach (var d in detectors)
{
registry.RegisterDlc(i.Key, i.Value);
if (d.IsInstalled(ksp, out var identifier, out var version))
{
dlc[identifier] = version ?? new UnmanagedModuleVersion(null);
}
}

return dlc;
}
}
}

0 comments on commit ca28830

Please sign in to comment.