ZNZNCOOP

pm.lua

Mar 17th, 2016
88
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Created by NEO
  2. -- You can find me at : http://computercraft.ru/user/13-neo/
  3.  
  4.  
  5.  
  6.   internet = require('internet')
  7.   shell = require('shell')
  8.   X, Y = require('component').gpu.getResolution()
  9.   fs = require('filesystem')
  10.  
  11.   repository = {}
  12.  
  13.   repoPath = 'repo.meta'
  14.   url = 'http://pastebin.com/raw/vtKanXDC'
  15.  
  16.  
  17.   args = {...}
  18.  
  19. function getPages()
  20.     if repository ~= nil then
  21.         return math.ceil(#repository/Y)
  22.     end
  23.     return 0
  24. end
  25.  
  26. function isId(id)
  27.     if id <= 0 or id > #repository then error('Id not found.') end
  28. end
  29.  
  30. function getNameById(id)
  31.     isId(id)
  32.     return repository[id].name
  33. end
  34.  
  35. function getVersionById(id)
  36.     isId(id)
  37.     return repository[id].version
  38. end
  39.  
  40. function getAuthorById(id)
  41.     isId(id)
  42.     return repository[id].author
  43. end
  44.  
  45. function getDescById(id)
  46.     isId(id)
  47.     return repository[id].desc
  48. end
  49.  
  50. function getUrlsById(id)
  51.     isId(id)
  52.     return repository[id].urls
  53. end
  54.  
  55. function getDependencies(id)
  56.    isId(id)
  57.    return repository[id].dependencies
  58. end
  59.  
  60. function isDependenciesPastebin(id, idd)
  61.    return getDependencies(id)[idd]['pastebin'] ~= nil
  62. end
  63.  
  64. function isDependenciesGitHub(id, idd)
  65.    return getDependencies(id)[idd]['github'] ~= nil
  66. end
  67.  
  68. function getDependenciesPastebin(id, idd)
  69.    return getDependencies(id)[idd]['pastebin']
  70. end
  71.  
  72. function getDependenciesGitHub(id, idd)
  73.    return getDependencies(id)[idd]['github']
  74. end
  75.  
  76. function isPastebin(id)
  77.     urls = getUrlsById(id)
  78.     return urls['pastebin'] ~= nil
  79. end
  80.  
  81. function isGitHub(id)
  82.     urls = getUrlsById(id)
  83.     return urls['github'] ~= nil
  84. end
  85.  
  86. function getPastebin(id)
  87.     urls = getUrlsById(id)
  88.     return urls['pastebin']
  89. end
  90.  
  91. function getGitHub(id)
  92.     urls = getUrlsById(id)
  93.     return urls['github']
  94. end
  95.  
  96. function view(page)
  97.     if page == nil then
  98.         print('Pages: ', getPages())
  99.     else
  100.         if page > getPages() or page <= 0 then error('Page not found.') end
  101.         min = (page - 1) * Y + 1
  102.         max = page * Y
  103.         repo_len = #repository
  104.        
  105.         if min <= 0 or max <= 0 then return error() end
  106.         if max > repo_len then max = repo_len end
  107.         for i = min, max do
  108.             drawInfo(i)
  109.         end
  110.     end
  111. end
  112.  
  113. function getMode(str)
  114.     if str == 'pastebin' or str == 'p' then return 1 end
  115.     if str == 'github' or str == 'g' then return 2 end
  116.     return -1
  117. end
  118.  
  119. function status(err, stat, file)
  120.     if err then
  121.         print(file..' downloading finish.')
  122.     else
  123.         print(file..' error: '..stat)
  124.     end
  125. end
  126.  
  127. function download(path, id, mode)
  128.     isId(id)
  129.     if path ~= nil then
  130.         if string.sub(path, #path, #path) ~= '/' then
  131.             path = path..'/'
  132.         end
  133.     end
  134.     name = getNameById(id)..'-v'..string.gsub(getVersionById(id), '%.', '_')..'.lua'
  135.     if mode == 1 and isPastebin(id) then
  136.         if path == nil then
  137.             err, stat = shell.execute('pastebin get -f '..getPastebin(id)..' '..name)
  138.             status(err, stat, getNameById(id))
  139.         else
  140.             err, stat = shell.execute('pastebin get -f '..getPastebin(id)..' '..path..name)
  141.             status(err, stat, getNameById(id))
  142.         end
  143.         downloadDependencies(path, id)
  144.     elseif mode == 1 then
  145.         print('the program is not on Pastebin')
  146.     end
  147.     if mode == 2 and isGitHub(id) then
  148.         if path == nil then
  149.             err, stat = shell.execute('wget -Q '..getGitHub(id)..' '..name)
  150.             status(err, stat, getNameById(id))
  151.         else
  152.             err, stat = shell.execute('wget -Q '..getGitHub(id)..' '..path..name)
  153.             status(err, stat, getNameById(id))
  154.         end
  155.         downloadDependencies(path, id)
  156.     elseif mode == 2 then
  157.         error('The program is not on GitHub')
  158.     end
  159. end
  160.  
  161. function downloadDependencies(path, id)
  162.    print('Downloading dependencies...')
  163.    isId(id)
  164.    if path == nil then
  165.        dependencies = getDependencies(id)
  166.        for i = 1, #dependencies do
  167.           if isDependenciesPastebin(id, i) then
  168.             err, stat = shell.execute('pastebin get -f '..getDependenciesPastebin(id, i)..' '..dependencies[i].path)
  169.             status(err, stat, dependencies[i].path)
  170.           elseif isDependenciesGitHub(id, i) then
  171.             err, stat = shell.execute('wget -Q '..getDependenciesGitHub(id, i)..' '..dependencies[i].path)
  172.             status(err, stat, dependencies[i].path)
  173.           end
  174.        end
  175.    else
  176.       for i = 1, #dependencies do
  177.           if isDependenciesPastebin(id, i) then
  178.             err, stat = shell.execute('pastebin get -f '..getDependenciesPastebin(id, i).github..' '..path..dependencies[i].path)
  179.             status(err, stat, dependencies[i].path)
  180.           elseif isDependenciesGitHub(id, i) then
  181.             err, stat = shell.execute('wget -Q '..getDependenciesGitHub(id, i).github..' '..path..dependencies[i].path)
  182.             status(err, stat, dependencies[i].path)
  183.           end
  184.        end
  185.    end
  186. end
  187.  
  188. function drawInfo(id)
  189.     isId(id)
  190.     print(id, '"'..getNameById(id)..'" V:'..getVersionById(id), ' author: '..getAuthorById(id))
  191. end
  192.  
  193.   function find(name)
  194.     if name ~= nil then
  195.         name = string.gsub(name, '*', '.+')
  196.         name = string.gsub(name, '?', '.')
  197.         for i = 1, #repository do
  198.             res = string.match(getNameById(i), name)
  199.             if res ~= nil then
  200.                 drawInfo(i)
  201.             end
  202.         end
  203.     end
  204. end
  205.  
  206. function loadMeta()
  207.     f = fs.open(repoPath)
  208.     if f == nil then
  209.         loadRepository()
  210.     end
  211.     str = f:read(math.huge)
  212.     func = load(str, '= load')
  213.     repository = func()
  214. end
  215.  
  216. function loadRepository()
  217.     print('Loading repository...')
  218.     str = ''
  219.     for chunk in internet.request(url) do
  220.         str = str .. chunk
  221.         io.write('...')
  222.     end
  223.     func = load(str, '= load')
  224.     repository = func()
  225.     f = fs.open(repoPath, 'w')
  226.     f:write(str)
  227.     f:close()
  228. end
  229.  
  230. function init()
  231.     loadMeta()
  232. end
  233.  
  234. function main()
  235.     init()
  236.     if args ~= nil then
  237.         len = #args
  238.         if len == 1 then
  239.             if args[1] == 'load' then loadRepository() end
  240.             if args[1] == 'view' then view() end
  241.         end
  242.         if len == 2 then
  243.             if args[1] == 'view' then page = tonumber(args[2]) view(page) end
  244.             if args[1] == 'desc' then id = tonumber(args[2]) print(getDescById(id)) end
  245.             if args[1] == 'find' then find(args[2]) end
  246.         end
  247.         if len == 3 then
  248.             if args[1] == 'download' then download(nil, tonumber(args[2]), getMode(args[3])) end
  249.         end
  250.         if len == 4 then
  251.             if args[1] == 'download' then download(args[4], tonumber(args[2]), getMode(args[3])) end
  252.         end
  253.     end
  254. end
  255.  
  256. main()
RAW Paste Data