-
Notifications
You must be signed in to change notification settings - Fork 1
/
DCS-LuaConnector-hook.lua
188 lines (160 loc) · 5.37 KB
/
DCS-LuaConnector-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
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
local logName = 'DCS.Lua.Connector'
local function logWrite(name, level, message)
if log.write ~= nil then
log.write(name, level, message)
else
net.log("["..name.."] "..message)
end
end
logWrite(logName, log.INFO, "Loading")
local function runSnippetIn(env, code)
local resultStringCode = [[
local function serialize(svalue)
local seenTables = {}
local retlist = {}
local indentLevel = 0
local function serializeRecursive(value)
if type(value) == "string" then return table.insert(retlist, string.format("%q", value)) end
if type(value) ~= "table" then return table.insert(retlist, tostring(value)) end
if seenTables[value] == true then
table.insert(retlist, tostring(value))
return
end
seenTables[value] = true
-- we have a table, iterate over the keys
table.insert(retlist, "{\n")
indentLevel = indentLevel + 4
for k, v in pairs(value) do
table.insert(retlist, string.rep(" ", indentLevel).."[")
if type(k) == "table" then
table.insert(retlist, tostring(k))
else
serializeRecursive(k)
end
table.insert(retlist, "] = ")
serializeRecursive(v)
table.insert(retlist, ",\n")
end
indentLevel = indentLevel - 4
table.insert(retlist, string.rep(" ", indentLevel).."}")
end
serializeRecursive(svalue, " ")
return table.concat(retlist)
end
local function evalAndSerializeResult(code)
local success = false
local result = ""
local retstatus = ""
local f, error_msg = loadstring(code, "Lua Console Snippet")
if f then
--setfenv(f, _G)
success, result = pcall(f)
if success then
retstatus="success"
result = serialize(result)
else
retstatus = "runtimeError"
result = tostring(result)
end
else
retstatus = "syntaxError"
result = tostring(error_msg)
end
return retstatus.."\n"..result
end
]].."return evalAndSerializeResult("..string.format("%q", code)..")"
local result = nil
local success = nil
if env == "gui" then
result = loadstring(resultStringCode)()
success = true
else
result, success = net.dostring_in(env, resultStringCode)
--logWrite("Lua Console", log.INFO, "l94: success="..tostring(success))
end
if not success then
result = "dostringError\n"..tostring(env).."\n"..tostring(code).."\n"..tostring(result)
end
local firstNewlinePos = string.find(result, "\n")
--logWrite("Lua Console", log.INFO, "firstnewlinepos="..tostring(firstNewlinePos))
local result_str = string.sub(result, firstNewlinePos+1)
local status_str = string.sub(result, 1, firstNewlinePos-1)
return result_str, status_str
end
package.path = package.path..";.\\LuaSocket\\?.lua"
package.cpath = package.cpath..";.\\LuaSocket\\?.dll"
local JSON = loadfile("Scripts\\JSON.lua")()
local socket = require("socket")
local dcsBiosLuaConsole = {}
dcsBiosLuaConsole.host = "127.0.0.1"
dcsBiosLuaConsole.listenPort = 5000
dcsBiosLuaConsole.sendPort = dcsBiosLuaConsole.listenPort + 1
dcsBiosLuaConsole.conn = socket.udp()
dcsBiosLuaConsole.conn:setsockname(dcsBiosLuaConsole.host, dcsBiosLuaConsole.listenPort)
dcsBiosLuaConsole.conn:settimeout(0)
local function step()
local line, err = dcsBiosLuaConsole.conn:receive()
if line then
logWrite(logName, log.INFO, DCS.getRealTime().." UDP Received: "..tostring(line))
elseif err then
if (err ~= "timeout") then
logWrite(logName, log.INFO, DCS.getRealTime().." UDP Error: "..tostring(err))
end
return
else
logWrite(logName, log.INFO, DCS.getRealTime().." UDP Nothing Received")
return
end
local message = JSON:decode(line)
-- Construct response message
local response_msg = {}
response_msg.id = message.id
local result, status
-- Run lua command
if message.type == 'ping' then
status = 'success'
result = 'pong'
response_msg.type = "ping"
elseif message.type == 'command' then
result, status = runSnippetIn(message.luaEnv, message.code)
response_msg.type = "luaResult"
end
response_msg.result = tostring(result)
response_msg.status = tostring(status)
local response_string = ""
local function encode_response()
response_string = JSON:encode(response_msg):gsub("\n","").."\n"
end
local success, result = pcall(encode_response)
if not success then
response_msg.status = "encodeResponseError"
response_msg.result = tostring(result)
encode_response()
end
logWrite(logName, log.INFO, DCS.getRealTime().." UDP Send: "..response_string.." to "..dcsBiosLuaConsole.host..':'..dcsBiosLuaConsole.sendPort)
dcsBiosLuaConsole.conn:sendto(response_string, dcsBiosLuaConsole.host, dcsBiosLuaConsole.sendPort)
end
local callbacks = {}
function callbacks.onSimulationFrame()
status, err = pcall(step)
if not status then
logWrite(logName, log.INFO, "onSimulationFrame Error: "..tostring(err))
end
end
function callbacks.onMissionLoadBegin()
logWrite(logName, log.INFO, "onMissionLoadBegin")
end
function callbacks.onMissionLoadProgress(progress, message)
-- logWrite("DCS.Lua.Connector", log.INFO, "onMissionLoadProgress - "..progress.." - "..message)
end
function callbacks.onMissionLoadEnd()
logWrite(logName, log.INFO, "onMissionLoadEnd")
end
function callbacks.onSimulationStart()
logWrite(logName, log.INFO, "onSimulationStart")
end
function callbacks.onSimulationStop()
logWrite(logName, log.INFO, "onSimulationStop")
end
DCS.setUserCallbacks(callbacks)
logWrite(logName, log.INFO, "Loaded Successfully")