Add a test that puts all components on an entity and checks for no exceptions (#1815)

* Add test that puts all components on an entity and checks for no exceptions

Also fix all the exceptions that happened because of this

* Add comments to the test

* Fix nullable errors

* Fix more nullable errors

* More nullable error fixes

* Unignore basic actor component

* Fix more nullable errors

* NULLABLE ERROR

* Add string interpolation

* Merge if checks

* Remove redundant pragma warning disable 649

* Address reviews

* Remove null wrappers around TryGetComponent

* Merge conflict fixes

* APC battery component error fix

* Fix power test

* Fix atmos mapgrid usages
This commit is contained in:
DrSmugleaf
2020-08-22 22:29:20 +02:00
committed by GitHub
parent c8178550b8
commit b9196d0a10
84 changed files with 1790 additions and 1123 deletions

View File

@@ -1,4 +1,5 @@
using Content.Server.GameObjects.EntitySystems.AI;
#nullable enable
using Content.Server.GameObjects.EntitySystems.AI;
using Content.Shared.GameObjects.Components.Movement;
using Robust.Server.AI;
using Robust.Shared.GameObjects;
@@ -14,23 +15,23 @@ namespace Content.Server.GameObjects.Components.Movement
[RegisterComponent, ComponentReference(typeof(IMoverComponent))]
public class AiControllerComponent : Component, IMoverComponent
{
private string _logicName;
private string? _logicName;
private float _visionRadius;
public override string Name => "AiController";
[ViewVariables(VVAccess.ReadWrite)]
public string LogicName
public string? LogicName
{
get => _logicName;
set
{
_logicName = value;
Processor = null;
Processor = null!;
}
}
public AiLogicProcessor Processor { get; set; }
public AiLogicProcessor? Processor { get; set; }
[ViewVariables(VVAccess.ReadWrite)]
public float VisionRadius
@@ -45,9 +46,8 @@ namespace Content.Server.GameObjects.Components.Movement
base.Initialize();
// This component requires a collidable component.
if (!Owner.HasComponent<ICollidableComponent>())
Owner.AddComponent<CollidableComponent>();
Owner.EnsureComponent<CollidableComponent>();
EntitySystem.Get<AiSystem>().ProcessorInitialize(this);
}
@@ -74,7 +74,7 @@ namespace Content.Server.GameObjects.Components.Movement
{
get
{
if (Owner.TryGetComponent(out MovementSpeedModifierComponent component))
if (Owner.TryGetComponent(out MovementSpeedModifierComponent? component))
{
return component.CurrentWalkSpeed;
}
@@ -91,7 +91,7 @@ namespace Content.Server.GameObjects.Components.Movement
{
get
{
if (Owner.TryGetComponent(out MovementSpeedModifierComponent component))
if (Owner.TryGetComponent(out MovementSpeedModifierComponent? component))
{
return component.CurrentSprintSpeed;
}

View File

@@ -24,10 +24,8 @@ namespace Content.Server.GameObjects.Components.Movement
[ComponentReference(typeof(IClimbable))]
public class ClimbableComponent : SharedClimbableComponent, IDragDropOn
{
#pragma warning disable 649
[Dependency] private readonly IServerNotifyManager _notifyManager = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
#pragma warning restore 649
/// <summary>
/// The range from which this entity can be climbed.
@@ -81,7 +79,7 @@ namespace Content.Server.GameObjects.Components.Movement
var bodyManager = eventArgs.User.GetComponent<BodyManagerComponent>();
if (bodyManager.GetBodyPartsOfType(Shared.GameObjects.Components.Body.BodyPartType.Leg).Count == 0 ||
bodyManager.GetBodyPartsOfType(Shared.GameObjects.Components.Body.BodyPartType.Foot).Count == 0)
bodyManager.GetBodyPartsOfType(Shared.GameObjects.Components.Body.BodyPartType.Foot).Count == 0)
{
_notifyManager.PopupMessage(eventArgs.User, eventArgs.User, Loc.GetString("You are unable to climb!"));
@@ -102,10 +100,10 @@ namespace Content.Server.GameObjects.Components.Movement
}
else // user is dragging some other entity onto a climbable
{
if (eventArgs.Target == null || !eventArgs.Dropped.HasComponent<ClimbingComponent>())
if (eventArgs.Target == null || !eventArgs.Dropped.HasComponent<ClimbingComponent>())
{
_notifyManager.PopupMessage(eventArgs.User, eventArgs.User, Loc.GetString("You can't do that!"));
return false;
}

View File

@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using Content.Shared.GameObjects.Components.Movement;
using Robust.Server.GameObjects;
@@ -18,9 +19,7 @@ namespace Content.Server.GameObjects.Components.Movement
[RegisterComponent]
public class ServerPortalComponent : SharedPortalComponent
{
#pragma warning disable 649
[Dependency] private readonly IServerEntityManager _serverEntityManager;
#pragma warning restore 649
[Dependency] private readonly IServerEntityManager _serverEntityManager = default!;
// Potential improvements: Different sounds,
// Add Gateways
@@ -28,15 +27,14 @@ namespace Content.Server.GameObjects.Components.Movement
// Put portal above most other things layer-wise
// Add telefragging (get entities on connecting portal and force brute damage)
private AppearanceComponent _appearanceComponent;
private IEntity _connectingTeleporter;
private IEntity? _connectingTeleporter;
private PortalState _state = PortalState.Pending;
[ViewVariables(VVAccess.ReadWrite)] private float _individualPortalCooldown;
[ViewVariables] private float _overallPortalCooldown;
[ViewVariables] private bool _onCooldown;
[ViewVariables] private string _departureSound;
[ViewVariables] private string _arrivalSound;
public List<IEntity> immuneEntities = new List<IEntity>(); // K
[ViewVariables] private string _departureSound = "";
[ViewVariables] private string _arrivalSound = "";
public readonly List<IEntity> ImmuneEntities = new List<IEntity>(); // K
[ViewVariables(VVAccess.ReadWrite)] private float _aliveTime;
public override void ExposeData(ObjectSerializer serializer)
@@ -52,12 +50,6 @@ namespace Content.Server.GameObjects.Components.Movement
serializer.DataField(ref _arrivalSound, "arrival_sound", "/Audio/Effects/teleport_arrival.ogg");
}
public override void Initialize()
{
base.Initialize();
_appearanceComponent = Owner.GetComponent<AppearanceComponent>();
}
public override void OnAdd()
{
// This will blow up an entity it's attached to
@@ -74,13 +66,6 @@ namespace Content.Server.GameObjects.Components.Movement
}
}
public override void OnRemove()
{
_appearanceComponent = null;
base.OnRemove();
}
public bool CanBeConnected()
{
if (_connectingTeleporter == null)
@@ -108,23 +93,24 @@ namespace Content.Server.GameObjects.Components.Movement
}
_state = targetState;
if (_appearanceComponent != null)
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
{
_appearanceComponent.SetData(PortalVisuals.State, _state);
appearance.SetData(PortalVisuals.State, _state);
}
}
private void releaseCooldown(IEntity entity)
private void ReleaseCooldown(IEntity entity)
{
if (immuneEntities.Contains(entity))
if (ImmuneEntities.Contains(entity))
{
immuneEntities.Remove(entity);
ImmuneEntities.Remove(entity);
}
if (_connectingTeleporter != null &&
_connectingTeleporter.TryGetComponent<ServerPortalComponent>(out var otherPortal))
{
otherPortal.immuneEntities.Remove(entity);
otherPortal.ImmuneEntities.Remove(entity);
}
}
@@ -142,7 +128,7 @@ namespace Content.Server.GameObjects.Components.Movement
private bool IsEntityPortable(IEntity entity)
{
// TODO: Check if it's slotted etc. Otherwise the slot item itself gets ported.
if (!immuneEntities.Contains(entity) && entity.HasComponent<TeleportableComponent>())
if (!ImmuneEntities.Contains(entity) && entity.HasComponent<TeleportableComponent>())
{
return true;
}
@@ -192,7 +178,7 @@ namespace Content.Server.GameObjects.Components.Movement
public void TryPortalEntity(IEntity entity)
{
if (immuneEntities.Contains(entity) || _connectingTeleporter == null)
if (ImmuneEntities.Contains(entity) || _connectingTeleporter == null)
{
return;
}
@@ -208,9 +194,9 @@ namespace Content.Server.GameObjects.Components.Movement
soundPlayer.PlayAtCoords(_arrivalSound, entity.Transform.GridPosition);
TryChangeState(PortalState.RecentlyTeleported);
// To stop spam teleporting. Could potentially look at adding a timer to flush this from the portal
immuneEntities.Add(entity);
_connectingTeleporter.GetComponent<ServerPortalComponent>().immuneEntities.Add(entity);
Timer.Spawn(TimeSpan.FromSeconds(_individualPortalCooldown), () => releaseCooldown(entity));
ImmuneEntities.Add(entity);
_connectingTeleporter.GetComponent<ServerPortalComponent>().ImmuneEntities.Add(entity);
Timer.Spawn(TimeSpan.FromSeconds(_individualPortalCooldown), () => ReleaseCooldown(entity));
StartCooldown();
}
}

View File

@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Linq;
using Content.Shared.GameObjects.Components.Movement;
using Content.Shared.Interfaces.GameObjects.Components;
@@ -24,11 +25,10 @@ namespace Content.Server.GameObjects.Components.Movement
[RegisterComponent]
public class ServerTeleporterComponent : Component, IAfterInteract
{
#pragma warning disable 649
[Dependency] private readonly IMapManager _mapManager;
[Dependency] private readonly IServerEntityManager _serverEntityManager;
[Dependency] private readonly IRobustRandom _spreadRandom;
#pragma warning restore 649
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IServerEntityManager _serverEntityManager = default!;
[Dependency] private readonly IRobustRandom _spreadRandom = default!;
// TODO: Look at MapManager.Map for Beacons to get all entities on grid
public ItemTeleporterState State => _state;
@@ -39,15 +39,13 @@ namespace Content.Server.GameObjects.Components.Movement
[ViewVariables] private int _range;
[ViewVariables] private ItemTeleporterState _state;
[ViewVariables] private TeleporterType _teleporterType;
[ViewVariables] private string _departureSound;
[ViewVariables] private string _arrivalSound;
[ViewVariables] private string _cooldownSound;
[ViewVariables] private string _departureSound = "";
[ViewVariables] private string _arrivalSound = "";
[ViewVariables] private string? _cooldownSound;
// If the direct OR random teleport will try to avoid hitting collidables
[ViewVariables] private bool _avoidCollidable;
[ViewVariables] private float _portalAliveTime;
private AppearanceComponent _appearanceComponent;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
@@ -63,22 +61,20 @@ namespace Content.Server.GameObjects.Components.Movement
serializer.DataField(ref _portalAliveTime, "portal_alive_time", 5.0f); // TODO: Change this to 0 before PR?
}
public override void OnRemove()
{
_appearanceComponent = null;
base.OnRemove();
}
private void SetState(ItemTeleporterState newState)
{
if (!Owner.TryGetComponent(out AppearanceComponent? appearance))
{
return;
}
if (newState == ItemTeleporterState.Cooldown)
{
_appearanceComponent.SetData(TeleporterVisuals.VisualState, TeleporterVisualState.Charging);
appearance.SetData(TeleporterVisuals.VisualState, TeleporterVisualState.Charging);
}
else
{
_appearanceComponent.SetData(TeleporterVisuals.VisualState, TeleporterVisualState.Ready);
appearance.SetData(TeleporterVisuals.VisualState, TeleporterVisualState.Ready);
}
_state = newState;
}
@@ -149,12 +145,11 @@ namespace Content.Server.GameObjects.Components.Movement
public override void Initialize()
{
_appearanceComponent = Owner.GetComponent<AppearanceComponent>();
_state = ItemTeleporterState.Off;
base.Initialize();
_state = ItemTeleporterState.Off;
}
private bool emptySpace(IEntity user, Vector2 target)
private bool EmptySpace(IEntity user, Vector2 target)
{
// TODO: Check the user's spot? Upside is no stacking TPs but downside is they can't unstuck themselves from walls.
foreach (var entity in _serverEntityManager.GetEntitiesIntersecting(user.Transform.MapID, target))
@@ -167,7 +162,7 @@ namespace Content.Server.GameObjects.Components.Movement
return true;
}
private Vector2 randomEmptySpot(IEntity user, int range)
private Vector2 RandomEmptySpot(IEntity user, int range)
{
Vector2 targetVector = user.Transform.GridPosition.Position;
// Definitely a better way to do this
@@ -176,7 +171,7 @@ namespace Content.Server.GameObjects.Components.Movement
var randomRange = _spreadRandom.Next(0, range);
var angle = Angle.FromDegrees(_spreadRandom.Next(0, 359));
targetVector = user.Transform.GridPosition.Position + angle.ToVec() * randomRange;
if (emptySpace(user, targetVector))
if (EmptySpace(user, targetVector))
{
return targetVector;
}
@@ -200,7 +195,7 @@ namespace Content.Server.GameObjects.Components.Movement
Vector2 targetVector;
if (_avoidCollidable)
{
targetVector = randomEmptySpot(user, _range);
targetVector = RandomEmptySpot(user, _range);
}
else
{
@@ -227,7 +222,7 @@ namespace Content.Server.GameObjects.Components.Movement
public void Teleport(IEntity user, Vector2 vector)
{
// Messy maybe?
GridCoordinates targetGrid = new GridCoordinates(vector, user.Transform.GridID);
var targetGrid = new GridCoordinates(vector, user.Transform.GridID);
var soundPlayer = EntitySystem.Get<AudioSystem>();
// If portals use those, otherwise just move em over
@@ -240,10 +235,11 @@ namespace Content.Server.GameObjects.Components.Movement
// Arrival portal
var arrivalPortal = _serverEntityManager.SpawnEntity("Portal", targetGrid);
arrivalPortal.TryGetComponent<ServerPortalComponent>(out var arrivalComponent);
// Connect. TODO: If the OnUpdate in ServerPortalComponent is changed this may need to change as well.
arrivalComponent.TryConnectPortal(departurePortal);
if (arrivalPortal.TryGetComponent<ServerPortalComponent>(out var arrivalComponent))
{
// Connect. TODO: If the OnUpdate in ServerPortalComponent is changed this may need to change as well.
arrivalComponent.TryConnectPortal(departurePortal);
}
}
else
{

View File

@@ -22,10 +22,8 @@ namespace Content.Server.GameObjects.Components.Movement
[ComponentReference(typeof(IMoverComponent))]
internal class ShuttleControllerComponent : Component, IMoverComponent
{
#pragma warning disable 649
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
#pragma warning restore 649
private bool _movingUp;
private bool _movingDown;