Skip to content

Commit

Permalink
Merge Pull Request #1900 Use maximum of buildID and buildID64 if both…
Browse files Browse the repository at this point in the history
… are available
  • Loading branch information
politas committed Sep 15, 2016
2 parents 5361c58 + a408bb9 commit d41fe54
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
48 changes: 45 additions & 3 deletions Core/GameVersionProviders/KspBuildIdVersionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Text.RegularExpressions;
using CKAN.Versioning;
using log4net;

namespace CKAN.GameVersionProviders
{
Expand All @@ -12,6 +13,8 @@ public sealed class KspBuildIdVersionProvider : IGameVersionProvider
RegexOptions.IgnoreCase | RegexOptions.Compiled
);

private static readonly ILog Log = LogManager.GetLogger(typeof(KspBuildIdVersionProvider));

private readonly IKspBuildMap _kspBuildMap;

public KspBuildIdVersionProvider(IKspBuildMap kspBuildMap)
Expand All @@ -21,12 +24,51 @@ public KspBuildIdVersionProvider(IKspBuildMap kspBuildMap)

public bool TryGetVersion(string directory, out KspVersion result)
{
var buildIdPath = Path.Combine(directory, "buildID.txt");
KspVersion buildIdVersion;
var hasBuildId = TryGetVersionFromFile(Path.Combine(directory, "buildID.txt"), out buildIdVersion);

KspVersion buildId64Version;
var hasBuildId64 = TryGetVersionFromFile(Path.Combine(directory, "buildID64.txt"), out buildId64Version);

if (File.Exists(buildIdPath))
if (hasBuildId && hasBuildId64)
{
result = KspVersion.Max(buildIdVersion, buildId64Version);

if (buildIdVersion != buildId64Version)
{
Log.WarnFormat(
"Found different KSP versions in buildID.txt ({0}) and buildID64.txt ({1}), assuming {2}.",
buildIdVersion,
buildId64Version,
result
);
}

return true;
}
else if (hasBuildId64)
{
result = buildId64Version;
return true;
}
else if (hasBuildId)
{
result = buildIdVersion;
return true;
}
else
{
result = default(KspVersion);
return false;
}
}

private bool TryGetVersionFromFile(string file, out KspVersion result)
{
if (File.Exists(file))
{
var match = File
.ReadAllLines(buildIdPath)
.ReadAllLines(file)
.Select(i => BuildIdPattern.Match(i))
.FirstOrDefault(i => i.Success);

Expand Down
32 changes: 32 additions & 0 deletions Core/Versioning/KspVersion.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
Expand Down Expand Up @@ -641,6 +642,37 @@ public int CompareTo(KspVersion other)
}
}

public sealed partial class KspVersion
{
public static KspVersion Min(params KspVersion[] versions)
{
if (versions == null)
throw new ArgumentNullException("versions");

if (!versions.Any())
throw new ArgumentException("Value cannot be empty.", "versions");

if (versions.Any(i => i == null))
throw new ArgumentException("Value cannot contain null.", "versions");

return versions.Min();
}

public static KspVersion Max(params KspVersion[] versions)
{
if (versions == null)
throw new ArgumentNullException("versions");

if (!versions.Any())
throw new ArgumentException("Value cannot be empty.", "versions");

if (versions.Any(i => i == null))
throw new ArgumentException("Value cannot contain null.", "versions");

return versions.Max();
}
}

public sealed partial class KspVersion
{
private static string DeriveString(int major, int minor, int patch, int build)
Expand Down
4 changes: 3 additions & 1 deletion Core/builds.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"1230": "1.1.0.1230",
"1250": "1.1.1.1250",
"1260": "1.1.2.1260",
"1289": "1.1.3.1289"
"1289": "1.1.3.1289",
"1473": "1.2.0.1473",
"1479": "1.2.0.1479"
}
}

0 comments on commit d41fe54

Please sign in to comment.