Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDebug.lua
More file actions
Latest commit
378 lines (307 loc) · 10.6 KB
/
Copy pathDebug.lua
File metadata and controls
378 lines (307 loc) · 10.6 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
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
localResources=require(game:GetService("ReplicatedStorage"):WaitForChild("Resources"))
localTable=Resources:LoadLibrary("Table")
localTyper=Resources:LoadLibrary("Typer")
localDebug= {}
localTAB= (""):rep(4)
localServices=setmetatable({}, { -- Memoize GetService calls
__index=function(self, i)
localSuccess, Object=pcall(game.GetService, game, i)
localService=SuccessandObject
self[i] =Service
returnService
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.
localFullName= {}
localCount=0
whileObject.Parent~=gameandObject.Parent~=nildo
localObjectName=Object.Name:gsub("([\\\"])", "\\%1")
ifObjectName:find("^[_%a][_%w]*$") then
FullName[Count] ="." ..ObjectName
else
FullName[Count] ="[\"" ..ObjectName.."\"]"
end
Count=Count-1
Object=Object.Parent
end
ifServices[Object.ClassName] ==Objectthen
FullName[Count] ="game:GetService(\"" ..Object.ClassName.."\")"
else
FullName[Count] ="." .."[\"" ..Object.Name.."\"]" -- A dot at the beginning indicates a rootless Object
end
returntable.concat(FullName, "", Count, 0)
end)
localGetErrorDatado
-- 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
localReplacers= {
["Index ?"] ="__index";
["Newindex ?"] ="__newindex";
}
localfunctionFormat(String, ...)
returnString:format(...)
end
localCommandBar= {Name="Command bar"}
functionGetErrorData(Err, ...) -- Make sure if you don't intend to format arguments in, you do %%f instead of %f
iftype(Err) ~="string" then
error(GetErrorData("!The first parameter of error formatting must be a string", "Debug"))
end
localt= {...}
localTraceback=debug.traceback()
localErrorDepth=select(2, Traceback:gsub("\n", "")) -2
localPrefix
Err, Prefix=Err:gsub("^!", "", 1)
localModuleName=Prefix==1andtable.remove(t, 1) or (getfenv(ErrorDepth).scriptorCommandBar).Name
localFunctionName=""
fori=1, select("#", ...) do
t[i] =Debug.Inspect(t[i])
end
forxinTraceback: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)
locali=0
forxinErr:gmatch("%%%l") do
i=i+1
ifx=="%q" then
t[i] =t[i]:gsub(" (%S+)$", "\"%1\"", 1)
end
end
localSuccess, ErrorString=pcall(Format, "[%s] {%s} " ..Err:gsub("%%q", "%%s"), ModuleName, Replacers[FunctionName] orFunctionName, unpack(t))
ifSuccessthen
returnErrorString, ErrorDepth
else
error(GetErrorData("!Error formatting failed, perhaps try escaping non-formattable tags like so: %%%%f\n(Error Message): " ..ErrorString, "Debug"))
end
end
functionDebug.Warn(...)
warn((GetErrorData(...)))
end
functionDebug.Error(...)
error(GetErrorData(...))
end
functionDebug.Assert(Condition, ...)
returnConditionorerror(GetErrorData(...))
end
end
do
localfunctionAlphabetically(a, b)
localtypeA=type(a)
localtypeB=type(b)
iftypeA==typeBthen
iftypeA=="number" then
returna<b
else
returntostring(a):lower() <tostring(b):lower()
end
else
returntypeA<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
localCount=0
localOrder= {}
forKeyinnext, Dictionarydo
Count=Count+1
Order[Count] =Key
end
table.sort(Order, Alphabetically)
locali=0
returnfunction(Table)
i=i+1
localKey=Order[i]
returnKey, Table[Key], i
end, Dictionary, nil
end)
end
functionDebug.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)
localIteratorFunctions= {...}
fori=1, #IteratorFunctionsdo
iftype(IteratorFunctions[i]) ~="function" then
error(GetErrorData("Cannot union Iterator functions which aren't functions"))
end
end
returnfunction(Table)
localCount=0
localOrder= {[0] = {}}
localKeysSeen= {}
fori=1, #IteratorFunctionsdo
localFunction, TableToIterateThrough, Next=IteratorFunctions[i](Table)
iftype(Function) ~="function" ortype(TableToIterateThrough) ~="table" then
error(GetErrorData("Iterator function " ..i.." must return a stack of types as follows: Function, Table, Variant"))
end
whiletruedo
localData= {Function(TableToIterateThrough, Next)}
Next=Data[1]
ifNext==nilthenbreakend
ifnotKeysSeen[Next] then
KeysSeen[Next] =true
Count=Count+1
Data[#Data+1] =i
Order[Count] =Data
end
end
end
returnfunction(_, Previous)
fori=0, Countdo
ifOrder[i][1] ==Previousthen
localData=Order[i+1]
ifDatathen
returnunpack(Data)
else
returnnil
end
end
end
error(GetErrorData("invalid key to unioned iterator function: " ..Previous))
end, Table, nil
end
end
localEachOrderdo
-- 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
localtypeof=typeofortype
localConvertTableIntoString
localfunctionParse(Object, Multiline, Depth, EncounteredTables)
localType=typeof(Object)
return
Type=="table" and (EncounteredTables[Object] and"[table " ..EncounteredTables[Object] .."]" orConvertTableIntoString(Object, nil, Multiline, Depth+1, EncounteredTables))
orType=="string" and"\"" ..Object.."\""
orType=="Instance" and"<" ..Debug.DirectoryToString(Object) ..">"
or (Type=="function" orType=="userdata") andType
ortostring(Object)
end
functionConvertTableIntoString(Table, TableName, Multiline, Depth, EncounteredTables)
localn=EncounteredTables.n+1
EncounteredTables[Table] =n
EncounteredTables.n=n
localt= {}
localCurrentArrayIndex=1
ifTableNamethen
t[1] =TableName
t[2] =" = {"
else
t[1] ="{"
end
ifnotnext(Table) then
t[#t+1] ="}"
returntable.concat(t)
end
forKey, ValueinDebug.AlphabeticalOrder(Table) do
ifnotMultilineandtype(Key) =="number" then
ifKey==CurrentArrayIndexthen
CurrentArrayIndex=CurrentArrayIndex+1
else
t[#t+1] ="[" ..Key.."] = "
end
t[#t+1] =Parse(Value, Multiline, Depth, EncounteredTables)
t[#t+1] =", "
else
ifMultilinethen
t[#t+1] ="\n"
t[#t+1] = (TAB):rep(Depth)
end
iftype(Key) =="string" andKey: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] =Multilineand";" or", "
end
end
ifMultilinethen
t[#t+1] ="\n"
t[#t+1] = (TAB):rep(Depth-1)
else
t[#t] =nil
end
t[#t+1] ="}"
localMetatable=getmetatable(Table)
ifMetatablethen
t[#t+1] =" <- "
t[#t+1] =type(Metatable) =="table" andConvertTableIntoString(Metatable, nil, Multiline, Depth, EncounteredTables) orDebug.Inspect(Metatable)
end
returntable.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
returnConvertTableIntoString(Table, TableName, Multiline, 1, {n=0})
end)
end
do
localEscapedCharacters= {"%", "^", "$", "(", ")", ".", "[", "]", "*", "+", "-", "?"}
localEscapable="([%" ..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
functionDebug.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
localList=""
fori=1, select("#", ...) do
localData=select(i, ...)
localDataType=typeof(Data)
localDataString
ifDataType=="Instance" then
DataType=Data.ClassName
DataString=Debug.DirectoryToString(Data)
else
DataString=DataType=="table" andDebug.TableToString(Data)
orDataType=="string" and"\"" ..Data.."\""
ortostring(Data)
end
List=List..", " .. ((DataType.."" ..DataString):gsub("^" ..DataType.."" ..DataType, DataType, 1))
end
ifList=="" then
return"NONE"
else
returnList:sub(3)
end
end
returnTable.Lock(Debug)