forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.lua
378 lines (340 loc) · 13.3 KB
/
debug.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
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
function CpManager:setUpDebugChannels()
print('## Courseplay: setting up debug channels');
-- DEBUG CHANNELS
courseplay.numAvailableDebugChannels = 24;
courseplay.numDebugChannels = 21;
courseplay.numDebugChannelButtonsPerLine = 12;
courseplay.numDebugChannelSections = math.ceil(courseplay.numAvailableDebugChannels / courseplay.numDebugChannelButtonsPerLine);
courseplay.debugChannelSection = 1;
courseplay.debugChannelSectionStart = 1;
courseplay.debugChannelSectionEnd = courseplay.numDebugChannelButtonsPerLine;
courseplay.debugChannels = {};
for channel=1, courseplay.numAvailableDebugChannels do
courseplay.debugChannels[channel] = false;
end;
-- Debug channels legend:
courseplay.debugChannelsDesc = {
[1] = 'Debug: Raycast (drive + tipTriggers)';
[2] = 'Debug: Load and unload tippers';
[3] = 'Debug: Traffic collision';
[4] = 'Debug: Combines/mode2, register and unload combines';
[5] = 'Debug: Multiplayer';
[6] = 'Debug: implements (updateWorkTools etc.)';
[7] = 'Debug: course generation';
[8] = 'Debug: course management';
[9] = 'Debug: path finding';
[10] = 'Debug: mode9: shovel loading/unloading';
[11] = 'Debug: mode7: combine self-unloading';
[12] = 'Debug: all other debugs (uncategorized)';
[13] = 'Debug: reverse driving';
[14] = 'Debug: driving specific';
[15] = 'Debug: mode3: overloader';
[16] = 'Debug: recording courses';
[17] = 'Debug: mode4/6: seeding/fieldWork';
[18] = 'Debug: hud action';
[19] = 'Debug: special triggers';
[20] = 'Debug: WeightStation';
[21] = 'Debug: Speed setting';
};
courseplay.debugButtonPosData = {};
local dbgW = courseplay.hud:pxToNormal(22, 'x');
local dbgH = courseplay.hud:pxToNormal(22, 'y');
local dbgMarginX = dbgW * 0.075;
local dbgMaxX = courseplay.hud.contentMaxX - (2 * (courseplay.hud.buttonSize.small.w + courseplay.hud.buttonSize.small.margin));
local dbgMinX = dbgMaxX - (courseplay.numDebugChannelButtonsPerLine * dbgW) - ((courseplay.numDebugChannelButtonsPerLine - 1) * dbgMarginX);
local dbgBtnPosY = courseplay.hud.linesPosY[8] - courseplay.hud:pxToNormal(5, 'y');
for i = 1, courseplay.numDebugChannelButtonsPerLine do
local data = {};
data.width = dbgW;
data.height = dbgH;
data.posX = dbgMinX + ((i - 1) * (dbgW + dbgMarginX));
data.posY = dbgBtnPosY;
data.textPosX = data.posX + (dbgW * 0.5);
data.textPosY = courseplay.hud.linesPosY[8];
courseplay.debugButtonPosData[i] = data;
end;
end;
--------------------------------------------------
-- GENERAL DEBUG
function courseplay:debug(str, channel)
if channel ~= nil and courseplay.debugChannels[channel] ~= nil and courseplay.debugChannels[channel] == true then
print('[dbg' .. tostring(channel) .. ' lp' .. g_updateLoopIndex .. '] ' .. str);
end;
end;
local lines = {
('-'):rep(50),
('_'):rep(50),
('#'):rep(50)
};
function cpPrintLine(debugChannel, line)
if debugChannel == nil or courseplay.debugChannels[debugChannel] then
line = line or 1;
print(lines[line]);
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;
-- (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);
local oType = type(o);
if oType == '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 ('"%s, C function"'):format(so);
else
-- the information is defined in a script
return ('"%s, defined in %s (lines %d-%d)"'):format(so, info.source, info.linedefined, info.lastlinedefined);
end
elseif oType == 'number' then
return so;
elseif oType == 'boolean' then
return ('%s'):format(so);
else
return ('%q'):format(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
cart = indent .. field
-- print(('addToCart(value=%q, name=%q, indent, saved, field=%q, curDepth=%d)'):format(tostring(value), tostring(name), tostring(field), tostring(curDepth)));
if type(value) ~= 'table' then
cart = cart .. ' = ' .. basicSerialize(value) .. ';';
print(cart);
else
if saved[value] then
cart = cart .. ' = {}; -- ' .. saved[value] .. ' (self reference)';
print(cart);
autoref = autoref .. name .. ' = ' .. saved[value] .. ';\n';
else
saved[value] = name;
if isemptytable(value) then
cart = cart .. ' = {};';
print(cart);
else
if curDepth <= maxDepth then
cart = cart .. ' = {';
print(cart);
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 = indent .. '};';
print(cart);
else
cart = cart .. ' = { ... };';
print(cart);
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
print(autoref);
return ('-- %s %s -END- %s --'):format(('#'):rep(40), name, ('#'):rep(40));
end;
function eval(str)
return assert(loadstring(str))()
end
--------------------------------------------------
-- MULTIPLAYER DEBUG
courseplay.streamDebugCounter = 0;
courseplay.streamWriteFunctions = {
Bool = streamWriteBool;
Float = streamWriteFloat32;
Int = streamWriteInt32;
String = streamWriteString;
};
courseplay.streamReadFunctions = {
Bool = streamReadBool;
Float = streamReadFloat32;
Int = streamReadInt32;
String = streamReadString;
};
function courseplay.streamDebugWrite(streamId, varType, value)
courseplay.streamDebugCounter = courseplay.streamDebugCounter + 1;
if varType == 'Bool' then
value = Utils.getNoNil(value, false);
if value == 1 then
value = true;
elseif value == 0 then
value = false;
end;
courseplay:debug(('%d: writing bool: %s'):format(courseplay.streamDebugCounter, tostring(value)), 5);
elseif varType == 'Float' then
value = value or 0.0;
courseplay:debug(('%d: writing float: %f'):format(courseplay.streamDebugCounter, value), 5);
elseif varType == 'Int' then
value = value or 0.0;
courseplay:debug(('%d: writing int: %d'):format(courseplay.streamDebugCounter, value), 5);
elseif varType == 'String' then
value = value or '';
courseplay:debug(('%d: writing string: %q'):format(courseplay.streamDebugCounter, value), 5);
end;
courseplay.streamWriteFunctions[varType](streamId, value);
end;
function courseplay.streamDebugRead(streamId, varType)
courseplay.streamDebugCounter = courseplay.streamDebugCounter + 1;
local value = courseplay.streamReadFunctions[varType](streamId);
if varType == 'Bool' then
courseplay:debug(('%d: reading bool: %s'):format(courseplay.streamDebugCounter, tostring(value)), 5);
elseif varType == 'Float' then
courseplay:debug(('%d: reading float: %s'):format(courseplay.streamDebugCounter, tostring(value)), 5);
elseif varType == 'Int' then
courseplay:debug(('%d: reading int: %s'):format(courseplay.streamDebugCounter, tostring(value)), 5);
elseif varType == 'String' then
courseplay:debug(('%d: reading string: %s'):format(courseplay.streamDebugCounter, tostring(value)), 5);
end;
return value;
end;
stream_debug_counter = 0;
function streamDebugWriteFloat32(streamId, value)
value = Utils.getNoNil(value, 0.0)
stream_debug_counter = stream_debug_counter + 1
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
elseif value == 0 then
value = false
end
stream_debug_counter = stream_debug_counter + 1
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(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(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
local value = streamReadFloat32(streamId)
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
local value = streamReadInt32(streamId)
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
local value = streamReadBool(streamId)
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
local value = streamReadString(streamId)
courseplay:debug(string.format("%d: reading string: %s",stream_debug_counter, value ),5)
return value
end
--e.g. courseplay:findInTables(g_currentMission ,"g_currentMission", otherId)
function courseplay:findInTables(tableToSearchIn , tableToSearchString, valueToSearch)
if courseplay.lastSearchedValue == nil then
courseplay.lastSearchedValue = "empty"
end
if courseplay.lastSearchedValue == valueToSearch then --prevent loops in searching
return
else
print("courseplay:findInTables -> searching "..type(valueToSearch).." "..tostring(valueToSearch).." in "..tableToSearchString)
--courseplay.lastSearchedValue = valueToSearch
end
if type(tableToSearchIn) == "table" then
--Level 0
for index, value in pairs(tableToSearchIn) do
if courseplay:findInMatchingValues(index,value,valueToSearch) then
print(string.format("courseplay:findInTables -> %s.%s = %s",tableToSearchString,tostring(index),tostring(value)))
elseif type(value) == "table" then
local table1 = tableToSearchIn[index]
for index1, value1 in pairs(table1) do
if courseplay:findInMatchingValues(index1,value1,valueToSearch) then
print(string.format("courseplay:findInTables -> %s.%s.%s = %s",tableToSearchString,tostring(index),tostring(index1),tostring(value1)))
elseif type(value1) == "table" then
local table2 = table1[index1]
for index2, value2 in pairs(table2) do
if courseplay:findInMatchingValues(index2,value2,valueToSearch) then
print(string.format("courseplay:findInTables -> %s.%s.%s.%s = %s",tableToSearchString,tostring(index),tostring(index1),tostring(index2),tostring(value2)))
elseif type(value2) == "table" then
local table3 = table2[index2]
for index3, value3 in pairs(table3) do
if courseplay:findInMatchingValues(index3,value3 ,valueToSearch) then
print(string.format("courseplay:findInTables -> %s.%s.%s.%s.%s = %s",tableToSearchString,tostring(index),tostring(index1),tostring(index2),tostring(index3),tostring(value3)))
elseif type(value3) == "table" then
local table4 = table3[index3]
for index4, value4 in pairs(table3) do
if courseplay:findInMatchingValues(index4,value4,valueToSearch) then
print(string.format("courseplay:findInTables -> %s.%s.%s.%s.%s.%s = %s",tableToSearchString,tostring(index),tostring(index1),tostring(index2),tostring(index3),tostring(index4),tostring(value4)))
elseif type(value4) == "table" then
local table5 = table4[index4]
for index5, value5 in pairs(table4) do
if courseplay:findInMatchingValues(index5,value5,valueToSearch) then
print(string.format("courseplay:findInTables -> %s.%s.%s.%s.%s.%s.%s = %s",tableToSearchString,tostring(index),tostring(index1),tostring(index2),tostring(index3),tostring(index4),tostring(index5),tostring(value5)))
elseif type(value4) == "table" then
end
end
end
end
end
end
end
end
end
end
end
end
else
print("courseplay:findInTables -> "..tableToSearchString.." is not a table")
return
end
print("courseplay:findInTables -> searching finished")
end
function courseplay:findInMatchingValues(index, value1, value2)
local type1 = type(value1)
local type2 = type(value2)
--print("checking "..type1..tostring(value1).."vs "..type2.." "..tostring(value2))
if type1 == type2 and value1 == value2 then
return true
end
if type2 == "string" then
if tostring(index) == value2 then
return true
end
end
return false
end