FrenchAccentSystem.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using Content.Server.Speech.Components;
  2. using System.Text.RegularExpressions;
  3. namespace Content.Server.Speech.EntitySystems;
  4. /// <summary>
  5. /// System that gives the speaker a faux-French accent.
  6. /// </summary>
  7. public sealed class FrenchAccentSystem : EntitySystem
  8. {
  9. [Dependency] private readonly ReplacementAccentSystem _replacement = default!;
  10. private static readonly Regex RegexTh = new(@"th", RegexOptions.IgnoreCase);
  11. private static readonly Regex RegexStartH = new(@"(?<!\w)h", RegexOptions.IgnoreCase);
  12. private static readonly Regex RegexSpacePunctuation = new(@"(?<=\w\w)[!?;:](?!\w)", RegexOptions.IgnoreCase);
  13. public override void Initialize()
  14. {
  15. base.Initialize();
  16. SubscribeLocalEvent<FrenchAccentComponent, AccentGetEvent>(OnAccentGet);
  17. }
  18. public string Accentuate(string message, FrenchAccentComponent component)
  19. {
  20. var msg = message;
  21. msg = _replacement.ApplyReplacements(msg, "french");
  22. // replaces th with z
  23. msg = RegexTh.Replace(msg, "'z");
  24. // replaces h with ' at the start of words.
  25. msg = RegexStartH.Replace(msg, "'");
  26. // spaces out ! ? : and ;.
  27. msg = RegexSpacePunctuation.Replace(msg, " $&");
  28. return msg;
  29. }
  30. private void OnAccentGet(EntityUid uid, FrenchAccentComponent component, AccentGetEvent args)
  31. {
  32. args.Message = Accentuate(args.Message, component);
  33. }
  34. }