/// <inheritdoc/>
public override void Initialize()
{
- SubscribeLocalEvent<DeviceLinkSourceComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<DeviceLinkSourceComponent, ComponentStartup>(OnSourceStartup);
- SubscribeLocalEvent<DeviceLinkSinkComponent, ComponentStartup>(OnSinkStartup);
SubscribeLocalEvent<DeviceLinkSourceComponent, ComponentRemove>(OnSourceRemoved);
SubscribeLocalEvent<DeviceLinkSinkComponent, ComponentRemove>(OnSinkRemoved);
}
#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>
/// Removes invalid links where the saved sink doesn't exist/have a sink component for example
/// </summary>
- private void OnSourceStartup(EntityUid sourceUid, DeviceLinkSourceComponent sourceComponent, ComponentStartup args)
+ private void OnSourceStartup(Entity<DeviceLinkSourceComponent> source, ref ComponentStartup args)
{
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);
- foreach (var savedSinks in sourceComponent.Outputs.Values)
- {
- savedSinks.Remove(sinkUid);
- }
-
+ invalidSinks.Add(sink);
continue;
}
- sinkComponent.LinkedSources.Add(sourceUid);
- }
-
- 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))
+ foreach (var link in links)
{
- invalidSources.Add(sourceUid);
- continue;
+ if (sinkComponent.Ports.Contains(link.Sink) && source.Comp.Ports.Contains(link.Source))
+ source.Comp.Outputs.GetOrNew(link.Source).Add(sink);
+ else
+ invalidLinks.Add(link);
}
- if (!sourceComponent.LinkedPorts.TryGetValue(sinkUid, out var linkedPorts))
+ foreach (var link in invalidLinks)
{
- foreach (var savedSinks in sourceComponent.Outputs.Values)
- {
- savedSinks.Remove(sinkUid);
- }
- continue;
+ Log.Warning($"Device source {ToPrettyString(source)} contains invalid links to entity {ToPrettyString(sink)}: {link.Item1}->{link.Item2}");
+ links.Remove(link);
}
- if (sinkComponent.Ports == null)
- continue;
-
- List<(string, string)> invalidLinks = new();
- foreach (var link in linkedPorts)
+ if (links.Count == 0)
{
- if (!sinkComponent.Ports.Contains(link.sink))
- invalidLinks.Add(link);
+ invalidSinks.Add(sink);
+ continue;
}
- foreach (var invalidLink in invalidLinks)
- {
- linkedPorts.Remove(invalidLink);
- sourceComponent.Outputs.GetValueOrDefault(invalidLink.Item1)?.Remove(sinkUid);
- }
+ 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
/// <summary>
/// Ensures that its links get deleted when a source gets removed
/// </summary>
- private void OnSourceRemoved(EntityUid uid, DeviceLinkSourceComponent component, ComponentRemove args)
+ private void OnSourceRemoved(Entity<DeviceLinkSourceComponent> source, ref ComponentRemove args)
{
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))
- RemoveSinkFromSourceInternal(uid, sinkUid, component, sink);
+ RemoveSinkFromSourceInternal(source, sinkUid, source, sink);
+ else
+ Log.Error($"Device source {ToPrettyString(source)} links to invalid entity: {ToPrettyString(sinkUid)}");
}
}
/// <summary>
/// Ensures that its links get deleted when a sink gets removed
/// </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 linkedSource in sinkComponent.LinkedSources)
+ foreach (var sourceUid in sink.Comp.LinkedSources)
{
- if (query.TryGetComponent(sinkUid, out var source))
- RemoveSinkFromSourceInternal(linkedSource, sinkUid, source, sinkComponent);
+ if (TryComp(sourceUid, out DeviceLinkSourceComponent? source))
+ RemoveSinkFromSourceInternal(sourceUid, sink, source, sink);
+ else
+ Log.Error($"Device sink {ToPrettyString(sink)} source list contains invalid entity: {ToPrettyString(sourceUid)}");
}
}
/// <summary>
/// Convenience function to add several ports to an entity
/// </summary>
- public void EnsureSourcePorts(EntityUid uid, params string[] ports)
+ public void EnsureSourcePorts(EntityUid uid, params ProtoId<SourcePortPrototype>[] ports)
{
if (ports.Length == 0)
return;
var comp = EnsureComp<DeviceLinkSourceComponent>(uid);
- comp.Ports ??= new HashSet<ProtoId<SourcePortPrototype>>();
-
foreach (var port in ports)
{
- DebugTools.Assert(_prototypeManager.HasIndex<SourcePortPrototype>(port));
- comp.Ports?.Add(port);
+ if (!_prototypeManager.HasIndex(port))
+ Log.Error($"Attempted to add invalid port {port} to {ToPrettyString(uid)}");
+ else
+ comp.Ports.Add(port);
}
}
/// <summary>
/// Convenience function to add several ports to an entity.
/// </summary>
- public void EnsureSinkPorts(EntityUid uid, params string[] ports)
+ public void EnsureSinkPorts(EntityUid uid, params ProtoId<SinkPortPrototype>[] ports)
{
if (ports.Length == 0)
return;
var comp = EnsureComp<DeviceLinkSinkComponent>(uid);
- comp.Ports ??= new HashSet<string>();
-
foreach (var port in ports)
{
- DebugTools.Assert(_prototypeManager.HasIndex<SinkPortPrototype>(port));
- comp.Ports?.Add(port);
+ if (!_prototypeManager.HasIndex(port))
+ Log.Error($"Attempted to add invalid port {port} to {ToPrettyString(uid)}");
+ else
+ comp.Ports.Add(port);
}
}
/// <returns>A list of source port prototypes</returns>
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>();
var sourcePorts = new List<SourcePortPrototype>();
foreach (var port in sourceComponent.Ports)
{
- sourcePorts.Add(_prototypeManager.Index<SourcePortPrototype>(port));
+ sourcePorts.Add(_prototypeManager.Index(port));
}
return sourcePorts;
/// <returns>A list of sink port prototypes</returns>
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>();
var sinkPorts = new List<SinkPortPrototype>();
foreach (var port in sinkComponent.Ports)
{
- sinkPorts.Add(_prototypeManager.Index<SinkPortPrototype>(port));
+ sinkPorts.Add(_prototypeManager.Index(port));
}
return sinkPorts;
if (!Resolve(sourceUid, ref sourceComponent) || !Resolve(sinkUid, ref sinkComponent))
return;
- if (sourceComponent.Ports == null || sinkComponent.Ports == null)
- return;
-
if (!InRange(sourceUid, sinkUid, sourceComponent.Range))
{
if (userId != null)
else
{
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);
}
}
sinkComponent.LinkedSources.Remove(sourceUid);
sourceComponent.LinkedPorts.Remove(sinkUid);
- var outputLists = sourceComponent.Outputs.Values;
- foreach (var outputList in outputLists)
+ foreach (var outputList in sourceComponent.Outputs.Values)
{
outputList.Remove(sinkUid);
}
-
}
/// <summary>
if (!Resolve(sourceUid, ref sourceComponent) || !Resolve(sinkUid, ref sinkComponent))
return false;
- if (sourceComponent.Ports == null || sinkComponent.Ports == null)
- return false;
-
var outputs = sourceComponent.Outputs.GetOrNew(source);
var linkedPorts = sourceComponent.LinkedPorts.GetOrNew(sinkUid);