]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Fix NetEntity DataField in AnalysisConsoleComponent (#39984)
authorslarticodefast <161409025+slarticodefast@users.noreply.github.com>
Sat, 11 Oct 2025 00:22:31 +0000 (02:22 +0200)
committerGitHub <noreply@github.com>
Sat, 11 Oct 2025 00:22:31 +0000 (00:22 +0000)
* fix analysis console

* fix test

* totally not a web edit

Content.Shared/DeviceLinking/SharedDeviceLinkSystem.cs
Content.Shared/Xenoarchaeology/Equipment/Components/AnalysisConsoleComponent.cs
Content.Shared/Xenoarchaeology/Equipment/Components/ArtifactAnalyzerComponent.cs
Content.Shared/Xenoarchaeology/Equipment/SharedArtifactAnalyzerSystem.cs

index 5d51bbc3e83b4bdbad02dee64fbf075466f1a031..74bbc259c6ada11d31ac10cd677413f3008cfd7d 100644 (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>
index 3247e3365a3dc4aba3c2bdff6298a41179af960c..3f596287142356be9be6465aa96c8c92f5cbd670 100644 (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";
index 05d17bb12391a858ef9220c2aa4d633cf1629a6c..9581285efb378738d92f9084fb9b012ea1a5a2dd 100644 (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";
 }
index e47e964225d104b8401960525f1e501e46b08dd5..086b402bb28f6340143568bb17a450cbbbd21e56 100644 (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<AnalysisConsoleComponent, NewLinkEvent>(OnNewLink);
-        SubscribeLocalEvent<AnalysisConsoleComponent, PortDisconnectedEvent>(OnPortDisconnected);
+        SubscribeLocalEvent<ArtifactAnalyzerComponent, NewLinkEvent>(OnNewLinkAnalyzer);
+        SubscribeLocalEvent<ArtifactAnalyzerComponent, LinkAttemptEvent>(OnLinkAttemptAnalyzer);
+        SubscribeLocalEvent<ArtifactAnalyzerComponent, PortDisconnectedEvent>(OnPortDisconnectedAnalyzer);
+
+        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;
     }