Remove IoC resolves in BaseNetConnectorNodeGroup and friends (#20333)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<TNetType> is slow as shit.
|
||||
public interface IBaseNetConnectorComponent<in TNetType>
|
||||
{
|
||||
public TNetType? Net { set; }
|
||||
|
||||
@@ -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>, IApcNet
|
||||
{
|
||||
private PowerNetSystem? _powerNetSystem;
|
||||
|
||||
[ViewVariables] public readonly List<ApcComponent> Apcs = new();
|
||||
[ViewVariables] public readonly List<ApcPowerProviderComponent> 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>();
|
||||
_powerNetSystem.InitApcNet(this);
|
||||
PowerNetSystem.InitApcNet(this);
|
||||
}
|
||||
|
||||
public override void AfterRemake(IEnumerable<IGrouping<INodeGroup?, Node>> 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<IEntityManager>().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<IEntityManager>().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<IApcNet> 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);
|
||||
|
||||
@@ -6,29 +6,33 @@ namespace Content.Server.Power.NodeGroups
|
||||
{
|
||||
public abstract class BaseNetConnectorNodeGroup<TNetType> : BaseNodeGroup
|
||||
{
|
||||
protected IEntityManager EntMan = default!;
|
||||
|
||||
public override void Initialize(Node sourceNode, IEntityManager entMan)
|
||||
{
|
||||
base.Initialize(sourceNode, entMan);
|
||||
EntMan = entMan;
|
||||
}
|
||||
|
||||
public override void LoadNodes(List<Node> groupNodes)
|
||||
{
|
||||
base.LoadNodes(groupNodes);
|
||||
var entManager = IoCManager.Resolve<IEntityManager>();
|
||||
|
||||
foreach (var node in groupNodes)
|
||||
{
|
||||
var newNetConnectorComponents = new List<IBaseNetConnectorComponent<TNetType>>();
|
||||
|
||||
foreach (var comp in entManager.GetComponents<IBaseNetConnectorComponent<TNetType>>(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<IBaseNetConnectorComponent<TNetType>>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<TNetType> : BaseNetConnectorNodeGroup<TNetTyp
|
||||
{
|
||||
[ViewVariables] public readonly List<PowerConsumerComponent> Consumers = new();
|
||||
[ViewVariables] public readonly List<PowerSupplierComponent> 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<PowerNetSystem>();
|
||||
}
|
||||
|
||||
public void AddConsumer(PowerConsumerComponent consumer)
|
||||
{
|
||||
DebugTools.Assert(consumer.NetworkLoad.LinkedNetwork == default);
|
||||
|
||||
@@ -23,27 +23,20 @@ namespace Content.Server.Power.NodeGroups
|
||||
[UsedImplicitly]
|
||||
public sealed partial class PowerNet : BasePowerNet<IPowerNet>, IPowerNet
|
||||
{
|
||||
private PowerNetSystem? _powerNetSystem;
|
||||
private IEntityManager? _entMan;
|
||||
|
||||
[ViewVariables] public readonly List<BatteryChargerComponent> Chargers = new();
|
||||
[ViewVariables] public readonly List<BatteryDischargerComponent> Dischargers = new();
|
||||
|
||||
public override void Initialize(Node sourceNode, IEntityManager entMan)
|
||||
{
|
||||
base.Initialize(sourceNode, entMan);
|
||||
|
||||
_entMan = entMan;
|
||||
|
||||
_powerNetSystem = entMan.EntitySysManager.GetEntitySystem<PowerNetSystem>();
|
||||
_powerNetSystem.InitPowerNet(this);
|
||||
PowerNetSystem.InitPowerNet(this);
|
||||
}
|
||||
|
||||
public override void AfterRemake(IEnumerable<IGrouping<INodeGroup?, Node>> newGroups)
|
||||
{
|
||||
base.AfterRemake(newGroups);
|
||||
|
||||
_powerNetSystem?.DestroyPowerNet(this);
|
||||
PowerNetSystem?.DestroyPowerNet(this);
|
||||
}
|
||||
|
||||
protected override void SetNetConnectorNet(IBaseNetConnectorComponent<IPowerNet> 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<PowerNetworkBatteryComponent>(discharger.Owner);
|
||||
var battery = EntMan.GetComponent<PowerNetworkBatteryComponent>(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<PowerNetworkBatteryComponent>(charger.Owner);
|
||||
var battery = EntMan.GetComponent<PowerNetworkBatteryComponent>(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);
|
||||
|
||||
Reference in New Issue
Block a user