145 lines
6.1 KiB
C#
145 lines
6.1 KiB
C#
using Content.Server.Administration.Logs;
|
|
using Content.Server.Chat.Systems;
|
|
using Content.Server.Radio.Components;
|
|
using Content.Server.VoiceMask;
|
|
using Content.Server.Popups;
|
|
using Content.Shared.Chat;
|
|
using Content.Shared.Database;
|
|
using Content.Shared.Radio;
|
|
using Robust.Server.GameObjects;
|
|
using Robust.Shared.Network;
|
|
using Robust.Shared.Replays;
|
|
using Robust.Shared.Utility;
|
|
using Content.Shared.Popups;
|
|
using Robust.Shared.Map;
|
|
using Content.Shared.Radio.Components;
|
|
using Content.Server.Power.Components;
|
|
|
|
namespace Content.Server.Radio.EntitySystems;
|
|
|
|
/// <summary>
|
|
/// This system handles intrinsic radios and the general process of converting radio messages into chat messages.
|
|
/// </summary>
|
|
public sealed class RadioSystem : EntitySystem
|
|
{
|
|
[Dependency] private readonly INetManager _netMan = default!;
|
|
[Dependency] private readonly IReplayRecordingManager _replay = default!;
|
|
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
|
|
[Dependency] private readonly PopupSystem _popup = default!;
|
|
|
|
// set used to prevent radio feedback loops.
|
|
private readonly HashSet<string> _messages = new();
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
SubscribeLocalEvent<IntrinsicRadioReceiverComponent, RadioReceiveEvent>(OnIntrinsicReceive);
|
|
SubscribeLocalEvent<IntrinsicRadioTransmitterComponent, EntitySpokeEvent>(OnIntrinsicSpeak);
|
|
}
|
|
|
|
private void OnIntrinsicSpeak(EntityUid uid, IntrinsicRadioTransmitterComponent component, EntitySpokeEvent args)
|
|
{
|
|
if (args.Channel != null && component.Channels.Contains(args.Channel.ID))
|
|
{
|
|
SendRadioMessage(uid, args.Message, args.Channel, uid);
|
|
args.Channel = null; // prevent duplicate messages from other listeners.
|
|
}
|
|
}
|
|
|
|
private void OnIntrinsicReceive(EntityUid uid, IntrinsicRadioReceiverComponent component, ref RadioReceiveEvent args)
|
|
{
|
|
if (TryComp(uid, out ActorComponent? actor))
|
|
_netMan.ServerSendMessage(args.ChatMsg, actor.PlayerSession.ConnectedClient);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Send radio message to all active radio listeners
|
|
/// </summary>
|
|
/// <param name="messageSource">Entity that spoke the message</param>
|
|
/// <param name="radioSource">Entity that picked up the message and will send it, e.g. headset</param>
|
|
public void SendRadioMessage(EntityUid messageSource, string message, RadioChannelPrototype channel, EntityUid radioSource)
|
|
{
|
|
// TODO if radios ever garble / modify messages, feedback-prevention needs to be handled better than this.
|
|
if (!_messages.Add(message))
|
|
return;
|
|
|
|
var name = TryComp(messageSource, out VoiceMaskComponent? mask) && mask.Enabled
|
|
? mask.VoiceName
|
|
: MetaData(messageSource).EntityName;
|
|
|
|
name = FormattedMessage.EscapeText(name);
|
|
|
|
// most radios are relayed to chat, so lets parse the chat message beforehand
|
|
var chat = new ChatMessage(
|
|
ChatChannel.Radio,
|
|
message,
|
|
Loc.GetString("chat-radio-message-wrap", ("color", channel.Color), ("channel", $"\\[{channel.LocalizedName}\\]"), ("name", name), ("message", FormattedMessage.EscapeText(message))),
|
|
EntityUid.Invalid);
|
|
var chatMsg = new MsgChatMessage { Message = chat };
|
|
var ev = new RadioReceiveEvent(message, messageSource, channel, chatMsg);
|
|
|
|
var sendAttemptEv = new RadioSendAttemptEvent(channel, radioSource);
|
|
RaiseLocalEvent(ref sendAttemptEv);
|
|
RaiseLocalEvent(radioSource, ref sendAttemptEv);
|
|
var canSend = !sendAttemptEv.Cancelled;
|
|
|
|
var sourceMapId = Transform(radioSource).MapID;
|
|
var hasActiveServer = HasActiveServer(sourceMapId, channel.ID);
|
|
var hasMicro = HasComp<RadioMicrophoneComponent>(radioSource);
|
|
|
|
var speakerQuery = GetEntityQuery<RadioSpeakerComponent>();
|
|
var radioQuery = EntityQueryEnumerator<ActiveRadioComponent, TransformComponent>();
|
|
var sentAtLeastOnce = false;
|
|
while (canSend && radioQuery.MoveNext(out var receiver, out var radio, out var transform))
|
|
{
|
|
if (!radio.Channels.Contains(channel.ID))
|
|
continue;
|
|
|
|
if (!channel.LongRange && transform.MapID != sourceMapId && !radio.GlobalReceive)
|
|
continue;
|
|
|
|
// don't need telecom server for long range channels or handheld radios and intercoms
|
|
var needServer = !channel.LongRange && (!hasMicro || !speakerQuery.HasComponent(receiver));
|
|
if (needServer && !hasActiveServer)
|
|
continue;
|
|
|
|
// check if message can be sent to specific receiver
|
|
var attemptEv = new RadioReceiveAttemptEvent(channel, radioSource, receiver);
|
|
RaiseLocalEvent(ref attemptEv);
|
|
RaiseLocalEvent(receiver, ref attemptEv);
|
|
if (attemptEv.Cancelled)
|
|
continue;
|
|
|
|
// send the message
|
|
RaiseLocalEvent(receiver, ref ev);
|
|
sentAtLeastOnce = true;
|
|
}
|
|
if (!sentAtLeastOnce)
|
|
_popup.PopupEntity(Loc.GetString("failed-to-send-message"), messageSource, messageSource, PopupType.MediumCaution);
|
|
|
|
if (name != Name(messageSource))
|
|
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Radio message from {ToPrettyString(messageSource):user} as {name} on {channel.LocalizedName}: {message}");
|
|
else
|
|
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Radio message from {ToPrettyString(messageSource):user} on {channel.LocalizedName}: {message}");
|
|
|
|
_replay.QueueReplayMessage(chat);
|
|
_messages.Remove(message);
|
|
}
|
|
|
|
/// <inheritdoc cref="TelecomServerComponent"/>
|
|
private bool HasActiveServer(MapId mapId, string channelId)
|
|
{
|
|
var servers = EntityQuery<TelecomServerComponent, EncryptionKeyHolderComponent, ApcPowerReceiverComponent, TransformComponent>();
|
|
foreach (var (_, keys, power, transform) in servers)
|
|
{
|
|
if (transform.MapID == mapId &&
|
|
power.Powered &&
|
|
keys.Channels.Contains(channelId))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|