From 8946389da418d82ee1bafa5beae9db27b7e69672 Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Wed, 1 Apr 2020 06:17:38 -0600 Subject: [PATCH] Fix upgrading test broken by Nexus JSON property name change --- Wabbajack.Common/Json.cs | 87 +++++++++++-------- .../Downloaders/DownloadDispatcher.cs | 3 +- Wabbajack.Lib/Downloaders/NexusDownloader.cs | 4 + 3 files changed, 57 insertions(+), 37 deletions(-) diff --git a/Wabbajack.Common/Json.cs b/Wabbajack.Common/Json.cs index c40a4685..2b814f0f 100644 --- a/Wabbajack.Common/Json.cs +++ b/Wabbajack.Common/Json.cs @@ -15,52 +15,53 @@ namespace Wabbajack.Common new RelativePathConverter(), new AbolutePathConverter(), new HashRelativePathConverter(), - new FullPathConverter() - + new FullPathConverter(), + new GameConverter() }; + public static void ToJSON(this T obj, string filename) { if (File.Exists(filename)) File.Delete(filename); File.WriteAllText(filename, JsonConvert.SerializeObject(obj, Formatting.Indented, - new JsonSerializerSettings { - TypeNameHandling = TypeNameHandling.Auto, - Converters = Converters})); + new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.Auto, Converters = Converters})); } - public static string ToJSON(this T obj, + public static string ToJSON(this T obj, TypeNameHandling handling = TypeNameHandling.All, TypeNameAssemblyFormatHandling format = TypeNameAssemblyFormatHandling.Full, bool prettyPrint = false) { return JsonConvert.SerializeObject(obj, Formatting.Indented, - new JsonSerializerSettings {TypeNameHandling = handling, - TypeNameAssemblyFormatHandling = format, + new JsonSerializerSettings + { + TypeNameHandling = handling, + TypeNameAssemblyFormatHandling = format, Formatting = prettyPrint ? Formatting.Indented : Formatting.None, Converters = Converters }); } - - public static T FromJSON(this AbsolutePath filename, - TypeNameHandling handling = TypeNameHandling.All, + + public static T FromJSON(this AbsolutePath filename, + TypeNameHandling handling = TypeNameHandling.All, TypeNameAssemblyFormatHandling format = TypeNameAssemblyFormatHandling.Full) { return JsonConvert.DeserializeObject(filename.ReadAllText(), - new JsonSerializerSettings {TypeNameHandling = handling, - TypeNameAssemblyFormatHandling = format, - Converters = Converters + new JsonSerializerSettings + { + TypeNameHandling = handling, TypeNameAssemblyFormatHandling = format, Converters = Converters }); } - - public static T FromJSONString(this string data, + + public static T FromJSONString(this string data, TypeNameHandling handling = TypeNameHandling.All, TypeNameAssemblyFormatHandling format = TypeNameAssemblyFormatHandling.Full) { return JsonConvert.DeserializeObject(data, - new JsonSerializerSettings {TypeNameHandling = handling, - TypeNameAssemblyFormatHandling = format, - Converters = Converters + new JsonSerializerSettings + { + TypeNameHandling = handling, TypeNameAssemblyFormatHandling = format, Converters = Converters }); } @@ -70,11 +71,7 @@ namespace Wabbajack.Common try { return JsonConvert.DeserializeObject(s, - new JsonSerializerSettings - { - TypeNameHandling = TypeNameHandling.Auto, - Converters = Converters - }); + new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.Auto, Converters = Converters}); } catch (JsonSerializationException) { @@ -85,8 +82,8 @@ namespace Wabbajack.Common throw; } } - - + + private class HashJsonConverter : JsonConverter { public override void WriteJson(JsonWriter writer, Hash value, JsonSerializer serializer) @@ -94,12 +91,13 @@ namespace Wabbajack.Common writer.WriteValue(value.ToBase64()); } - public override Hash ReadJson(JsonReader reader, Type objectType, Hash existingValue, bool hasExistingValue, JsonSerializer serializer) + public override Hash ReadJson(JsonReader reader, Type objectType, Hash existingValue, bool hasExistingValue, + JsonSerializer serializer) { return Hash.FromBase64((string)reader.Value); } } - + private class RelativePathConverter : JsonConverter { public override void WriteJson(JsonWriter writer, RelativePath value, JsonSerializer serializer) @@ -107,13 +105,14 @@ namespace Wabbajack.Common writer.WriteValue((string)value); } - public override RelativePath ReadJson(JsonReader reader, Type objectType, RelativePath existingValue, bool hasExistingValue, + public override RelativePath ReadJson(JsonReader reader, Type objectType, RelativePath existingValue, + bool hasExistingValue, JsonSerializer serializer) { return (RelativePath)(string)reader.Value; } } - + private class AbolutePathConverter : JsonConverter { public override void WriteJson(JsonWriter writer, AbsolutePath value, JsonSerializer serializer) @@ -121,13 +120,14 @@ namespace Wabbajack.Common writer.WriteValue((string)value); } - public override AbsolutePath ReadJson(JsonReader reader, Type objectType, AbsolutePath existingValue, bool hasExistingValue, + public override AbsolutePath ReadJson(JsonReader reader, Type objectType, AbsolutePath existingValue, + bool hasExistingValue, JsonSerializer serializer) { return (AbsolutePath)(string)reader.Value; } } - + private class HashRelativePathConverter : JsonConverter { public override void WriteJson(JsonWriter writer, HashRelativePath value, JsonSerializer serializer) @@ -139,7 +139,8 @@ namespace Wabbajack.Common writer.WriteEndArray(); } - public override HashRelativePath ReadJson(JsonReader reader, Type objectType, HashRelativePath existingValue, bool hasExistingValue, + public override HashRelativePath ReadJson(JsonReader reader, Type objectType, + HashRelativePath existingValue, bool hasExistingValue, JsonSerializer serializer) { if (reader.TokenType != JsonToken.StartArray) @@ -159,7 +160,7 @@ namespace Wabbajack.Common return new HashRelativePath(hash, paths.ToArray()); } } - + private class FullPathConverter : JsonConverter { public override void WriteJson(JsonWriter writer, FullPath value, JsonSerializer serializer) @@ -171,7 +172,8 @@ namespace Wabbajack.Common writer.WriteEndArray(); } - public override FullPath ReadJson(JsonReader reader, Type objectType, FullPath existingValue, bool hasExistingValue, + public override FullPath ReadJson(JsonReader reader, Type objectType, FullPath existingValue, + bool hasExistingValue, JsonSerializer serializer) { if (reader.TokenType != JsonToken.StartArray) @@ -192,6 +194,19 @@ namespace Wabbajack.Common } } - + public class GameConverter : JsonConverter + { + public override void WriteJson(JsonWriter writer, Game value, JsonSerializer serializer) + { + writer.WriteValue(Enum.GetName(typeof(Game), value)); + } + + public override Game ReadJson(JsonReader reader, Type objectType, Game existingValue, + bool hasExistingValue, + JsonSerializer serializer) + { + return GameRegistry.GetByFuzzyName((string)reader.Value).Game; + } + } } } diff --git a/Wabbajack.Lib/Downloaders/DownloadDispatcher.cs b/Wabbajack.Lib/Downloaders/DownloadDispatcher.cs index e8a14249..6123133e 100644 --- a/Wabbajack.Lib/Downloaders/DownloadDispatcher.cs +++ b/Wabbajack.Lib/Downloaders/DownloadDispatcher.cs @@ -102,7 +102,8 @@ namespace Wabbajack.Lib.Downloaders Utils.Log($"No upgrade found for {archive.Hash}"); return false; } - + Utils.Log($"Upgrading via {upgrade.State.PrimaryKeyString}"); + Utils.Log($"Upgrading {archive.Hash}"); var upgradePath = destination.Parent.Combine("_Upgrade_" + archive.Name); var upgradeResult = await Download(upgrade, upgradePath); diff --git a/Wabbajack.Lib/Downloaders/NexusDownloader.cs b/Wabbajack.Lib/Downloaders/NexusDownloader.cs index 20a3ff2b..c7b8d7ce 100644 --- a/Wabbajack.Lib/Downloaders/NexusDownloader.cs +++ b/Wabbajack.Lib/Downloaders/NexusDownloader.cs @@ -5,9 +5,11 @@ using System.Reactive.Linq; using System.Threading.Tasks; using MessagePack; using MongoDB.Bson.Serialization.Attributes; +using Newtonsoft.Json; using ReactiveUI; using Wabbajack.Common; using Wabbajack.Common.StatusFeed.Errors; +using Wabbajack.Lib.CompilationSteps; using Wabbajack.Lib.NexusApi; using Wabbajack.Lib.Validation; using Game = Wabbajack.Common.Game; @@ -146,6 +148,8 @@ namespace Wabbajack.Lib.Downloaders public string Description { get; set; } [Key(6)] + [JsonProperty("GameName")] + [JsonConverter(typeof(Utils.GameConverter))] public Game Game { get; set; } [Key(7)]