using System; using System.Diagnostics.CodeAnalysis; using JetBrains.Annotations; using Robust.Shared.Reflection; using Robust.Shared.Serialization.Markdown; using Robust.Shared.Serialization.Markdown.Mapping; using Robust.Shared.Serialization.Markdown.Validation; using Robust.Shared.Serialization.TypeSerializers.Interfaces; namespace Robust.Shared.Serialization.Manager { public interface ISerializationManager { public delegate T InstantiationDelegate(); /// /// Initializes the serialization manager. /// void Initialize(); /// /// Shuts down the serialization manager. /// void Shutdown(); IReflectionManager ReflectionManager { get; } #region Validation /// /// Validates that a node has all the properties required by a certain type. /// /// The type to check for. /// The node to check. /// The context to use, if any. /// /// A node with whether or not is valid and which of its fields /// are invalid, if any. /// [PreferGenericVariant] ValidationNode ValidateNode(Type type, DataNode node, ISerializationContext? context = null); /// /// Validates that a node has all the properties required by a certain type. /// /// The node to check. /// The context to use, if any. /// The type this node should be able to be read into. /// /// A node with whether or not is valid and which of its fields /// are invalid, if any. /// ValidationNode ValidateNode(DataNode node, ISerializationContext? context = null); /// /// Validates that a node has all the properties required by a certain type using a specified instance. /// /// The instance to use. /// The node to check. /// The context to use, if any. /// The type this node should be able to be read into. /// The node type /// ValidationNode ValidateNode(ITypeValidator typeValidator, TNode node, ISerializationContext? context = null) where TNode : DataNode; /// /// Validates that a node has all the properties required by a certain type using a specified type. /// /// The node to check. /// The context to use, if any. /// The type this node should be able to be read into. /// The node type /// The type of the . /// ValidationNode ValidateNode(TNode node, ISerializationContext? context = null) where TNode : DataNode where TValidator : ITypeValidator; #endregion #region Read /// /// Deserializes a node into an object, populating it. /// /// The type of object to deserialize into. /// The node to deserialize. /// The context to use, if any. /// Whether or not to skip running /// Set true if a reference Type should not allow null. Not necessary for value types. /// The deserialized object or null. public object? Read(Type type, DataNode node, ISerializationContext? context = null, bool skipHook = false, bool notNullableOverride = false); public object? Read( Type type, DataNode node, SerializationHookContext hookCtx, ISerializationContext? context = null, bool notNullableOverride = false); /// /// Deserializes a node into a populated object of the given generic type /// /// The node to deserialize. /// The context to use, if any. /// Whether or not to skip running /// The valueProvider which can provide a value to read into. If none is supplied, a new object will be created. /// Set true if a reference Type should not allow null. Not necessary for value types. /// The type of object to create and populate. /// The deserialized object, or null. T Read(DataNode node, ISerializationContext? context = null, bool skipHook = false, InstantiationDelegate? instanceProvider = null, [NotNullableFlag(nameof(T))] bool notNullableOverride = false); T Read( DataNode node, SerializationHookContext hookCtx, ISerializationContext? context = null, InstantiationDelegate? instanceProvider = null, [NotNullableFlag(nameof(T))] bool notNullableOverride = false); /// /// Deserializes a node into a populated object of the given generic type using the provided instance. /// /// The instance to use. /// The node to deserialize. /// The context to use, if any. /// Whether or not to skip running /// The valueProvider which can provide a value to read into. If none is supplied, a new object will be created. /// Set true if a reference Type should not allow null. Not necessary for value types. /// The type of object to create and populate. /// The node type that will be returned by the /// The deserialized object, or null. T Read(ITypeReader reader, TNode node, ISerializationContext? context = null, bool skipHook = false, InstantiationDelegate? instanceProvider = null, [NotNullableFlag(nameof(T))] bool notNullableOverride = false) where TNode : DataNode; T Read( ITypeReader reader, TNode node, SerializationHookContext hookCtx, ISerializationContext? context = null, InstantiationDelegate? instanceProvider = null, bool notNullableOverride = false) where TNode : DataNode; /// /// Deserializes a node into a populated object of the given generic type using the provided type. /// /// The node to deserialize. /// The context to use, if any. /// Whether or not to skip running /// The valueProvider which can provide a value to read into. If none is supplied, a new object will be created. /// Set true if a reference Type should not allow null. Not necessary for value types. /// The type of object to create and populate. /// The node type that will be returned by the /// The type of the . /// The deserialized object, or null. T Read(TNode node, ISerializationContext? context = null, bool skipHook = false, InstantiationDelegate? instanceProvider = null, [NotNullableFlag(nameof(T))] bool notNullableOverride = false) where TNode : DataNode where TReader : ITypeReader; T Read( TNode node, SerializationHookContext hookCtx, ISerializationContext? context = null, InstantiationDelegate? instanceProvider = null, bool notNullableOverride = false) where TNode : DataNode where TReader : ITypeReader; #endregion #region Write /// /// Serializes a value into a node. /// /// The value to serialize. /// /// Whether or not to always write the given values into the resulting node, /// even if they are the default. /// /// The context to use, if any. /// Set true if a reference Type should not allow null. Not necessary for value types. /// The type to serialize. /// A created from the given . DataNode WriteValue(T value, bool alwaysWrite = false, ISerializationContext? context = null, [NotNullableFlag(nameof(T))] bool notNullableOverride = false); /// /// Serializes a value into a node using a instance. /// /// The to use for serializing the value. /// The value to serialize. /// /// Whether or not to always write the given values into the resulting node, /// even if they are the default. /// /// The context to use, if any. /// Set true if a reference Type should not allow null. Not necessary for value types. /// The type to serialize. /// A serialized datanode created from the given by using the typewriter. DataNode WriteValue(ITypeWriter writer, T value, bool alwaysWrite = false, ISerializationContext? context = null, [NotNullableFlag(nameof(T))] bool notNullableOverride = false); /// /// Serializes a value into a node using a type. /// /// The value to serialize. /// /// Whether or not to always write the given values into the resulting node, /// even if they are the default. /// /// The context to use, if any. /// Set true if a reference Type should not allow null. Not necessary for value types. /// The type to serialize. /// The type of the . /// A serialized datanode created from the given by using the typewriter. DataNode WriteValue(T value, bool alwaysWrite = false, ISerializationContext? context = null, [NotNullableFlag(nameof(T))] bool notNullableOverride = false) where TWriter : ITypeWriter; /// /// Serializes a value into a node. /// /// The type of the to serialize as. /// The value to serialize. /// /// Whether or not to always write the given values into the resulting node, /// even if they are the default. /// /// The context to use, if any. /// Set true if a reference Type should not allow null. Not necessary for value types. /// /// A serialized datanode created from the given /// of type . /// [PreferGenericVariant] DataNode WriteValue(Type type, object? value, bool alwaysWrite = false, ISerializationContext? context = null, bool notNullableOverride = false); #endregion #region Copy /// /// Copies the values of one object into another. /// This does not guarantee that the object passed as /// is actually mutated. /// /// The object to copy values from. /// The object to copy values into. /// The context to use, if any. /// Whether or not to skip running /// Set true if a reference Type should not allow null. Not necessary for value types. void CopyTo(object source, ref object? target, ISerializationContext? context = null, bool skipHook = false, bool notNullableOverride = false); void CopyTo( object source, ref object? target, SerializationHookContext hookCtx, ISerializationContext? context = null, bool notNullableOverride = false); /// /// Copies the values of one object into another. /// This does not guarantee that the object passed as /// is actually mutated. /// /// The object to copy values from. /// The object to copy values into. /// The context to use, if any. /// Whether or not to skip running /// Set true if a reference Type should not allow null. Not necessary for value types. /// The type of the objects to copy from and into. void CopyTo(T source, ref T target, ISerializationContext? context = null, bool skipHook = false, [NotNullableFlag(nameof(T))] bool notNullableOverride = false); void CopyTo( T source, ref T target, SerializationHookContext hookCtx, ISerializationContext? context = null, [NotNullableFlag(nameof(T))] bool notNullableOverride = false); /// /// Copies the values of one object into another using a specified instance. /// This does not guarantee that the object passed as /// is actually mutated. /// /// the instance to use /// The object to copy values from. /// The object to copy values into. /// The context to use, if any. /// Whether or not to skip running /// Set true if a reference Type should not allow null. Not necessary for value types. /// The type of the objects to copy from and into. void CopyTo(ITypeCopier copier, T source, ref T target, ISerializationContext? context = null, bool skipHook = false, [NotNullableFlag(nameof(T))] bool notNullableOverride = false); void CopyTo( ITypeCopier copier, T source, ref T target, SerializationHookContext hookCtx, ISerializationContext? context = null, [NotNullableFlag(nameof(T))] bool notNullableOverride = false); /// /// Copies the values of one object into another using a specified type. /// This does not guarantee that the object passed as /// is actually mutated. /// /// The object to copy values from. /// The object to copy values into. /// The context to use, if any. /// Whether or not to skip running /// Set true if a reference Type should not allow null. Not necessary for value types. /// The type of the objects to copy from and into. /// The type of the . void CopyTo(T source, ref T target, ISerializationContext? context = null, bool skipHook = false, [NotNullableFlag(nameof(T))] bool notNullableOverride = false) where TCopier : ITypeCopier; void CopyTo( T source, ref T target, SerializationHookContext hookCtx, ISerializationContext? context = null, [NotNullableFlag(nameof(T))] bool notNullableOverride = false) where TCopier : ITypeCopier; /// /// Creates a copy of the given object. /// /// The object to copy. /// The context to use, if any. /// Whether or not to skip running /// Set true if a reference Type should not allow null. Not necessary for value types. /// A copy of the given object. [MustUseReturnValue] object? CreateCopy(object? source, ISerializationContext? context = null, bool skipHook = false, bool notNullableOverride = false); [MustUseReturnValue] object? CreateCopy( object? source, SerializationHookContext hookCtx, ISerializationContext? context = null, bool notNullableOverride = false); /// /// Creates a copy of the given object. /// /// The object to copy. /// The context to use, if any. /// Whether or not to skip running /// Set true if a reference Type should not allow null. Not necessary for value types. /// The type of the object to copy. /// A copy of the given object. [MustUseReturnValue] T CreateCopy(T source, ISerializationContext? context = null, bool skipHook = false, [NotNullableFlag(nameof(T))] bool notNullableOverride = false); [MustUseReturnValue] T CreateCopy( T source, SerializationHookContext hookCtx, ISerializationContext? context = null, bool notNullableOverride = false); /// /// Creates a copy of the given object using a specified instance. /// /// The instance. /// The object to copy. /// The context to use, if any. /// Whether or not to skip running /// Set true if a reference Type should not allow null. Not necessary for value types. /// The type of the object to copy. /// A copy of the given object. [MustUseReturnValue] T CreateCopy(ITypeCopyCreator copyCreator, T source, ISerializationContext? context = null, bool skipHook = false, [NotNullableFlag(nameof(T))] bool notNullableOverride = false); [MustUseReturnValue] T CreateCopy( ITypeCopyCreator copyCreator, T source, SerializationHookContext hookCtx, ISerializationContext? context = null, bool notNullableOverride = false); /// /// Creates a copy of the given object using a specified type. /// /// The object to copy. /// The context to use, if any. /// Whether or not to skip running /// Set true if a reference Type should not allow null. Not necessary for value types. /// The type of the object to copy. /// The type of the to use. /// A copy of the given object. [MustUseReturnValue] T CreateCopy(T source, ISerializationContext? context = null, bool skipHook = false, [NotNullableFlag(nameof(T))] bool notNullableOverride = false) where TCopyCreator : ITypeCopyCreator; [MustUseReturnValue] T CreateCopy( T source, SerializationHookContext hookCtx, ISerializationContext? context = null, bool notNullableOverride = false) where TCopyCreator : ITypeCopyCreator; [Obsolete] bool TryGetCopierOrCreator( out ITypeCopier? copier, out ITypeCopyCreator? copyCreator, ISerializationContext? context = null); [Obsolete] bool TryCustomCopy( T source, ref T target, SerializationHookContext hookCtx, bool hasHooks, ISerializationContext? context = null); #endregion #region Flags And Constants Type GetFlagTypeFromTag(Type tagType); int GetFlagHighestBit(Type tagType); Type GetConstantTypeFromTag(Type tagType); #endregion #region Composition DataNode PushComposition(Type type, DataNode[] parents, DataNode child, ISerializationContext? context = null); DataNode PushComposition(Type type, DataNode parent, DataNode child, ISerializationContext? context = null); public TNode PushComposition(TNode[] parents, TNode child, ISerializationContext? context = null) where TNode : DataNode { // ReSharper disable once CoVariantArrayConversion return (TNode)PushComposition(typeof(TType), parents, child, context); } public TNode PushComposition(TNode parent, TNode child, ISerializationContext? context = null) where TNode : DataNode { return (TNode) PushComposition(typeof(TType), parent, child, context); } TNode PushInheritance(ITypeInheritanceHandler inheritanceHandler, TNode parent, TNode child, ISerializationContext? context = null) where TNode : DataNode; TNode PushInheritance(TNode parent, TNode child, ISerializationContext? context = null) where TNode : DataNode where TInheritanceHandler : ITypeInheritanceHandler; public TNode PushCompositionWithGenericNode(Type type, TNode[] parents, TNode child, ISerializationContext? context = null) where TNode : DataNode { // ReSharper disable once CoVariantArrayConversion return (TNode) PushComposition(type, parents, child, context); } public TNode PushCompositionWithGenericNode(Type type, TNode parent, TNode child, ISerializationContext? context = null) where TNode : DataNode { return (TNode) PushComposition(type, parent, child, context); } /// /// Simple inheritance pusher clones data and overrides a parent's values with /// the child's. /// MappingDataNode CombineMappings(MappingDataNode child, MappingDataNode parent); #endregion public bool TryGetVariableType(Type type, string variableName, [NotNullWhen(true)] out Type? variableType); } }