Files
tbd-station-14/Content.Server/Speech/EntitySystems/SlowAccentSystem.cs
Princess Cheeseballs 367ff79006 Accents Event to Shared (#38948)
* 27 file diff

* 27 file diff 2

* Apply suggestions from code review

---------

Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
2025-07-12 22:52:51 +02:00

43 lines
1.3 KiB
C#

using System.Text.RegularExpressions;
using Content.Server.Speech.Components;
using Content.Shared.Speech;
namespace Content.Server.Speech.EntitySystems;
public sealed partial class SlowAccentSystem : EntitySystem
{
/// <summary>
/// Matches whitespace characters or commas (with or without a space after them).
/// </summary>
private static readonly Regex WordEndings = new("\\s|, |,");
/// <summary>
/// Matches the end of the string only if the last character is a "word" character.
/// </summary>
private static readonly Regex NoFinalPunctuation = new("\\w\\z");
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SlowAccentComponent, AccentGetEvent>(OnAccentGet);
}
private void OnAccentGet(Entity<SlowAccentComponent> ent, ref AccentGetEvent args)
{
args.Message = Accentuate(ent, args.Message);
}
public string Accentuate(Entity<SlowAccentComponent> ent, string message)
{
// Add... some... delay... between... each... word
message = WordEndings.Replace(message, "... ");
// Add "..." to the end, if the last character is part of a word...
if (NoFinalPunctuation.IsMatch(message))
message += "...";
return message;
}
}