Make TryGetComponent output nullable (#2905)

* Make TryGetComponent output nullable

* figs

* ah. there are more. Why do you fail me VS
This commit is contained in:
Leon Friedrich
2022-06-05 00:13:53 +02:00
committed by GitHub
parent 6c92d246c8
commit fc4eb664e2
10 changed files with 45 additions and 34 deletions
@@ -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;
@@ -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;
@@ -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)
@@ -642,7 +642,7 @@ namespace Robust.Shared.GameObjects
}
/// <inheritdoc />
public bool TryGetComponent<T>(EntityUid uid, [NotNullWhen(true)] out T component)
public bool TryGetComponent<T>(EntityUid uid, [NotNullWhen(true)] out T? component)
{
var dict = _entTraitArray[CompIdx.ArrayIndex<T>()];
if (dict.TryGetValue(uid, out var comp))
@@ -654,12 +654,12 @@ namespace Robust.Shared.GameObjects
}
}
component = default!;
component = default;
return false;
}
/// <inheritdoc />
public bool TryGetComponent<T>([NotNullWhen(true)] EntityUid? uid, [NotNullWhen(true)] out T component)
public bool TryGetComponent<T>([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;
}
+1 -1
View File
@@ -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<TransformComponent>(uid);
@@ -221,7 +221,7 @@ namespace Robust.Shared.GameObjects
/// <param name="uid">Entity UID to check.</param>
/// <param name="component">Component of the specified type (if exists).</param>
/// <returns>If the component existed in the entity.</returns>
bool TryGetComponent<T>(EntityUid uid, [NotNullWhen(true)] out T component);
bool TryGetComponent<T>(EntityUid uid, [NotNullWhen(true)] out T? component);
/// <summary>
/// Returns the component of a specific type.
@@ -230,7 +230,7 @@ namespace Robust.Shared.GameObjects
/// <param name="uid">Entity UID to check.</param>
/// <param name="component">Component of the specified type (if exists).</param>
/// <returns>If the component existed in the entity.</returns>
bool TryGetComponent<T>([NotNullWhen(true)] EntityUid? uid, [NotNullWhen(true)] out T component);
bool TryGetComponent<T>([NotNullWhen(true)] EntityUid? uid, [NotNullWhen(true)] out T? component);
/// <summary>
/// Returns the component of a specific type.
@@ -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
+16 -5
View File
@@ -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);
@@ -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));
@@ -38,19 +38,19 @@ namespace Robust.UnitTesting.Shared.Physics
var grid2 = mapManager.CreateGrid(mapId);
Assert.That(entityManager.TryGetComponent<PhysicsComponent>(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<PhysicsComponent>(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);