WIP merge fixes and entity serialization

This commit is contained in:
DrSmugleaf
2021-03-02 20:57:48 +01:00
parent d437f82787
commit 1b2e774b74
17 changed files with 119 additions and 118 deletions

View File

@@ -280,13 +280,13 @@ namespace Robust.Server.Maps
_prototypeManager = prototypeManager;
RootNode = new YamlMappingNode();
TypeWriters = new()
TypeWriters = new Dictionary<Type, object>()
{
{typeof(IEntity), this},
{typeof(GridId), this},
{typeof(EntityUid), this}
};
TypeReaders = new()
TypeReaders = new Dictionary<(Type, Type), object>()
{
{(typeof(IEntity), typeof(ValueDataNode)), this},
{(typeof(GridId), typeof(ValueDataNode)), this},
@@ -309,13 +309,13 @@ namespace Robust.Server.Maps
RootNode = node;
TargetMap = targetMapId;
_prototypeManager = prototypeManager;
TypeWriters = new()
TypeWriters = new Dictionary<Type, object>()
{
{typeof(IEntity), this},
{typeof(GridId), this},
{typeof(EntityUid), this}
};
TypeReaders = new()
TypeReaders = new Dictionary<(Type, Type), object>()
{
{(typeof(IEntity), typeof(ValueDataNode)), this},
{(typeof(GridId), typeof(ValueDataNode)), this},

View File

@@ -1,7 +1,7 @@
using System.Collections.Generic;
using Robust.Shared.GameObjects;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
@@ -32,6 +32,7 @@ namespace Robust.Shared.Containers
/// <inheritdoc />
[ViewVariables(VVAccess.ReadWrite)]
[field: DataField("occludes")]
public bool OccludesLight { get; set; } = true;
/// <inheritdoc />
@@ -40,6 +41,7 @@ namespace Robust.Shared.Containers
/// <inheritdoc />
[ViewVariables(VVAccess.ReadWrite)]
[field: DataField("showEnts")]
public bool ShowContents { get; set; }
/// <summary>
@@ -165,13 +167,5 @@ namespace Robust.Shared.Containers
Manager.Owner.SendMessage(Manager, new ContainerContentsModifiedMessage(this, toremove, true));
Manager.Dirty();
}
/// <inheritdoc />
public virtual void ExposeData(ObjectSerializer serializer)
{
// ID and Manager are filled in Initialize
serializer.DataReadWriteFunction("showEnts", false, value => ShowContents = value, () => ShowContents);
serializer.DataReadWriteFunction("occludes", true, value => OccludesLight = value, () => OccludesLight);
}
}
}

View File

@@ -1,9 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Robust.Shared.Containers
{
@@ -22,37 +21,14 @@ namespace Robust.Shared.Containers
/// <summary>
/// The generic container class uses a list of entities
/// </summary>
private List<IEntity> _containerList = new();
[DataField("ents")]
private readonly List<IEntity> _containerList = new();
/// <inheritdoc />
public override IReadOnlyList<IEntity> ContainedEntities => _containerList;
/// <inheritdoc />
public override string ContainerType => ClassName;
/// <inheritdoc />
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
#if SERV3
// ONLY PAUL CAN MAKE ME WHOLE
serializer.DataField(ref _containerList, "ents", new List<IEntity>());
#else
if (serializer.Writing)
{
serializer.DataWriteFunction("ents", new List<EntityUid>(),
() => _containerList.Select(e => e.Uid).ToList());
}
else
{
var entMan = IoCManager.Resolve<IEntityManager>();
serializer.DataReadFunction("ents", new List<EntityUid>(),
value => _containerList = value.Select((uid => entMan.GetEntity(uid))).ToList());
}
#endif
}
/// <inheritdoc />
protected override void InternalInsert(IEntity toinsert)

View File

@@ -7,6 +7,8 @@ using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Players;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Robust.Shared.Containers
@@ -20,8 +22,10 @@ namespace Robust.Shared.Containers
{
[Dependency] private readonly IRobustSerializer _serializer = default!;
[Dependency] private readonly IDynamicTypeFactoryInternal _dynFactory = default!;
[ViewVariables] private Dictionary<string, IContainer> _containers = new();
[ViewVariables]
[DataField("containers")]
private Dictionary<string, IContainer> _containers = new();
/// <inheritdoc />
public sealed override string Name => "ContainerContainer";
@@ -81,7 +85,7 @@ namespace Robust.Shared.Containers
_containers.Remove(dead);
}
}
// Add new containers and update existing contents.
foreach (var (containerType, id, showEnts, occludesLight, entityUids) in cast.ContainerSet)
@@ -135,13 +139,6 @@ namespace Robust.Shared.Containers
newContainer.Manager = this;
return newContainer;
}
/// <inheritdoc />
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _containers, "containers", new Dictionary<string, IContainer>());
}
/// <inheritdoc />
public override ComponentState GetComponentState(ICommonSession player)
@@ -319,9 +316,13 @@ namespace Robust.Shared.Containers
}
}
private struct ContainerPrototypeData : IExposeData
[DataDefinition]
private struct ContainerPrototypeData : IPopulateDefaultValues
{
[DataField("entities")]
public List<EntityUid> Entities;
[DataField("type")]
public string? Type;
public ContainerPrototypeData(List<EntityUid> entities, string type)
@@ -330,10 +331,9 @@ namespace Robust.Shared.Containers
Type = type;
}
void IExposeData.ExposeData(ObjectSerializer serializer)
public void PopulateDefaultValues()
{
serializer.DataField(ref Entities, "entities", new List<EntityUid>());
serializer.DataField(ref Type, "type", null);
Entities = new List<EntityUid>();
}
}

View File

@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Robust.Shared.Containers
@@ -15,8 +14,6 @@ namespace Robust.Shared.Containers
{
private const string ClassName = "ContainerSlot";
private IEntity? _containedEntity;
/// <inheritdoc />
public override IReadOnlyList<IEntity> ContainedEntities
{
@@ -29,38 +26,11 @@ namespace Robust.Shared.Containers
}
[ViewVariables]
public IEntity? ContainedEntity
{
get => _containedEntity;
private set => _containedEntity = value;
}
[field: DataField("ent")]
public IEntity? ContainedEntity { get; private set; }
/// <inheritdoc />
public override string ContainerType => ClassName;
/// <inheritdoc />
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
#if SERV3
// ONLY PAUL CAN MAKE ME WHOLE
serializer.DataField(ref _containedEntity, "ent", default);
#else
if (serializer.Writing)
{
serializer.DataWriteFunction("ents", EntityUid.Invalid,
() => _containedEntity?.Uid ?? EntityUid.Invalid);
}
else
{
var entMan = IoCManager.Resolve<IEntityManager>();
serializer.DataReadFunction("ent", EntityUid.Invalid,
value => _containedEntity = value != EntityUid.Invalid ? entMan.GetEntity(value) : null);
}
#endif
}
/// <inheritdoc />
public override bool CanInsert(IEntity toinsert)

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Robust.Shared.Containers
{
@@ -25,7 +26,8 @@ namespace Robust.Shared.Containers
/// </remarks>
/// <seealso cref="IContainerManager" />
[PublicAPI]
public interface IContainer : IExposeData
[ImplicitDataDefinitionForInheritors]
public interface IContainer
{
/// <summary>
/// Readonly collection of all the entities contained within this specific container

View File

@@ -1,8 +1,12 @@
using System;
using JetBrains.Annotations;
namespace Robust.Shared.Serialization.Manager.Attributes
{
[MeansDataDefinition]
[MeansImplicitUse]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false)]
public class DataDefinition : Attribute{ }
public class DataDefinition : Attribute
{
}
}

View File

@@ -1,5 +1,6 @@
using System;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Result;
using Robust.Shared.Serialization.Markdown;
using Robust.Shared.Serialization.Markdown.Validation;
@@ -8,6 +9,10 @@ namespace Robust.Shared.Serialization.Manager
{
public interface ISerializationManager
{
IComponentFactory ComponentFactory { get; }
IEntityManager EntityManager { get; }
#region Serialization
/// <summary>

View File

@@ -37,6 +37,6 @@ namespace Robust.Shared.Serialization.Manager.Result
public abstract class DeserializationResult<T> : DeserializationResult
{
public abstract T Value { get; }
public abstract T? Value { get; }
}
}

View File

@@ -28,7 +28,7 @@ namespace Robust.Shared.Serialization.Manager.Result
foreach (var oldRes in sourceCollection.Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<T>>();
valueList.Add(newRes.Value);
valueList.Add(newRes.Value!);
resList.Add(newRes);
}
}
@@ -38,7 +38,7 @@ namespace Robust.Shared.Serialization.Manager.Result
foreach (var oldRes in Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<T>>();
valueList.Add(newRes.Value);
valueList.Add(newRes.Value!);
resList.Add(newRes);
}
}
@@ -54,7 +54,7 @@ namespace Robust.Shared.Serialization.Manager.Result
foreach (var oldRes in Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<T>>();
valueList.Add(newRes.Value);
valueList.Add(newRes.Value!);
resList.Add(newRes);
}

View File

@@ -28,7 +28,7 @@ namespace Robust.Shared.Serialization.Manager.Result
foreach (var oldRes in sourceCollection.Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<T>>();
valueSet.Add(newRes.Value);
valueSet.Add(newRes.Value!);
resList.Add(newRes);
}
}
@@ -38,7 +38,7 @@ namespace Robust.Shared.Serialization.Manager.Result
foreach (var oldRes in Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<T>>();
valueSet.Add(newRes.Value);
valueSet.Add(newRes.Value!);
resList.Add(newRes);
}
}
@@ -54,7 +54,7 @@ namespace Robust.Shared.Serialization.Manager.Result
foreach (var oldRes in Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<T>>();
valueSet.Add(newRes.Value);
valueSet.Add(newRes.Value!);
resList.Add(newRes);
}

View File

@@ -2,15 +2,16 @@
namespace Robust.Shared.Serialization.Manager.Result
{
public class DeserializedMutableCollection<T, E> : DeserializationResult<T> where T : class, ICollection<E>, new()
public class DeserializedMutableCollection<TCollection, TElement> : DeserializationResult<TCollection>
where TCollection : class, ICollection<TElement>, new()
{
public DeserializedMutableCollection(T? value, IEnumerable<DeserializationResult> mappings)
public DeserializedMutableCollection(TCollection? value, IEnumerable<DeserializationResult> mappings)
{
Value = value;
Mappings = mappings;
}
public override T? Value { get; }
public override TCollection? Value { get; }
public IEnumerable<DeserializationResult> Mappings { get; }
@@ -18,16 +19,16 @@ namespace Robust.Shared.Serialization.Manager.Result
public override DeserializationResult PushInheritanceFrom(DeserializationResult source)
{
var sourceCollection = source.Cast<DeserializedMutableCollection<T, E>>();
var valueList = new T();
var sourceCollection = source.Cast<DeserializedMutableCollection<TCollection, TElement>>();
var valueList = new TCollection();
var resList = new List<DeserializationResult>();
if (sourceCollection.Value != null)
{
foreach (var oldRes in sourceCollection.Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<E>>();
valueList.Add(newRes.Value);
var newRes = oldRes.Copy().Cast<DeserializationResult<TElement>>();
valueList.Add(newRes.Value!);
resList.Add(newRes);
}
}
@@ -36,27 +37,27 @@ namespace Robust.Shared.Serialization.Manager.Result
{
foreach (var oldRes in Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<E>>();
valueList.Add(newRes.Value);
var newRes = oldRes.Copy().Cast<DeserializationResult<TElement>>();
valueList.Add(newRes.Value!);
resList.Add(newRes);
}
}
return new DeserializedMutableCollection<T, E>(valueList, resList);
return new DeserializedMutableCollection<TCollection, TElement>(valueList, resList);
}
public override DeserializationResult Copy()
{
var valueList = new T();
var valueList = new TCollection();
var resList = new List<DeserializationResult>();
foreach (var oldRes in Mappings)
{
var newRes = oldRes.Copy();
valueList.Add((E) newRes.RawValue!);
valueList.Add((TElement) newRes.RawValue!);
resList.Add(newRes);
}
return new DeserializedMutableCollection<T, E>(Value == null ? null : valueList, resList);
return new DeserializedMutableCollection<TCollection, TElement>(Value == null ? null : valueList, resList);
}
}
}

View File

@@ -35,7 +35,7 @@ namespace Robust.Shared.Serialization.Manager.Result
foreach (var oldRes in sourceCollection.Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<TElement>>();
valueList.Add(newRes.Value);
valueList.Add(newRes.Value!);
resList.Add(newRes);
}
}
@@ -45,7 +45,7 @@ namespace Robust.Shared.Serialization.Manager.Result
foreach (var oldRes in Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<TElement>>();
valueList.Add(newRes.Value);
valueList.Add(newRes.Value!);
resList.Add(newRes);
}
}
@@ -61,7 +61,7 @@ namespace Robust.Shared.Serialization.Manager.Result
foreach (var oldRes in Mappings)
{
var newRes = oldRes.Copy().Cast<DeserializationResult<TElement>>();
valueList.Add(newRes.Value);
valueList.Add(newRes.Value!);
resList.Add(newRes);
}

View File

@@ -2,12 +2,12 @@
{
public class DeserializedValue<T> : DeserializationResult<T>
{
public DeserializedValue(T value)
public DeserializedValue(T? value)
{
Value = value;
}
public override T Value { get; }
public override T? Value { get; }
public override object? RawValue => Value;

View File

@@ -7,6 +7,7 @@ using System.Linq;
using System.Reflection;
using System.Text;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Reflection;
@@ -21,6 +22,8 @@ namespace Robust.Shared.Serialization.Manager
public partial class SerializationManager : ISerializationManager
{
[Dependency] private readonly IReflectionManager _reflectionManager = default!;
[field: Dependency] public IComponentFactory ComponentFactory { get; } = default!;
[field: Dependency] public IEntityManager EntityManager { get; } = default!;
public const string LogCategory = "serialization";
@@ -168,7 +171,6 @@ namespace Robust.Shared.Serialization.Manager
{
return new ErrorNode(node);
}
}
if (TryValidateWithTypeReader(type, node, context, out var valid)) return valid;

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Manager.Attributes;
@@ -22,7 +21,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
bool skipHook,
ISerializationContext? context = null)
{
var factory = IoCManager.Resolve<IComponentFactory>();
var factory = serializationManager.ComponentFactory;
var components = new ComponentRegistry();
var mappings = new Dictionary<DeserializationResult, DeserializationResult>();
@@ -83,7 +82,7 @@ namespace Robust.Shared.Serialization.TypeSerializers
public ValidatedNode Validate(ISerializationManager serializationManager, SequenceDataNode node,
ISerializationContext? context = null)
{
var factory = IoCManager.Resolve<IComponentFactory>();
var factory = serializationManager.ComponentFactory;
var components = new ComponentRegistry();
var list = new List<ValidatedNode>();

View File

@@ -0,0 +1,48 @@
using Robust.Shared.GameObjects;
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;
namespace Robust.Shared.Serialization.TypeSerializers
{
[TypeSerializer]
public class EntitySerializer : ITypeReaderWriter<IEntity, ValueDataNode>
{
public DeserializationResult Read(ISerializationManager serializationManager, ValueDataNode node, bool skipHook,
ISerializationContext? context = null)
{
if (!EntityUid.TryParse(node.Value, out var uid))
{
throw new InvalidMappingException($"{node.Value} is not a valid entity uid.");
}
if (!uid.IsValid())
{
return new DeserializedValue<IEntity>(null);
}
var entity = serializationManager.EntityManager.GetEntity(uid);
// TODO Paul what type to return here
return new DeserializedValue<IEntity>(entity);
}
public ValidatedNode Validate(ISerializationManager serializationManager, ValueDataNode node,
ISerializationContext? context = null)
{
return EntityUid.TryParse(node.Value, out var uid) &&
uid.IsValid() &&
serializationManager.EntityManager.EntityExists(uid)
? new ValidatedValueNode(node)
: new ErrorNode(node);
}
public DataNode Write(ISerializationManager serializationManager, IEntity value, bool alwaysWrite = false,
ISerializationContext? context = null)
{
return new ValueDataNode(value.Uid.ToString());
}
}
}