* Revert "- fix: YAML linter fixes. (#598)" This reverts commit012bf3c357. * Revert "Automatic changelog update" This reverts commitcf1c3a9af5. * Revert "[Fix] Base Layer Prototype (#597)" This reverts commitb000423999. * Revert "Modules update (#596)" This reverts commit00fbdead77. * Revert "Automatic changelog update" This reverts commit0d7a12b2a2. * Revert "Fixes (#593)" This reverts commit943c77031c. * Revert "minor loadout fixes (#594)" This reverts commit143c010a89. * Revert "Update DryDock.yml (#595)" This reverts commit4cd0100ac7. * Revert "Automatic changelog update" This reverts commit08eadc690f. * Revert "fix: Maximum message size (#591)" This reverts commit343f3612eb. * Revert "Черри пики 7 (#592)" This reverts commit3f97bdce2f. * Revert "Automatic changelog update" This reverts commit0678eca250. * Revert "Рандомфиксы (#590)" This reverts commit2b9e5e2437. * Revert "Нижнее бельё в лодауты (#580)" This reverts commite01a47b089. * Revert "add lathe sounds (#588)" This reverts commitc80a2985f2. * Revert "Добавил параметр группы для некоторых реагентов (#585)" This reverts commit713b16bb98. * Revert "add hrp ++++ aspect (#587)" This reverts commita6a69cc60f. * Revert "Новые амбиенты и пару песен (#586)" This reverts commit48c86bd846. * Revert "Сообщения в ПДА 2 (#583)" This reverts commitcced3cc98b. * Revert "Automatic changelog update" This reverts commitabf435b11d. * Revert "Chem stuff and more (#584)" This reverts commit3608960f5c. * Revert "JobRequiremet refactor (#579)" This reverts commit9a9c9598e0. * Revert "Revert "Reapply "Нижнее бельё в лодауты""" This reverts commit44447d573f. * Revert "Reapply "Нижнее бельё в лодауты"" This reverts commit0c4d082ad3. * Revert "Revert "Нижнее бельё в лодауты"" This reverts commit56473c5492. * Revert "Нижнее бельё в лодауты" This reverts commitd1cb0cb364. * Revert "DryDock and WhiteMoose update (#578)" This reverts commit14755808af. * Revert "Automatic changelog update" This reverts commit0133f82722. * Revert "Fixes (#576)" This reverts commitb7cc49896c. * Revert "порт системы регенерации солюшена цинки (#574)" This reverts commita22cf3d50b. * Revert "Воровские перчатки (#573)" This reverts commitbb7140f3d4. * Revert "mood resprite (#572)" This reverts commit4db96dc569. * Revert "fix missing letter (#571)" This reverts commit94ea756794. * Revert "Сообщения в ПДА (#564)" This reverts commitd023d29e54. * Revert "- fix: No visible aghost." This reverts commit27e7f25f7e. * Revert "- tweak: Nerf cult shield." This reverts commit6a384246b8.
139 lines
5.1 KiB
C#
139 lines
5.1 KiB
C#
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)
|
|
{
|
|
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;
|