diff --git a/Server/src/SOR.Game/Abilities/AbilitySystem.cs b/Server/src/SOR.Game/Abilities/AbilitySystem.cs index 93f987d..860c51b 100644 --- a/Server/src/SOR.Game/Abilities/AbilitySystem.cs +++ b/Server/src/SOR.Game/Abilities/AbilitySystem.cs @@ -20,7 +20,14 @@ public static class AbilitySystem private static readonly JsonSerializerOptions _jsonOpts = new() { PropertyNameCaseInsensitive = true, - Converters = { new System.Text.Json.Serialization.JsonStringEnumConverter() }, + Converters = + { + new System.Text.Json.Serialization.JsonStringEnumConverter(), + // Handles polymorphic Effect deserialization when $type is not the first + // property — the default [JsonPolymorphic] behavior requires $type first + // for abstract base types. + new EffectPolymorphicConverter(), + }, }; // ── Parsing ──────────────────────────────────────────────────────────────── diff --git a/Server/src/SOR.Game/Abilities/EffectPolymorphicConverter.cs b/Server/src/SOR.Game/Abilities/EffectPolymorphicConverter.cs new file mode 100644 index 0000000..64617cf --- /dev/null +++ b/Server/src/SOR.Game/Abilities/EffectPolymorphicConverter.cs @@ -0,0 +1,98 @@ +namespace SOR.Game.Abilities; + +using System.Reflection; +using System.Text.Json; +using System.Text.Json.Serialization; +using SOR.Game.Abilities.Models; + +/// +/// Custom JSON converter for the polymorphic hierarchy. +/// +/// Problem: requires the $type discriminator to be +/// the first property in the JSON object when the base class is abstract. +/// Card-definition JSON authored in the admin tool does not guarantee that ordering +/// (e.g. {"N":1,"$type":"DrawCards","Player":"Owner"}). +/// +/// Fix: this converter intercepts only typeof(Effect) (not derived types — +/// see ), reads the entire JSON object into a +/// , finds $type regardless of position, then +/// deserializes into the correct concrete type. Concrete derived types are handled +/// by the standard serializer so there is no infinite recursion. +/// +internal sealed class EffectPolymorphicConverter : JsonConverter +{ + private static readonly Dictionary _typeMap; + private static readonly Dictionary _reverseMap; + + static EffectPolymorphicConverter() + { + _typeMap = new Dictionary(StringComparer.OrdinalIgnoreCase); + _reverseMap = new Dictionary(); + + foreach (var attr in typeof(Effect) + .GetCustomAttributes()) + { + if (attr.TypeDiscriminator is string disc) + { + _typeMap[disc] = attr.DerivedType; + _reverseMap[attr.DerivedType] = disc; + } + } + } + + /// + /// Only intercept the abstract base itself. + /// Concrete derived types (e.g. DrawCardsEffect) fall through to the + /// default serializer — this prevents infinite recursion. + /// + public override bool CanConvert(Type typeToConvert) + => typeToConvert == typeof(Effect); + + public override Effect? Read( + ref Utf8JsonReader reader, + Type typeToConvert, + JsonSerializerOptions options) + { + // Buffer the entire object so we can find $type anywhere. + using var doc = JsonDocument.ParseValue(ref reader); + var root = doc.RootElement; + + if (!root.TryGetProperty("$type", out var discriminatorProp)) + throw new JsonException("Effect object is missing the '$type' discriminator."); + + var discriminator = discriminatorProp.GetString() + ?? throw new JsonException("Effect '$type' discriminator is null."); + + if (!_typeMap.TryGetValue(discriminator, out var concreteType)) + throw new JsonException($"Unknown Effect '$type': '{discriminator}'."); + + // Deserialize as the concrete type. CanConvert returns false for concrete + // types, so this call uses the default converter — no recursion. + return (Effect?)root.Deserialize(concreteType, options); + } + + public override void Write( + Utf8JsonWriter writer, + Effect value, + JsonSerializerOptions options) + { + var concreteType = value.GetType(); + if (!_reverseMap.TryGetValue(concreteType, out var discriminator)) + throw new JsonException($"No '$type' discriminator registered for {concreteType.Name}."); + + // Serialize the concrete object first to get its properties as a JsonDocument. + // CanConvert returns false for concrete types, so no recursion. + using var mem = new System.IO.MemoryStream(); + using (var inner = new Utf8JsonWriter(mem)) + JsonSerializer.Serialize(inner, value, concreteType, options); + + using var doc = JsonDocument.Parse(mem.ToArray()); + + // Write the final object: $type first, then all concrete properties. + writer.WriteStartObject(); + writer.WriteString("$type", discriminator); + foreach (var prop in doc.RootElement.EnumerateObject()) + prop.WriteTo(writer); + writer.WriteEndObject(); + } +}