-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug_print.lua
More file actions
298 lines (188 loc) · 5.6 KB
/
debug_print.lua
File metadata and controls
298 lines (188 loc) · 5.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
local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local debug = _tl_compat and _tl_compat.debug or debug; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local load = _tl_compat and _tl_compat.load or load; local os = _tl_compat and _tl_compat.os or os; local pairs = _tl_compat and _tl_compat.pairs or pairs; local pcall = _tl_compat and _tl_compat.pcall or pcall; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table
require('common')
require("love_inc").require_pls_nographic()
local ecodes = require("errorcodes")
local colorize = require('ansicolors2').ansicolors
local serpent = require("serpent")
local format = string.format
local PrintCallback = {}
local Filter = {}
local LoaderFilter = {}
local Enabled = {}
local LoaderEnabled = {}
local channel_filter = love.thread.getChannel("debug_filter")
local channel_enabled = love.thread.getChannel("debug_enabled")
local filter = {}
local enabled = {
[0] = false, [1] = false, [2] = false, [3] = false, [4] = false,
[5] = false, [6] = false, [7] = false, [8] = false, [9] = false,
}
local shouldPrint = {}
local ids = {}
local function checkNum(n)
local m = {
[0] = true,
[1] = true,
[2] = true,
[3] = true,
[4] = true,
[5] = true,
[6] = true,
[7] = true,
[8] = true,
[9] = true,
}
return m[n] or false
end
local function checkNumbers(filt)
for k, _ in pairs(filt) do
local num = tonumber(k)
if not (num and checkNum(num)) then
return false, "Incorrect number(not in 0..9 range): " .. num
end
end
return true
end
local function parse_ids(setup)
local ret_ids = {}
for _, row in pairs(setup) do
for _, id in ipairs(row) do
ret_ids[id] = true
end
end
return ret_ids
end
local function push_shared_enabled(state)
local enabled_ser = serpent.dump(state)
channel_enabled:clear()
channel_enabled:push(enabled_ser)
end
local function push_shared_filter(state)
local filter_ser = serpent.dump(state)
channel_filter:clear()
channel_filter:push(filter_ser)
end
local function set_filter(setup)
assert(setup)
local ok, errmsg = checkNumbers(setup)
if not ok then
error("Error in filter setup: " .. errmsg)
end
filter = deepCopy(setup)
push_shared_filter(filter)
push_shared_enabled(enabled)
end
local function peek_shared_enabled()
local shared_enabled
local enabled_ser = channel_enabled:peek()
if enabled_ser then
local chunk = load(enabled_ser)
if not chunk then
error("Could not load(enabled_ser)")
end
shared_enabled = chunk()
end
return shared_enabled
end
local function peek_shared_filter()
local shared_filter
local filter_ser = channel_filter:peek()
if filter_ser then
local chunk = load(filter_ser)
if not chunk then
error("Could not load(filter_ser)")
end
shared_filter = chunk()
end
return shared_filter
end
local printCallback = function(...)
print(...)
end
local function set_callback(cb)
assert(cb)
printCallback = cb
end
local function keypressed(key, key2)
assert(key2 == nil, "Use only scancode. Second param always unused.")
local num = tonumber(key)
local shared_filter = peek_shared_filter()
if checkNum(num) then
enabled[num] = not enabled[num]
local ids_list = shared_filter[num]
if ids_list then
for _, v in ipairs(ids_list) do
shouldPrint[v] = enabled[num]
end
end
end
push_shared_enabled(enabled)
end
local function print_ids()
local msg = ""
for k, _ in pairs(ids) do
msg = msg .. k .. " "
end
print("Avaible ids are: ", colorize("%{yellow}" .. msg))
end
local function debug_print(id, ...)
assert(type(id) == 'string')
local shared_filter = peek_shared_filter()
ids = parse_ids(shared_filter)
if not ids[id] then
local msg = format("id = '%s' not found in filter", tostring(id))
print(msg)
print_ids()
print(debug.traceback())
os.exit(ecodes.ERROR_NO_SUCH_DEBUG_ID)
end
if shouldPrint[id] then
printCallback(...)
end
end
local function build_str()
local shared_filter = peek_shared_filter()
local shared_enabled = peek_shared_enabled() or enabled
local s = {}
for k, ids_arr in pairs(shared_filter) do
local state = "(" .. tostring(k)
if shared_enabled[k] then
state = state .. "+"
else
state = state .. "-"
end
state = state .. "): "
local count = #ids_arr
for i, id in ipairs(ids_arr) do
local appendix = i ~= count and "," or " "
state = state .. id .. appendix
end
table.insert(s, state)
end
return table.concat(s)
end
local font_size = 32
local font
local ok, errmsg = pcall(function()
font = love.graphics.newFont(font_size)
end), string
if not ok then
print("Could not create new default font:", errmsg)
end
local function render(x0, y0)
local s = build_str()
assert(x0)
assert(y0)
local width, _ = love.graphics.getDimensions()
local old_font = love.graphics.getFont()
love.graphics.setFont(font)
love.graphics.printf(s, x0, y0, width)
love.graphics.setFont(old_font)
end
return {
render = render,
debug_print = debug_print,
set_filter = set_filter,
set_callback = set_callback,
keypressed = keypressed,
}