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 System;
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@@ -25,15 +26,12 @@ namespace Content.Server.GameObjects.Components.Research
{
public const int VolumePerSheet = 3750;
private BoundUserInterface _userInterface;
[ViewVariables]
public Queue<LatheRecipePrototype> Queue { get; } = new Queue<LatheRecipePrototype>();
[ViewVariables]
public bool Producing { get; private set; } = false;
public bool Producing { get; private set; }
private AppearanceComponent _appearance;
private LatheState _state = LatheState.Base;
protected virtual LatheState State
@@ -42,19 +40,26 @@ namespace Content.Server.GameObjects.Components.Research
set => _state = value;
}
private LatheRecipePrototype _producingRecipe = null;
private PowerReceiverComponent _powerReceiver;
private bool Powered => _powerReceiver.Powered;
private LatheRecipePrototype? _producingRecipe;
private bool Powered => !Owner.TryGetComponent(out PowerReceiverComponent? receiver) || receiver.Powered;
private static readonly TimeSpan InsertionTime = TimeSpan.FromSeconds(0.9f);
[ViewVariables]
private BoundUserInterface? UserInterface =>
Owner.TryGetComponent(out ServerUserInterfaceComponent? ui) &&
ui.TryGetBoundUserInterface(LatheUiKey.Key, out var boundUi)
? boundUi
: null;
public override void Initialize()
{
base.Initialize();
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>().GetBoundUserInterface(LatheUiKey.Key);
_userInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
_powerReceiver = Owner.GetComponent<PowerReceiverComponent>();
_appearance = Owner.GetComponent<AppearanceComponent>();
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
}
}
private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage message)
@@ -66,28 +71,28 @@ namespace Content.Server.GameObjects.Components.Research
{
case LatheQueueRecipeMessage msg:
_prototypeManager.TryIndex(msg.ID, out LatheRecipePrototype recipe);
if (recipe != null)
if (recipe != null!)
for (var i = 0; i < msg.Quantity; i++)
{
Queue.Enqueue(recipe);
_userInterface.SendMessage(new LatheFullQueueMessage(GetIDQueue()));
UserInterface?.SendMessage(new LatheFullQueueMessage(GetIdQueue()));
}
break;
case LatheSyncRequestMessage msg:
if (!Owner.TryGetComponent(out MaterialStorageComponent storage)) return;
_userInterface.SendMessage(new LatheFullQueueMessage(GetIDQueue()));
case LatheSyncRequestMessage _:
if (!Owner.HasComponent<MaterialStorageComponent>()) return;
UserInterface?.SendMessage(new LatheFullQueueMessage(GetIdQueue()));
if (_producingRecipe != null)
_userInterface.SendMessage(new LatheProducingRecipeMessage(_producingRecipe.ID));
UserInterface?.SendMessage(new LatheProducingRecipeMessage(_producingRecipe.ID));
break;
case LatheServerSelectionMessage msg:
if (!Owner.TryGetComponent(out ResearchClientComponent researchClient)) return;
case LatheServerSelectionMessage _:
if (!Owner.TryGetComponent(out ResearchClientComponent? researchClient)) return;
researchClient.OpenUserInterface(message.Session);
break;
case LatheServerSyncMessage msg:
if (!Owner.TryGetComponent(out TechnologyDatabaseComponent database)
|| !Owner.TryGetComponent(out ProtolatheDatabaseComponent protoDatabase)) return;
case LatheServerSyncMessage _:
if (!Owner.TryGetComponent(out TechnologyDatabaseComponent? database)
|| !Owner.TryGetComponent(out ProtolatheDatabaseComponent? protoDatabase)) return;
if (database.SyncWithServer())
protoDatabase.Sync();
@@ -103,9 +108,9 @@ namespace Content.Server.GameObjects.Components.Research
{
return false;
}
if (Producing || !CanProduce(recipe) || !Owner.TryGetComponent(out MaterialStorageComponent storage)) return false;
if (Producing || !CanProduce(recipe) || !Owner.TryGetComponent(out MaterialStorageComponent? storage)) return false;
_userInterface.SendMessage(new LatheFullQueueMessage(GetIDQueue()));
UserInterface?.SendMessage(new LatheFullQueueMessage(GetIdQueue()));
Producing = true;
_producingRecipe = recipe;
@@ -116,7 +121,7 @@ namespace Content.Server.GameObjects.Components.Research
storage.RemoveMaterial(material, amount);
}
_userInterface.SendMessage(new LatheProducingRecipeMessage(recipe.ID));
UserInterface?.SendMessage(new LatheProducingRecipeMessage(recipe.ID));
State = LatheState.Producing;
SetAppearance(LatheVisualState.Producing);
@@ -126,7 +131,7 @@ namespace Content.Server.GameObjects.Components.Research
Producing = false;
_producingRecipe = null;
Owner.EntityManager.SpawnEntity(recipe.Result, Owner.Transform.GridPosition);
_userInterface.SendMessage(new LatheStoppedProducingRecipeMessage());
UserInterface?.SendMessage(new LatheStoppedProducingRecipeMessage());
State = LatheState.Base;
SetAppearance(LatheVisualState.Idle);
});
@@ -136,12 +141,12 @@ namespace Content.Server.GameObjects.Components.Research
public void OpenUserInterface(IPlayerSession session)
{
_userInterface.Open(session);
UserInterface?.Open(session);
}
void IActivate.Activate(ActivateEventArgs eventArgs)
{
if (!eventArgs.User.TryGetComponent(out IActorComponent actor))
if (!eventArgs.User.TryGetComponent(out IActorComponent? actor))
return;
if (!Powered)
{
@@ -153,12 +158,12 @@ namespace Content.Server.GameObjects.Components.Research
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
{
if (!Owner.TryGetComponent(out MaterialStorageComponent storage)
|| !eventArgs.Using.TryGetComponent(out MaterialComponent material)) return false;
if (!Owner.TryGetComponent(out MaterialStorageComponent? storage)
|| !eventArgs.Using.TryGetComponent(out MaterialComponent? material)) return false;
var multiplier = 1;
if (eventArgs.Using.TryGetComponent(out StackComponent stack)) multiplier = stack.Count;
if (eventArgs.Using.TryGetComponent(out StackComponent? stack)) multiplier = stack.Count;
var totalAmount = 0;
@@ -205,11 +210,13 @@ namespace Content.Server.GameObjects.Components.Research
private void SetAppearance(LatheVisualState state)
{
if (_appearance != null || Owner.TryGetComponent(out _appearance))
_appearance.SetData(PowerDeviceVisuals.VisualState, state);
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
{
appearance.SetData(PowerDeviceVisuals.VisualState, state);
}
}
private Queue<string> GetIDQueue()
private Queue<string> GetIdQueue()
{
var queue = new Queue<string>();
foreach (var recipePrototype in Queue)

View File

@@ -1,4 +1,5 @@
using Content.Server.GameObjects.EntitySystems;
#nullable enable
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Components.Research;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects.Components.UserInterface;
@@ -14,20 +15,21 @@ namespace Content.Server.GameObjects.Components.Research
[RegisterComponent]
public class ResearchClientComponent : SharedResearchClientComponent, IActivate
{
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
// TODO: Create GUI for changing RD server.
private BoundUserInterface _userInterface;
#pragma warning disable 649
[Dependency] private readonly IEntitySystemManager _entitySystemManager;
#pragma warning restore 649
private BoundUserInterface? UserInterface =>
Owner.TryGetComponent(out ServerUserInterfaceComponent? ui) &&
ui.TryGetBoundUserInterface(ResearchClientUiKey.Key, out var boundUi)
? boundUi
: null;
public bool ConnectedToServer => Server != null;
[ViewVariables(VVAccess.ReadOnly)]
public ResearchServerComponent Server { get; set; }
public ResearchServerComponent? Server { get; set; }
public bool RegisterServer(ResearchServerComponent server)
public bool RegisterServer(ResearchServerComponent? server)
{
var result = server != null && server.RegisterClient(this);
return result;
@@ -41,23 +43,28 @@ namespace Content.Server.GameObjects.Components.Research
public override void Initialize()
{
base.Initialize();
// For now it just registers on the first server it can find.
var servers = _entitySystemManager.GetEntitySystem<ResearchSystem>().Servers;
if(servers.Count > 0)
if (servers.Count > 0)
RegisterServer(servers[0]);
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>().GetBoundUserInterface(ResearchClientUiKey.Key);
_userInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
}
}
public void OpenUserInterface(IPlayerSession session)
{
UpdateUserInterface();
_userInterface.Open(session);
UserInterface?.Open(session);
}
void IActivate.Activate(ActivateEventArgs eventArgs)
{
if (!eventArgs.User.TryGetComponent(out IActorComponent actor))
if (!eventArgs.User.TryGetComponent(out IActorComponent? actor))
return;
OpenUserInterface(actor.playerSession);
@@ -65,7 +72,7 @@ namespace Content.Server.GameObjects.Components.Research
public void UpdateUserInterface()
{
_userInterface?.SetState(GetNewUiState());
UserInterface?.SetState(GetNewUiState());
}
private ResearchClientBoundInterfaceState GetNewUiState()
@@ -73,7 +80,7 @@ namespace Content.Server.GameObjects.Components.Research
var rd = _entitySystemManager.GetEntitySystem<ResearchSystem>();
return new ResearchClientBoundInterfaceState(rd.Servers.Count, rd.GetServerNames(),
rd.GetServerIds(), ConnectedToServer ? Server.Id : -1);
rd.GetServerIds(), ConnectedToServer ? Server!.Id : -1);
}
private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage msg)

View File

@@ -1,4 +1,5 @@
using Content.Server.GameObjects.Components.Power.ApcNetComponents;
#nullable enable
using Content.Server.GameObjects.Components.Power.ApcNetComponents;
using Content.Shared.Audio;
using Content.Shared.GameObjects.Components.Research;
using Content.Shared.Interfaces.GameObjects.Components;
@@ -14,6 +15,7 @@ using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Research
{
@@ -21,31 +23,38 @@ namespace Content.Server.GameObjects.Components.Research
[ComponentReference(typeof(IActivate))]
public class ResearchConsoleComponent : SharedResearchConsoleComponent, IActivate
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
#pragma warning disable 649
[Dependency] private readonly IPrototypeManager _prototypeManager;
[Dependency] private readonly IRobustRandom _random;
#pragma warning restore 649
private const string SoundCollectionName = "keyboard";
private BoundUserInterface _userInterface;
private ResearchClientComponent _client;
private PowerReceiverComponent _powerReceiver;
private const string _soundCollectionName = "keyboard";
private bool Powered => !Owner.TryGetComponent(out PowerReceiverComponent? receiver) || receiver.Powered;
private bool Powered => _powerReceiver.Powered;
[ViewVariables]
private BoundUserInterface? UserInterface =>
Owner.TryGetComponent(out ServerUserInterfaceComponent? ui) &&
ui.TryGetBoundUserInterface(ResearchConsoleUiKey.Key, out var boundUi)
? boundUi
: null;
public override void Initialize()
{
base.Initialize();
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>().GetBoundUserInterface(ResearchConsoleUiKey.Key);
_userInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
_client = Owner.GetComponent<ResearchClientComponent>();
_powerReceiver = Owner.GetComponent<PowerReceiverComponent>();
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
}
Owner.EnsureComponent<ResearchClientComponent>();
}
private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage message)
{
if (!Owner.TryGetComponent(out TechnologyDatabaseComponent database)) return;
if (!Owner.TryGetComponent(out TechnologyDatabaseComponent? database))
return;
if (!Owner.TryGetComponent(out ResearchClientComponent? client))
return;
if (!Powered)
return;
@@ -54,8 +63,9 @@ namespace Content.Server.GameObjects.Components.Research
case ConsoleUnlockTechnologyMessage msg:
var protoMan = IoCManager.Resolve<IPrototypeManager>();
if (!protoMan.TryIndex(msg.Id, out TechnologyPrototype tech)) break;
if(!_client.Server.CanUnlockTechnology(tech)) break;
if (_client.Server.UnlockTechnology(tech))
if (client.Server == null) break;
if (!client.Server.CanUnlockTechnology(tech)) break;
if (client.Server.UnlockTechnology(tech))
{
database.SyncWithServer();
database.Dirty();
@@ -64,13 +74,12 @@ namespace Content.Server.GameObjects.Components.Research
break;
case ConsoleServerSyncMessage msg:
case ConsoleServerSyncMessage _:
database.SyncWithServer();
UpdateUserInterface();
break;
case ConsoleServerSelectionMessage msg:
if (!Owner.TryGetComponent(out ResearchClientComponent client)) break;
case ConsoleServerSelectionMessage _:
client.OpenUserInterface(message.Session);
break;
}
@@ -81,13 +90,17 @@ namespace Content.Server.GameObjects.Components.Research
/// </summary>
public void UpdateUserInterface()
{
_userInterface.SetState(GetNewUiState());
UserInterface?.SetState(GetNewUiState());
}
private ResearchConsoleBoundInterfaceState GetNewUiState()
{
var points = _client.ConnectedToServer ? _client.Server.Point : 0;
var pointsPerSecond = _client.ConnectedToServer ? _client.Server.PointsPerSecond : 0;
if (!Owner.TryGetComponent(out ResearchClientComponent? client) ||
client.Server == null)
return new ResearchConsoleBoundInterfaceState(default, default);
var points = client.ConnectedToServer ? client.Server.Point : 0;
var pointsPerSecond = client.ConnectedToServer ? client.Server.PointsPerSecond : 0;
return new ResearchConsoleBoundInterfaceState(points, pointsPerSecond);
}
@@ -98,12 +111,12 @@ namespace Content.Server.GameObjects.Components.Research
/// <param name="session">Session where the UI will be shown</param>
public void OpenUserInterface(IPlayerSession session)
{
_userInterface.Open(session);
UserInterface?.Open(session);
}
void IActivate.Activate(ActivateEventArgs eventArgs)
{
if (!eventArgs.User.TryGetComponent(out IActorComponent actor))
if (!eventArgs.User.TryGetComponent(out IActorComponent? actor))
return;
if (!Powered)
{
@@ -112,17 +125,14 @@ namespace Content.Server.GameObjects.Components.Research
OpenUserInterface(actor.playerSession);
PlayKeyboardSound();
return;
}
private void PlayKeyboardSound()
{
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>(_soundCollectionName);
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>(SoundCollectionName);
var file = _random.Pick(soundCollection.PickFiles);
var audioSystem = EntitySystem.Get<AudioSystem>();
audioSystem.PlayFromEntity(file,Owner,AudioParams.Default);
}
}
}

View File

@@ -73,7 +73,7 @@ namespace Content.Server.GameObjects.Components.Research
base.Initialize();
Id = ServerCount++;
EntitySystem.Get<ResearchSystem>()?.RegisterServer(this);
Database = Owner.GetComponent<TechnologyDatabaseComponent>();
Database = Owner.EnsureComponent<TechnologyDatabaseComponent>();
Owner.TryGetComponent(out _powerReceiver);
}