From fc4eb664e28c360f00a615bea5f05e58e630a983 Mon Sep 17 00:00:00 2001
From: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com>
Date: Sun, 5 Jun 2022 10:13:53 +1200
Subject: [PATCH] Make TryGetComponent output nullable (#2905)
* Make TryGetComponent output nullable
* figs
* ah. there are more. Why do you fail me VS
---
.../Console/Commands/PlayerCommands.cs | 2 +-
Robust.Server/Console/Commands/SpinCommand.cs | 2 +-
.../GameObjects/EntitySystems/ActorSystem.cs | 2 +-
.../GameObjects/EntityManager.Components.cs | 8 +++---
Robust.Shared/GameObjects/EntityManager.cs | 2 +-
.../GameObjects/IEntityManager.Components.cs | 4 +--
.../Map/MapManager.GridCollection.cs | 4 +--
Robust.Shared/Physics/SharedJointSystem.cs | 21 +++++++++++----
.../Shared/Map/GridFixtures_Tests.cs | 8 +++---
.../Shared/Physics/MapVelocity_Test.cs | 26 +++++++++----------
10 files changed, 45 insertions(+), 34 deletions(-)
diff --git a/Robust.Server/Console/Commands/PlayerCommands.cs b/Robust.Server/Console/Commands/PlayerCommands.cs
index e722532118..4df7f68cbc 100644
--- a/Robust.Server/Console/Commands/PlayerCommands.cs
+++ b/Robust.Server/Console/Commands/PlayerCommands.cs
@@ -100,7 +100,7 @@ namespace Robust.Server.Console.Commands
return;
}
- if (!entMan.TryGetComponent(player.AttachedEntity, out TransformComponent playerTransform))
+ if (!entMan.TryGetComponent(player.AttachedEntity, out TransformComponent? playerTransform))
{
shell.WriteError("You don't have an entity.");
return;
diff --git a/Robust.Server/Console/Commands/SpinCommand.cs b/Robust.Server/Console/Commands/SpinCommand.cs
index 5d0be457f7..c6cf5b1f8b 100644
--- a/Robust.Server/Console/Commands/SpinCommand.cs
+++ b/Robust.Server/Console/Commands/SpinCommand.cs
@@ -50,7 +50,7 @@ public sealed class SpinCommand : IConsoleCommand
}
// Try get physics
- if (!entMan.TryGetComponent(target, out PhysicsComponent physics))
+ if (!entMan.TryGetComponent(target, out PhysicsComponent? physics))
{
shell.WriteError($"Target entity is incorporeal");
return;
diff --git a/Robust.Server/GameObjects/EntitySystems/ActorSystem.cs b/Robust.Server/GameObjects/EntitySystems/ActorSystem.cs
index e50563e98d..3f421464fe 100644
--- a/Robust.Server/GameObjects/EntitySystems/ActorSystem.cs
+++ b/Robust.Server/GameObjects/EntitySystems/ActorSystem.cs
@@ -57,7 +57,7 @@ namespace Robust.Server.GameObjects
}
// Check if there was a player attached to the entity already...
- if (EntityManager.TryGetComponent(uid, out ActorComponent actor))
+ if (EntityManager.TryGetComponent(uid, out ActorComponent? actor))
{
// If we're not forcing the attach, this fails.
if (!force)
diff --git a/Robust.Shared/GameObjects/EntityManager.Components.cs b/Robust.Shared/GameObjects/EntityManager.Components.cs
index 9c6fd687e7..7bbfbd2b53 100644
--- a/Robust.Shared/GameObjects/EntityManager.Components.cs
+++ b/Robust.Shared/GameObjects/EntityManager.Components.cs
@@ -642,7 +642,7 @@ namespace Robust.Shared.GameObjects
}
///
- public bool TryGetComponent(EntityUid uid, [NotNullWhen(true)] out T component)
+ public bool TryGetComponent(EntityUid uid, [NotNullWhen(true)] out T? component)
{
var dict = _entTraitArray[CompIdx.ArrayIndex()];
if (dict.TryGetValue(uid, out var comp))
@@ -654,12 +654,12 @@ namespace Robust.Shared.GameObjects
}
}
- component = default!;
+ component = default;
return false;
}
///
- public bool TryGetComponent([NotNullWhen(true)] EntityUid? uid, [NotNullWhen(true)] out T component)
+ public bool TryGetComponent([NotNullWhen(true)] EntityUid? uid, [NotNullWhen(true)] out T? component)
{
if (!uid.HasValue)
{
@@ -676,7 +676,7 @@ namespace Robust.Shared.GameObjects
}
}
- component = default!;
+ component = default;
return false;
}
diff --git a/Robust.Shared/GameObjects/EntityManager.cs b/Robust.Shared/GameObjects/EntityManager.cs
index 679cd7a7a7..ed94c31d9e 100644
--- a/Robust.Shared/GameObjects/EntityManager.cs
+++ b/Robust.Shared/GameObjects/EntityManager.cs
@@ -290,7 +290,7 @@ namespace Robust.Shared.GameObjects
private void RecursiveDeleteEntity(EntityUid uid)
{
- if (!TryGetComponent(uid, out MetaDataComponent metadata) || metadata.EntityDeleted)
+ if (!TryGetComponent(uid, out MetaDataComponent? metadata) || metadata.EntityDeleted)
return; //TODO: Why was this still a child if it was already deleted?
var transform = GetComponent(uid);
diff --git a/Robust.Shared/GameObjects/IEntityManager.Components.cs b/Robust.Shared/GameObjects/IEntityManager.Components.cs
index 281c6bd334..035f66eaca 100644
--- a/Robust.Shared/GameObjects/IEntityManager.Components.cs
+++ b/Robust.Shared/GameObjects/IEntityManager.Components.cs
@@ -221,7 +221,7 @@ namespace Robust.Shared.GameObjects
/// Entity UID to check.
/// Component of the specified type (if exists).
/// If the component existed in the entity.
- bool TryGetComponent(EntityUid uid, [NotNullWhen(true)] out T component);
+ bool TryGetComponent(EntityUid uid, [NotNullWhen(true)] out T? component);
///
/// Returns the component of a specific type.
@@ -230,7 +230,7 @@ namespace Robust.Shared.GameObjects
/// Entity UID to check.
/// Component of the specified type (if exists).
/// If the component existed in the entity.
- bool TryGetComponent([NotNullWhen(true)] EntityUid? uid, [NotNullWhen(true)] out T component);
+ bool TryGetComponent([NotNullWhen(true)] EntityUid? uid, [NotNullWhen(true)] out T? component);
///
/// Returns the component of a specific type.
diff --git a/Robust.Shared/Map/MapManager.GridCollection.cs b/Robust.Shared/Map/MapManager.GridCollection.cs
index 92b347d273..f9c3ed5a3e 100644
--- a/Robust.Shared/Map/MapManager.GridCollection.cs
+++ b/Robust.Shared/Map/MapManager.GridCollection.cs
@@ -159,7 +159,7 @@ internal partial class MapManager
public bool TryGetGrid(EntityUid euid, [MaybeNullWhen(false)] out IMapGrid grid)
{
- if (EntityManager.TryGetComponent(euid, out IMapGridComponent comp))
+ if (EntityManager.TryGetComponent(euid, out IMapGridComponent? comp))
{
grid = comp.Grid;
return true;
@@ -231,7 +231,7 @@ internal partial class MapManager
}
var entityId = grid.GridEntityId;
- if (!EntityManager.TryGetComponent(entityId, out MetaDataComponent metaComp))
+ if (!EntityManager.TryGetComponent(entityId, out MetaDataComponent? metaComp))
{
DebugTools.Assert($"Calling {nameof(DeleteGrid)} with {gridId}, but there was no allocated entity.");
return; // Silently fail on release
diff --git a/Robust.Shared/Physics/SharedJointSystem.cs b/Robust.Shared/Physics/SharedJointSystem.cs
index 50a1397137..0fce4d7351 100644
--- a/Robust.Shared/Physics/SharedJointSystem.cs
+++ b/Robust.Shared/Physics/SharedJointSystem.cs
@@ -446,11 +446,22 @@ namespace Robust.Shared.Physics
FilterContactsForJoint(joint);
}
- var vera = new JointRemovedEvent(joint, bodyA, bodyB);
- EntityManager.EventBus.RaiseLocalEvent(bodyA.Owner, vera, false);
- var smug = new JointRemovedEvent(joint, bodyB, bodyA);
- EntityManager.EventBus.RaiseLocalEvent(bodyB.Owner, smug, false);
- EntityManager.EventBus.RaiseEvent(EventSource.Local, vera);
+ if (bodyA == null)
+ {
+ _sawmill.Debug($"Removing joint from entioty {ToPrettyString(bodyAUid)} without a physics component?");
+ }
+ else if (bodyB == null)
+ {
+ _sawmill.Debug($"Removing joint from entioty {ToPrettyString(bodyBUid)} without a physics component?");
+ }
+ else
+ {
+ var vera = new JointRemovedEvent(joint, bodyA, bodyB);
+ EntityManager.EventBus.RaiseLocalEvent(bodyA.Owner, vera, false);
+ var smug = new JointRemovedEvent(joint, bodyB, bodyA);
+ EntityManager.EventBus.RaiseLocalEvent(bodyB.Owner, smug, false);
+ EntityManager.EventBus.RaiseEvent(EventSource.Local, vera);
+ }
// We can't just check up front due to how prediction works.
_dirtyJoints.Add(jointComponentA);
diff --git a/Robust.UnitTesting/Shared/Map/GridFixtures_Tests.cs b/Robust.UnitTesting/Shared/Map/GridFixtures_Tests.cs
index 0550b7435f..e4229d7d66 100644
--- a/Robust.UnitTesting/Shared/Map/GridFixtures_Tests.cs
+++ b/Robust.UnitTesting/Shared/Map/GridFixtures_Tests.cs
@@ -29,10 +29,10 @@ namespace Robust.UnitTesting.Shared.Map
var grid = mapManager.CreateGrid(mapId);
// Should be nothing if grid empty
- Assert.That(entManager.TryGetComponent(grid.GridEntityId, out PhysicsComponent gridBody));
- Assert.That(entManager.TryGetComponent(grid.GridEntityId, out FixturesComponent manager));
- Assert.That(manager.FixtureCount, Is.EqualTo(0));
- Assert.That(gridBody.BodyType, Is.EqualTo(BodyType.Static));
+ Assert.That(entManager.TryGetComponent(grid.GridEntityId, out PhysicsComponent? gridBody));
+ Assert.That(entManager.TryGetComponent(grid.GridEntityId, out FixturesComponent? manager));
+ Assert.That(manager!.FixtureCount, Is.EqualTo(0));
+ Assert.That(gridBody!.BodyType, Is.EqualTo(BodyType.Static));
// 1 fixture if we only ever update the 1 chunk
grid.SetTile(Vector2i.Zero, new Tile(1));
diff --git a/Robust.UnitTesting/Shared/Physics/MapVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/MapVelocity_Test.cs
index fc40deaedb..760821f855 100644
--- a/Robust.UnitTesting/Shared/Physics/MapVelocity_Test.cs
+++ b/Robust.UnitTesting/Shared/Physics/MapVelocity_Test.cs
@@ -38,19 +38,19 @@ namespace Robust.UnitTesting.Shared.Physics
var grid2 = mapManager.CreateGrid(mapId);
Assert.That(entityManager.TryGetComponent(grid.GridEntityId, out var gridPhysics));
- gridPhysics.BodyType = BodyType.Dynamic;
+ gridPhysics!.BodyType = BodyType.Dynamic;
Vector2 offset = new(3, 4);
Vector2 expectedFinalVelocity = new Vector2(-4, 3) * 2 + Vector2.One;
var dummy = entityManager.SpawnEntity(DummyEntity, new EntityCoordinates(grid.GridEntityId, offset));
- Assert.That(entityManager.TryGetComponent(dummy, out PhysicsComponent body));
- Assert.That(entityManager.TryGetComponent(dummy, out TransformComponent xform));
- xform.AttachParent(grid.GridEntityId);
+ Assert.That(entityManager.TryGetComponent(dummy, out PhysicsComponent? body));
+ Assert.That(entityManager.TryGetComponent(dummy, out TransformComponent? xform));
+ xform!.AttachParent(grid.GridEntityId);
// Test Linear Velocities
gridPhysics.LinearVelocity = Vector2.One;
- Assert.That(body.LinearVelocity, Is.Approximately(Vector2.Zero, 1e-6));
+ Assert.That(body!.LinearVelocity, Is.Approximately(Vector2.Zero, 1e-6));
Assert.That(body.AngularVelocity, Is.Approximately(0f, 1e-6));
var linearVelocity = physicsSys.GetMapLinearVelocity(dummy, body);
@@ -103,20 +103,20 @@ namespace Robust.UnitTesting.Shared.Physics
var grid = mapManager.CreateGrid(mapId);
Assert.That(entityManager.TryGetComponent(grid.GridEntityId, out var gridPhysics));
- gridPhysics.BodyType = BodyType.Dynamic;
+ gridPhysics!.BodyType = BodyType.Dynamic;
Vector2 offset1 = new(2, 0);
var dummy1 = entityManager.SpawnEntity(DummyEntity, new EntityCoordinates(grid.GridEntityId, offset1));
- Assert.That(entityManager.TryGetComponent(dummy1, out PhysicsComponent body1));
- Assert.That(entityManager.TryGetComponent(dummy1, out TransformComponent xform1));
- xform1.AttachParent(grid.GridEntityId);
+ Assert.That(entityManager.TryGetComponent(dummy1, out PhysicsComponent? body1));
+ Assert.That(entityManager.TryGetComponent(dummy1, out TransformComponent? xform1));
+ xform1!.AttachParent(grid.GridEntityId);
// create another entity attached to the dummy1
Vector2 offset2 = new(-1, 0);
var dummy2 = entityManager.SpawnEntity(DummyEntity, new EntityCoordinates(dummy1, offset2));
- Assert.That(entityManager.TryGetComponent(dummy2, out PhysicsComponent body2));
- Assert.That(entityManager.TryGetComponent(dummy2, out TransformComponent xform2));
- xform2.AttachParent(dummy1);
+ Assert.That(entityManager.TryGetComponent(dummy2, out PhysicsComponent? body2));
+ Assert.That(entityManager.TryGetComponent(dummy2, out TransformComponent? xform2));
+ xform2!.AttachParent(dummy1);
Assert.That(xform2.WorldPosition, Is.Approximately(new Vector2(1, 0), 1e-6));
@@ -134,7 +134,7 @@ namespace Robust.UnitTesting.Shared.Physics
// check that if we make move in the opposite direction, but spin in the same direction, then dummy2 is
// (for this moment in time) stationary, but still rotating.
- body1.LinearVelocity = -gridPhysics.LinearVelocity;
+ body1!.LinearVelocity = -gridPhysics.LinearVelocity;
body1.AngularVelocity = gridPhysics.AngularVelocity;
linearVelocity = physicsSys.GetMapLinearVelocity(dummy2, body2);
angularVelocity = physicsSys.GetMapAngularVelocity(dummy2, body2);