IListener and IRadio purge (#11980)
This commit is contained in:
@@ -1,22 +1,12 @@
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Server.Radio.Components;
|
||||
using Content.Shared.Radio;
|
||||
using System.Text.RegularExpressions;
|
||||
using Content.Server.Explosion.EntitySystems;
|
||||
using Content.Server.Radio.EntitySystems;
|
||||
using System;
|
||||
|
||||
namespace Content.Server.Explosion.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Sends a trigger when the keyphrase is heard
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(IListen))]
|
||||
public sealed class TriggerOnVoiceComponent : Component, IListen
|
||||
public sealed class TriggerOnVoiceComponent : Component
|
||||
{
|
||||
private SharedInteractionSystem _sharedInteractionSystem = default!;
|
||||
private TriggerSystem _triggerSystem = default!;
|
||||
public bool IsListening => IsRecording || !string.IsNullOrWhiteSpace(KeyPhrase);
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("keyPhrase")]
|
||||
@@ -26,51 +16,13 @@ namespace Content.Server.Explosion.Components
|
||||
[DataField("listenRange")]
|
||||
public int ListenRange { get; private set; } = 4;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("isRecording")]
|
||||
public bool IsRecording = false;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("minLength")]
|
||||
public int MinLength = 3;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("maxLength")]
|
||||
public int MaxLength = 50;
|
||||
|
||||
/// <summary>
|
||||
/// Displays 'recorded' popup only for the one who activated
|
||||
/// it in order to allow for stealthily recording others
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public EntityUid Activator;
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_sharedInteractionSystem = EntitySystem.Get<SharedInteractionSystem>();
|
||||
_triggerSystem = EntitySystem.Get<TriggerSystem>();
|
||||
}
|
||||
|
||||
bool IListen.CanListen(string message, EntityUid source, RadioChannelPrototype? channelPrototype)
|
||||
{
|
||||
//will hear standard speech and radio messages originating nearby but not independant whispers
|
||||
return _sharedInteractionSystem.InRangeUnobstructed(Owner, source, range: ListenRange);
|
||||
}
|
||||
|
||||
void IListen.Listen(string message, EntityUid speaker, RadioChannelPrototype? channel)
|
||||
{
|
||||
message = message.Trim();
|
||||
|
||||
if (IsRecording && message.Length >= MinLength && message.Length <= MaxLength)
|
||||
{
|
||||
KeyPhrase = message;
|
||||
_triggerSystem.ToggleRecord(this, Activator, true);
|
||||
}
|
||||
else if (KeyPhrase != null && message.Contains(KeyPhrase, StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
_triggerSystem.Trigger(Owner, speaker);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
using Content.Server.Explosion.Components;
|
||||
using Content.Server.Nutrition.Components;
|
||||
using Content.Server.Speech;
|
||||
using Content.Server.Speech.Components;
|
||||
using Content.Shared.Database;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Interaction.Events;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Verbs;
|
||||
using Microsoft.CodeAnalysis.Options;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Physics.Events;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Server.Explosion.EntitySystems
|
||||
{
|
||||
@@ -16,8 +12,37 @@ namespace Content.Server.Explosion.EntitySystems
|
||||
{
|
||||
private void InitializeVoice()
|
||||
{
|
||||
SubscribeLocalEvent<TriggerOnVoiceComponent, ComponentInit>(OnVoiceInit);
|
||||
SubscribeLocalEvent<TriggerOnVoiceComponent, ExaminedEvent>(OnVoiceExamine);
|
||||
SubscribeLocalEvent<TriggerOnVoiceComponent, GetVerbsEvent<AlternativeVerb>>(OnVoiceGetAltVerbs);
|
||||
SubscribeLocalEvent<TriggerOnVoiceComponent, ListenEvent>(OnListen);
|
||||
}
|
||||
|
||||
private void OnVoiceInit(EntityUid uid, TriggerOnVoiceComponent component, ComponentInit args)
|
||||
{
|
||||
if (component.IsListening)
|
||||
EnsureComp<ActiveListenerComponent>(uid).Range = component.ListenRange;
|
||||
else
|
||||
RemCompDeferred<ActiveListenerComponent>(uid);
|
||||
}
|
||||
|
||||
private void OnListen(EntityUid uid, TriggerOnVoiceComponent component, ListenEvent args)
|
||||
{
|
||||
var message = args.Message.Trim();
|
||||
|
||||
if (component.IsRecording)
|
||||
{
|
||||
if (message.Length >= component.MinLength || message.Length <= component.MaxLength)
|
||||
FinishRecording(component, args.Source, args.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(component.KeyPhrase) && message.Contains(component.KeyPhrase, StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
_adminLogger.Add(LogType.Trigger, LogImpact.High,
|
||||
$"A voice-trigger on {ToPrettyString(uid):entity} was triggered by {ToPrettyString(args.Source):speaker} speaking the key-phrase {component.KeyPhrase}.");
|
||||
Trigger(uid);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnVoiceGetAltVerbs(EntityUid uid, TriggerOnVoiceComponent component, GetVerbsEvent<AlternativeVerb> args)
|
||||
@@ -27,35 +52,74 @@ namespace Content.Server.Explosion.EntitySystems
|
||||
|
||||
args.Verbs.Add(new AlternativeVerb()
|
||||
{
|
||||
Text = Loc.GetString("verb-trigger-voice-record"),
|
||||
Act = () => ToggleRecord(component, args.User),
|
||||
Text = Loc.GetString(component.IsRecording ? "verb-trigger-voice-record-stop" : "verb-trigger-voice-record"),
|
||||
Act = () =>
|
||||
{
|
||||
if (component.IsRecording)
|
||||
StopRecording(component);
|
||||
else
|
||||
StartRecording(component, args.User);
|
||||
},
|
||||
Priority = 1
|
||||
});
|
||||
|
||||
if (string.IsNullOrWhiteSpace(component.KeyPhrase))
|
||||
return;
|
||||
|
||||
|
||||
args.Verbs.Add(new AlternativeVerb()
|
||||
{
|
||||
Text = Loc.GetString("verb-trigger-voice-clear"),
|
||||
Act = () =>
|
||||
{
|
||||
component.KeyPhrase = null;
|
||||
component.IsRecording = false;
|
||||
RemComp<ActiveListenerComponent>(uid);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void ToggleRecord(TriggerOnVoiceComponent component, EntityUid user, bool recorded = false)
|
||||
public void StartRecording(TriggerOnVoiceComponent component, EntityUid user)
|
||||
{
|
||||
component.IsRecording ^= true;
|
||||
component.IsRecording = true;
|
||||
EnsureComp<ActiveListenerComponent>(component.Owner).Range = component.ListenRange;
|
||||
|
||||
if (recorded) //recording success popup
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("popup-trigger-voice-recorded"), component.Owner, Filter.Entities(user));
|
||||
}
|
||||
else if (component.IsRecording) //recording start popup
|
||||
{
|
||||
component.Activator = user;
|
||||
_popupSystem.PopupEntity(Loc.GetString("popup-trigger-voice-start-recording"), component.Owner, Filter.Entities(user));
|
||||
}
|
||||
else //recording stopped manually popup
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("popup-trigger-voice-stop-recording"), component.Owner, Filter.Entities(user));
|
||||
}
|
||||
_adminLogger.Add(LogType.Trigger, LogImpact.Low,
|
||||
$"A voice-trigger on {ToPrettyString(component.Owner):entity} has started recording. User: {ToPrettyString(user):user}");
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString("popup-trigger-voice-start-recording"), component.Owner, Filter.Pvs(component.Owner));
|
||||
}
|
||||
|
||||
public void StopRecording(TriggerOnVoiceComponent component)
|
||||
{
|
||||
component.IsRecording = false;
|
||||
if (string.IsNullOrWhiteSpace(component.KeyPhrase))
|
||||
RemComp<ActiveListenerComponent>(component.Owner);
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString("popup-trigger-voice-stop-recording"), component.Owner, Filter.Pvs(component.Owner));
|
||||
}
|
||||
|
||||
public void FinishRecording(TriggerOnVoiceComponent component, EntityUid source, string message)
|
||||
{
|
||||
component.KeyPhrase = message;
|
||||
component.IsRecording = false;
|
||||
|
||||
_adminLogger.Add(LogType.Trigger, LogImpact.Low,
|
||||
$"A voice-trigger on {ToPrettyString(component.Owner):entity} has recorded a new keyphrase: '{component.KeyPhrase}'. Recorded from {ToPrettyString(source):speaker}");
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString("popup-trigger-voice-recorded", ("keyphrase", component.KeyPhrase!)), component.Owner, Filter.Pvs(component.Owner));
|
||||
}
|
||||
|
||||
private void OnVoiceExamine(EntityUid uid, TriggerOnVoiceComponent component, ExaminedEvent args)
|
||||
{
|
||||
args.PushText(Loc.GetString("examine-trigger-voice"));
|
||||
if (args.IsInDetailsRange)
|
||||
args.PushText(Loc.GetString("examine-trigger-voice", ("keyphrase", component.KeyPhrase?? Loc.GetString("trigger-voice-uninitialized"))));
|
||||
{
|
||||
if (component.KeyPhrase == null)
|
||||
args.PushText(string.IsNullOrWhiteSpace(component.KeyPhrase)
|
||||
? Loc.GetString("examine-trigger-voice-blank")
|
||||
: Loc.GetString("examine-trigger-voice-keyphrase", ("keyphrase", component.KeyPhrase)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user