Files
ClassicAPI/src/embed/AddOns/ClassicAPI_DebugTools/Profiler/FramesTab.lua
T
8e721237f6 feat(addons): zhCN localization for ClassicAPI_DebugTools and 6 missing globals fix (#141)
* feat(debugtools): add zhCN localization for ClassicAPI_DebugTools

* fix(locales): add 6 missing global strings to zhCN

* feat(debugtools): clean up slash command dispatch and add help subcommand

* fix(debugtools): use %s instead of %d in PROFILER_CALLS_FORMAT

ns.Count() always returns a string (either a formatted number or '-'),
never a number. Feeding it to %d causes format() to throw. Change the
locale template to %s so both '-', '1,234', etc. render correctly.

* fix(debugtools): repair regressions from the localization pass

- XML labels: 1.12's Button/FontString loader passes the text attribute
  verbatim to FrameScript_GetText (0x703BF0), which reads the global of
  exactly that name and falls back to the raw value. Nothing strips
  "$...$", so text="$TI_SHOW$" rendered as literal "$TI_SHOW$". Use the
  bare global name, as stock FrameXML does.
- Tabs: ns.AddTab(name, label) keeps a fixed English key for ns.ShowIn
  and a separate localized button label. Keying tabs by the translated
  name made ShowIn("Globals") return nil and error on a zhCN client.
- Frames tab state: restore visible / "shown, parent hidden" / hidden.
  The inverted branch reported a shown frame under a hidden parent as
  hidden and made the third state unreachable.
- /profiler: unknown or incomplete subcommands print the usage again
  instead of doing nothing.
- Deep Tracking / CPU Profiling buttons: split the label from ON/OFF so
  the state keeps its accent / grey colour.
- Restore the gap between owner and CPU time in the frame-stack and
  Frames-tab tooltips, and the leading space of " (replaced)".
- zhCN: ADDON_OPTIONS titles the shared Interface Options panel and the
  game-menu button, not ClassicAPI's own settings; revert to 插件设置.

---------

Co-authored-by: jilinge <jilinge@users.noreply.github.com>
Co-authored-by: Brues <5278969+brues-code@users.noreply.github.com>
2026-11-13 10:33:01 -06:00

168 lines
5.4 KiB
Lua

-- Frames tab: the frames that run scripts, with the CPU time of all their
-- scripts (CPU profiling) and the memory they allocate (deep tracking).
local _, ns = ...
local track = ns.track
local function Profiled(fn)
return function(rec)
if ns.profiling then return fn(rec) end
end
end
local function Tracked(fn)
return function(rec)
if rec.hookedUpdate or rec.hookedEvent then return fn(rec) end
end
end
local function Line(left, right)
GameTooltip:AddDoubleLine(left, right, 1, 1, 1, 1, 1, 1)
end
-- the events of a frame, most expensive first
local function SortedEvents(rec)
local list = {}
for name, stat in pairs(rec.events) do
tinsert(list, { name = name, stat = stat })
end
local key = ns.profiling and "cpu" or "alloc"
table.sort(list, function(a, b) return a.stat[key] > b.stat[key] end)
return list
end
local function Tooltip(rec)
local frame = rec.frame
GameTooltip:AddLine(rec.name)
Line(PROFILER_LABEL_TYPE, frame:GetObjectType())
Line(PROFILER_LABEL_OWNER, ns.OwnerLabel(rec.owner))
Line(PROFILER_LABEL_STATE, frame:IsVisible() and PROFILER_STATE_VISIBLE
or frame:IsShown() and PROFILER_STATE_SHOWN_HIDDEN or PROFILER_STATE_HIDDEN)
local source = frame:GetSourceLocation()
if source then
GameTooltip:AddLine(source, .6, .6, .6, true)
end
GameTooltip:AddLine(" ")
for _, script in ipairs({ "OnUpdate", "OnEvent" }) do
local fn = frame:GetScript(script)
if fn then
local text = ns.OwnerLabel(ns.Owner(fn))
if ns.profiling then
local ms, calls = GetFunctionCPUUsage(fn)
text = text .. ns.GREY .. " " .. ns.MS(ms) .. format(PROFILER_CALLS_FORMAT, ns.Count(calls))
end
Line(script, text)
end
end
if ns.profiling then
Line(PROFILER_LABEL_ALL_SCRIPTS, ns.MS(rec.cpu) .. format(PROFILER_CALLS_FORMAT, ns.Count(rec.calls)))
end
if rec.hookedUpdate or rec.hookedEvent then
Line(PROFILER_LABEL_ALLOCATED, ns.KB(rec.alloc))
end
local events = SortedEvents(rec)
if #events > 0 then
GameTooltip:AddLine(" ")
GameTooltip:AddLine(PROFILER_LABEL_EVENTS, 1, .8, 0)
for i = 1, min(#events, 12) do
local stat = events[i].stat
local text = ns.Count(stat.count) .. "x"
if ns.profiling then text = text .. " " .. ns.MS(stat.cpu) end
Line(events[i].name, text .. " " .. ns.KB(stat.alloc))
end
end
GameTooltip:AddLine(" ")
GameTooltip:AddLine(PROFILER_LABEL_RIGHT_CLICK_HINT, .6, .6, .6)
end
local tab = ns.AddTab("Frames", PROFILER_TAB_FRAMES)
tab.list = ns.CreateList(tab.frame, {
sort = 3,
columns = {
{
title = PROFILER_FRAMES_COL_FRAME,
text = function(rec) return rec.name end,
value = function(rec) return strlower(rec.name) end,
},
{
title = PROFILER_FRAMES_COL_OWNER, width = 100, justify = "LEFT",
tip = PROFILER_FRAMES_TIP_OWNER,
text = function(rec) return ns.OwnerLabel(rec.owner) end,
value = function(rec) return strlower(ns.OwnerLabel(rec.owner)) end,
},
{
title = PROFILER_FRAMES_COL_CPU_S, width = 64,
tip = PROFILER_FRAMES_TIP_CPU_S,
text = Profiled(function(rec) return ns.MS(rec.cpuRate) end),
value = Profiled(function(rec) return rec.cpuRate end),
},
{
title = PROFILER_FRAMES_COL_CALLS_S, width = 60,
tip = PROFILER_FRAMES_TIP_CALLS_S,
text = Profiled(function(rec) return ns.Count(rec.callRate) end),
value = Profiled(function(rec) return rec.callRate end),
},
{
title = PROFILER_FRAMES_COL_PER_CALL, width = 64,
tip = PROFILER_FRAMES_TIP_PER_CALL,
text = Profiled(function(rec) return rec.calls > 0 and ns.MS(rec.cpu / rec.calls) end),
value = Profiled(function(rec) return rec.calls > 0 and rec.cpu / rec.calls or nil end),
},
{
title = PROFILER_FRAMES_COL_ALLOC_S, width = 72,
tip = PROFILER_FRAMES_TIP_ALLOC_S,
text = Tracked(function(rec) return ns.KBRate(rec.allocRate) end),
value = Tracked(function(rec) return rec.allocRate end),
},
{
title = PROFILER_FRAMES_COL_CPU_TOTAL, width = 64,
tip = PROFILER_FRAMES_TIP_CPU_TOTAL,
text = Profiled(function(rec) return ns.MS(rec.cpu) end),
value = Profiled(function(rec) return rec.cpu end),
},
},
tooltip = Tooltip,
menu = function(rec, root)
root:CreateTitle(rec.name)
root:CreateButton(PROFILER_FRAMES_MENU_HIGHLIGHT, function() ns.Highlight(rec.frame) end)
root:CreateButton(PROFILER_FRAMES_MENU_INSPECT, function() ns.Inspect(rec.frame) end)
root:CreateButton(PROFILER_FRAMES_MENU_COPY_NAME, function() ns.Copy(rec.name) end)
local source = rec.frame:GetSourceLocation()
if source then
root:CreateButton(PROFILER_FRAMES_MENU_COPY_SRC, function() ns.Copy(source) end)
end
root:CreateButton(format(PROFILER_FRAMES_MENU_SHOW_OWNER, ns.OwnerLabel(rec.owner)), function()
ns.ShowIn("Frames", ns.OwnerLabel(rec.owner))
end)
end,
})
tab.list:SetAllPoints()
local function Update(self)
local entries = {}
for _, rec in pairs(track.frames) do
if rec.calls > 0 or rec.alloc > 0 then tinsert(entries, rec) end
end
self.list:SetEntries(entries)
end
tab.OnSelect = Update
tab.OnSample = Update
function tab:Status()
local text = format(PROFILER_FRAMES_STATUS, track.frameCount, #self.list.shown)
if not ns.profiling and not track.deep then
return text .. " " .. ns.GOLD .. PROFILER_FRAMES_STATUS_PROMPT
end
if track.deep then text = text .. PROFILER_FRAMES_STATUS_DEEP end
return text
end