-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.lua
More file actions
198 lines (174 loc) · 5.14 KB
/
debug.lua
File metadata and controls
198 lines (174 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
-- Written by Noguai
local BUILD = 11
if ZZDebugBuild and ZZDebugBuild > BUILD then return end
ZZDebugBuild = BUILD
--- Throws a new error message
-- @param msg (string) The error message
-- @param level (number) The level to throw the error at (1 equals your function, 2 the function calling your function, ...)
-- @param ... (any) Values to append to the error message
--
function throw(msg, level, ...)
local num = select('#', ...)
for i = 1, num do
msg = string.format("%s\n%d: %s", msg, i, tostring(select(i, ...)))
end
error(msg, level + 1)
end
--- Throws an error when assertion of //check// fails
-- @param check (any) The expression to check for true or false
-- @param msg (string) The message for the possible error to throw
-- @param ... (any) Values to append to the error message
--
function catch(check, msg, ...)
if not check then
throw(msg, 2, ...)
end
return check, msg, ...
end
--- Returns an argument list as string (i.e. for bug messages)
-- @param ... (any) The argumanents to list
--
function arglist(...)
local argNum = select('#', ...)
local argStr = ""
if argNum > 0 then
argStr = "Argument list:"
for i = 1, argNum do
local arg = select(i, ...)
if arg ~= nil then
local argType = type(arg)
local str = argType == "string" and string.format("'%s'", arg) or tostring(arg)
argStr = string.format("%s\n%d: %s", argStr, i, str)
end
end
end
return argStr
end
--- Throws an error if //var// doesnt have valid type from //types//
-- @param var (any) The variable to check
-- @param types (table) An array of valid type names
-- @param argn (num) The argument 'index'
-- @param func (string) The function name
-- @usage typecatch("invalid", {"number"}, 1, "someFunction")
-- @usage "Invalid Argument #1 for someFunction (got string, expected number)"
--
function checktype(var, types, argn, func, ...)
local varType = type(var)
local typesType = type(types)
if typesType == "string" then
if types == varType then
return
end
else
for i, type in pairs(types) do
if type == varType then
return
end
end
end
local argStr = arglist(...)
local validTypeStr = typesType == "string" and types or table.concat(types, ", or ")
throw(string.format("Invalid argument #%d for %s. (got %s; expected %s)\n%s", tonumber(argn) or 0, tostring(func), varType, validTypeStr, argStr), 3)
end
typecatch = checktype
--- Returns the number of passed parameters
-- @param ... (any) List of parameters to count
--
function argn(...)
return arg.n
end
--- Prints a line to //DEFAULT_CHAT_FRAME//
-- @param msg (string) The message to print
--
function println(msg)
DEFAULT_CHAT_FRAME:AddMessage(msg)
end
--- Prints a colored line to //DEFAULT_CHAT_FRAME//
-- @param msg (string) The message to print
-- @param r (number) The red color
-- @param g (number) The green color
-- @param b (number) The blue color
--
function printlnc(msg, r, g, b)
DEFAULT_CHAT_FRAME:AddMessage(msg, r, g, b)
end
--- Prints multiple colored lines for each parameter to //DEFAULT_CHAT_FRAME//
-- @param r (number) The red color
-- @param g (number) The green color
-- @param b (number) The blue color
-- @param ... (string) The messages to print
--
function printc(r, g, b, ...)
local num = select('#', ...)
for i = 1, num do
printlnc(tostring(select(i, ...)), r, g, b)
end
end
--- Prints multiple lines for each parameter to //DEFAULT_CHAT_FRAME//
-- @param ... (string) The messages to print
--
function print(...)
local num = select('#', ...)
for i = 1, num do
println(tostring(select(i, ...)))
end
end
SLASH_PRINTFN1 = "/print"
SLASH_PRINTFN2 = "/out"
SLASH_PRINTFN3 = "/eval"
SLASH_PRINTFN4 = "/e"
SlashCmdList["PRINTFN"] = function(editBox, msg)
print(assert(loadstring("return "..(msg or "")))())
end
--- Prints a formatted line, with a default color
-- @param r (number) The red color
-- @param g (number) The green color
-- @param b (number) The blue color
-- @param format (string) The format string (string.format())
-- @param ... (depending) The values to insert into the format
--
function printfc(format, r, g, b, ...)
local needed = 0
for _ in format:gmatch("%%.") do
needed = needed + 1;
end
catch(needed == argn(...), "Argument number mismatch!", 2)
DEFAULT_CHAT_FRAME:AddMessage(format:format(...), r, g, b);
end
--- Prints a formatted line
-- @param format (string) The format string (string.format())
-- @param ... (depending) The values to insert into the format
--
function printf(format, ...)
printfc(format, 1, 1, 1, ...)
end
--- Prints an error message to //DEFAULT_CHAT_FRAME// and //BugMessageFrame//
-- @param errMsg
--
function bug(errMsg, forceFrame)
printc(0.6, 0.6, 0.6, "|cffff0000== SCRIPT_RUNTIIME_ERROR ==|r", errMsg)
BugMessageFrame_AddText(errMsg)
BUG_MESSAGE_NEW_ITEM = true
MinimapFrameBugGartherButton:Show()
if forceFrame then
BugMessageFrame:Show()
end
end
newbug = bug
--- Prints an formatted error message to //DEFAULT_CHAT_FRAME// and //BugMessageFrame//
-- @param errMsg
-- @param ...
--
function bugf(errMsg, ...)
local needVarArg = 0
local msg
for _ in errMsg:gmatch("%%.") do
needVarArg = needVarArg + 1;
end
if needVarArg == argn(...) then
msg = errMsg:format(...)
else
msg = errMsg
end
bug(msg)
end