ldd-CC/netrequire.lua

55 lines
1.5 KiB
Lua
Raw Normal View History

2019-05-26 01:56:30 +00:00
local function netrequire(_name, alwaysDownload, ...)
assert(type(_name) == "string", "API name must be a string")
local DL_path = ".netrequire_storage"
local name
2019-05-26 02:06:52 +00:00
if _name:sub(-4, -1) == ".lua" then
name = _name:sub(1, -5)
2019-05-26 01:56:30 +00:00
else
name = _name
end
if (not alwaysDownload) and fs.exists(fs.combine(DL_path .. "/require", name)) then
2019-05-26 03:00:37 +00:00
return loadfile(fs.combine(DL_path .. "/require", name))(...)
elseif (not alwaysDownload) and fs.exists(fs.combine(DL_path .. "/loadAPI", name)) then
2019-05-26 03:00:37 +00:00
os.loadAPI(fs.combine(DL_path .. "/loadAPI", name))
return _ENV[fs.getName(name)]
else
2019-05-26 01:56:30 +00:00
local url = "https://github.com/LDDestroier/CC/raw/master/netrequire/" .. name
local net = http.get(url)
2019-11-03 19:44:51 +00:00
local description, creator
2019-05-26 01:56:30 +00:00
if net then
2019-05-26 02:05:13 +00:00
url = net.readLine()
2019-05-26 03:00:37 +00:00
local useLoadAPI = net.readLine():sub(1, 4) == "true"
2019-11-03 19:44:51 +00:00
creator = net.readLine()
2019-11-03 19:44:16 +00:00
description = net.readAll()
2019-05-26 01:56:30 +00:00
net.close()
2019-05-26 02:05:13 +00:00
net = http.get(url)
if net then
local contents = net.readAll()
net.close()
2019-05-26 03:00:37 +00:00
if useLoadAPI then
local file = fs.open(fs.combine(DL_path .. "/loadAPI", name), "w")
file.write(contents)
file.close()
os.loadAPI(fs.combine(DL_path .. "/loadAPI", name))
return _ENV[fs.getName(name)]
else
local file = fs.open(fs.combine(DL_path .. "/require", name), "w")
file.write(contents)
file.close()
return loadstring(contents)(...)
end
2019-05-26 02:05:13 +00:00
else
error("Couldn't connect to '" .. url .. "'")
end
2019-05-26 01:56:30 +00:00
else
error("Cannot find any such API '" .. name .. "'")
end
end
end
return netrequire