Rejig device link sink & source startup & shutdown (#29035)

* Fix DeviceLinkSinkComponent not updating sources on shutdown

* Log error

* Misc link changes & fixes

* Fix core
This commit is contained in:
Leon Friedrich
2024-06-20 03:03:32 +12:00
committed by GitHub
parent 8a4ce13d1c
commit 20544b0c76
4 changed files with 63 additions and 112 deletions

View File

@@ -1,4 +1,5 @@
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set; using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set;
namespace Content.Shared.DeviceLinking; namespace Content.Shared.DeviceLinking;
@@ -11,13 +12,14 @@ public sealed partial class DeviceLinkSinkComponent : Component
/// <summary> /// <summary>
/// The ports this sink has /// The ports this sink has
/// </summary> /// </summary>
[DataField("ports", customTypeSerializer: typeof(PrototypeIdHashSetSerializer<SinkPortPrototype>))] [DataField]
public HashSet<string>? Ports; public HashSet<ProtoId<SinkPortPrototype>> Ports = new();
/// <summary> /// <summary>
/// Used for removing a sink from all linked sources when it gets removed /// Used for removing a sink from all linked sources when this component gets removed.
/// This is not serialized to yaml as it can be inferred from source components.
/// </summary> /// </summary>
[DataField("links")] [ViewVariables]
public HashSet<EntityUid> LinkedSources = new(); public HashSet<EntityUid> LinkedSources = new();
/// <summary> /// <summary>
@@ -25,14 +27,13 @@ public sealed partial class DeviceLinkSinkComponent : Component
/// The counter is counted down by one every tick if it's higher than 0 /// The counter is counted down by one every tick if it's higher than 0
/// This is for preventing infinite loops /// This is for preventing infinite loops
/// </summary> /// </summary>
[DataField("invokeCounter")] [DataField]
public int InvokeCounter; public int InvokeCounter;
/// <summary> /// <summary>
/// How high the invoke counter is allowed to get before the links to the sink are removed and the DeviceLinkOverloadedEvent gets raised /// How high the invoke counter is allowed to get before the links to the sink are removed and the DeviceLinkOverloadedEvent gets raised
/// If the invoke limit is smaller than 1 the sink can't overload /// If the invoke limit is smaller than 1 the sink can't overload
/// </summary> /// </summary>
[ViewVariables(VVAccess.ReadWrite)] [DataField]
[DataField("invokeLimit")]
public int InvokeLimit = 10; public int InvokeLimit = 10;
} }

View File

@@ -12,12 +12,12 @@ public sealed partial class DeviceLinkSourceComponent : Component
/// The ports the device link source sends signals from /// The ports the device link source sends signals from
/// </summary> /// </summary>
[DataField] [DataField]
public HashSet<ProtoId<SourcePortPrototype>>? Ports; public HashSet<ProtoId<SourcePortPrototype>> Ports = new();
/// <summary> /// <summary>
/// A list of sink uids that got linked for each port /// Dictionary mapping each port to a set of linked sink entities.
/// </summary> /// </summary>
[ViewVariables] [ViewVariables] // This is not serialized as it can be constructed from LinkedPorts
public Dictionary<ProtoId<SourcePortPrototype>, HashSet<EntityUid>> Outputs = new(); public Dictionary<ProtoId<SourcePortPrototype>, HashSet<EntityUid>> Outputs = new();
/// <summary> /// <summary>
@@ -32,7 +32,7 @@ public sealed partial class DeviceLinkSourceComponent : Component
/// The list of source to sink ports for each linked sink entity for easier managing of links /// The list of source to sink ports for each linked sink entity for easier managing of links
/// </summary> /// </summary>
[DataField] [DataField]
public Dictionary<EntityUid, HashSet<(ProtoId<SourcePortPrototype> source, ProtoId<SinkPortPrototype> sink)>> LinkedPorts = new(); public Dictionary<EntityUid, HashSet<(ProtoId<SourcePortPrototype> Source, ProtoId<SinkPortPrototype> Sink)>> LinkedPorts = new();
/// <summary> /// <summary>
/// Limits the range devices can be linked across. /// Limits the range devices can be linked across.

View File

@@ -20,98 +20,56 @@ public abstract class SharedDeviceLinkSystem : EntitySystem
/// <inheritdoc/> /// <inheritdoc/>
public override void Initialize() public override void Initialize()
{ {
SubscribeLocalEvent<DeviceLinkSourceComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<DeviceLinkSourceComponent, ComponentStartup>(OnSourceStartup); SubscribeLocalEvent<DeviceLinkSourceComponent, ComponentStartup>(OnSourceStartup);
SubscribeLocalEvent<DeviceLinkSinkComponent, ComponentStartup>(OnSinkStartup);
SubscribeLocalEvent<DeviceLinkSourceComponent, ComponentRemove>(OnSourceRemoved); SubscribeLocalEvent<DeviceLinkSourceComponent, ComponentRemove>(OnSourceRemoved);
SubscribeLocalEvent<DeviceLinkSinkComponent, ComponentRemove>(OnSinkRemoved); SubscribeLocalEvent<DeviceLinkSinkComponent, ComponentRemove>(OnSinkRemoved);
} }
#region Link Validation #region Link Validation
private void OnInit(EntityUid uid, DeviceLinkSourceComponent component, ComponentInit args)
{
// Populate the output dictionary.
foreach (var (sinkUid, links) in component.LinkedPorts)
{
foreach (var link in links)
{
component.Outputs.GetOrNew(link.source).Add(sinkUid);
}
}
}
/// <summary> /// <summary>
/// Removes invalid links where the saved sink doesn't exist/have a sink component for example /// Removes invalid links where the saved sink doesn't exist/have a sink component for example
/// </summary> /// </summary>
private void OnSourceStartup(EntityUid sourceUid, DeviceLinkSourceComponent sourceComponent, ComponentStartup args) private void OnSourceStartup(Entity<DeviceLinkSourceComponent> source, ref ComponentStartup args)
{ {
List<EntityUid> invalidSinks = new(); List<EntityUid> invalidSinks = new();
foreach (var sinkUid in sourceComponent.LinkedPorts.Keys) List<(string, string)> invalidLinks = new();
foreach (var (sink, links) in source.Comp.LinkedPorts)
{ {
if (!TryComp<DeviceLinkSinkComponent>(sinkUid, out var sinkComponent)) if (!TryComp(sink, out DeviceLinkSinkComponent? sinkComponent))
{ {
invalidSinks.Add(sinkUid); invalidSinks.Add(sink);
foreach (var savedSinks in sourceComponent.Outputs.Values)
{
savedSinks.Remove(sinkUid);
}
continue; continue;
} }
sinkComponent.LinkedSources.Add(sourceUid); foreach (var link in links)
}
foreach (var invalidSink in invalidSinks)
{
sourceComponent.LinkedPorts.Remove(invalidSink);
}
}
/// <summary>
/// Same with <see cref="OnSourceStartup"/> but also checks that the saved ports are present on the sink
/// </summary>
private void OnSinkStartup(EntityUid sinkUid, DeviceLinkSinkComponent sinkComponent, ComponentStartup args)
{
List<EntityUid> invalidSources = new();
foreach (var sourceUid in sinkComponent.LinkedSources)
{
if (!TryComp<DeviceLinkSourceComponent>(sourceUid, out var sourceComponent))
{ {
invalidSources.Add(sourceUid); if (sinkComponent.Ports.Contains(link.Sink) && source.Comp.Ports.Contains(link.Source))
continue; source.Comp.Outputs.GetOrNew(link.Source).Add(sink);
} else
if (!sourceComponent.LinkedPorts.TryGetValue(sinkUid, out var linkedPorts))
{
foreach (var savedSinks in sourceComponent.Outputs.Values)
{
savedSinks.Remove(sinkUid);
}
continue;
}
if (sinkComponent.Ports == null)
continue;
List<(string, string)> invalidLinks = new();
foreach (var link in linkedPorts)
{
if (!sinkComponent.Ports.Contains(link.sink))
invalidLinks.Add(link); invalidLinks.Add(link);
} }
foreach (var invalidLink in invalidLinks) foreach (var link in invalidLinks)
{ {
linkedPorts.Remove(invalidLink); Log.Warning($"Device source {ToPrettyString(source)} contains invalid links to entity {ToPrettyString(sink)}: {link.Item1}->{link.Item2}");
sourceComponent.Outputs.GetValueOrDefault(invalidLink.Item1)?.Remove(sinkUid); links.Remove(link);
} }
if (links.Count == 0)
{
invalidSinks.Add(sink);
continue;
}
invalidLinks.Clear();
sinkComponent.LinkedSources.Add(source.Owner);
} }
foreach (var invalidSource in invalidSources) foreach (var sink in invalidSinks)
{ {
sinkComponent.LinkedSources.Remove(invalidSource); source.Comp.LinkedPorts.Remove(sink);
Log.Warning($"Device source {ToPrettyString(source)} contains invalid sink: {ToPrettyString(sink)}");
} }
} }
#endregion #endregion
@@ -119,26 +77,29 @@ public abstract class SharedDeviceLinkSystem : EntitySystem
/// <summary> /// <summary>
/// Ensures that its links get deleted when a source gets removed /// Ensures that its links get deleted when a source gets removed
/// </summary> /// </summary>
private void OnSourceRemoved(EntityUid uid, DeviceLinkSourceComponent component, ComponentRemove args) private void OnSourceRemoved(Entity<DeviceLinkSourceComponent> source, ref ComponentRemove args)
{ {
var query = GetEntityQuery<DeviceLinkSinkComponent>(); var query = GetEntityQuery<DeviceLinkSinkComponent>();
foreach (var sinkUid in component.LinkedPorts.Keys) foreach (var sinkUid in source.Comp.LinkedPorts.Keys)
{ {
if (query.TryGetComponent(sinkUid, out var sink)) if (query.TryGetComponent(sinkUid, out var sink))
RemoveSinkFromSourceInternal(uid, sinkUid, component, sink); RemoveSinkFromSourceInternal(source, sinkUid, source, sink);
else
Log.Error($"Device source {ToPrettyString(source)} links to invalid entity: {ToPrettyString(sinkUid)}");
} }
} }
/// <summary> /// <summary>
/// Ensures that its links get deleted when a sink gets removed /// Ensures that its links get deleted when a sink gets removed
/// </summary> /// </summary>
private void OnSinkRemoved(EntityUid sinkUid, DeviceLinkSinkComponent sinkComponent, ComponentRemove args) private void OnSinkRemoved(Entity<DeviceLinkSinkComponent> sink, ref ComponentRemove args)
{ {
var query = GetEntityQuery<DeviceLinkSourceComponent>(); foreach (var sourceUid in sink.Comp.LinkedSources)
foreach (var linkedSource in sinkComponent.LinkedSources)
{ {
if (query.TryGetComponent(sinkUid, out var source)) if (TryComp(sourceUid, out DeviceLinkSourceComponent? source))
RemoveSinkFromSourceInternal(linkedSource, sinkUid, source, sinkComponent); RemoveSinkFromSourceInternal(sourceUid, sink, source, sink);
else
Log.Error($"Device sink {ToPrettyString(sink)} source list contains invalid entity: {ToPrettyString(sourceUid)}");
} }
} }
@@ -146,36 +107,36 @@ public abstract class SharedDeviceLinkSystem : EntitySystem
/// <summary> /// <summary>
/// Convenience function to add several ports to an entity /// Convenience function to add several ports to an entity
/// </summary> /// </summary>
public void EnsureSourcePorts(EntityUid uid, params string[] ports) public void EnsureSourcePorts(EntityUid uid, params ProtoId<SourcePortPrototype>[] ports)
{ {
if (ports.Length == 0) if (ports.Length == 0)
return; return;
var comp = EnsureComp<DeviceLinkSourceComponent>(uid); var comp = EnsureComp<DeviceLinkSourceComponent>(uid);
comp.Ports ??= new HashSet<ProtoId<SourcePortPrototype>>();
foreach (var port in ports) foreach (var port in ports)
{ {
DebugTools.Assert(_prototypeManager.HasIndex<SourcePortPrototype>(port)); if (!_prototypeManager.HasIndex(port))
comp.Ports?.Add(port); Log.Error($"Attempted to add invalid port {port} to {ToPrettyString(uid)}");
else
comp.Ports.Add(port);
} }
} }
/// <summary> /// <summary>
/// Convenience function to add several ports to an entity. /// Convenience function to add several ports to an entity.
/// </summary> /// </summary>
public void EnsureSinkPorts(EntityUid uid, params string[] ports) public void EnsureSinkPorts(EntityUid uid, params ProtoId<SinkPortPrototype>[] ports)
{ {
if (ports.Length == 0) if (ports.Length == 0)
return; return;
var comp = EnsureComp<DeviceLinkSinkComponent>(uid); var comp = EnsureComp<DeviceLinkSinkComponent>(uid);
comp.Ports ??= new HashSet<string>();
foreach (var port in ports) foreach (var port in ports)
{ {
DebugTools.Assert(_prototypeManager.HasIndex<SinkPortPrototype>(port)); if (!_prototypeManager.HasIndex(port))
comp.Ports?.Add(port); Log.Error($"Attempted to add invalid port {port} to {ToPrettyString(uid)}");
else
comp.Ports.Add(port);
} }
} }
@@ -185,13 +146,13 @@ public abstract class SharedDeviceLinkSystem : EntitySystem
/// <returns>A list of source port prototypes</returns> /// <returns>A list of source port prototypes</returns>
public List<SourcePortPrototype> GetSourcePorts(EntityUid sourceUid, DeviceLinkSourceComponent? sourceComponent = null) public List<SourcePortPrototype> GetSourcePorts(EntityUid sourceUid, DeviceLinkSourceComponent? sourceComponent = null)
{ {
if (!Resolve(sourceUid, ref sourceComponent) || sourceComponent.Ports == null) if (!Resolve(sourceUid, ref sourceComponent))
return new List<SourcePortPrototype>(); return new List<SourcePortPrototype>();
var sourcePorts = new List<SourcePortPrototype>(); var sourcePorts = new List<SourcePortPrototype>();
foreach (var port in sourceComponent.Ports) foreach (var port in sourceComponent.Ports)
{ {
sourcePorts.Add(_prototypeManager.Index<SourcePortPrototype>(port)); sourcePorts.Add(_prototypeManager.Index(port));
} }
return sourcePorts; return sourcePorts;
@@ -203,13 +164,13 @@ public abstract class SharedDeviceLinkSystem : EntitySystem
/// <returns>A list of sink port prototypes</returns> /// <returns>A list of sink port prototypes</returns>
public List<SinkPortPrototype> GetSinkPorts(EntityUid sinkUid, DeviceLinkSinkComponent? sinkComponent = null) public List<SinkPortPrototype> GetSinkPorts(EntityUid sinkUid, DeviceLinkSinkComponent? sinkComponent = null)
{ {
if (!Resolve(sinkUid, ref sinkComponent) || sinkComponent.Ports == null) if (!Resolve(sinkUid, ref sinkComponent))
return new List<SinkPortPrototype>(); return new List<SinkPortPrototype>();
var sinkPorts = new List<SinkPortPrototype>(); var sinkPorts = new List<SinkPortPrototype>();
foreach (var port in sinkComponent.Ports) foreach (var port in sinkComponent.Ports)
{ {
sinkPorts.Add(_prototypeManager.Index<SinkPortPrototype>(port)); sinkPorts.Add(_prototypeManager.Index(port));
} }
return sinkPorts; return sinkPorts;
@@ -315,9 +276,6 @@ public abstract class SharedDeviceLinkSystem : EntitySystem
if (!Resolve(sourceUid, ref sourceComponent) || !Resolve(sinkUid, ref sinkComponent)) if (!Resolve(sourceUid, ref sourceComponent) || !Resolve(sinkUid, ref sinkComponent))
return; return;
if (sourceComponent.Ports == null || sinkComponent.Ports == null)
return;
if (!InRange(sourceUid, sinkUid, sourceComponent.Range)) if (!InRange(sourceUid, sinkUid, sourceComponent.Range))
{ {
if (userId != null) if (userId != null)
@@ -391,7 +349,7 @@ public abstract class SharedDeviceLinkSystem : EntitySystem
else else
{ {
Log.Error($"Attempted to remove link between {ToPrettyString(sourceUid)} and {ToPrettyString(sinkUid)}, but the sink component was missing."); Log.Error($"Attempted to remove link between {ToPrettyString(sourceUid)} and {ToPrettyString(sinkUid)}, but the sink component was missing.");
sourceComponent.LinkedPorts.Remove(sourceUid); sourceComponent.LinkedPorts.Remove(sinkUid);
} }
} }
@@ -414,12 +372,10 @@ public abstract class SharedDeviceLinkSystem : EntitySystem
sinkComponent.LinkedSources.Remove(sourceUid); sinkComponent.LinkedSources.Remove(sourceUid);
sourceComponent.LinkedPorts.Remove(sinkUid); sourceComponent.LinkedPorts.Remove(sinkUid);
var outputLists = sourceComponent.Outputs.Values; foreach (var outputList in sourceComponent.Outputs.Values)
foreach (var outputList in outputLists)
{ {
outputList.Remove(sinkUid); outputList.Remove(sinkUid);
} }
} }
/// <summary> /// <summary>
@@ -438,9 +394,6 @@ public abstract class SharedDeviceLinkSystem : EntitySystem
if (!Resolve(sourceUid, ref sourceComponent) || !Resolve(sinkUid, ref sinkComponent)) if (!Resolve(sourceUid, ref sourceComponent) || !Resolve(sinkUid, ref sinkComponent))
return false; return false;
if (sourceComponent.Ports == null || sinkComponent.Ports == null)
return false;
var outputs = sourceComponent.Outputs.GetOrNew(source); var outputs = sourceComponent.Outputs.GetOrNew(source);
var linkedPorts = sourceComponent.LinkedPorts.GetOrNew(sinkUid); var linkedPorts = sourceComponent.LinkedPorts.GetOrNew(sinkUid);

View File

@@ -115412,9 +115412,6 @@ entities:
rot: 3.141592653589793 rad rot: 3.141592653589793 rad
pos: 50.5,-2.5 pos: 50.5,-2.5
parent: 2 parent: 2
- type: DeviceLinkSource
linkedPorts:
6516: []
- proto: SignalButtonDirectional - proto: SignalButtonDirectional
entities: entities:
- uid: 2100 - uid: 2100