From 54e5f2b4877f1ad2dea2b9bfbbc193ba2f3b68ea Mon Sep 17 00:00:00 2001 From: Dusk-92 Date: Tue, 25 Aug 2026 22:48:42 +0200 Subject: [PATCH] Load player cache before chat history restore --- libs/libunitscan.lua | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/libs/libunitscan.lua b/libs/libunitscan.lua index 9313a63..7965f9c 100644 --- a/libs/libunitscan.lua +++ b/libs/libunitscan.lua @@ -9,6 +9,34 @@ local PASSIVE_CACHE_TTL = 90 * 24 * 60 * 60 local libunitscan = CreateFrame("Frame", "ShaguTweaksUnitScan", UIParent) +-- Chat history can be restored before PLAYER_ENTERING_WORLD. Bind the saved +-- player database lazily whenever unit data is queried so Social Colors can use +-- classes learned before a /reload. Merge any transient entries collected before +-- SavedVariables became available instead of discarding them. +local function EnsurePlayerCache() + ShaguTweaks_cache = ShaguTweaks_cache or {} + ShaguTweaks_cache["players"] = ShaguTweaks_cache["players"] or {} + + if units.players ~= ShaguTweaks_cache["players"] then + local transient = units.players + units.players = ShaguTweaks_cache["players"] + + for name, data in pairs(transient) do + if not units.players[name] then + units.players[name] = data + elseif data then + for key, value in pairs(data) do + if units.players[name][key] == nil then + units.players[name][key] = value + end + end + end + end + end + + return units.players +end + local function EnrichPlayerFromClassicAPI(name) if not name or not API or not API.GetCachedPlayerInfoByName then return end @@ -51,6 +79,8 @@ end function ShaguTweaks.GetUnitData(name, active) if not name then return end + EnsurePlayerCache() + if not units["players"][name] then EnrichPlayerFromClassicAPI(name) if not units["players"][name] then @@ -90,9 +120,7 @@ end local function RememberPlayer(name) if not name or name == "" or name == _G.UNKNOWN then return end - ShaguTweaks_cache = ShaguTweaks_cache or {} - ShaguTweaks_cache["players"] = ShaguTweaks_cache["players"] or {} - units.players = ShaguTweaks_cache["players"] + EnsurePlayerCache() -- ClassicAPI exposes the GUID of the CHAT_MSG_* sender synchronously. -- Resolve class from the engine's existing name cache only: this is a pure @@ -166,10 +194,9 @@ end libunitscan:SetScript("OnEvent", function() if event == "PLAYER_ENTERING_WORLD" then - -- load database - ShaguTweaks_cache = ShaguTweaks_cache or {} - ShaguTweaks_cache["players"] = ShaguTweaks_cache["players"] or {} - units.players = ShaguTweaks_cache["players"] + -- Ensure the saved database is already bound even if another module queried + -- it earlier while restoring UI state. + EnsurePlayerCache() -- One cheap cleanup pass at login. Known classes are kept forever. PrunePassivePlayers()