Code:
--Disclaimer: For anyone who attempts to read my code..... I'm sorry.
local addon = CreateFrame("Frame")
local db
local origadds = {
[ChatFrame1] = ChatFrame1.AddMessage,
--[ChatFrame2] = ChatFrame2.AddMessage,
[ChatFrame3] = ChatFrame3.AddMessage,
[ChatFrame4] = ChatFrame4.AddMessage,
[ChatFrame5] = ChatFrame5.AddMessage,
[ChatFrame6] = ChatFrame6.AddMessage
}
local tblsizes = setmetatable({}, {__index = function() return 0 end}) --nil == 0
local function NewAddMessage(f, msg, r, g, b, ...)
if msg then --print(nil) breaks things.
local tbl = db[f:GetName()]
local msgs, colours = tbl.msgs, tbl.colours
if tblsizes[tbl] > 50 then
tremove(msgs, 1)
tremove(colours, 1)
tremove(colours, 1)
tremove(colours, 1)
else
tblsizes[tbl] = tblsizes[tbl] + 1
end
tinsert(msgs, msg)
tinsert(colours, r or 1)
tinsert(colours, g or 1)
tinsert(colours, b or 1)
end
return origadds[f](f, msg, r, g, b, ...)
end
--Hook nice n' early
ChatFrame1.AddMessage = NewAddMessage
--ChatFrame2.AddMessage = NewAddMessage
ChatFrame3.AddMessage = NewAddMessage
ChatFrame4.AddMessage = NewAddMessage
ChatFrame5.AddMessage = NewAddMessage
ChatFrame6.AddMessage = NewAddMessage
local givetblmt
givetblmt = {__index = function(t, k)
local newt = setmetatable({}, givetblmt)
t[k] = newt
return newt
end}
addon:SetScript("OnEvent", function(self)
TransScriptDB = setmetatable(TransScriptDB or {}, givetblmt)
db = TransScriptDB
local f
local msgs, colours, x
for name, tbl in pairs(db) do
f = _G[name]
if f and f.AddMessage then
msgs, colours = tbl.msgs, tbl.colours
tblsizes[tbl] = #msgs
for i = 1, tblsizes[tbl] do
x = (i-1)*3+1
origadds[f](f, "|cFFFF7722>|r "..msgs[i], colours[x], colours[x+1], colours[x+2])
end
end
end
self:UnregisterEvent("ADDON_LOADED")
self:SetScript("OnEvent", nil)
end)
addon:RegisterEvent("ADDON_LOADED")
This is what I've been using for a long time, I'm pretty sure theres a better way to do it, but this is good enough for me.
Just make an addon and add this to the toc:
## SavedVariablesPerCharacter: TransScriptDB