From 2624d962375b7f0a2f27a86292764a0a04a89877 Mon Sep 17 00:00:00 2001 From: Zemill <33404722+Zemill@users.noreply.github.com> Date: Tue, 3 Jan 2023 22:46:01 -0500 Subject: [PATCH] Fixed issue with bad json casting, switched to object --- Heroesprofile.Uploader.Common/UploadResult.cs | 51 +++++++++++++++++++ Heroesprofile.Uploader.Common/Uploader.cs | 15 ++---- .../Properties/AssemblyInfo.cs | 4 +- .../SquirrelRelease.txt | 2 +- 4 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 Heroesprofile.Uploader.Common/UploadResult.cs diff --git a/Heroesprofile.Uploader.Common/UploadResult.cs b/Heroesprofile.Uploader.Common/UploadResult.cs new file mode 100644 index 0000000..61b5e51 --- /dev/null +++ b/Heroesprofile.Uploader.Common/UploadResult.cs @@ -0,0 +1,51 @@ +// +// +// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do: +// +// using Heroesprofile.Uploader.Common; +// +// var uploadResult = UploadResult.FromJson(jsonString); + +namespace Heroesprofile.Uploader.Common +{ + using System; + using System.Collections.Generic; + + using System.Globalization; + using Newtonsoft.Json; + using Newtonsoft.Json.Converters; + + public partial class UploadResult + { + [JsonProperty("fingerprint")] + public Guid Fingerprint { get; set; } + + [JsonProperty("replayID")] + public int ReplayId { get; set; } + + [JsonProperty("status")] + public string Status { get; set; } + } + + public partial class UploadResult + { + public static UploadResult FromJson(string json) => JsonConvert.DeserializeObject(json, Heroesprofile.Uploader.Common.Converter.Settings); + } + + public static class Serialize + { + public static string ToJson(this UploadResult self) => JsonConvert.SerializeObject(self, Heroesprofile.Uploader.Common.Converter.Settings); + } + + internal static class Converter + { + public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings { + MetadataPropertyHandling = MetadataPropertyHandling.Ignore, + DateParseHandling = DateParseHandling.None, + Converters = + { + new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } + }, + }; + } +} diff --git a/Heroesprofile.Uploader.Common/Uploader.cs b/Heroesprofile.Uploader.Common/Uploader.cs index 84633e9..b1e591f 100644 --- a/Heroesprofile.Uploader.Common/Uploader.cs +++ b/Heroesprofile.Uploader.Common/Uploader.cs @@ -65,15 +65,10 @@ public async Task Upload(Replay replay_results, string fingerprint response = Encoding.UTF8.GetString(bytes); } - dynamic json = JObject.Parse(response); + UploadResult result = UploadResult.FromJson(response); try { - int replayID = 0; - - if (json.replayID != null) { - replayID = json.replayID; - } - + int replayID = result.ReplayId; if (File.GetLastWriteTime(file) >= DateTime.Now.Subtract(TimeSpan.FromMinutes(60)) && PostMatchPage && replayID != 0) { await postMatchAnalysis(replayID); } @@ -83,12 +78,12 @@ public async Task Upload(Replay replay_results, string fingerprint } - if ((bool)json.success) { - if (Enum.TryParse((string)json.status, out UploadStatus status)) { + if (!string.IsNullOrEmpty(result.Status)) { + if (Enum.TryParse((string)result.Status, out UploadStatus status)) { _log.Debug($"Uploaded file '{file}': {status}"); return status; } else { - _log.Error($"Unknown upload status '{file}': {json.status}"); + _log.Error($"Unknown upload status '{file}': {result.Status}"); return UploadStatus.UploadError; } } else { diff --git a/Heroesprofile.Uploader.Windows/Properties/AssemblyInfo.cs b/Heroesprofile.Uploader.Windows/Properties/AssemblyInfo.cs index 7d82af4..e473917 100644 --- a/Heroesprofile.Uploader.Windows/Properties/AssemblyInfo.cs +++ b/Heroesprofile.Uploader.Windows/Properties/AssemblyInfo.cs @@ -52,6 +52,6 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.3.0")] -[assembly: AssemblyFileVersion("2.3.0")] +[assembly: AssemblyVersion("2.3.1")] +[assembly: AssemblyFileVersion("2.3.1")] [assembly: AssemblyInformationalVersion("1.0.0")] diff --git a/Heroesprofile.Uploader.Windows/SquirrelRelease.txt b/Heroesprofile.Uploader.Windows/SquirrelRelease.txt index 1f7aff4..da8b497 100644 --- a/Heroesprofile.Uploader.Windows/SquirrelRelease.txt +++ b/Heroesprofile.Uploader.Windows/SquirrelRelease.txt @@ -8,4 +8,4 @@ 5: CD to Installer directory -6: Squirrel --releasify=Heroesprofile.Uploader.2.3.0.nupkg --no-msi --setupIcon=uploader_icon_light.ico \ No newline at end of file +6: Squirrel --releasify=Heroesprofile.Uploader.2.3.1.nupkg --no-msi --setupIcon=uploader_icon_light.ico \ No newline at end of file