#nullable enable using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Robust.Shared.GameObjects; using Robust.Shared.Map; namespace Content.IntegrationTests.Fixtures; public abstract partial class GameTest { /// /// Contains all server entities spawned using GameTest proxy methods. /// private readonly List _serverEntitiesToClean = new(); /// /// Contains all client entities spawned using GameTest proxy methods. /// private readonly List _clientEntitiesToClean = new(); private async Task CleanUpEntities() { await Task.WhenAll( Server.WaitAssertion(() => { foreach (var junk in _serverEntitiesToClean) { if (!SEntMan.Deleted(junk)) SEntMan.DeleteEntity(junk); } }), Client.WaitAssertion(() => { foreach (var junk in _clientEntitiesToClean) { if (!CEntMan.Deleted(junk)) CEntMan.DeleteEntity(junk); } }) ); } /// /// Returns a string representation of an entity for the server. /// public string SToPrettyString(EntityUid uid) { return Pair.Server.EntMan.ToPrettyString(uid); } /// /// Returns a string representation of an entity for the client. /// public string CToPrettyString(EntityUid uid) { return Pair.Client.EntMan.ToPrettyString(uid); } /// /// Converts a server EntityUid into the client-side equivalent entity. /// public EntityUid ToClientUid(EntityUid serverUid) { return Pair.ToClientUid(serverUid); } /// /// Converts a client EntityUid into the server-side equivalent entity. /// public EntityUid ToServerUid(EntityUid clientUid) { return Pair.ToServerUid(clientUid); } /// /// Retrieves the given component from an entity on the server. /// public T SComp(EntityUid target) where T : IComponent { return SEntMan.GetComponent(target); } /// /// Attempts to retrieve the given component from an entity on the server. /// public bool STryComp(EntityUid? target, [NotNullWhen(true)] out T? component) where T : IComponent { return SEntMan.TryGetComponent(target, out component); } /// /// Retrieves the given component from an entity on the client. /// public T CComp(EntityUid target) where T : IComponent { return CEntMan.GetComponent(target); } /// /// Attempts to retrieve the given component from an entity on the server. /// public bool CTryComp(EntityUid? target, [NotNullWhen(true)] out T? component) where T : IComponent { return SEntMan.TryGetComponent(target, out component); } /// /// Pairs an EntityUid with the given component, from the server. /// public Entity SEntity(EntityUid target) where T : IComponent { return new(target, SEntMan.GetComponent(target)); } /// /// Pairs an EntityUid with the given component, from the client. /// public Entity CEntity(EntityUid target) where T : IComponent { return new(target, CEntMan.GetComponent(target)); } /// /// Spawns an entity on the server. /// /// This tracks the entity for post-test cleanup. public EntityUid SSpawn(string? id) { var res = SEntMan.Spawn(id); _serverEntitiesToClean.Add(res); return res; } /// /// Spawns an entity on the server at a location. /// /// This tracks the entity for post-test cleanup. public EntityUid SSpawnAtPosition(string? id, EntityCoordinates coordinates) { var res = SEntMan.SpawnAtPosition(id, coordinates); _serverEntitiesToClean.Add(res); return res; } /// /// Spawns an entity on the client. /// /// This tracks the entity for post-test cleanup. public EntityUid CSpawn(string? id) { var res = CEntMan.Spawn(id); _clientEntitiesToClean.Add(res); return res; } /// /// Spawns an entity on the server at a location. /// /// This tracks the entity for post-test cleanup. public EntityUid CSpawnAtPosition(string? id, EntityCoordinates coordinates) { var res = CEntMan.SpawnAtPosition(id, coordinates); _clientEntitiesToClean.Add(res); return res; } /// /// Asynchronously spawns an entity on the server. /// public async Task Spawn(string? id) { var ent = EntityUid.Invalid; await Server.WaitPost(() => ent = SSpawn(id)); return ent; } /// /// Asynchronously spawns an entity on the server at the given position. /// public async Task SpawnAtPosition(string? id, EntityCoordinates coords) { var ent = EntityUid.Invalid; await Server.WaitPost(() => ent = SSpawnAtPosition(id, coords)); return ent; } /// /// Deletes an entity on the server immediately. /// public void SDeleteNow(EntityUid id) { SEntMan.DeleteEntity(id); } /// /// Deletes an entity on the client immediately. /// public void CDeleteNow(EntityUid id) { CEntMan.DeleteEntity(id); } /// /// Queues an entity for deletion at the end of the tick on the server. /// public void SQueueDel(EntityUid id) { SEntMan.QueueDeleteEntity(id); } /// /// Queues an entity for deletion at the end of the tick on the client. /// public void CQueueDel(EntityUid id) { CEntMan.QueueDeleteEntity(id); } }