using System.Diagnostics.CodeAnalysis;
using Content.Shared.DeviceLinking;
using Content.Shared.DeviceLinking.Events;
using Content.Shared.Placeable;
using Content.Shared.Power.EntitySystems;
using Content.Shared.Xenoarchaeology.Artifact.Components;
using Content.Shared.Xenoarchaeology.Equipment.Components;
namespace Content.Shared.Xenoarchaeology.Equipment;
///
/// This system is used for managing the artifact analyzer as well as the analysis console.
/// It also handles scanning and ui updates for both systems.
///
public abstract class SharedArtifactAnalyzerSystem : EntitySystem
{
[Dependency] private readonly SharedPowerReceiverSystem _powerReceiver = default!;
///
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent(OnItemPlaced);
SubscribeLocalEvent(OnItemRemoved);
SubscribeLocalEvent(OnMapInit);
SubscribeLocalEvent(OnNewLink);
SubscribeLocalEvent(OnPortDisconnected);
}
private void OnItemPlaced(Entity ent, ref ItemPlacedEvent args)
{
ent.Comp.CurrentArtifact = args.OtherEntity;
Dirty(ent);
}
private void OnItemRemoved(Entity ent, ref ItemRemovedEvent args)
{
if (args.OtherEntity != ent.Comp.CurrentArtifact)
return;
ent.Comp.CurrentArtifact = null;
Dirty(ent);
}
private void OnMapInit(Entity ent, ref MapInitEvent args)
{
if (!TryComp(ent, out var sink))
return;
foreach (var source in sink.LinkedSources)
{
if (!TryComp(source, out var analysis))
continue;
analysis.AnalyzerEntity = GetNetEntity(ent);
ent.Comp.Console = source;
Dirty(source, analysis);
Dirty(ent);
break;
}
}
private void OnNewLink(Entity ent, ref NewLinkEvent args)
{
if (!TryComp(args.Sink, out var analyzer))
return;
ent.Comp.AnalyzerEntity = GetNetEntity(args.Sink);
analyzer.Console = ent;
Dirty(args.Sink, analyzer);
Dirty(ent);
}
private void OnPortDisconnected(Entity ent, ref PortDisconnectedEvent args)
{
var analyzerNetEntity = ent.Comp.AnalyzerEntity;
if (args.Port != ent.Comp.LinkingPort || analyzerNetEntity == null)
return;
var analyzerEntityUid = GetEntity(analyzerNetEntity);
if (TryComp(analyzerEntityUid, out var analyzer))
{
analyzer.Console = null;
Dirty(analyzerEntityUid.Value, analyzer);
}
ent.Comp.AnalyzerEntity = null;
Dirty(ent);
}
public bool TryGetAnalyzer(Entity ent, [NotNullWhen(true)] out Entity? analyzer)
{
analyzer = null;
var consoleEnt = ent.Owner;
if (!_powerReceiver.IsPowered(consoleEnt))
return false;
var analyzerUid = GetEntity(ent.Comp.AnalyzerEntity);
if (!TryComp(analyzerUid, out var analyzerComp))
return false;
if (!_powerReceiver.IsPowered(analyzerUid.Value))
return false;
analyzer = (analyzerUid.Value, analyzerComp);
return true;
}
public bool TryGetArtifactFromConsole(Entity ent, [NotNullWhen(true)] out Entity? artifact)
{
artifact = null;
if (!TryGetAnalyzer(ent, out var analyzer))
return false;
if (!TryComp(analyzer.Value.Comp.CurrentArtifact, out var comp))
return false;
artifact = (analyzer.Value.Comp.CurrentArtifact.Value, comp);
return true;
}
public bool TryGetAnalysisConsole(Entity ent, [NotNullWhen(true)] out Entity? analysisConsole)
{
analysisConsole = null;
if (!TryComp(ent.Comp.Console, out var consoleComp))
return false;
analysisConsole = (ent.Comp.Console.Value, consoleComp);
return true;
}
}