From 1f7a9bdf0a6b744555ba3cb2f4852a8cd23eeb66 Mon Sep 17 00:00:00 2001 From: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Date: Sun, 26 May 2024 15:19:52 +1200 Subject: [PATCH] Remove container IoC resolves (#5172) --- .../EntitySystems/ContainerSystem.cs | 2 +- Robust.Shared/Containers/BaseContainer.cs | 21 ++++++---- Robust.Shared/Containers/Container.cs | 11 +----- .../Containers/ContainerManagerComponent.cs | 4 +- Robust.Shared/Containers/ContainerSlot.cs | 11 +----- .../Containers/SharedContainerSystem.cs | 39 +++++++++++++++---- 6 files changed, 48 insertions(+), 40 deletions(-) diff --git a/Robust.Client/GameObjects/EntitySystems/ContainerSystem.cs b/Robust.Client/GameObjects/EntitySystems/ContainerSystem.cs index 661d412cdfd..46ec41efa53 100644 --- a/Robust.Client/GameObjects/EntitySystems/ContainerSystem.cs +++ b/Robust.Client/GameObjects/EntitySystems/ContainerSystem.cs @@ -120,7 +120,7 @@ private void HandleComponentState(EntityUid uid, ContainerManagerComponent compo { var type = _serializer.FindSerializedType(typeof(BaseContainer), data.ContainerType); container = _dynFactory.CreateInstanceUnchecked(type!, inject:false); - InitContainer(container, (uid, component), id); + container.Init(this, id, (uid, component)); component.Containers.Add(id, container); } diff --git a/Robust.Shared/Containers/BaseContainer.cs b/Robust.Shared/Containers/BaseContainer.cs index 650b197f426..33588e04bdf 100644 --- a/Robust.Shared/Containers/BaseContainer.cs +++ b/Robust.Shared/Containers/BaseContainer.cs @@ -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; @@ -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 owner) + { + DebugTools.Assert(ID == null || ID == id); + ID = id; + Owner = owner; + Manager = owner; + System = system; + } + /// /// Readonly collection of all the entities contained within this specific container /// diff --git a/Robust.Shared/Containers/Container.cs b/Robust.Shared/Containers/Container.cs index dab821254a3..40a2c94ab02 100644 --- a/Robust.Shared/Containers/Container.cs +++ b/Robust.Shared/Containers/Container.cs @@ -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 @@ -51,14 +49,7 @@ public override bool Contains(EntityUid contained) if (!_containerList.Contains(contained)) return false; -#if DEBUG - if (IoCManager.Resolve().ApplyingState) - return true; - - var entMan = IoCManager.Resolve(); - var flags = entMan.GetComponent(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; } diff --git a/Robust.Shared/Containers/ContainerManagerComponent.cs b/Robust.Shared/Containers/ContainerManagerComponent.cs index 960084e6e22..b96ac472b3d 100644 --- a/Robust.Shared/Containers/ContainerManagerComponent.cs +++ b/Robust.Shared/Containers/ContainerManagerComponent.cs @@ -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)); } } diff --git a/Robust.Shared/Containers/ContainerSlot.cs b/Robust.Shared/Containers/ContainerSlot.cs index 2581f878f3b..28722effd15 100644 --- a/Robust.Shared/Containers/ContainerSlot.cs +++ b/Robust.Shared/Containers/ContainerSlot.cs @@ -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 @@ -59,14 +57,7 @@ public override bool Contains(EntityUid contained) if (contained != ContainedEntity) return false; -#if DEBUG - if (IoCManager.Resolve().ApplyingState) - return true; - - var entMan = IoCManager.Resolve(); - var flags = entMan.GetComponent(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; } diff --git a/Robust.Shared/Containers/SharedContainerSystem.cs b/Robust.Shared/Containers/SharedContainerSystem.cs index 675f986d02a..0707716ee36 100644 --- a/Robust.Shared/Containers/SharedContainerSystem.cs +++ b/Robust.Shared/Containers/SharedContainerSystem.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Linq; using Robust.Shared.GameObjects; @@ -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 @@ -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 _managerQuery; private EntityQuery _gridQuery; @@ -39,6 +42,7 @@ public override void Initialize() base.Initialize(); SubscribeLocalEvent(OnParentChanged); + SubscribeLocalEvent(OnInit); SubscribeLocalEvent(OnStartupValidation); SubscribeLocalEvent(OnContainerGetState); SubscribeLocalEvent(OnContainerManagerRemove); @@ -52,6 +56,14 @@ public override void Initialize() TransformQuery = GetEntityQuery(); } + private void OnInit(Entity 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 containerSet = @@ -101,18 +113,12 @@ public T MakeContainer(EntityUid uid, string id, ContainerManagerComponent? c throw new ArgumentException($"Container with specified ID already exists: '{id}'"); var container = _dynFactory.CreateInstanceUnchecked(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 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); @@ -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")] @@ -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)}"); + } } }