Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Загрузчик библиотек с rep.computercraft.ru/cat/libs v0.1
- ]]
- local component = require "component"
- local table = require "table"
- local fs = require "filesystem"
- local io = require "io"
- local global = require "_G"
- local invoke = component.invoke
- local internet = component.list("internet")()
- local site
- global.package.libs = global.package.libs or setmetatable({}, {__mode = "v"})
- local function findin_table(name)
- local lib = global.package.libs[name]
- if lib then
- return lib
- else
- return nil, "\tno field package.libs['"..name.."']"
- end
- end
- local function findin_path(name)
- local error_mes = {}
- for path in package.path:gmatch "([^;]+)" do
- path = path:gsub("?", name)
- if fs.exists(path) then
- ok, err = pcall(dofile, path)
- if ok then
- return err
- else
- error(path..":"..err, 3)
- end
- else
- error_mes[#error_mes+1] = "\tno file '"..path.."'"
- end
- end
- return nil, table.concat(error_mes, "\n")
- end
- local function findin_rep(name)
- local internet = component.list("internet")()
- if not internet then
- return nil, "\tinternet component not found"
- end
- if not site then
- site = {}
- local host, err = invoke(internet, 'request', "http://rep.computercraft.ru/cat/libs/", 80)
- if not host then
- return nil, "\tconection error: "..err
- end
- while host:read() do
- site[#site+1] = host:read()
- end
- site = table.concat(site)
- host:close()
- end
- local saveName = name:gsub("%%", "%%"):gsub("%.", "%.")
- for text in site:gmatch[[<div class="panel panel%-default">.-<div class="panel%-body">(.-)</div>]] do
- if text:match("<li><b>Название:</b> "..saveName.." </li>") then
- local host, err = invoke(internet, 'request', text:match[[a href=.-"(.-)."]]or"", 80)
- if not host then
- return nil, "\tconection error: "..err
- end
- local text = host:read()
- while text do
- text = host:read()
- local s = text:match([[http://pastebin.com/(........)]])
- if s then
- text = s
- break
- end
- end
- host:close()
- host, err = invoke(internet, 'request', "http://pastebin.com/raw.php?i="..text, 80)
- if not host then
- return nil, "\tconection error: "..err
- end
- if not fs.exists("/usr/lib") then
- fs.makeDirectory("/usr/lib")
- end
- local file, err = io.open("/usr/lib/"..name..".lua", "w")
- if not file then
- return nil, "\t error while recording: "..err
- end
- while host:read() do
- file:write(host:read()or"")
- file:flush()
- end
- file:close()
- return dofile("/usr/lib/"..name..".lua")
- end
- end
- return nil, "\tno library '"..name.."' in repository"
- end
- global.getlib = function(...)
- local args, result = table.pack(...), {}
- for i = 1, args.n do
- local name, lib = args[i]
- checkArg(i, name, "string")
- local error_mes = {"lib #"..i.." '"..name.."' not found:"}
- lib, error_mes[#error_mes+1] = findin_table(name)
- if not lib then
- lib, error_mes[#error_mes+1] = findin_path(name)
- end
- if not lib then
- lib, error_mes[#error_mes+1] = findin_rep(name)
- end
- if lib then
- result[i] = lib
- global.package.libs[name] = lib
- else
- site = nil
- error(table.concat(error_mes, "\n"), 2)
- end
- end
- site = nil
- return table.unpack(result)
- end
Advertisement
Add Comment
Please, Sign In to add comment