From fa41d22cb8ca7437019772a4837c81fc581c006a Mon Sep 17 00:00:00 2001 From: Infi Date: Sat, 11 May 2024 05:13:31 +0200 Subject: [PATCH] Only load emote list if the user has them activated --- ChatTwo/EmoteCache.cs | 50 ++++++++++++++++++++++++------------------ ChatTwo/Plugin.cs | 3 +++ ChatTwo/Ui/Settings.cs | 3 +++ 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/ChatTwo/EmoteCache.cs b/ChatTwo/EmoteCache.cs index b42bff3..a1bd1d5 100644 --- a/ChatTwo/EmoteCache.cs +++ b/ChatTwo/EmoteCache.cs @@ -29,42 +29,50 @@ public static class EmoteCache public bool Animated { get; set; } }; + private enum LoadingState + { + Unloaded, + Loading, + Done + } + private const string BetterTTV = "https://api.betterttv.net/3"; private const string GlobalEmotes = $"{BetterTTV}/cached/emotes/global"; private const string Top100Emotes = $"{BetterTTV}/emotes/shared/top?limit=100"; private const string EmotePath = "https://cdn.betterttv.net/emote/{0}/3x"; - private static readonly bool IsBTTVDataLoaded; - private static readonly Dictionary EmoteImages = new(); + private static readonly HttpClient Client = new(); - private static readonly Dictionary Cache = new(); + // All of this data is uninitalized while State is not `LoadingState.Done` + private static LoadingState State = LoadingState.Unloaded; + private static Dictionary EmoteImages = new(); - public static readonly string[] EmoteCodeArray = []; + private static Dictionary Cache = new(); - static EmoteCache() + public static string[] EmoteCodeArray = []; + + public static async void LoadData() { + if (State is not LoadingState.Unloaded) + return; + + State = LoadingState.Loading; try { - var globalCache = new HttpClient().GetAsync(GlobalEmotes) - .Result - .Content - .ReadAsStringAsync() - .Result; + var global = await Client.GetAsync(GlobalEmotes); + var globalList = await global.Content.ReadAsStringAsync(); - foreach (var emote in JsonSerializer.Deserialize(globalCache)!) + foreach (var emote in JsonSerializer.Deserialize(globalList)!) Cache.TryAdd(emote.Code, emote); - var top100 = new HttpClient().GetAsync(Top100Emotes) - .Result - .Content - .ReadAsStringAsync() - .Result; + var top = await Client.GetAsync(Top100Emotes); + var topList = await top.Content.ReadAsStringAsync(); - foreach (var emote in JsonSerializer.Deserialize>(top100)!) + foreach (var emote in JsonSerializer.Deserialize>(topList)!) Cache.TryAdd(emote.Emote.Code, emote.Emote); EmoteCodeArray = Cache.Keys.ToArray(); - IsBTTVDataLoaded = true; + State = LoadingState.Done; } catch (Exception ex) { @@ -74,12 +82,12 @@ public static class EmoteCache internal static bool Exists(string code) { - return IsBTTVDataLoaded && EmoteCodeArray.Contains(code); + return State is LoadingState.Done && EmoteCodeArray.Contains(code); } internal static IEmote? GetEmote(string code) { - if (!IsBTTVDataLoaded) + if (State is not LoadingState.Done) return null; if (!Cache.TryGetValue(code, out var emoteDetail)) @@ -111,7 +119,7 @@ public static class EmoteCache public class IEmote { - public bool IsLoaded = false; + public bool IsLoaded; public bool IsAnimated = false; public IDalamudTextureWrap Texture; diff --git a/ChatTwo/Plugin.cs b/ChatTwo/Plugin.cs index f4cfd9b..b5759ca 100755 --- a/ChatTwo/Plugin.cs +++ b/ChatTwo/Plugin.cs @@ -118,6 +118,9 @@ public sealed class Plugin : IDalamudPlugin Interface.UiBuilder.Draw += Draw; Interface.LanguageChanged += LanguageChanged; + if (Config.ShowEmotes) + Task.Run(EmoteCache.LoadData); + #if !DEBUG // Avoid 300ms hitch when sending first message by preloading the // auto-translate cache. Don't do this in debug because it makes diff --git a/ChatTwo/Ui/Settings.cs b/ChatTwo/Ui/Settings.cs index 596066e..877fe7d 100755 --- a/ChatTwo/Ui/Settings.cs +++ b/ChatTwo/Ui/Settings.cs @@ -172,6 +172,9 @@ public sealed class SettingsWindow : Window if (hideChanged) GameFunctions.GameFunctions.SetChatInteractable(true); + if (Plugin.Config.ShowEmotes) + Task.Run(EmoteCache.LoadData); + Initialise(); } }