Lua libcurl

  • #1, by darren-beckettWednesday, 15. March 2017, 17:58 3 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)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

    Great Poster

    351 Posts


  • #2, by darren-beckettThursday, 16. March 2017, 16:51 3 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?

    Great Poster

    351 Posts

  • #3, by SimonSFriday, 17. March 2017, 00:30 3 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")
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114

    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) 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

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

    Thread Captain

    1367 Posts

  • #4, by darren-beckettFriday, 17. March 2017, 10:44 3 years ago
    Thanks Simon

    Great Poster

    351 Posts

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

    Great Poster

    351 Posts

  • #6, by SimonSFriday, 17. March 2017, 10:56 3 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.

    Thread Captain

    1367 Posts

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

    Great Poster

    351 Posts

  • #8, by darren-beckettFriday, 17. March 2017, 14:22 3 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
    1
    2
    3
    4

    Great Poster

    351 Posts

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

    Thread Captain

    1367 Posts

  • #10, by sebastianSaturday, 18. March 2017, 20:45 3 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


    Thread Captain

    2337 Posts

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

    Great Poster

    351 Posts

Write post