Remove IContainerManager (#4308)

This commit is contained in:
metalgearsloth
2023-08-24 16:39:35 +10:00
committed by GitHub
parent d66f7c7c06
commit a4474d8df8
5 changed files with 23 additions and 98 deletions

View File

@@ -34,7 +34,7 @@ namespace Robust.Shared.Containers
/// <param name="entity">Entity that might be inside a container.</param>
/// <param name="manager">The container manager that this entity is inside of.</param>
/// <returns>If a container manager was found.</returns>
public static bool TryGetContainerMan(this EntityUid entity, [NotNullWhen(true)] out IContainerManager? manager, IEntityManager? entMan = null)
public static bool TryGetContainerMan(this EntityUid entity, [NotNullWhen(true)] out ContainerManagerComponent? manager, IEntityManager? entMan = null)
{
IoCManager.Resolve(ref entMan);
DebugTools.Assert(entMan.EntityExists(entity));
@@ -132,9 +132,10 @@ namespace Robust.Shared.Containers
/// <see cref="SharedContainerSystem.TryGetManagerComp"/>
/// </summary>
[Obsolete("Use SharedContainerSystem.TryGetManagerComp() instead")]
private static bool TryGetManagerComp(this EntityUid entity, [NotNullWhen(true)] out IContainerManager? manager, IEntityManager? entMan = null)
private static bool TryGetManagerComp(this EntityUid entity, [NotNullWhen(true)] out ContainerManagerComponent? manager, IEntityManager? entMan = null)
{
return IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<SharedContainerSystem>()
IoCManager.Resolve(ref entMan);
return entMan.System<SharedContainerSystem>()
.TryGetManagerComp(entity, out manager);
}

View File

@@ -16,10 +16,9 @@ namespace Robust.Shared.Containers
/// <summary>
/// Holds data about a set of entity containers on this entity.
/// </summary>
[ComponentReference(typeof(IContainerManager))]
[NetworkedComponent]
[RegisterComponent, ComponentProtoName("ContainerContainer")]
public sealed partial class ContainerManagerComponent : Component, IContainerManager, ISerializationHooks
public sealed partial class ContainerManagerComponent : Component, ISerializationHooks
{
[Dependency] private readonly IDynamicTypeFactoryInternal _dynFactory = default!;
[Dependency] private readonly IEntityManager _entMan = default!;

View File

@@ -1,78 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Maths;
namespace Robust.Shared.Containers
{
/// <summary>
/// Manages containers on an entity.
/// </summary>
/// <seealso cref="IContainer" />
public partial interface IContainerManager : IComponent
{
/// <summary>
/// Makes a new container of the specified type.
/// </summary>
/// <param name="id">The ID for the new container.</param>
/// <typeparam name="T">The type of the new container</typeparam>
/// <returns>The new container.</returns>
/// <exception cref="ArgumentException">Thrown if there already is a container with the specified ID</exception>
T MakeContainer<T>(string id)
where T : IContainer;
/// <summary>
/// Attempts to remove the entity from some container on this entity.
/// </summary>
/// <param name="reparent">If false, this operation will not rigger a move or parent change event. Ignored if
/// destination is not null</param>
/// <param name="force">If true, this will not perform can-remove checks.</param>
/// <param name="destination">Where to place the entity after removing. Avoids unnecessary broadphase updates.
/// If not specified, and reparent option is true, then the entity will either be inserted into a parent
/// container, the grid, or the map.</param>
/// <param name="localRotation">Optional final local rotation after removal. Avoids redundant move events.</param>
bool Remove(EntityUid toremove,
TransformComponent? xform = null,
MetaDataComponent? meta = null,
bool reparent = true,
bool force = false,
EntityCoordinates? destination = null,
Angle? localRotation = null);
/// <summary>
/// Gets the container with the specified ID.
/// </summary>
/// <param name="id">The ID to look up.</param>
/// <returns>The container.</returns>
/// <exception cref="KeyNotFoundException">Thrown if the container does not exist.</exception>
IContainer GetContainer(string id);
/// <summary>
/// Checks whether we have a container with the specified ID.
/// </summary>
/// <param name="id">The entity ID to check.</param>
/// <returns>True if we already have a container, false otherwise.</returns>
bool HasContainer(string id);
/// <summary>
/// Attempt to retrieve a container with specified ID.
/// </summary>
/// <param name="id">The ID to look up.</param>
/// <param name="container">The container if it was found, <c>null</c> if not found.</param>
/// <returns>True if the container was found, false otherwise.</returns>
bool TryGetContainer(string id, [NotNullWhen(true)] out IContainer? container);
/// <summary>
/// Attempt to retrieve a container that contains a specific entity.
/// </summary>
/// <param name="entity">The entity that is inside the container.</param>
/// <param name="container">The container if it was found, <c>null</c> if not found.</param>
/// <returns>True if the container was found, false otherwise.</returns>
/// <returns>True if the container was found, false otherwise.</returns>
bool TryGetContainer(EntityUid entity, [NotNullWhen(true)] out IContainer? container);
bool ContainsEntity(EntityUid entity);
}
}

View File

@@ -440,7 +440,7 @@ namespace Robust.Shared.Containers
return false;
}
internal bool TryGetManagerComp(EntityUid entity, [NotNullWhen(true)] out IContainerManager? manager)
internal bool TryGetManagerComp(EntityUid entity, [NotNullWhen(true)] out ContainerManagerComponent? manager)
{
DebugTools.Assert(Exists(entity));

View File

@@ -33,6 +33,7 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
public void TestCreation()
{
var sim = SimulationFactory();
var entManager = sim.Resolve<IEntityManager>();
var containerSys = sim.Resolve<IEntitySystemManager>().GetEntitySystem<ContainerSystem>();
var entity = sim.SpawnEntity(null, new EntityCoordinates(new EntityUid(1), new Vector2(0, 0)));
@@ -42,7 +43,7 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
Assert.That(container.ID, Is.EqualTo("dummy"));
Assert.That(container.Owner, Is.EqualTo(entity));
var manager = IoCManager.Resolve<IEntityManager>().GetComponent<IContainerManager>(entity);
var manager = entManager.GetComponent<ContainerManagerComponent>(entity);
Assert.That(container.Manager, Is.EqualTo(manager));
Assert.That(() => containerSys.MakeContainer<Container>(entity, "dummy"), Throws.ArgumentException);
@@ -62,7 +63,7 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
Assert.That(manager.GetContainer("dummy2"), Is.EqualTo(container2));
Assert.That(() => manager.GetContainer("dummy3"), Throws.TypeOf<KeyNotFoundException>());
IoCManager.Resolve<IEntityManager>().DeleteEntity(entity);
entManager.DeleteEntity(entity);
Assert.That(manager.Deleted, Is.True);
Assert.That(container.Deleted, Is.True);
@@ -73,11 +74,12 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
public void TestInsertion()
{
var sim = SimulationFactory();
var entManager = sim.Resolve<IEntityManager>();
var containerSys = sim.Resolve<IEntitySystemManager>().GetEntitySystem<ContainerSystem>();
var owner = sim.SpawnEntity(null, new EntityCoordinates(new EntityUid(1), new Vector2(0, 0)));
var inserted = sim.SpawnEntity(null, new EntityCoordinates(new EntityUid(1), new Vector2(0, 0)));
var transform = IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(inserted);
var transform = entManager.GetComponent<TransformComponent>(inserted);
var container = containerSys.MakeContainer<Container>(owner, "dummy");
Assert.That(container.Insert(inserted), Is.True);
@@ -93,7 +95,7 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
Assert.That(success, Is.False);
container.Insert(inserted);
IoCManager.Resolve<IEntityManager>().DeleteEntity(owner);
entManager.DeleteEntity(owner);
// Make sure inserted was detached.
Assert.That(transform.Deleted, Is.True);
}
@@ -102,11 +104,12 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
public void TestNestedRemoval()
{
var sim = SimulationFactory();
var entManager = sim.Resolve<IEntityManager>();
var containerSys = sim.Resolve<IEntitySystemManager>().GetEntitySystem<ContainerSystem>();
var owner = sim.SpawnEntity(null, new EntityCoordinates(new EntityUid(1), new Vector2(0, 0)));
var inserted = sim.SpawnEntity(null, new EntityCoordinates(new EntityUid(1), new Vector2(0, 0)));
var transform = IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(inserted);
var transform = entManager.GetComponent<TransformComponent>(inserted);
var entity = sim.SpawnEntity(null, new EntityCoordinates(new EntityUid(1), new Vector2(0, 0)));
var container = containerSys.MakeContainer<Container>(owner, "dummy");
@@ -115,13 +118,13 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
var container2 = containerSys.MakeContainer<Container>(inserted, "dummy");
Assert.That(container2.Insert(entity), Is.True);
Assert.That(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(entity).ParentUid, Is.EqualTo(inserted));
Assert.That(entManager.GetComponent<TransformComponent>(entity).ParentUid, Is.EqualTo(inserted));
Assert.That(container2.Remove(entity), Is.True);
Assert.That(container.Contains(entity), Is.True);
Assert.That(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(entity).ParentUid, Is.EqualTo(owner));
Assert.That(entManager.GetComponent<TransformComponent>(entity).ParentUid, Is.EqualTo(owner));
IoCManager.Resolve<IEntityManager>().DeleteEntity(owner);
entManager.DeleteEntity(owner);
Assert.That(transform.Deleted, Is.True);
}
@@ -129,6 +132,7 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
public void TestNestedRemovalWithDenial()
{
var sim = SimulationFactory();
var entMan = sim.Resolve<IEntityManager>();
var containerSys = sim.Resolve<IEntitySystemManager>().GetEntitySystem<ContainerSystem>();
var coordinates = new EntityCoordinates(new EntityUid(1), new Vector2(0, 0));
@@ -141,8 +145,6 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
var container2 = containerSys.MakeContainer<ContainerOnlyContainer>(entityTwo, "dummy");
var container3 = containerSys.MakeContainer<Container>(entityThree, "dummy");
var entMan = IoCManager.Resolve<IEntityManager>();
Assert.That(container.Insert(entityTwo), Is.True);
Assert.That(entMan.GetComponent<TransformComponent>(entityTwo).ParentUid, Is.EqualTo(entityOne));
@@ -205,6 +207,7 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
public void BaseContainer_Insert_True()
{
var sim = SimulationFactory();
var entManager = sim.Resolve<IEntityManager>();
var containerSys = sim.Resolve<IEntitySystemManager>().GetEntitySystem<ContainerSystem>();
var containerEntity = sim.SpawnEntity(null, new EntityCoordinates(new EntityUid(1), new Vector2(0, 0)));
@@ -216,8 +219,8 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
Assert.That(result, Is.True);
Assert.That(container.ContainedEntities.Count, Is.EqualTo(1));
Assert.That(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(containerEntity).ChildCount, Is.EqualTo(1));
Assert.That(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(containerEntity).ChildEntities.First(), Is.EqualTo(insertEntity));
Assert.That(entManager.GetComponent<TransformComponent>(containerEntity).ChildCount, Is.EqualTo(1));
Assert.That(entManager.GetComponent<TransformComponent>(containerEntity).ChildEntities.First(), Is.EqualTo(insertEntity));
result = insertEntity.TryGetContainerMan(out var resultContainerMan);
Assert.That(result, Is.True);
@@ -274,7 +277,7 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
container.ShowContents = true;
container.Insert(childEnt);
var containerMan = entManager.GetComponent<IContainerManager>(entity);
var containerMan = entManager.GetComponent<ContainerManagerComponent>(entity);
var getState = new ComponentGetState();
entManager.EventBus.RaiseComponentEvent(containerMan, ref getState);
var state = (ContainerManagerComponent.ContainerManagerComponentState)getState.State!;
@@ -341,7 +344,7 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
public override bool CanInsert(EntityUid toinsert, IEntityManager? entMan = null)
{
IoCManager.Resolve(ref entMan);
return entMan.TryGetComponent(toinsert, out IContainerManager? _);
return entMan.TryGetComponent(toinsert, out ContainerManagerComponent? _);
}
}
}