forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.lua
More file actions
204 lines (182 loc) · 6.29 KB
/
debug.lua
File metadata and controls
204 lines (182 loc) · 6.29 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
function courseplay:debug(str, channel)
if channel ~= nil and courseplay.debugChannels[channel] ~= nil and courseplay.debugChannels[channel] == true then
print("[dbg" .. tostring(channel) .. "] " .. str);
end;
end;
function tableShow(t, name, channel, indent, maxDepth)
--important performance backup: the channel is checked first before proceeding with the compilation of the table
if channel ~= nil and courseplay.debugChannels[channel] ~= nil and courseplay.debugChannels[channel] == false then
return;
end;
local cart -- a container
local autoref -- for self references
maxDepth = maxDepth or 50;
local depth = 0;
--[[ counts the number of elements in a table
local function tablecount(t)
local n = 0
for _, _ in pairs(t) do n = n+1 end
return n
end
]]
-- (RiciLake) returns true if the table is empty
local function isemptytable(t) return next(t) == nil end
local function basicSerialize(o)
local so = tostring(o)
if type(o) == "function" then
local info = debug.getinfo(o, "S")
-- info.name is nil because o is not a calling level
if info.what == "C" then
return string.format("%q", so .. ", C function")
else
-- the information is defined through lines
return string.format("%q", so .. ", defined in (" ..
info.linedefined .. "-" .. info.lastlinedefined ..
")" .. info.source)
end
elseif type(o) == "number" then
return so
else
return string.format("%q", so)
end
end
local function addtocart(value, name, indent, saved, field, curDepth)
indent = indent or ""
saved = saved or {}
field = field or name
cart = cart .. indent .. field
if type(value) ~= "table" then
cart = cart .. " = " .. basicSerialize(value) .. ";\n"
else
if saved[value] then
cart = cart .. " = {}; -- " .. saved[value]
.. " (self reference)\n"
autoref = autoref .. name .. " = " .. saved[value] .. ";\n"
else
saved[value] = name
--if tablecount(value) == 0 then
if isemptytable(value) then
cart = cart .. " = {};\n"
else
if curDepth <= maxDepth then
cart = cart .. " = {\n"
for k, v in pairs(value) do
k = basicSerialize(k)
local fname = string.format("%s[%s]", name, k)
field = string.format("[%s]", k)
-- three spaces between levels
addtocart(v, fname, indent .. "\t", saved, field, curDepth + 1);
end
cart = cart .. indent .. "};\n"
else
cart = cart .. " = { ... };\n";
end;
end
end
end;
end
name = name or "__unnamed__"
if type(t) ~= "table" then
return name .. " = " .. basicSerialize(t)
end
cart, autoref = "", ""
addtocart(t, name, indent, nil, nil, depth + 1)
return cart .. autoref
end;
function eval(str)
return assert(loadstring(str))()
end
stream_debug_counter = 0
function streamDebugWriteFloat32(streamId, value)
value = Utils.getNoNil(value, 0.0)
stream_debug_counter = stream_debug_counter + 1
--[[courseplay:debug("++++++++++++++++", 55)
courseplay:debug(stream_debug_counter, 55)
courseplay:debug("float: " .. value, 5)
courseplay:debug("-----------------", 5)]]
courseplay:debug(string.format("%d: writing float: %f",stream_debug_counter, value ),5)
streamWriteFloat32(streamId, value)
end
function streamDebugWriteBool(streamId, value)
value = Utils.getNoNil(value, false)
if value == 1 then
value = true
end
if value == 0 then
value = false
end
stream_debug_counter = stream_debug_counter + 1
--[[courseplay:debug("++++++++++++++++", 5)
courseplay:debug(stream_debug_counter, 5)
courseplay:debug("Bool: ", 5)
courseplay:debug(value, 5)
courseplay:debug("-----------------", 5)]]
courseplay:debug(string.format("%d: writing bool: %s",stream_debug_counter, tostring(value) ),5)
streamWriteBool(streamId, value)
end
function streamDebugWriteInt32(streamId, value)
value = Utils.getNoNil(value, 0)
stream_debug_counter = stream_debug_counter + 1
--[[courseplay:debug("++++++++++++++++", 5)
courseplay:debug(stream_debug_counter, 5)
courseplay:debug("Int32: ", 5)
courseplay:debug(value, 5)
courseplay:debug("-----------------", 5)]]
courseplay:debug(string.format("%d: writing int: %d",stream_debug_counter, value ),5)
streamWriteInt32(streamId, value)
end
function streamDebugWriteString(streamId, value)
value = Utils.getNoNil(value, "")
stream_debug_counter = stream_debug_counter + 1
--[[courseplay:debug("++++++++++++++++", 5)
courseplay:debug(stream_debug_counter, 5)
courseplay:debug("String: ", 5)
courseplay:debug(value, 5)
courseplay:debug("-----------------", 5)]]
courseplay:debug(string.format("%d: writing string: %s",stream_debug_counter, value ),5)
streamWriteString(streamId, value)
end
function streamDebugReadFloat32(streamId)
stream_debug_counter = stream_debug_counter + 1
--courseplay:debug("++++++++++++++++", 5)
--courseplay:debug(stream_debug_counter, 5)
local value = streamReadFloat32(streamId)
--[[courseplay:debug("Float32: ", 5)
courseplay:debug(value, 5)
courseplay:debug("-----------------", 5)]]
courseplay:debug(string.format("%d: reading float: %f",stream_debug_counter, value ),5)
return value
end
function streamDebugReadInt32(streamId)
stream_debug_counter = stream_debug_counter + 1
--courseplay:debug("++++++++++++++++", 5)
--courseplay:debug(stream_debug_counter, 5)
local value = streamReadInt32(streamId)
--[[courseplay:debug("Int32: ", 5)
courseplay:debug(value, 5)
courseplay:debug("-----------------", 5)]]
courseplay:debug(string.format("%d: reading int: %d",stream_debug_counter, value ),5)
return value
end
function streamDebugReadBool(streamId)
stream_debug_counter = stream_debug_counter + 1
--courseplay:debug("++++++++++++++++", 5)
--courseplay:debug(stream_debug_counter, 5)
local value = streamReadBool(streamId)
--[[courseplay:debug("Bool: ", 5)
courseplay:debug(value, 5)
courseplay:debug("-----------------", 5)]]
courseplay:debug(string.format("%d: reading bool: %s",stream_debug_counter, tostring(value)),5)
return value
end
function streamDebugReadString(streamId)
stream_debug_counter = stream_debug_counter + 1
--courseplay:debug("++++++++++++++++", 5)
--courseplay:debug(stream_debug_counter, 5)
local value = streamReadString(streamId)
--[[courseplay:debug("String: ", 5)
courseplay:debug(value, 5)
courseplay:debug("-----------------", 5)]]
courseplay:debug(string.format("%d: reading string: %s",stream_debug_counter, value ),5)
return value
end