112th_Rossi Posted November 2, 2015 Posted November 2, 2015 I'm writing a service in C# that can dynamically generate LUA code. Is there an entry point in DCS where I can inject LUA and have it execute? Thanks
FSFIan Posted November 2, 2015 Posted November 2, 2015 Yes, this is possible. I assume you want the mission scripting environment, in that case take a look at Scripts\MissionScripting.lua in your DCS: World installation directory. Have it execute a script file before all the fun stuff (require, lfs, io, etc.) gets removed from the environment to protect the user from malicious mission files. In your script file you can use LuaSocket to spin up a server that accepts lua snippets to execute. Make sure that your script does not expose any fun stuff to the global mission environment (grab what you need and stuff it into file-local variables). The source code of DCS: Witchcraft (link in my sig) provides an example. In Witchcraft, I have DCS connect as a client to the external program that provides the server because I didn't want to think about concurrent connections in Lua when I wrote it. For an example of a TCP server in Lua, look at the DCS-BIOS source code. What does your external service do? DCS-BIOS | How to export CMSP, RWR, etc. through MonitorSetup.lua
112th_Rossi Posted November 2, 2015 Author Posted November 2, 2015 I'm looking at collecting the game state data, position of units etc and evaluating it to generate tasks then sending the lua script to the game in real time. It would occasionally poll the game world and work out what tasks to assign. In a very broad sense. I assume I can get information back from the dcs server? Such as enumerations of units in game and their states etc? Thanks for the info.
Durham Posted January 13, 2017 Posted January 13, 2017 Tried to do this in a script file. Got the client and server communicating with the remote machine fine - sending mist.DBs.units in JSON to server and then taking messages back which I printed to screen using action.outText(string), which I then converted into lua injection. One issue: Using the witchcraft.lua as a template, I had above the loop: tcpClient:settimeout(0.0001) and in the loop: local line, err = tcpClient:receive() if err then trigger.action.outText("Error receiving message from server!\n", 2) else trigger.action.outText("Message from server:"..line.."\n", 5) end If there was no message from the server, then the game hung for the length of the timeout. If I pinged the client with repetitive [0/enter] then no problem. I looked at multi-threading with:- function receive (connection) connection:timeout(0) -- do not block local s, status = connection:receive(2^10) if status == "timeout" then coroutine.yield(connection) end return s, status end but didn't make any progress. At present, my solution to this issue will be to send a 0 each cycle from the server and run a if client:receive ~=0 then do stuff end which seems really inefficient, but will work smoothly. I got the lua injection to work with f = loadstring(string)(), which has no error handling which can be added later. Do you have a solution to the stuttering on the receive() function, other than regularly sending a message from the server? Many thanks Durham
FSFIan Posted January 13, 2017 Posted January 13, 2017 If there was no message from the server, then the game hung for the length of the timeout. Have you tried simply setting the timeout to zero? And how are you noticing a hang of a tenth of a millisecond anyway? I looked at multi-threading with:- function receive (connection) connection:timeout(0) -- do not block local s, status = connection:receive(2^10) if status == "timeout" then coroutine.yield(connection) end return s, status end but didn't make any progress. I can't tell why that doesn't work without looking at your complete script, but you should be aware that coroutines have nothing to do with multithreading. At any given time, at most one coroutine is executing. DCS-BIOS | How to export CMSP, RWR, etc. through MonitorSetup.lua
FlightControl Posted January 13, 2017 Posted January 13, 2017 Yes, but it is disabled by default in dcs to prevent other embed malicious code in their missions. On the server side however, there are more possibilities. Modify the missionscripting.lua file. Erase the sanitisation of io and os modules. And you have a lua function called os.execute.... Check here... http://www.lua.org/manual/5.1/manual.html#pdf-os.execute [TABLE][sIGPIC][/sIGPIC]| Join MOOSE community on: DISCORD :thumbup: Website of the MOOSE LUA Framework. MOOSE framework Downloads. Check out Example Missions to try out and learn. MOOSE YouTube Channel for live demonstrations and tutorials. [/TABLE]
Pikey Posted January 13, 2017 Posted January 13, 2017 Yes there's an easier way. put a script on your desktop. Call it continuously using the Asset and loadfile. ___________________________________________________________________________ SIMPLE SCENERY SAVING * SIMPLE GROUP SAVING * SIMPLE STATIC SAVING *
Recommended Posts