1
0

VoiceMaskSystem.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Content.Shared.Actions;
  2. using Content.Shared.Administration.Logs;
  3. using Content.Shared.Chat;
  4. using Content.Shared.Clothing;
  5. using Content.Shared.Database;
  6. using Content.Shared.Inventory;
  7. using Content.Shared.Popups;
  8. using Content.Shared.Preferences;
  9. using Content.Shared.Speech;
  10. using Content.Shared.VoiceMask;
  11. using Robust.Shared.Prototypes;
  12. namespace Content.Server.VoiceMask;
  13. public sealed partial class VoiceMaskSystem : EntitySystem
  14. {
  15. [Dependency] private readonly SharedUserInterfaceSystem _uiSystem = default!;
  16. [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
  17. [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
  18. [Dependency] private readonly IPrototypeManager _proto = default!;
  19. [Dependency] private readonly SharedActionsSystem _actions = default!;
  20. public override void Initialize()
  21. {
  22. base.Initialize();
  23. SubscribeLocalEvent<VoiceMaskComponent, InventoryRelayedEvent<TransformSpeakerNameEvent>>(OnTransformSpeakerName);
  24. SubscribeLocalEvent<VoiceMaskComponent, VoiceMaskChangeNameMessage>(OnChangeName);
  25. SubscribeLocalEvent<VoiceMaskComponent, VoiceMaskChangeVerbMessage>(OnChangeVerb);
  26. SubscribeLocalEvent<VoiceMaskComponent, ClothingGotEquippedEvent>(OnEquip);
  27. SubscribeLocalEvent<VoiceMaskSetNameEvent>(OpenUI);
  28. }
  29. private void OnTransformSpeakerName(Entity<VoiceMaskComponent> entity, ref InventoryRelayedEvent<TransformSpeakerNameEvent> args)
  30. {
  31. args.Args.VoiceName = GetCurrentVoiceName(entity);
  32. args.Args.SpeechVerb = entity.Comp.VoiceMaskSpeechVerb ?? args.Args.SpeechVerb;
  33. }
  34. #region User inputs from UI
  35. private void OnChangeVerb(Entity<VoiceMaskComponent> entity, ref VoiceMaskChangeVerbMessage msg)
  36. {
  37. if (msg.Verb is { } id && !_proto.HasIndex<SpeechVerbPrototype>(id))
  38. return;
  39. entity.Comp.VoiceMaskSpeechVerb = msg.Verb;
  40. // verb is only important to metagamers so no need to log as opposed to name
  41. _popupSystem.PopupEntity(Loc.GetString("voice-mask-popup-success"), entity, msg.Actor);
  42. UpdateUI(entity);
  43. }
  44. private void OnChangeName(Entity<VoiceMaskComponent> entity, ref VoiceMaskChangeNameMessage message)
  45. {
  46. if (message.Name.Length > HumanoidCharacterProfile.MaxNameLength || message.Name.Length <= 0)
  47. {
  48. _popupSystem.PopupEntity(Loc.GetString("voice-mask-popup-failure"), entity, message.Actor, PopupType.SmallCaution);
  49. return;
  50. }
  51. entity.Comp.VoiceMaskName = message.Name;
  52. _adminLogger.Add(LogType.Action, LogImpact.Medium, $"{ToPrettyString(message.Actor):player} set voice of {ToPrettyString(entity):mask}: {entity.Comp.VoiceMaskName}");
  53. _popupSystem.PopupEntity(Loc.GetString("voice-mask-popup-success"), entity, message.Actor);
  54. UpdateUI(entity);
  55. }
  56. #endregion
  57. #region UI
  58. private void OnEquip(EntityUid uid, VoiceMaskComponent component, ClothingGotEquippedEvent args)
  59. {
  60. _actions.AddAction(args.Wearer, ref component.ActionEntity, component.Action, uid);
  61. }
  62. private void OpenUI(VoiceMaskSetNameEvent ev)
  63. {
  64. var maskEntity = ev.Action.Comp.Container;
  65. if (!TryComp<VoiceMaskComponent>(maskEntity, out var voiceMaskComp))
  66. return;
  67. if (!_uiSystem.HasUi(maskEntity.Value, VoiceMaskUIKey.Key))
  68. return;
  69. _uiSystem.OpenUi(maskEntity.Value, VoiceMaskUIKey.Key, ev.Performer);
  70. UpdateUI((maskEntity.Value, voiceMaskComp));
  71. }
  72. private void UpdateUI(Entity<VoiceMaskComponent> entity)
  73. {
  74. if (_uiSystem.HasUi(entity, VoiceMaskUIKey.Key))
  75. _uiSystem.SetUiState(entity.Owner, VoiceMaskUIKey.Key, new VoiceMaskBuiState(GetCurrentVoiceName(entity), entity.Comp.VoiceMaskSpeechVerb));
  76. }
  77. #endregion
  78. #region Helper functions
  79. private string GetCurrentVoiceName(Entity<VoiceMaskComponent> entity)
  80. {
  81. return entity.Comp.VoiceMaskName ?? Loc.GetString("voice-mask-default-name-override");
  82. }
  83. #endregion
  84. }