nullable parts in body prototypes (#12935)

Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
This commit is contained in:
Nemanja
2022-12-09 20:07:09 -05:00
committed by GitHub
parent 4a17d2cb33
commit 45b72d4852
4 changed files with 25 additions and 25 deletions

View File

@@ -1,4 +1,5 @@
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Shared.Body.Prototypes;
@@ -34,18 +35,19 @@ public sealed class BodyPrototype : IPrototype
[DataRecord]
public sealed record BodyPrototypeSlot
{
[DataField("part", required: true)] public readonly string Part = default!;
[DataField("part", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
public readonly string? Part;
public readonly HashSet<string> Connections = new();
public readonly Dictionary<string, string> Organs = new();
public BodyPrototypeSlot(string part, HashSet<string>? connections, Dictionary<string, string>? organs)
public BodyPrototypeSlot(string? part, HashSet<string>? connections, Dictionary<string, string>? organs)
{
Part = part;
Connections = connections ?? new HashSet<string>();
Organs = organs ?? new Dictionary<string, string>();
}
public void Deconstruct(out string part, out HashSet<string> connections, out Dictionary<string, string> organs)
public void Deconstruct(out string? part, out HashSet<string> connections, out Dictionary<string, string> organs)
{
part = Part;
connections = Connections;

View File

@@ -14,18 +14,10 @@ namespace Content.Shared.Body.Prototypes;
[TypeSerializer]
public sealed class BodyPrototypeSerializer : ITypeReader<BodyPrototype, MappingDataNode>
{
private (ValidationNode Node, List<string> Connections) ValidateSlot(ISerializationManager serializationManager, MappingDataNode slot, string slotId, IDependencyCollection dependencies)
private (ValidationNode Node, List<string> Connections) ValidateSlot(MappingDataNode slot, IDependencyCollection dependencies)
{
var nodes = new List<ValidationNode>();
var prototypes = dependencies.Resolve<IPrototypeManager>();
if (!slot.TryGet("part", out ValueDataNode? part))
{
nodes.Add(new ErrorNode(slot, $"No part value data node found in root slot {slotId}"));
}
else if (!prototypes.HasIndex<EntityPrototype>(part.Value))
{
nodes.Add(new ErrorNode(slot, $"No entity prototype found with id {part.Value} for root slot {slotId}"));
}
var connections = new List<string>();
if (slot.TryGet("connections", out SequenceDataNode? connectionsNode))
@@ -79,8 +71,6 @@ public sealed class BodyPrototypeSerializer : ITypeReader<BodyPrototype, Mapping
IDependencyCollection dependencies, ISerializationContext? context = null)
{
var nodes = new List<ValidationNode>();
if (!node.TryGet("id", out ValueDataNode? id))
nodes.Add(new ErrorNode(node, "No id value data node found"));
if (!node.TryGet("root", out ValueDataNode? root))
nodes.Add(new ErrorNode(node, $"No root value data node found"));
@@ -111,7 +101,7 @@ public sealed class BodyPrototypeSerializer : ITypeReader<BodyPrototype, Mapping
continue;
}
var result = ValidateSlot(serializationManager, slot, root.Value, dependencies);
var result = ValidateSlot(slot, dependencies);
nodes.Add(result.Node);
foreach (var connection in result.Connections)
@@ -134,13 +124,18 @@ public sealed class BodyPrototypeSerializer : ITypeReader<BodyPrototype, Mapping
var name = node.Get<ValueDataNode>("name").Value;
var root = node.Get<ValueDataNode>("root").Value;
var slotNodes = node.Get<MappingDataNode>("slots");
var allConnections = new Dictionary<string, (string Part, HashSet<string>? Connections, Dictionary<string, string>? Organs)>();
var allConnections = new Dictionary<string, (string? Part, HashSet<string>? Connections, Dictionary<string, string>? Organs)>();
foreach (var (keyNode, valueNode) in slotNodes)
{
var slotId = ((ValueDataNode) keyNode).Value;
var slot = ((MappingDataNode) valueNode);
var part = slot.Get<ValueDataNode>("part").Value;
string? part = null;
if (slot.TryGet<ValueDataNode>("part", out var value))
{
part = value.Value;
}
HashSet<string>? connections = null;
if (slot.TryGet("connections", out SequenceDataNode? slotConnectionsNode))

View File

@@ -88,6 +88,9 @@ public partial class SharedBodySystem
foreach (var connection in connections)
{
var childSlot = prototype.Slots[connection];
if (childSlot.Part == null)
continue;
var childPart = Spawn(childSlot.Part, coordinates);
var childPartComponent = Comp<BodyPartComponent>(childPart);
var slot = CreatePartSlot(connection, parent.Owner, childPartComponent.PartType, parent);