-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfunctions.lua
More file actions
493 lines (412 loc) · 11.6 KB
/
Copy pathfunctions.lua
File metadata and controls
493 lines (412 loc) · 11.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
-- Finds out the type of the return types
function ObjectToTypeName(a_ClassName, a_FunctionName, a_ReturnTypes)
local ret = {}
for index, rType in ipairs(a_ReturnTypes) do
if
rType == "boolean" or
rType == "number" or
rType == "string" or
rType == "table"
then
ret[index] = rType
elseif g_ObjectToTypeName[rType] ~= nil then
ret[index] = g_ObjectToTypeName[rType]
elseif
a_ClassName == "cCompositeChat" and
rType == "self"
then
ret[index] = "userdata"
elseif
string.find(rType, "#") ~= nil or
string.find(string.lower(rType), "^e") ~= nil
then
-- Enums
ret[index] = "number"
elseif
-- Classes
string.find(rType, "^c") ~= nil or
string.find(rType, "^Vec") ~= nil
then
ret[index] = "userdata"
end
if ret[index] == nil then
Abort(string.format("ObjectToTypeName(%s, %s): %s not handled", a_ClassName, a_FunctionName, rType))
end
end
return ret
end
-- Gathers the return values from the called function
function GatherReturnValues(...)
if arg.n == 0 then
g_ReturnTypes = nil
elseif arg[1] == nil and arg.n == 1 then
g_ReturnTypes = nil
else
g_ReturnTypes = {}
for _, r in ipairs(arg) do
table.insert(g_ReturnTypes, type(r))
end
end
end
function CopyTable(a_Source)
local tmp = {}
for key, value in pairs(a_Source) do
tmp[key] = value
end
return tmp
end
-- Called when plugin is loaded
-- If file crashed.txt exists, a crash occurred on last run
function CheckIfCrashed()
local fileCrashed = io.open(g_Plugin:GetLocalFolder() .. cFile:GetPathSeparator() .. "crashed.txt", "r")
if fileCrashed == nil then
return
end
-- Add function to table crashed
local className = fileCrashed:read("*line")
if className ~= nil and className ~= "" then
local functionName = fileCrashed:read("*line")
local fncTest = fileCrashed:read("*line")
if g_Crashed[className] == nil then
g_Crashed[className] = {}
end
g_Crashed[className][functionName] = fncTest
-- Remove function from table ignore
if g_Ignore[className] == nil then
g_Ignore[className] = {}
end
-- TODO
g_Ignore[className][functionName] = nil
end
fileCrashed:close()
cFile:DeleteFile(g_Plugin:GetLocalFolder() .. cFile:GetPathSeparator() .. "crashed.txt")
SaveTableIgnore()
SaveTableCrashed()
end
-- Saves current class name, function name and params
function SaveCurrentTest(a_ClassName, a_FunctionName, a_FunctionAndParams) -- TODO
local fileCurrent = io.open(g_Plugin:GetLocalFolder() .. cFile:GetPathSeparator() .. "current.txt", "w")
fileCurrent:write(a_ClassName, "\n")
fileCurrent:write(a_FunctionName, "\n")
if a_FunctionAndParams:len() > 10000 then
fileCurrent:write("infinity", "\n")
else
fileCurrent:write(a_FunctionAndParams, "\n")
end
fileCurrent:close()
end
-- Loads the table g_Ignore from file
-- Aborts if file exists and loading fails
function LoadTableIgnore()
-- Check if file exists
if not(cFile:IsFile(g_Plugin:GetLocalFolder() .. cFile:GetPathSeparator() .. "ignore_table.txt")) then
g_Ignore = {}
return
end
local fncIgnore = loadfile(g_Plugin:GetLocalFolder() .. cFile:GetPathSeparator() .. "ignore_table.txt")
if (fncIgnore == nil) then
Abort("The file ignore_table.txt could not be loaded.")
end
g_Ignore = fncIgnore()
end
-- Saves the table g_Crashed to file
function SaveTableIgnore()
local fileIgnore = io.open(g_Plugin:GetLocalFolder() .. cFile:GetPathSeparator() .. "ignore_table.txt", "w")
fileIgnore:write("return\n{\n")
for className, tbFunctions in pairs(g_Ignore) do
local s = "\t" .. className .. " =\n\t{\n"
local writeIt = false
for functionName, tbKeys in pairs(tbFunctions) do
s = s .. "\t\t" .. functionName .. " =\n\t\t{\n"
for key, _ in pairs(tbKeys) do
writeIt = true
s = s .. "\t\t\t[ \"" .. key .. "\" ] = true,\n"
end
s = s .. "\t\t},\n"
end
s = s .. "\t},\n"
if writeIt then
fileIgnore:write(s)
end
end
fileIgnore:write("}\n")
fileIgnore:close()
end
-- Loads the table g_Crashed from file
-- Aborts if file exists and loading fails
function LoadTableCrashed()
-- Check if file exists
if not(cFile:IsFile(g_Plugin:GetLocalFolder() .. cFile:GetPathSeparator() .. "crashed_table.txt")) then
g_Crashed = {}
return
end
local fncCrashed = loadfile(g_Plugin:GetLocalFolder() .. cFile:GetPathSeparator() .. "crashed_table.txt")
if (fncCrashed == nil) then
Abort("The file crashed_table.txt could not be loaded.")
end
g_Crashed = fncCrashed()
end
-- Saves the table g_Crashed to file
function SaveTableCrashed()
local fileCrashed = io.open(g_Plugin:GetLocalFolder() .. cFile:GetPathSeparator() .. "crashed_table.txt", "w")
fileCrashed:write("return\n{\n")
for className, tbFunctions in pairs(g_Crashed) do
local s = "\t" .. className .. " =\n\t{\n"
local writeIt = false
if tbFunctions ~= "*" then
for functionName, funcTest in pairs(tbFunctions) do
writeIt = true
s = s .. "\t\t" .. functionName .. " = \"" .. funcTest .. "\",\n"
end
s = s .. "\t},\n"
end
if writeIt then
fileCrashed:write(s)
end
end
fileCrashed:write("}\n")
fileCrashed:close()
end
function IsDeprecated(a_Notes)
a_Notes = a_Notes:lower()
if
string.find(a_Notes, "vector%-parametered") or
string.find(a_Notes, "obsolete") or
string.find(a_Notes, "deprecated")
then
return true
end
return false
end
-- Check if passed function table has params
-- Returns table with tables of param types or nil
-- Also adds flag IsStatic, if present
function GetParamTypes(a_FncInfo, a_FunctionName)
if a_FncInfo.Notes ~= nil and IsDeprecated(a_FncInfo.Notes) then
return "ignore"
end
local paramTypes = {}
if a_FncInfo.Params ~= nil then
for _, param in ipairs(a_FncInfo.Params) do
table.insert(paramTypes, param.Type)
end
if a_FncInfo.IsStatic then
paramTypes.IsStatic = true
end
return { paramTypes }
end
local hasParamTypes = false
for _, tb in ipairs(a_FncInfo) do
local temp = {}
local bIgnore = false
if tb.Params ~= nil then
-- Check for vector-parametered, OBSOLETE and DEPRECATED
if IsDeprecated(tb.Notes) then
bIgnore = true
else
for _, param in pairs(tb.Params) do
hasParamTypes = true
table.insert(temp, param.Type)
end
end
end
if not(bIgnore) then
if tb.IsStatic then
temp.IsStatic = true
end
table.insert(paramTypes, temp)
end
end
if hasParamTypes then
return paramTypes
end
return nil
end
-- Check if passed function table has return types
-- Returns table with tables of return types or nil
function GetReturnTypes(a_FncInfo, a_ClassName, a_FunctionName)
local returnTypes = {}
if a_FncInfo.Returns ~= nil then
for _, ret in ipairs(a_FncInfo.Returns) do
table.insert(returnTypes, ret.Type)
end
return { returnTypes }
end
local hasReturnTypes = false
for _, tb in ipairs(a_FncInfo) do
local temp = {}
if tb.Returns ~= nil then
for _, ret in pairs(tb.Returns) do
hasReturnTypes = true
table.insert(temp, ret.Type)
end
end
table.insert(returnTypes, temp)
end
if hasReturnTypes then
return returnTypes
end
return nil
end
-- Find enum value for the passed enum type
-- Looks first into the table g_EnumValues.
-- If not found, searches in APIDesc
function GetEnumValue(a_EnumType)
if g_EnumValues[a_EnumType] ~= nil then
return g_EnumValues[a_EnumType]
end
-- Search in APIDoc
if string.find(a_EnumType, "#") ~= nil then
local tbClassEnumType = StringSplit(a_EnumType, "#")
local class = GetClass(tbClassEnumType[1])
local include = class.ConstantGroups[tbClassEnumType[2]].Include
if
type(include) == "table" and
(string.find(include[1], "^") ~= nil) and
(string.find(include[1], "*") ~= nil)
then
-- A Include with pattern can be a string or a table:
-- Include = { "^gm.*" } or Include = "^gm.*"
include = include[1]
end
if type(include) == "table" then
g_EnumValues[a_EnumType] = tbClassEnumType[1] .. "." .. include[1]
return g_EnumValues[a_EnumType]
end
for var, value in pairs(_G[tbClassEnumType[1]]) do
if string.find(var, include) ~= nil then
g_EnumValues[a_EnumType] = tbClassEnumType[1] .. "." ..var
return g_EnumValues[a_EnumType]
end
end
else
-- Check Globals in APIDesc
local class = GetClass("Globals")
if class.ConstantGroups[a_EnumType] ~= nil then
local include = class.ConstantGroups[a_EnumType].Include
if
type(include) == "table" and
(string.find(include[1], "^") ~= nil) and
(string.find(include[1], "*") ~= nil)
then
-- A Include with pattern can be a string or a table:
-- Include = { "^gm.*" } Include = "^gm.*"
include = include[1]
end
if
type(include) == "table"
then
-- Return first entry of table
g_EnumValues[a_EnumType] = include[1]
return g_EnumValues[a_EnumType]
else
-- Check for pattern
if
string.find(include, "^") ~= nil or
string.find(include, "*") ~= nil
then
-- Check _G
for var, value in pairs(_G) do
if (
(value ~= _G) and -- don't want the global namespace
(value ~= _G.packages) and -- don't want any packages
(value ~= _G[".get"])
) then
if (type(var) == "string") and string.find(var, include) then
g_EnumValues[a_EnumType] = var
return var
end
end
end
end
end
end
end
Abort("Enum not found: " .. a_EnumType)
end
-- Loops over the API files and searches the class
function GetClass(a_ClassName)
for _, tbClasses in pairs(g_APIDesc) do
if tbClasses[a_ClassName] ~= nil then
return tbClasses[a_ClassName]
end
end
Abort("Class not found: " .. a_ClassName)
end
function IsIgnored(a_ClassName, a_FunctionName, a_ParamTypes)
if g_IgnoreShared[a_ClassName] == "*" then
return true
end
-- Check if function is ignored, causes crash or is special
if
(type(g_IgnoreShared[a_ClassName]) == "table" and
g_IgnoreShared[a_ClassName][a_FunctionName] == true) or
g_Crashed[a_ClassName][a_FunctionName] ~= nil or
a_FunctionName == "constructor" or
a_FunctionName == "operator_div" or
a_FunctionName == "operator_eq" or
a_FunctionName == "operator_mul" or
a_FunctionName == "operator_plus" or
a_FunctionName == "operator_sub"
then
return true
end
-- Check table g_Ignore
if g_IsFuzzing then
if
g_Ignore[a_ClassName] ~= nil and
g_Ignore[a_ClassName][a_FunctionName] ~= nil
then
for index = #a_ParamTypes, 1, -1 do
local key = table.concat(a_ParamTypes[index], ", ")
if g_Ignore[a_ClassName][a_FunctionName][key] then
table.remove(a_ParamTypes, index)
end
end
if #a_ParamTypes == 0 then
return true
end
end
end
-- Check table g_IgnoreShared
if
g_IgnoreShared[a_ClassName] ~= nil and
g_IgnoreShared[a_ClassName][a_FunctionName] ~= nil
then
for index = #a_ParamTypes, 1, -1 do
local key = table.concat(a_ParamTypes[index], ", ")
if g_IgnoreShared[a_ClassName][a_FunctionName][key] then
table.remove(a_ParamTypes, index)
end
end
if #a_ParamTypes == 0 then
return true
end
end
-- Check if g_Params contains the class and function name
if g_Params[a_ClassName] ~= nil and g_Params[a_ClassName][a_FunctionName] ~= nil then
return false
end
if
g_IgnoreShared[a_ClassName] ~= "*" and
g_IgnoreShared[a_ClassName][a_FunctionName] == nil and
g_Crashed[a_ClassName][a_FunctionName] == nil
then
return false
end
return true
end
function AddToIgnoreTable(a_ClassName, a_FunctionName, a_Key)
if g_Ignore[a_ClassName][a_FunctionName] == nil then
g_Ignore[a_ClassName][a_FunctionName] = {}
end
g_Ignore[a_ClassName][a_FunctionName][a_Key] = true
end
function Abort(a_ErrorMessage)
-- Only create stop file in fuzzing mode
if g_IsFuzzing then
local fileStop = io.open("stop.txt", "w")
fileStop:close()
end
assert(false, a_ErrorMessage)
end