Files
OldThink/Content.Server/DeviceNetwork/Systems/SingletonDeviceNetServerSystem.cs

139 lines
5.1 KiB
C#
Raw Normal View History

using System.Diagnostics.CodeAnalysis;
using Content.Server.DeviceNetwork.Components;
using Content.Server.Medical.CrewMonitoring;
using Content.Server.Power.Components;
using Content.Server.Station.Systems;
namespace Content.Server.DeviceNetwork.Systems;
/// <summary>
/// Keeps one active server entity per station. Activates another available one if the currently active server becomes unavailable
/// Server in this context means an entity that manages the devicenet packets like the <see cref="Content.Server.Medical.CrewMonitoring.CrewMonitoringServerSystem"/>
/// </summary>
public sealed class SingletonDeviceNetServerSystem : EntitySystem
{
[Dependency] private readonly DeviceNetworkSystem _deviceNetworkSystem = default!;
[Dependency] private readonly StationSystem _stationSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SingletonDeviceNetServerComponent, PowerChangedEvent>(OnPowerChanged);
}
/// <summary>
/// Returns whether the given entity is an active server or not
/// </summary>
public bool IsActiveServer(EntityUid serverId, SingletonDeviceNetServerComponent? serverComponent = default)
{
return Resolve(serverId, ref serverComponent) && serverComponent.Active;
}
/// <summary>
/// Returns the address of the currently active server for the given station id if there is one.<br/>
/// What kind of server you're trying to get the active instance of is determined by the component type parameter TComp.<br/>
/// <br/>
/// Setting TComp to <see cref="CrewMonitoringServerComponent"/>, for example, gives you the address of an entity containing the crew monitoring server component.<br/>
/// </summary>
/// <param name="stationId">The entityUid of the station</param>
/// <param name="address">The address of the active server if it exists</param>
/// <typeparam name="TComp">The component type that determines what type of server you're getting the address of</typeparam>
/// <returns>True if there is an active serve. False otherwise</returns>
public bool TryGetActiveServerAddress<TComp>(EntityUid stationId, [NotNullWhen(true)] out string? address) where TComp : IComponent
{
var servers = EntityQueryEnumerator<
SingletonDeviceNetServerComponent,
DeviceNetworkComponent,
TComp
>();
(EntityUid id, SingletonDeviceNetServerComponent server, DeviceNetworkComponent device)? last = default;
while (servers.MoveNext(out var uid, out var server, out var device, out _))
{
if (!_stationSystem.GetOwningStation(uid)?.Equals(stationId) ?? true)
continue;
if (!server.Available)
{
Goida revert (#600) * Revert "- fix: YAML linter fixes. (#598)" This reverts commit 012bf3c3579103365c3df9d85022d43da4137881. * Revert "Automatic changelog update" This reverts commit cf1c3a9af576a73338da6c552e09f08644a03b79. * Revert "[Fix] Base Layer Prototype (#597)" This reverts commit b0004239995aaa8b18ef031efb0b97e7e1a3be78. * Revert "Modules update (#596)" This reverts commit 00fbdead77e16f443d6d9d2f0d74fdd9ae674b89. * Revert "Automatic changelog update" This reverts commit 0d7a12b2a22f560ce2dcac7e2c3fd1f40b5baef9. * Revert "Fixes (#593)" This reverts commit 943c77031cd4d4d3b24a4e4a299ae7490d4cd3fe. * Revert "minor loadout fixes (#594)" This reverts commit 143c010a894df76d1125dd872bc4fd9df69d1ca3. * Revert "Update DryDock.yml (#595)" This reverts commit 4cd0100ac76195a1df027cfa49a51d45db6024f6. * Revert "Automatic changelog update" This reverts commit 08eadc690f8c015e8990b0a81666f38da287cb7c. * Revert "fix: Maximum message size (#591)" This reverts commit 343f3612ebe5f471bc8a6ace56d1d47430b8a067. * Revert "Черри пики 7 (#592)" This reverts commit 3f97bdce2f162a28ff98b54c614522f02de8127f. * Revert "Automatic changelog update" This reverts commit 0678eca2502d10842c89ac55e28deb0c867849f8. * Revert "Рандомфиксы (#590)" This reverts commit 2b9e5e2437a00855d512106096c1acc634bebdfd. * Revert "Нижнее бельё в лодауты (#580)" This reverts commit e01a47b0899c2f37c0bbad8209b03dfe593c2bc6. * Revert "add lathe sounds (#588)" This reverts commit c80a2985f2d3fe7c934809285cef142eaa38274e. * Revert "Добавил параметр группы для некоторых реагентов (#585)" This reverts commit 713b16bb98f65fa8de685b376fc5a96355c69799. * Revert "add hrp ++++ aspect (#587)" This reverts commit a6a69cc60fcd3a7f13221fd622b0843d7961c595. * Revert "Новые амбиенты и пару песен (#586)" This reverts commit 48c86bd8467fd10bbcf1f1f01b6934cffb888029. * Revert "Сообщения в ПДА 2 (#583)" This reverts commit cced3cc98b46864e058600ce3b588303efc95f73. * Revert "Automatic changelog update" This reverts commit abf435b11dee2dfc7bdc5df033053c2a643b5231. * Revert "Chem stuff and more (#584)" This reverts commit 3608960f5c081ce0db8e058fb6dc3991feae82e3. * Revert "JobRequiremet refactor (#579)" This reverts commit 9a9c9598e069dc81e250864279134ce881dac6ce. * Revert "Revert "Reapply "Нижнее бельё в лодауты""" This reverts commit 44447d573f8ade0b556030ad55ea8b622aee7745. * Revert "Reapply "Нижнее бельё в лодауты"" This reverts commit 0c4d082ad38b9cd033432e76863ad593726adf78. * Revert "Revert "Нижнее бельё в лодауты"" This reverts commit 56473c5492f16d6c72325fef1c28772df02de1d1. * Revert "Нижнее бельё в лодауты" This reverts commit d1cb0cb364caed1aa182cece11739c01a6ed798e. * Revert "DryDock and WhiteMoose update (#578)" This reverts commit 14755808aff7366f1fece4149c5518c4411f5ad9. * Revert "Automatic changelog update" This reverts commit 0133f8272272f24aa0620ea1fb718ddc50c3c424. * Revert "Fixes (#576)" This reverts commit b7cc49896cb38055f0e1b5b3c838a004991a2e61. * Revert "порт системы регенерации солюшена цинки (#574)" This reverts commit a22cf3d50b5d974c4cc4b3ad36fbb04dc4a2920a. * Revert "Воровские перчатки (#573)" This reverts commit bb7140f3d48fa435d7926561d41e9cee07bcdc19. * Revert "mood resprite (#572)" This reverts commit 4db96dc569f7b3ea3a7b88133458b477592b2f8c. * Revert "fix missing letter (#571)" This reverts commit 94ea7567940ff3af1967bf16b47b6c5250f0934e. * Revert "Сообщения в ПДА (#564)" This reverts commit d023d29e54b48dc56dc782ba5c9c57872aef1bab. * Revert "- fix: No visible aghost." This reverts commit 27e7f25f7e0748c8d676678cb34322fb630f26c5. * Revert "- tweak: Nerf cult shield." This reverts commit 6a384246b886556032f825377d869ad963e723c7.
2024-08-09 16:09:22 +00:00
DisconnectServer(uid,server, device);
continue;
}
last = (uid, server, device);
if (!server.Active)
continue;
address = device.Address;
return true;
}
//If there was no active server for the station make the last available inactive one active
if (last.HasValue)
{
ConnectServer(last.Value.id, last.Value.server, last.Value.device);
address = last.Value.device.Address;
return true;
}
address = null;
return address != null;
}
/// <summary>
/// Disconnects the server losing power
/// </summary>
private void OnPowerChanged(EntityUid uid, SingletonDeviceNetServerComponent component, ref PowerChangedEvent args)
{
component.Available = args.Powered;
if (!args.Powered && component.Active)
DisconnectServer(uid, component);
}
private void ConnectServer(EntityUid uid, SingletonDeviceNetServerComponent? server = null, DeviceNetworkComponent? device = null)
{
if (!Resolve(uid, ref server, ref device))
return;
server.Active = true;
var connectedEvent = new DeviceNetServerConnectedEvent();
RaiseLocalEvent(uid, ref connectedEvent);
if (_deviceNetworkSystem.IsDeviceConnected(uid, device))
return;
_deviceNetworkSystem.ConnectDevice(uid, device);
}
/// <summary>
/// Disconnects a server from the device network and clears the currently active server
/// </summary>
private void DisconnectServer(EntityUid uid, SingletonDeviceNetServerComponent? server = null, DeviceNetworkComponent? device = null)
{
if (!Resolve(uid, ref server, ref device))
return;
server.Active = false;
var disconnectedEvent = new DeviceNetServerDisconnectedEvent();
RaiseLocalEvent(uid, ref disconnectedEvent);
_deviceNetworkSystem.DisconnectDevice(uid, device, false);
}
}
/// <summary>
/// Raised when a server gets activated and connected to the device net
/// </summary>
[ByRefEvent]
public record struct DeviceNetServerConnectedEvent;
/// <summary>
/// Raised when a server gets disconnected
/// </summary>
[ByRefEvent]
public record struct DeviceNetServerDisconnectedEvent;