From 9be88e76ef3ea2841ff6248b547b0a4d1e01f7cd Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 8 Jan 2025 23:22:23 +0000 Subject: [PATCH] more stuff --- Core/Models/Eft/Common/Tables/BotBase.cs | 3 +- Core/Models/Eft/Hideout/QteData.cs | 4 +- Core/Utils/ImporterUtil.cs | 2 +- .../Converters/DictionaryOrListConverter.cs | 55 +++++++++++++++++++ Core/Utils/Json/DictionaryOrList.cs | 10 ++++ 5 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 Core/Utils/Json/Converters/DictionaryOrListConverter.cs create mode 100644 Core/Utils/Json/DictionaryOrList.cs diff --git a/Core/Models/Eft/Common/Tables/BotBase.cs b/Core/Models/Eft/Common/Tables/BotBase.cs index 6d2789cd..8e6bd87d 100644 --- a/Core/Models/Eft/Common/Tables/BotBase.cs +++ b/Core/Models/Eft/Common/Tables/BotBase.cs @@ -308,8 +308,7 @@ public class BaseJsonSkills public class Skills { - [JsonConverter(typeof(DictionaryOfListOrTConverter))] - public Dictionary>? Common { get; set; } + public DictionaryOrList? Common { get; set; } [JsonConverter(typeof(ArrayToObjectFactoryConverter))] public Dictionary? Mastering { get; set; } diff --git a/Core/Models/Eft/Hideout/QteData.cs b/Core/Models/Eft/Hideout/QteData.cs index 90ed9c94..2cdb8931 100644 --- a/Core/Models/Eft/Hideout/QteData.cs +++ b/Core/Models/Eft/Hideout/QteData.cs @@ -1,3 +1,4 @@ +using System.Runtime.CompilerServices; using System.Text.Json.Serialization; using Core.Models.Eft.Health; using Core.Models.Enums; @@ -166,6 +167,7 @@ public class SkillRequirement : QteRequirement public RequirementType? Type { get; set; } = Models.Enums.Hideout.RequirementType.Skill; [JsonPropertyName("skillName")] + [JsonConverter(typeof(JsonStringEnumConverter))] public SkillTypes? SkillName { get; set; } [JsonPropertyName("skillLevel")] @@ -254,4 +256,4 @@ public class BodyPartBuffRequirement : QteRequirement [JsonPropertyName("excluded")] public bool? Excluded { get; set; } -} \ No newline at end of file +} diff --git a/Core/Utils/ImporterUtil.cs b/Core/Utils/ImporterUtil.cs index 80bdf70b..b257b7e3 100644 --- a/Core/Utils/ImporterUtil.cs +++ b/Core/Utils/ImporterUtil.cs @@ -15,7 +15,7 @@ public class ImporterUtil private readonly JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions { - UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow, Converters = { new ListOrTConverterFactory() } + UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow, Converters = { new ListOrTConverterFactory(), new DictionaryOrListConverter() } }; public ImporterUtil(FileUtil fileUtil) diff --git a/Core/Utils/Json/Converters/DictionaryOrListConverter.cs b/Core/Utils/Json/Converters/DictionaryOrListConverter.cs new file mode 100644 index 00000000..c6913deb --- /dev/null +++ b/Core/Utils/Json/Converters/DictionaryOrListConverter.cs @@ -0,0 +1,55 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Core.Utils.Json.Converters; + +public class DictionaryOrListConverter: JsonConverterFactory +{ + public override bool CanConvert(Type typeToConvert) + { + return typeToConvert.IsGenericType && typeToConvert.GetGenericTypeDefinition() == typeof(DictionaryOrList<,>); + } + + public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) + { + return (JsonConverter)Activator.CreateInstance(typeof(DictionaryOrListConverter<,>).MakeGenericType(typeToConvert.GenericTypeArguments[0], typeToConvert.GenericTypeArguments[1])); + } +} + +public class DictionaryOrListConverter : JsonConverter?> +{ + public override DictionaryOrList? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + switch (reader.TokenType) + { + case JsonTokenType.StartArray: + using (var jsonDocument = JsonDocument.ParseValue(ref reader)) + { + var jsonText = jsonDocument.RootElement.GetRawText(); + var list = JsonSerializer.Deserialize>(jsonText, options); + return new DictionaryOrList(null, list); + } + case JsonTokenType.StartObject: + using (var jsonDocument = JsonDocument.ParseValue(ref reader)) + { + var jsonText = jsonDocument.RootElement.GetRawText(); + var dictionary = JsonSerializer.Deserialize>(jsonText, options); + return new DictionaryOrList(dictionary, null); + } + default: + throw new Exception($"Unable to translate object type {reader.TokenType} to ListOrT."); + } + } + + public override void Write(Utf8JsonWriter writer, DictionaryOrList value, JsonSerializerOptions options) + { + if (value.IsList) + { + JsonSerializer.Serialize(writer, value.List, options); + } + else + { + JsonSerializer.Serialize(writer, value.Dictionary, options); + } + } +} diff --git a/Core/Utils/Json/DictionaryOrList.cs b/Core/Utils/Json/DictionaryOrList.cs new file mode 100644 index 00000000..bdcb6ceb --- /dev/null +++ b/Core/Utils/Json/DictionaryOrList.cs @@ -0,0 +1,10 @@ +namespace Core.Utils.Json; + +public class DictionaryOrList(Dictionary? dictionary, List? list) +{ + public Dictionary? Dictionary { get; } = dictionary; + public List? List { get; } = list; + + public bool IsList => List != null; + public bool IsDictionary => Dictionary != null; +}