5bitesofcookies

Ext2CC Installer

Oct 22nd, 2016
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.50 KB | None | 0 0
  1. --Modified Gitget
  2. --Changes: Added installutils
  3. --Stopped it from downloading JSON, loads the API off pastebin instead.
  4.  
  5. --[[
  6. This is Install Utils by Geforce Fan. It's found @ BvL9rr0N , please get it from there to be sure it's up to date.
  7. Just paste it into your installer!
  8. ]]
  9. function pasteRun(id,aa)
  10.   aa=aa or {}
  11.   if not type(id)=="string" then
  12.     return "Must has string!"
  13.   end
  14.   --get program off pastebin
  15.   local a=http.get("http://www.pastebin.com/raw.php?i="..id)
  16.   if not a then return false end
  17.   a=a.readAll()
  18.   if not a then return false end
  19.   local env = {}
  20.   a=loadstring(a)
  21.   if not a then return false end
  22.   --local env= setmetatable({},{__index=_G})
  23.   local env = getfenv()
  24.   setfenv(a,env)
  25.        
  26.   local ok,err=pcall(a,unpack(aa))
  27.   --a()
  28.   if (not ok) and err then
  29.     printError("Error running "..id..": "..err)
  30.     return false
  31.   end
  32.   return err,env
  33. end
  34. function pastePI(id,tab)
  35.   local returned,env = pasteRun(id)
  36.   _G[tab] = env
  37. end
  38.  
  39. shell.run"delete start"
  40. shell.run"delete bootstrap"
  41.  
  42.  
  43. --[[ /gitget
  44. GitHub downloading utility for CC.
  45. Developed by apemanzilla.
  46.  
  47. This requires ElvishJerricco's JSON parsing API.
  48. Direct link: http://pastebin.com/raw.php?i=4nRg9CHU
  49. ]]--
  50.  
  51. -- Edit these variables to use preset mode.
  52. -- Whether to download the files asynchronously (huge speed benefits, will also retry failed files)
  53. -- 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
  54. local async = true
  55.  
  56. -- Whether to write to the terminal as files are downloaded
  57. -- Note that unless checked for this will not affect pre-set start/done code below
  58. local silent = false
  59.  
  60. local preset = {
  61.     -- The GitHub account name
  62.     user = "AI221",
  63.     -- The GitHub repository name
  64.     repo = "ext2cc",
  65.    
  66.     -- The branch or commit tree to download (defaults to 'master')
  67.     branch = nil,
  68.    
  69.     -- The local folder to save all the files to (defaults to '/')
  70.     path = nil,
  71.    
  72.     -- Function to run before starting the download
  73.     start = function()
  74.         if not silent then print("Downloading files from GitHub...") end
  75.     end,
  76.    
  77.     -- Function to run when the download completes
  78.     done = function()
  79.         if not silent then print("Done") end
  80.         fs.copy("rootFS_temp", "rootFS")
  81.         --fs.delete("json")
  82.         print("Installed! Run 'start' to start the FS replacement. Edit 'start' and add the location of disks to the table at the top to add space to the filesystem.")
  83.     end
  84. }
  85.  
  86. -- Leave the rest of the program alone.
  87. local args = {...}
  88.  
  89. args[1] = preset.user or args[1]
  90. args[2] = preset.repo or args[2]
  91. args[3] = preset.branch or args[3] or "master"
  92. args[4] = preset.path or args[4] or ""
  93.  
  94. if #args < 2 then
  95.         print("Usage:\n"..((shell and shell.getRunningProgram()) or "gitget").." <user> <repo> [branch/tree] [path]") error()
  96. end
  97.  
  98. local function save(data,file)
  99.     local file = shell.resolve(file:gsub("%%20"," "))
  100.     if not (fs.exists(string.sub(file,1,#file - #fs.getName(file))) and fs.isDir(string.sub(file,1,#file - #fs.getName(file)))) then
  101.         if fs.exists(string.sub(file,1,#file - #fs.getName(file))) then fs.delete(string.sub(file,1,#file - #fs.getName(file))) end
  102.         fs.makeDir(string.sub(file,1,#file - #fs.getName(file)))
  103.     end
  104.     local f = fs.open(file,"w")
  105.     f.write(data)
  106.     f.close()
  107. end
  108.  
  109. local function download(url, file)
  110.     save(http.get(url).readAll(),file)
  111. end
  112.  
  113. if not json then
  114.     --download("http://pastebin.com/raw.php?i=4nRg9CHU","json")
  115.     --os.loadAPI("json")
  116.     pastePI("4nRg9CHU","json")
  117. end
  118.  
  119. preset.start()
  120. local data = json.decode(http.get("https://api.github.com/repos/"..args[1].."/"..args[2].."/git/trees/"..args[3].."?recursive=1").readAll())
  121. if data.message and data.message:find("API rate limit exceeded") then error("Out of API calls, try again later") end
  122. if data.message and data.message == "Not found" then error("Invalid repository",2) else
  123.     for k,v in pairs(data.tree) do
  124.         -- Make directories
  125.         if v.type == "tree" then
  126.             fs.makeDir(fs.combine(args[4],v.path))
  127.             if not hide_progress then
  128.             end
  129.         end
  130.     end
  131.     local drawProgress
  132.     if async and not silent then
  133.         local _, y = term.getCursorPos()
  134.         local wide, _ = term.getSize()
  135.         term.setCursorPos(1, y)
  136.         term.write("[")
  137.         term.setCursorPos(wide - 6, y)
  138.         term.write("]")
  139.         drawProgress = function(done, max)
  140.             local value = done / max
  141.             term.setCursorPos(2,y)
  142.             term.write(("="):rep(math.floor(value * (wide - 8))))
  143.             local percent = math.floor(value * 100) .. "%"
  144.             term.setCursorPos(wide - percent:len(),y)
  145.             term.write(percent)
  146.         end
  147.     end
  148.     local filecount = 0
  149.     local downloaded = 0
  150.     local paths = {}
  151.     local failed = {}
  152.     for k,v in pairs(data.tree) do
  153.         -- Send all HTTP requests (async)
  154.         if v.type == "blob" then
  155.             v.path = v.path:gsub("%s","%%20")
  156.             local url = "https://raw.github.com/"..args[1].."/"..args[2].."/"..args[3].."/"..v.path,fs.combine(args[4],v.path)
  157.             if async then
  158.                 http.request(url)
  159.                 paths[url] = fs.combine(args[4],v.path)
  160.                 filecount = filecount + 1
  161.             else
  162.                 download(url, fs.combine(args[4], v.path))
  163.                 if not silent then print(fs.combine(args[4], v.path)) end
  164.             end
  165.         end
  166.     end
  167.     while downloaded < filecount do
  168.         local e, a, b = os.pullEvent()
  169.         if e == "http_success" then
  170.             save(b.readAll(),paths[a])
  171.             downloaded = downloaded + 1
  172.             if not silent then drawProgress(downloaded,filecount) end
  173.         elseif e == "http_failure" then
  174.             -- Retry in 3 seconds
  175.             failed[os.startTimer(3)] = a
  176.         elseif e == "timer" and failed[a] then
  177.             http.request(failed[a])
  178.         end
  179.     end
  180. end
  181. preset.done()
Advertisement
Add Comment
Please, Sign In to add comment