From: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Date: Tue, 19 Sep 2023 12:44:49 +0000 (+1200) Subject: Remove IoC resolves in BaseNetConnectorNodeGroup and friends (#20333) X-Git-Url: https://git.smokeofanarchy.ru/gitweb.cgi?a=commitdiff_plain;h=86fa8ae180e9e96f59753f7a77618491a40b96b3;p=space-station-14.git Remove IoC resolves in BaseNetConnectorNodeGroup and friends (#20333) --- diff --git a/Content.Server/Power/Components/ApcComponent.cs b/Content.Server/Power/Components/ApcComponent.cs index 2a31a6ba4d..8f74146b45 100644 --- a/Content.Server/Power/Components/ApcComponent.cs +++ b/Content.Server/Power/Components/ApcComponent.cs @@ -37,13 +37,14 @@ public sealed partial class ApcComponent : BaseApcNetComponent public static TimeSpan VisualsChangeDelay = TimeSpan.FromSeconds(1); // TODO ECS power a little better! + // End the suffering protected override void AddSelfToNet(IApcNet apcNet) { - apcNet.AddApc(this); + apcNet.AddApc(Owner, this); } protected override void RemoveSelfFromNet(IApcNet apcNet) { - apcNet.RemoveApc(this); + apcNet.RemoveApc(Owner, this); } } diff --git a/Content.Server/Power/Components/BaseNetConnectorComponent.cs b/Content.Server/Power/Components/BaseNetConnectorComponent.cs index 174c62a1df..551b5f3621 100644 --- a/Content.Server/Power/Components/BaseNetConnectorComponent.cs +++ b/Content.Server/Power/Components/BaseNetConnectorComponent.cs @@ -5,6 +5,9 @@ using Content.Server.NodeContainer.NodeGroups; namespace Content.Server.Power.Components { + // TODO find a way to just remove this or turn it into one component. + // Component interface queries require enumerating over ALL of an entities components. + // So BaseNetConnectorNodeGroup is slow as shit. public interface IBaseNetConnectorComponent { public TNetType? Net { set; } diff --git a/Content.Server/Power/NodeGroups/ApcNet.cs b/Content.Server/Power/NodeGroups/ApcNet.cs index 32bb4e33b1..8c0b89b507 100644 --- a/Content.Server/Power/NodeGroups/ApcNet.cs +++ b/Content.Server/Power/NodeGroups/ApcNet.cs @@ -9,9 +9,9 @@ namespace Content.Server.Power.NodeGroups { public interface IApcNet : IBasePowerNet { - void AddApc(ApcComponent apc); + void AddApc(EntityUid uid, ApcComponent apc); - void RemoveApc(ApcComponent apc); + void RemoveApc(EntityUid uid, ApcComponent apc); void AddPowerProvider(ApcPowerProviderComponent provider); @@ -24,8 +24,6 @@ namespace Content.Server.Power.NodeGroups [UsedImplicitly] public sealed partial class ApcNet : BasePowerNet, IApcNet { - private PowerNetSystem? _powerNetSystem; - [ViewVariables] public readonly List Apcs = new(); [ViewVariables] public readonly List Providers = new(); @@ -39,30 +37,28 @@ namespace Content.Server.Power.NodeGroups public override void Initialize(Node sourceNode, IEntityManager entMan) { base.Initialize(sourceNode, entMan); - - _powerNetSystem = entMan.EntitySysManager.GetEntitySystem(); - _powerNetSystem.InitApcNet(this); + PowerNetSystem.InitApcNet(this); } public override void AfterRemake(IEnumerable> newGroups) { base.AfterRemake(newGroups); - _powerNetSystem?.DestroyApcNet(this); + PowerNetSystem?.DestroyApcNet(this); } - public void AddApc(ApcComponent apc) + public void AddApc(EntityUid uid, ApcComponent apc) { - if (IoCManager.Resolve().TryGetComponent(apc.Owner, out PowerNetworkBatteryComponent? netBattery)) + if (EntMan.TryGetComponent(uid, out PowerNetworkBatteryComponent? netBattery)) netBattery.NetworkBattery.LinkedNetworkDischarging = default; QueueNetworkReconnect(); Apcs.Add(apc); } - public void RemoveApc(ApcComponent apc) + public void RemoveApc(EntityUid uid, ApcComponent apc) { - if (IoCManager.Resolve().TryGetComponent(apc.Owner, out PowerNetworkBatteryComponent? netBattery)) + if (EntMan.TryGetComponent(uid, out PowerNetworkBatteryComponent? netBattery)) netBattery.NetworkBattery.LinkedNetworkDischarging = default; QueueNetworkReconnect(); @@ -85,7 +81,7 @@ namespace Content.Server.Power.NodeGroups public override void QueueNetworkReconnect() { - _powerNetSystem?.QueueReconnectApcNet(this); + PowerNetSystem?.QueueReconnectApcNet(this); } protected override void SetNetConnectorNet(IBaseNetConnectorComponent netConnectorComponent) @@ -95,12 +91,9 @@ namespace Content.Server.Power.NodeGroups public override string? GetDebugData() { - if (_powerNetSystem == null) - return null; - // This is just recycling the multi-tool examine. - var ps = _powerNetSystem.GetNetworkStatistics(NetworkNode); + var ps = PowerNetSystem.GetNetworkStatistics(NetworkNode); float storageRatio = ps.InStorageCurrent / Math.Max(ps.InStorageMax, 1.0f); float outStorageRatio = ps.OutStorageCurrent / Math.Max(ps.OutStorageMax, 1.0f); diff --git a/Content.Server/Power/NodeGroups/BaseNetConnectorNodeGroup.cs b/Content.Server/Power/NodeGroups/BaseNetConnectorNodeGroup.cs index 97b585d367..d70fbceed3 100644 --- a/Content.Server/Power/NodeGroups/BaseNetConnectorNodeGroup.cs +++ b/Content.Server/Power/NodeGroups/BaseNetConnectorNodeGroup.cs @@ -6,29 +6,33 @@ namespace Content.Server.Power.NodeGroups { public abstract class BaseNetConnectorNodeGroup : BaseNodeGroup { + protected IEntityManager EntMan = default!; + + public override void Initialize(Node sourceNode, IEntityManager entMan) + { + base.Initialize(sourceNode, entMan); + EntMan = entMan; + } + public override void LoadNodes(List groupNodes) { base.LoadNodes(groupNodes); - var entManager = IoCManager.Resolve(); foreach (var node in groupNodes) { - var newNetConnectorComponents = new List>(); - - foreach (var comp in entManager.GetComponents>(node.Owner)) + // TODO POWER PERFORMANCE + // Replace this with TryComps or some other sane way of doing this, the current solution is awful. + // This allocates an array, copies ALL of an entities components over, and then iterates over them to + // yield any that implement the interface. + foreach (var comp in EntMan.GetComponents>(node.Owner)) { if ((comp.NodeId == null || comp.NodeId == node.Name) && (NodeGroupID) comp.Voltage == node.NodeGroupID) { - newNetConnectorComponents.Add(comp); + SetNetConnectorNet(comp); } } - - foreach (var netConnector in newNetConnectorComponents) - { - SetNetConnectorNet(netConnector); - } } } diff --git a/Content.Server/Power/NodeGroups/BasePowerNet.cs b/Content.Server/Power/NodeGroups/BasePowerNet.cs index 295740d29e..99fc64ad48 100644 --- a/Content.Server/Power/NodeGroups/BasePowerNet.cs +++ b/Content.Server/Power/NodeGroups/BasePowerNet.cs @@ -1,4 +1,6 @@ -using Content.Server.Power.Components; +using Content.Server.NodeContainer.Nodes; +using Content.Server.Power.Components; +using Content.Server.Power.EntitySystems; using Content.Server.Power.Pow3r; using Robust.Shared.Utility; @@ -9,10 +11,17 @@ public abstract class BasePowerNet : BaseNetConnectorNodeGroup Consumers = new(); [ViewVariables] public readonly List Suppliers = new(); + public PowerNetSystem PowerNetSystem = default!; [ViewVariables] public PowerState.Network NetworkNode { get; } = new(); + public override void Initialize(Node sourceNode, IEntityManager entMan) + { + base.Initialize(sourceNode, entMan); + PowerNetSystem = entMan.EntitySysManager.GetEntitySystem(); + } + public void AddConsumer(PowerConsumerComponent consumer) { DebugTools.Assert(consumer.NetworkLoad.LinkedNetwork == default); diff --git a/Content.Server/Power/NodeGroups/PowerNet.cs b/Content.Server/Power/NodeGroups/PowerNet.cs index 0bbf11db72..edbc5661e2 100644 --- a/Content.Server/Power/NodeGroups/PowerNet.cs +++ b/Content.Server/Power/NodeGroups/PowerNet.cs @@ -23,27 +23,20 @@ namespace Content.Server.Power.NodeGroups [UsedImplicitly] public sealed partial class PowerNet : BasePowerNet, IPowerNet { - private PowerNetSystem? _powerNetSystem; - private IEntityManager? _entMan; - [ViewVariables] public readonly List Chargers = new(); [ViewVariables] public readonly List Dischargers = new(); public override void Initialize(Node sourceNode, IEntityManager entMan) { base.Initialize(sourceNode, entMan); - - _entMan = entMan; - - _powerNetSystem = entMan.EntitySysManager.GetEntitySystem(); - _powerNetSystem.InitPowerNet(this); + PowerNetSystem.InitPowerNet(this); } public override void AfterRemake(IEnumerable> newGroups) { base.AfterRemake(newGroups); - _powerNetSystem?.DestroyPowerNet(this); + PowerNetSystem?.DestroyPowerNet(this); } protected override void SetNetConnectorNet(IBaseNetConnectorComponent netConnectorComponent) @@ -53,10 +46,10 @@ namespace Content.Server.Power.NodeGroups public void AddDischarger(BatteryDischargerComponent discharger) { - if (_entMan == null) + if (EntMan == null) return; - var battery = _entMan.GetComponent(discharger.Owner); + var battery = EntMan.GetComponent(discharger.Owner); DebugTools.Assert(battery.NetworkBattery.LinkedNetworkDischarging == default); battery.NetworkBattery.LinkedNetworkDischarging = default; Dischargers.Add(discharger); @@ -65,11 +58,11 @@ namespace Content.Server.Power.NodeGroups public void RemoveDischarger(BatteryDischargerComponent discharger) { - if (_entMan == null) + if (EntMan == null) return; // Can be missing if the entity is being deleted, not a big deal. - if (_entMan.TryGetComponent(discharger.Owner, out PowerNetworkBatteryComponent? battery)) + if (EntMan.TryGetComponent(discharger.Owner, out PowerNetworkBatteryComponent? battery)) { // Linked network can be default if it was re-connected twice in one tick. DebugTools.Assert(battery.NetworkBattery.LinkedNetworkDischarging == default || battery.NetworkBattery.LinkedNetworkDischarging == NetworkNode.Id); @@ -82,10 +75,10 @@ namespace Content.Server.Power.NodeGroups public void AddCharger(BatteryChargerComponent charger) { - if (_entMan == null) + if (EntMan == null) return; - var battery = _entMan.GetComponent(charger.Owner); + var battery = EntMan.GetComponent(charger.Owner); DebugTools.Assert(battery.NetworkBattery.LinkedNetworkCharging == default); battery.NetworkBattery.LinkedNetworkCharging = default; Chargers.Add(charger); @@ -94,11 +87,11 @@ namespace Content.Server.Power.NodeGroups public void RemoveCharger(BatteryChargerComponent charger) { - if (_entMan == null) + if (EntMan == null) return; // Can be missing if the entity is being deleted, not a big deal. - if (_entMan.TryGetComponent(charger.Owner, out PowerNetworkBatteryComponent? battery)) + if (EntMan.TryGetComponent(charger.Owner, out PowerNetworkBatteryComponent? battery)) { // Linked network can be default if it was re-connected twice in one tick. DebugTools.Assert(battery.NetworkBattery.LinkedNetworkCharging == default || battery.NetworkBattery.LinkedNetworkCharging == NetworkNode.Id); @@ -111,16 +104,16 @@ namespace Content.Server.Power.NodeGroups public override void QueueNetworkReconnect() { - _powerNetSystem?.QueueReconnectPowerNet(this); + PowerNetSystem?.QueueReconnectPowerNet(this); } public override string? GetDebugData() { - if (_powerNetSystem == null) + if (PowerNetSystem == null) return null; // This is just recycling the multi-tool examine. - var ps = _powerNetSystem.GetNetworkStatistics(NetworkNode); + var ps = PowerNetSystem.GetNetworkStatistics(NetworkNode); float storageRatio = ps.InStorageCurrent / Math.Max(ps.InStorageMax, 1.0f); float outStorageRatio = ps.OutStorageCurrent / Math.Max(ps.OutStorageMax, 1.0f);