IListener and IRadio purge (#11980)

This commit is contained in:
Leon Friedrich
2022-11-15 17:09:27 +13:00
committed by GitHub
parent bc525425da
commit 0b5a58001c
48 changed files with 946 additions and 643 deletions

View File

@@ -30,6 +30,8 @@ using Robust.Shared.Players;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Utility;
using Content.Server.Speech.EntitySystems;
using Content.Shared.Radio;
namespace Content.Server.Chat.Systems;
@@ -48,15 +50,14 @@ public sealed partial class ChatSystem : SharedChatSystem
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly ActionBlockerSystem _actionBlocker = default!;
[Dependency] private readonly ListeningSystem _listener = default!;
[Dependency] private readonly InventorySystem _inventory = default!;
[Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly StationSystem _stationSystem = default!;
[Dependency] private readonly MobStateSystem _mobStateSystem = default!;
private const int VoiceRange = 10; // how far voice goes in world units
private const int WhisperRange = 2; // how far whisper goes in world units
private const string DefaultAnnouncementSound = "/Audio/Announcements/announce.ogg";
public const int VoiceRange = 10; // how far voice goes in world units
public const int WhisperRange = 2; // how far whisper goes in world units
public const string DefaultAnnouncementSound = "/Audio/Announcements/announce.ogg";
private bool _loocEnabled = true;
private bool _deadLoocEnabled = false;
@@ -106,9 +107,19 @@ public sealed partial class ChatSystem : SharedChatSystem
_configurationManager.SetCVar(CCVars.OocEnabled, true);
}
// ReSharper disable once InconsistentNaming
public void TrySendInGameICMessage(EntityUid source, string message, InGameICChatType desiredType, bool hideChat,
IConsoleShell? shell = null, IPlayerSession? player = null)
/// <summary>
/// Sends an in-character chat message to relevant clients.
/// </summary>
/// <param name="source">The entity that is speaking</param>
/// <param name="message">The message being spoken or emoted</param>
/// <param name="desiredType">The chat type</param>
/// <param name="hideChat">Whether or not this message should appear in the chat window</param>
/// <param name="hideGlobalGhostChat">Whether or not this message should appear in the chat window for out-of-range ghosts (which otherwise ignore range restrictions)</param>
/// <param name="shell"></param>
/// <param name="player">The player doing the speaking</param>
/// <param name="nameOverride">The name to use for the speaking entity. Usually this should just be modified via <see cref="TransformSpeakerNameEvent"/>. If this is set, the event will not get raised.</param>
public void TrySendInGameICMessage(EntityUid source, string message, InGameICChatType desiredType, bool hideChat, bool hideGlobalGhostChat = false,
IConsoleShell? shell = null, IPlayerSession? player = null, string? nameOverride = null)
{
if (HasComp<GhostComponent>(source))
{
@@ -126,6 +137,7 @@ public sealed partial class ChatSystem : SharedChatSystem
if (!CanSendInGame(message, shell, player))
return;
hideGlobalGhostChat |= hideChat;
bool shouldCapitalize = (desiredType != InGameICChatType.Emote);
bool shouldPunctuate = _configurationManager.GetCVar(CCVars.ChatPunctuation);
@@ -134,7 +146,7 @@ public sealed partial class ChatSystem : SharedChatSystem
// Was there an emote in the message? If so, send it.
if (player != null && emoteStr != message && emoteStr != null)
{
SendEntityEmote(source, emoteStr, hideChat);
SendEntityEmote(source, emoteStr, hideChat, hideGlobalGhostChat, nameOverride);
}
// This can happen if the entire string is sanitized out.
@@ -145,13 +157,13 @@ public sealed partial class ChatSystem : SharedChatSystem
switch (desiredType)
{
case InGameICChatType.Speak:
SendEntitySpeak(source, message, hideChat);
SendEntitySpeak(source, message, hideChat, hideGlobalGhostChat, nameOverride);
break;
case InGameICChatType.Whisper:
SendEntityWhisper(source, message, hideChat);
SendEntityWhisper(source, message, hideChat, hideGlobalGhostChat, null, nameOverride);
break;
case InGameICChatType.Emote:
SendEntityEmote(source, message, hideChat);
SendEntityEmote(source, message, hideChat, hideGlobalGhostChat, nameOverride);
break;
}
}
@@ -245,7 +257,7 @@ public sealed partial class ChatSystem : SharedChatSystem
#region Private API
private void SendEntitySpeak(EntityUid source, string originalMessage, bool hideChat = false)
private void SendEntitySpeak(EntityUid source, string originalMessage, bool hideChat, bool hideGlobalGhostChat, string? nameOverride)
{
if (!_actionBlocker.CanSpeak(source))
return;
@@ -254,30 +266,38 @@ public sealed partial class ChatSystem : SharedChatSystem
if (channel != null)
{
_listener.PingListeners(source, message, channel);
SendEntityWhisper(source, message, hideChat);
SendEntityWhisper(source, message, hideChat, hideGlobalGhostChat, channel, nameOverride);
return;
}
var nameEv = new TransformSpeakerNameEvent(source, Name(source));
RaiseLocalEvent(source, nameEv);
var name = FormattedMessage.EscapeText(nameEv.Name);
message = TransformSpeech(source, message);
if (message.Length == 0)
return;
// get the entity's apparent name (if no override provided).
string name;
if (nameOverride != null)
{
name = nameOverride;
}
else
{
var nameEv = new TransformSpeakerNameEvent(source, Name(source));
RaiseLocalEvent(source, nameEv);
name = nameEv.Name;
}
name = FormattedMessage.EscapeText(name);
var wrappedMessage = Loc.GetString("chat-manager-entity-say-wrap-message",
("entityName", name), ("message", FormattedMessage.EscapeText(message)));
SendInVoiceRange(ChatChannel.Local, message, wrappedMessage, source, hideChat);
_listener.PingListeners(source, message, null);
SendInVoiceRange(ChatChannel.Local, message, wrappedMessage, source, hideChat, hideGlobalGhostChat);
var ev = new EntitySpokeEvent(message);
RaiseLocalEvent(source, ev);
var ev = new EntitySpokeEvent(source, message, channel, null);
RaiseLocalEvent(source, ev, true);
// To avoid logging any messages sent by entities that are not players, like vendors, cloning, etc.
if (!TryComp(source, out ActorComponent? mind))
if (!HasComp<ActorComponent>(source))
return;
if (originalMessage == message)
@@ -286,7 +306,7 @@ public sealed partial class ChatSystem : SharedChatSystem
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Say from {ToPrettyString(source):user}, original: {originalMessage}, transformed: {message}.");
}
private void SendEntityWhisper(EntityUid source, string originalMessage, bool hideChat = false)
private void SendEntityWhisper(EntityUid source, string originalMessage, bool hideChat, bool hideGlobalGhostChat, RadioChannelPrototype? channel, string? nameOverride)
{
if (!_actionBlocker.CanSpeak(source))
return;
@@ -297,48 +317,47 @@ public sealed partial class ChatSystem : SharedChatSystem
var obfuscatedMessage = ObfuscateMessageReadability(message, 0.2f);
var transformSource = Transform(source);
var sourceCoords = transformSource.Coordinates;
// get the entity's apparent name (if no override provided).
string name;
if (nameOverride != null)
{
name = nameOverride;
}
else
{
var nameEv = new TransformSpeakerNameEvent(source, Name(source));
RaiseLocalEvent(source, nameEv);
name = nameEv.Name;
}
name = FormattedMessage.EscapeText(name);
var nameEv = new TransformSpeakerNameEvent(source, Name(source));
RaiseLocalEvent(source, nameEv);
var name = FormattedMessage.EscapeText(nameEv.Name);
var xforms = GetEntityQuery<TransformComponent>();
var ghosts = GetEntityQuery<GhostComponent>();
var sessions = new List<ICommonSession>();
ClientDistanceToList(source, VoiceRange, sessions);
// Whisper needs these special calculations, since it can obfuscate the message.
foreach (var session in sessions)
foreach (var (session, data) in GetRecipients(source, VoiceRange))
{
if (session.AttachedEntity is not { Valid: true } playerEntity)
continue;
var transformEntity = xforms.GetComponent(playerEntity);
if (hideGlobalGhostChat && data.Observer && data.Range < 0)
continue; // Won't get logged to chat, and ghosts are too far away to see the pop-up, so we just won't send it to them.
if (sourceCoords.InRange(EntityManager, transformEntity.Coordinates, WhisperRange) ||
ghosts.HasComponent(playerEntity))
if (data.Range <= WhisperRange)
{
var wrappedMessage = Loc.GetString("chat-manager-entity-whisper-wrap-message",
("entityName", name), ("message", FormattedMessage.EscapeText(message)));
_chatManager.ChatMessageToOne(ChatChannel.Whisper, message, wrappedMessage, source, hideChat, session.ConnectedClient);
_chatManager.ChatMessageToOne(ChatChannel.Whisper, message, wrappedMessage, source, data.HideChatOverride ?? hideChat, session.ConnectedClient);
}
else
{
var wrappedMessage = Loc.GetString("chat-manager-entity-whisper-wrap-message",
("entityName", name), ("message", FormattedMessage.EscapeText(obfuscatedMessage)));
_chatManager.ChatMessageToOne(ChatChannel.Whisper, obfuscatedMessage, wrappedMessage, source, hideChat,
_chatManager.ChatMessageToOne(ChatChannel.Whisper, obfuscatedMessage, wrappedMessage, source, data.HideChatOverride ?? hideChat,
session.ConnectedClient);
}
}
var ev = new EntitySpokeEvent(message);
RaiseLocalEvent(source, ev, false);
var ev = new EntitySpokeEvent(source, message, channel, obfuscatedMessage);
RaiseLocalEvent(source, ev, true);
if (originalMessage == message)
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Whisper from {ToPrettyString(source):user}: {originalMessage}.");
@@ -346,18 +365,19 @@ public sealed partial class ChatSystem : SharedChatSystem
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Whisper from {ToPrettyString(source):user}, original: {originalMessage}, transformed: {message}.");
}
private void SendEntityEmote(EntityUid source, string action, bool hideChat)
private void SendEntityEmote(EntityUid source, string action, bool hideChat, bool hideGlobalGhostChat, string? nameOverride)
{
if (!_actionBlocker.CanEmote(source)) return;
var name = FormattedMessage.EscapeText(Identity.Name(source, EntityManager));
// get the entity's apparent name (if no override provided).
string name = FormattedMessage.EscapeText(nameOverride ?? Identity.Name(source, EntityManager));
// Emotes use Identity.Name, since it doesn't actually involve your voice at all.
var wrappedMessage = Loc.GetString("chat-manager-entity-me-wrap-message",
("entityName", name),
("message", FormattedMessage.EscapeText(action)));
("message", FormattedMessage.EscapeText(action)));
SendInVoiceRange(ChatChannel.Emotes, action, wrappedMessage, source, hideChat);
SendInVoiceRange(ChatChannel.Emotes, action, wrappedMessage, source, hideChat, hideGlobalGhostChat);
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Emote from {ToPrettyString(source):user}: {action}");
}
@@ -375,7 +395,7 @@ public sealed partial class ChatSystem : SharedChatSystem
("entityName", name),
("message", FormattedMessage.EscapeText(message)));
SendInVoiceRange(ChatChannel.LOOC, message, wrappedMessage, source, hideChat);
SendInVoiceRange(ChatChannel.LOOC, message, wrappedMessage, source, hideChat, false);
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"LOOC from {player:Player}: {message}");
}
@@ -411,11 +431,13 @@ public sealed partial class ChatSystem : SharedChatSystem
/// <summary>
/// Sends a chat message to the given players in range of the source entity.
/// </summary>
private void SendInVoiceRange(ChatChannel channel, string message, string wrappedMessage, EntityUid source, bool hideChat)
private void SendInVoiceRange(ChatChannel channel, string message, string wrappedMessage, EntityUid source, bool hideChat, bool hideGlobalGhostChat)
{
var sessions = new List<ICommonSession>();
ClientDistanceToList(source, VoiceRange, sessions);
_chatManager.ChatMessageToMany(channel, message, wrappedMessage, source, hideChat, sessions.Select(s => s.ConnectedClient).ToList());
foreach (var (session, data) in GetRecipients(source, VoiceRange))
{
var entHideChat = data.HideChatOverride ?? (hideChat || hideGlobalGhostChat && data.Observer && data.Range < 0);
_chatManager.ChatMessageToOne(channel, message, wrappedMessage, source, entHideChat, session.ConnectedClient);
}
}
/// <summary>
@@ -502,8 +524,14 @@ public sealed partial class ChatSystem : SharedChatSystem
return message;
}
private void ClientDistanceToList(EntityUid source, int voiceRange, List<ICommonSession> playerSessions)
/// <summary>
/// Returns list of players and ranges for all players withing some range. Also returns observers with a range of -1.
/// </summary>
private Dictionary<ICommonSession, ICChatRecipientData> GetRecipients(EntityUid source, float voiceRange)
{
// TODO proper speech occlusion
var recipients = new Dictionary<ICommonSession, ICChatRecipientData>();
var ghosts = GetEntityQuery<GhostComponent>();
var xforms = GetEntityQuery<TransformComponent>();
@@ -518,13 +546,28 @@ public sealed partial class ChatSystem : SharedChatSystem
var transformEntity = xforms.GetComponent(playerEntity);
if (transformEntity.MapID != sourceMapId ||
!ghosts.HasComponent(playerEntity) &&
!sourceCoords.InRange(EntityManager, transformEntity.Coordinates, voiceRange))
if (transformEntity.MapID != sourceMapId)
continue;
playerSessions.Add(player);
var observer = ghosts.HasComponent(playerEntity);
// even if they are an observer, in some situations we still need the range
if (sourceCoords.TryDistance(EntityManager, transformEntity.Coordinates, out var distance) && distance < voiceRange)
{
recipients.Add(player, new ICChatRecipientData(distance, observer));
continue;
}
if (observer)
recipients.Add(player, new ICChatRecipientData(-1, true));
}
RaiseLocalEvent(new ExpandICChatRecipientstEvent(source, voiceRange, recipients));
return recipients;
}
public readonly record struct ICChatRecipientData(float Range, bool Observer, bool? HideChatOverride = null)
{
}
private string ObfuscateMessageReadability(string message, float chance)
@@ -550,6 +593,14 @@ public sealed partial class ChatSystem : SharedChatSystem
#endregion
}
/// <summary>
/// This event is raised before chat messages are sent out to clients. This enables some systems to send the chat
/// messages to otherwise out-of view entities (e.g. for multiple viewports from cameras).
/// </summary>
public record class ExpandICChatRecipientstEvent(EntityUid Source, float VoiceRange, Dictionary<ICommonSession, ChatSystem.ICChatRecipientData> Recipients)
{
}
public sealed class TransformSpeakerNameEvent : EntityEventArgs
{
public EntityUid Sender;
@@ -582,11 +633,22 @@ public sealed class TransformSpeechEvent : EntityEventArgs
/// </summary>
public sealed class EntitySpokeEvent : EntityEventArgs
{
public string Message;
public readonly EntityUid Source;
public readonly string Message;
public readonly string? ObfuscatedMessage; // not null if this was a whisper
public EntitySpokeEvent(string message)
/// <summary>
/// If the entity was trying to speak into a radio, this was the channel they were trying to access. If a radio
/// message gets sent on this channel, this should be set to null to prevent duplicate messages.
/// </summary>
public RadioChannelPrototype? Channel;
public EntitySpokeEvent(EntityUid source, string message, RadioChannelPrototype? channel, string? obfuscatedMessage)
{
Source = source;
Message = message;
Channel = channel;
ObfuscatedMessage = obfuscatedMessage;
}
}