docs: Fix the last comments i think now
Security / scan (push) Successful in 12s

This commit is contained in:
2026-05-11 08:11:30 +02:00
parent 7d73def53d
commit b1b6402827
10 changed files with 70 additions and 199 deletions
+11 -26
View File
@@ -17,10 +17,9 @@ internal static class AutoTranslate
private static readonly Dictionary<ClientLanguage, List<AutoTranslateEntry>> Entries = new();
private static readonly HashSet<(uint, uint)> ValidEntries = [];
// Serializes all reads and writes against Entries / ValidEntries.
// PreloadCache spawns a worker thread that fills both, while the main
// thread reads them via Matching / ReplaceWithPayload / StartsWithCommand
// — without this lock the HashSet/Dictionary access is undefined.
// Serialises all reads/writes against Entries and ValidEntries.
// PreloadCache fills both from a worker thread while the main thread
// reads via Matching/ReplaceWithPayload/StartsWithCommand.
private static readonly object EntriesLock = new();
private static Parser<char, (string name, Maybe<IEnumerable<ISelectorPart>> selector)> Parser()
@@ -54,13 +53,8 @@ internal static class AutoTranslate
return Map((name, sel) => (name, sel), sheetName, selector.Optional());
}
/// <summary>
/// Preloads auto-translate entries into the cache for the current game
/// language. Without this, the first message will take a long time to send
/// (which causes a hitch in the main thread).
///
/// This spawns a new thread.
/// </summary>
// Warms the auto-translate cache on a background thread so the first
// message send doesn't hitch the main thread.
internal static void PreloadCache()
{
new Thread(() =>
@@ -104,7 +98,7 @@ internal static class AutoTranslate
{
if (lookup is not ("" or "@"))
{
// SE added whitespace to the newest additions, but ParseOrThrow doesn't see them as valid
// SE added whitespace to newer entries; strip it before parsing.
lookup = lookup.Replace(" ", "");
var (sheetName, selector) = parser.ParseOrThrow(lookup);
@@ -144,19 +138,13 @@ internal static class AutoTranslate
columns.Add(0);
if (rows.Count == 0)
// We can't use an "index from end" (like `^0`) here because
// we're iterating over integers, not an array directly.
// Previously, we were setting `0..^0` which caused these
// sheets to be completely skipped due to this bug.
// See below.
// Can't use index-from-end here because we iterate over integers,
// not an array directly. `0..^0` would silently skip the sheet.
rows.Add(..Index.FromStart((int)sheet.GetRowAt(sheet.Count - 1).RowId + 1));
foreach (var range in rows)
{
// We iterate over the range by numerical values here, so
// we can't use an "index from end" otherwise nothing will
// happen.
// See above.
// Integer iteration -- can't use index-from-end (see above).
for (var i = range.Start.Value; i < range.End.Value; i++)
{
if (!sheet.TryGetRow((uint)i, out var rowParser))
@@ -261,7 +249,6 @@ internal static class AutoTranslate
if (bytes.Length <= search.Length)
return;
// populate the list of valid entries
bool needBuild;
lock (EntriesLock)
needBuild = ValidEntries.Count == 0;
@@ -308,9 +295,8 @@ internal static class AutoTranslate
start = -1;
}
// Pure managed comparison via Span avoids the msvcrt.dll P/Invoke,
// which is fragile under Wine and triggered an extra managed-to-
// unmanaged copy per check.
// Span comparison avoids the msvcrt.dll P/Invoke which is fragile
// under Wine and caused an extra managed-to-unmanaged copy per check.
if (
i + search.Length < bytes.Length
&& bytes.AsSpan(i, search.Length).SequenceEqual(search)
@@ -325,7 +311,6 @@ internal static class AutoTranslate
if (bytes.Length <= search.Length)
return false;
// populate the list of valid entries
bool needBuild;
lock (EntriesLock)
needBuild = ValidEntries.Count == 0;