Skip to content
This repository has been archived by the owner on Nov 8, 2020. It is now read-only.

Commit

Permalink
NetKAN: Always use default_version_id to locate latest release.
Browse files Browse the repository at this point in the history
- Includes tests.
- Closes #214.
- Thanks to @Starstrider42 for spotting this.
  • Loading branch information
pjf committed Oct 31, 2014
1 parent 7c934b5 commit 8cb7b54
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
11 changes: 11 additions & 0 deletions KS/KSAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,20 @@ public static string Call(string path)
return web.DownloadString(url);
}

/// <summary>
/// Given a mod id, returns a KSMod with its metadata from the network.
/// </summary>
public static KSMod Mod(int mod_id)
{
string json = Call("/mod/" + mod_id);
return Mod(json);
}

/// <summary>
/// Given a JSON string, inflates and returns a KSMod.
/// </summary>
public static KSMod Mod(string json)
{
return JsonConvert.DeserializeObject<KSMod>(json);
}

Expand Down
17 changes: 17 additions & 0 deletions KS/KSMod.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.IO;
using log4net;
using Newtonsoft.Json.Linq;
Expand All @@ -17,6 +18,7 @@ public class KSMod : CkanInflator
public string author;
public KSVersion[] versions;
public string website;
public int default_version_id;

public override string ToString()
{
Expand Down Expand Up @@ -68,6 +70,21 @@ override public void InflateMetadata(JObject metadata, string filename, object c
Inflate((JObject) metadata["resources"]["kerbalstuff"], "url", KSHome());
}

internal KSVersion Latest()
{
// The version we want is specified by `default_version_id`, it's not just
// the latest. See GH #214. Thanks to @Starstrider42 for spotting this.

var latest =
from release in this.versions
where release.id == this.default_version_id
select release
;

// There should only ever be one.
return latest.First();
}

internal string KSHome()
{
Uri path = KSAPI.ExpandPath(String.Format("/mod/{0}/{1}", id, name));
Expand Down
8 changes: 5 additions & 3 deletions MainClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,18 @@ public static int Main(string[] args)
/// Fetch le things from le KerbalStuff.
/// Returns a JObject that should be a fully formed CKAN file.
/// </summary>
private static JObject KerbalStuff(JObject orig_metadata, string remote_id, Cache cache)
internal static JObject KerbalStuff(JObject orig_metadata, string remote_id, Cache cache)
{
// Look up our mod on KS by its ID.
KSMod ks = KSAPI.Mod(Convert.ToInt32(remote_id));
Version version = ks.versions[0].friendly_version;

KSVersion latest = ks.Latest();

Version version = latest.friendly_version;

log.DebugFormat("Mod: {0} {1}", ks.name, version);

// Find the latest download.
KSVersion latest = ks.versions[0];
string filename = latest.Download((string) orig_metadata["identifier"], cache);

JObject metadata = MetadataFromFileOrDefault(filename, orig_metadata);
Expand Down

0 comments on commit 8cb7b54

Please sign in to comment.