restore messages: improvements

This commit is contained in:
Sattva
2025-04-25 11:12:29 +03:00
parent 7d49e7acd3
commit a5d09da374
+38 -44
View File
@@ -4364,94 +4364,88 @@
if LeaPlusLC["RestoreChatMessages"] == "On" and not LeaLockList["RestoreChatMessages"] then if LeaPlusLC["RestoreChatMessages"] == "On" and not LeaLockList["RestoreChatMessages"] then
-- ---------------------------------------------------------------- -- ===== helpers ==================================================
-- shared helpers --------------------------------------------------
-------------------------------------------------------------------
-- chat-type index ➜ token (same table ShowChatbox builds)
local chatTypeIndexToName = {} local chatTypeIndexToName = {}
for t in pairs(ChatTypeInfo) do for t in pairs(ChatTypeInfo) do
chatTypeIndexToName[ GetChatTypeIndex(t) ] = t chatTypeIndexToName[ GetChatTypeIndex(t) ] = t
end end
-- strip any icons and wrap the whole line in the right colour
local function CleanAndColour(msg, lineID) local function CleanAndColour(msg, lineID)
msg = gsub(msg, "|T.-|t", "") -- remove |Ttexture|t msg = gsub(msg, "|T.-|t", "") -- strip textures
msg = gsub(msg, "|A.-|a", "") -- remove |Aatlas|a msg = gsub(msg, "|A.-|a", "") -- strip atlases
local inf = ChatTypeInfo[ chatTypeIndexToName[lineID] ]
local info = ChatTypeInfo[ chatTypeIndexToName[lineID] ] local r, g, b = (inf and inf.r) or 1, (inf and inf.g) or 1, (inf and inf.b) or 1
local r, g, b = (info and info.r) or 1, local hex = format("|cff%02x%02x%02x", r*255, g*255, b*255)
(info and info.g) or 1, return hex .. msg:gsub("|r", "|r"..hex) .. "|r"
(info and info.b) or 1
local hex = format("|cff%02x%02x%02x", r * 255, g * 255, b * 255)
return hex .. msg:gsub("|r", "|r" .. hex) .. "|r"
end end
local function FCF_IsChatWindowIndexActive(idx) local function WindowActive(idx)
local shown = select(7, FCF_GetChatWindowInfo(idx)) local shown = select(7, FCF_GetChatWindowInfo(idx))
if shown then return true end if shown then return true end
local cf = _G["ChatFrame" .. idx] local f = _G["ChatFrame"..idx]
return (cf and cf.isDocked) return (f and f.isDocked)
end end
------------------------------------------------------------------ -- ===== SAVE on logout ===========================================
-- SAVE on PLAYER_LOGOUT ----------------------------------------
------------------------------------------------------------------
local saver = CreateFrame("Frame") local saver = CreateFrame("Frame")
saver:RegisterEvent("PLAYER_LOGOUT") saver:RegisterEvent("PLAYER_LOGOUT")
saver:SetScript("OnEvent", function() saver:SetScript("OnEvent", function()
local name, realm = LibCompat.UnitFullName("player") local name, realm = LibCompat.UnitFullName("player")
realm = realm or GetRealmName() ; if not name then return end realm = realm or GetRealmName() ; if not name then return end
LeaPlusDB["ChatHistoryName"] = name .. "-" .. realm LeaPlusDB["ChatHistoryName"] = name .. "-" .. realm
LeaPlusDB["ChatHistoryTime"] = time() LeaPlusDB["ChatHistoryTime"] = time()
for i = 1, 50 do for i = 1, 50 do
if i ~= 2 and _G["ChatFrame" .. i] and FCF_IsChatWindowIndexActive(i) then if i ~= 2 and _G["ChatFrame"..i] and WindowActive(i) then
local cf, num = _G["ChatFrame" .. i], _G["ChatFrame" .. i]:GetNumMessages() local cf, num = _G["ChatFrame"..i], _G["ChatFrame"..i]:GetNumMessages()
local first = (num > 499) and (num - 499 + 1) or 1 local first = (num > 499) and (num - 499 + 1) or 1
LeaPlusDB["ChatHistory" .. i] = {} LeaPlusDB["ChatHistory"..i] = {}
for n = first, num do for n = first, num do
local txt, _, lineID = cf:GetMessageInfo(n) local txt, _, lineID = cf:GetMessageInfo(n)
if txt then if txt and not txt:find(L["Restored"], 1, true) then
tinsert(LeaPlusDB["ChatHistory" .. i], CleanAndColour(txt, lineID)) tinsert(LeaPlusDB["ChatHistory"..i], CleanAndColour(txt, lineID))
end end
end end
end end
end end
end) end)
------------------------------------------------------------------ -- ===== RESTORE once UI is up ====================================
-- RESTORE once UI is ready (no LOGIN listener needed) ---------- local function Restore()
------------------------------------------------------------------ if not (LeaPlusDB["ChatHistoryTime"] and LeaPlusDB["ChatHistoryName"]) then return end
local function RestoreHistory() if time() - LeaPlusDB["ChatHistoryTime"] > 10 then return end
local name, realm = LibCompat.UnitFullName("player")
realm = realm or GetRealmName()
if not (name and realm) then return end
if not LeaPlusDB["ChatHistoryName"] or not LeaPlusDB["ChatHistoryTime"] then return end
if time() - LeaPlusDB["ChatHistoryTime"] > 10 then return end -- only for quick /reloads
for i = 1, 50 do for i = 1, 50 do
if i ~= 2 and _G["ChatFrame" .. i] and FCF_IsChatWindowIndexActive(i) then if i ~= 2 and _G["ChatFrame"..i] and WindowActive(i) then
local cf = _G["ChatFrame" .. i] local cf = _G["ChatFrame"..i]
cf:Clear() cf:Clear()
local restored = 0 local restored = 0
for _, line in ipairs(LeaPlusDB["ChatHistory" .. i] or {}) do
for _, line in ipairs(LeaPlusDB["ChatHistory"..i] or {}) do
cf:AddMessage(line) cf:AddMessage(line)
restored = restored + 1 restored = restored + 1
end end
if restored > 0 then if restored > 0 then
cf:AddMessage(format("|cffffd800" .. L["Restored"] .. " %d " .. L["messages"] .. "|r", restored)) cf:AddMessage(format("|cffffd800%s %d %s|r",
L["Restored"], restored, L["message from previous session"]))
end end
end end
end end
end end
LibCompat.After(1, Restore)
-- Run restore once, one second after addon load (chat frames are ready) -- ===== OPTION OFF wipe stored history =============================
LibCompat.After(1, RestoreHistory) else
LeaPlusDB["ChatHistoryName"] = nil
LeaPlusDB["ChatHistoryTime"] = nil
for i = 1, 50 do
LeaPlusDB["ChatHistory"..i] = nil
LeaPlusDB["ChatTemp"..i] = nil
LeaPlusDB["ChatHistory"..i.."Count"] = nil
end
end end
---------------------------------------------------------------------- ----------------------------------------------------------------------