-
-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
2,013 additions
and
607 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
build/ | ||
/.vs/ | ||
packages/ | ||
CKAN/*/bin | ||
CKAN/*/obj | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using CKAN.Versioning; | ||
|
||
namespace CKAN.GameVersionProviders | ||
{ | ||
public interface IGameVersionProvider | ||
{ | ||
bool TryGetVersion(string directory, out KspVersion result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using CKAN.Versioning; | ||
|
||
namespace CKAN.GameVersionProviders | ||
{ | ||
public interface IKspBuildMap | ||
{ | ||
KspVersion this[string buildId] { get; } | ||
|
||
void Refresh(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using CKAN.Versioning; | ||
|
||
namespace CKAN.GameVersionProviders | ||
{ | ||
// ReSharper disable once ClassNeverInstantiated.Global | ||
public sealed class KspBuildIdVersionProvider : IGameVersionProvider | ||
{ | ||
private static readonly Regex BuildIdPattern = new Regex(@"^build id\s+=\s+0*(?<buildid>\d+)", | ||
RegexOptions.IgnoreCase | RegexOptions.Compiled | ||
); | ||
|
||
private readonly IKspBuildMap _kspBuildMap; | ||
|
||
public KspBuildIdVersionProvider(IKspBuildMap kspBuildMap) | ||
{ | ||
_kspBuildMap = kspBuildMap; | ||
} | ||
|
||
public bool TryGetVersion(string directory, out KspVersion result) | ||
{ | ||
var buildIdPath = Path.Combine(directory, "buildID.txt"); | ||
|
||
if (File.Exists(buildIdPath)) | ||
{ | ||
var match = File | ||
.ReadAllLines(buildIdPath) | ||
.Select(i => BuildIdPattern.Match(i)) | ||
.FirstOrDefault(i => i.Success); | ||
|
||
if (match != null) | ||
{ | ||
var version = _kspBuildMap[match.Groups["buildid"].Value]; | ||
|
||
if (version != null) | ||
{ | ||
result = version; | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
result = default(KspVersion); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using CKAN.Versioning; | ||
using log4net; | ||
using Newtonsoft.Json; | ||
|
||
namespace CKAN.GameVersionProviders | ||
{ | ||
// ReSharper disable once ClassNeverInstantiated.Global | ||
public sealed class KspBuildMap : IKspBuildMap | ||
{ | ||
// TODO: Need a way for the client to configure this | ||
private static readonly Uri BuildMapUri = | ||
new Uri("https://raw.githubusercontent.com/KSP-CKAN/CKAN-meta/master/builds.json"); | ||
|
||
// TODO: Get this through dependency injection | ||
private readonly ILog _log = LogManager.GetLogger(typeof(KspBuildMap)); | ||
|
||
private readonly object _buildMapLock = new object(); | ||
private JBuilds _jBuilds; | ||
|
||
private readonly IWin32Registry _registry; | ||
|
||
public KspVersion this[string buildId] | ||
{ | ||
get | ||
{ | ||
EnsureBuildMap(); | ||
|
||
string version; | ||
return _jBuilds.Builds.TryGetValue(buildId, out version) ? KspVersion.Parse(version) : null; | ||
} | ||
} | ||
|
||
public KspBuildMap(IWin32Registry registry) | ||
{ | ||
_registry = registry; | ||
} | ||
|
||
private void EnsureBuildMap() | ||
{ | ||
if (ReferenceEquals(_jBuilds, null)) | ||
{ | ||
lock(_buildMapLock) | ||
{ | ||
if (ReferenceEquals(_jBuilds, null)) | ||
{ | ||
Refresh(useCachedVersion: true); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void Refresh() | ||
{ | ||
Refresh(useCachedVersion: false); | ||
} | ||
|
||
private void Refresh(bool useCachedVersion) | ||
{ | ||
if (useCachedVersion) | ||
{ | ||
// Attempt to set the build map from the cached version in the registry | ||
if (TrySetRegistryBuildMap()) return; | ||
|
||
// Attempt to set the build map from the repository | ||
if (TrySetRemoteBuildMap()) return; | ||
} | ||
else | ||
{ | ||
// Attempt to set the build map from the repository | ||
if (TrySetRemoteBuildMap()) return; | ||
|
||
// Attempt to set the build map from the cached version in the registry | ||
if (TrySetRegistryBuildMap()) return; | ||
} | ||
|
||
// If that fails attempt to set the build map from the embedded version | ||
if (TrySetEmbeddedBuildMap()) return; | ||
|
||
_log.Warn("Could not refresh the build map from any source"); | ||
} | ||
|
||
private bool TrySetBuildMap(string buildMapJson) | ||
{ | ||
try | ||
{ | ||
_jBuilds = JsonConvert.DeserializeObject<JBuilds>(buildMapJson); | ||
return true; | ||
} | ||
catch(Exception e) | ||
{ | ||
_log.WarnFormat("Could not parse build map"); | ||
_log.DebugFormat("{0}\n{1}", buildMapJson, e); | ||
return false; | ||
} | ||
} | ||
|
||
private bool TrySetRemoteBuildMap() | ||
{ | ||
try | ||
{ | ||
var json = Net.DownloadText(BuildMapUri); | ||
|
||
if (TrySetBuildMap(json)) | ||
{ | ||
_registry.SetKSPBuilds(json); | ||
return true; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
_log.WarnFormat("Could not retrieve latest build map from: {0}", BuildMapUri); | ||
_log.Debug(e); | ||
return false; | ||
} | ||
} | ||
|
||
private bool TrySetRegistryBuildMap() | ||
{ | ||
try | ||
{ | ||
var json = _registry.GetKSPBuilds(); | ||
return json != null && TrySetBuildMap(json); | ||
} | ||
catch(Exception e) | ||
{ | ||
_log.WarnFormat("Could not retrieve build map from registry"); | ||
_log.Debug(e); | ||
return false; | ||
} | ||
} | ||
|
||
private bool TrySetEmbeddedBuildMap() | ||
{ | ||
try | ||
{ | ||
var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("CKAN.builds.json"); | ||
|
||
if (resourceStream != null) | ||
{ | ||
using (var reader = new StreamReader(resourceStream)) | ||
{ | ||
TrySetBuildMap(reader.ReadToEnd()); | ||
return true; | ||
} | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
catch(Exception e) | ||
{ | ||
_log.WarnFormat("Could not retrieve build map from embedded resource"); | ||
_log.Debug(e); | ||
return false; | ||
} | ||
} | ||
|
||
// ReSharper disable once ClassNeverInstantiated.Local | ||
private sealed class JBuilds | ||
{ | ||
[JsonProperty("builds")] | ||
// ReSharper disable once UnusedAutoPropertyAccessor.Local | ||
public Dictionary<string, string> Builds { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using CKAN.Versioning; | ||
|
||
namespace CKAN.GameVersionProviders | ||
{ | ||
// ReSharper disable once ClassNeverInstantiated.Global | ||
public sealed class KspReadmeVersionProvider : IGameVersionProvider | ||
{ | ||
private static readonly Regex VersionPattern = new Regex(@"^Version\s+(?<version>\d+\.\d+\.\d+)", | ||
RegexOptions.IgnoreCase | RegexOptions.Compiled | ||
); | ||
|
||
public bool TryGetVersion(string directory, out KspVersion result) | ||
{ | ||
var readmePath = Path.Combine(directory, "readme.txt"); | ||
|
||
if (File.Exists(readmePath)) | ||
{ | ||
var match = File | ||
.ReadAllLines(readmePath) | ||
.Select(i => VersionPattern.Match(i)) | ||
.FirstOrDefault(i => i.Success); | ||
|
||
if (match != null) | ||
{ | ||
result = KspVersion.Parse(match.Groups["version"].Value); | ||
return true; | ||
} | ||
} | ||
|
||
result = default(KspVersion); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace CKAN.GameVersionProviders | ||
{ | ||
public enum KspVersionSource | ||
{ | ||
BuildId = 1, | ||
Readme = 2 | ||
} | ||
} |
Oops, something went wrong.