Lua Socket (aka libraries in OpenXRay)

User avatar
Gazprom
Scavenger
Posts: 14
Joined: 25 Jul 2017, 03:46

Lua Socket (aka libraries in OpenXRay)

Postby Gazprom » 25 Jul 2017, 04:42

Hi everyone,
I'm working on the second version of http://www.moddb.com/mods/call-of-chernobyl/addons/stal-kpk
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)

User avatar
Alundaio
S.T.A.L.K.E.R.
Posts: 1368
Joined: 26 May 2012, 22:26

Re: Lua Socket (aka libraries in OpenXRay)

Postby Alundaio » 30 Jul 2017, 13:51

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: https://luapower.com

You can download socket from here: https://luapower.com/socket/download

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("http://coc.epicstalker.com/patches/Patch_Notes.txt")
   printf("b=%s",b)


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


If you are having trouble with the steps, just download this: https://drive.google.com/file/d/0B0zCay ... sp=sharing
Place it into bin and it should just work.
"I have a dream that one day this community will rise up and live out the true meaning of its creed: "We hold these truths to be self-evident; that all mods are created equal."

User avatar
Gazprom
Scavenger
Posts: 14
Joined: 25 Jul 2017, 03:46

Re: Lua Socket (aka libraries in OpenXRay)

Postby Gazprom » 30 Jul 2017, 16:42

Got it!
Thank you Alundaio!

User avatar
Gazprom
Scavenger
Posts: 14
Joined: 25 Jul 2017, 03:46

Re: Lua Socket (aka libraries in OpenXRay)

Postby Gazprom » 31 Jul 2017, 07:59

Another question:
I'm trying to send POST data through a GET-similar scheme, so my data is
"param1=value1&param2=value2"

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 = "http://www.myserver.com/page.php",
        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)
end


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.

User avatar
Gazprom
Scavenger
Posts: 14
Joined: 25 Jul 2017, 03:46

Re: Lua Socket (aka libraries in OpenXRay)

Postby Gazprom » 31 Jul 2017, 12:18

Actually nevermind. There was a miscommunication with the server, we used two different methods. Everything's fine

User avatar
Alundaio
S.T.A.L.K.E.R.
Posts: 1368
Joined: 26 May 2012, 22:26

Re: Lua Socket (aka libraries in OpenXRay)

Postby Alundaio » 27 Aug 2017, 20:42

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 (db.actor) then
      return
   end
   if (dtimer and time_global() < dtimer) then
      return
   end
   dtimer = time_global() + 5000
   
    local USERNAMEVALUE
   local PASSWORDVALUE
   local input = io.open( INPATH, "r" )
   if input ~= nil then
       local fileText = input:read( "*a" )
       for line in fileText:gmatch( "[^\n]+" ) do
           USERNAMEVALUE, PASSWORDVALUE, triggerXRay = line:match( "([^/]+)/([^/]+)/(.+)" )
       end
    end
    if triggerXRay == "true" then
       if (is_empty(xr_combat_ignore.fighting_with_actor_npcs)) then
            ReceiveMessages(1,5)
       end
   end   
   --CreateTimeEvent("MessageUpdate","Continuous",5,MessageEvaluator)
   
   history_ini:save()
   return false   
end


Code: Select all

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



And I gave myself a chat history ltx

Code: Select all

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


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")
"I have a dream that one day this community will rise up and live out the true meaning of its creed: "We hold these truths to be self-evident; that all mods are created equal."

User avatar
Gazprom
Scavenger
Posts: 14
Joined: 25 Jul 2017, 03:46

Re: Lua Socket (aka libraries in OpenXRay)

Postby Gazprom » 30 Aug 2017, 05:17

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.

User avatar
Alundaio
S.T.A.L.K.E.R.
Posts: 1368
Joined: 26 May 2012, 22:26

Re: Lua Socket (aka libraries in OpenXRay)

Postby Alundaio » 02 Sep 2017, 23:24

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.
"I have a dream that one day this community will rise up and live out the true meaning of its creed: "We hold these truths to be self-evident; that all mods are created equal."

User avatar
Gazprom
Scavenger
Posts: 14
Joined: 25 Jul 2017, 03:46

Re: Lua problem

Postby Gazprom » 04 Sep 2017, 04:22

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.


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest

cron