using IPA.Logging; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; namespace IPA.JsonConverters { internal class FeaturesFieldConverter : JsonConverter>> { [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void Assert([DoesNotReturnIf(false)] bool condition) { if (!condition) throw new InvalidOperationException(); } public override Dictionary> ReadJson(JsonReader reader, Type objectType, Dictionary> existingValue, bool hasExistingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.StartArray) { _ = serializer.Deserialize(reader); Logger.Features.Warn("Encountered old features used. They no longer do anything, please move to the new format."); return existingValue; } var dict = new Dictionary>(); Assert(reader.TokenType == JsonToken.StartObject && reader.Read()); while (reader.TokenType == JsonToken.PropertyName) { var name = (string)reader.Value; Assert(reader.Read()); var list = reader.TokenType == JsonToken.StartObject ? (new() { serializer.Deserialize(reader) }) : serializer.Deserialize>(reader); dict.Add(name, list); Assert(reader.Read()); } return dict; } public override void WriteJson(JsonWriter writer, Dictionary> value, JsonSerializer serializer) { serializer.Serialize(writer, value); } } }