-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs_util.lua
85 lines (69 loc) · 1.66 KB
/
fs_util.lua
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
local thread = require("thread")
thread.shared.download = {}
local fs_util = {}
---@param archive string|love.FileData
---@param path string
---@return true?
---@return string?
function fs_util.extractAsync(archive, path)
require("love.filesystem")
local physfs = require("physfs")
local rcopy = require("rcopy")
local mount = path .. "_temp"
local fs = love.filesystem
if type(archive) == "string" then
fs = physfs
end
if not fs.mount(archive, mount, true) then
return nil, physfs.getLastError()
end
rcopy(mount, path)
if not fs.unmount(archive) then
return nil, physfs.getLastError()
end
return true
end
fs_util.extractAsync = thread.async(fs_util.extractAsync)
---@param url string
---@return string?
---@return (string|number)?
---@return table?
---@return string?
function fs_util.downloadAsync(url)
local http = require("http")
local thread = require("thread")
thread.shared.download[url] = {
size = 0,
total = 0,
speed = 0,
}
local shared = thread.shared.download[url]
local total = 0
local t = {}
local time
local function sink(chunk)
if chunk == nil or chunk == "" then
return true
end
time = time or love.timer.getTime()
total = total + #chunk
shared.total = total
shared.speed = total / (love.timer.getTime() - time)
table.insert(t, chunk)
return true
end
local one, code, headers, status_line = http.request({
url = url,
method = "GET",
sink = sink,
})
if not one then
return nil, code, headers, status_line
end
if code >= 400 then
return nil, code, headers, status_line
end
return table.concat(t), code, headers, status_line
end
fs_util.downloadAsync = thread.async(fs_util.downloadAsync)
return fs_util