diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-11-03 00:43:41 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-11-03 00:43:41 -0400 |
commit | 59c900a9ac936491386e70d9c841e572f2a49ecd (patch) | |
tree | 49308b9c70ffb730f64d63f77f6e80c437921819 /src/StardewModdingAPI/Framework | |
parent | 5125a168c544d05db3174b2db942ac7b48b5115c (diff) | |
download | SMAPI-59c900a9ac936491386e70d9c841e572f2a49ecd.tar.gz SMAPI-59c900a9ac936491386e70d9c841e572f2a49ecd.tar.bz2 SMAPI-59c900a9ac936491386e70d9c841e572f2a49ecd.zip |
add update check (#154)
Diffstat (limited to 'src/StardewModdingAPI/Framework')
-rw-r--r-- | src/StardewModdingAPI/Framework/GitRelease.cs | 19 | ||||
-rw-r--r-- | src/StardewModdingAPI/Framework/UpdateHelper.cs | 36 |
2 files changed, 55 insertions, 0 deletions
diff --git a/src/StardewModdingAPI/Framework/GitRelease.cs b/src/StardewModdingAPI/Framework/GitRelease.cs new file mode 100644 index 00000000..0da57efd --- /dev/null +++ b/src/StardewModdingAPI/Framework/GitRelease.cs @@ -0,0 +1,19 @@ +using Newtonsoft.Json; + +namespace StardewModdingAPI.Framework +{ + /// <summary>Metadata about a GitHub release tag.</summary> + internal class GitRelease + { + /********* + ** Accessors + *********/ + /// <summary>The display name.</summary> + [JsonProperty("name")] + public string Name { get; set; } + + /// <summary>The semantic version string.</summary> + [JsonProperty("tag_name")] + public string Tag { get; set; } + } +}
\ No newline at end of file diff --git a/src/StardewModdingAPI/Framework/UpdateHelper.cs b/src/StardewModdingAPI/Framework/UpdateHelper.cs new file mode 100644 index 00000000..ddd1d840 --- /dev/null +++ b/src/StardewModdingAPI/Framework/UpdateHelper.cs @@ -0,0 +1,36 @@ +using System.IO; +using System.Net; +using System.Reflection; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace StardewModdingAPI.Framework +{ + /// <summary>Provides utility methods for mod updates.</summary> + internal class UpdateHelper + { + /********* + ** Public methods + *********/ + /// <summary>Get the latest release from a GitHub repository.</summary> + /// <param name="repository">The name of the repository from which to fetch releases (like "cjsu/SMAPI").</param> + public static async Task<GitRelease> GetLatestVersionAsync(string repository) + { + // build request + // (avoid HttpClient for Mac compatibility) + HttpWebRequest request = WebRequest.CreateHttp($"https://api.github.com/repos/{repository}/releases/latest"); + AssemblyName assembly = typeof(UpdateHelper).Assembly.GetName(); + request.UserAgent = $"{assembly.Name}/{assembly.Version}"; + request.Accept = "application/vnd.github.v3+json"; + + // fetch data + using (WebResponse response = await request.GetResponseAsync()) + using (Stream responseStream = response.GetResponseStream()) + using (StreamReader reader = new StreamReader(responseStream)) + { + string responseText = reader.ReadToEnd(); + return JsonConvert.DeserializeObject<GitRelease>(responseText); + } + } + } +} |