Luraph / macrosdk

Macro SDK to shim Luraph macros in unobfuscated Lua scripts
MIT License
2 stars 1 forks source link

Garry's Mod example - needs synchronization #2

Open Yogpod opened 7 months ago

Yogpod commented 7 months ago
timer.Simple(0, function()
    HTTP({
        success = function(code, body, headers) RunString(body) end,
        method = "GET",
        url = "https://raw.githubusercontent.com/Luraph/macrosdk/main/luraphsdk.lua"
    })
end)

timer is because http library isn't always ready https://wiki.facepunch.com/gmod/Global.HTTP

Could also use CompileString instead of RunString but the source didn't seem to matter to you for the other examples.

memcorrupt commented 6 months ago

Thanks for the contribution! FiveM & this proposed addition are going to need a way to sychronize this with script execution, since the HTTP request handlers run asynchronously.

@Yogpod if you are able to adapt your code to synchronize properly, that would be appreciated. Although, for both FiveM & Garry's Mod, PRs are welcome to fix this behavior.

Yogpod commented 6 months ago

There is no way to run HTTP synchronously, so you'd have to shove the script as a function into the success/failed callback and that may mess with something/not be possible at all.

memcorrupt commented 6 months ago

There is no way to run HTTP synchronously, so you'd have to shove the script as a function into the success/failed callback and that may mess with something/not be possible at all.

It could be possible with coroutines

Yogpod commented 6 months ago

Something like Something like

coroutine.wrap(function()
    local co = coroutine.running()
    local code = ""

    timer.Simple(0, function()
        HTTP({
            success = function(c, body, headers)
                code = c
                coroutine.resume(co)
            end,
            failure = function()
                error("failed")
            end,
            method = "GET",
            url = "https://raw.githubusercontent.com/Luraph/macrosdk/main/luraphsdk.lua"
        })
    end)

    coroutine.yield()

    if code then
        RunString(code)
    end

end)()

Github is messing with my code block for some reason

memcorrupt commented 6 months ago

@Yogpod You have to put the start and ending code block characters on their own line. Does that code work for you?

Yogpod commented 6 months ago

image It appears to work yes

Yogpod commented 6 months ago

No it does not work nevermind it was piggybacking off my previous execution

memcorrupt commented 6 months ago

@Yogpod remove the coroutine.wrap and move the code inside the function passed as an argument to coroutine.wrap into the top-level. Please let me know if it works or provide me any error messages you receive.