From a99b1dba3dc1d987975e0353c9be071ac5adfcb3 Mon Sep 17 00:00:00 2001 From: Rynchodon Date: Sat, 29 Oct 2016 19:15:49 -0400 Subject: [PATCH] release notes, progress --- Injector/ArmsUpdater.cs | 4 +-- Injector/GitHubClient.cs | 66 ++++++++++++++++++++++++++++------------ Injector/Release.cs | 4 +-- 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/Injector/ArmsUpdater.cs b/Injector/ArmsUpdater.cs index c2fac7c..f4bfaf1 100644 --- a/Injector/ArmsUpdater.cs +++ b/Injector/ArmsUpdater.cs @@ -33,7 +33,7 @@ public static void UpdateArms() if (bestRelease == null) { - WriteLine("No releases found"); + WriteLine("ERROR: No releases found"); return; } @@ -47,7 +47,7 @@ public static void UpdateArms() if (asset == null) { - WriteLine("Could not get asset"); + WriteLine("ERROR: Could not get asset"); return; } diff --git a/Injector/GitHubClient.cs b/Injector/GitHubClient.cs index 0f76db0..c538b84 100644 --- a/Injector/GitHubClient.cs +++ b/Injector/GitHubClient.cs @@ -2,6 +2,7 @@ using System.IO; using System.Net; using System.Runtime.Serialization.Json; +using System.Threading.Tasks; namespace Rynchodon { @@ -15,27 +16,53 @@ public static void CreateRelease(string userAgent, string oAuthToken, Release re request.Method = "POST"; request.Headers.Add("Authorization", "token " + oAuthToken); - release.WriteCreateJson(request.GetRequestStream()); + using (Stream requestStream = request.GetRequestStream()) + release.WriteCreateJson(requestStream); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Release)); - release = (Release)serializer.ReadObject(request.GetResponse().GetResponseStream()); + using (WebResponse response = request.GetResponse()) + using (Stream responseStream = response.GetResponseStream()) + release = (Release)serializer.ReadObject(responseStream); Console.WriteLine("Release id: " + release.id); foreach (string asset in assetsPaths) { string fileName = Path.GetFileName(asset); - Console.WriteLine("Posting asset: " + fileName); - request = WebRequest.CreateHttp(@"https://uploads.github.com/repos/Rynchodon/ARMS/releases/" + release.id + "/assets?name=" + fileName); - request.UserAgent = userAgent; - request.Method = "POST"; - request.ContentType = "application/dll"; - request.Headers.Add("Authorization", "token " + oAuthToken); - - FileStream fileStream = new FileStream(asset, FileMode.Open); - fileStream.CopyTo(request.GetRequestStream()); - request.GetResponse(); - - Console.WriteLine("Posted: " + fileName); + Console.Write("Posting asset " + fileName + ": "); + + using (WebClient client = new WebClient()) + { + client.Headers.Add(HttpRequestHeader.UserAgent, userAgent); + client.Headers.Add(HttpRequestHeader.ContentType, "application/dll"); + client.Headers.Add(HttpRequestHeader.Authorization, "token " + oAuthToken); + + int cursorLeft = Console.CursorLeft, cursorTop = Console.CursorTop; + long lastPercent = -1L; + object locker = new object(); + UploadProgressChangedEventHandler handler = (sender, e) => { + long percent = e.BytesSent * 100L / e.TotalBytesToSend; + lock (locker) + { + if (percent == lastPercent) + return; + lastPercent = percent; + Console.SetCursorPosition(cursorLeft, cursorTop); + if (percent < 10) + Console.Write(' '); + if (percent < 100) + Console.Write(' '); + Console.Write(percent); + Console.Write('%'); + } + }; + + client.UploadProgressChanged += handler; + Task uploadTask = client.UploadFileTaskAsync(@"https://uploads.github.com/repos/Rynchodon/ARMS/releases/" + release.id + "/assets?name=" + fileName, asset); + uploadTask.Wait(); + uploadTask.Dispose(); + } + + Console.WriteLine(); } } @@ -43,11 +70,12 @@ public static Release[] GetReleases(string userAgent) { HttpWebRequest request = WebRequest.CreateHttp(@"https://api.github.com/repos/Rynchodon/ARMS/releases"); request.UserAgent = userAgent; - WebResponse response; - response = request.GetResponse(); - - DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Release[])); - return (Release[])serializer.ReadObject(response.GetResponseStream()); + using (WebResponse response = request.GetResponse()) + { + DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Release[])); + using (Stream responseStream = response.GetResponseStream()) + return (Release[])serializer.ReadObject(responseStream); + } } } diff --git a/Injector/Release.cs b/Injector/Release.cs index 7b77fb8..b4d8dcf 100644 --- a/Injector/Release.cs +++ b/Injector/Release.cs @@ -21,7 +21,7 @@ public class Asset private class Create { [DataMember] - public string tag_name; + public string tag_name, body; [DataMember] public bool draft, prerelease; } @@ -59,7 +59,7 @@ public int CompareTo(Release other) public void WriteCreateJson(Stream writeTo) { - Create c = new Create() { tag_name = tag_name, draft = draft, prerelease = prerelease }; + Create c = new Create() { tag_name = tag_name, body = body, draft = draft, prerelease = prerelease }; DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Create)); serializer.WriteObject(writeTo, c); }