using System.Collections.Generic; namespace Robust.Shared.Serialization.Manager.Result { public sealed class DeserializedDictionary : DeserializationResult where TKey : notnull where TDict : IReadOnlyDictionary { public delegate TDict Create(Dictionary elements); public DeserializedDictionary( TDict value, IReadOnlyDictionary mappings, Create createDelegate) { Value = value; Mappings = mappings; CreateDelegate = createDelegate; } public override TDict Value { get; } public IReadOnlyDictionary Mappings { get; } public Create CreateDelegate { get; } public override object RawValue => Value; public override DeserializationResult PushInheritanceFrom(DeserializationResult source) { var sourceRes = source.Cast>(); var valueDict = new Dictionary(); var mappingDict = new Dictionary(); foreach (var (keyRes, valRes) in sourceRes.Mappings) { var newKeyRes = keyRes.Copy(); var newValueRes = valRes.Copy(); valueDict.Add((TKey)newKeyRes.RawValue!, (TValue)newValueRes.RawValue!); mappingDict.Add(newKeyRes, newValueRes); } foreach (var (keyRes, valRes) in Mappings) { var newKeyRes = keyRes.Copy(); var newValueRes = valRes.Copy(); valueDict.Add((TKey) newKeyRes.RawValue!, (TValue)newValueRes.RawValue!); mappingDict.Add(newKeyRes, newValueRes); } return new DeserializedDictionary(CreateDelegate(valueDict), mappingDict, CreateDelegate); } public override DeserializationResult Copy() { var valueDict = new Dictionary(); var mappingDict = new Dictionary(); foreach (var (keyRes, valRes) in Mappings) { var newKeyRes = keyRes.Copy(); var newValueRes = valRes.Copy(); valueDict.Add((TKey)newKeyRes.RawValue!, (TValue)newValueRes.RawValue!); mappingDict.Add(newKeyRes, newValueRes); } return new DeserializedDictionary(CreateDelegate(valueDict), mappingDict, CreateDelegate); } public override void CallAfterDeserializationHook() { foreach (var (key, val) in Mappings) { key.CallAfterDeserializationHook(); val.CallAfterDeserializationHook(); } } } }