Fix NetEntity DataField in AnalysisConsoleComponent (#39984)

* fix analysis console

* fix test

* totally not a web edit
This commit is contained in:
slarticodefast
2025-10-11 02:22:31 +02:00
committed by GitHub
parent bed5e8fd7a
commit bed556051b
4 changed files with 75 additions and 31 deletions

View File

@@ -214,6 +214,17 @@ public abstract class SharedDeviceLinkSystem : EntitySystem
return links;
}
/// <summary>
/// Gets the entities linked to a specific source port.
/// </summary>
public HashSet<EntityUid> GetLinkedSinks(Entity<DeviceLinkSourceComponent?> source, ProtoId<SourcePortPrototype> port)
{
if (!Resolve(source, ref source.Comp) || !source.Comp.Outputs.TryGetValue(port, out var linked))
return new HashSet<EntityUid>(); // not a source or not linked
return new HashSet<EntityUid>(linked); // clone to prevent modifying the original
}
/// <summary>
/// Returns the default links for the given list of source port prototypes
/// </summary>

View File

@@ -17,7 +17,7 @@ public sealed partial class AnalysisConsoleComponent : Component
/// Can be null if not linked.
/// </summary>
[DataField, AutoNetworkedField]
public NetEntity? AnalyzerEntity;
public EntityUid? AnalyzerEntity;
[DataField]
public SoundSpecifier? ScanFinishedSound = new SoundPathSpecifier("/Audio/Machines/scan_finish.ogg");
@@ -35,7 +35,7 @@ public sealed partial class AnalysisConsoleComponent : Component
};
/// <summary>
/// The machine linking port for the analyzer
/// The machine linking port for linking the console with the analyzer.
/// </summary>
[DataField]
public ProtoId<SourcePortPrototype> LinkingPort = "ArtifactAnalyzerSender";

View File

@@ -1,5 +1,6 @@
using Robust.Shared.Audio;
using Content.Shared.DeviceLinking;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
namespace Content.Shared.Xenoarchaeology.Equipment.Components;
@@ -35,4 +36,10 @@ public sealed partial class ArtifactAnalyzerComponent : Component
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public bool ReadyToPrint = false;
/// <summary>
/// The machine linking port for linking the analyzer with the console.
/// </summary>
[DataField]
public ProtoId<SinkPortPrototype> LinkingPort = "ArtifactAnalyzerReceiver";
}

View File

@@ -15,6 +15,7 @@ namespace Content.Shared.Xenoarchaeology.Equipment;
public abstract class SharedArtifactAnalyzerSystem : EntitySystem
{
[Dependency] private readonly SharedPowerReceiverSystem _powerReceiver = default!;
[Dependency] private readonly SharedDeviceLinkSystem _deviceLink = default!;
/// <inheritdoc/>
public override void Initialize()
@@ -23,10 +24,14 @@ public abstract class SharedArtifactAnalyzerSystem : EntitySystem
SubscribeLocalEvent<ArtifactAnalyzerComponent, ItemPlacedEvent>(OnItemPlaced);
SubscribeLocalEvent<ArtifactAnalyzerComponent, ItemRemovedEvent>(OnItemRemoved);
SubscribeLocalEvent<ArtifactAnalyzerComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<ArtifactAnalyzerComponent, NewLinkEvent>(OnNewLinkAnalyzer);
SubscribeLocalEvent<ArtifactAnalyzerComponent, LinkAttemptEvent>(OnLinkAttemptAnalyzer);
SubscribeLocalEvent<ArtifactAnalyzerComponent, PortDisconnectedEvent>(OnPortDisconnectedAnalyzer);
SubscribeLocalEvent<AnalysisConsoleComponent, NewLinkEvent>(OnNewLink);
SubscribeLocalEvent<AnalysisConsoleComponent, PortDisconnectedEvent>(OnPortDisconnected);
SubscribeLocalEvent<AnalysisConsoleComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<AnalysisConsoleComponent, NewLinkEvent>(OnNewLinkConsole);
SubscribeLocalEvent<AnalysisConsoleComponent, LinkAttemptEvent>(OnLinkAttemptConsole);
SubscribeLocalEvent<AnalysisConsoleComponent, PortDisconnectedEvent>(OnPortDisconnectedConsole);
}
private void OnItemPlaced(Entity<ArtifactAnalyzerComponent> ent, ref ItemPlacedEvent args)
@@ -44,52 +49,74 @@ public abstract class SharedArtifactAnalyzerSystem : EntitySystem
Dirty(ent);
}
private void OnMapInit(Entity<ArtifactAnalyzerComponent> ent, ref MapInitEvent args)
private void OnMapInit(Entity<AnalysisConsoleComponent> ent, ref MapInitEvent args)
{
if (!TryComp<DeviceLinkSinkComponent>(ent, out var sink))
if (!TryComp<DeviceLinkSourceComponent>(ent, out var source))
return;
foreach (var source in sink.LinkedSources)
var linkedEntities = _deviceLink.GetLinkedSinks((ent.Owner, source), ent.Comp.LinkingPort);
foreach (var sink in linkedEntities)
{
if (!TryComp<AnalysisConsoleComponent>(source, out var analysis))
if (!TryComp<ArtifactAnalyzerComponent>(sink, out var analyzer))
continue;
analysis.AnalyzerEntity = GetNetEntity(ent);
ent.Comp.Console = source;
Dirty(source, analysis);
ent.Comp.AnalyzerEntity = sink;
analyzer.Console = ent.Owner;
Dirty(ent);
Dirty(sink, analyzer);
break;
}
}
private void OnNewLink(Entity<AnalysisConsoleComponent> ent, ref NewLinkEvent args)
private void OnNewLinkConsole(Entity<AnalysisConsoleComponent> ent, ref NewLinkEvent args)
{
if (!TryComp<ArtifactAnalyzerComponent>(args.Sink, out var analyzer))
if (args.SourcePort != ent.Comp.LinkingPort || !HasComp<ArtifactAnalyzerComponent>(args.Sink))
return;
ent.Comp.AnalyzerEntity = GetNetEntity(args.Sink);
analyzer.Console = ent;
Dirty(args.Sink, analyzer);
ent.Comp.AnalyzerEntity = args.Sink;
Dirty(ent);
}
private void OnPortDisconnected(Entity<AnalysisConsoleComponent> ent, ref PortDisconnectedEvent args)
private void OnNewLinkAnalyzer(Entity<ArtifactAnalyzerComponent> ent, ref NewLinkEvent args)
{
var analyzerNetEntity = ent.Comp.AnalyzerEntity;
if (args.Port != ent.Comp.LinkingPort || analyzerNetEntity == null)
if (args.SinkPort != ent.Comp.LinkingPort || !HasComp<AnalysisConsoleComponent>(args.Source))
return;
var analyzerEntityUid = GetEntity(analyzerNetEntity);
if (TryComp<ArtifactAnalyzerComponent>(analyzerEntityUid, out var analyzer))
{
analyzer.Console = null;
Dirty(analyzerEntityUid.Value, analyzer);
}
ent.Comp.Console = args.Source;
Dirty(ent);
}
private void OnLinkAttemptConsole(Entity<AnalysisConsoleComponent> ent, ref LinkAttemptEvent args)
{
if (ent.Comp.AnalyzerEntity != null)
args.Cancel(); // can only link to one device at a time
}
private void OnLinkAttemptAnalyzer(Entity<ArtifactAnalyzerComponent> ent, ref LinkAttemptEvent args)
{
if (ent.Comp.Console != null)
args.Cancel(); // can only link to one device at a time
}
private void OnPortDisconnectedConsole(Entity<AnalysisConsoleComponent> ent, ref PortDisconnectedEvent args)
{
if (args.Port != ent.Comp.LinkingPort || ent.Comp.AnalyzerEntity == null)
return;
ent.Comp.AnalyzerEntity = null;
Dirty(ent);
}
private void OnPortDisconnectedAnalyzer(Entity<ArtifactAnalyzerComponent> ent, ref PortDisconnectedEvent args)
{
if (args.Port != ent.Comp.LinkingPort || ent.Comp.Console == null)
return;
ent.Comp.Console = null;
Dirty(ent);
}
public bool TryGetAnalyzer(Entity<AnalysisConsoleComponent> ent, [NotNullWhen(true)] out Entity<ArtifactAnalyzerComponent>? analyzer)
{
analyzer = null;
@@ -98,14 +125,13 @@ public abstract class SharedArtifactAnalyzerSystem : EntitySystem
if (!_powerReceiver.IsPowered(consoleEnt))
return false;
var analyzerUid = GetEntity(ent.Comp.AnalyzerEntity);
if (!TryComp<ArtifactAnalyzerComponent>(analyzerUid, out var analyzerComp))
if (!TryComp<ArtifactAnalyzerComponent>(ent.Comp.AnalyzerEntity, out var analyzerComp))
return false;
if (!_powerReceiver.IsPowered(analyzerUid.Value))
if (!_powerReceiver.IsPowered(ent.Comp.AnalyzerEntity.Value))
return false;
analyzer = (analyzerUid.Value, analyzerComp);
analyzer = (ent.Comp.AnalyzerEntity.Value, analyzerComp);
return true;
}