DebugTools

This commit is contained in:
Brues
2026-05-08 10:47:46 -05:00
parent f1c0994cbf
commit ea46b3341b
6 changed files with 1627 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
-- Lua 5.0 / WoW 1.12 compatibility shims for the DebugTools backport.
if not _G then
_G = getfenv(0)
end
-- Lua 5.0 has no string.match. Approximate it with string.find.
if not string.match then
function string.match(s, pattern, init)
if s == nil then return nil end
local r1, r2, c1, c2, c3, c4, c5, c6, c7, c8, c9 =
string.find(s, pattern, init)
if r1 == nil then return nil end
if c1 ~= nil then
return c1, c2, c3, c4, c5, c6, c7, c8, c9
end
return string.sub(s, r1, r2)
end
end
-- Lua 5.0 spelled gmatch as gfind.
if not string.gmatch then
string.gmatch = string.gfind
end
-- These exist in 3.x+ but not in 1.12; stub them out so calls are harmless.
if not forceinsecure then
function forceinsecure() end
end
if not debuglocals then
function debuglocals() return "" end
end
-- Convenience: count entries in a sequence (Lua 5.0 has no # operator).
function DebugTools_TableLen(t)
if t == nil then return 0 end
return table.getn(t)
end
+823
View File
@@ -0,0 +1,823 @@
------------------------------------------------------------------------------
-- DebugTools.lua (1.12 / Lua 5.0 backport of Blizzard_DebugTools 3.3.5)
--
-- Major changes from the 3.3.5 source:
-- * Lua 5.0 has no "..." varargs and no select(); script handlers take
-- explicit (this, event, arg1..arg9) parameters from XML inline scripts.
-- * RegisterAllEvents is used when available; the curated default list is
-- a fallback if the client doesn't expose it.
-- * No GameTooltip:SetFrameStack -- a basic walk over global frames is
-- used instead.
-- * No UIPanelDialogTemplate, no parentKey, no clampedToScreen, no
-- OnSizeChanged in vanilla -- frames have fixed dimensions.
-- * No debuglocals -- shimmed to "" by Compat.lua.
------------------------------------------------------------------------------
EVENT_TRACE_EVENT_HEIGHT = 16;
EVENT_TRACE_MAX_ENTRIES = 1000;
EVENT_TRACE_NUM_BUTTONS = 28;
EVENT_TRACE_FRAME_HEIGHT = (EVENT_TRACE_NUM_BUTTONS * EVENT_TRACE_EVENT_HEIGHT) + 36;
DEBUGLOCALS_LEVEL = 4;
local _normalFontColor = { 1, .82, 0, 1 };
EVENT_TRACE_SYSTEM_TIMES = {};
EVENT_TRACE_SYSTEM_TIMES["System"] = true;
EVENT_TRACE_SYSTEM_TIMES["Elapsed"] = true;
EVENT_TRACE_EVENT_COLORS = {};
EVENT_TRACE_EVENT_COLORS["System"] = _normalFontColor;
EVENT_TRACE_EVENT_COLORS["Elapsed"] = { .6, .6, .6, 1 };
-- Default events to register when /etrace start is issued. The user can
-- /etrace add additional events. Vanilla doesn't expose RegisterAllEvents.
local DEFAULT_TRACED_EVENTS = {
"ADDON_LOADED", "VARIABLES_LOADED", "PLAYER_LOGIN", "PLAYER_LOGOUT",
"PLAYER_ENTERING_WORLD", "PLAYER_LEAVING_WORLD",
"PLAYER_TARGET_CHANGED", "PLAYER_REGEN_DISABLED", "PLAYER_REGEN_ENABLED",
"PLAYER_DEAD", "PLAYER_ALIVE", "PLAYER_UNGHOST", "PLAYER_LEVEL_UP",
"PLAYER_AURAS_CHANGED", "PLAYER_XP_UPDATE",
"UNIT_HEALTH", "UNIT_MANA", "UNIT_MAXHEALTH", "UNIT_MAXMANA",
"UNIT_LEVEL", "UNIT_FACTION", "UNIT_NAME_UPDATE",
"UNIT_PORTRAIT_UPDATE", "UNIT_INVENTORY_CHANGED",
"BAG_UPDATE", "BAG_UPDATE_COOLDOWN",
"ITEM_LOCK_CHANGED", "ITEM_PUSH",
"QUEST_LOG_UPDATE", "QUEST_ACCEPTED", "QUEST_FINISHED",
"QUEST_PROGRESS", "QUEST_DETAIL", "QUEST_COMPLETE", "QUEST_GREETING",
"MERCHANT_SHOW", "MERCHANT_CLOSED",
"TRADE_SHOW", "TRADE_CLOSED", "TRADE_REQUEST", "TRADE_REQUEST_CANCEL",
"LOOT_OPENED", "LOOT_CLOSED",
"BANKFRAME_OPENED", "BANKFRAME_CLOSED",
"MAIL_SHOW", "MAIL_CLOSED", "MAIL_INBOX_UPDATE",
"AUCTION_HOUSE_SHOW", "AUCTION_HOUSE_CLOSED",
"TAXIMAP_OPENED", "TAXIMAP_CLOSED",
"PARTY_MEMBERS_CHANGED", "PARTY_LEADER_CHANGED",
"PARTY_LOOT_METHOD_CHANGED", "RAID_ROSTER_UPDATE",
"CHAT_MSG_SAY", "CHAT_MSG_PARTY", "CHAT_MSG_RAID", "CHAT_MSG_RAID_LEADER",
"CHAT_MSG_GUILD", "CHAT_MSG_OFFICER",
"CHAT_MSG_WHISPER", "CHAT_MSG_WHISPER_INFORM",
"CHAT_MSG_SYSTEM", "CHAT_MSG_YELL", "CHAT_MSG_CHANNEL",
"CHAT_MSG_LOOT", "CHAT_MSG_MONEY",
"SPELLCAST_START", "SPELLCAST_STOP", "SPELLCAST_FAILED",
"SPELLCAST_INTERRUPTED", "SPELLCAST_DELAYED",
"SPELLCAST_CHANNEL_START", "SPELLCAST_CHANNEL_UPDATE",
"SPELLCAST_CHANNEL_STOP",
"ACTIONBAR_SLOT_CHANGED", "ACTIONBAR_PAGE_CHANGED",
"ACTIONBAR_UPDATE_USABLE", "ACTIONBAR_UPDATE_COOLDOWN",
"UI_ERROR_MESSAGE", "UI_INFO_MESSAGE",
"UPDATE_FACTION", "SKILL_LINES_CHANGED", "SPELLS_CHANGED",
"TIME_PLAYED_MSG",
"TRADE_SKILL_SHOW", "TRADE_SKILL_CLOSE",
"CRAFT_SHOW", "CRAFT_CLOSE",
"GOSSIP_SHOW", "GOSSIP_CLOSED",
"FRIENDLIST_UPDATE", "WHO_LIST_UPDATE", "GUILD_ROSTER_UPDATE",
};
-- Vanilla has no Frame:IsMouseOver(); do it ourselves from cursor + bounds.
local function DebugTools_IsMouseOver(frame)
if (not frame) then return false; end
local visOK, visible = pcall(frame.IsVisible, frame);
if (not visOK or not visible) then return false; end
local sOK, scale = pcall(frame.GetEffectiveScale, frame);
if (not sOK or not scale or scale == 0) then return false; end
local cx, cy = GetCursorPosition();
cx = cx / scale;
cy = cy / scale;
local lOK, left = pcall(frame.GetLeft, frame);
local rOK, right = pcall(frame.GetRight, frame);
local tOK, top = pcall(frame.GetTop, frame);
local bOK, bottom = pcall(frame.GetBottom, frame);
if (not (lOK and rOK and tOK and bOK)) then return false; end
if (not (left and right and top and bottom)) then return false; end
return cx >= left and cx <= right and cy >= bottom and cy <= top;
end
local _EventTraceFrame;
_framesSinceLast = 0;
_timeSinceLast = 0;
local _timer = CreateFrame("Frame");
_timer:SetScript("OnUpdate", function()
_framesSinceLast = _framesSinceLast + 1;
_timeSinceLast = _timeSinceLast + (arg1 or 0);
end);
------------------------------------------------------------------------------
-- EventTraceFrame
------------------------------------------------------------------------------
function EventTraceFrame_OnLoad(self)
self.buttons = {};
self.events = {};
self.times = {};
self.rawtimes = {};
self.args = { {}, {}, {}, {}, {}, {}, {}, {}, {} };
self.ignoredEvents = {};
self.tracedEvents = {}; -- set of registered event names
self.lastIndex = 0;
self.visibleButtons = EVENT_TRACE_NUM_BUTTONS;
_EventTraceFrame = self;
self:EnableMouse(true);
self:EnableMouseWheel(true);
self:SetScript("OnMouseWheel", function() EventTraceFrame_OnMouseWheel(this, arg1); end);
-- Pre-create the fixed list of buttons.
for i = 1, EVENT_TRACE_NUM_BUTTONS do
local button = CreateFrame("Button", "EventTraceFrameButton" .. i,
self, "EventTraceEventTemplate");
button:SetPoint("BOTTOMLEFT", 12, (EVENT_TRACE_EVENT_HEIGHT * (i - 1)) + 12);
button:SetPoint("RIGHT", -28, 0);
table.insert(self.buttons, button);
button:Show();
end
end
function EventTraceFrame_OnEvent(self, event, a1, a2, a3, a4, a5, a6, a7, a8, a9)
if (self.ignoredEvents[event]) then return; end
if (not self.ignoreElapsed and _framesSinceLast ~= 0) then
self.ignoreElapsed = true;
EventTraceFrame_OnEvent(self, "On Update");
self.ignoreElapsed = nil;
end
local nextIndex = self.lastIndex + 1;
if (nextIndex > EVENT_TRACE_MAX_ENTRIES) then
local staleIndex = nextIndex - EVENT_TRACE_MAX_ENTRIES;
self.events[staleIndex] = nil;
self.times[staleIndex] = nil;
self.rawtimes[staleIndex] = nil;
for k, v in pairs(self.args) do
self.args[k][staleIndex] = nil;
end
end
if (string.find(event, "Begin Capture", 1, true) or
string.find(event, "End Capture", 1, true)) then
self.times[nextIndex] = "System";
if (self.eventsToCapture) then
self.events[nextIndex] = string.format("%s (%s events)", event,
tostring(self.eventsToCapture));
else
self.events[nextIndex] = event;
end
elseif (event == "On Update") then
self.times[nextIndex] = "Elapsed";
self.events[nextIndex] = string.format(
string.format("%.3f sec", _timeSinceLast) .. " - %d frame(s)",
_framesSinceLast);
_timeSinceLast = 0;
_framesSinceLast = 0;
else
self.events[nextIndex] = event;
local seconds = GetTime();
local minutes = math.floor(math.floor(seconds) / 60);
local hours = math.floor(minutes / 60);
seconds = seconds - 60 * minutes;
minutes = minutes - 60 * hours;
hours = math.mod(hours, 1000);
self.times[nextIndex] = string.format("%.2d:%.2d:%06.3f", hours, minutes, seconds);
local packed = { a1, a2, a3, a4, a5, a6, a7, a8, a9 };
local numArgs = 0;
for i = 9, 1, -1 do
if (packed[i] ~= nil) then
numArgs = i;
break;
end
end
for i = 1, numArgs do
if (not self.args[i]) then self.args[i] = {}; end
self.args[i][nextIndex] = packed[i];
end
if (self.eventsToCapture) then
self.eventsToCapture = self.eventsToCapture - 1;
end
end
self.rawtimes[nextIndex] = GetTime();
self.lastIndex = nextIndex;
EventTraceFrame_Update();
if (self.eventsToCapture and self.eventsToCapture <= 0) then
self.eventsToCapture = nil;
EventTraceFrame_StopEventCapture();
end
end
function EventTraceFrame_OnUpdate(self, elapsed)
EventTraceFrame_Update();
end
function EventTraceFrame_Update()
if (not _EventTraceFrame or not _EventTraceFrame.buttons) then
-- Slider OnValueChanged can fire during slider OnLoad, which happens
-- before EventTraceFrame_OnLoad in 1.12 XML load order.
return;
end
local scrollBar = getglobal("EventTraceFrameScroll");
if (not scrollBar) then return; end
local scrollBarValue = scrollBar:GetValue();
local minValue, maxValue = scrollBar:GetMinMaxValues();
local firstID = math.max(1, _EventTraceFrame.lastIndex - EVENT_TRACE_MAX_ENTRIES + 1);
local lastID = _EventTraceFrame.lastIndex or 1;
if (firstID >= lastID) then
scrollBar:SetMinMaxValues(firstID - 1, lastID);
else
scrollBar:SetMinMaxValues(firstID, lastID);
end
if (scrollBarValue < firstID) then
scrollBar:SetValue(firstID);
scrollBarValue = firstID;
end
if (scrollBarValue < 1) then
scrollBarValue = 1;
elseif (not _EventTraceFrame.selectedEvent) then
if (scrollBarValue == maxValue) then
scrollBar:SetValue(_EventTraceFrame.lastIndex);
end
end
for i = 1, _EventTraceFrame.visibleButtons do
local button = _EventTraceFrame.buttons[i];
if (button) then
local index = scrollBarValue - (i - 1);
local event = _EventTraceFrame.events[index];
if (event) then
local timeString = _EventTraceFrame.times[index];
button.index = index;
local timeFS = getglobal(button:GetName() .. "Time");
local eventFS = getglobal(button:GetName() .. "Event");
timeFS:SetText(timeString);
eventFS:SetText(event);
local color = EVENT_TRACE_EVENT_COLORS[event]
or EVENT_TRACE_EVENT_COLORS[timeString];
if (color) then
timeFS:SetTextColor(unpack(color));
eventFS:SetTextColor(unpack(color));
else
timeFS:SetTextColor(1, 1, 1, 1);
eventFS:SetTextColor(1, 1, 1, 1);
end
button:Show();
if (_EventTraceFrame.selectedEvent) then
if (index == _EventTraceFrame.selectedEvent) then
EventTraceFrameEvent_DisplayTooltip(button);
button:GetHighlightTexture():SetVertexColor(.15, .25, 1, .35);
button:LockHighlight();
button.wasSelected = true;
elseif (button.wasSelected) then
button.wasSelected = nil;
button:GetHighlightTexture():SetVertexColor(.8, .8, 1, .15);
button:UnlockHighlight();
end
else
if (button.wasSelected) then
button.wasSelected = nil;
button:GetHighlightTexture():SetVertexColor(.8, .8, 1, .15);
button:UnlockHighlight();
end
if (DebugTools_IsMouseOver(button)) then
EventTraceFrameEvent_OnEnter(button);
end
end
else
button.index = index;
button:Hide();
end
end
end
end
function EventTraceFrame_StartEventCapture()
if (_EventTraceFrame.started) then return; end
_EventTraceFrame.started = true;
_framesSinceLast = 0;
_timeSinceLast = 0;
-- 1.12 does expose RegisterAllEvents; fall back to the curated list only
-- if it's missing on this client.
if (_EventTraceFrame.RegisterAllEvents) then
_EventTraceFrame:RegisterAllEvents();
else
for _, ev in ipairs(DEFAULT_TRACED_EVENTS) do
_EventTraceFrame.tracedEvents[ev] = true;
end
for ev, _ in pairs(_EventTraceFrame.tracedEvents) do
pcall(function() _EventTraceFrame:RegisterEvent(ev); end);
end
end
EventTraceFrame_OnEvent(_EventTraceFrame, "Begin Capture");
end
function EventTraceFrame_StopEventCapture()
if (not _EventTraceFrame.started) then return; end
_EventTraceFrame.started = false;
_framesSinceLast = 0;
_timeSinceLast = 0;
_EventTraceFrame:UnregisterAllEvents();
EventTraceFrame_OnEvent(_EventTraceFrame, "End Capture");
end
local function _ChatPrint(msg)
DEFAULT_CHAT_FRAME:AddMessage("|cff88ff88EventTrace:|r " .. msg);
end
function EventTraceFrame_HandleSlashCmd(msg)
if (msg == nil) then msg = ""; end
msg = string.lower(msg);
-- /etrace add EVENT
local _, _, addEvent = string.find(msg, "^add%s+(%S+)");
if (addEvent) then
addEvent = string.upper(addEvent);
_EventTraceFrame.tracedEvents[addEvent] = true;
if (_EventTraceFrame.started) then
_EventTraceFrame:RegisterEvent(addEvent);
end
_ChatPrint("added " .. addEvent);
return;
end
-- /etrace remove EVENT
local _, _, rmEvent = string.find(msg, "^remove%s+(%S+)");
if (rmEvent) then
rmEvent = string.upper(rmEvent);
_EventTraceFrame.tracedEvents[rmEvent] = nil;
if (_EventTraceFrame.started) then
_EventTraceFrame:UnregisterEvent(rmEvent);
end
_ChatPrint("removed " .. rmEvent);
return;
end
-- /etrace ignore EVENT
local _, _, igEvent = string.find(msg, "^ignore%s+(%S+)");
if (igEvent) then
igEvent = string.upper(igEvent);
_EventTraceFrame.ignoredEvents[igEvent] = true;
_ChatPrint("ignoring " .. igEvent);
return;
end
if (msg == "start") then
EventTraceFrame_StartEventCapture();
elseif (msg == "stop") then
EventTraceFrame_StopEventCapture();
elseif (msg == "clear") then
_EventTraceFrame.events = {};
_EventTraceFrame.times = {};
_EventTraceFrame.rawtimes = {};
_EventTraceFrame.args = { {}, {}, {}, {}, {}, {}, {}, {}, {} };
_EventTraceFrame.lastIndex = 0;
EventTraceFrame_Update();
elseif (tonumber(msg) and tonumber(msg) > 0) then
if (not _EventTraceFrame.started) then
_EventTraceFrame.eventsToCapture = tonumber(msg);
EventTraceFrame_StartEventCapture();
end
elseif (msg == "" or msg == "toggle") then
if (not _EventTraceFrame:IsShown()) then
_EventTraceFrame:Show();
if (_EventTraceFrame.started == nil) then
EventTraceFrame_StartEventCapture();
end
else
_EventTraceFrame:Hide();
end
else
_ChatPrint("usage:");
_ChatPrint(" /etrace -- toggle window (auto-start first time)");
_ChatPrint(" /etrace start -- begin event capture");
_ChatPrint(" /etrace stop -- end event capture");
_ChatPrint(" /etrace <N> -- capture next N events");
_ChatPrint(" /etrace add <EV> -- register additional event");
_ChatPrint(" /etrace remove <EV>-- unregister event");
_ChatPrint(" /etrace ignore <EV>-- hide event from display");
_ChatPrint(" /etrace clear -- clear log");
end
end
function EventTraceFrame_OnMouseWheel(self, delta)
local scrollBar = getglobal("EventTraceFrameScroll");
local minVal, maxVal = scrollBar:GetMinMaxValues();
local currentValue = scrollBar:GetValue();
local newValue = currentValue - (delta * 3);
if (newValue < minVal) then newValue = minVal; end
if (newValue > maxVal) then newValue = maxVal; end
if (newValue ~= currentValue) then
scrollBar:SetValue(newValue);
end
end
local TIME_ENTRY_FORMAT = "Time:";
local DETAILS_ENTRY_FORMAT = "Details:";
local ARGUMENT_ENTRY_FORMAT = "arg %d:";
local function EventTrace_FormatArgValue(val)
local t = type(val);
if (t == "string") then
return string.format('"%s"', val);
elseif (t == "number") then
return tostring(val);
elseif (t == "boolean") then
return string.format('|cffaaaaff%s|r', tostring(val));
elseif (t == "table" or t == "function" or t == "userdata") then
return string.format('|cffffaaaa%s|r', tostring(val));
end
return tostring(val);
end
function EventTraceFrameEvent_DisplayTooltip(eventButton)
local index = eventButton.index;
if (not index) then return; end
local tooltip = EventTraceTooltip;
tooltip:SetOwner(eventButton, "ANCHOR_NONE");
tooltip:SetPoint("TOPLEFT", eventButton, "TOPRIGHT", 24, 2);
local timeString = _EventTraceFrame.times[index];
if (EVENT_TRACE_SYSTEM_TIMES[timeString]) then
tooltip:AddLine(timeString, 1, 1, 1);
tooltip:AddDoubleLine(TIME_ENTRY_FORMAT,
tostring(_EventTraceFrame.rawtimes[index]),
1, .82, 0, 1, 1, 1);
tooltip:AddDoubleLine(DETAILS_ENTRY_FORMAT,
_EventTraceFrame.events[index],
1, .82, 0, 1, 1, 1);
else
tooltip:AddLine(_EventTraceFrame.events[index], 1, 1, 1);
tooltip:AddDoubleLine(TIME_ENTRY_FORMAT,
tostring(_EventTraceFrame.rawtimes[index]),
1, .82, 0, 1, 1, 1);
for k, v in ipairs(_EventTraceFrame.args) do
if (v[index] ~= nil) then
tooltip:AddDoubleLine(string.format(ARGUMENT_ENTRY_FORMAT, k),
EventTrace_FormatArgValue(v[index]),
1, .82, 0, 1, 1, 1);
end
end
end
tooltip:Show();
end
function EventTraceFrameEvent_OnEnter(self)
if (_EventTraceFrame.selectedEvent) then return; end
EventTraceFrameEvent_DisplayTooltip(self);
end
function EventTraceFrameEvent_OnLeave(self)
if (not _EventTraceFrame.selectedEvent) then
EventTraceTooltip:Hide();
end
end
function EventTraceFrameEvent_OnClick(self)
if (_EventTraceFrame.selectedEvent == self.index) then
_EventTraceFrame.selectedEvent = nil;
else
_EventTraceFrame.selectedEvent = self.index;
end
EventTraceFrame_Update();
end
------------------------------------------------------------------------------
-- ScriptErrorsFrame
------------------------------------------------------------------------------
local ERROR_FORMAT = "|cffffd200Message:|r|cffffffff %s|r\n"
.. "|cffffd200Time:|r|cffffffff %s|r\n"
.. "|cffffd200Count:|r|cffffffff %s|r\n"
.. "|cffffd200Stack:|r|cffffffff %s|r";
local INDEX_ORDER_FORMAT = "%d / %d";
local _ScriptErrorsFrame;
local _previousErrorHandler;
function ScriptErrorsFrame_OnLoad(self)
local titleFS = getglobal(self:GetName() .. "Title");
if (titleFS) then
titleFS:SetText("Lua Error");
end
self:RegisterForDrag("LeftButton");
self.seen = {};
self.order = {};
self.count = {};
self.messages = {};
self.times = {};
_ScriptErrorsFrame = self;
end
function ScriptErrorsFrame_OnShow(self)
ScriptErrorsFrame_Update();
end
function ScriptErrorsFrame_OnError(message, keepHidden)
if (message == nil) then return; end
local stack = debugstack(DEBUGLOCALS_LEVEL) or "";
local messageStack = message .. stack;
if (_ScriptErrorsFrame) then
local index = _ScriptErrorsFrame.seen[messageStack];
if (index) then
_ScriptErrorsFrame.count[index] = _ScriptErrorsFrame.count[index] + 1;
_ScriptErrorsFrame.messages[index] = message;
_ScriptErrorsFrame.times[index] = date();
else
table.insert(_ScriptErrorsFrame.order, stack);
index = table.getn(_ScriptErrorsFrame.order);
_ScriptErrorsFrame.count[index] = 1;
_ScriptErrorsFrame.messages[index] = message;
_ScriptErrorsFrame.times[index] = date();
_ScriptErrorsFrame.seen[messageStack] = index;
end
if (not _ScriptErrorsFrame:IsShown() and not keepHidden) then
_ScriptErrorsFrame.index = index;
_ScriptErrorsFrame:Show();
else
ScriptErrorsFrame_Update();
end
end
-- Chain through to whatever previous handler existed (e.g. BugGrabber)
if (_previousErrorHandler and _previousErrorHandler ~= ScriptErrorsFrame_OnError) then
pcall(_previousErrorHandler, message);
end
end
function ScriptErrorsFrame_Update()
local editBox = getglobal("ScriptErrorsFrameScrollFrameText");
local index = _ScriptErrorsFrame.index;
if (not index or not _ScriptErrorsFrame.order[index]) then
index = table.getn(_ScriptErrorsFrame.order);
_ScriptErrorsFrame.index = index;
end
if (index == 0) then
editBox:SetText("");
ScriptErrorsFrame_UpdateButtons();
return;
end
local text = string.format(ERROR_FORMAT,
_ScriptErrorsFrame.messages[index],
_ScriptErrorsFrame.times[index],
_ScriptErrorsFrame.count[index],
_ScriptErrorsFrame.order[index]);
editBox:SetText(text);
editBox:HighlightText(0);
editBox:SetCursorPosition(0);
local scrollFrame = getglobal("ScriptErrorsFrameScrollFrame");
if (scrollFrame and scrollFrame.SetVerticalScroll) then
scrollFrame:SetVerticalScroll(0);
end
ScriptErrorsFrame_UpdateButtons();
end
function ScriptErrorsFrame_UpdateButtons()
local index = _ScriptErrorsFrame.index;
local numErrors = table.getn(_ScriptErrorsFrame.order);
local nextBtn = getglobal("ScriptErrorsFrameNext");
local prevBtn = getglobal("ScriptErrorsFramePrevious");
local idxLbl = getglobal("ScriptErrorsFrameIndexLabel");
if (index == 0 or numErrors == 0) then
nextBtn:Disable();
prevBtn:Disable();
else
if (numErrors == 1) then
nextBtn:Disable();
prevBtn:Disable();
elseif (index == 1) then
nextBtn:Enable();
prevBtn:Disable();
elseif (index == numErrors) then
nextBtn:Disable();
prevBtn:Enable();
else
nextBtn:Enable();
prevBtn:Enable();
end
end
idxLbl:SetText(string.format(INDEX_ORDER_FORMAT, index, numErrors));
end
function ScriptErrorsFrameButton_OnClick(self)
local id = self:GetID();
if (id == 1) then
_ScriptErrorsFrame.index = _ScriptErrorsFrame.index + 1;
else
_ScriptErrorsFrame.index = _ScriptErrorsFrame.index - 1;
end
ScriptErrorsFrame_Update();
end
function ScriptErrorsFrame_TitleButton_OnDragStart()
_ScriptErrorsFrame.moving = true;
_ScriptErrorsFrame:StartMoving();
end
function ScriptErrorsFrame_TitleButton_OnDragStop()
_ScriptErrorsFrame.moving = nil;
_ScriptErrorsFrame:StopMovingOrSizing();
end
function ScriptErrorsFrame_HandleSlashCmd(msg)
if (msg == nil) then msg = ""; end
msg = string.lower(msg);
if (msg == "on" or msg == "install") then
_previousErrorHandler = geterrorhandler();
seterrorhandler(ScriptErrorsFrame_OnError);
_ChatPrint("Lua error capture installed.");
elseif (msg == "off" or msg == "uninstall") then
if (_previousErrorHandler) then
seterrorhandler(_previousErrorHandler);
end
_ChatPrint("Lua error capture uninstalled.");
elseif (msg == "test") then
ScriptErrorsFrame_OnError("test error from /luaerrors test");
elseif (msg == "clear") then
_ScriptErrorsFrame.seen = {};
_ScriptErrorsFrame.order = {};
_ScriptErrorsFrame.count = {};
_ScriptErrorsFrame.messages = {};
_ScriptErrorsFrame.times = {};
_ScriptErrorsFrame.index = 0;
ScriptErrorsFrame_Update();
else
if (_ScriptErrorsFrame:IsShown()) then
_ScriptErrorsFrame:Hide();
else
_ScriptErrorsFrame:Show();
end
end
end
------------------------------------------------------------------------------
-- FrameStackTooltip
------------------------------------------------------------------------------
function DebugTooltip_OnLoad(self)
self:SetFrameLevel(self:GetFrameLevel() + 2);
if (TOOLTIP_DEFAULT_COLOR and self.SetBackdropBorderColor) then
self:SetBackdropBorderColor(TOOLTIP_DEFAULT_COLOR.r,
TOOLTIP_DEFAULT_COLOR.g,
TOOLTIP_DEFAULT_COLOR.b);
end
if (TOOLTIP_DEFAULT_BACKGROUND_COLOR and self.SetBackdropColor) then
self:SetBackdropColor(TOOLTIP_DEFAULT_BACKGROUND_COLOR.r,
TOOLTIP_DEFAULT_BACKGROUND_COLOR.g,
TOOLTIP_DEFAULT_BACKGROUND_COLOR.b);
end
end
local function _IsFrameLike(v)
if (type(v) ~= "table") then return false; end
if (type(v.IsObjectType) ~= "function") then return false; end
if (type(v.GetFrameLevel) ~= "function") then return false; end
if (type(v.GetLeft) ~= "function") then return false; end
return true;
end
local _STRATA_RANK = {
BACKGROUND = 1, LOW = 2, MEDIUM = 3, HIGH = 4,
DIALOG = 5, FULLSCREEN = 6, FULLSCREEN_DIALOG = 7, TOOLTIP = 8,
};
local function _CollectFramesUnderMouse(showHidden)
local matches = {};
for k, v in pairs(getfenv(0)) do
if (_IsFrameLike(v)) then
local ok, isFrame = pcall(v.IsObjectType, v, "Frame");
if (ok and isFrame) then
local visOK, visible = pcall(v.IsVisible, v);
local mouseOver = DebugTools_IsMouseOver(v);
if (mouseOver and (showHidden or (visOK and visible))) then
table.insert(matches, { name = k, frame = v });
end
end
end
end
table.sort(matches, function(a, b)
local sa = _STRATA_RANK[a.frame:GetFrameStrata()] or 0;
local sb = _STRATA_RANK[b.frame:GetFrameStrata()] or 0;
if (sa ~= sb) then return sa > sb; end
local la = a.frame:GetFrameLevel() or 0;
local lb = b.frame:GetFrameLevel() or 0;
return la > lb;
end);
return matches;
end
local function _UpdateFrameStackTooltip(tooltip, showHidden)
tooltip:ClearLines();
tooltip:AddLine("Frame Stack", 1, 1, 1);
local matches = _CollectFramesUnderMouse(showHidden);
local count = table.getn(matches);
if (count == 0) then
tooltip:AddLine("(no frames under cursor)", .7, .7, .7);
else
local shown = 0;
for i = 1, count do
local m = matches[i];
local strata = m.frame:GetFrameStrata() or "?";
local level = m.frame:GetFrameLevel() or 0;
local label = string.format("%s |cff888888[%s:%d]|r",
m.name, strata, level);
tooltip:AddLine(label, 1, .82, 0);
shown = shown + 1;
if (shown >= 30) then
tooltip:AddLine(string.format("|cffff8888...%d more|r",
count - shown),
1, 1, 1);
break;
end
end
end
tooltip:Show();
end
function FrameStackTooltip_Toggle(showHidden)
local tooltip = FrameStackTooltip;
if (tooltip:IsVisible()) then
tooltip:Hide();
else
tooltip:SetOwner(UIParent, "ANCHOR_NONE");
tooltip:SetPoint("BOTTOMRIGHT", UIParent, "BOTTOMRIGHT", -32, 96);
tooltip.default = 1;
tooltip.showHidden = showHidden;
_UpdateFrameStackTooltip(tooltip, showHidden);
end
end
FRAMESTACK_UPDATE_TIME = .1;
local _frameStackAccum = 0;
function FrameStackTooltip_OnUpdate(self, elapsed)
_frameStackAccum = _frameStackAccum + elapsed;
if (_frameStackAccum >= FRAMESTACK_UPDATE_TIME) then
_frameStackAccum = 0;
_UpdateFrameStackTooltip(self, self.showHidden);
end
end
function FrameStackTooltip_OnShow(self)
local parent = self:GetParent() or UIParent;
local ps = parent:GetEffectiveScale();
local px, py = parent:GetCenter();
if (not px or not py) then return; end
px, py = px * ps, py * ps;
local x, y = GetCursorPosition();
self:ClearAllPoints();
if (x > px) then
if (y > py) then
self:SetPoint("BOTTOMLEFT", parent, "BOTTOMLEFT", 20, 20);
else
self:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, -20);
end
else
if (y > py) then
self:SetPoint("BOTTOMRIGHT", parent, "BOTTOMRIGHT", -20, 20);
else
self:SetPoint("TOPRIGHT", parent, "TOPRIGHT", -20, -20);
end
end
end
FrameStackTooltip_OnEnter = FrameStackTooltip_OnShow;
function FrameStackTooltip_HandleSlashCmd(msg)
if (msg == nil) then msg = ""; end
local showHidden = (string.find(msg, "hidden") ~= nil);
FrameStackTooltip_Toggle(showHidden);
end
------------------------------------------------------------------------------
-- Slash command registration
------------------------------------------------------------------------------
SLASH_ETRACE1 = "/etrace";
SlashCmdList["ETRACE"] = EventTraceFrame_HandleSlashCmd;
SLASH_FRAMESTACK1 = "/framestack";
SLASH_FRAMESTACK2 = "/fstack";
SlashCmdList["FRAMESTACK"] = FrameStackTooltip_HandleSlashCmd;
SLASH_LUAERRORS1 = "/luaerrors";
SLASH_LUAERRORS2 = "/scripterrors";
SlashCmdList["LUAERRORS"] = ScriptErrorsFrame_HandleSlashCmd;
+10
View File
@@ -0,0 +1,10 @@
## Interface: 11200
## Title: UI Debug Tools
## Notes: Tools for developing addons (backport of Blizzard_DebugTools 3.3.5 to 1.12.1 / Lua 5.0)
## Author: Blizzard Entertainment, backported
## Special Thanks: Iriel, Kirov, Esamynn
## Version: 1.0
Compat.lua
Dump.lua
DebugTools.lua
DebugTools.xml
+296
View File
@@ -0,0 +1,296 @@
<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/ ..\FrameXML\UI.xsd">
<!-- ================================================================ -->
<!-- EventTrace -->
<!-- ================================================================ -->
<Button name="EventTraceEventTemplate" virtual="true">
<Size x="280" y="16"/>
<Layers>
<Layer level="ARTWORK">
<FontString name="$parentTime" inherits="NumberFontNormalSmall" justifyH="RIGHT">
<Anchors>
<Anchor point="TOPLEFT"/>
<Anchor point="TOPRIGHT" relativePoint="TOPLEFT">
<Offset x="62" y="0"/>
</Anchor>
<Anchor point="BOTTOM"/>
</Anchors>
</FontString>
<FontString name="$parentEvent" inherits="GameFontHighlightSmall" justifyH="LEFT">
<Anchors>
<Anchor point="TOPRIGHT"/>
<Anchor point="TOPLEFT">
<Offset x="70" y="0"/>
</Anchor>
<Anchor point="BOTTOM"/>
</Anchors>
</FontString>
</Layer>
</Layers>
<Scripts>
<OnLoad>
this:GetHighlightTexture():SetAlpha(.15);
</OnLoad>
<OnEnter>EventTraceFrameEvent_OnEnter(this);</OnEnter>
<OnLeave>EventTraceFrameEvent_OnLeave(this);</OnLeave>
<OnClick>EventTraceFrameEvent_OnClick(this);</OnClick>
</Scripts>
<HighlightTexture file="Interface\Buttons\UI-Common-MouseHilight" setAllPoints="true" alphaMode="ADD">
<Color r=".8" g=".8" b="1" a="1"/>
</HighlightTexture>
</Button>
<Frame name="EventTraceFrame" parent="UIParent" movable="true" hidden="true" frameStrata="MEDIUM" toplevel="true">
<Size x="306" y="484"/>
<Anchors>
<Anchor point="LEFT">
<Offset x="64" y="0"/>
</Anchor>
</Anchors>
<Backdrop bgFile="Interface\DialogFrame\UI-DialogBox-Background" edgeFile="Interface\Tooltips\UI-Tooltip-Border" tile="true">
<BackgroundInsets>
<AbsInset left="5" right="5" top="5" bottom="5"/>
</BackgroundInsets>
<TileSize>
<AbsValue val="16"/>
</TileSize>
<EdgeSize>
<AbsValue val="16"/>
</EdgeSize>
</Backdrop>
<Layers>
<Layer level="BACKGROUND">
<Texture name="$parentTitleBG" file="Interface\QuestFrame\UI-QuestLogTitleHighlight">
<Size x="280" y="20"/>
<Anchors>
<Anchor point="TOP">
<Offset x="0" y="-6"/>
</Anchor>
</Anchors>
<Color r=".6" g=".6" b=".6" a=".4"/>
</Texture>
</Layer>
<Layer level="OVERLAY">
<FontString name="$parentTitle" inherits="GameFontNormal" text="Events">
<Anchors>
<Anchor point="TOP">
<Offset x="0" y="-8"/>
</Anchor>
</Anchors>
</FontString>
</Layer>
</Layers>
<Frames>
<Button name="$parentCloseButton" inherits="UIPanelCloseButton">
<Anchors>
<Anchor point="TOPRIGHT">
<Offset x="-2" y="-2"/>
</Anchor>
</Anchors>
</Button>
<Frame name="$parentTitleButton">
<Size x="280" y="20"/>
<Anchors>
<Anchor point="TOP">
<Offset x="0" y="-6"/>
</Anchor>
</Anchors>
<Scripts>
<OnLoad>
this:RegisterForDrag("LeftButton");
</OnLoad>
<OnDragStart>
EventTraceFrame.moving = true;
EventTraceFrame:StartMoving();
</OnDragStart>
<OnDragStop>
EventTraceFrame.moving = nil;
EventTraceFrame:StopMovingOrSizing();
</OnDragStop>
</Scripts>
</Frame>
<Slider name="$parentScroll" orientation="VERTICAL">
<Size x="16" y="0"/>
<Anchors>
<Anchor point="TOPRIGHT">
<Offset x="-7" y="-28"/>
</Anchor>
<Anchor point="BOTTOMRIGHT">
<Offset x="-7" y="10"/>
</Anchor>
</Anchors>
<Layers>
<Layer level="BACKGROUND">
<Texture name="$parentBG" setAllPoints="true">
<Color r=".8" g=".8" b="1" a="0.1"/>
</Texture>
</Layer>
</Layers>
<Scripts>
<OnLoad>
this:SetFrameLevel(this:GetFrameLevel() + 1);
this:SetMinMaxValues(0, 1);
this:SetValue(0);
this:SetValueStep(1);
</OnLoad>
<OnValueChanged>
EventTraceFrame_Update();
</OnValueChanged>
</Scripts>
<ThumbTexture name="$parentThumb" file="Interface\Buttons\UI-ScrollBar-Knob">
<Size x="16" y="16"/>
<TexCoords left="0.25" right="0.75" top="0.25" bottom="0.75"/>
</ThumbTexture>
</Slider>
</Frames>
<Scripts>
<OnLoad>EventTraceFrame_OnLoad(this);</OnLoad>
<OnEvent>EventTraceFrame_OnEvent(this, event, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);</OnEvent>
<OnUpdate>EventTraceFrame_OnUpdate(this, arg1);</OnUpdate>
</Scripts>
</Frame>
<!-- ================================================================ -->
<!-- ScriptErrorsFrame -->
<!-- ================================================================ -->
<Frame name="ScriptErrorsFrame" parent="UIParent" movable="true" hidden="true" toplevel="true" frameStrata="DIALOG" enableMouse="true">
<Size x="384" y="260"/>
<Anchors>
<Anchor point="CENTER"/>
</Anchors>
<Backdrop bgFile="Interface\DialogFrame\UI-DialogBox-Background" edgeFile="Interface\DialogFrame\UI-DialogBox-Border" tile="true">
<BackgroundInsets>
<AbsInset left="11" right="12" top="12" bottom="11"/>
</BackgroundInsets>
<TileSize>
<AbsValue val="32"/>
</TileSize>
<EdgeSize>
<AbsValue val="32"/>
</EdgeSize>
</Backdrop>
<Layers>
<Layer level="BACKGROUND">
<Texture name="$parentTitleBG" file="Interface\DialogFrame\UI-DialogBox-Header">
<Size x="256" y="64"/>
<Anchors>
<Anchor point="TOP">
<Offset x="0" y="12"/>
</Anchor>
</Anchors>
</Texture>
</Layer>
<Layer level="ARTWORK">
<FontString name="$parentTitle" inherits="GameFontNormal" text="Lua Error">
<Anchors>
<Anchor point="TOP">
<Offset x="0" y="-2"/>
</Anchor>
</Anchors>
</FontString>
<FontString name="$parentIndexLabel" inherits="GameFontNormal" justifyH="CENTER">
<Size x="70" y="16"/>
<Anchors>
<Anchor point="BOTTOM">
<Offset x="0" y="20"/>
</Anchor>
</Anchors>
</FontString>
</Layer>
</Layers>
<Frames>
<Frame name="$parentTitleButton">
<Size x="256" y="20"/>
<Anchors>
<Anchor point="TOP">
<Offset x="0" y="0"/>
</Anchor>
</Anchors>
<Scripts>
<OnLoad>
this:RegisterForDrag("LeftButton");
</OnLoad>
<OnDragStart>ScriptErrorsFrame_TitleButton_OnDragStart();</OnDragStart>
<OnDragStop>ScriptErrorsFrame_TitleButton_OnDragStop();</OnDragStop>
</Scripts>
</Frame>
<ScrollFrame name="$parentScrollFrame" inherits="UIPanelScrollFrameTemplate">
<Size x="343" y="170"/>
<Anchors>
<Anchor point="TOPLEFT">
<Offset x="16" y="-32"/>
</Anchor>
</Anchors>
<ScrollChild>
<EditBox name="$parentText" multiLine="true" letters="4000" autoFocus="false">
<Size x="343" y="170"/>
<Scripts>
<OnEscapePressed>this:ClearFocus();</OnEscapePressed>
<OnEditFocusGained>this:HighlightText(0);</OnEditFocusGained>
</Scripts>
<FontString inherits="GameFontHighlightSmall"/>
</EditBox>
</ScrollChild>
</ScrollFrame>
<Button name="$parentPrevious" inherits="UIPanelButtonTemplate" text="Previous" id="2">
<Size x="96" y="24"/>
<Anchors>
<Anchor point="BOTTOMLEFT">
<Offset x="16" y="16"/>
</Anchor>
</Anchors>
<Scripts>
<OnClick>ScriptErrorsFrameButton_OnClick(this);</OnClick>
</Scripts>
</Button>
<Button name="$parentNext" inherits="UIPanelButtonTemplate" text="Next" id="1">
<Size x="96" y="24"/>
<Anchors>
<Anchor point="BOTTOMLEFT">
<Offset x="116" y="16"/>
</Anchor>
</Anchors>
<Scripts>
<OnClick>ScriptErrorsFrameButton_OnClick(this);</OnClick>
</Scripts>
</Button>
<Button name="$parentClose" inherits="UIPanelButtonTemplate" text="Close">
<Size x="96" y="24"/>
<Anchors>
<Anchor point="BOTTOMRIGHT">
<Offset x="-16" y="16"/>
</Anchor>
</Anchors>
<Scripts>
<OnClick>ScriptErrorsFrame:Hide();</OnClick>
</Scripts>
</Button>
</Frames>
<Scripts>
<OnLoad>ScriptErrorsFrame_OnLoad(this);</OnLoad>
<OnShow>ScriptErrorsFrame_OnShow(this);</OnShow>
</Scripts>
</Frame>
<!-- ================================================================ -->
<!-- Tooltips -->
<!-- ================================================================ -->
<GameTooltip name="FrameStackTooltip" frameStrata="TOOLTIP" hidden="true" parent="UIParent" inherits="GameTooltipTemplate">
<Scripts>
<OnLoad>DebugTooltip_OnLoad(this);</OnLoad>
<OnShow>FrameStackTooltip_OnShow(this);</OnShow>
<OnEnter>FrameStackTooltip_OnEnter(this);</OnEnter>
<OnUpdate>FrameStackTooltip_OnUpdate(this, arg1);</OnUpdate>
</Scripts>
</GameTooltip>
<GameTooltip name="EventTraceTooltip" frameStrata="TOOLTIP" hidden="true" parent="EventTraceFrame" inherits="GameTooltipTemplate">
<Scripts>
<OnLoad>DebugTooltip_OnLoad(this);</OnLoad>
</Scripts>
</GameTooltip>
</Ui>
+438
View File
@@ -0,0 +1,438 @@
------------------------------------------------------------------------------
-- Dump.lua (1.12 / Lua 5.0 backport)
--
-- Original by Iriel, Esamynn and Kirov from DevTools v1.11
-- /dump implementation
--
-- Globals: DevTools, SLASH_DUMP1, DevTools_Dump, DevTools_RunDump
-- Globals: DEVTOOLS_MAX_ENTRY_CUTOFF, DEVTOOLS_LONG_STRING_CUTOFF
-- Globals: DEVTOOLS_DEPTH_CUTOFF, DEVTOOLS_INDENT
-- Globals: DEVTOOLS_USE_TABLE_CACHE, DEVTOOLS_USE_FUNCTION_CACHE
-- Globals: DEVTOOLS_USE_USERDATA_CACHE
------------------------------------------------------------------------------
local DT = {};
DEVTOOLS_MAX_ENTRY_CUTOFF = 30; -- Maximum table entries shown
DEVTOOLS_LONG_STRING_CUTOFF = 200; -- Maximum string size shown
DEVTOOLS_DEPTH_CUTOFF = 10; -- Maximum table depth
DEVTOOLS_USE_TABLE_CACHE = true; -- Look up table names
DEVTOOLS_USE_FUNCTION_CACHE = true; -- Look up function names
DEVTOOLS_USE_USERDATA_CACHE = true; -- Look up userdata names
DEVTOOLS_INDENT = ' '; -- Indentation string
local DEVTOOLS_TYPE_COLOR = "|cff88ff88";
local DEVTOOLS_TABLEREF_COLOR = "|cffffcc00";
local DEVTOOLS_CUTOFF_COLOR = "|cffff0000";
local DEVTOOLS_TABLEKEY_COLOR = "|cff88ccff";
local FORMATS = {};
-- prefix type suffix
FORMATS["opaqueTypeVal"] = "%s" .. DEVTOOLS_TYPE_COLOR .. "<%s>|r%s";
-- prefix type name suffix
FORMATS["opaqueTypeValName"] = "%s" .. DEVTOOLS_TYPE_COLOR .. "<%s %s>|r%s";
-- type
FORMATS["opaqueTypeKey"] = "<%s>";
-- type name
FORMATS["opaqueTypeKeyName"] = "<%s %s>";
-- value
FORMATS["bracketTableKey"] = "[%s]";
-- prefix value
FORMATS["tableKeyAssignPrefix"] = DEVTOOLS_TABLEKEY_COLOR .. "%s%s|r=";
-- prefix cutoff
FORMATS["tableEntriesSkipped"] = "%s" .. DEVTOOLS_CUTOFF_COLOR .. "<skipped %s>|r";
-- prefix suffix
FORMATS["tableTooDeep"] = "%s" .. DEVTOOLS_CUTOFF_COLOR .. "<table (too deep)>|r%s";
-- prefix value suffix
FORMATS["simpleValue"] = "%s%s%s";
-- prefix tablename suffix
FORMATS["tableReference"] = "%s" .. DEVTOOLS_TABLEREF_COLOR .. "%s|r%s";
-- Grab a copy of various oft-used functions
local rawget = rawget;
local type = type;
local string_len = string.len;
local string_sub = string.sub;
local string_gsub = string.gsub;
local string_format = string.format;
local string_find = string.find;
local function WriteMessage(msg)
DEFAULT_CHAT_FRAME:AddMessage(msg);
end
local function prepSimple(val, context)
local valType = type(val);
if (valType == "nil") then
return "nil";
elseif (valType == "number") then
return val;
elseif (valType == "boolean") then
if (val) then
return "true";
else
return "false";
end
elseif (valType == "string") then
local l = string_len(val);
if ((l > DEVTOOLS_LONG_STRING_CUTOFF) and
(DEVTOOLS_LONG_STRING_CUTOFF > 0)) then
local more = l - DEVTOOLS_LONG_STRING_CUTOFF;
val = string_sub(val, 1, DEVTOOLS_LONG_STRING_CUTOFF);
return string_gsub(string_format("%q...+%s", val, more), "[|]", "||");
else
return string_gsub(string_format("%q", val), "[|]", "||");
end
elseif (valType == "function") then
local fName = context:GetFunctionName(val);
if (fName) then
return string_format(FORMATS.opaqueTypeKeyName, valType, fName);
else
return string_format(FORMATS.opaqueTypeKey, valType);
end
elseif (valType == "userdata") then
local uName = context:GetUserdataName(val);
if (uName) then
return string_format(FORMATS.opaqueTypeKeyName, valType, uName);
else
return string_format(FORMATS.opaqueTypeKey, valType);
end
elseif (valType == 'table') then
local tName = context:GetTableName(val);
if (tName) then
return string_format(FORMATS.opaqueTypeKeyName, valType, tName);
else
return string_format(FORMATS.opaqueTypeKey, valType);
end
end
error("Bad type '" .. valType .. "' to prepSimple");
end
local function prepSimpleKey(val, context)
local valType = type(val);
if (valType == "string") then
local l = string_len(val);
if ((l <= DEVTOOLS_LONG_STRING_CUTOFF) or
(DEVTOOLS_LONG_STRING_CUTOFF <= 0)) then
if (string_find(val, "^[a-zA-Z_][a-zA-Z0-9_]*$")) then
return val;
end
end
end
return string_format(FORMATS.bracketTableKey, prepSimple(val, context));
end
local function DevTools_InitFunctionCache(context)
local ret = {};
for _, k in ipairs(DT.functionSymbols) do
local v = getglobal(k);
if (type(v) == 'function') then
ret[v] = '[' .. k .. ']';
end
end
for k, v in pairs(getfenv(0)) do
if (type(v) == 'function') then
if (not ret[v]) then
ret[v] = '[' .. k .. ']';
end
end
end
return ret;
end
local function DevTools_InitUserdataCache(context)
local ret = {};
for _, k in ipairs(DT.userdataSymbols) do
local v = getglobal(k);
if (type(v) == 'table') then
local u = rawget(v, 0);
if (type(u) == 'userdata') then
ret[u] = k .. '[0]';
end
end
end
for k, v in pairs(getfenv(0)) do
if (type(v) == 'table') then
local u = rawget(v, 0);
if (type(u) == 'userdata') then
if (not ret[u]) then
ret[u] = k .. '[0]';
end
end
end
end
return ret;
end
local function DevTools_Cache_Nil(self, value, newName)
return nil;
end
local function DevTools_Cache_Function(self, value, newName)
if (not self.fCache) then
self.fCache = DevTools_InitFunctionCache(self);
end
local name = self.fCache[value];
if ((not name) and newName) then
self.fCache[value] = newName;
end
return name;
end
local function DevTools_Cache_Userdata(self, value, newName)
if (not self.uCache) then
self.uCache = DevTools_InitUserdataCache(self);
end
local name = self.uCache[value];
if ((not name) and newName) then
self.uCache[value] = newName;
end
return name;
end
local function DevTools_Cache_Table(self, value, newName)
if (not self.tCache) then
self.tCache = {};
end
local name = self.tCache[value];
if ((not name) and newName) then
self.tCache[value] = newName;
end
return name;
end
local function DevTools_Write(self, msg)
DEFAULT_CHAT_FRAME:AddMessage(msg);
end
local DevTools_DumpValue;
-- Strict weak order across mixed key types so table.sort is well-defined:
-- numbers first (by value), then strings (by value), then everything else
-- (by tostring). Booleans/tables/etc. fall through to tostring comparison.
local function _DumpKeyLess(a, b)
local ta, tb = type(a), type(b);
if (ta == tb) then
if (ta == "number" or ta == "string") then
return a < b;
end
return tostring(a) < tostring(b);
end
if (ta == "number") then return true; end
if (tb == "number") then return false; end
if (ta == "string") then return true; end
if (tb == "string") then return false; end
return tostring(a) < tostring(b);
end
local function DevTools_DumpTableContents(val, prefix, firstPrefix, context)
local showCount = 0;
local oldDepth = context.depth;
local oldKey = context.key;
-- Use this to set the cache name
context:GetTableName(val, oldKey or 'value');
-- Collect and sort keys so dump output is deterministic. pairs() walks
-- the hash part in internal order, which surfaces mixed-key tables out
-- of numeric order (e.g. GetFactionInfo's packed return values).
local keys = {};
for k in pairs(val) do
table.insert(keys, k);
end
table.sort(keys, _DumpKeyLess);
local total = table.getn(keys);
for i = 1, total do
local k = keys[i];
local v = val[k];
showCount = showCount + 1;
if ((showCount <= DEVTOOLS_MAX_ENTRY_CUTOFF) or
(DEVTOOLS_MAX_ENTRY_CUTOFF <= 0)) then
local prepKey = prepSimpleKey(k, context);
if (oldKey == nil) then
context.key = prepKey;
elseif (string_sub(prepKey, 1, 1) == "[") then
context.key = oldKey .. prepKey;
else
context.key = oldKey .. "." .. prepKey;
end
context.depth = oldDepth + 1;
local rp = string_format(FORMATS.tableKeyAssignPrefix, firstPrefix,
prepKey);
firstPrefix = prefix;
local trailing;
if (i < total) then
trailing = ",";
else
trailing = '';
end
DevTools_DumpValue(v, prefix, rp, trailing, context);
end
end
local cutoff = showCount - DEVTOOLS_MAX_ENTRY_CUTOFF;
if ((cutoff > 0) and (DEVTOOLS_MAX_ENTRY_CUTOFF > 0)) then
context:Write(string_format(FORMATS.tableEntriesSkipped, firstPrefix,
cutoff));
end
context.key = oldKey;
context.depth = oldDepth;
return (showCount > 0);
end
-- Return the specified value
function DevTools_DumpValue(val, prefix, firstPrefix, suffix, context)
local valType = type(val);
if (valType == "userdata") then
local uName = context:GetUserdataName(val, 'value');
if (uName) then
context:Write(string_format(FORMATS.opaqueTypeValName,
firstPrefix, valType, uName, suffix));
else
context:Write(string_format(FORMATS.opaqueTypeVal,
firstPrefix, valType, suffix));
end
return;
elseif (valType == "function") then
local fName = context:GetFunctionName(val, 'value');
if (fName) then
context:Write(string_format(FORMATS.opaqueTypeValName,
firstPrefix, valType, fName, suffix));
else
context:Write(string_format(FORMATS.opaqueTypeVal,
firstPrefix, valType, suffix));
end
return;
elseif (valType ~= "table") then
context:Write(string_format(FORMATS.simpleValue,
firstPrefix, prepSimple(val, context),
suffix));
return;
end
local cacheName = context:GetTableName(val);
if (cacheName) then
context:Write(string_format(FORMATS.tableReference,
firstPrefix, cacheName, suffix));
return;
end
if ((context.depth >= DEVTOOLS_DEPTH_CUTOFF) and
(DEVTOOLS_DEPTH_CUTOFF > 0)) then
context:Write(string_format(FORMATS.tableTooDeep,
firstPrefix, suffix));
return;
end
firstPrefix = firstPrefix .. "{";
local oldPrefix = prefix;
prefix = prefix .. DEVTOOLS_INDENT;
context:Write(firstPrefix);
firstPrefix = prefix;
DevTools_DumpTableContents(val, prefix, firstPrefix, context);
context:Write(oldPrefix .. "}" .. suffix);
end
local function Pick_Cache_Function(func, setting)
if (setting) then
return func;
else
return DevTools_Cache_Nil;
end
end
function DevTools_RunDump(value, context)
local prefix = "";
local firstPrefix = prefix;
if (type(value) == 'table') then
local any =
DevTools_DumpTableContents(value, prefix, firstPrefix, context);
if (context.Result) then
return context:Result();
end
if (not any) then
context:Write("empty result");
end
return;
end
DevTools_DumpValue(value, '', '', '', context);
if (context.Result) then
return context:Result();
end
end
-- Dump the specified list of values
function DevTools_Dump(value, startKey)
local context = {
depth = 0,
key = startKey,
};
context.GetTableName = Pick_Cache_Function(DevTools_Cache_Table,
DEVTOOLS_USE_TABLE_CACHE);
context.GetFunctionName = Pick_Cache_Function(DevTools_Cache_Function,
DEVTOOLS_USE_FUNCTION_CACHE);
context.GetUserdataName = Pick_Cache_Function(DevTools_Cache_Userdata,
DEVTOOLS_USE_USERDATA_CACHE);
context.Write = DevTools_Write;
DevTools_RunDump(value, context);
end
function DevTools_DumpCommand(msg)
forceinsecure();
if (msg == nil) then msg = ""; end
if (string_find(msg, "^[A-Za-z_][A-Za-z0-9_]*$")) then
WriteMessage("Dump: " .. msg);
local val = getglobal(msg);
local tmp = {};
if (val == nil) then
local key = string_format(FORMATS.tableKeyAssignPrefix,
'', prepSimpleKey(msg, {}));
WriteMessage(key .. "nil,");
else
tmp[msg] = val;
end
DevTools_Dump(tmp);
return;
end
WriteMessage("Dump: value=" .. msg);
local func, err = loadstring("return " .. msg);
if (not func) then
WriteMessage("Dump: ERROR: " .. err);
else
DevTools_Dump({ func() }, "value");
end
end
DT.functionSymbols = {};
DT.userdataSymbols = {};
local funcSyms = DT.functionSymbols;
local userSyms = DT.userdataSymbols;
for k, v in pairs(getfenv(0)) do
if (type(v) == 'function') then
table.insert(funcSyms, k);
elseif (type(v) == 'table') then
if (type(rawget(v, 0)) == 'userdata') then
table.insert(userSyms, k);
end
end
end
-- Slash command setup
SLASH_DUMP1 = "/dump";
SlashCmdList["DUMP"] = DevTools_DumpCommand;