This commit is contained in:
Paul
2021-03-03 21:04:49 +01:00
parent 5711cad41e
commit 5e477bcee8
9 changed files with 46 additions and 344 deletions

View File

@@ -1,12 +1,13 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
namespace Robust.Shared.Serialization.Manager.Result
{
public class DeserializedReadOnlyCollection<TCollection, TElement> : DeserializationResult<TCollection> where TCollection : IReadOnlyCollection<TElement>
public class DeserializedCollection<TCollection, TElement> : DeserializationResult<TCollection> where TCollection : IReadOnlyCollection<TElement>
{
public delegate TCollection Create(List<TElement> elements);
public DeserializedReadOnlyCollection(
public DeserializedCollection(
TCollection? value,
IEnumerable<DeserializationResult> mappings,
Create createDelegate)
@@ -26,7 +27,7 @@ namespace Robust.Shared.Serialization.Manager.Result
public override DeserializationResult PushInheritanceFrom(DeserializationResult source)
{
var sourceCollection = source.Cast<DeserializedReadOnlyCollection<TCollection, TElement>>();
var sourceCollection = source.Cast<DeserializedCollection<TCollection, TElement>>();
var valueList = new List<TElement>();
var resList = new List<DeserializationResult>();
@@ -50,7 +51,7 @@ namespace Robust.Shared.Serialization.Manager.Result
}
}
return new DeserializedReadOnlyCollection<TCollection, TElement>(CreateDelegate(valueList), resList, CreateDelegate);
return new DeserializedCollection<TCollection, TElement>(CreateDelegate(valueList), resList, CreateDelegate);
}
public override DeserializationResult Copy()
@@ -65,7 +66,7 @@ namespace Robust.Shared.Serialization.Manager.Result
resList.Add(newRes);
}
return new DeserializedReadOnlyCollection<TCollection, TElement>(Value == null ? default : CreateDelegate(valueList), resList, CreateDelegate);
return new DeserializedCollection<TCollection, TElement>(Value == null ? default : CreateDelegate(valueList), resList, CreateDelegate);
}
public override void CallAfterDeserializationHook()

View File

@@ -1,63 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Robust.Shared.Utility;
using System.Collections.Generic;
namespace Robust.Shared.Serialization.Manager.Result
{
public class DeserializedDictionary<TDict, TKey, TValue> :
DeserializationResult<TDict>
where TKey : notnull
where TDict : IDictionary<TKey, TValue>, new()
where TDict : IReadOnlyDictionary<TKey, TValue>
{
public delegate TDict Create(Dictionary<TKey, TValue> elements);
public DeserializedDictionary(
TDict? value,
IReadOnlyDictionary<DeserializationResult, DeserializationResult> mappings)
IReadOnlyDictionary<DeserializationResult, DeserializationResult> mappings,
Create createDelegate)
{
Value = value;
Mappings = mappings;
CreateDelegate = createDelegate;
}
public override TDict? Value { get; }
public IReadOnlyDictionary<DeserializationResult, DeserializationResult> Mappings { get; }
public Create CreateDelegate { get; }
public override object? RawValue => Value;
public override DeserializationResult PushInheritanceFrom(DeserializationResult source)
{
var sourceRes = source.Cast<DeserializedDictionary<TDict, TKey, TValue>>();
var mappingDict = Mappings.ToDictionary(p => p.Key.Copy(), p => p.Value.Copy());
var valueDict = new Dictionary<TKey, TValue>();
var mappingDict = new Dictionary<DeserializationResult, DeserializationResult>();
foreach (var (keyRes, valRes) in sourceRes.Mappings)
{
var newKeyRes = keyRes.Copy();
var newValueRes = valRes.Copy();
var oldEntry = mappingDict.FirstOrNull(p => Equals(p.Key.RawValue, newKeyRes.RawValue));
if (oldEntry.HasValue)
{
newKeyRes = oldEntry.Value.Key.PushInheritanceFrom(newKeyRes);
newValueRes = oldEntry.Value.Value.PushInheritanceFrom(newValueRes);
mappingDict.Remove(oldEntry.Value.Key);
}
valueDict.Add((TKey)newKeyRes.RawValue!, (TValue)newValueRes.RawValue!);
mappingDict.Add(newKeyRes, newValueRes);
}
var valueDict = new TDict();
foreach (var (key, val) in mappingDict)
{
valueDict.Add((TKey) key.RawValue!, (TValue) val.RawValue!);
}
return new DeserializedDictionary<TDict, TKey, TValue>(valueDict, mappingDict);
}
public override DeserializationResult Copy()
{
var valueDict = new TDict();
var mappingDict = new Dictionary<DeserializationResult, DeserializationResult>();
foreach (var (keyRes, valRes) in Mappings)
{
var newKeyRes = keyRes.Copy();
@@ -67,7 +51,24 @@ namespace Robust.Shared.Serialization.Manager.Result
mappingDict.Add(newKeyRes, newValueRes);
}
return new DeserializedDictionary<TDict, TKey, TValue>(valueDict, mappingDict);
return new DeserializedDictionary<TDict, TKey, TValue>(CreateDelegate(valueDict), mappingDict, CreateDelegate);
}
public override DeserializationResult Copy()
{
var valueDict = new Dictionary<TKey, TValue>();
var mappingDict = new Dictionary<DeserializationResult, DeserializationResult>();
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<TDict, TKey, TValue>(CreateDelegate(valueDict), mappingDict, CreateDelegate);
}
public override void CallAfterDeserializationHook()

View File

@@ -1,72 +0,0 @@
using System.Collections.Generic;
using System.Collections.Immutable;
namespace Robust.Shared.Serialization.Manager.Result
{
public class DeserializedImmutableList<T> : DeserializationResult<ImmutableList<T>>
{
public DeserializedImmutableList(ImmutableList<T>? value, IEnumerable<DeserializationResult> mappings)
{
Value = value;
Mappings = mappings;
}
public override ImmutableList<T>? Value { get; }
public IEnumerable<DeserializationResult> Mappings { get; }
public override object? RawValue => Value;
public override DeserializationResult PushInheritanceFrom(DeserializationResult source)
{
var sourceCollection = source.Cast<DeserializedImmutableSet<T>>();
var valueList = ImmutableList.CreateBuilder<T>();
var resList = new List<DeserializationResult>();
if (sourceCollection.Value != null)
{
foreach (var oldRes in sourceCollection.Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<T>>();
valueList.Add(newRes.Value!);
resList.Add(newRes);
}
}
if (Value != null)
{
foreach (var oldRes in Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<T>>();
valueList.Add(newRes.Value);
resList.Add(newRes);
}
}
return new DeserializedImmutableList<T>(valueList.ToImmutable(), resList);
}
public override DeserializationResult Copy()
{
var valueList = ImmutableList.CreateBuilder<T>();
var resList = new List<DeserializationResult>();
foreach (var oldRes in Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<T>>();
valueList.Add(newRes.Value!);
resList.Add(newRes);
}
return new DeserializedImmutableList<T>(Value == null ? null : valueList.ToImmutable(), resList);
}
public override void CallAfterDeserializationHook()
{
foreach (var elem in Mappings)
{
elem.CallAfterDeserializationHook();
}
}
}
}

View File

@@ -1,73 +0,0 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
namespace Robust.Shared.Serialization.Manager.Result
{
public class DeserializedImmutableSet<T> : DeserializationResult<ImmutableHashSet<T>>
{
public DeserializedImmutableSet(ImmutableHashSet<T>? value, IEnumerable<DeserializationResult> mappings)
{
Value = value;
Mappings = mappings;
}
public override ImmutableHashSet<T>? Value { get; }
public IEnumerable<DeserializationResult> Mappings { get; }
public override object? RawValue => Value;
public override DeserializationResult PushInheritanceFrom(DeserializationResult source)
{
var sourceCollection = source.Cast<DeserializedImmutableSet<T>>();
var valueSet = ImmutableHashSet.CreateBuilder<T>();
var resList = new List<DeserializationResult>();
if (sourceCollection.Value != null)
{
foreach (var oldRes in sourceCollection.Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<T>>();
valueSet.Add(newRes.Value!);
resList.Add(newRes);
}
}
if (Value != null)
{
foreach (var oldRes in Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<T>>();
valueSet.Add(newRes.Value!);
resList.Add(newRes);
}
}
return new DeserializedImmutableSet<T>(valueSet.ToImmutable(), resList);
}
public override DeserializationResult Copy()
{
var valueSet = ImmutableHashSet.CreateBuilder<T>();
var resList = new List<DeserializationResult>();
foreach (var oldRes in Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<T>>();
valueSet.Add(newRes.Value!);
resList.Add(newRes);
}
return new DeserializedImmutableSet<T>(Value == null ? null : valueSet.ToImmutable(), resList);
}
public override void CallAfterDeserializationHook()
{
foreach (var val in Mappings)
{
val.CallAfterDeserializationHook();
}
}
}
}

View File

@@ -1,72 +0,0 @@
using System;
using System.Collections.Generic;
namespace Robust.Shared.Serialization.Manager.Result
{
public class DeserializedMutableCollection<TCollection, TElement> : DeserializationResult<TCollection>
where TCollection : class, ICollection<TElement>, new()
{
public DeserializedMutableCollection(TCollection? value, IEnumerable<DeserializationResult> mappings)
{
Value = value;
Mappings = mappings;
}
public override TCollection? Value { get; }
public IEnumerable<DeserializationResult> Mappings { get; }
public override object? RawValue => Value;
public override DeserializationResult PushInheritanceFrom(DeserializationResult source)
{
var sourceCollection = source.Cast<DeserializedMutableCollection<TCollection, TElement>>();
var valueList = new TCollection();
var resList = new List<DeserializationResult>();
if (sourceCollection.Value != null)
{
foreach (var oldRes in sourceCollection.Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<TElement>>();
valueList.Add(newRes.Value!);
resList.Add(newRes);
}
}
if (Value != null)
{
foreach (var oldRes in Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<TElement>>();
valueList.Add(newRes.Value!);
resList.Add(newRes);
}
}
return new DeserializedMutableCollection<TCollection, TElement>(valueList, resList);
}
public override DeserializationResult Copy()
{
var valueList = new TCollection();
var resList = new List<DeserializationResult>();
foreach (var oldRes in Mappings)
{
var newRes = oldRes.Copy();
valueList.Add((TElement) newRes.RawValue!);
resList.Add(newRes);
}
return new DeserializedMutableCollection<TCollection, TElement>(Value == null ? null : valueList, resList);
}
public override void CallAfterDeserializationHook()
{
foreach (var val in Mappings)
{
val.CallAfterDeserializationHook();
}
}
}
}

View File

@@ -1,83 +0,0 @@
using System.Collections.Generic;
namespace Robust.Shared.Serialization.Manager.Result
{
public class DeserializedReadOnlyDictionary<TDict, TKey, TValue> :
DeserializationResult<TDict>
where TKey : notnull
where TDict : IReadOnlyDictionary<TKey, TValue>
{
public delegate TDict Create(Dictionary<TKey, TValue> elements);
public DeserializedReadOnlyDictionary(
TDict? value,
IReadOnlyDictionary<DeserializationResult, DeserializationResult> mappings,
Create createDelegate)
{
Value = value;
Mappings = mappings;
CreateDelegate = createDelegate;
}
public override TDict? Value { get; }
public IReadOnlyDictionary<DeserializationResult, DeserializationResult> Mappings { get; }
public Create CreateDelegate { get; }
public override object? RawValue => Value;
public override DeserializationResult PushInheritanceFrom(DeserializationResult source)
{
var sourceRes = source.Cast<DeserializedReadOnlyDictionary<TDict, TKey, TValue>>();
var valueDict = new Dictionary<TKey, TValue>();
var mappingDict = new Dictionary<DeserializationResult, DeserializationResult>();
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 DeserializedReadOnlyDictionary<TDict, TKey, TValue>(CreateDelegate(valueDict), mappingDict, CreateDelegate);
}
public override DeserializationResult Copy()
{
var valueDict = new Dictionary<TKey, TValue>();
var mappingDict = new Dictionary<DeserializationResult, DeserializationResult>();
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 DeserializedReadOnlyDictionary<TDict, TKey, TValue>(CreateDelegate(valueDict), mappingDict, CreateDelegate);
}
public override void CallAfterDeserializationHook()
{
foreach (var (key, val) in Mappings)
{
key.CallAfterDeserializationHook();
val.CallAfterDeserializationHook();
}
}
}
}

View File

@@ -49,7 +49,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
mappedFields.Add(keyResult, valueResult);
}
return new DeserializedReadOnlyDictionary<Dictionary<TKey, TValue>, TKey, TValue>(dict, mappedFields, dictInstance => dictInstance);
return new DeserializedDictionary<Dictionary<TKey, TValue>, TKey, TValue>(dict, mappedFields, dictInstance => dictInstance);
}
ValidatedNode ITypeReader<SortedDictionary<TKey, TValue>, MappingDataNode>.Validate(
@@ -120,7 +120,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
mappedFields.Add(keyResult, valueResult);
}
return new DeserializedReadOnlyDictionary<IReadOnlyDictionary<TKey, TValue>, TKey, TValue>(dict, mappedFields, dictInstance => dictInstance);
return new DeserializedDictionary<IReadOnlyDictionary<TKey, TValue>, TKey, TValue>(dict, mappedFields, dictInstance => dictInstance);
}
DeserializationResult
@@ -140,7 +140,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
mappedFields.Add(keyResult, valueResult);
}
return new DeserializedReadOnlyDictionary<SortedDictionary<TKey, TValue>, TKey, TValue>(dict, mappedFields, dictInstance => new SortedDictionary<TKey, TValue>(dictInstance));
return new DeserializedDictionary<SortedDictionary<TKey, TValue>, TKey, TValue>(dict, mappedFields, dictInstance => new SortedDictionary<TKey, TValue>(dictInstance));
}
[MustUseReturnValue]

View File

@@ -32,7 +32,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
mappings.Add(result);
}
return new DeserializedMutableCollection<HashSet<T>, T>(set, mappings);
return new DeserializedCollection<HashSet<T>, T>(set, mappings, elements => new HashSet<T>(elements));
}
ValidatedNode ITypeReader<ImmutableHashSet<T>, SequenceDataNode>.Validate(
@@ -96,7 +96,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
mappings.Add(result);
}
return new DeserializedImmutableSet<T>(set.ToImmutable(), mappings);
return new DeserializedCollection<ImmutableHashSet<T>, T>(set.ToImmutable(), mappings, elements => ImmutableHashSet.Create(elements.ToArray()));
}
[MustUseReturnValue]

View File

@@ -71,7 +71,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
results.Add(result);
}
return new DeserializedMutableCollection<List<T>, T>(list, results);
return new DeserializedCollection<List<T>, T>(list, results, elements => elements);
}
ValidatedNode ITypeReader<ImmutableList<T>, SequenceDataNode>.Validate(
@@ -127,7 +127,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
results.Add(result);
}
return new DeserializedReadOnlyCollection<IReadOnlyList<T>, T>(list, results, l => l);
return new DeserializedCollection<IReadOnlyList<T>, T>(list, results, l => l);
}
DeserializationResult ITypeReader<IReadOnlyCollection<T>, SequenceDataNode>.Read(
@@ -144,7 +144,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
results.Add(result);
}
return new DeserializedReadOnlyCollection<IReadOnlyCollection<T>, T>(list, results, l => l);
return new DeserializedCollection<IReadOnlyCollection<T>, T>(list, results, l => l);
}
DeserializationResult ITypeReader<ImmutableList<T>, SequenceDataNode>.Read(
@@ -161,7 +161,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
results.Add(result);
}
return new DeserializedImmutableList<T>(list.ToImmutable(), results);
return new DeserializedCollection<ImmutableList<T>,T>(list.ToImmutable(), results, elements => ImmutableList.Create(elements.ToArray()));
}
[MustUseReturnValue]