diff --git a/Common.csproj b/Common.csproj index 073fe9d..2e3eb0a 100644 --- a/Common.csproj +++ b/Common.csproj @@ -170,6 +170,7 @@ + @@ -219,6 +220,11 @@ + + + 11.0.2 + + diff --git a/Update/GitHubRelease.cs b/Update/GitHubRelease.cs new file mode 100644 index 0000000..14e0cc6 --- /dev/null +++ b/Update/GitHubRelease.cs @@ -0,0 +1,177 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; +using System; +using System.Collections.Generic; +using System.Globalization; + +namespace Common.Update +{ + public partial class GitHubRelease + { + [JsonProperty("url")] + public string Url { get; set; } + + [JsonProperty("assets_url")] + public string AssetsUrl { get; set; } + + [JsonProperty("upload_url")] + public string UploadUrl { get; set; } + + [JsonProperty("html_url")] + public string HtmlUrl { get; set; } + + [JsonProperty("id")] + public long Id { get; set; } + + [JsonProperty("tag_name")] + public string TagName { get; set; } + + [JsonProperty("target_commitish")] + public string TargetCommitish { get; set; } + + [JsonProperty("name")] + public string Name { get; set; } + + [JsonProperty("draft")] + public bool Draft { get; set; } + + [JsonProperty("author")] + public Uploader Author { get; set; } + + [JsonProperty("prerelease")] + public bool Prerelease { get; set; } + + [JsonProperty("created_at")] + public DateTimeOffset CreatedAt { get; set; } + + [JsonProperty("published_at")] + public DateTimeOffset PublishedAt { get; set; } + + [JsonProperty("assets")] + public List Assets { get; set; } + + [JsonProperty("tarball_url")] + public string TarballUrl { get; set; } + + [JsonProperty("zipball_url")] + public string ZipballUrl { get; set; } + + [JsonProperty("body")] + public string Body { get; set; } + } + + public class Asset + { + [JsonProperty("url")] + public string Url { get; set; } + + [JsonProperty("id")] + public long Id { get; set; } + + [JsonProperty("name")] + public string Name { get; set; } + + [JsonProperty("label")] + public string Label { get; set; } + + [JsonProperty("uploader")] + public Uploader Uploader { get; set; } + + [JsonProperty("content_type")] + public string ContentType { get; set; } + + [JsonProperty("state")] + public string State { get; set; } + + [JsonProperty("size")] + public long Size { get; set; } + + [JsonProperty("download_count")] + public long DownloadCount { get; set; } + + [JsonProperty("created_at")] + public DateTimeOffset CreatedAt { get; set; } + + [JsonProperty("updated_at")] + public DateTimeOffset UpdatedAt { get; set; } + + [JsonProperty("browser_download_url")] + public string BrowserDownloadUrl { get; set; } + } + + public class Uploader + { + [JsonProperty("login")] + public string Login { get; set; } + + [JsonProperty("id")] + public long Id { get; set; } + + [JsonProperty("avatar_url")] + public string AvatarUrl { get; set; } + + [JsonProperty("gravatar_id")] + public string GravatarId { get; set; } + + [JsonProperty("url")] + public string Url { get; set; } + + [JsonProperty("html_url")] + public string HtmlUrl { get; set; } + + [JsonProperty("followers_url")] + public string FollowersUrl { get; set; } + + [JsonProperty("following_url")] + public string FollowingUrl { get; set; } + + [JsonProperty("gists_url")] + public string GistsUrl { get; set; } + + [JsonProperty("starred_url")] + public string StarredUrl { get; set; } + + [JsonProperty("subscriptions_url")] + public string SubscriptionsUrl { get; set; } + + [JsonProperty("organizations_url")] + public string OrganizationsUrl { get; set; } + + [JsonProperty("repos_url")] + public string ReposUrl { get; set; } + + [JsonProperty("events_url")] + public string EventsUrl { get; set; } + + [JsonProperty("received_events_url")] + public string ReceivedEventsUrl { get; set; } + + [JsonProperty("type")] + public string Type { get; set; } + + [JsonProperty("site_admin")] + public bool SiteAdmin { get; set; } + } + + public partial class GitHubRelease + { + public static GitHubRelease FromJson(string json) => JsonConvert.DeserializeObject(json, Converter.Settings); + } + + public static class Serialize + { + public static string ToJson(this GitHubRelease self) => JsonConvert.SerializeObject(self, Converter.Settings); + } + + internal class Converter + { + public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings + { + MetadataPropertyHandling = MetadataPropertyHandling.Ignore, + DateParseHandling = DateParseHandling.None, + Converters = { + new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } + }, + }; + } +} diff --git a/Update/UpdateCheck.cs b/Update/UpdateCheck.cs index f718147..d0f94c0 100644 --- a/Update/UpdateCheck.cs +++ b/Update/UpdateCheck.cs @@ -8,6 +8,12 @@ using System.Threading.Tasks; namespace Common.Update { + public enum ServerType + { + Generic, + GitHub + } + public static class UpdateCheck { public delegate void ApplicationShutdownDelegate(); @@ -18,6 +24,7 @@ namespace Common.Update public static ApplicationCurrentMessageDelegate ApplicationCurrentMessage = null; public static ApplicationUpdateMessageDelegate ApplicationUpdateMessage = null; + public static ServerType UpdateServerType; public static string UpdateServer; public static string UpdateFile; public static string ApplicationName { get; set; } @@ -26,14 +33,11 @@ namespace Common.Update public static string LocalInstallFile { get; private set; } public static bool UpdateAvailable { get; private set; } - public static Version LocalVersion - { - get { return Assembly.GetEntryAssembly().GetName().Version; } - } + public static Version LocalVersion => Assembly.GetEntryAssembly().GetName().Version; public static bool CheckForUpdate() { - RemoteVersion = VersionInfo.Load(UpdateServer, UpdateFile); + RemoteVersion = VersionInfo.Load(UpdateServerType, UpdateServer, UpdateFile); if (RemoteVersion == null) return false; diff --git a/Update/VersionInfo.cs b/Update/VersionInfo.cs index e81e391..433867f 100644 --- a/Update/VersionInfo.cs +++ b/Update/VersionInfo.cs @@ -1,5 +1,6 @@ using Common.Debug; using System; +using System.Net; using System.Xml.Linq; namespace Common.Update @@ -10,7 +11,20 @@ namespace Common.Update public string InstallFile { get; set; } public DateTime InstallCreated { get; set; } - public static VersionInfo Load(string server, string file) + public static VersionInfo Load(ServerType updateServerType, string server, string file) + { + switch (updateServerType) + { + case ServerType.Generic: + return LoadFile(server, file); + case ServerType.GitHub: + return LoadGitHub(server); + } + + return null; + } + + private static VersionInfo LoadFile(string server, string file) { try { @@ -44,5 +58,38 @@ namespace Common.Update return null; } } + + private static VersionInfo LoadGitHub(string server) + { + try + { + ServicePointManager.Expect100Continue = true; + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; + + using (var webClient = new WebClient()) + { + webClient.Headers.Add("User-Agent", UpdateCheck.ApplicationName); + + var json = webClient.DownloadString(server); + + var release = GitHubRelease.FromJson(json); + + var versionInfo = new VersionInfo + { + Version = Version.Parse(release.TagName), + InstallFile = release.Assets[0].BrowserDownloadUrl, + InstallCreated = release.Assets[0].CreatedAt.LocalDateTime + }; + + return versionInfo; + } + } + catch (Exception exception) + { + Tracer.WriteException(exception); + + return null; + } + } } }