Files
crystall-punk-14/Content.Server/Speech/EntitySystems/FrenchAccentSystem.cs

47 lines
1.4 KiB
C#
Raw Permalink Normal View History

2024-02-15 00:52:24 +00:00
using Content.Server.Speech.Components;
using System.Text.RegularExpressions;
namespace Content.Server.Speech.EntitySystems;
/// <summary>
/// System that gives the speaker a faux-French accent.
/// </summary>
public sealed class FrenchAccentSystem : EntitySystem
{
[Dependency] private readonly ReplacementAccentSystem _replacement = default!;
private static readonly Regex RegexTh = new(@"th", RegexOptions.IgnoreCase);
private static readonly Regex RegexStartH = new(@"(?<!\w)h", RegexOptions.IgnoreCase);
private static readonly Regex RegexSpacePunctuation = new(@"(?<=\w\w)[!?;:](?!\w)", RegexOptions.IgnoreCase);
2024-02-15 00:52:24 +00:00
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FrenchAccentComponent, AccentGetEvent>(OnAccentGet);
}
public string Accentuate(string message, FrenchAccentComponent component)
{
var msg = message;
msg = _replacement.ApplyReplacements(msg, "french");
// replaces th with z
msg = RegexTh.Replace(msg, "'z");
2024-02-15 00:52:24 +00:00
// replaces h with ' at the start of words.
msg = RegexStartH.Replace(msg, "'");
2024-02-15 00:52:24 +00:00
// spaces out ! ? : and ;.
msg = RegexSpacePunctuation.Replace(msg, " $&");
2024-02-15 00:52:24 +00:00
return msg;
}
private void OnAccentGet(EntityUid uid, FrenchAccentComponent component, AccentGetEvent args)
{
args.Message = Accentuate(args.Message, component);
}
}