Advertisement
Jummit

Lua Visual Game Engine Installer

Dec 12th, 2018
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.26 KB | None | 0 0
  1. --[[ /gitget
  2. GitHub downloading utility for CC.
  3. Developed by apemanzilla.
  4.  
  5. This requires ElvishJerricco's JSON parsing API.
  6. Direct link: http://pastebin.com/raw.php?i=4nRg9CHU
  7. ]]--
  8.  
  9. -- Edit these variables to use preset mode.
  10. -- Whether to download the files asynchronously (huge speed benefits, will also retry failed files)
  11. -- If false will download the files one by one and use the old output (List each file name as it's downloaded) instead of the progress bar
  12. local async = true
  13.  
  14. -- Whether to write to the terminal as files are downloaded
  15. -- Note that unless checked for this will not affect pre-set start/done code below
  16. local silent = false
  17.  
  18. local preset = {
  19.     -- The GitHub account name
  20.     user = nil,
  21.     -- The GitHub repository name
  22.     repo = nil,
  23.    
  24.     -- The branch or commit tree to download (defaults to 'master')
  25.     branch = nil,
  26.    
  27.     -- The local folder to save all the files to (defaults to '/')
  28.     path = nil,
  29.    
  30.     -- Function to run before starting the download
  31.     start = function()
  32.         if not silent then print("Downloading files from GitHub...") end
  33.     end,
  34.    
  35.     -- Function to run when the download completes
  36.     done = function()
  37.         if not silent then print("Done") end
  38.     end
  39. }
  40.  
  41. -- Leave the rest of the program alone.
  42. local args = {
  43.   "Jummit",
  44.   "Visual-Computercraft-Game-Engine"
  45. }
  46.  
  47. args[1] = preset.user or args[1]
  48. args[2] = preset.repo or args[2]
  49. args[3] = preset.branch or args[3] or "master"
  50. args[4] = preset.path or args[4] or ""
  51.  
  52. if #args < 2 then
  53.         print("Usage:\n"..((shell and shell.getRunningProgram()) or "gitget").." <user> <repo> [branch/tree] [path]") error()
  54. end
  55.  
  56. local function save(data,file)
  57.     local file = shell.resolve(file:gsub("%%20"," "))
  58.     if not (fs.exists(string.sub(file,1,#file - #fs.getName(file))) and fs.isDir(string.sub(file,1,#file - #fs.getName(file)))) then
  59.         if fs.exists(string.sub(file,1,#file - #fs.getName(file))) then fs.delete(string.sub(file,1,#file - #fs.getName(file))) end
  60.         fs.makeDir(string.sub(file,1,#file - #fs.getName(file)))
  61.     end
  62.     local f = fs.open(file,"w")
  63.     f.write(data)
  64.     f.close()
  65. end
  66.  
  67. local function download(url, file)
  68.     save(http.get(url).readAll(),file)
  69. end
  70.  
  71. if not json then
  72.     download("http://pastebin.com/raw.php?i=4nRg9CHU","json")
  73.     os.loadAPI("json")
  74. end
  75.  
  76. preset.start()
  77. local data = json.decode(http.get("https://api.github.com/repos/"..args[1].."/"..args[2].."/git/trees/"..args[3].."?recursive=1").readAll())
  78. if data.message and data.message:find("API rate limit exceeded") then error("Out of API calls, try again later") end
  79. if data.message and data.message == "Not found" then error("Invalid repository",2) else
  80.     for k,v in pairs(data.tree) do
  81.         -- Make directories
  82.         if v.type == "tree" then
  83.             fs.makeDir(fs.combine(args[4],v.path))
  84.             if not hide_progress then
  85.             end
  86.         end
  87.     end
  88.     local drawProgress
  89.     if async and not silent then
  90.         local _, y = term.getCursorPos()
  91.         local wide, _ = term.getSize()
  92.         term.setCursorPos(1, y)
  93.         term.write("[")
  94.         term.setCursorPos(wide - 6, y)
  95.         term.write("]")
  96.         drawProgress = function(done, max)
  97.             local value = done / max
  98.             term.setCursorPos(2,y)
  99.             term.write(("="):rep(math.floor(value * (wide - 8))))
  100.             local percent = math.floor(value * 100) .. "%"
  101.             term.setCursorPos(wide - percent:len(),y)
  102.             term.write(percent)
  103.         end
  104.     end
  105.     local filecount = 0
  106.     local downloaded = 0
  107.     local paths = {}
  108.     local failed = {}
  109.     for k,v in pairs(data.tree) do
  110.         -- Send all HTTP requests (async)
  111.         if v.type == "blob" then
  112.             v.path = v.path:gsub("%s","%%20")
  113.             local url = "https://raw.github.com/"..args[1].."/"..args[2].."/"..args[3].."/"..v.path,fs.combine(args[4],v.path)
  114.             if async then
  115.                 http.request(url)
  116.                 paths[url] = fs.combine(args[4],v.path)
  117.                 filecount = filecount + 1
  118.             else
  119.                 download(url, fs.combine(args[4], v.path))
  120.                 if not silent then print(fs.combine(args[4], v.path)) end
  121.             end
  122.         end
  123.     end
  124.     while downloaded < filecount do
  125.         local e, a, b = os.pullEvent()
  126.         if e == "http_success" then
  127.             save(b.readAll(),paths[a])
  128.             downloaded = downloaded + 1
  129.             if not silent then drawProgress(downloaded,filecount) end
  130.         elseif e == "http_failure" then
  131.             -- Retry in 3 seconds
  132.             failed[os.startTimer(3)] = a
  133.         elseif e == "timer" and failed[a] then
  134.             http.request(failed[a])
  135.         end
  136.     end
  137. end
  138. preset.done()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement