ConstructionActionValid.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Text;
  2. using Content.Server.Construction.Completions;
  3. using Content.Shared.Construction;
  4. using Content.Shared.Construction.Prototypes;
  5. using Robust.Shared.Prototypes;
  6. namespace Content.IntegrationTests.Tests.Construction
  7. {
  8. [TestFixture]
  9. public sealed class ConstructionActionValid
  10. {
  11. private bool IsValid(IGraphAction action, IPrototypeManager protoMan, out string prototype)
  12. {
  13. switch (action)
  14. {
  15. case SpawnPrototype spawn:
  16. prototype = spawn.Prototype;
  17. return protoMan.TryIndex<EntityPrototype>(spawn.Prototype, out _);
  18. case SpawnPrototypeAtContainer spawn:
  19. prototype = spawn.Prototype;
  20. return protoMan.TryIndex<EntityPrototype>(spawn.Prototype, out _);
  21. case ConditionalAction conditional:
  22. var valid = IsValid(conditional.Action, protoMan, out var protoA) & IsValid(conditional.Else, protoMan, out var protoB);
  23. if (!string.IsNullOrEmpty(protoA) && string.IsNullOrEmpty(protoB))
  24. {
  25. prototype = protoA;
  26. }
  27. else if (string.IsNullOrEmpty(protoA) && !string.IsNullOrEmpty(protoB))
  28. {
  29. prototype = protoB;
  30. }
  31. else
  32. {
  33. prototype = $"{protoA}, {protoB}";
  34. }
  35. return valid;
  36. default:
  37. prototype = string.Empty;
  38. return true;
  39. }
  40. }
  41. [Test]
  42. public async Task ConstructionGraphSpawnPrototypeValid()
  43. {
  44. await using var pair = await PoolManager.GetServerClient();
  45. var server = pair.Server;
  46. var protoMan = server.ResolveDependency<IPrototypeManager>();
  47. var valid = true;
  48. var message = new StringBuilder();
  49. await server.WaitPost(() =>
  50. {
  51. foreach (var graph in protoMan.EnumeratePrototypes<ConstructionGraphPrototype>())
  52. {
  53. foreach (var node in graph.Nodes.Values)
  54. {
  55. foreach (var action in node.Actions)
  56. {
  57. if (IsValid(action, protoMan, out var prototype)) continue;
  58. valid = false;
  59. message.Append($"Invalid entity prototype \"{prototype}\" on graph action in node \"{node.Name}\" of graph \"{graph.ID}\"\n");
  60. }
  61. foreach (var edge in node.Edges)
  62. {
  63. foreach (var action in edge.Completed)
  64. {
  65. if (IsValid(action, protoMan, out var prototype)) continue;
  66. valid = false;
  67. message.Append($"Invalid entity prototype \"{prototype}\" on graph action in edge \"{edge.Target}\" of node \"{node.Name}\" of graph \"{graph.ID}\"\n");
  68. }
  69. }
  70. }
  71. }
  72. });
  73. Assert.That(valid, Is.True, $"One or more SpawnPrototype actions specified invalid entity prototypes!\n{message}");
  74. await pair.CleanReturnAsync();
  75. }
  76. [Test]
  77. public async Task ConstructionGraphEdgeValid()
  78. {
  79. await using var pair = await PoolManager.GetServerClient();
  80. var server = pair.Server;
  81. var protoMan = server.ResolveDependency<IPrototypeManager>();
  82. var valid = true;
  83. var message = new StringBuilder();
  84. await server.WaitPost(() =>
  85. {
  86. foreach (var graph in protoMan.EnumeratePrototypes<ConstructionGraphPrototype>())
  87. {
  88. foreach (var node in graph.Nodes.Values)
  89. {
  90. foreach (var edge in node.Edges)
  91. {
  92. if (graph.Nodes.ContainsKey(edge.Target))
  93. continue;
  94. valid = false;
  95. message.Append(
  96. $"Invalid target \"{edge.Target}\" in edge on node \"{node.Name}\" of graph \"{graph.ID}\"\n");
  97. }
  98. }
  99. }
  100. });
  101. Assert.That(valid, Is.True, $"One or more edges specified invalid node targets!\n{message}");
  102. await pair.CleanReturnAsync();
  103. }
  104. }
  105. }