Skip to content

Commit 15fdb32

Browse files
author
karel26
committed
ENHANCEMENTS:
- Plugin "debug" added as an event debugger that you can use to see which events are being fired by DCS
1 parent 1761725 commit 15fdb32

File tree

6 files changed

+178
-1
lines changed

6 files changed

+178
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Scripts/net/DCSServerBot/DCSServerBotConfig.lua
1212
/export/
1313
/plugins/test/
1414
/plugins/community/
15-
/plugins/debug/
1615
/build/
1716
/fonts/
1817
/Music/

plugins/debug/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Plugin "Debug"
2+
Simple event debugger for DCS callbacks and DCS mission events. It dumps the whole event structure for debug purposes.
3+
4+
## Configuration
5+
As Debug is an optional plugin, you need to activate it in main.yaml first like so:
6+
```yaml
7+
opt_plugins:
8+
- debug
9+
```
10+
> Please keep in mind that additional logging can cost some performance, where I don't expect this plugin to take much.
11+
12+
To separate the debug events from your dcs.log, I would recommend to add this to your autoexec.cfg:
13+
```lua
14+
log.set_output('events', 'EVENT DEBUGGER', log.ALL, log.FULL)
15+
```
16+
17+
## Credits
18+
Thanks to MisterOutOfTime (Moots) for helping me building that.

plugins/debug/commands.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import discord
2+
3+
from core import Plugin, command, utils
4+
from discord import app_commands
5+
from services.bot import DCSServerBot
6+
7+
8+
class Debug(Plugin):
9+
...
10+
11+
12+
async def setup(bot: DCSServerBot):
13+
await bot.add_cog(Debug(bot))

plugins/debug/lua/callbacks.lua

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
local base = _G
2+
local utils = base.require("DCSServerBotUtils")
3+
local JSON = loadfile(lfs.currentdir() .. "Scripts\\JSON.lua")()
4+
local debug = debug or {}
5+
6+
7+
local function sanitizer(t)
8+
local visitedTable = {}
9+
local function sanitizeTable(ta)
10+
local t = {}
11+
for k, v in pairs(ta) do
12+
if type(v) == "number" then
13+
if v == math.huge or v == -math.huge then
14+
v = nil
15+
end
16+
if v ~= v then
17+
v = "NAN"
18+
end
19+
end
20+
if type(k) == "string" then
21+
else
22+
k = tostring(k)
23+
end
24+
t[k] = k
25+
if type(v) == "function" then
26+
t[k] = "function"
27+
elseif type(v) == "userdata" then
28+
t[k] = "userdata"
29+
elseif type(v) == "table" then
30+
if not visitedTable[tostring(v)] then
31+
visitedTable[tostring(v)] = 1
32+
t[k] = sanitizeTable(v)
33+
else
34+
t[k] = "visited " .. tostring(v)
35+
end
36+
else
37+
t[k] = v
38+
end
39+
end
40+
return t
41+
end
42+
43+
return sanitizeTable(t)
44+
end
45+
46+
debug.mt = {
47+
__index = function(t, key)
48+
if rawget(t,"killed") ~= nil or key == 'onSimulationFrame' or key == 'RPC' then
49+
return
50+
end
51+
if key == 'onMissionLoadEnd' then
52+
utils.loadScript('DCSServerBot.lua')
53+
utils.loadScript('debug/mission.lua')
54+
end
55+
return function(...)
56+
js = JSON:encode(sanitizer(arg))
57+
log.write('EVENT DEBUGGER', log.DEBUG, key .. '(' .. js .. ')')
58+
visitedTable = {}
59+
end
60+
end
61+
}
62+
63+
debug.kill = function(o)
64+
rawset(o,"killed", true)
65+
setmetatable(o,{})
66+
return o
67+
end
68+
69+
debug.new = function(o)
70+
setmetatable(o, debug.mt)
71+
return o
72+
end
73+
74+
local h = debug.new({})
75+
76+
DCS.setUserCallbacks(h)

plugins/debug/lua/mission.lua

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
local base = _G
2+
dcsbot = base.dcsbot
3+
local JSON = loadfile(lfs.currentdir() .. "Scripts\\JSON.lua")()
4+
5+
6+
local event_by_id = {}
7+
local function sanitizer(t)
8+
local visitedTable = {}
9+
local function sanitizeTable(ta)
10+
local t = {}
11+
for k, v in pairs(ta) do
12+
if type(v) == "number" then
13+
if v == math.huge or v == -math.huge then
14+
v = nil
15+
end
16+
if v ~= v then
17+
v = "NAN"
18+
end
19+
end
20+
if type(k) == "string" then
21+
else
22+
k = tostring(k)
23+
end
24+
t[k] = k
25+
if type(v) == "function" then
26+
t[k] = "function"
27+
elseif type(v) == "userdata" then
28+
t[k] = "userdata"
29+
elseif type(v) == "table" then
30+
if not visitedTable[tostring(v)] then
31+
visitedTable[tostring(v)] = 1
32+
t[k] = sanitizeTable(v)
33+
else
34+
t[k] = "visited " .. tostring(v)
35+
end
36+
else
37+
t[k] = v
38+
end
39+
end
40+
return t
41+
end
42+
43+
return sanitizeTable(t)
44+
end
45+
46+
dcsbot.debugEventHandler = {}
47+
function dcsbot.debugEventHandler:onEvent(event)
48+
status, err = pcall(onEvent, event)
49+
if not status then
50+
env.warning("DCSServerBot - Error during Debug:onEvent(): " .. err)
51+
end
52+
end
53+
54+
function onEvent(event)
55+
if not event then
56+
return
57+
end
58+
log.write('EVENT DEBUGGER', log.DEBUG, event_by_id[event.id] .. '(' .. JSON:encode(sanitizer(event)) .. ')')
59+
end
60+
61+
62+
if not dcsbot.debug_enabled then
63+
for k, v in pairs(world.event) do
64+
event_by_id[v] = k
65+
end
66+
67+
world.addEventHandler(dcsbot.debugEventHandler)
68+
env.info('DCSServerBot - Event-Debugger enabled.')
69+
dcsbot.debug_enabled = true
70+
end

plugins/debug/version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = "3.0"

0 commit comments

Comments
 (0)