Construction graph improvements (#17960)
This commit is contained in:
committed by
GitHub
parent
fbf1d476f2
commit
9243050e1a
@@ -1,4 +1,6 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Content.Shared.Construction.NodeEntities;
|
||||
using Content.Shared.Construction.Serialization;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
@@ -23,8 +25,11 @@ namespace Content.Shared.Construction
|
||||
[ViewVariables]
|
||||
public IReadOnlyList<IGraphAction> Actions => _actions;
|
||||
|
||||
[DataField("entity", customTypeSerializer:typeof(PrototypeIdSerializer<EntityPrototype>))]
|
||||
public string? Entity { get; private set; }
|
||||
[DataField("transform")]
|
||||
public IGraphTransform[] TransformLogic = Array.Empty<IGraphTransform>();
|
||||
|
||||
[DataField("entity", customTypeSerializer: typeof(GraphNodeEntitySerializer), serverOnly:true)]
|
||||
public IGraphNodeEntity Entity { get; private set; } = new NullNodeEntity();
|
||||
|
||||
public ConstructionGraphEdge? GetEdge(string target)
|
||||
{
|
||||
|
||||
29
Content.Shared/Construction/IGraphNodeEntity.cs
Normal file
29
Content.Shared/Construction/IGraphNodeEntity.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Shared.Construction;
|
||||
|
||||
public interface IGraphNodeEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the <see cref="EntityPrototype"/> ID for a node, given the <see cref="EntityUid"/> of both the
|
||||
/// construction entity and the user entity.
|
||||
/// If the construction entity is null, then we are dealing with a "start construction" for an entity that
|
||||
/// does not exist yet.
|
||||
/// If the user entity is null, this node was reached through means other some sort of "user interaction".
|
||||
/// </summary>
|
||||
/// <param name="uid">Uid of the construction entity.</param>
|
||||
/// <param name="userUid">Uid of the user that caused the transition to the node.</param>
|
||||
/// <param name="args">Arguments with useful instances, etc.</param>
|
||||
/// <returns></returns>
|
||||
public string? GetId(EntityUid? uid, EntityUid? userUid, GraphNodeEntityArgs args);
|
||||
}
|
||||
|
||||
public readonly struct GraphNodeEntityArgs
|
||||
{
|
||||
public readonly IEntityManager EntityManager;
|
||||
|
||||
public GraphNodeEntityArgs(IEntityManager entityManager)
|
||||
{
|
||||
EntityManager = entityManager;
|
||||
}
|
||||
}
|
||||
16
Content.Shared/Construction/IGraphTransform.cs
Normal file
16
Content.Shared/Construction/IGraphTransform.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace Content.Shared.Construction;
|
||||
|
||||
public interface IGraphTransform
|
||||
{
|
||||
public void Transform(EntityUid oldUid, EntityUid newUid, EntityUid? userUid, GraphTransformArgs args);
|
||||
}
|
||||
|
||||
public readonly struct GraphTransformArgs
|
||||
{
|
||||
public readonly IEntityManager EntityManager;
|
||||
|
||||
public GraphTransformArgs(IEntityManager entityManager)
|
||||
{
|
||||
EntityManager = entityManager;
|
||||
}
|
||||
}
|
||||
13
Content.Shared/Construction/NodeEntities/NullNodeEntity.cs
Normal file
13
Content.Shared/Construction/NodeEntities/NullNodeEntity.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Content.Shared.Construction.NodeEntities;
|
||||
|
||||
[UsedImplicitly]
|
||||
[DataDefinition]
|
||||
public sealed class NullNodeEntity : IGraphNodeEntity
|
||||
{
|
||||
public string? GetId(EntityUid? uid, EntityUid? userUid, GraphNodeEntityArgs args)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
28
Content.Shared/Construction/NodeEntities/StaticNodeEntity.cs
Normal file
28
Content.Shared/Construction/NodeEntities/StaticNodeEntity.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Shared.Construction.NodeEntities;
|
||||
|
||||
[UsedImplicitly]
|
||||
[DataDefinition]
|
||||
public sealed class StaticNodeEntity : IGraphNodeEntity
|
||||
{
|
||||
[DataField("id", customTypeSerializer:typeof(PrototypeIdSerializer<EntityPrototype>))]
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public string? Id { get; }
|
||||
|
||||
public StaticNodeEntity()
|
||||
{
|
||||
}
|
||||
|
||||
public StaticNodeEntity(string id)
|
||||
{
|
||||
Id = id;
|
||||
}
|
||||
|
||||
public string? GetId(EntityUid? uid, EntityUid? userUid, GraphNodeEntityArgs args)
|
||||
{
|
||||
return Id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using Content.Shared.Construction.NodeEntities;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager;
|
||||
using Robust.Shared.Serialization.Markdown;
|
||||
using Robust.Shared.Serialization.Markdown.Mapping;
|
||||
using Robust.Shared.Serialization.Markdown.Validation;
|
||||
using Robust.Shared.Serialization.Markdown.Value;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Interfaces;
|
||||
|
||||
namespace Content.Shared.Construction.Serialization;
|
||||
|
||||
public sealed class GraphNodeEntitySerializer : ITypeSerializer<IGraphNodeEntity, ValueDataNode>, ITypeSerializer<IGraphNodeEntity, MappingDataNode>
|
||||
{
|
||||
public ValidationNode Validate(ISerializationManager serializationManager, ValueDataNode node,
|
||||
IDependencyCollection dependencies, ISerializationContext? context = null)
|
||||
{
|
||||
var id = node.Value;
|
||||
|
||||
var protoMan = dependencies.Resolve<IPrototypeManager>();
|
||||
|
||||
if (!protoMan.HasIndex<EntityPrototype>(id))
|
||||
{
|
||||
return new ErrorNode(node, $"Entity Prototype {id} was not found!");
|
||||
}
|
||||
|
||||
return new ValidatedValueNode(node);
|
||||
}
|
||||
|
||||
public IGraphNodeEntity Read(ISerializationManager serializationManager, ValueDataNode node,
|
||||
IDependencyCollection dependencies, SerializationHookContext hookCtx, ISerializationContext? context = null, ISerializationManager.InstantiationDelegate<IGraphNodeEntity>? instanceProvider = null)
|
||||
{
|
||||
return new StaticNodeEntity(node.Value);
|
||||
}
|
||||
|
||||
public ValidationNode Validate(ISerializationManager serializationManager, MappingDataNode node,
|
||||
IDependencyCollection dependencies, ISerializationContext? context = null)
|
||||
{
|
||||
return serializationManager.ValidateNode<IGraphNodeEntity>(node, context);
|
||||
}
|
||||
|
||||
public IGraphNodeEntity Read(ISerializationManager serializationManager, MappingDataNode node,
|
||||
IDependencyCollection dependencies, SerializationHookContext hookCtx, ISerializationContext? context = null, ISerializationManager.InstantiationDelegate<IGraphNodeEntity>? instanceProvider = null)
|
||||
{
|
||||
return serializationManager.Read(node, hookCtx, context, instanceProvider, false);
|
||||
}
|
||||
|
||||
public DataNode Write(ISerializationManager serializationManager, IGraphNodeEntity value, IDependencyCollection dependencies,
|
||||
bool alwaysWrite = false, ISerializationContext? context = null)
|
||||
{
|
||||
return serializationManager.WriteValue(value, alwaysWrite, context, false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user