Page 1 of 1

Lua Socket (aka libraries in OpenXRay)

Posted: 25 Jul 2017, 04:42
by Gazprom
Hi everyone,
I'm working on the second version of
Fortunately a guy kindly offered his time for realizing a mobile app that does pretty much everything that the one for Fallout 4 does: dynamic maps and mission display on top of all.
However, instead of using an IRC channel as basis, we decided to use a dedicated connection (TCP or HTTP)
The client (especially the TCP one) is very, very simple to code and use, but I don't have any possibility to execute it inside S.T.A.L.K.E.R., as it doesn't include the libraries required (Lua Socket manages everything related to internet).
I looked into the unpacked, vanilla CoP multiplayer scripts for some tips, but there wasn't anything related to internet at all.
Is there any chance we'll see those libraries into the engine? Maybe instead of building them directly we can place .dlls/.lua files on a hypothetical libs directory where the engine can parse them when it's started.
Otherwise, how can I run those scripts outside S.T.A.L.K.E.R.?
Running them from inside CoC with os.execute lua.exe and myscript.lua as a "parameter" has proven wrong, so I'm out of options.
(As it was both a suggestion and a help request, I didn't felt right to post this in the "Suggestions" topic)

Re: Lua Socket (aka libraries in OpenXRay)

Posted: 30 Jul 2017, 13:51
by Alundaio
Since CoC uses LuaJIT-2, that means you can use FFI library to use socket and mime dlls, without having it hardcoded into the lua binary.

local ffi = require("ffi")

FFI lets you use any DLL written in C. This means you can use just about anything found here:

You can download socket from here:

These are just libraries that already have FFI bindings.

You need to take the socket and mime folders from bin/mingw32 and put them in stalker bin folder
You need to create a lua folder in stalker bin folder. Put socket folder, with the *.lua in this folder. Put socket.lua, mime.lua and ltn12.lua into this folder, also.

Code: Select all

   local ffi = require("ffi")
   local http = require("socket.http")
   local b, c, h = http.request("")

With this example you should see in the log the CoC patch notes!

If you are having trouble with the steps, just download this: ... sp=sharing
Place it into bin and it should just work.

Re: Lua Socket (aka libraries in OpenXRay)

Posted: 30 Jul 2017, 16:42
by Gazprom
Got it!
Thank you Alundaio!

Re: Lua Socket (aka libraries in OpenXRay)

Posted: 31 Jul 2017, 07:59
by Gazprom
Another question:
I'm trying to send POST data through a GET-similar scheme, so my data is

So my script is:

Code: Select all

local ffi = require("ffi")
local http = require("socket.http")
local ltn12 = require("ltn12")

function OutputManager()
    local reqbody = "param1=value1&param2=value2"
    local respbody = {}
    local  body, code, headers, status = http.request {
        method = "POST",
        url = "",
        source = ltn12.source.string(reqbody),
        headers =
                        ["Accept"] = "*/*",
                        ["Accept-Encoding"] = "gzip, deflate",
                        ["Accept-Language"] = "en-us",
                        ["Content-Type"] = "application/x-www-form-urlencoded",
                        ["Content-Length"] = string.len(reqbody)
        sink = ltn12.sink.table(respbody)
    respbody = table.concat(respbody)

However when I call by keypress my OutputManager function the game appears to proceed, but when I check in the server index if something is received nothing appears.
The libraries are correctly configured, but for all this morning I've tried to send that POST, with no results.

Re: Lua Socket (aka libraries in OpenXRay)

Posted: 31 Jul 2017, 12:18
by Gazprom
Actually nevermind. There was a miscommunication with the server, we used two different methods. Everything's fine

Re: Lua Socket (aka libraries in OpenXRay)

Posted: 27 Aug 2017, 20:42
by Alundaio
I'm thinking that now it would be better if this was a separate background process that launched after you logged in, it could log messages to a .ltx and every few seconds you check that in-game. It would probably eliminate the lag.

EDIT: replacing the CreateTimeEvent with AddUniqueCall seems to fair a bit better.

Code: Select all

local dtimer
function MessageEvaluator()
   if not ( then
   if (dtimer and time_global() < dtimer) then
   dtimer = time_global() + 5000
   local input = INPATH, "r" )
   if input ~= nil then
       local fileText = input:read( "*a" )
       for line in fileText:gmatch( "[^\n]+" ) do
           USERNAMEVALUE, PASSWORDVALUE, triggerXRay = line:match( "([^/]+)/([^/]+)/(.+)" )
    if triggerXRay == "true" then
       if (is_empty(xr_combat_ignore.fighting_with_actor_npcs)) then
   return false   

Code: Select all

function on_actor_update()
   -- CreateTimeEvent("MessageUpdate","Continuous",5,MessageEvaluator)

And I gave myself a chat history ltx

Code: Select all

    if contenttext ~= LASTID and contenttext ~= nil then, contenttext, "ui_inGame2_Istoriya_dolga", 0, 6000)
        LASTID = contenttext
      history_ini:w_value("history",sendername,'"'"%c").." | " .. contenttext .. '"')

local history_ini = ini_file_ex("message_history.ltx",true)

You might find that useful, because you can add a History tab, and then display the history in a list box using local t = history_ini:collect_section("history")

Re: Lua Socket (aka libraries in OpenXRay)

Posted: 30 Aug 2017, 05:17
by Gazprom
Thank you for all of these suggestions.
At this time, we are really considering to move on a separated script for downloading everything, otherwise with all the things it would handle from now on it would get somewhat laggy.
AXRToolset employes luajit-2, right? Doing the same trick for S.T.A.L.K.E.R./bin on a new Autohotkey executable should do the game also for this one.
In this way a player shouldn't even install Lua for executing the script.

Re: Lua Socket (aka libraries in OpenXRay)

Posted: 02 Sep 2017, 23:24
by Alundaio
If you compile luajit-2 for x86, you can use the luajit.exe right from stalker bin folder to execute a lua script. You would need a while loop to keep it active, not sure though how you would handle closing it or whatever.

If you use the AHK+luajit, it will be detected as virus by some AV (it's not obviously). But it will at least let you detect xrEngine process, so maybe this is a solution.

You could have the game launch the ahk app. Then the ahk app can do all the http work, writing new messages to a file, it can check if xrEngine process exists, if it doesn't, exit.

You can probably just gut AXRToolset, and use it.

Re: Lua problem

Posted: 04 Sep 2017, 04:22
by Gazprom
Thank you for your response, but we coded an .exe for doing what we want.
However I have some troubles with the new reading function.
The .exe checks every second if a login command appears, reports back the response of the POST request and then starts searching for every other request (message update, leaderboard update and so on)
The problem is that no POST response shows up in game, even if I have made the script for checking continuously in a file where the program writes.
I still have no clues, if you want to take a peek on this and help me out, it would be really appreciated.

EDIT: Problem solved, I made an AddUniqueCall for the function I needed to launch.