Log better error for incorrect Construction Graphs.

Also improves integration tests for them to catch more errors.
This commit is contained in:
Vera Aguilera Puerto
2022-03-04 16:53:06 +01:00
parent 0b0de4fe10
commit 37fb2bd3c7
2 changed files with 20 additions and 5 deletions

View File

@@ -1,4 +1,6 @@
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Content.Server.Construction.Components;
using Content.Shared.Construction.Prototypes; using Content.Shared.Construction.Prototypes;
using NUnit.Framework; using NUnit.Framework;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
@@ -8,6 +10,9 @@ namespace Content.IntegrationTests.Tests.Construction
[TestFixture] [TestFixture]
public sealed class ConstructionPrototypeTest : ContentIntegrationTest public sealed class ConstructionPrototypeTest : ContentIntegrationTest
{ {
// discount linter for construction graphs
// TODO: Create serialization validators for these?
[Test] [Test]
public async Task TestStartIsValid() public async Task TestStartIsValid()
{ {
@@ -45,7 +50,7 @@ namespace Content.IntegrationTests.Tests.Construction
} }
[Test] [Test]
public async Task TestStartReachesTarget() public async Task TestStartReachesValidTarget()
{ {
var server = StartServer(); var server = StartServer();
@@ -58,7 +63,12 @@ namespace Content.IntegrationTests.Tests.Construction
var start = proto.StartNode; var start = proto.StartNode;
var target = proto.TargetNode; var target = proto.TargetNode;
var graph = protoMan.Index<ConstructionGraphPrototype>(proto.Graph); var graph = protoMan.Index<ConstructionGraphPrototype>(proto.Graph);
Assert.That(graph.TryPath(start, target, out _), $"Unable to find path from \"{start}\" to \"{target}\" on graph \"{graph.ID}\""); Assert.That(graph.TryPath(start, target, out var path), $"Unable to find path from \"{start}\" to \"{target}\" on graph \"{graph.ID}\"");
Assert.That(path!.Length, Is.GreaterThanOrEqualTo(1), $"Unable to find path from \"{start}\" to \"{target}\" on graph \"{graph.ID}\".");
var next = path[0];
Assert.That(next.Entity, Is.Not.Null, $"The next node ({next.Name}) in the path from the start node ({start}) to the target node ({target}) must specify an entity! Graph: {graph.ID}");
Assert.That(protoMan.TryIndex(next.Entity, out EntityPrototype entity), $"The next node ({next.Name}) in the path from the start node ({start}) to the target node ({target}) specified an invalid entity prototype ({next.Entity})");
Assert.That(entity.Components.ContainsKey("Construction"), $"The next node ({next.Name}) in the path from the start node ({start}) to the target node ({target}) specified an entity prototype ({next.Entity}) without a ConstructionComponent.");
} }
} }
} }

View File

@@ -234,10 +234,15 @@ namespace Content.Server.Construction
return null; return null;
} }
var newEntity = EntityManager.SpawnEntity(graph.Nodes[edge.Target].Entity, EntityManager.GetComponent<TransformComponent>(user).Coordinates); var newEntityProto = graph.Nodes[edge.Target].Entity;
var newEntity = EntityManager.SpawnEntity(newEntityProto, EntityManager.GetComponent<TransformComponent>(user).Coordinates);
// Yes, this should throw if it's missing the component. if (!TryComp(newEntity, out ConstructionComponent? construction))
var construction = EntityManager.GetComponent<ConstructionComponent>(newEntity); {
_sawmill.Error($"Initial construction does not have a valid target entity! It is missing a ConstructionComponent.\nGraph: {graph.ID}, Initial Target: {edge.Target}, Ent. Prototype: {newEntityProto}\nCreated Entity {ToPrettyString(newEntity)} will be deleted.");
Del(newEntity); // Screw you, make proper construction graphs.
return null;
}
// We attempt to set the pathfinding target. // We attempt to set the pathfinding target.
SetPathfindingTarget(newEntity, targetNode.Name, construction); SetPathfindingTarget(newEntity, targetNode.Name, construction);