mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
Add skip hook argument to readers and copiers
This commit is contained in:
@@ -14,6 +14,7 @@ namespace Robust.Client.Serialization
|
||||
public class AppearanceVisualizerSerializer : ITypeSerializer<AppearanceVisualizer, MappingDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager, MappingDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
if (!node.TryGetNode("type", out var typeNode))
|
||||
@@ -30,7 +31,7 @@ namespace Robust.Client.Serialization
|
||||
|
||||
var newNode = (MappingDataNode)node.Copy();
|
||||
newNode.RemoveNode("type");
|
||||
return serializationManager.Read(type, newNode, context);
|
||||
return serializationManager.Read(type, newNode, context, skipHook);
|
||||
}
|
||||
|
||||
public ValidatedNode Validate(ISerializationManager serializationManager, MappingDataNode node,
|
||||
@@ -61,7 +62,7 @@ namespace Robust.Client.Serialization
|
||||
}
|
||||
|
||||
public AppearanceVisualizer Copy(ISerializationManager serializationManager, AppearanceVisualizer source,
|
||||
AppearanceVisualizer target, ISerializationContext? context = null)
|
||||
AppearanceVisualizer target, bool skipHook, ISerializationContext? context = null)
|
||||
{
|
||||
return serializationManager.Copy(source, target, context)!;
|
||||
}
|
||||
|
||||
@@ -788,7 +788,8 @@ namespace Robust.Server.Maps
|
||||
|
||||
if (CurrentReadingEntityComponents.TryGetValue(componentName, out var mapping))
|
||||
{
|
||||
var mapData = (IDeserializedDefinition)serializationManager.Read(factory.GetRegistration(componentName).Type,
|
||||
var mapData = (IDeserializedDefinition) serializationManager.Read(
|
||||
factory.GetRegistration(componentName).Type,
|
||||
mapping.ToDataNode(), this);
|
||||
var newData = serializationManager.PopulateDataDefinition(data, mapData);
|
||||
data = (IComponent) newData.RawValue!;
|
||||
@@ -826,6 +827,7 @@ namespace Robust.Server.Maps
|
||||
}
|
||||
|
||||
public DeserializationResult Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
if (node.Value == "null") return new DeserializedValue<GridId>(GridId.Invalid);
|
||||
@@ -933,6 +935,7 @@ namespace Robust.Server.Maps
|
||||
|
||||
DeserializationResult ITypeReader<EntityUid, ValueDataNode>.Read(ISerializationManager serializationManager,
|
||||
ValueDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context)
|
||||
{
|
||||
if (node.Value == "null")
|
||||
@@ -955,6 +958,7 @@ namespace Robust.Server.Maps
|
||||
|
||||
DeserializationResult ITypeReader<IEntity, ValueDataNode>.Read(ISerializationManager serializationManager,
|
||||
ValueDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context)
|
||||
{
|
||||
var val = int.Parse(node.Value);
|
||||
@@ -972,6 +976,7 @@ namespace Robust.Server.Maps
|
||||
|
||||
[MustUseReturnValue]
|
||||
public GridId Copy(ISerializationManager serializationManager, GridId source, GridId target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new(source.Value);
|
||||
@@ -979,6 +984,7 @@ namespace Robust.Server.Maps
|
||||
|
||||
[MustUseReturnValue]
|
||||
public EntityUid Copy(ISerializationManager serializationManager, EntityUid source, EntityUid target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new((int) source);
|
||||
|
||||
@@ -10,10 +10,8 @@ using Robust.Shared.Log;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Serialization.Manager;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Serialization.Markdown;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
using YamlDotNet.RepresentationModel;
|
||||
|
||||
namespace Robust.Shared.Prototypes
|
||||
{
|
||||
@@ -319,39 +317,6 @@ namespace Robust.Shared.Prototypes
|
||||
IoCManager.Resolve<ISerializationManager>().Copy(data, component, context);
|
||||
}
|
||||
|
||||
private void ReadComponent(YamlMappingNode mapping, IComponentFactory factory)
|
||||
{
|
||||
string type = mapping.GetNode("type").AsString();
|
||||
// See if type exists to detect errors.
|
||||
switch (factory.GetComponentAvailability(type))
|
||||
{
|
||||
case ComponentAvailability.Available:
|
||||
break;
|
||||
|
||||
case ComponentAvailability.Ignore:
|
||||
return;
|
||||
|
||||
case ComponentAvailability.Unknown:
|
||||
Log.Logger.Error($"Unknown component '{type}' in prototype {ID}!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Has this type already been added?
|
||||
if (Components.Keys.Contains(type))
|
||||
{
|
||||
Log.Logger.Error($"Component of type '{type}' defined twice in prototype {ID}!");
|
||||
return;
|
||||
}
|
||||
|
||||
var copy = new YamlMappingNode(mapping.AsEnumerable());
|
||||
// TODO: figure out a better way to exclude the type node.
|
||||
// Also maybe deep copy this? Right now it's pretty error prone.
|
||||
copy.Children.Remove(new YamlScalarNode("type"));
|
||||
var compType = factory.GetRegistration(type).Type;
|
||||
|
||||
Components[type] = IoCManager.Resolve<ISerializationManager>().ReadValueOrThrow<IComponent>(compType, copy.ToDataNode());
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"EntityPrototype({ID})";
|
||||
|
||||
@@ -459,7 +459,7 @@ namespace Robust.Shared.Prototypes
|
||||
}
|
||||
|
||||
var prototypeType = prototypeTypes[type];
|
||||
var res = _serializationManager.Read(prototypeType, node.ToDataNode());
|
||||
var res = _serializationManager.Read(prototypeType, node.ToDataNode(), skipHook: true);
|
||||
var prototype = (IPrototype) res.RawValue!;
|
||||
|
||||
if (!overwrite && prototypes[prototypeType].ContainsKey(prototype.ID))
|
||||
|
||||
@@ -33,6 +33,18 @@ namespace Robust.Shared.Serialization.Manager
|
||||
/// </returns>
|
||||
ValidatedNode ValidateNode(Type type, DataNode node, ISerializationContext? context = null);
|
||||
|
||||
/// <summary>
|
||||
/// Validates that a node has all the properties required by a certain type with its serializer.
|
||||
/// </summary>
|
||||
/// <param name="type">The type to check for.</param>
|
||||
/// <param name="node">The node to check.</param>
|
||||
/// <param name="context">The context to use, if any.</param>
|
||||
/// <returns>
|
||||
/// A node with whether or not <see cref="node"/> is valid and which of its fields
|
||||
/// are invalid, if any.
|
||||
/// </returns>
|
||||
ValidatedNode ValidateNode<T>(DataNode node, ISerializationContext? context = null);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a deserialization result from a generic type and its fields,
|
||||
/// populating the object.
|
||||
@@ -40,7 +52,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
/// <param name="fields">The fields to use for deserialization.</param>
|
||||
/// <typeparam name="T">The type to populate.</typeparam>
|
||||
/// <returns>A result with the populated type.</returns>
|
||||
DeserializationResult CreateDataDefinition<T>(DeserializedFieldEntry[] fields) where T : notnull, new();
|
||||
DeserializationResult CreateDataDefinition<T>(DeserializedFieldEntry[] fields, bool skipHook = false) where T : notnull, new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a deserialization result from a generic type and its definition,
|
||||
@@ -50,7 +62,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
/// <param name="definition">The data to use for deserialization.</param>
|
||||
/// <typeparam name="T">The type of <see cref="obj"/> to populate.</typeparam>
|
||||
/// <returns>A result with the populated object.</returns>
|
||||
DeserializationResult PopulateDataDefinition<T>(T obj, DeserializedDefinition<T> definition) where T : notnull, new();
|
||||
DeserializationResult PopulateDataDefinition<T>(T obj, DeserializedDefinition<T> definition, bool skipHook = false) where T : notnull, new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a deserialization result from an object and its definition,
|
||||
@@ -59,7 +71,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
/// <param name="obj">The object to populate.</param>
|
||||
/// <param name="definition">The data to use for deserialization.</param>
|
||||
/// <returns>A result with the populated object.</returns>
|
||||
DeserializationResult PopulateDataDefinition(object obj, IDeserializedDefinition definition);
|
||||
DeserializationResult PopulateDataDefinition(object obj, IDeserializedDefinition definition, bool skipHook = false);
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes a node into an object, populating it.
|
||||
@@ -67,8 +79,9 @@ namespace Robust.Shared.Serialization.Manager
|
||||
/// <param name="type">The type of object to populate.</param>
|
||||
/// <param name="node">The node to deserialize.</param>
|
||||
/// <param name="context">The context to use, if any.</param>
|
||||
/// <param name="skipHook">Whether or not to skip running <see cref="ISerializationHooks"/></param>
|
||||
/// <returns>A result with the deserialized object.</returns>
|
||||
DeserializationResult Read(Type type, DataNode node, ISerializationContext? context = null);
|
||||
DeserializationResult Read(Type type, DataNode node, ISerializationContext? context = null, bool skipHook = false);
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes a node into an object, populating it.
|
||||
@@ -76,8 +89,9 @@ namespace Robust.Shared.Serialization.Manager
|
||||
/// <param name="type">The type of object to deserialize into.</param>
|
||||
/// <param name="node">The node to deserialize.</param>
|
||||
/// <param name="context">The context to use, if any.</param>
|
||||
/// <param name="skipHook">Whether or not to skip running <see cref="ISerializationHooks"/></param>
|
||||
/// <returns>The deserialized object or null.</returns>
|
||||
public object? ReadValue(Type type, DataNode node, ISerializationContext? context = null);
|
||||
public object? ReadValue(Type type, DataNode node, ISerializationContext? context = null, bool skipHook = false);
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes a node into an object of the given <see cref="type"/>,
|
||||
@@ -86,18 +100,20 @@ namespace Robust.Shared.Serialization.Manager
|
||||
/// <param name="type">The type of object to deserialize into.</param>
|
||||
/// <param name="node">The node to deserialize.</param>
|
||||
/// <param name="context">The context to use, if any.</param>
|
||||
/// <param name="skipHook">Whether or not to skip running <see cref="ISerializationHooks"/></param>
|
||||
/// <typeparam name="T">The generic type to cast the resulting object to.</typeparam>
|
||||
/// <returns>The deserialized casted object, or null.</returns>
|
||||
T? ReadValueCast<T>(Type type, DataNode node, ISerializationContext? context = null);
|
||||
T? ReadValueCast<T>(Type type, DataNode node, ISerializationContext? context = null, bool skipHook = false);
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes a node into a populated object of the given generic type <see cref="T"/>
|
||||
/// </summary>
|
||||
/// <param name="node">The node to deserialize.</param>
|
||||
/// <param name="context">The context to use, if any.</param>
|
||||
/// <param name="skipHook">Whether or not to skip running <see cref="ISerializationHooks"/></param>
|
||||
/// <typeparam name="T">The type of object to create and populate.</typeparam>
|
||||
/// <returns>The deserialized object, or null.</returns>
|
||||
T? ReadValue<T>(DataNode node, ISerializationContext? context = null);
|
||||
T? ReadValue<T>(DataNode node, ISerializationContext? context = null, bool skipHook = false);
|
||||
|
||||
/// <summary>
|
||||
/// Serializes a value into a node.
|
||||
@@ -142,7 +158,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
/// This object is not necessarily the same instance as the one passed
|
||||
/// as <see cref="target"/>.
|
||||
/// </returns>
|
||||
object? Copy(object? source, object? target, ISerializationContext? context = null);
|
||||
object? Copy(object? source, object? target, ISerializationContext? context = null, bool skipHook = false);
|
||||
|
||||
/// <summary>
|
||||
/// Copies the values of one object into another.
|
||||
@@ -158,7 +174,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
/// This object is not necessarily the same instance as the one passed
|
||||
/// as <see cref="target"/>.
|
||||
/// </returns>
|
||||
T? Copy<T>(T? source, T? target, ISerializationContext? context = null);
|
||||
T? Copy<T>(T? source, T? target, ISerializationContext? context = null, bool skipHook = false);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a copy of the given object.
|
||||
@@ -166,7 +182,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
/// <param name="source">The object to copy.</param>
|
||||
/// <param name="context">The context to use, if any.</param>
|
||||
/// <returns>A copy of the given object.</returns>
|
||||
object? CreateCopy(object? source, ISerializationContext? context = null);
|
||||
object? CreateCopy(object? source, ISerializationContext? context = null, bool skipHook = false);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a copy of the given object.
|
||||
@@ -175,7 +191,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
/// <param name="context">The context to use, if any.</param>
|
||||
/// <typeparam name="T">The type of the object to copy.</typeparam>
|
||||
/// <returns>A copy of the given object.</returns>
|
||||
T? CreateCopy<T>(T? source, ISerializationContext? context = null);
|
||||
T? CreateCopy<T>(T? source, ISerializationContext? context = null, bool skipHook = false);
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
{
|
||||
[MustUseReturnValue]
|
||||
TType Copy(ISerializationManager serializationManager, TType source, TType target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,10 @@ namespace Robust.Shared.Serialization.Manager
|
||||
{
|
||||
public interface ITypeReader<TType, TNode> where TType : notnull where TNode : DataNode
|
||||
{
|
||||
DeserializationResult Read(ISerializationManager serializationManager, TNode node,
|
||||
DeserializationResult Read(
|
||||
ISerializationManager serializationManager,
|
||||
TNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null);
|
||||
|
||||
ValidatedNode Validate(ISerializationManager serializationManager, TNode node,
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
public class SerializationDataDefinition
|
||||
{
|
||||
private delegate DeserializedFieldEntry[] DeserializeDelegate(MappingDataNode mappingDataNode,
|
||||
ISerializationManager serializationManager, ISerializationContext? context);
|
||||
ISerializationManager serializationManager, ISerializationContext? context, bool skipHook);
|
||||
|
||||
private delegate DeserializationResult PopulateDelegateSignature(object target, DeserializedFieldEntry[] deserializationResults, object?[] defaultValues);
|
||||
|
||||
@@ -44,9 +44,9 @@ namespace Robust.Shared.Serialization.Manager
|
||||
_populateDelegate(target, fields, _defaultValues);
|
||||
|
||||
public DeserializationResult InvokePopulateDelegate(object target, MappingDataNode mappingDataNode, ISerializationManager serializationManager,
|
||||
ISerializationContext? context)
|
||||
ISerializationContext? context, bool skipHook)
|
||||
{
|
||||
var fields = _deserializeDelegate(mappingDataNode, serializationManager, context);
|
||||
var fields = _deserializeDelegate(mappingDataNode, serializationManager, context, skipHook);
|
||||
return _populateDelegate(target, fields, _defaultValues);
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
private DeserializeDelegate EmitDeserializationDelegate()
|
||||
{
|
||||
DeserializedFieldEntry[] DeserializationDelegate(MappingDataNode mappingDataNode,
|
||||
ISerializationManager serializationManager, ISerializationContext? serializationContext)
|
||||
ISerializationManager serializationManager, ISerializationContext? serializationContext, bool skipHook)
|
||||
{
|
||||
var mappedInfo = new DeserializedFieldEntry[_baseFieldDefinitions.Length];
|
||||
|
||||
@@ -204,7 +204,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
{
|
||||
var type = fieldDefinition.FieldType;
|
||||
var node = mappingDataNode.GetNode(fieldDefinition.Attribute.Tag);
|
||||
result = serializationManager.Read(type, node, serializationContext);
|
||||
result = serializationManager.Read(type, node, serializationContext, skipHook);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,29 +186,41 @@ namespace Robust.Shared.Serialization.Manager
|
||||
return new ErrorNode(node);
|
||||
}
|
||||
|
||||
public DeserializationResult CreateDataDefinition<T>(DeserializedFieldEntry[] fields) where T : notnull, new()
|
||||
public ValidatedNode ValidateNode<T>(DataNode node, ISerializationContext? context = null)
|
||||
{
|
||||
return ValidateNode(typeof(T), node, context);
|
||||
}
|
||||
|
||||
public DeserializationResult CreateDataDefinition<T>(DeserializedFieldEntry[] fields, bool skipHook = false)
|
||||
where T : notnull, new()
|
||||
{
|
||||
var obj = new T();
|
||||
return PopulateDataDefinition(obj, new DeserializedDefinition<T>(obj, fields));
|
||||
return PopulateDataDefinition(obj, new DeserializedDefinition<T>(obj, fields), skipHook);
|
||||
}
|
||||
|
||||
public DeserializationResult PopulateDataDefinition<T>(T obj, DeserializedDefinition<T> definition) where T : notnull, new()
|
||||
public DeserializationResult PopulateDataDefinition<T>(T obj, DeserializedDefinition<T> definition, bool skipHook = false)
|
||||
where T : notnull, new()
|
||||
{
|
||||
return PopulateDataDefinition(obj, (IDeserializedDefinition) definition);
|
||||
return PopulateDataDefinition(obj, (IDeserializedDefinition) definition, skipHook);
|
||||
}
|
||||
|
||||
public DeserializationResult PopulateDataDefinition(object obj, IDeserializedDefinition definition)
|
||||
public DeserializationResult PopulateDataDefinition(object obj, IDeserializedDefinition definition, bool skipHook = false)
|
||||
{
|
||||
if (!TryGetDataDefinition(obj.GetType(), out var dataDefinition))
|
||||
throw new ArgumentException($"Provided Type is not a data definition ({obj.GetType()})");
|
||||
|
||||
if (obj is IPopulateDefaultValues populateDefaultValues)
|
||||
if (!skipHook && obj is IPopulateDefaultValues populateDefaultValues)
|
||||
{
|
||||
populateDefaultValues.PopulateDefaultValues();
|
||||
}
|
||||
|
||||
var res = dataDefinition.InvokePopulateDelegate(obj, definition.Mapping);
|
||||
if (res.RawValue is ISerializationHooks serializationHooksAfter) serializationHooksAfter.AfterDeserialization();
|
||||
|
||||
if (!skipHook && res.RawValue is ISerializationHooks serializationHooksAfter)
|
||||
{
|
||||
serializationHooksAfter.AfterDeserialization();
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -225,7 +237,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
return dataDefinition != null;
|
||||
}
|
||||
|
||||
public DeserializationResult Read(Type type, DataNode node, ISerializationContext? context = null)
|
||||
public DeserializationResult Read(Type type, DataNode node, ISerializationContext? context = null, bool skipHook = false)
|
||||
{
|
||||
var underlyingType = type.EnsureNotNullableType();
|
||||
|
||||
@@ -247,7 +259,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
var idx = 0;
|
||||
foreach (var entryNode in sequenceDataNode.Sequence)
|
||||
{
|
||||
var value = Read(type.GetElementType()!, entryNode, context);
|
||||
var value = Read(type.GetElementType()!, entryNode, context, skipHook);
|
||||
results.Add(value);
|
||||
newArray.SetValue(value.RawValue, idx++);
|
||||
}
|
||||
@@ -271,7 +283,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
underlyingType = ResolveConcreteType(underlyingType, typeString);
|
||||
}
|
||||
|
||||
if (TryReadWithTypeSerializers(underlyingType, node, out var serializedObj, context))
|
||||
if (TryReadWithTypeSerializers(underlyingType, node, out var serializedObj, skipHook, context))
|
||||
{
|
||||
return serializedObj;
|
||||
}
|
||||
@@ -312,9 +324,9 @@ namespace Robust.Shared.Serialization.Manager
|
||||
mappingDataNode = new MappingDataNode(); //if we get an emptyValueDataNode we just use an empty mapping
|
||||
}
|
||||
|
||||
var res = dataDef.InvokePopulateDelegate(obj, mappingDataNode, this, context);
|
||||
var res = dataDef.InvokePopulateDelegate(obj, mappingDataNode, this, context, skipHook);
|
||||
|
||||
if (res.RawValue is ISerializationHooks serHooks)
|
||||
if (!skipHook && res.RawValue is ISerializationHooks serHooks)
|
||||
{
|
||||
serHooks.AfterDeserialization();
|
||||
}
|
||||
@@ -322,14 +334,14 @@ namespace Robust.Shared.Serialization.Manager
|
||||
return res;
|
||||
}
|
||||
|
||||
public object? ReadValue(Type type, DataNode node, ISerializationContext? context = null)
|
||||
public object? ReadValue(Type type, DataNode node, ISerializationContext? context = null, bool skipHook = false)
|
||||
{
|
||||
return Read(type, node, context).RawValue;
|
||||
return Read(type, node, context, skipHook).RawValue;
|
||||
}
|
||||
|
||||
public T? ReadValueCast<T>(Type type, DataNode node, ISerializationContext? context = null)
|
||||
public T? ReadValueCast<T>(Type type, DataNode node, ISerializationContext? context = null, bool skipHook = false)
|
||||
{
|
||||
var value = Read(type, node, context);
|
||||
var value = Read(type, node, context, skipHook);
|
||||
|
||||
if (value.RawValue == null)
|
||||
{
|
||||
@@ -339,9 +351,9 @@ namespace Robust.Shared.Serialization.Manager
|
||||
return (T?) value.RawValue;
|
||||
}
|
||||
|
||||
public T? ReadValue<T>(DataNode node, ISerializationContext? context = null)
|
||||
public T? ReadValue<T>(DataNode node, ISerializationContext? context = null, bool skipHook = false)
|
||||
{
|
||||
return ReadValueCast<T>(typeof(T), node, context);
|
||||
return ReadValueCast<T>(typeof(T), node, context, skipHook);
|
||||
}
|
||||
|
||||
public DataNode WriteValue<T>(T value, bool alwaysWrite = false,
|
||||
@@ -420,7 +432,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
return mapping;
|
||||
}
|
||||
|
||||
private object? CopyToTarget(object? source, object? target, ISerializationContext? context = null)
|
||||
private object? CopyToTarget(object? source, object? target, ISerializationContext? context = null, bool skipHook = false)
|
||||
{
|
||||
if (source == null || target == null)
|
||||
{
|
||||
@@ -439,8 +451,10 @@ namespace Robust.Shared.Serialization.Manager
|
||||
}
|
||||
|
||||
if (source.GetType().IsPrimitive != target.GetType().IsPrimitive)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Source and target do not match. Source ({sourceType}) is primitive type? {sourceType.IsPrimitive}. Target ({targetType}) is primitive type? {targetType.IsPrimitive}");
|
||||
}
|
||||
|
||||
if (sourceType.IsValueType && targetType.IsValueType)
|
||||
{
|
||||
@@ -448,8 +462,10 @@ namespace Robust.Shared.Serialization.Manager
|
||||
}
|
||||
|
||||
if (source.GetType().IsValueType != target.GetType().IsValueType)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Source and target do not match. Source ({sourceType}) is value type? {sourceType.IsValueType}. Target ({targetType}) is value type? {targetType.IsValueType}");
|
||||
}
|
||||
|
||||
// array
|
||||
if (sourceType.IsArray && targetType.IsArray)
|
||||
@@ -463,11 +479,13 @@ namespace Robust.Shared.Serialization.Manager
|
||||
}
|
||||
|
||||
if (source.GetType().IsArray != target.GetType().IsArray)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Source and target do not match. Source ({sourceType}) is array type? {sourceType.IsArray}. Target ({targetType}) is array type? {targetType.IsArray}");
|
||||
}
|
||||
|
||||
var commonType = TypeHelpers.SelectCommonType(source.GetType(), target.GetType());
|
||||
if(commonType == null)
|
||||
if (commonType == null)
|
||||
{
|
||||
throw new InvalidOperationException("Could not find common type in Copy!");
|
||||
}
|
||||
@@ -477,12 +495,15 @@ namespace Robust.Shared.Serialization.Manager
|
||||
return source;
|
||||
}
|
||||
|
||||
if (TryCopyWithTypeCopier(commonType, source, ref target, context))
|
||||
if (TryCopyWithTypeCopier(commonType, source, ref target, skipHook, context))
|
||||
{
|
||||
return target;
|
||||
}
|
||||
|
||||
if(target is ISerializationHooks beforeHooks) beforeHooks.BeforeSerialization();
|
||||
if (target is ISerializationHooks beforeHooks)
|
||||
{
|
||||
beforeHooks.BeforeSerialization();
|
||||
}
|
||||
|
||||
if (!TryGetDataDefinition(commonType, out var dataDef))
|
||||
{
|
||||
@@ -491,19 +512,22 @@ namespace Robust.Shared.Serialization.Manager
|
||||
|
||||
target = dataDef.InvokeCopyDelegate(source, target, this, context);
|
||||
|
||||
if(target is ISerializationHooks afterHooks) afterHooks.AfterDeserialization();
|
||||
if (!skipHook && target is ISerializationHooks afterHooks)
|
||||
{
|
||||
afterHooks.AfterDeserialization();
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
public object? Copy(object? source, object? target, ISerializationContext? context = null)
|
||||
public object? Copy(object? source, object? target, ISerializationContext? context = null, bool skipHook = false)
|
||||
{
|
||||
return CopyToTarget(source, target, context);
|
||||
return CopyToTarget(source, target, context, skipHook);
|
||||
}
|
||||
|
||||
public T? Copy<T>(T? source, T? target, ISerializationContext? context = null)
|
||||
public T? Copy<T>(T? source, T? target, ISerializationContext? context = null, bool skipHook = false)
|
||||
{
|
||||
var copy = CopyToTarget(source, target, context);
|
||||
var copy = CopyToTarget(source, target, context, skipHook);
|
||||
|
||||
if (copy == null)
|
||||
{
|
||||
@@ -513,7 +537,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
return (T?) copy;
|
||||
}
|
||||
|
||||
private object? CreateCopyInternal(object? source, ISerializationContext? context = null)
|
||||
private object? CreateCopyInternal(object? source, ISerializationContext? context = null, bool skipHook = false)
|
||||
{
|
||||
if (source == null) return source;
|
||||
|
||||
@@ -526,17 +550,17 @@ namespace Robust.Shared.Serialization.Manager
|
||||
|
||||
//todo paul checks here
|
||||
var target = Activator.CreateInstance(source.GetType())!;
|
||||
return Copy(source, target, context);
|
||||
return Copy(source, target, context, skipHook);
|
||||
}
|
||||
|
||||
public object? CreateCopy(object? source, ISerializationContext? context = null)
|
||||
public object? CreateCopy(object? source, ISerializationContext? context = null, bool skipHook = false)
|
||||
{
|
||||
return CreateCopyInternal(source, context);
|
||||
return CreateCopyInternal(source, context, skipHook);
|
||||
}
|
||||
|
||||
public T? CreateCopy<T>(T? source, ISerializationContext? context = null)
|
||||
public T? CreateCopy<T>(T? source, ISerializationContext? context = null, bool skipHook = false)
|
||||
{
|
||||
var copy = CreateCopyInternal(source, context);
|
||||
var copy = CreateCopyInternal(source, context, skipHook);
|
||||
|
||||
if (copy == null)
|
||||
{
|
||||
|
||||
@@ -10,44 +10,49 @@ namespace Robust.Shared.Serialization.Manager
|
||||
public static T ReadValueOrThrow<T>(
|
||||
this ISerializationManager manager,
|
||||
DataNode node,
|
||||
ISerializationContext? context = null)
|
||||
ISerializationContext? context = null,
|
||||
bool skipHook = false)
|
||||
{
|
||||
return manager.ReadValue<T>(node, context) ?? throw new NullReferenceException();
|
||||
return manager.ReadValue<T>(node, context, skipHook) ?? throw new NullReferenceException();
|
||||
}
|
||||
|
||||
public static T ReadValueOrThrow<T>(
|
||||
this ISerializationManager manager,
|
||||
Type type,
|
||||
DataNode node,
|
||||
ISerializationContext? context = null)
|
||||
ISerializationContext? context = null,
|
||||
bool skipHook = false)
|
||||
{
|
||||
return manager.ReadValueCast<T>(type, node, context) ?? throw new NullReferenceException();
|
||||
return manager.ReadValueCast<T>(type, node, context, skipHook) ?? throw new NullReferenceException();
|
||||
}
|
||||
|
||||
public static object ReadValueOrThrow(
|
||||
this ISerializationManager manager,
|
||||
Type type,
|
||||
DataNode node,
|
||||
ISerializationContext? context = null)
|
||||
ISerializationContext? context = null,
|
||||
bool skipHook = false)
|
||||
{
|
||||
return manager.ReadValue(type, node, context) ?? throw new NullReferenceException();
|
||||
return manager.ReadValue(type, node, context, skipHook) ?? throw new NullReferenceException();
|
||||
}
|
||||
|
||||
public static (DeserializationResult result, object? value) ReadWithValue(
|
||||
this ISerializationManager manager,
|
||||
Type type, DataNode node,
|
||||
ISerializationContext? context = null)
|
||||
ISerializationContext? context = null,
|
||||
bool skipHook = false)
|
||||
{
|
||||
var result = manager.Read(type, node, context);
|
||||
var result = manager.Read(type, node, context, skipHook);
|
||||
return (result, result.RawValue);
|
||||
}
|
||||
|
||||
public static (DeserializationResult result, T? value) ReadWithValue<T>(
|
||||
this ISerializationManager manager,
|
||||
DataNode node,
|
||||
ISerializationContext? context = null)
|
||||
ISerializationContext? context = null,
|
||||
bool skipHook = false)
|
||||
{
|
||||
var result = manager.Read(typeof(T), node, context);
|
||||
var result = manager.Read(typeof(T), node, context, skipHook);
|
||||
|
||||
if (result.RawValue == null)
|
||||
{
|
||||
@@ -61,9 +66,10 @@ namespace Robust.Shared.Serialization.Manager
|
||||
this ISerializationManager manager,
|
||||
Type type,
|
||||
DataNode node,
|
||||
ISerializationContext? context = null)
|
||||
ISerializationContext? context = null,
|
||||
bool skipHook = false)
|
||||
{
|
||||
var result = manager.Read(type, node, context);
|
||||
var result = manager.Read(type, node, context, skipHook);
|
||||
|
||||
if (result.RawValue == null)
|
||||
{
|
||||
@@ -77,9 +83,10 @@ namespace Robust.Shared.Serialization.Manager
|
||||
public static (T value, DeserializationResult result) ReadWithValueOrThrow<T>(
|
||||
this ISerializationManager manager,
|
||||
DataNode node,
|
||||
ISerializationContext? context = null)
|
||||
ISerializationContext? context = null,
|
||||
bool skipHook = false)
|
||||
{
|
||||
var result = manager.Read(typeof(T), node, context);
|
||||
var result = manager.Read(typeof(T), node, context, skipHook);
|
||||
|
||||
if (result.RawValue == null)
|
||||
{
|
||||
@@ -93,9 +100,10 @@ namespace Robust.Shared.Serialization.Manager
|
||||
this ISerializationManager manager,
|
||||
Type type,
|
||||
DataNode node,
|
||||
ISerializationContext? context = null)
|
||||
ISerializationContext? context = null,
|
||||
bool skipHook = false)
|
||||
{
|
||||
var result = manager.Read(type, node, context);
|
||||
var result = manager.Read(type, node, context, skipHook);
|
||||
|
||||
if (result.RawValue == null)
|
||||
{
|
||||
|
||||
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Serialization.Manager.Result;
|
||||
@@ -30,7 +29,6 @@ namespace Robust.Shared.Serialization.Manager
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Paul register copiers
|
||||
private object? RegisterSerializer(Type type)
|
||||
{
|
||||
var writerInterfaces = type.GetInterfaces()
|
||||
@@ -50,19 +48,19 @@ namespace Robust.Shared.Serialization.Manager
|
||||
{
|
||||
foreach (var writerInterface in writerInterfaces)
|
||||
{
|
||||
if(!_genericWriterTypes.TryAdd(writerInterface.GetGenericArguments()[0], type))
|
||||
if (!_genericWriterTypes.TryAdd(writerInterface.GetGenericArguments()[0], type))
|
||||
Logger.Error($"Tried registering generic writer for type {writerInterface.GetGenericArguments()[0]} twice");
|
||||
}
|
||||
|
||||
foreach (var readerInterface in readerInterfaces)
|
||||
{
|
||||
if(!_genericReaderTypes.TryAdd((readerInterface.GetGenericArguments()[0], readerInterface.GetGenericArguments()[1]), type))
|
||||
if (!_genericReaderTypes.TryAdd((readerInterface.GetGenericArguments()[0], readerInterface.GetGenericArguments()[1]), type))
|
||||
Logger.Error($"Tried registering generic reader for type {readerInterface.GetGenericArguments()[0]} and datanode {readerInterface.GetGenericArguments()[1]} twice");
|
||||
}
|
||||
|
||||
foreach (var copierInterface in copierInterfaces)
|
||||
{
|
||||
if(!_genericCopierTypes.TryAdd(copierInterface.GetGenericArguments()[0], type))
|
||||
if (!_genericCopierTypes.TryAdd(copierInterface.GetGenericArguments()[0], type))
|
||||
Logger.Error($"Tried registering generic copier for type {copierInterface.GetGenericArguments()[0]} twice");
|
||||
}
|
||||
|
||||
@@ -74,19 +72,19 @@ namespace Robust.Shared.Serialization.Manager
|
||||
|
||||
foreach (var writerInterface in writerInterfaces)
|
||||
{
|
||||
if(!_typeWriters.TryAdd(writerInterface.GetGenericArguments()[0], serializer))
|
||||
if (!_typeWriters.TryAdd(writerInterface.GetGenericArguments()[0], serializer))
|
||||
Logger.Error($"Tried registering writer for type {writerInterface.GetGenericArguments()[0]} twice");
|
||||
}
|
||||
|
||||
foreach (var readerInterface in readerInterfaces)
|
||||
{
|
||||
if(!_typeReaders.TryAdd((readerInterface.GetGenericArguments()[0], readerInterface.GetGenericArguments()[1]), serializer))
|
||||
if (!_typeReaders.TryAdd((readerInterface.GetGenericArguments()[0], readerInterface.GetGenericArguments()[1]), serializer))
|
||||
Logger.Error($"Tried registering reader for type {readerInterface.GetGenericArguments()[0]} and datanode {readerInterface.GetGenericArguments()[1]} twice");
|
||||
}
|
||||
|
||||
foreach (var copierInterface in copierInterfaces)
|
||||
{
|
||||
if(!_typeCopiers.TryAdd(copierInterface.GetGenericArguments()[0], serializer))
|
||||
if (!_typeCopiers.TryAdd(copierInterface.GetGenericArguments()[0], serializer))
|
||||
Logger.Error($"Tried registering copier for type {copierInterface.GetGenericArguments()[0]} twice");
|
||||
}
|
||||
|
||||
@@ -98,10 +96,13 @@ namespace Robust.Shared.Serialization.Manager
|
||||
where TNode : DataNode where T : notnull
|
||||
{
|
||||
rawReader = null;
|
||||
|
||||
if (typeof(T).IsGenericType)
|
||||
{
|
||||
var typeDef = typeof(T).GetGenericTypeDefinition();
|
||||
|
||||
Type? serializerTypeDef = null;
|
||||
|
||||
foreach (var (key, val) in _genericReaderTypes)
|
||||
{
|
||||
if (typeDef.HasSameMetadataDefinitionAs(key.Type) && key.DataNodeType.IsAssignableFrom(typeof(TNode)))
|
||||
@@ -110,9 +111,12 @@ namespace Robust.Shared.Serialization.Manager
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (serializerTypeDef == null) return false;
|
||||
|
||||
var serializerType = serializerTypeDef.MakeGenericType(typeof(T).GetGenericArguments());
|
||||
rawReader = (ITypeReader<T, TNode>)RegisterSerializer(serializerType)!;
|
||||
rawReader = (ITypeReader<T, TNode>) RegisterSerializer(serializerType)!;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -122,10 +126,13 @@ namespace Robust.Shared.Serialization.Manager
|
||||
private bool TryGetGenericWriter<T>([NotNullWhen(true)] out ITypeWriter<T>? rawWriter) where T : notnull
|
||||
{
|
||||
rawWriter = null;
|
||||
|
||||
if (typeof(T).IsGenericType)
|
||||
{
|
||||
var typeDef = typeof(T).GetGenericTypeDefinition();
|
||||
|
||||
Type? serializerTypeDef = null;
|
||||
|
||||
foreach (var (key, val) in _genericWriterTypes)
|
||||
{
|
||||
if (typeDef.HasSameMetadataDefinitionAs(key))
|
||||
@@ -134,9 +141,12 @@ namespace Robust.Shared.Serialization.Manager
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (serializerTypeDef == null) return false;
|
||||
|
||||
var serializerType = serializerTypeDef.MakeGenericType(typeof(T).GetGenericArguments());
|
||||
rawWriter = (ITypeWriter<T>)RegisterSerializer(serializerType)!;
|
||||
rawWriter = (ITypeWriter<T>) RegisterSerializer(serializerType)!;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -146,10 +156,13 @@ namespace Robust.Shared.Serialization.Manager
|
||||
private bool TryGetGenericCopier<T>([NotNullWhen(true)] out ITypeCopier<T>? rawCopier) where T : notnull
|
||||
{
|
||||
rawCopier = null;
|
||||
|
||||
if (typeof(T).IsGenericType)
|
||||
{
|
||||
var typeDef = typeof(T).GetGenericTypeDefinition();
|
||||
|
||||
Type? serializerTypeDef = null;
|
||||
|
||||
foreach (var (key, val) in _genericCopierTypes)
|
||||
{
|
||||
if (typeDef.HasSameMetadataDefinitionAs(key))
|
||||
@@ -158,29 +171,38 @@ namespace Robust.Shared.Serialization.Manager
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (serializerTypeDef == null) return false;
|
||||
|
||||
var serializerType = serializerTypeDef.MakeGenericType(typeof(T).GetGenericArguments());
|
||||
rawCopier = (ITypeCopier<T>)RegisterSerializer(serializerType)!;
|
||||
rawCopier = (ITypeCopier<T>) RegisterSerializer(serializerType)!;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryReadWithTypeSerializers(Type type, DataNode node, [NotNullWhen(true)] out DeserializationResult? obj, ISerializationContext? context = null)
|
||||
private bool TryReadWithTypeSerializers(
|
||||
Type type,
|
||||
DataNode node,
|
||||
[NotNullWhen(true)] out DeserializationResult? obj,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
//TODO Paul: do this shit w/ delegates
|
||||
var method = typeof(SerializationManager).GetRuntimeMethods().First(m =>
|
||||
m.Name == nameof(TryReadWithTypeSerializers) && m.GetParameters().Length == 3).MakeGenericMethod(type, node.GetType());
|
||||
var method = typeof(SerializationManager).GetRuntimeMethods()
|
||||
.First(m => m.Name == nameof(TryReadWithTypeSerializers) && m.GetParameters().Length == 4)
|
||||
.MakeGenericMethod(type, node.GetType());
|
||||
|
||||
obj = null;
|
||||
|
||||
var arr = new object?[] {node, obj, context};
|
||||
var arr = new object?[] {node, obj, context, skipHook};
|
||||
var res = method.Invoke(this, arr);
|
||||
|
||||
if (res as bool? ?? false)
|
||||
{
|
||||
obj = (DeserializationResult?)arr[1];
|
||||
obj = (DeserializationResult?) arr[1];
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -206,7 +228,12 @@ namespace Robust.Shared.Serialization.Manager
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryValidateWithTypeReader<T, TNode>(TNode node, ISerializationContext? context, [NotNullWhen(true)] out ValidatedNode? valid) where T : notnull where TNode : DataNode
|
||||
private bool TryValidateWithTypeReader<T, TNode>(
|
||||
TNode node,
|
||||
ISerializationContext? context,
|
||||
[NotNullWhen(true)] out ValidatedNode? valid)
|
||||
where T : notnull
|
||||
where TNode : DataNode
|
||||
{
|
||||
if (TryGetReader<T, TNode>(null, out var reader))
|
||||
{
|
||||
@@ -218,8 +245,11 @@ namespace Robust.Shared.Serialization.Manager
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryGetReader<T, TNode>(ISerializationContext? context, [NotNullWhen(true)] out ITypeReader<T, TNode>? reader)
|
||||
where T : notnull where TNode : DataNode
|
||||
private bool TryGetReader<T, TNode>(
|
||||
ISerializationContext? context,
|
||||
[NotNullWhen(true)] out ITypeReader<T, TNode>? reader)
|
||||
where T : notnull
|
||||
where TNode : DataNode
|
||||
{
|
||||
if (context != null && context.TypeReaders.TryGetValue((typeof(T), typeof(TNode)), out var rawTypeReader) ||
|
||||
_typeReaders.TryGetValue((typeof(T), typeof(TNode)), out rawTypeReader))
|
||||
@@ -231,11 +261,17 @@ namespace Robust.Shared.Serialization.Manager
|
||||
return TryGetGenericReader(out reader);
|
||||
}
|
||||
|
||||
private bool TryReadWithTypeSerializers<T, TNode>(TNode node, out DeserializationResult? obj, ISerializationContext? context = null) where T : notnull where TNode : DataNode
|
||||
private bool TryReadWithTypeSerializers<T, TNode>(
|
||||
TNode node,
|
||||
out DeserializationResult? obj,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
where T : notnull
|
||||
where TNode : DataNode
|
||||
{
|
||||
if (TryGetReader<T, TNode>(context, out var reader))
|
||||
{
|
||||
obj = reader.Read(this, node, context);
|
||||
obj = reader.Read(this, node, skipHook, context);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -243,7 +279,11 @@ namespace Robust.Shared.Serialization.Manager
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryWriteWithTypeSerializers(Type type, object obj, [NotNullWhen(true)] out DataNode? node, bool alwaysWrite = false,
|
||||
private bool TryWriteWithTypeSerializers(
|
||||
Type type,
|
||||
object obj,
|
||||
[NotNullWhen(true)] out DataNode? node,
|
||||
bool alwaysWrite = false,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
//TODO Paul: do this shit w/ delegates
|
||||
@@ -289,13 +329,13 @@ namespace Robust.Shared.Serialization.Manager
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryCopyWithTypeCopier(Type type, object source, ref object target, ISerializationContext? context = null)
|
||||
private bool TryCopyWithTypeCopier(Type type, object source, ref object target, bool skipHook, ISerializationContext? context = null)
|
||||
{
|
||||
//TODO Paul: do this shit w/ delegates
|
||||
var method = typeof(SerializationManager).GetRuntimeMethods().First(m =>
|
||||
m.Name == nameof(TryCopyWithTypeCopier) && m.GetParameters().Length == 3).MakeGenericMethod(type, source.GetType(), target.GetType());
|
||||
m.Name == nameof(TryCopyWithTypeCopier) && m.GetParameters().Length == 4).MakeGenericMethod(type, source.GetType(), target.GetType());
|
||||
|
||||
var arr = new[] {source, target, context};
|
||||
var arr = new[] {source, target, context, skipHook};
|
||||
var res = method.Invoke(this, arr);
|
||||
|
||||
if (res as bool? ?? false)
|
||||
@@ -307,21 +347,29 @@ namespace Robust.Shared.Serialization.Manager
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryCopyWithTypeCopier<TCommon, TSource, TTarget>(TSource source, ref TTarget target, ISerializationContext? context = null)
|
||||
where TSource : TCommon where TTarget : TCommon where TCommon : notnull
|
||||
private bool TryCopyWithTypeCopier<TCommon, TSource, TTarget>(
|
||||
TSource source,
|
||||
ref TTarget target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
where TSource : TCommon
|
||||
where TTarget : TCommon
|
||||
where TCommon : notnull
|
||||
{
|
||||
object? rawTypeCopier;
|
||||
if (context != null && context.TypeCopiers.TryGetValue(typeof(TCommon), out rawTypeCopier) ||
|
||||
|
||||
if (context != null &&
|
||||
context.TypeCopiers.TryGetValue(typeof(TCommon), out rawTypeCopier) ||
|
||||
_typeCopiers.TryGetValue(typeof(TCommon), out rawTypeCopier))
|
||||
{
|
||||
var ser = (ITypeCopier<TCommon>) rawTypeCopier;
|
||||
target = (TTarget) ser.Copy(this, source, target, context);
|
||||
target = (TTarget) ser.Copy(this, source, target, skipHook, context);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (TryGetGenericCopier(out ITypeCopier<TCommon>? genericTypeWriter))
|
||||
{
|
||||
target = (TTarget) genericTypeWriter.Copy(this, source, target, context);
|
||||
target = (TTarget) genericTypeWriter.Copy(this, source, target, skipHook, context);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace Robust.Shared.Serialization.Markdown.Validation
|
||||
}
|
||||
|
||||
public override bool Valid => false;
|
||||
|
||||
public override IEnumerable<string> Invalids()
|
||||
{
|
||||
var str = Node.ToString();
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public class AngleSerializer : ITypeSerializer<Angle, ValueDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
var nodeContents = node.Value;
|
||||
@@ -41,7 +42,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
}
|
||||
|
||||
[MustUseReturnValue]
|
||||
public Angle Copy(ISerializationManager serializationManager, Angle source, Angle target, ISerializationContext? context = null)
|
||||
public Angle Copy(ISerializationManager serializationManager, Angle source, Angle target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new(source);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public class Box2Serializer : ITypeSerializer<Box2, ValueDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
var args = node.Value.Split(',');
|
||||
@@ -43,17 +44,27 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
return float.TryParse(args[0], out _) &&
|
||||
float.TryParse(args[1], out _) &&
|
||||
float.TryParse(args[2], out _) &&
|
||||
float.TryParse(args[3], out _) ? new ValidatedValueNode(node) : new ErrorNode(node);
|
||||
float.TryParse(args[3], out _)
|
||||
? new ValidatedValueNode(node)
|
||||
: new ErrorNode(node);
|
||||
}
|
||||
|
||||
public DataNode Write(ISerializationManager serializationManager, Box2 value, bool alwaysWrite = false,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new ValueDataNode($"{value.Bottom.ToString(CultureInfo.InvariantCulture)},{value.Left.ToString(CultureInfo.InvariantCulture)},{value.Top.ToString(CultureInfo.InvariantCulture)},{value.Right.ToString(CultureInfo.InvariantCulture)}");
|
||||
var nodeValue =
|
||||
$"{value.Bottom.ToString(CultureInfo.InvariantCulture)}," +
|
||||
$"{value.Left.ToString(CultureInfo.InvariantCulture)}," +
|
||||
$"{value.Top.ToString(CultureInfo.InvariantCulture)}," +
|
||||
$"{value.Right.ToString(CultureInfo.InvariantCulture)}";
|
||||
|
||||
return new ValueDataNode(nodeValue);
|
||||
}
|
||||
|
||||
[MustUseReturnValue]
|
||||
public Box2 Copy(ISerializationManager serializationManager, Box2 source, Box2 target, ISerializationContext? context = null)
|
||||
public Box2 Copy(ISerializationManager serializationManager, Box2 source, Box2 target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new(source.Left, source.Bottom, source.Right, source.Top);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public class ColorSerializer : ITypeSerializer<Color, ValueDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
var deserializedColor = Color.TryFromName(node.Value, out var color)
|
||||
@@ -34,7 +35,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
}
|
||||
|
||||
[MustUseReturnValue]
|
||||
public Color Copy(ISerializationManager serializationManager, Color source, Color target, ISerializationContext? context = null)
|
||||
public Color Copy(ISerializationManager serializationManager, Color source, Color target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new(source.R, source.G, source.B, source.A);
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager,
|
||||
SequenceDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
var factory = IoCManager.Resolve<IComponentFactory>();
|
||||
@@ -53,7 +54,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
copy.RemoveNode("type");
|
||||
|
||||
var type = factory.GetRegistration(compType).Type;
|
||||
var read = serializationManager.ReadWithValueOrThrow<IComponent>(type, copy);
|
||||
var read = serializationManager.ReadWithValueOrThrow<IComponent>(type, copy, skipHook: skipHook);
|
||||
|
||||
components[compType] = read.value;
|
||||
mappings.Add(DeserializationResult.Value(compType), read.result);
|
||||
@@ -124,6 +125,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
foreach (var componentName in components.Keys)
|
||||
{
|
||||
var registration = factory.GetRegistration(componentName);
|
||||
|
||||
foreach (var compType in registration.References)
|
||||
{
|
||||
if (referenceTypes.Contains(compType))
|
||||
@@ -159,7 +161,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
|
||||
[MustUseReturnValue]
|
||||
public ComponentRegistry Copy(ISerializationManager serializationManager, ComponentRegistry source,
|
||||
ComponentRegistry target, ISerializationContext? context = null)
|
||||
ComponentRegistry target, bool skipHook, ISerializationContext? context = null)
|
||||
{
|
||||
target.Clear();
|
||||
target.EnsureCapacity(source.Count);
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public class FormattedMessageSerializer : ITypeSerializer<FormattedMessage, ValueDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager,
|
||||
ValueDataNode node, ISerializationContext? context = null)
|
||||
ValueDataNode node, bool skipHook, ISerializationContext? context = null)
|
||||
{
|
||||
return new DeserializedValue<FormattedMessage>(FormattedMessage.FromMarkup(node.Value));
|
||||
}
|
||||
@@ -32,7 +32,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
|
||||
[MustUseReturnValue]
|
||||
public FormattedMessage Copy(ISerializationManager serializationManager, FormattedMessage source,
|
||||
FormattedMessage target, ISerializationContext? context = null)
|
||||
FormattedMessage target, bool skipHook, ISerializationContext? context = null)
|
||||
{
|
||||
return new(source);
|
||||
}
|
||||
|
||||
@@ -2,13 +2,11 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Serialization.Manager;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Serialization.Manager.Result;
|
||||
using Robust.Shared.Serialization.Markdown;
|
||||
using Robust.Shared.Serialization.Markdown.Validation;
|
||||
using YamlDotNet.Core.Tokens;
|
||||
|
||||
namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
{
|
||||
@@ -37,15 +35,15 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
}
|
||||
|
||||
public DeserializationResult Read(ISerializationManager serializationManager,
|
||||
MappingDataNode node, ISerializationContext? context)
|
||||
MappingDataNode node, bool skipHook, ISerializationContext? context)
|
||||
{
|
||||
var dict = new Dictionary<TKey, TValue>();
|
||||
var mappedFields = new Dictionary<DeserializationResult, DeserializationResult>();
|
||||
|
||||
foreach (var (key, value) in node.Children)
|
||||
{
|
||||
var (keyVal, keyResult) = serializationManager.ReadWithValueOrThrow<TKey>(key, context);
|
||||
var (valueResult, valueVal) = serializationManager.ReadWithValueCast<TValue>(typeof(TValue), value, context);
|
||||
var (keyVal, keyResult) = serializationManager.ReadWithValueOrThrow<TKey>(key, context, skipHook);
|
||||
var (valueResult, valueVal) = serializationManager.ReadWithValueCast<TValue>(typeof(TValue), value, context, skipHook);
|
||||
|
||||
dict.Add(keyVal, valueVal!);
|
||||
mappedFields.Add(keyResult, valueResult);
|
||||
@@ -107,15 +105,16 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
|
||||
DeserializationResult
|
||||
ITypeReader<IReadOnlyDictionary<TKey, TValue>, MappingDataNode>.Read(
|
||||
ISerializationManager serializationManager, MappingDataNode node, ISerializationContext? context)
|
||||
ISerializationManager serializationManager, MappingDataNode node,
|
||||
bool skipHook, ISerializationContext? context)
|
||||
{
|
||||
var dict = new Dictionary<TKey, TValue>();
|
||||
var mappedFields = new Dictionary<DeserializationResult, DeserializationResult>();
|
||||
|
||||
foreach (var (key, value) in node.Children)
|
||||
{
|
||||
var (keyVal, keyResult) = serializationManager.ReadWithValueOrThrow<TKey>(key, context);
|
||||
var (valueResult, valueVal) = serializationManager.ReadWithValueCast<TValue>(typeof(TValue), value, context);
|
||||
var (keyVal, keyResult) = serializationManager.ReadWithValueOrThrow<TKey>(key, context, skipHook);
|
||||
var (valueResult, valueVal) = serializationManager.ReadWithValueCast<TValue>(typeof(TValue), value, context, skipHook);
|
||||
|
||||
dict.Add(keyVal, valueVal!);
|
||||
mappedFields.Add(keyResult, valueResult);
|
||||
@@ -126,15 +125,16 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
|
||||
DeserializationResult
|
||||
ITypeReader<SortedDictionary<TKey, TValue>, MappingDataNode>.Read(
|
||||
ISerializationManager serializationManager, MappingDataNode node, ISerializationContext? context)
|
||||
ISerializationManager serializationManager, MappingDataNode node,
|
||||
bool skipHook, ISerializationContext? context)
|
||||
{
|
||||
var dict = new SortedDictionary<TKey, TValue>();
|
||||
var mappedFields = new Dictionary<DeserializationResult, DeserializationResult>();
|
||||
|
||||
foreach (var (key, value) in node.Children)
|
||||
{
|
||||
var (keyVal, keyResult) = serializationManager.ReadWithValueOrThrow<TKey>(key, context);
|
||||
var (valueResult, valueVal) = serializationManager.ReadWithValueCast<TValue>(typeof(TValue), value, context);
|
||||
var (keyVal, keyResult) = serializationManager.ReadWithValueOrThrow<TKey>(key, context, skipHook);
|
||||
var (valueResult, valueVal) = serializationManager.ReadWithValueCast<TValue>(typeof(TValue), value, context, skipHook);
|
||||
|
||||
dict.Add(keyVal, valueVal!);
|
||||
mappedFields.Add(keyResult, valueResult);
|
||||
@@ -161,7 +161,8 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
|
||||
[MustUseReturnValue]
|
||||
public Dictionary<TKey, TValue> Copy(ISerializationManager serializationManager,
|
||||
Dictionary<TKey, TValue> source, Dictionary<TKey, TValue> target, ISerializationContext? context = null)
|
||||
Dictionary<TKey, TValue> source, Dictionary<TKey, TValue> target,
|
||||
bool skipHook, ISerializationContext? context = null)
|
||||
{
|
||||
return CopyInternal(serializationManager, source, target, context);
|
||||
}
|
||||
@@ -169,6 +170,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
[MustUseReturnValue]
|
||||
public IReadOnlyDictionary<TKey, TValue> Copy(ISerializationManager serializationManager,
|
||||
IReadOnlyDictionary<TKey, TValue> source, IReadOnlyDictionary<TKey, TValue> target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
if (target is Dictionary<TKey, TValue> targetDictionary)
|
||||
@@ -192,6 +194,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
[MustUseReturnValue]
|
||||
public SortedDictionary<TKey, TValue> Copy(ISerializationManager serializationManager,
|
||||
SortedDictionary<TKey, TValue> source, SortedDictionary<TKey, TValue> target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return CopyInternal(serializationManager, source, target, context);
|
||||
|
||||
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Serialization.Manager;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Serialization.Manager.Result;
|
||||
@@ -19,6 +18,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
{
|
||||
DeserializationResult ITypeReader<HashSet<T>, SequenceDataNode>.Read(ISerializationManager serializationManager,
|
||||
SequenceDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context)
|
||||
{
|
||||
var set = new HashSet<T>();
|
||||
@@ -26,7 +26,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
|
||||
foreach (var dataNode in node.Sequence)
|
||||
{
|
||||
var (value, result) = serializationManager.ReadWithValueOrThrow<T>(dataNode, context);
|
||||
var (value, result) = serializationManager.ReadWithValueOrThrow<T>(dataNode, context, skipHook);
|
||||
|
||||
set.Add(value);
|
||||
mappings.Add(result);
|
||||
@@ -80,7 +80,9 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
}
|
||||
|
||||
DeserializationResult ITypeReader<ImmutableHashSet<T>, SequenceDataNode>.Read(
|
||||
ISerializationManager serializationManager, SequenceDataNode node,
|
||||
ISerializationManager serializationManager,
|
||||
SequenceDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context)
|
||||
{
|
||||
var set = ImmutableHashSet.CreateBuilder<T>();
|
||||
@@ -88,7 +90,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
|
||||
foreach (var dataNode in node.Sequence)
|
||||
{
|
||||
var (value, result) = serializationManager.ReadWithValueOrThrow<T>(dataNode, context);
|
||||
var (value, result) = serializationManager.ReadWithValueOrThrow<T>(dataNode, context, skipHook);
|
||||
|
||||
set.Add(value);
|
||||
mappings.Add(result);
|
||||
@@ -98,7 +100,9 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
}
|
||||
|
||||
[MustUseReturnValue]
|
||||
public HashSet<T> Copy(ISerializationManager serializationManager, HashSet<T> source, HashSet<T> target, ISerializationContext? context = null)
|
||||
public HashSet<T> Copy(ISerializationManager serializationManager, HashSet<T> source, HashSet<T> target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
target.Clear();
|
||||
target.EnsureCapacity(source.Count);
|
||||
@@ -114,7 +118,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
|
||||
[MustUseReturnValue]
|
||||
public ImmutableHashSet<T> Copy(ISerializationManager serializationManager, ImmutableHashSet<T> source,
|
||||
ImmutableHashSet<T> target, ISerializationContext? context = null)
|
||||
ImmutableHashSet<T> target, bool skipHook, ISerializationContext? context = null)
|
||||
{
|
||||
var builder = ImmutableHashSet.CreateBuilder<T>();
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Serialization.Manager;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Serialization.Manager.Result;
|
||||
@@ -58,14 +57,16 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
}
|
||||
|
||||
DeserializationResult ITypeReader<List<T>, SequenceDataNode>.Read(ISerializationManager serializationManager,
|
||||
SequenceDataNode node, ISerializationContext? context)
|
||||
SequenceDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context)
|
||||
{
|
||||
var list = new List<T>();
|
||||
var results = new List<DeserializationResult>();
|
||||
|
||||
foreach (var dataNode in node.Sequence)
|
||||
{
|
||||
var (value, result) = serializationManager.ReadWithValueOrThrow<T>(typeof(T), dataNode, context);
|
||||
var (value, result) = serializationManager.ReadWithValueOrThrow<T>(typeof(T), dataNode, context, skipHook);
|
||||
list.Add(value);
|
||||
results.Add(result);
|
||||
}
|
||||
@@ -112,14 +113,15 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
}
|
||||
|
||||
DeserializationResult ITypeReader<IReadOnlyList<T>, SequenceDataNode>.Read(
|
||||
ISerializationManager serializationManager, SequenceDataNode node, ISerializationContext? context)
|
||||
ISerializationManager serializationManager, SequenceDataNode node,
|
||||
bool skipHook, ISerializationContext? context)
|
||||
{
|
||||
var list = new List<T>();
|
||||
var results = new List<DeserializationResult>();
|
||||
|
||||
foreach (var dataNode in node.Sequence)
|
||||
{
|
||||
var (value, result) = serializationManager.ReadWithValueOrThrow<T>(dataNode, context);
|
||||
var (value, result) = serializationManager.ReadWithValueOrThrow<T>(dataNode, context, skipHook);
|
||||
|
||||
list.Add(value);
|
||||
results.Add(result);
|
||||
@@ -129,14 +131,15 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
}
|
||||
|
||||
DeserializationResult ITypeReader<IReadOnlyCollection<T>, SequenceDataNode>.Read(
|
||||
ISerializationManager serializationManager, SequenceDataNode node, ISerializationContext? context)
|
||||
ISerializationManager serializationManager, SequenceDataNode node,
|
||||
bool skipHook, ISerializationContext? context)
|
||||
{
|
||||
var list = new List<T>();
|
||||
var results = new List<DeserializationResult>();
|
||||
|
||||
foreach (var dataNode in node.Sequence)
|
||||
{
|
||||
var (value, result) = serializationManager.ReadWithValueOrThrow<T>(dataNode, context);
|
||||
var (value, result) = serializationManager.ReadWithValueOrThrow<T>(dataNode, context, skipHook);
|
||||
list.Add(value);
|
||||
results.Add(result);
|
||||
}
|
||||
@@ -145,14 +148,15 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
}
|
||||
|
||||
DeserializationResult ITypeReader<ImmutableList<T>, SequenceDataNode>.Read(
|
||||
ISerializationManager serializationManager, SequenceDataNode node, ISerializationContext? context)
|
||||
ISerializationManager serializationManager, SequenceDataNode node,
|
||||
bool skipHook, ISerializationContext? context)
|
||||
{
|
||||
var list = ImmutableList.CreateBuilder<T>();
|
||||
var results = new List<DeserializationResult>();
|
||||
|
||||
foreach (var dataNode in node.Sequence)
|
||||
{
|
||||
var (value, result) = serializationManager.ReadWithValueOrThrow<T>(dataNode, context);
|
||||
var (value, result) = serializationManager.ReadWithValueOrThrow<T>(dataNode, context, skipHook);
|
||||
list.Add(value);
|
||||
results.Add(result);
|
||||
}
|
||||
@@ -175,14 +179,16 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
}
|
||||
|
||||
[MustUseReturnValue]
|
||||
public List<T> Copy(ISerializationManager serializationManager, List<T> source, List<T> target, ISerializationContext? context = null)
|
||||
public List<T> Copy(ISerializationManager serializationManager, List<T> source, List<T> target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return CopyInternal(serializationManager, source, target, context);
|
||||
}
|
||||
|
||||
[MustUseReturnValue]
|
||||
public IReadOnlyList<T> Copy(ISerializationManager serializationManager, IReadOnlyList<T> source,
|
||||
IReadOnlyList<T> target, ISerializationContext? context = null)
|
||||
IReadOnlyList<T> target, bool skipHook, ISerializationContext? context = null)
|
||||
{
|
||||
if (target is List<T> targetList)
|
||||
{
|
||||
@@ -202,7 +208,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
|
||||
[MustUseReturnValue]
|
||||
public IReadOnlyCollection<T> Copy(ISerializationManager serializationManager, IReadOnlyCollection<T> source,
|
||||
IReadOnlyCollection<T> target, ISerializationContext? context = null)
|
||||
IReadOnlyCollection<T> target, bool skipHook, ISerializationContext? context = null)
|
||||
{
|
||||
if (target is List<T> targetList)
|
||||
{
|
||||
@@ -221,7 +227,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
}
|
||||
|
||||
public ImmutableList<T> Copy(ISerializationManager serializationManager, ImmutableList<T> source,
|
||||
ImmutableList<T> target, ISerializationContext? context = null)
|
||||
ImmutableList<T> target, bool skipHook, ISerializationContext? context = null)
|
||||
{
|
||||
var builder = ImmutableList.CreateBuilder<T>();
|
||||
|
||||
|
||||
@@ -13,14 +13,15 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
public class ValueTupleSerializer<T1, T2> : ITypeSerializer<ValueTuple<T1, T2>, MappingDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager, MappingDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
if (node.Children.Count != 1)
|
||||
throw new InvalidMappingException("Less than or more than 1 mappings provided to ValueTupleSerializer");
|
||||
|
||||
var entry = node.Children.First();
|
||||
var v1 = serializationManager.ReadValueOrThrow<T1>(entry.Key, context);
|
||||
var v2 = serializationManager.ReadValueOrThrow<T2>(entry.Value, context);
|
||||
var v1 = serializationManager.ReadValueOrThrow<T1>(entry.Key, context, skipHook);
|
||||
var v2 = serializationManager.ReadValueOrThrow<T2>(entry.Value, context, skipHook);
|
||||
|
||||
return DeserializationResult.Value(new ValueTuple<T1, T2>(v1, v2));
|
||||
}
|
||||
@@ -29,10 +30,16 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
if (node.Children.Count != 1) return new ErrorNode(node);
|
||||
|
||||
var entry = node.Children.First();
|
||||
var dict = new Dictionary<ValidatedNode, ValidatedNode>();
|
||||
dict.Add(serializationManager.ValidateNode(typeof(T1), entry.Key, context),
|
||||
serializationManager.ValidateNode(typeof(T2), entry.Value, context));
|
||||
var dict = new Dictionary<ValidatedNode, ValidatedNode>
|
||||
{
|
||||
{
|
||||
serializationManager.ValidateNode(typeof(T1), entry.Key, context),
|
||||
serializationManager.ValidateNode(typeof(T2), entry.Value, context)
|
||||
}
|
||||
};
|
||||
|
||||
return new ValidatedMappingNode(dict);
|
||||
}
|
||||
|
||||
@@ -49,6 +56,7 @@ namespace Robust.Shared.Serialization.TypeSerializers.Generic
|
||||
}
|
||||
|
||||
public (T1, T2) Copy(ISerializationManager serializationManager, (T1, T2) source, (T1, T2) target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return (serializationManager.Copy(source.Item1, target.Item1)!,
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public class MapIdSerializer : ITypeSerializer<MapId, ValueDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
var val = int.Parse(node.Value, CultureInfo.InvariantCulture);
|
||||
@@ -33,7 +34,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
}
|
||||
|
||||
[MustUseReturnValue]
|
||||
public MapId Copy(ISerializationManager serializationManager, MapId source, MapId target, ISerializationContext? context = null)
|
||||
public MapId Copy(ISerializationManager serializationManager, MapId source, MapId target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new(source.Value);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public class RegexSerializer : ITypeSerializer<Regex, ValueDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new DeserializedValue<Regex>(new Regex(node.Value, RegexOptions.Compiled));
|
||||
@@ -40,7 +41,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
}
|
||||
|
||||
[MustUseReturnValue]
|
||||
public Regex Copy(ISerializationManager serializationManager, Regex source, Regex target, ISerializationContext? context = null)
|
||||
public Regex Copy(ISerializationManager serializationManager, Regex source, Regex target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new(source.ToString(), source.Options, source.MatchTimeout);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public class ResourcePathSerializer : ITypeSerializer<ResourcePath, ValueDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new DeserializedValue<ResourcePath>(new ResourcePath(node.Value));
|
||||
@@ -42,7 +43,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
}
|
||||
|
||||
[MustUseReturnValue]
|
||||
public ResourcePath Copy(ISerializationManager serializationManager, ResourcePath source, ResourcePath target, ISerializationContext? context = null)
|
||||
public ResourcePath Copy(ISerializationManager serializationManager, ResourcePath source, ResourcePath target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new(source.ToString());
|
||||
}
|
||||
|
||||
@@ -19,9 +19,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
ITypeCopier<EntityPrototype>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager,
|
||||
ValueDataNode node, ISerializationContext? context = null)
|
||||
ValueDataNode node, bool skipHook, ISerializationContext? context = null)
|
||||
{
|
||||
var path = serializationManager.ReadValueOrThrow<ResourcePath>(node, context);
|
||||
var path = serializationManager.ReadValueOrThrow<ResourcePath>(node, context, skipHook);
|
||||
var texture = new Texture(path);
|
||||
|
||||
return new DeserializedValue<SpriteSpecifier>(texture);
|
||||
@@ -30,17 +30,17 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public ValidatedNode Validate(ISerializationManager serializationManager, ValueDataNode node,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return serializationManager.ReadValue<ResourcePath>(node) != null ? new ValidatedValueNode(node) : new ErrorNode(node);
|
||||
return serializationManager.ValidateNode<ResourcePath>(node);
|
||||
}
|
||||
|
||||
public DeserializationResult Read(ISerializationManager serializationManager,
|
||||
MappingDataNode node, ISerializationContext? context = null)
|
||||
MappingDataNode node, bool skipHook, ISerializationContext? context = null)
|
||||
{
|
||||
if (node.TryGetNode("sprite", out var spriteNode)
|
||||
&& node.TryGetNode("state", out var rawStateNode)
|
||||
&& rawStateNode is ValueDataNode stateNode)
|
||||
{
|
||||
var path = serializationManager.ReadValueOrThrow<ResourcePath>(spriteNode, context);
|
||||
var path = serializationManager.ReadValueOrThrow<ResourcePath>(spriteNode, context, skipHook);
|
||||
var rsi = new Rsi(path, stateNode.Value);
|
||||
|
||||
return new DeserializedValue<SpriteSpecifier>(rsi);
|
||||
@@ -52,12 +52,14 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public ValidatedNode Validate(ISerializationManager serializationManager, MappingDataNode node,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return node.HasNode("sprite") &&
|
||||
node.TryGetNode("state", out var stateNode) &&
|
||||
stateNode is ValueDataNode &&
|
||||
serializationManager.ReadValue<ResourcePath>(stateNode) != null
|
||||
? new ValidatedValueNode(node)
|
||||
: new ErrorNode(node);
|
||||
if (!node.HasNode("sprite") ||
|
||||
!node.TryGetNode("state", out var stateNode) ||
|
||||
stateNode is not ValueDataNode)
|
||||
{
|
||||
return new ErrorNode(node);
|
||||
}
|
||||
|
||||
return serializationManager.ValidateNode<ResourcePath>(stateNode);
|
||||
}
|
||||
|
||||
public DataNode Write(ISerializationManager serializationManager, SpriteSpecifier value,
|
||||
@@ -78,19 +80,23 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Rsi Copy(ISerializationManager serializationManager, Rsi source, Rsi target, ISerializationContext? context = null)
|
||||
public Rsi Copy(ISerializationManager serializationManager, Rsi source, Rsi target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new(source.RsiPath, source.RsiState);
|
||||
}
|
||||
|
||||
public Texture Copy(ISerializationManager serializationManager, Texture source, Texture target, ISerializationContext? context = null)
|
||||
public Texture Copy(ISerializationManager serializationManager, Texture source, Texture target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new(source.TexturePath);
|
||||
}
|
||||
|
||||
[MustUseReturnValue]
|
||||
public EntityPrototype Copy(ISerializationManager serializationManager, EntityPrototype source,
|
||||
EntityPrototype target, ISerializationContext? context = null)
|
||||
EntityPrototype target, bool skipHook, ISerializationContext? context = null)
|
||||
{
|
||||
return new(source.EntityPrototypeId);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public class StringSerializer : ITypeSerializer<string, ValueDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new DeserializedValue<string>(node.Value);
|
||||
@@ -29,7 +30,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
}
|
||||
|
||||
[MustUseReturnValue]
|
||||
public string Copy(ISerializationManager serializationManager, string source, string target, ISerializationContext? context = null)
|
||||
public string Copy(ISerializationManager serializationManager, string source, string target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public class TimespanSerializer : ITypeSerializer<TimeSpan, ValueDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
var seconds = double.Parse(node.Value, CultureInfo.InvariantCulture);
|
||||
@@ -22,7 +23,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public ValidatedNode Validate(ISerializationManager serializationManager, ValueDataNode node,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return double.TryParse(node.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out _) ? new ValidatedValueNode(node) : new ErrorNode(node);
|
||||
return double.TryParse(node.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out _)
|
||||
? new ValidatedValueNode(node)
|
||||
: new ErrorNode(node);
|
||||
}
|
||||
|
||||
public DataNode Write(ISerializationManager serializationManager, TimeSpan value, bool alwaysWrite = false,
|
||||
@@ -32,7 +35,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
}
|
||||
|
||||
[MustUseReturnValue]
|
||||
public TimeSpan Copy(ISerializationManager serializationManager, TimeSpan source, TimeSpan target, ISerializationContext? context = null)
|
||||
public TimeSpan Copy(ISerializationManager serializationManager, TimeSpan source, TimeSpan target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public class UIBox2Serializer : ITypeSerializer<UIBox2, ValueDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
var args = node.Value.Split(',');
|
||||
@@ -44,7 +45,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
return float.TryParse(args[0], NumberStyles.Any, CultureInfo.InvariantCulture, out _) &&
|
||||
float.TryParse(args[1], NumberStyles.Any, CultureInfo.InvariantCulture, out _) &&
|
||||
float.TryParse(args[2], NumberStyles.Any, CultureInfo.InvariantCulture, out _) &&
|
||||
float.TryParse(args[3], NumberStyles.Any, CultureInfo.InvariantCulture, out _) ? new ValidatedValueNode(node) : new ErrorNode(node);
|
||||
float.TryParse(args[3], NumberStyles.Any, CultureInfo.InvariantCulture, out _)
|
||||
? new ValidatedValueNode(node)
|
||||
: new ErrorNode(node);
|
||||
}
|
||||
|
||||
public DataNode Write(ISerializationManager serializationManager, UIBox2 value, bool alwaysWrite = false,
|
||||
@@ -54,7 +57,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
}
|
||||
|
||||
[MustUseReturnValue]
|
||||
public UIBox2 Copy(ISerializationManager serializationManager, UIBox2 source, UIBox2 target, ISerializationContext? context = null)
|
||||
public UIBox2 Copy(ISerializationManager serializationManager, UIBox2 source, UIBox2 target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new(source.Left, source.Top, source.Right, source.Bottom);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Serialization.Manager;
|
||||
@@ -13,6 +12,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public class Vector2Serializer : ITypeSerializer<Vector2, ValueDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
string raw = node.Value;
|
||||
@@ -42,7 +42,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
}
|
||||
|
||||
return float.TryParse(args[0], NumberStyles.Any, CultureInfo.InvariantCulture, out _) &&
|
||||
float.TryParse(args[1], NumberStyles.Any, CultureInfo.InvariantCulture, out _) ? new ValidatedValueNode(node) : new ErrorNode(node);
|
||||
float.TryParse(args[1], NumberStyles.Any, CultureInfo.InvariantCulture, out _)
|
||||
? new ValidatedValueNode(node)
|
||||
: new ErrorNode(node);
|
||||
}
|
||||
|
||||
public DataNode Write(ISerializationManager serializationManager, Vector2 value, bool alwaysWrite = false,
|
||||
@@ -51,7 +53,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
return new ValueDataNode($"{value.X.ToString(CultureInfo.InvariantCulture)},{value.Y.ToString(CultureInfo.InvariantCulture)}");
|
||||
}
|
||||
|
||||
public Vector2 Copy(ISerializationManager serializationManager, Vector2 source, Vector2 target, ISerializationContext? context = null)
|
||||
public Vector2 Copy(ISerializationManager serializationManager, Vector2 source, Vector2 target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new(source.X, source.Y);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Serialization.Manager;
|
||||
@@ -13,6 +12,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public class Vector2iSerializer : ITypeSerializer<Vector2i, ValueDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
string raw = node.Value;
|
||||
@@ -42,7 +42,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
}
|
||||
|
||||
return int.TryParse(args[0], NumberStyles.Any, CultureInfo.InvariantCulture, out _) &&
|
||||
int.TryParse(args[1], NumberStyles.Any, CultureInfo.InvariantCulture, out _) ? new ValidatedValueNode(node) : new ErrorNode(node);
|
||||
int.TryParse(args[1], NumberStyles.Any, CultureInfo.InvariantCulture, out _)
|
||||
? new ValidatedValueNode(node)
|
||||
: new ErrorNode(node);
|
||||
}
|
||||
|
||||
public DataNode Write(ISerializationManager serializationManager, Vector2i value, bool alwaysWrite = false,
|
||||
@@ -51,7 +53,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
return new ValueDataNode($"{value.X.ToString(CultureInfo.InvariantCulture)},{value.Y.ToString(CultureInfo.InvariantCulture)}");
|
||||
}
|
||||
|
||||
public Vector2i Copy(ISerializationManager serializationManager, Vector2i source, Vector2i target, ISerializationContext? context = null)
|
||||
public Vector2i Copy(ISerializationManager serializationManager, Vector2i source, Vector2i target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new(source.X, source.Y);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Serialization.Manager;
|
||||
@@ -13,6 +12,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public class Vector3Serializer : ITypeSerializer<Vector3, ValueDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
string raw = node.Value;
|
||||
@@ -44,7 +44,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
|
||||
return float.TryParse(args[0], NumberStyles.Any, CultureInfo.InvariantCulture, out _) &&
|
||||
float.TryParse(args[1], NumberStyles.Any, CultureInfo.InvariantCulture, out _) &&
|
||||
float.TryParse(args[2], NumberStyles.Any, CultureInfo.InvariantCulture, out _) ? new ValidatedValueNode(node) : new ErrorNode(node);
|
||||
float.TryParse(args[2], NumberStyles.Any, CultureInfo.InvariantCulture, out _)
|
||||
? new ValidatedValueNode(node)
|
||||
: new ErrorNode(node);
|
||||
}
|
||||
|
||||
public DataNode Write(ISerializationManager serializationManager, Vector3 value, bool alwaysWrite = false,
|
||||
@@ -54,7 +56,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
$"{value.X.ToString(CultureInfo.InvariantCulture)},{value.Y.ToString(CultureInfo.InvariantCulture)},{value.Z.ToString(CultureInfo.InvariantCulture)}");
|
||||
}
|
||||
|
||||
public Vector3 Copy(ISerializationManager serializationManager, Vector3 source, Vector3 target, ISerializationContext? context = null)
|
||||
public Vector3 Copy(ISerializationManager serializationManager, Vector3 source, Vector3 target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new(source);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Serialization.Manager;
|
||||
@@ -13,6 +12,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
public class Vector4Serializer : ITypeSerializer<Vector4, ValueDataNode>
|
||||
{
|
||||
public DeserializationResult Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
string raw = node.Value;
|
||||
@@ -46,7 +46,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
return float.TryParse(args[0], NumberStyles.Any, CultureInfo.InvariantCulture, out _) &&
|
||||
float.TryParse(args[1], NumberStyles.Any, CultureInfo.InvariantCulture, out _) &&
|
||||
float.TryParse(args[2], NumberStyles.Any, CultureInfo.InvariantCulture, out _) &&
|
||||
float.TryParse(args[3], NumberStyles.Any, CultureInfo.InvariantCulture, out _) ? new ValidatedValueNode(node) : new ErrorNode(node);
|
||||
float.TryParse(args[3], NumberStyles.Any, CultureInfo.InvariantCulture, out _)
|
||||
? new ValidatedValueNode(node)
|
||||
: new ErrorNode(node);
|
||||
}
|
||||
|
||||
public DataNode Write(ISerializationManager serializationManager, Vector4 value, bool alwaysWrite = false,
|
||||
@@ -55,7 +57,9 @@ namespace Robust.Shared.Serialization.TypeSerializers
|
||||
return new ValueDataNode($"{value.X.ToString(CultureInfo.InvariantCulture)},{value.Y.ToString(CultureInfo.InvariantCulture)},{value.Z.ToString(CultureInfo.InvariantCulture)},{value.W.ToString(CultureInfo.InvariantCulture)}");
|
||||
}
|
||||
|
||||
public Vector4 Copy(ISerializationManager serializationManager, Vector4 source, Vector4 target, ISerializationContext? context = null)
|
||||
public Vector4 Copy(ISerializationManager serializationManager, Vector4 source, Vector4 target,
|
||||
bool skipHook,
|
||||
ISerializationContext? context = null)
|
||||
{
|
||||
return new(source);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user