zz_Infoclock/core.lua

339 lines
10 KiB
Lua
Raw Normal View History

2021-01-13 15:15:33 +00:00
local name, addon = ...
local parentName = addon['parentName'] or "BrokerPack"
local childName = "zz_Infoclock"
local replaces = {
['Sunday'] = true,
['Monday'] = true,
['Tuesday'] = true,
['Wednesday'] = true,
['Thursday'] = true,
['Friday'] = true,
['Saturday'] = true,
['January'] = true,
['February'] = true,
['March'] = true,
['April'] = true,
['May'] = true,
['June'] = true,
['July'] = true,
['August'] = true,
['September'] = true,
['October'] = true,
['November'] = true,
['December'] = true,
}
local L = LibStub("AceLocale-3.0"):GetLocale(childName, true)
local defcolor = "ffffff"
local defaults = {
["showClock"] = true,
["showLatency"] = true,
["showFPS"] = true,
["update"] = 2,
["clockstring"] = "%H:%M",
["datestring"] = "%A, %d.%B",
["latencystring"] = "%world ms",
["fpsstring"] = "%fps "..L['FPS'],
["ldbstring"] = "%clock %latency %fps",
['datecolor'] = defcolor,
['fpscolor'] = defcolor,
['clockcolor'] = defcolor,
['latencycolor'] = "ffff00",
}
local options = {}
local ldb = LibStub:GetLibrary("LibDataBroker-1.1")
local ldbicon = LibStub("LibDBIcon-1.0")
local cols = {
['datecolor'] = true,
['fpscolor'] = true,
['clockcolor'] = true,
['latencycolor'] = true,
}
local ainfo = {}
local GetNumAddOns = GetNumAddOns
local GetAddOnInfo = GetAddOnInfo
local GetAddOnMemoryUsage = GetAddOnMemoryUsage
local GetAddOnCPUUsage = GetAddOnCPUUsage
local IsAddOnLoaded = IsAddOnLoaded
local line
-- Fix Classic
local C_CVar = C_CVar or {
GetCVarBool = function(value)
return GetCVar(value) and true or false
end
}
local GameTimeFrame_OnClick = GameTimeFrame_OnClick or function(self) TimeManagerClockButton:Click() end
-- End of Fix Classic
local nextUpdate = GetTime()
local db = {}
local function toggleColor(col,col1,col2)
return col == col1 and col2 or col1
end
local function compute()
local c = date(db['clockstring'])
local d = date(db['datestring'])
for k in pairs(replaces) do
if(k ~= L[k]) then
d = d:gsub(k, L[k])
end
end
local h, w = select(3,GetNetStats())
local l = db['latencystring']
l = l:gsub("%%world", w)
l = l:gsub("%%local", h)
local f = db['fpsstring']
f = f:gsub("%%fps",floor(GetFramerate()))
return c,d,l,f
end
local function sortTab(a,b)
return a['mem'] > b['mem']
end
local function sortTab2(a,b)
return a['cpu'] > b['cpu']
end
local ainfodelay, lastainfo = 2, GetTime()
local function updateainfo()
wipe(ainfo)
UpdateAddOnMemoryUsage()
UpdateAddOnCPUUsage()
local name, enabled, security, mem, cpu, _
for i=1, GetNumAddOns() do
mem = 0
cpu = 0
name, _, _, enabled, _, _, security = GetAddOnInfo(i)
if(name and enabled and IsAddOnLoaded(i)) then
mem = GetAddOnMemoryUsage(i)
cpu = GetAddOnCPUUsage(i)
ainfo[#ainfo+1] = {
['name'] = name,
['mem'] = mem,
['cpu'] = cpu,
}
end
end
end
local function OnTooltip(tip)
if(GetTime() > lastainfo) then
updateainfo()
lastainfo = GetTime() + ainfodelay
end
local c,d,l,f = compute()
tip:AddLine(childName)
local col = "00ff00"
if(db['expert']) then
tip:AddLine(addon:colorize(d,col))
else
tip:AddLine(addon:colorize(format("%s, %s.%s",L[date("%A")],date("%d"),L[date("%B")]),col))
end
col = toggleColor(col,"ffffff","00ff00")
if(db["12-Hours"]) then
tip:AddDoubleLine(addon:colorize(L['Time'],col),addon:colorize(date("%I:%M:%S"),toggleColor(col,"ffffff","00ff00")))
else
tip:AddDoubleLine(addon:colorize(L['Time'],col),addon:colorize(date("%H:%M:%S"),toggleColor(col,"ffffff","00ff00")))
end
col = toggleColor(col,"ffffff","00ff00")
tip:AddDoubleLine(addon:colorize(L['Latency'],col),addon:colorize(format("%s ms",select(3,GetNetStats())),toggleColor(col,"ffffff","00ff00")))
col = toggleColor(col,"ffffff","00ff00")
tip:AddDoubleLine(addon:colorize(L['FPS'],col),addon:colorize(format("%i fps",floor(GetFramerate())),toggleColor(col,"ffffff","00ff00")))
col = toggleColor(col,"ffffff","00ff00")
tip:AddDoubleLine(addon:colorize(L['AddOn Memory'],col),addon:colorize(format("%i MB",floor(collectgarbage('count')/1024)),toggleColor(col,"ffffff","00ff00")))
tip:AddLine(" ")
tip:AddLine(L["AddOns"])
sort(ainfo,sortTab)
local sum,cnt = 0,0
for _, v in ipairs(ainfo) do
if(cnt<9) then
tip:AddDoubleLine(addon:colorize(v['name'],"00ffff"),format(L["%i kB"],v['mem']))
cnt = cnt + 1
end
sum = sum + v['mem']
end
tip:AddDoubleLine(L['total'],format(L["%i kB"],format("%.2f",sum)))
local pCPU = C_CVar.GetCVarBool("scriptProfile")
if(pCPU) then
tip:AddLine(" ")
tip:AddLine(L["CPU"])
sort(ainfo,sortTab2)
sum,cnt = 0,0
for _, v in ipairs(ainfo) do
if(cnt<5) then
tip:AddDoubleLine(addon:colorize(v['name'],"00ffff"),format(L["%i ms"],v['cpu']))
cnt = cnt + 1
end
sum = sum + v['cpu']
end
tip:AddDoubleLine(L['total'],format(L["%i ms"],format("%.2f",sum)))
end
tip:AddLine(" ")
tip:AddLine(L['HINT4'])
tip:AddLine(L["garbagecollect"])
tip:AddLine(L['HINT3'])
tip:AddLine(L['HINT5'])
end
local function OnText(message)
addon:OnText(childName, message)
end
local function OnButton(self, button)
if(button == 'RightButton') then
InterfaceOptionsFrame_OpenToCategory(childName)
InterfaceOptionsFrame_OpenToCategory(childName) -- twice needed
elseif(button == 'MiddleButton') then
local pCPU = C_CVar.GetCVarBool("scriptProfile")
if(pCPU) then
pCPU = 0
else
pCPU = 1
end
C_CVar.SetCVar("scriptProfile", pCPU)
addon:Print(L["CPU setting changed. Please Reload UI."])
updateainfo()
else
if(IsShiftKeyDown()) then
local db = addon['db']['global']['ldbicons']
db[childName]['hide'] = not db[childName]['hide']
if(ldbicon) then
if(db[childName]['hide']) then
ldbicon:Hide(childName)
else
ldbicon:Show(childName)
end
end
elseif(IsControlKeyDown()) then
addon:Print(L["garbagecollected"])
collectgarbage("collect")
else
GameTimeFrame_OnClick(_G["GameTimeFrame"])
end
end
end
local function OnDataUpdate()
if(db['expert']) then
local c,d,l,f = compute()
line = db['ldbstring']
line = line:gsub("%%clock", addon:colorize(c,db['clockcolor']))
line = line:gsub("%%date", addon:colorize(d,db['datecolor']))
line = line:gsub("%%fps", addon:colorize(f,db['fpscolor']))
line = line:gsub("%%latency", addon:colorize(l,db['latencycolor']))
else
line = ''
if(db["showClock"]) then
line = format("%s%s ", line, db["12-Hours"] and date("%I:%M") or date("%H:%M"))
end
if(db["showLatency"]) then
line = format("%s%s",line,addon:colorize(format("%s ms ",select(3,GetNetStats())),"ffff00"))
end
if(db["showFPS"]) then
line = format("%s%s fps", line, floor(GetFramerate()))
end
end
if(line ~= '') then
OnText(line)
else
OnText(childName)
end
C_Timer.After(db['update'], OnDataUpdate)
end
local function getPref(pref)
if(cols[pref[#pref]]) then
local r,g,b = addon:Hex2RGB(db[pref[#pref]])
return r/255,g/255,b/255,1
end
return db[pref[#pref]]
end
local function setPref(pref,value,g,b)
db[pref[#pref]] = cols[pref[#pref]] and addon:RGB2Hex(value*255,g*255,b*255) or value
end
local function expertMode()
return not db['expert']
end
local function init()
options = addon:InitConfig(childName, true, {
['name'] = childName,
['type'] = "data source",
['OnTooltipShow'] = OnTooltip,
['OnClick'] = OnButton,
}, getPref, setPref)
db = addon['db']['profile'][childName]
db["update"] = db["update"] or 1
db["clockstring"] = db["clockstring"] or (db["12-Hours"] and "%I:%M" or "%H:%M")
db["datestring"] = db["datestring"] or "%A, %d.%B"
db["latencystring"] = db["latencystring"] or "%world ms"
db["fpsstring"] = db["fpsstring"] or "%fps "..L['FPS']
db["ldbstring"] = db["ldbstring"] or "%clock %latency %fps"
for k in pairs(cols) do
db[k] = db[k] or defcolor
end
local menu = addon:AddConfigMenu({
['name'] = L['Clock'],
['order'] = 3,
['menuGet'] = getPref,
['menuSet'] = setPref,
['disabled'] = expertMode,
}, options)
addon:AddConfigEntry(childName,"input","clockstring",L['Clock'],nil,1,nil,nil,nil,nil,menu)
addon:AddConfigEntry(childName,"color","clockcolor",L['Clock'],nil,2,nil,nil,nil,nil,menu)
addon:AddConfigEntry(childName,"description","dateinfo",L['dateinfo'],nil,100,nil,nil,nil,nil,menu)
local menu2 = addon:AddConfigMenu({
['name'] = L['Date'],
['order'] = 4,
['menuGet'] = getPref,
['menuSet'] = setPref,
['disabled'] = expertMode,
}, options)
addon:AddConfigEntry(childName,"input","datestring",L['Date'],nil,1,nil,nil,nil,nil,menu2)
addon:AddConfigEntry(childName,"color","datecolor",L['Date'],nil,2,nil,nil,nil,nil,menu2)
addon:AddConfigEntry(childName,"description","dateinfo",L['dateinfo'],nil,100,nil,nil,nil,nil,menu2)
local menu3 = addon:AddConfigMenu({
['name'] = L['Latency'],
['order'] = 5,
['menuGet'] = getPref,
['menuSet'] = setPref,
['disabled'] = expertMode,
}, options)
addon:AddConfigEntry(childName,"input","latencystring",L['Latency'],nil,1,nil,nil,nil,nil,menu3)
addon:AddConfigEntry(childName,"color","latencycolor",L['Latency'],nil,2,nil,nil,nil,nil,menu3)
addon:AddConfigEntry(childName,"description","latencyinfo",L['Latencystring'],nil,100,nil,nil,nil,nil,menu3)
local menu4 = addon:AddConfigMenu({
['name'] = L['FPS'],
['order'] = 6,
['menuGet'] = getPref,
['menuSet'] = setPref,
['disabled'] = expertMode,
}, options)
addon:AddConfigEntry(childName,"input","fpsstring",L['FPS'],nil,1,nil,nil,nil,nil,menu4)
addon:AddConfigEntry(childName,"color","fpscolor",L['FPS'],nil,2,nil,nil,nil,nil,menu4)
addon:AddConfigEntry(childName,"description","fpsinfo",L['fpsstring'],nil,100,nil,nil,nil,nil,menu4)
local menu5 = addon:AddConfigMenu({
['name'] = L['LDB'],
['order'] = 7,
['menuGet'] = getPref,
['menuSet'] = setPref,
['disabled'] = expertMode,
}, options)
addon:AddConfigEntry(childName,"input","ldbstring",L['LDB'],nil,1,nil,nil,nil,nil,menu5)
addon:AddConfigEntry(childName,"description","ldbinfo",L['ldbstring'],nil,100,nil,nil,nil,nil,menu5)
addon:AddConfigEntry(childName,"toggle","showClock",L['Show Clock'],nil,2,nil,nil,nil,nil,options)
addon:AddConfigEntry(childName,"toggle","showLatency",L['Show Latency'],nil,3,nil,nil,nil,nil,options)
addon:AddConfigEntry(childName,"toggle","showFPS",L['Show FPS'],nil,4,nil,nil,nil,nil,options)
addon:AddConfigEntry(childName,"toggle","12-Hours",L['12-Hours'],nil,5,nil,nil,nil,nil,options)
addon:AddConfigEntry(childName,"toggle","expert",L['Expert'],nil,6,nil,nil,nil,nil,options)
C_Timer.After(db['update'], OnDataUpdate)
end
addon:startup(name, childName, init, true, defaults)