-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdcs-code-injector-hook.lua
84 lines (67 loc) · 2.56 KB
/
dcs-code-injector-hook.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
package.path = package.path .. ";.\\LuaSocket\\?.lua"
package.cpath = package.cpath .. ";.\\LuaSocket\\?.dll"
socket = require("socket")
DCSCI = {}
DCSCI.host = "*"
DCSCI.port = 45221
DCSCI.mission_load_complete = false
DCSCI.server = socket.bind(DCSCI.host, DCSCI.port)
DCSCI.server:settimeout(0)
local function init()
log.write("DCS Code Injector", log.INFO, "Starting...")
local handler = {}
function handler.onMissionLoadEnd()
log.write("DCS Code Injector -->", log.INFO, "Mission load complete, Code Injector Loaded")
DCSCI.mission_load_complete = true
end
function handler.onSimulationFrame()
if not DCSCI.mission_load_complete then
return
end
local code_injector_client = DCSCI.server:accept()
if code_injector_client then
code_injector_client:settimeout(0.005)
local chunk, err, partial
local response = ""
repeat
chunk, err, partial = code_injector_client:receive()
response = response .. (chunk or partial) .. "\n"
until err or chunk == nil
if response:len() > 1 then
-- Execute the received code in the mission
local mission_string =
[[
local ok, err = pcall(a_do_script(
[=[
]]
.. response ..
[[
]=]
))
if not ok then
log.write("DCS Code Injector", log.ERROR, err)
end
]]
net.dostring_in('mission', mission_string)
-- After executing the code, send back a "message received" response
local send_ok, send_err = code_injector_client:send("MSG_OK\n")
if not send_ok then
log.write("DCS Code Injector", log.ERROR, "Error sending confirmation: " .. tostring(send_err))
end
end
-- Close the client connection
code_injector_client:close()
else
return
end
end
function handler.onSimulationStop()
log.write("DCS Code Injector", log.INFO, "Simulation stopped, closing connection")
end
DCS.setUserCallbacks(handler)
log.write("DCS Code Injector", log.INFO, "Load OK!")
end
local ok, err = pcall(init)
if not ok then
log.write("DCS Code Injector", log.ERROR, "Error loading Code Injector: " .. tostring(err))
end