/// </summary>
public sealed partial class ChatUIController : IOnSystemChanged<CharacterInfoSystem>
{
+ [Dependency] private readonly ILocalizationManager _loc = default!;
[UISystemDependency] private readonly CharacterInfoSystem _characterInfo = default!;
+ private static readonly Regex StartDoubleQuote = new("\"$");
+ private static readonly Regex EndDoubleQuote = new("^\"|(?<=^@)\"");
+ private static readonly Regex StartAtSign = new("^@");
+
/// <summary>
/// The list of words to be highlighted in the chatbox.
/// </summary>
- private List<string> _highlights = new();
+ private readonly List<string> _highlights = new();
/// <summary>
/// The string holding the hex color used to highlight words.
_config.OnValueChanged(CCVars.ChatHighlightsColor, (value) => { _highlightsColor = value; }, true);
// Load highlights if any were saved.
- string highlights = _config.GetCVar(CCVars.ChatHighlights);
+ var highlights = _config.GetCVar(CCVars.ChatHighlights);
if (!string.IsNullOrEmpty(highlights))
{
// We first subdivide the highlights based on newlines to prevent replacing
// a valid "\n" tag and adding it to the final regex.
- string[] splittedHighlights = newHighlights.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
+ var splittedHighlights = newHighlights.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
- for (int i = 0; i < splittedHighlights.Length; i++)
+ for (var i = 0; i < splittedHighlights.Length; i++)
{
// Replace every "\" character with a "\\" to prevent "\n", "\0", etc...
- string keyword = splittedHighlights[i].Replace(@"\", @"\\");
+ var keyword = splittedHighlights[i].Replace(@"\", @"\\");
// Escape the keyword to prevent special characters like "(" and ")" to be considered valid regex.
keyword = Regex.Escape(keyword);
// that make sure the words to match are separated by spaces or punctuation.
// NOTE: The reason why we don't use \b tags is that \b doesn't match reverse slash characters "\" so
// a pre-sanitized (see 1.) string like "\[test]" wouldn't get picked up by the \b.
- if (keyword.Count(c => (c == '"')) > 0)
+ if (keyword.Any(c => c == '"'))
{
// Matches the last double quote character.
- keyword = Regex.Replace(keyword, "\"$", "(?!\\w)");
+ keyword = StartDoubleQuote.Replace(keyword, "(?!\\w)");
// When matching for the first double quote character we also consider the possibility
// of the double quote being preceded by a @ character.
- keyword = Regex.Replace(keyword, "^\"|(?<=^@)\"", "(?<!\\w)");
+ keyword = EndDoubleQuote.Replace(keyword, "(?<!\\w)");
}
// Make sure any name tagged as ours gets highlighted only when others say it.
- keyword = Regex.Replace(keyword, "^@", "(?<=(?<=/name.*)|(?<=,.*\"\".*))");
+ keyword = StartAtSign.Replace(keyword, "(?<=(?<=/name.*)|(?<=,.*\"\".*))");
_highlights.Add(keyword);
}
var (_, job, _, _, entityName) = data;
// Mark this entity's name as our character name for the "UpdateHighlights" function.
- string newHighlights = "@" + entityName;
+ var newHighlights = "@" + entityName;
// Subdivide the character's name based on spaces or hyphens so that every word gets highlighted.
if (newHighlights.Count(c => (c == ' ' || c == '-')) == 1)
newHighlights = newHighlights.Split('-')[0] + "\n@" + newHighlights.Split('-')[^1];
// Convert the job title to kebab-case and use it as a key for the loc file.
- string jobKey = job.Replace(' ', '-').ToLower();
+ var jobKey = job.Replace(' ', '-').ToLower();
- if (Loc.TryGetString($"highlights-{jobKey}", out var jobMatches))
+ if (_loc.TryGetString($"highlights-{jobKey}", out var jobMatches))
newHighlights += '\n' + jobMatches.Replace(", ", "\n");
UpdateHighlights(newHighlights);