Ktlo

LibLoader

Oct 24th, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. --[[
  2.     Загрузчик библиотек с rep.computercraft.ru/cat/libs v0.1
  3. ]]
  4. local component = require "component"
  5. local table = require "table"
  6. local fs = require "filesystem"
  7. local io = require "io"
  8. local global = require "_G"
  9. local invoke = component.invoke
  10. local internet = component.list("internet")()
  11. local site
  12. global.package.libs = global.package.libs or setmetatable({}, {__mode = "v"})
  13.  
  14. local function findin_table(name)
  15.     local lib = global.package.libs[name]
  16.     if lib then
  17.         return lib
  18.     else
  19.         return nil, "\tno field package.libs['"..name.."']"
  20.     end
  21. end
  22.  
  23. local function findin_path(name)
  24.     local error_mes = {}
  25.     for path in package.path:gmatch "([^;]+)" do
  26.         path = path:gsub("?", name)
  27.         if fs.exists(path) then
  28.             ok, err = pcall(dofile, path)
  29.             if ok then
  30.                 return err
  31.             else
  32.                 error(path..":"..err, 3)
  33.             end
  34.         else
  35.             error_mes[#error_mes+1] = "\tno file '"..path.."'"
  36.         end
  37.     end
  38.     return nil, table.concat(error_mes, "\n")
  39. end
  40.  
  41. local function findin_rep(name)
  42.     local internet = component.list("internet")()
  43.     if not internet then
  44.         return nil, "\tinternet component not found"
  45.     end
  46.     if not site then
  47.         site = {}
  48.         local host, err = invoke(internet, 'request', "http://rep.computercraft.ru/cat/libs/", 80)
  49.         if not host then
  50.             return nil, "\tconection error: "..err
  51.         end
  52.         while host:read() do
  53.             site[#site+1] = host:read()
  54.         end
  55.         site = table.concat(site)
  56.         host:close()
  57.     end
  58.     local saveName = name:gsub("%%", "%%"):gsub("%.", "%.")
  59.     for text in site:gmatch[[<div class="panel panel%-default">.-<div class="panel%-body">(.-)</div>]] do
  60.         if text:match("<li><b>Название:</b> "..saveName.." </li>") then
  61.             local host, err = invoke(internet, 'request', text:match[[a href=.-"(.-)."]]or"", 80)
  62.             if not host then
  63.                 return nil, "\tconection error: "..err
  64.             end
  65.             local text = host:read()
  66.             while text do
  67.                 text = host:read()
  68.                 local s = text:match([[http://pastebin.com/(........)]])
  69.                 if s then
  70.                     text = s
  71.                     break
  72.                 end
  73.             end
  74.             host:close()
  75.             host, err = invoke(internet, 'request', "http://pastebin.com/raw.php?i="..text, 80)
  76.             if not host then
  77.                 return nil, "\tconection error: "..err
  78.             end
  79.             if not fs.exists("/usr/lib") then
  80.                 fs.makeDirectory("/usr/lib")
  81.             end
  82.             local file, err = io.open("/usr/lib/"..name..".lua", "w")
  83.             if not file then
  84.                 return nil, "\t error while recording: "..err
  85.             end
  86.             while host:read() do
  87.                 file:write(host:read()or"")
  88.                 file:flush()
  89.             end
  90.             file:close()
  91.             return dofile("/usr/lib/"..name..".lua")
  92.         end
  93.     end
  94.     return nil, "\tno library '"..name.."' in repository"
  95. end
  96.  
  97. global.getlib = function(...)
  98.     local args, result = table.pack(...), {}
  99.     for i = 1, args.n do
  100.         local name, lib = args[i]
  101.         checkArg(i, name, "string")
  102.         local error_mes = {"lib #"..i.." '"..name.."' not found:"}
  103.         lib, error_mes[#error_mes+1] = findin_table(name)
  104.         if not lib then
  105.             lib, error_mes[#error_mes+1] = findin_path(name)
  106.         end
  107.         if not lib then
  108.             lib, error_mes[#error_mes+1] = findin_rep(name)
  109.         end
  110.         if lib then
  111.             result[i] = lib
  112.             global.package.libs[name] = lib
  113.         else
  114.             site = nil
  115.             error(table.concat(error_mes, "\n"), 2)
  116.         end
  117.     end
  118.     site = nil
  119.     return table.unpack(result)
  120. end
Advertisement
Add Comment
Please, Sign In to add comment