#nullable enable using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; namespace Barotrauma { /// /// A serializable dictionary that can be sent over the network. /// /// The backing array of key-value pairs that gets serialized. /// Key /// Value /// /// This isn't a full implementation of a dictionary, but rather a simple wrapper around a list of key-value pairs /// that can be serialized and deserialized in an INetSerializableStruct. /// Normally there wouldn't be duplicate keys in a dictionary, but this implementation doesn't enforce that. /// [NetworkSerialize] public readonly record struct NetDictionary(ImmutableArray> Pairs) : INetSerializableStruct where T : notnull { public Dictionary ToDictionary() => Pairs.ToDictionary( static pair => pair.First, static pair => pair.Second); public ImmutableDictionary ToImmutableDictionary() => Pairs.ToImmutableDictionary( static pair => pair.First, static pair => pair.Second); } public static class DictionaryExtensions { public static NetDictionary ToNetDictionary(this Dictionary source) where T : notnull => new NetDictionary(source.Select(static pair => new NetPair(pair.Key, pair.Value)).ToImmutableArray()); public static NetDictionary ToNetDictionary(this ImmutableDictionary source) where T : notnull => new NetDictionary(source.Select(static pair => new NetPair(pair.Key, pair.Value)).ToImmutableArray()); } }