CatStation/Content.Server/Robotics/Systems/RoboticsConsoleSystem.cs
Rouden cc2c14f8e4
Device Network refactor part 1: API improvements (#44346)
* Refactor DeviceNetwork

* Singleton

* NetworkPayloads refactor

* Reorganize DeviceNetworkSystem

* Surveillance cameras

* Fix code

* Fix build

* Fix test fail by rewriting entire singleton logic

* Properly predict examine

* Update DeviceNetworkSystem.API.cs

* Update EntityTest.cs

* Update DeviceNetworkSystem.cs

* Alright I'll do this expensive check fine

* Fix disposals debug assert

* Implement device network static events support

* Use frozen dictionaries for performance

* Update SharedDeviceNetworkSystem.Payloads.cs

* Parallel payload handling support

* Fix some spagetti-code

* Optimize NetworkPayload cancellation

* Parallel payloads are now cancellable

* Fix screens

* Fix robotics console

* Allow new methods for shared

* Mail fix

* Turret fix

* Fix network system method usage + signal and borg fix

* finally fix atmos devices OH MY GOOOD

* Remove stupid parallelism from fax

* Fix surveiollance cameras

* Working on a single thing for 2 days straight award

* Fix the test?

* Update units.yml

* Add IDevice interface because why not

* Add more device network suffixes

* Fix camera router failing test

* yaml linter peak

* Improve docs for DeviceNetworkHandlers

* fix build

* Update CartridgeLoaderSystem.Relay.cs

* Remove parallel system and EntitySystem post init

* Add more docs and also events

* Remove unnecessary caching

* Update DevicePayloadComponentSystem.cs

* Back to entity events

* Implement DeviceData

* Remove serializable from payloads

* Update DeviceData.cs

* Add a prototype for a singleton

* Fix tests

* Fix air alarms

* Update DeviceNetworkSystem.cs

* Fix cameras

* Make camera router event-less

* Fix atmos sync payloads, add more docs

* Remove singleton + instant packets

* oops remove public access

* Fix tests?

* Why the hell overload effect makes puddle sparks

* Apparently it was redundant

* Update gates.yml

* Finish documentation

* Review

* Mark relay properties as obsolete

* Struct payloads support (#blazinglyfastmemorysafe)

* Update DeviceNetworkTest.cs

* Make device structs stuff better

* Move address prefix to DeviceNetworkComponent

* Locale fixes

* Fix tests

* Doesn't know boolean logic award

* Update AtmosAlarmableSystem.cs

* Fix address predicted examine

* Partial fix to air alarmables

* Fix deployable turrets UI

* Fix firelocks, maybe

* Apparently Activatable UI was enough lol

* Fix surveillance cameras

* Rename `QueuePacket` into `SendPacket`

* Simplify DeviceData as much as possible

* Update DeviceData.cs

* Update FaxMachineComponent.cs

* Remove DeviceData because its stupid

* Update ScreenSystem.cs

* Fix build (camera map broken still)

* Fix map

* fix usings

* Review changes

* Build fix
2026-08-31 19:42:56 +00:00

134 lines
4.6 KiB
C#

using Content.Server.Administration.Logs;
using Content.Server.DeviceNetwork.Systems;
using Content.Server.Radio.EntitySystems;
using Content.Shared.Lock;
using Content.Shared.Database;
using Content.Shared.Robotics;
using Content.Shared.Robotics.Components;
using Content.Shared.Robotics.Systems;
using Robust.Server.GameObjects;
using Robust.Shared.Timing;
using Content.Shared.DeviceNetwork.Events;
namespace Content.Server.Research.Systems;
/// <summary>
/// Handles UI and state receiving for the robotics control console.
/// <c>BorgTransponderComponent<c/> broadcasts state from the station's borgs to consoles.
/// </summary>
public sealed partial class RoboticsConsoleSystem : SharedRoboticsConsoleSystem
{
[Dependency] private DeviceNetworkSystem _deviceNetwork = default!;
[Dependency] private IAdminLogManager _adminLogger = default!;
[Dependency] private IGameTiming _timing = default!;
[Dependency] private LockSystem _lock = default!;
[Dependency] private RadioSystem _radio = default!;
[Dependency] private UserInterfaceSystem _ui = default!;
// almost never timing out more than 1 per tick so initialize with that capacity
private List<string> _removing = new(1);
public override void Initialize()
{
base.Initialize();
Subs.BuiEvents<RoboticsConsoleComponent>(RoboticsConsoleUiKey.Key, subs =>
{
subs.Event<BoundUIOpenedEvent>(OnOpened);
subs.Event<RoboticsConsoleDisableMessage>(OnDisable);
subs.Event<RoboticsConsoleDestroyMessage>(OnDestroy);
// TODO: camera stuff
});
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var now = _timing.CurTime;
var query = EntityQueryEnumerator<RoboticsConsoleComponent>();
while (query.MoveNext(out var uid, out var comp))
{
// remove cyborgs that havent pinged in a while
_removing.Clear();
foreach (var (address, data) in comp.Cyborgs)
{
if (now >= data.Timeout)
_removing.Add(address);
}
// needed to prevent modifying while iterating it
foreach (var address in _removing)
{
comp.Cyborgs.Remove(address);
}
if (_removing.Count > 0)
UpdateUserInterface((uid, comp));
}
}
[SubscribeLocalEvent]
private void OnPacketReceived(Entity<RoboticsConsoleComponent> ent, ref DeviceNetworkPacketEvent<RoboticsCyborgDataPayload> args)
{
var data = args.Data.Data;
data.Timeout = _timing.CurTime + ent.Comp.Timeout;
ent.Comp.Cyborgs[args.SenderAddress] = data;
UpdateUserInterface(ent);
}
private void OnOpened(Entity<RoboticsConsoleComponent> ent, ref BoundUIOpenedEvent args)
{
UpdateUserInterface(ent);
}
private void OnDisable(Entity<RoboticsConsoleComponent> ent, ref RoboticsConsoleDisableMessage args)
{
if (!ent.Comp.AllowBorgControl)
return;
if (_lock.IsLocked(ent.Owner))
return;
if (!ent.Comp.Cyborgs.TryGetValue(args.Address, out var data))
return;
var payload = new RoboticsCyborgDisablePayload();
_deviceNetwork.SendPacket(ent.Owner, args.Address, ref payload);
_adminLogger.Add(LogType.Action, LogImpact.High, $"{ToPrettyString(args.Actor):user} disabled borg {data.Name} with address {args.Address}");
}
private void OnDestroy(Entity<RoboticsConsoleComponent> ent, ref RoboticsConsoleDestroyMessage args)
{
if (!ent.Comp.AllowBorgControl)
return;
if (_lock.IsLocked(ent.Owner))
return;
var now = _timing.CurTime;
if (now < ent.Comp.NextDestroy)
return;
if (!ent.Comp.Cyborgs.Remove(args.Address, out var data))
return;
var payload = new RoboticsCyborgDestroyPayload();
_deviceNetwork.SendPacket(ent.Owner, args.Address, ref payload);
var message = Loc.GetString(ent.Comp.DestroyMessage, ("name", data.Name));
_radio.SendRadioMessage(ent, message, ent.Comp.RadioChannel, ent);
_adminLogger.Add(LogType.Action, LogImpact.Extreme, $"{ToPrettyString(args.Actor):user} destroyed borg {data.Name} with address {args.Address}");
ent.Comp.NextDestroy = now + ent.Comp.DestroyCooldown;
Dirty(ent, ent.Comp);
}
private void UpdateUserInterface(Entity<RoboticsConsoleComponent> ent)
{
var state = new RoboticsConsoleState(ent.Comp.Cyborgs, ent.Comp.AllowBorgControl);
_ui.SetUiState(ent.Owner, RoboticsConsoleUiKey.Key, state);
}
}