Skip to content

Commit

Permalink
Remove container IoC resolves (space-wizards#5172)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectroJr authored May 26, 2024
1 parent 796abe1 commit 1f7a9bd
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Robust.Client/GameObjects/EntitySystems/ContainerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void HandleComponentState(EntityUid uid, ContainerManagerComponent compo
{
var type = _serializer.FindSerializedType(typeof(BaseContainer), data.ContainerType);
container = _dynFactory.CreateInstanceUnchecked<BaseContainer>(type!, inject:false);
InitContainer(container, (uid, component), id);
container.Init(this, id, (uid, component));
component.Containers.Add(id, container);
}

Expand Down
21 changes: 13 additions & 8 deletions Robust.Shared/Containers/BaseContainer.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Network;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
Expand All @@ -23,6 +15,19 @@ namespace Robust.Shared.Containers
[ImplicitDataDefinitionForInheritors]
public abstract partial class BaseContainer
{
// Will be null until after the component has been initialized.
protected SharedContainerSystem? System;

[Access(typeof(SharedContainerSystem), typeof(ContainerManagerComponent))]
internal void Init(SharedContainerSystem system, string id, Entity<ContainerManagerComponent> owner)
{
DebugTools.Assert(ID == null || ID == id);
ID = id;
Owner = owner;
Manager = owner;
System = system;
}

/// <summary>
/// Readonly collection of all the entities contained within this specific container
/// </summary>
Expand Down
11 changes: 1 addition & 10 deletions Robust.Shared/Containers/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
using System.Collections.Generic;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Timing;
using Robust.Shared.Utility;

namespace Robust.Shared.Containers
Expand Down Expand Up @@ -51,14 +49,7 @@ public override bool Contains(EntityUid contained)
if (!_containerList.Contains(contained))
return false;

#if DEBUG
if (IoCManager.Resolve<IGameTiming>().ApplyingState)
return true;

var entMan = IoCManager.Resolve<IEntityManager>();
var flags = entMan.GetComponent<MetaDataComponent>(contained).Flags;
DebugTools.Assert((flags & MetaDataFlags.InContainer) != 0, $"Entity has bad container flags. Ent: {entMan.ToPrettyString(contained)}. Container: {ID}, Owner: {entMan.ToPrettyString(Owner)}");
#endif
System?.AssertInContainer(contained, this);
return true;
}

Expand Down
4 changes: 1 addition & 3 deletions Robust.Shared/Containers/ContainerManagerComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ void ISerializationHooks.AfterDeserialization()
{
foreach (var (id, container) in Containers)
{
container.ID = id;
container.Owner = Owner;
container.Manager = this;
container.Init(default!, id, (Owner, this));
}
}

Expand Down
11 changes: 1 addition & 10 deletions Robust.Shared/Containers/ContainerSlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
using System.Collections.Generic;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Timing;
using Robust.Shared.Utility;

namespace Robust.Shared.Containers
Expand Down Expand Up @@ -59,14 +57,7 @@ public override bool Contains(EntityUid contained)
if (contained != ContainedEntity)
return false;

#if DEBUG
if (IoCManager.Resolve<IGameTiming>().ApplyingState)
return true;

var entMan = IoCManager.Resolve<IEntityManager>();
var flags = entMan.GetComponent<MetaDataComponent>(contained).Flags;
DebugTools.Assert((flags & MetaDataFlags.InContainer) != 0, $"Entity has bad container flags. Ent: {entMan.ToPrettyString(contained)}. Container: {ID}, Owner: {entMan.ToPrettyString(Owner)}");
#endif
System?.AssertInContainer(contained, this);
return true;
}

Expand Down
39 changes: 31 additions & 8 deletions Robust.Shared/Containers/SharedContainerSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Robust.Shared.GameObjects;
Expand All @@ -12,6 +13,7 @@
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Timing;
using Robust.Shared.Utility;

namespace Robust.Shared.Containers
Expand All @@ -24,6 +26,7 @@ public abstract partial class SharedContainerSystem
[Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly SharedJointSystem _joint = default!;
[Dependency] private readonly IGameTiming _timing = default!;

private EntityQuery<ContainerManagerComponent> _managerQuery;
private EntityQuery<MapGridComponent> _gridQuery;
Expand All @@ -39,6 +42,7 @@ public override void Initialize()
base.Initialize();

SubscribeLocalEvent<EntParentChangedMessage>(OnParentChanged);
SubscribeLocalEvent<ContainerManagerComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<ContainerManagerComponent, ComponentStartup>(OnStartupValidation);
SubscribeLocalEvent<ContainerManagerComponent, ComponentGetState>(OnContainerGetState);
SubscribeLocalEvent<ContainerManagerComponent, ComponentRemove>(OnContainerManagerRemove);
Expand All @@ -52,6 +56,14 @@ public override void Initialize()
TransformQuery = GetEntityQuery<TransformComponent>();
}

private void OnInit(Entity<ContainerManagerComponent> ent, ref ComponentInit args)
{
foreach (var (id, container) in ent.Comp.Containers)
{
container.Init(this, id, ent);
}
}

private void OnContainerGetState(EntityUid uid, ContainerManagerComponent component, ref ComponentGetState args)
{
Dictionary<string, ContainerManagerComponent.ContainerManagerComponentState.ContainerData> containerSet =
Expand Down Expand Up @@ -101,18 +113,12 @@ public T MakeContainer<T>(EntityUid uid, string id, ContainerManagerComponent? c
throw new ArgumentException($"Container with specified ID already exists: '{id}'");

var container = _dynFactory.CreateInstanceUnchecked<T>(typeof(T), inject: false);
InitContainer(container, (uid, containerManager), id);
container.Init(this, id, (uid, containerManager));
containerManager.Containers[id] = container;
Dirty(uid, containerManager);
return container;
}

protected void InitContainer(BaseContainer container, Entity<ContainerManagerComponent> containerEnt, string id)
{
DebugTools.AssertNull(container.ID);
((container.Owner, container.Manager), container.ID) = (containerEnt, id);
}

public virtual void ShutdownContainer(BaseContainer container)
{
container.InternalShutdown(EntityManager, this, _net.IsClient);
Expand Down Expand Up @@ -178,7 +184,13 @@ public bool TryGetContainer(
return false;
}

return containerManager.Containers.TryGetValue(id, out container);
if (!containerManager.Containers.TryGetValue(id, out container))
return false;

DebugTools.AssertEqual(container.ID, id);
DebugTools.AssertNotNull(container.Manager);
DebugTools.AssertNotEqual(container.Owner, EntityUid.Invalid);
return true;
}

[Obsolete("Use variant without skipExistCheck argument")]
Expand Down Expand Up @@ -689,5 +701,16 @@ protected virtual void OnParentChanged(ref EntParentChangedMessage message)
if (TryComp(message.OldParent, out ContainerManagerComponent? containerManager))
RemoveEntity(message.OldParent.Value, message.Entity, containerManager, message.Transform, meta, reparent: false, force: true);
}

[Conditional("DEBUG"), Access(typeof(BaseContainer))]
public void AssertInContainer(EntityUid uid, BaseContainer container)
{
if (_timing.ApplyingState)
return; // Entity might not yet have had its state updated.

var flags = MetaData(uid).Flags;
DebugTools.Assert((flags & MetaDataFlags.InContainer) != 0,
$"Entity has bad container flags. Ent: {ToPrettyString(uid)}. Container: {container.ID}, Owner: {ToPrettyString(container.Owner)}");
}
}
}

0 comments on commit 1f7a9bd

Please sign in to comment.