Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use "socket.http" to make HTTP calls #479

Open
yethranayeh opened this issue Dec 1, 2024 · 0 comments
Open

Can't use "socket.http" to make HTTP calls #479

yethranayeh opened this issue Dec 1, 2024 · 0 comments

Comments

@yethranayeh
Copy link

I have attempted solutions to common problems

Yes

I have checked that my game version is supported

Yes

OS

Windows 10

Platform

Steam

Description

I must preface this by saying that I am new to modding and I have not written a Lua script before.

So, I wanted to make a mod that would connect game events and state to SteelSeries Game Engine. To be able to manipulate my keyboard lights, I have to send HTTP requests to the local server run by SteelSeries GG app.

I tried an example Lua code that seems to be working fine by itself when run with Lua.exe.
However, after I pack this mod with LSLib and import it with BG3MM, I can see that my script runs because the debug print line appears on BG3SE console, but then it throws this error:

Loading bootstrap script: Mods/SteelSeriesLights/ScriptExtender/Lua/BootstrapServer.lua
bg3se::ExtensionStateBase::LuaLoadGameFile(): Script file could not be opened: Mods/SteelSeriesLights/ScriptExtender/Lua/socket.http
bg3se::lua::State::LoadScript(): Failed to execute script: [string "SteelSeriesLights/Server/lights.lua"]:4: attempt to index a nil value (local 'http')
stack traceback:
        SteelSeriesLights/Server/lights.lua:4: in main chunk
        [C++ Code]: in field 'Include'
        builtin://Libs/FileLoader.lua:34: in function <builtin://Libs/FileLoader.lua:6>
        (...tail calls...)
        SteelSeriesLights/BootstrapServer.lua:1: in main chunk
        [C++ Code]: in field 'Include'
        builtin://Libs/ModLoader.lua:68: in method 'LoadBootstrap'
        builtin://BuiltinLibrary.lua:26: in function <builtin://BuiltinLibrary.lua:25>

Indications

BG3SE treats a default library (socket.http) as a custom mod script when calling LuaLoadGameFile: bg3se::ExtensionStateBase::LuaLoadGameFile()

Diagnostic Files

No response

Steps to Reproduce

  1. Create a bootstrap script for the mod: Mods\SteelSeriesLights\ScriptExtender\Lua\BootstrapServer.lua
Ext.Require("Server/lights.lua")
print(" ")
print(" ")
print(" ")
print(" ")
print("SteelSeriesLights::BOOTSTRAP")
  1. Create custom script that will be imported by BootstrapServer.lua:
local http = require("socket.http")

-- Perform the GET request
local response_body, status_code, response_headers = http.request("https://jsonplaceholder.typicode.com/todos/1")

-- Print the response
print("Status Code:", status_code)           -- Similar to `response.status`
print("Response Body:", response_body)       -- Similar to `response.text()`

local function parse_json(json_str)
    local result = {}
    for key, value in json_str:gmatch('"([^"]+)":%s*("?.-["}]?)%s*[,%}]') do
        -- Remove quotes from keys and values if present
        key = key:gsub('"', '')
        if value:match('^".*"$') then
            value = value:sub(2, -2) -- Remove quotes for strings
        elseif value == "true" then
            value = true
        elseif value == "false" then
            value = false
        elseif tonumber(value) then
            value = tonumber(value)
        end
        result[key] = value
    end
    return result
end

-- Parse the JSON response
local data = parse_json(response_body)

-- Access the parsed data
print("Title:", data.title)
print("Completed:", data.completed)
  1. Pack your mod with LSLib
  2. Import mod with BG3MM
  3. Load into the game and check BG3SE console for print statements

Expected Behavior

require should be able to import default libraries like socket.http

Actual Behavior

BG3SE tries to import socket.http as if it was another custom script within the same directory: Mods/SteelSeriesLights/ScriptExtender/Lua/socket.http

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant