-
Notifications
You must be signed in to change notification settings - Fork 1
/
Debug.lua
378 lines (307 loc) · 10.6 KB
/
Debug.lua
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
-- Debugging Utilities
-- @documentation https://rostrap.github.io/Libraries/Debugging/Debug/
-- @author Validark
local Resources = require(game:GetService("ReplicatedStorage"):WaitForChild("Resources"))
local Table = Resources:LoadLibrary("Table")
local Typer = Resources:LoadLibrary("Typer")
local Debug = {}
local TAB = (" "):rep(4)
local Services = setmetatable({}, { -- Memoize GetService calls
__index = function(self, i)
local Success, Object = pcall(game.GetService, game, i)
local Service = Success and Object
self[i] = Service
return Service
end;
})
Debug.DirectoryToString = Typer.AssignSignature(Typer.Instance, function(Object)
--- Gets the string of the directory of an object, properly formatted
-- string DirectoryToString(Object)
-- @returns Objects location in proper Lua format
-- @author Validark
-- My implementation of the built-in GetFullName function which returns properly formatted text.
local FullName = {}
local Count = 0
while Object.Parent ~= game and Object.Parent ~= nil do
local ObjectName = Object.Name:gsub("([\\\"])", "\\%1")
if ObjectName:find("^[_%a][_%w]*$") then
FullName[Count] = "." .. ObjectName
else
FullName[Count] = "[\"" .. ObjectName .. "\"]"
end
Count = Count - 1
Object = Object.Parent
end
if Services[Object.ClassName] == Object then
FullName[Count] = "game:GetService(\"" .. Object.ClassName .. "\")"
else
FullName[Count] = "." .. "[\"" .. Object.Name .. "\"]" -- A dot at the beginning indicates a rootless Object
end
return table.concat(FullName, "", Count, 0)
end)
local GetErrorData do
-- Standard RoStrap Erroring system
-- Prefixing errors with '!' makes Error expect the [error origin].Name as first parameter after Error string
-- Past the initial Error string, subsequent arguments get unpacked in a string.format of the error string
-- Arguments formmatted into the string get stringified (see above function)
-- Assert falls back on Error
-- Error blames the latest item on the traceback as the cause of the error
-- Error makes it clear which Library and function are being misused
-- @author Validark
local Replacers = {
["Index ?"] = "__index";
["Newindex ?"] = "__newindex";
}
local function Format(String, ...)
return String:format(...)
end
local CommandBar = {Name = "Command bar"}
function GetErrorData(Err, ...) -- Make sure if you don't intend to format arguments in, you do %%f instead of %f
if type(Err) ~= "string" then
error(GetErrorData("!The first parameter of error formatting must be a string", "Debug"))
end
local t = {...}
local Traceback = debug.traceback()
local ErrorDepth = select(2, Traceback:gsub("\n", "")) - 2
local Prefix
Err, Prefix = Err:gsub("^!", "", 1)
local ModuleName = Prefix == 1 and table.remove(t, 1) or (getfenv(ErrorDepth).script or CommandBar).Name
local FunctionName = ""
for i = 1, select("#", ...) do
t[i] = Debug.Inspect(t[i])
end
for x in Traceback:sub(1, -11):gmatch("%- [^\r\n]+[\r\n]") do
FunctionName = x
end
FunctionName = FunctionName:sub(3, -2):gsub("%l+ (%S+)$", "%1"):gsub(" ([^\n\r]+)", " %1", 1)
local i = 0
for x in Err:gmatch("%%%l") do
i = i + 1
if x == "%q" then
t[i] = t[i]:gsub(" (%S+)$", " \"%1\"", 1)
end
end
local Success, ErrorString = pcall(Format, "[%s] {%s} " .. Err:gsub("%%q", "%%s"), ModuleName, Replacers[FunctionName] or FunctionName, unpack(t))
if Success then
return ErrorString, ErrorDepth
else
error(GetErrorData("!Error formatting failed, perhaps try escaping non-formattable tags like so: %%%%f\n(Error Message): " .. ErrorString, "Debug"))
end
end
function Debug.Warn(...)
warn((GetErrorData(...)))
end
function Debug.Error(...)
error(GetErrorData(...))
end
function Debug.Assert(Condition, ...)
return Condition or error(GetErrorData(...))
end
end
do
local function Alphabetically(a, b)
local typeA = type(a)
local typeB = type(b)
if typeA == typeB then
if typeA == "number" then
return a < b
else
return tostring(a):lower() < tostring(b):lower()
end
else
return typeA < typeB
end
end
Debug.AlphabeticalOrder = Typer.AssignSignature(Typer.Table, function(Dictionary)
--- Iteration function that iterates over a dictionary in alphabetical order
-- function AlphabeticalOrder(Dictionary)
-- @param table Dictionary That which will be iterated over in alphabetical order
-- A dictionary looks like this: {Apple = true, Noodles = 5, Soup = false}
-- Not case-sensitive
-- @author Validark
local Count = 0
local Order = {}
for Key in next, Dictionary do
Count = Count + 1
Order[Count] = Key
end
table.sort(Order, Alphabetically)
local i = 0
return function(Table)
i = i + 1
local Key = Order[i]
return Key, Table[Key], i
end, Dictionary, nil
end)
end
function Debug.UnionIteratorFunctions(...)
-- Takes in functions ..., and returns a function which unions them, which can be called on a table
-- Will iterate through a table, using the iterator functions passed in from left to right
-- Will pass the CurrentIteratorFunction index in the stack as the last variable
-- UnionIteratorFunctions(Get0, ipairs, Debug.AlphabeticalOrder)(Table)
local IteratorFunctions = {...}
for i = 1, #IteratorFunctions do
if type(IteratorFunctions[i]) ~= "function" then
error(GetErrorData("Cannot union Iterator functions which aren't functions"))
end
end
return function(Table)
local Count = 0
local Order = {[0] = {}}
local KeysSeen = {}
for i = 1, #IteratorFunctions do
local Function, TableToIterateThrough, Next = IteratorFunctions[i](Table)
if type(Function) ~= "function" or type(TableToIterateThrough) ~= "table" then
error(GetErrorData("Iterator function " .. i .. " must return a stack of types as follows: Function, Table, Variant"))
end
while true do
local Data = {Function(TableToIterateThrough, Next)}
Next = Data[1]
if Next == nil then break end
if not KeysSeen[Next] then
KeysSeen[Next] = true
Count = Count + 1
Data[#Data + 1] = i
Order[Count] = Data
end
end
end
return function(_, Previous)
for i = 0, Count do
if Order[i][1] == Previous then
local Data = Order[i + 1]
if Data then
return unpack(Data)
else
return nil
end
end
end
error(GetErrorData("invalid key to unioned iterator function: " .. Previous))
end, Table, nil
end
end
local EachOrder do
-- TODO: Write a function that takes multiple iterator functions and iterates through each passed in function
-- EachOrder(Get0(Table), ipairs(Table), AlphabeticalOrder(Table))
end
do
local typeof = typeof or type
local ConvertTableIntoString
local function Parse(Object, Multiline, Depth, EncounteredTables)
local Type = typeof(Object)
return
Type == "table" and (EncounteredTables[Object] and "[table " .. EncounteredTables[Object] .. "]" or ConvertTableIntoString(Object, nil, Multiline, Depth + 1, EncounteredTables))
or Type == "string" and "\"" .. Object .. "\""
or Type == "Instance" and "<" .. Debug.DirectoryToString(Object) .. ">"
or (Type == "function" or Type == "userdata") and Type
or tostring(Object)
end
function ConvertTableIntoString(Table, TableName, Multiline, Depth, EncounteredTables)
local n = EncounteredTables.n + 1
EncounteredTables[Table] = n
EncounteredTables.n = n
local t = {}
local CurrentArrayIndex = 1
if TableName then
t[1] = TableName
t[2] = " = {"
else
t[1] = "{"
end
if not next(Table) then
t[#t + 1] = "}"
return table.concat(t)
end
for Key, Value in Debug.AlphabeticalOrder(Table) do
if not Multiline and type(Key) == "number" then
if Key == CurrentArrayIndex then
CurrentArrayIndex = CurrentArrayIndex + 1
else
t[#t + 1] = "[" .. Key .. "] = "
end
t[#t + 1] = Parse(Value, Multiline, Depth, EncounteredTables)
t[#t + 1] = ", "
else
if Multiline then
t[#t + 1] = "\n"
t[#t + 1] = (TAB):rep(Depth)
end
if type(Key) == "string" and Key:find("^[%a_][%w_]*$") then
t[#t + 1] = Key
else
t[#t + 1] = "["
t[#t + 1] = Parse(Key, Multiline, Depth, EncounteredTables)
t[#t + 1] = "]"
end
t[#t + 1] = " = "
t[#t + 1] = Parse(Value, Multiline, Depth, EncounteredTables)
t[#t + 1] = Multiline and ";" or ", "
end
end
if Multiline then
t[#t + 1] = "\n"
t[#t + 1] = (TAB):rep(Depth - 1)
else
t[#t] = nil
end
t[#t + 1] = "}"
local Metatable = getmetatable(Table)
if Metatable then
t[#t + 1] = " <- "
t[#t + 1] = type(Metatable) == "table" and ConvertTableIntoString(Metatable, nil, Multiline, Depth, EncounteredTables) or Debug.Inspect(Metatable)
end
return table.concat(t)
end
Debug.TableToString = Typer.AssignSignature(Typer.Table, Typer.OptionalBoolean, Typer.OptionalString, function(Table, Multiline, TableName)
--- Converts a table into a readable string
-- string TableToString(Table, TableName, Multiline)
-- @param table Table The Table to convert into a readable string
-- @param string TableName Optional Name parameter that puts a "[TableName] = " at the beginning
-- @returns a readable string version of the table
return ConvertTableIntoString(Table, TableName, Multiline, 1, {n = 0})
end)
end
do
local EscapedCharacters = {"%", "^", "$", "(", ")", ".", "[", "]", "*", "+", "-", "?"}
local Escapable = "([%" .. table.concat(EscapedCharacters, "%") .. "])"
Debug.EscapeString = Typer.AssignSignature(Typer.String, function(String)
--- Turns strings into Lua-readble format
-- string Debug.EscapeString(String)
-- @returns Objects location in proper Lua format
-- @author Validark
-- Useful for when you are doing string-intensive coding
-- Those minus signs always get me when I'm not using this function!
return (
String
:gsub(Escapable, "%%%1")
:gsub("([\"\'\\])", "\\%1")
)
end)
end
function Debug.Inspect(...)
--- Returns a string representation of anything
-- @param any Object The object you wish to represent as a string
-- @returns a readable string representation of the object
local List = ""
for i = 1, select("#", ...) do
local Data = select(i, ...)
local DataType = typeof(Data)
local DataString
if DataType == "Instance" then
DataType = Data.ClassName
DataString = Debug.DirectoryToString(Data)
else
DataString = DataType == "table" and Debug.TableToString(Data)
or DataType == "string" and "\"" .. Data .. "\""
or tostring(Data)
end
List = List .. ", " .. ((DataType .. " " .. DataString):gsub("^" .. DataType .. " " .. DataType, DataType, 1))
end
if List == "" then
return "NONE"
else
return List:sub(3)
end
end
return Table.Lock(Debug)