From fdae6b1cd204ff829a0e40cf6587baeed023e84d Mon Sep 17 00:00:00 2001 From: Dean Sheather Date: Mon, 29 Apr 2024 18:55:24 +1000 Subject: [PATCH] fix: add Races, Clans and Companion Actions to auto-translate The Race, Tribe and BuddyAction sheets were skipped as the default behavior was to include rows from `0..^0` if no range was specified in the Completion sheet. That range would usually include everything, but since the ranges were iterated over as integers there is no "last" index so `^0` equals zero. Also preloads the AutoTranslate cache on plugin load in a new thread to avoid a hitch when sending the first message. This behavior is disabled in DEBUG builds to make profiling easier. --- ChatTwo/Plugin.cs | 7 +++++++ ChatTwo/Util/AutoTranslate.cs | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/ChatTwo/Plugin.cs b/ChatTwo/Plugin.cs index eb3f7fe..105602a 100755 --- a/ChatTwo/Plugin.cs +++ b/ChatTwo/Plugin.cs @@ -120,6 +120,13 @@ public sealed class Plugin : IDalamudPlugin Framework.Update += FrameworkUpdate; Interface.UiBuilder.Draw += Draw; Interface.LanguageChanged += LanguageChanged; + +#if !DEBUG + // Avoid 300ms hitch when sending first message by preloading the + // auto-translate cache. Don't do this in debug because it makes + // profiling difficult. + AutoTranslate.PreloadCache(DataManager); +#endif } catch { diff --git a/ChatTwo/Util/AutoTranslate.cs b/ChatTwo/Util/AutoTranslate.cs index cfe0085..d7b48a3 100644 --- a/ChatTwo/Util/AutoTranslate.cs +++ b/ChatTwo/Util/AutoTranslate.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; using Dalamud; @@ -92,6 +93,23 @@ internal static class AutoTranslate return string.Join("", payloads); } + /// + /// 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. + /// + internal static void PreloadCache(IDataManager data) + { + new Thread(() => + { + var sw = Stopwatch.StartNew(); + AllEntries(data); + Plugin.Log.Debug($"Warming up auto-translate took {sw.ElapsedMilliseconds}ms"); + }).Start(); + } + private static List AllEntries(IDataManager data) { if (Entries.TryGetValue(data.Language, out var entries)) @@ -150,12 +168,21 @@ internal static class AutoTranslate if (columns.Count == 0) columns.Add(0); - if (rows.Count == 0) - rows.Add(..); - var validRows = rowParsers.Select(p => p.RowId).ToArray(); + 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. + rows.Add(..Index.FromStart((int)validRows.Max())); + 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. for (var i = range.Start.Value; i < range.End.Value; i++) { if (!validRows.Contains((uint) i))