This repository has been archived by the owner on Jul 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
generate_api.lua
218 lines (194 loc) · 4.67 KB
/
generate_api.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
local math_ceil, math_floor = math.ceil, math.floor
local table_insert, table_remove, table_concat, string_len = table.insert, table.remove, table.concat, string.len
local client_log, client_color_log = client.log, client.color_log
local to_dump = {
"client",
"entity",
"globals",
"ui",
"renderer",
"math",
"table",
"string",
"materialsystem"
}
local to_dump_global = {
"tonumber",
"tostring",
"assert",
"error",
"pairs",
"ipairs",
"next",
"setmetatable",
"getmetatable",
"pcall",
"type",
"unpack",
}
local ignore = {
["string"] = {
"rep",
"dump",
"byte",
"char",
},
["table"] = {
"maxn",
"foreach",
"foreachi",
"move",
"getn",
"pack",
"unpack"
},
["math"] = {
"log10",
"randomseed",
"random",
"huge",
"ldexp",
"frexp",
"tanh",
"modf"
},
}
local ignore_draw = true
local optimize_lookups = false
local split_tables = true
local split_message_length = false
local function table_contains(table, val)
for i = 1, #table do
if table[i] == val then
return true
end
end
return false
end
local function array_split(table, n, start)
local new_table = {}
local start = start or 1
for i=start, n do
local element = table[i]
if element ~= nil then
table_insert(new_table, element)
end
end
return new_table
end
local function array_sub(t1, t2)
local t = {}
for i = 1, #t1 do
t[t1[i]] = true
end
for i = #t2, 1, -1 do
if t[t2[i]] then
table_remove(t2, i)
end
end
end
local function create_local_variables(tbl, name, ignored)
local part1 = {}
local part2 = {}
for key, name_tmp in pairs(tbl) do
local name = name
if type(name_tmp) == "string" then
name = name_tmp
end
if not table_contains(ignored, key) and type(key) == "string" then
if name then
table_insert(part1, name .. "_" .. key)
table_insert(part2, name .. "." .. key)
else
table_insert(part1, key)
table_insert(part2, key)
end
end
end
local message = "local " .. table_concat(part1, ", ") .. " = " .. table_concat(part2, ", ")
if split_message_length then
local messages = {}
local parts = 1
if message:len() > 400 then
parts = math_ceil(message:len() / 400)
end
local split_at = math_floor(#part1/parts)
local start = 0
for i=1, parts do
local split_at_temp = split_at * i + 2
local part1_1, part2_1 = array_split(part1, split_at_temp, start+1), array_split(part2, split_at_temp, start+1)
table_insert(messages, "local " .. table_concat(part1_1, ", ") .. " = " .. table_concat(part2_1, ", "))
start = split_at_temp
end
for i=1, #messages do
client_color_log(150, 150, 150, messages[i])
end
else
client_color_log(150, 150, 150, message)
end
end
local function dump_api()
client_log("Copy and paste this into your script:")
client_color_log(182, 231, 23, "--local variables for API functions. Generated using https://github.com/sapphyrus/gamesense-lua/blob/master/generate_api.lua")
local tbl = {}
if split_tables then
for i=1, #to_dump do
local name = to_dump[i]
local tbl = _G[name]
local ignored = ignore[name] or {}
if ignore_draw and name == "client" then
for key, _ in pairs(renderer) do
table_insert(ignored, key)
table_insert(ignored, "draw_" .. key)
end
end
create_local_variables(tbl, name, ignored)
end
else
for i=1, #to_dump do
local name = to_dump[i]
local tbl = _G[name]
local ignored = ignore[name] or {}
if ignore_draw and name == "client" then
for key, _ in pairs(renderer) do
table_insert(ignored, key)
table_insert(ignored, "draw_" .. key)
end
end
end
end
for i=1, #to_dump_global do
tbl[to_dump_global[i]] = false
end
create_local_variables(tbl, nil, {})
client_color_log(182, 231, 23, "--end of local variables")
end
local function dump_globals_recursive(tbl, ignored, inspect)
local result = {fields={}}
for key, value in pairs(tbl) do
if type(key) ~= "number" then
if type(value) == "function" or type(value) == "number" or type(value) == "string" then
result.fields[key] = {}
elseif type(value) == "table" and value ~= tbl and key ~= "_G" then
if (key == "loaded" or key == "preload") and tbl == package then
result.fields[key] = {other_fields = true}
else
result.fields[key] = dump_globals_recursive(value)
end
end
end
end
if next(result.fields) == nil then
result = {other_fields = true}
end
return result
end
local function dump_globals()
local inspect = require "inspect"
local result = dump_globals_recursive(_G, {}, inspect)["fields"]
client.log("std = ", inspect({
read_globals = result
}))
end
ui.new_button("MISC", "Lua", "Generate API local variables", dump_api)
ui.new_button("MISC", "Lua", "Generate .luacheckrc", dump_globals)