restore messages: fixed colouring finally, man :)

This commit is contained in:
Sattva
2025-04-25 11:05:28 +03:00
parent c8ecec83f9
commit 7d49e7acd3
+71 -130
View File
@@ -4364,153 +4364,94 @@
if LeaPlusLC["RestoreChatMessages"] == "On" and not LeaLockList["RestoreChatMessages"] then if LeaPlusLC["RestoreChatMessages"] == "On" and not LeaLockList["RestoreChatMessages"] then
local historyFrame = CreateFrame("FRAME") -- ----------------------------------------------------------------
historyFrame:RegisterEvent("PLAYER_LOGIN") -- shared helpers --------------------------------------------------
historyFrame:RegisterEvent("PLAYER_LOGOUT") -------------------------------------------------------------------
-- chat-type index ➜ token (same table ShowChatbox builds)
local FCF_IsChatWindowIndexActive = FCF_IsChatWindowIndexActive local chatTypeIndexToName = {}
local GetMessageInfo = GetMessageInfo for t in pairs(ChatTypeInfo) do
local GetNumMessages = GetNumMessages chatTypeIndexToName[ GetChatTypeIndex(t) ] = t
-- Use function from Dragonflight
local function FCF_IsChatWindowIndexActive(chatWindowIndex)
-- print("exectuing FCF func")
local shown = select(7, FCF_GetChatWindowInfo(chatWindowIndex))
if shown then
return true
end
local chatFrame = _G["ChatFrame" .. chatWindowIndex]
return (chatFrame and chatFrame.isDocked)
end end
-- Save chat messages on logout -- strip any icons and wrap the whole line in the right colour
historyFrame:SetScript("OnEvent", function(self, event) local function CleanAndColour(msg, lineID)
if event == "PLAYER_LOGOUT" then msg = gsub(msg, "|T.-|t", "") -- remove |Ttexture|t
-- print(event) msg = gsub(msg, "|A.-|a", "") -- remove |Aatlas|a
local name, realm = LibCompat.UnitFullName("player")
if not realm then local info = ChatTypeInfo[ chatTypeIndexToName[lineID] ]
realm = GetRealmName() local r, g, b = (info and info.r) or 1,
end (info and info.g) or 1,
-- print(name .. realm) (info and info.b) or 1
if name and realm then local hex = format("|cff%02x%02x%02x", r * 255, g * 255, b * 255)
LeaPlusDB["ChatHistoryName"] = name .. "-" .. realm return hex .. msg:gsub("|r", "|r" .. hex) .. "|r"
-- print(LeaPlusDB["ChatHistoryName"]) end
LeaPlusDB["ChatHistoryTime"] = time()
-- print(LeaPlusDB["ChatHistoryTime"]) local function FCF_IsChatWindowIndexActive(idx)
for i = 1, 50 do local shown = select(7, FCF_GetChatWindowInfo(idx))
if i ~= 2 and _G["ChatFrame" .. i] then if shown then return true end
if FCF_IsChatWindowIndexActive(i) then local cf = _G["ChatFrame" .. idx]
-- print("window active") return (cf and cf.isDocked)
LeaPlusDB["ChatHistory" .. i] = {} end
local chtfrm = _G["ChatFrame" .. i]
local NumMsg = chtfrm:GetNumMessages() ------------------------------------------------------------------
local MaxMsg = 499 -- SAVE on PLAYER_LOGOUT ----------------------------------------
local StartMsg = 1 ------------------------------------------------------------------
if NumMsg > MaxMsg then local saver = CreateFrame("Frame")
StartMsg = NumMsg - MaxMsg + 1 saver:RegisterEvent("PLAYER_LOGOUT")
end saver:SetScript("OnEvent", function()
-- print(NumMsg) local name, realm = LibCompat.UnitFullName("player")
for iMsg = StartMsg, NumMsg do realm = realm or GetRealmName() ; if not name then return end
local chatMessage, r, g, b, chatTypeID = chtfrm:GetMessageInfo(iMsg)
if chatMessage then LeaPlusDB["ChatHistoryName"] = name .. "-" .. realm
-- if r and g and b then LeaPlusDB["ChatHistoryTime"] = time()
-- local colorCode = RGBToColorCode(r, g, b)
chatMessage = chatMessage for i = 1, 50 do
-- print(chatMessage) if i ~= 2 and _G["ChatFrame" .. i] and FCF_IsChatWindowIndexActive(i) then
-- end local cf, num = _G["ChatFrame" .. i], _G["ChatFrame" .. i]:GetNumMessages()
tinsert(LeaPlusDB["ChatHistory" .. i], chatMessage) local first = (num > 499) and (num - 499 + 1) or 1
-- print("Inserted Chat Message: ".. chatMessage) LeaPlusDB["ChatHistory" .. i] = {}
end
end for n = first, num do
end local txt, _, lineID = cf:GetMessageInfo(n)
if txt then
tinsert(LeaPlusDB["ChatHistory" .. i], CleanAndColour(txt, lineID))
end end
end end
end end
end end
end) end)
-- Restore chat messages on login ------------------------------------------------------------------
local name, realm = LibCompat.UnitFullName("player") -- RESTORE once UI is ready (no LOGIN listener needed) ----------
if not realm then ------------------------------------------------------------------
realm = GetRealmName() local function RestoreHistory()
end local name, realm = LibCompat.UnitFullName("player")
if name and realm then realm = realm or GetRealmName()
if LeaPlusDB["ChatHistoryName"] and LeaPlusDB["ChatHistoryTime"] then if not (name and realm) then return end
-- print(LeaPlusDB["ChatHistoryName"])
-- print(LeaPlusDB["ChatHistoryTime"])
local timeDiff = time() - LeaPlusDB["ChatHistoryTime"]
-- print(timeDiff)
if timeDiff and timeDiff < 10 then
-- reload must be done within 15 seconds
if not LeaPlusDB["ChatHistoryName"] or not LeaPlusDB["ChatHistoryTime"] then return end
if time() - LeaPlusDB["ChatHistoryTime"] > 10 then return end -- only for quick /reloads
-- Store chat messages from current session and clear chat 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 FCF_IsChatWindowIndexActive(i) then local cf = _G["ChatFrame" .. i]
LeaPlusDB["ChatTemp" .. i] = {} cf:Clear()
local chtfrm = _G["ChatFrame" .. i]
local NumMsg = chtfrm:GetNumMessages() local restored = 0
for iMsg = 1, NumMsg do for _, line in ipairs(LeaPlusDB["ChatHistory" .. i] or {}) do
local chatMessage, r, g, b, chatTypeID = chtfrm:GetMessageInfo(iMsg) cf:AddMessage(line)
if chatMessage then restored = restored + 1
-- if r and g and b then
-- local colorCode = RGBToColorCode(r, g, b)
chatMessage = chatMessage
-- end
tinsert(LeaPlusDB["ChatTemp" .. i], chatMessage)
end
end
chtfrm:Clear()
end
end end
-- Restore chat messages from previous session if restored > 0 then
for i = 1, 50 do cf:AddMessage(format("|cffffd800" .. L["Restored"] .. " %d " .. L["messages"] .. "|r", restored))
if i ~= 2 and _G["ChatFrame" .. i] and LeaPlusDB["ChatHistory" .. i] and FCF_IsChatWindowIndexActive(i) then
LeaPlusDB["ChatHistory" .. i .. "Count"] = 0
-- Add previous session messages to chat
for k = 1, #LeaPlusDB["ChatHistory" .. i] do
if LeaPlusDB["ChatHistory" .. i][k] ~= string.match(LeaPlusDB["ChatHistory" .. i][k], "|cffffd800" .. L["Restored"] .. " " .. ".*" .. " " .. L["message"] .. ".*.|r") then
_G["ChatFrame" .. i]:AddMessage(LeaPlusDB["ChatHistory" .. i][k])
LeaPlusDB["ChatHistory" .. i .. "Count"] = LeaPlusDB["ChatHistory" .. i .. "Count"] + 1
end
end
-- Show how many messages were restored
if LeaPlusDB["ChatHistory" .. i .. "Count"] == 1 then
_G["ChatFrame" .. i]:AddMessage("|cffffd800" .. L["Restored"] .. " " .. LeaPlusDB["ChatHistory" .. i .. "Count"] .. " " .. L["message from previous session"] .. ".|r")
else
_G["ChatFrame" .. i]:AddMessage("|cffffd800" .. L["Restored"] .. " " .. LeaPlusDB["ChatHistory" .. i .. "Count"] .. " " .. L["messages from previous session"] .. ".|r")
end
else
-- No messages to restore
LeaPlusDB["ChatHistory" .. i] = nil
end
end end
-- Restore chat messages from this session
for i = 1, 50 do
if i ~= 2 and _G["ChatFrame" .. i] and LeaPlusDB["ChatTemp" .. i] and FCF_IsChatWindowIndexActive(i) then
for k = 1, #LeaPlusDB["ChatTemp" .. i] do
_G["ChatFrame" .. i]:AddMessage(LeaPlusDB["ChatTemp" .. i][k])
end
end
end
end end
end end
end end
else -- Run restore once, one second after addon load (chat frames are ready)
LibCompat.After(1, RestoreHistory)
-- Option is disabled so clear any messages from saved variables
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
---------------------------------------------------------------------- ----------------------------------------------------------------------