2022-10-16 13:44:50 -04:00
using Content.Server.Explosion.Components ;
2022-11-15 17:09:27 +13:00
using Content.Server.Speech ;
using Content.Server.Speech.Components ;
using Content.Shared.Database ;
2022-10-16 13:44:50 -04:00
using Content.Shared.Examine ;
2024-04-02 19:19:23 +05:00
using Content.Shared.Interaction.Events ;
2022-10-16 13:44:50 -04:00
using Content.Shared.Verbs ;
namespace Content.Server.Explosion.EntitySystems
{
public sealed partial class TriggerSystem
{
private void InitializeVoice ( )
{
2022-11-15 17:09:27 +13:00
SubscribeLocalEvent < TriggerOnVoiceComponent , ComponentInit > ( OnVoiceInit ) ;
2022-10-16 13:44:50 -04:00
SubscribeLocalEvent < TriggerOnVoiceComponent , ExaminedEvent > ( OnVoiceExamine ) ;
SubscribeLocalEvent < TriggerOnVoiceComponent , GetVerbsEvent < AlternativeVerb > > ( OnVoiceGetAltVerbs ) ;
2022-11-15 17:09:27 +13:00
SubscribeLocalEvent < TriggerOnVoiceComponent , ListenEvent > ( OnListen ) ;
2024-04-02 19:19:23 +05:00
SubscribeLocalEvent < TriggerOnVoiceComponent , UseInHandEvent > ( OnUseInHand ) ;
2022-11-15 17:09:27 +13:00
}
private void OnVoiceInit ( EntityUid uid , TriggerOnVoiceComponent component , ComponentInit args )
{
if ( component . IsListening )
EnsureComp < ActiveListenerComponent > ( uid ) . Range = component . ListenRange ;
else
2022-11-27 03:01:17 -05:00
RemCompDeferred < ActiveListenerComponent > ( uid ) ;
2022-11-15 17:09:27 +13:00
}
2023-10-19 12:34:31 -07:00
private void OnListen ( Entity < TriggerOnVoiceComponent > ent , ref ListenEvent args )
2022-11-15 17:09:27 +13:00
{
2023-10-19 12:34:31 -07:00
var component = ent . Comp ;
2022-11-15 17:09:27 +13:00
var message = args . Message . Trim ( ) ;
if ( component . IsRecording )
{
2024-02-21 01:57:23 -05:00
var ev = new ListenAttemptEvent ( args . Source ) ;
RaiseLocalEvent ( ent , ev ) ;
if ( ev . Cancelled )
return ;
if ( message . Length > = component . MinLength & & message . Length < = component . MaxLength )
2023-10-19 12:34:31 -07:00
FinishRecording ( ent , args . Source , args . Message ) ;
2024-02-21 01:57:23 -05:00
else if ( message . Length > component . MaxLength )
_popupSystem . PopupEntity ( Loc . GetString ( "popup-trigger-voice-record-failed-too-long" ) , ent ) ;
else if ( message . Length < component . MinLength )
_popupSystem . PopupEntity ( Loc . GetString ( "popup-trigger-voice-record-failed-too-short" ) , ent ) ;
2022-11-15 17:09:27 +13:00
return ;
}
if ( ! string . IsNullOrWhiteSpace ( component . KeyPhrase ) & & message . Contains ( component . KeyPhrase , StringComparison . InvariantCultureIgnoreCase ) )
{
_adminLogger . Add ( LogType . Trigger , LogImpact . High ,
2023-10-19 12:34:31 -07:00
$"A voice-trigger on {ToPrettyString(ent):entity} was triggered by {ToPrettyString(args.Source):speaker} speaking the key-phrase {component.KeyPhrase}." ) ;
Trigger ( ent , args . Source ) ;
2022-11-15 17:09:27 +13:00
}
2022-10-16 13:44:50 -04:00
}
2023-10-19 12:34:31 -07:00
private void OnVoiceGetAltVerbs ( Entity < TriggerOnVoiceComponent > ent , ref GetVerbsEvent < AlternativeVerb > args )
2022-10-16 13:44:50 -04:00
{
if ( ! args . CanInteract | | ! args . CanAccess )
return ;
2023-10-19 12:34:31 -07:00
var component = ent . Comp ;
var @event = args ;
2022-10-16 13:44:50 -04:00
args . Verbs . Add ( new AlternativeVerb ( )
{
2024-02-21 01:57:23 -05:00
Text = Loc . GetString ( component . IsRecording ? "verb-trigger-voice-stop" : "verb-trigger-voice-record" ) ,
2022-11-15 17:09:27 +13:00
Act = ( ) = >
{
if ( component . IsRecording )
2023-10-19 12:34:31 -07:00
StopRecording ( ent ) ;
2022-11-15 17:09:27 +13:00
else
2023-10-19 12:34:31 -07:00
StartRecording ( ent , @event . User ) ;
2022-11-15 17:09:27 +13:00
} ,
2022-10-16 13:44:50 -04:00
Priority = 1
} ) ;
2022-11-15 17:09:27 +13:00
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 ;
2023-10-19 12:34:31 -07:00
RemComp < ActiveListenerComponent > ( ent ) ;
2022-11-15 17:09:27 +13:00
}
} ) ;
2022-10-16 13:44:50 -04:00
}
2024-04-02 19:19:23 +05:00
public void OnUseInHand ( Entity < TriggerOnVoiceComponent > ent , ref UseInHandEvent args )
{
if ( args . Handled )
return ;
if ( ent . Comp . IsRecording )
{
StopRecording ( ent ) ;
}
else
{
StartRecording ( ent , args . User ) ;
}
}
2023-10-19 12:34:31 -07:00
public void StartRecording ( Entity < TriggerOnVoiceComponent > ent , EntityUid user )
2022-10-16 13:44:50 -04:00
{
2023-10-19 12:34:31 -07:00
var component = ent . Comp ;
2022-11-15 17:09:27 +13:00
component . IsRecording = true ;
2023-10-19 12:34:31 -07:00
EnsureComp < ActiveListenerComponent > ( ent ) . Range = component . ListenRange ;
2022-10-16 13:44:50 -04:00
2022-11-15 17:09:27 +13:00
_adminLogger . Add ( LogType . Trigger , LogImpact . Low ,
2023-10-19 12:34:31 -07:00
$"A voice-trigger on {ToPrettyString(ent):entity} has started recording. User: {ToPrettyString(user):user}" ) ;
2022-11-15 17:09:27 +13:00
2023-10-19 12:34:31 -07:00
_popupSystem . PopupEntity ( Loc . GetString ( "popup-trigger-voice-start-recording" ) , ent ) ;
2022-11-15 17:09:27 +13:00
}
2023-10-19 12:34:31 -07:00
public void StopRecording ( Entity < TriggerOnVoiceComponent > ent )
2022-11-15 17:09:27 +13:00
{
2023-10-19 12:34:31 -07:00
var component = ent . Comp ;
2022-11-15 17:09:27 +13:00
component . IsRecording = false ;
if ( string . IsNullOrWhiteSpace ( component . KeyPhrase ) )
2023-10-19 12:34:31 -07:00
RemComp < ActiveListenerComponent > ( ent ) ;
2022-11-15 17:09:27 +13:00
2023-10-19 12:34:31 -07:00
_popupSystem . PopupEntity ( Loc . GetString ( "popup-trigger-voice-stop-recording" ) , ent ) ;
2022-11-15 17:09:27 +13:00
}
2023-10-19 12:34:31 -07:00
public void FinishRecording ( Entity < TriggerOnVoiceComponent > ent , EntityUid source , string message )
2022-11-15 17:09:27 +13:00
{
2023-10-19 12:34:31 -07:00
var component = ent . Comp ;
2022-11-15 17:09:27 +13:00
component . KeyPhrase = message ;
component . IsRecording = false ;
_adminLogger . Add ( LogType . Trigger , LogImpact . Low ,
2023-10-19 12:34:31 -07:00
$"A voice-trigger on {ToPrettyString(ent):entity} has recorded a new keyphrase: '{component.KeyPhrase}'. Recorded from {ToPrettyString(source):speaker}" ) ;
2022-11-15 17:09:27 +13:00
2023-10-19 12:34:31 -07:00
_popupSystem . PopupEntity ( Loc . GetString ( "popup-trigger-voice-recorded" , ( "keyphrase" , component . KeyPhrase ! ) ) , ent ) ;
2022-10-16 13:44:50 -04:00
}
private void OnVoiceExamine ( EntityUid uid , TriggerOnVoiceComponent component , ExaminedEvent args )
{
if ( args . IsInDetailsRange )
2022-11-15 17:09:27 +13:00
{
args . PushText ( string . IsNullOrWhiteSpace ( component . KeyPhrase )
2022-11-27 03:01:17 -05:00
? Loc . GetString ( "trigger-voice-uninitialized" )
: Loc . GetString ( "examine-trigger-voice" , ( "keyphrase" , component . KeyPhrase ) ) ) ;
2022-11-15 17:09:27 +13:00
}
2022-10-16 13:44:50 -04:00
}
}
}