Lua libcurl

  • #1, z darren-beckettWednesday, 15. March 2017, 17:58 hodinky 8 years ago
    Are you able to give an example of how to display an image loaded into memory using the new Libcurl functions?

    This example from the forum is incomplete:
    function draw()
    
    if sprite2 ~= nil then
    
    sprite.position = {x = posx, y = 0}
    
    graphics.drawSprite(sprite2)
    
    end
    
    if sprite ~= nil then
    
    graphics.drawSprite(sprite)
    
    end
    
    -- Font switch
    
    -- graphics.font = Fonts.blocked
    
    graphics.drawFont(" Test\nTest", 10, 10)
    
    end
    
    
    
    graphics.setDrawFunc("draw()")
    
    
    
    startTween("posx", posx, 300, 3000, easeQuadInOut, true, true)

    Čestný člen

    384 Posts


  • #2, z darren-beckettThursday, 16. March 2017, 16:51 hodinky 8 years ago
    Also:
    1) Is is possible to display the HTML returned from a libcurl Get statement?
    2) Can the object be clickable? - Can it open a webpage?

    Čestný člen

    384 Posts

  • #3, z SimonSFriday, 17. March 2017, 00:30 hodinky 8 years ago
    At best use this script:
    --[[
    Lua-Curl script for Visionaire
    (c) 13/6/2015
    ]]
    
    curl_multi = curl.new_multi()
    curls = {}
    handles_count = 1
    loadcount = 0
    max_loadcount = 10
    loadlist = {}
    
    function GET(url, func,param, buf, startoffset, endoffset)
     if buf~=nil then
      table.insert(curls,{curl.new(),buf, func,param})
     else
      table.insert(curls,{curl.new(),{}, func,param})
     end
        c = curls[#curls][1]
        c:setopt(curl.OPT_URL, url)
     if startoffset ~= nil then
      c:setopt(curl.OPT_RANGE, startoffset.."-"..endoffset)
     end
        c:setopt(curl.OPT_FOLLOWLOCATION, true)
        c:setopt(curl.OPT_TIMEOUT, 300)
        c:setopt(curl.OPT_IOCTLFUNCTION, function (param, buf) return #buf end)
        c:setopt(curl.OPT_HEADERFUNCTION, function (param, buf) return #buf end)
        c:setopt(curl.OPT_WRITEDATA, {curls[#curls]})
        c:setopt(curl.OPT_WRITEFUNCTION, function (param, buf)
            table.insert(param[1][2], buf) -- store a chunk of data received
            return #buf
        end)
        c:setopt(curl.OPT_PROGRESSDATA, {curls[#curls]})
        c:setopt(curl.OPT_PROGRESSFUNCTION, function(param, dltotal, dlnow)
            if param[1][4][2]~=nil then
                param[1][4][2](dtotal, dnow)
            end
        end)
        c:setopt(curl.OPT_NOPROGRESS, false) -- use this to activate progress
        curl_multi:add(c)
        curl_multi:perform()
    end
    
    function POST(url,data,func,param, buf, startoffset, endoffset, userPassword)
        if buf~=nil then
            table.insert(curls,{curl.new(),buf, func,param})
        else
            table.insert(curls,{curl.new(),{}, func,param})
        end
        c = curls[#curls][1]
        c:setopt(curl.OPT_URL, url)
        if startoffset ~= nil then
            c:setopt(curl.OPT_RANGE, startoffset.."-"..endoffset)
        end
        c:setopt(curl.OPT_FOLLOWLOCATION, true)
        c:setopt(curl.OPT_TIMEOUT, 300)
        c:setopt(curl.OPT_WRITEDATA, {curls[#curls]})
        c:setopt(curl.OPT_WRITEFUNCTION, function (param, buf)
            table.insert(param[1][2], buf) -- store a chunk of data received
            return #buf
        end)
     c:setopt(curl.OPT_POST, true)
        c:setopt(curl.OPT_POSTFIELDS, data)
        c:setopt(curl.OPT_NOPROGRESS, false) -- use this to activate progress
     c:setopt(curl.OPT_HTTPHEADER, "Content-Type: application/json")
     if userPassword ~= nil then
      c:setopt(curl.OPT_USERPWD, userPassword)
     end
        curl_multi:add(c)
        curl_multi:perform()
    end
    
    function postRequest(httpurl, data, onFinish, onProgress, userPassword)
        POST(httpurl, data, onFinish, {nil, onProgress}, nil, nil, nil, userPassword)
    end
    
    function downloadFile(httpurl, file, onFinish, onProgress)
        GET(httpurl, function(s, param) 
            local file = io.open(param[1], "wb");file:write(s);file:close() 
            onFinish() 
        end, {file, onProgress})
    end
    
    function downloadMemory(httpurl, onFinish, onProgress)
        GET(httpurl, onFinish, {nil, onProgress})
    end
    
    function update_curl()
        while loadcount < max_loadcount and #loadlist > 0 do
            loadcount = loadcount + 1 
            GET(loadlist[1][1], function(s,param) loadcount = loadcount - 1; param[2](s,param[3]) end, loadlist[1])
            table.remove(loadlist, 1)
        end
        local handles = curl_multi:perform()
        local result = curl_multi:info()
        if result ~= -1 then
            for k,v in pairs(curls) do
                if tostring(v[1]) == tostring(result) then
                    local size = v[1]:getinfo(curl.INFO_CONTENT_LENGTH_DOWNLOAD)
                    local downsize = v[1]:getinfo(curl.INFO_SIZE_DOWNLOAD)
                    if(size~=-1 and size~=downsize)then
                        print("not complete")
                        local element = table.remove(curls, k)
                    else
                        v[3](table.concat(v[2]),v[4])
                        table.remove(curls, k)
                    end
                    break
                end
            end
        end
    end
    
    registerEventHandler("mainLoop", "update_curl")

    You can then do something like this:

    downloadMemory("http://url...", function(s,code,param) 
      sprite = graphics.loadMemoryJPG(s)
    -- sprite = graphics.loadMemoryPNG(s)
    -- sprite = graphics.loadMemoryWEBP(s)
    end)
    
    downloadMemory("http://url...", function(s,code,param) 
      print(s) -- html
    end) 
    

    If you want to open a webpage, use startDefaultBrowser, may not work everywhere.

    Kapitán

    1582 Posts

  • #4, z darren-beckettFriday, 17. March 2017, 10:44 hodinky 8 years ago
    Thanks Simon

    Čestný člen

    384 Posts

  • #5, z darren-beckettFriday, 17. March 2017, 10:48 hodinky 8 years ago
    Are you able to provide a link to some documentation relating to the specific implementation of libcurl?

    Čestný člen

    384 Posts

  • #6, z SimonSFriday, 17. March 2017, 10:56 hodinky 8 years ago
    We're using this implementation: http://luacurl.luaforge.net/
    It's modified for curl_multi, so I'd recommend sticking to the script, I already did all the heavy lifting there. Also because curl is not very easy.

    Kapitán

    1582 Posts

  • #7, z darren-beckettFriday, 17. March 2017, 10:57 hodinky 8 years ago
    This is great, we shouldn't need to bug you guys so much now :-)
    Thanks again.

    Čestný člen

    384 Posts

  • #8, z darren-beckettFriday, 17. March 2017, 14:22 hodinky 8 years ago
    I can't see how to assign the downloaded picture to an objects sprite, is there no SetSprite method?



    Also, The script isn't showing correctly, the LessThan "<" sign makes it show wrongly:
    function update_curl()
       <i>--while loadcount 0 do</i>
       while loadcount < max_loadcount and #loadlist > 0 do
          loadcount = loadcount + 1

    Čestný člen

    384 Posts

  • #9, z SimonSSaturday, 18. March 2017, 19:58 hodinky 8 years ago
    You need to draw the sprite yourself like you did in the initial code:
    graphics.drawSprite(sprite)

    Kapitán

    1582 Posts

  • #10, z sebastianSaturday, 18. March 2017, 20:45 hodinky 8 years ago
    I hope there will be a tutorial for these lua lib curl stuff some time after VS5 release. 
    It seems very weird if you guys tell "you need to ..." and all the above code is a level to high for me grin 
    I could assume now that the "sprite" was the previousely fetched png/jpg/webp image from the downloadMemory() function, right?

    Often it sounds like:

    "you need to program to get a game" .... yeah... but how do i start here? razz


    Kapitán

    2346 Posts

  • #11, z darren-beckettSunday, 19. March 2017, 22:09 hodinky 8 years ago
    Don't worry Seb, you don't need to do any of this stuff to make an awesome game.

    Čestný člen

    384 Posts