fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosReskinned the settings panel Added version number to the settings panel7d1f500

settings-overhaul
3.8 KiB125 linesraw
1-- This code is dedicated to the public domain. 2017, Moritz Neikes
2
3local http = require("socket.https")
4
5-- these two channels are used to send requests and progress updates back
6-- and forth
7local requests_channel = love.thread.getChannel("dl-requests")
8local progress_channel = love.thread.getChannel("dl-progress")
9
10-- this is a re-implementation of the ltn12 file sink that uses love.filesystem
11-- instead of Lua's io functions
12local function love_file_sink(filename)
13  -- create enclosing directories
14  local dirname = string.gmatch(filename, "(.*/)([^/]*)")()
15  if dirname then
16    local dir_ok = love.filesystem.createDirectory(dirname)
17    if not dir_ok then error("Could not create directory "..dirname) end
18  end
19
20  local file = love.filesystem.newFile(filename)
21  local ok, err = file:open("w")
22  if not ok then error(err) end
23
24  return function(chunk, err)
25    if chunk then
26      local ok, err = file:write(chunk, #chunk)
27      if not ok then return nil, err end
28    else
29      file:close()
30      return nil, err
31    end
32
33    -- indicate that we accept more data
34    return true
35  end
36end
37
38-- this is a special "sink" that sends progress updates whenever it receives
39-- a new chunk of data.
40-- See http://lua-users.org/wiki/FiltersSourcesAndSinks for a bit of documentation
41local function progress_sink(output_sink)
42  return function(chunk, err)
43    if chunk then
44      progress_channel:push({chunk = #chunk})
45    else
46
47      -- if chunk is nil, then the download is finished, or we have an error
48      if err then
49        progress_channel:push({error = err})
50      else
51        progress_channel:push({finished = true})
52      end
53
54    end
55
56    -- forward chunk and err to the underlying sink
57    return output_sink(chunk, err)
58  end
59end
60
61while true do
62
63  -- wait for a request to arrive
64  local request = requests_channel:demand()
65
66  -- extract the url and output filename
67  local url, filename = request.url, request.filename
68
69  -- we need to send two separate requests: one to determine the size
70  -- of the thing we are downloading, and another one to actually download
71  -- something.
72
73  local proceed
74  do
75    local request = {
76      url = url,
77      method = "HEAD", -- this only retrieves the header, which contains the
78                       -- size that we're interested in.
79    }
80    local success, status_code, response_header = http.request(request)
81    if success then
82      if status_code == 200 then
83        file_size = response_header["content-length"]
84        progress_channel:push({file_size = tonumber(response_header["content-length"])})
85        -- we only proceed to sending a second request when this worked fine.
86        proceed = true
87      else
88        progress_channel:push({error = string.format("Received status code %d",
89                                                     tonumber(status_code))})
90      end
91    else
92      -- in case of failure, the error message is stored in the second return value
93      local err = status_code
94      progress_channel:push({error = err})
95    end
96  end
97
98  -- now we start the actual download
99  if proceed then
100    -- this is the sink that will capture the downloaded data.
101    local output_sink = love_file_sink(filename)
102
103    -- this time we send an actual GET request.
104    -- note also that we use our custom sink to capture the downloaded data.
105    local request = {
106      url = url,
107      sink = progress_sink(output_sink),
108      method = "GET",
109    }
110
111    local success, status_code = http.request(request)
112
113    if success then
114      if status_code ~= 200 then
115        progress_channel:push({error = string.format("Received status code %d",
116                                                     tonumber(status_code))})
117      end
118    else
119      -- in case of failure, the error message is stored in the second return value
120      local err = status_code
121      progress_channel:push({error = err})
122    end
123  end
124
125end