2023-02-11 13:26:44 -06:00
using Content.Server.Administration.Logs ;
2022-09-28 19:22:27 -07:00
using Content.Server.Chat.Systems ;
using Content.Server.Popups ;
2023-12-12 19:02:35 -07:00
using Content.Shared.Clothing ;
2023-02-11 13:26:44 -06:00
using Content.Shared.Database ;
2022-09-28 19:22:27 -07:00
using Content.Shared.Inventory.Events ;
2023-11-13 23:55:24 +01:00
using Content.Shared.Popups ;
2022-09-28 19:22:27 -07:00
using Content.Shared.Preferences ;
2024-03-28 06:36:43 +00:00
using Content.Shared.Speech ;
2022-09-28 19:22:27 -07:00
using Content.Shared.VoiceMask ;
using Robust.Server.GameObjects ;
2023-10-29 04:21:02 +11:00
using Robust.Shared.Player ;
2024-03-28 06:36:43 +00:00
using Robust.Shared.Prototypes ;
2022-09-28 19:22:27 -07:00
namespace Content.Server.VoiceMask ;
public sealed partial class VoiceMaskSystem : EntitySystem
{
[Dependency] private readonly UserInterfaceSystem _uiSystem = default ! ;
[Dependency] private readonly PopupSystem _popupSystem = default ! ;
2023-02-11 13:26:44 -06:00
[Dependency] private readonly IAdminLogManager _adminLogger = default ! ;
2024-03-28 06:36:43 +00:00
[Dependency] private readonly IPrototypeManager _proto = default ! ;
2022-09-28 19:22:27 -07:00
public override void Initialize ( )
{
SubscribeLocalEvent < VoiceMaskComponent , TransformSpeakerNameEvent > ( OnSpeakerNameTransform ) ;
SubscribeLocalEvent < VoiceMaskComponent , VoiceMaskChangeNameMessage > ( OnChangeName ) ;
2024-03-28 06:36:43 +00:00
SubscribeLocalEvent < VoiceMaskComponent , VoiceMaskChangeVerbMessage > ( OnChangeVerb ) ;
2023-12-14 17:39:11 -08:00
SubscribeLocalEvent < VoiceMaskComponent , WearerMaskToggledEvent > ( OnMaskToggled ) ;
2022-09-28 19:22:27 -07:00
SubscribeLocalEvent < VoiceMaskerComponent , GotEquippedEvent > ( OnEquip ) ;
SubscribeLocalEvent < VoiceMaskerComponent , GotUnequippedEvent > ( OnUnequip ) ;
SubscribeLocalEvent < VoiceMaskSetNameEvent > ( OnSetName ) ;
// SubscribeLocalEvent<VoiceMaskerComponent, GetVerbsEvent<AlternativeVerb>>(GetVerbs);
2023-04-27 08:03:44 +06:00
InitializeTTS ( ) ;
2022-09-28 19:22:27 -07:00
}
private void OnSetName ( VoiceMaskSetNameEvent ev )
{
OpenUI ( ev . Performer ) ;
}
private void OnChangeName ( EntityUid uid , VoiceMaskComponent component , VoiceMaskChangeNameMessage message )
{
if ( message . Name . Length > HumanoidCharacterProfile . MaxNameLength | | message . Name . Length < = 0 )
{
2023-11-13 23:55:24 +01:00
_popupSystem . PopupEntity ( Loc . GetString ( "voice-mask-popup-failure" ) , uid , message . Session , PopupType . SmallCaution ) ;
2022-09-28 19:22:27 -07:00
return ;
}
component . VoiceName = message . Name ;
2023-02-11 13:26:44 -06:00
if ( message . Session . AttachedEntity ! = null )
_adminLogger . Add ( LogType . Action , LogImpact . Medium , $"{ToPrettyString(message.Session.AttachedEntity.Value):player} set voice of {ToPrettyString(uid):mask}: {component.VoiceName}" ) ;
else
_adminLogger . Add ( LogType . Action , LogImpact . Medium , $"Voice of {ToPrettyString(uid):mask} set: {component.VoiceName}" ) ;
2022-09-28 19:22:27 -07:00
2023-11-13 23:55:24 +01:00
_popupSystem . PopupEntity ( Loc . GetString ( "voice-mask-popup-success" ) , uid , message . Session ) ;
2022-09-28 19:22:27 -07:00
TrySetLastKnownName ( uid , message . Name ) ;
UpdateUI ( uid , component ) ;
}
2024-03-28 06:36:43 +00:00
private void OnChangeVerb ( Entity < VoiceMaskComponent > ent , ref VoiceMaskChangeVerbMessage msg )
{
if ( msg . Verb is { } id & & ! _proto . HasIndex < SpeechVerbPrototype > ( id ) )
return ;
ent . Comp . SpeechVerb = msg . Verb ;
// verb is only important to metagamers so no need to log as opposed to name
_popupSystem . PopupEntity ( Loc . GetString ( "voice-mask-popup-success" ) , ent , msg . Session ) ;
TrySetLastSpeechVerb ( ent , msg . Verb ) ;
UpdateUI ( ent , ent . Comp ) ;
}
2022-09-28 19:22:27 -07:00
private void OnSpeakerNameTransform ( EntityUid uid , VoiceMaskComponent component , TransformSpeakerNameEvent args )
{
if ( component . Enabled )
{
/ *
args . Name = _idCard . TryGetIdCard ( uid , out var card ) & & ! string . IsNullOrEmpty ( card . FullName )
? card . FullName
: Loc . GetString ( "voice-mask-unknown" ) ;
* /
args . Name = component . VoiceName ;
2024-01-23 17:12:18 -05:00
if ( component . SpeechVerb ! = null )
args . SpeechVerb = component . SpeechVerb ;
2022-09-28 19:22:27 -07:00
}
}
2023-12-14 17:39:11 -08:00
private void OnMaskToggled ( Entity < VoiceMaskComponent > ent , ref WearerMaskToggledEvent args )
2023-12-12 19:02:35 -07:00
{
ent . Comp . Enabled = ! args . IsToggled ;
}
2022-09-28 19:22:27 -07:00
private void OpenUI ( EntityUid player , ActorComponent ? actor = null )
{
if ( ! Resolve ( player , ref actor ) )
return ;
2023-07-08 09:02:17 -07:00
if ( ! _uiSystem . TryGetUi ( player , VoiceMaskUIKey . Key , out var bui ) )
return ;
2022-09-28 19:22:27 -07:00
2023-07-08 09:02:17 -07:00
_uiSystem . OpenUi ( bui , actor . PlayerSession ) ;
2022-09-28 19:22:27 -07:00
UpdateUI ( player ) ;
}
private void UpdateUI ( EntityUid owner , VoiceMaskComponent ? component = null )
{
if ( ! Resolve ( owner , ref component ) )
{
return ;
}
2023-07-08 09:02:17 -07:00
if ( _uiSystem . TryGetUi ( owner , VoiceMaskUIKey . Key , out var bui ) )
2024-03-30 10:36:17 +07:00
_uiSystem . SetUiState ( bui , new VoiceMaskBuiState ( component . VoiceName , component . VoiceId , component . SpeechVerb ) ) ;
2022-09-28 19:22:27 -07:00
}
}