Advertisement
Xelostar

Lightspeed Alpha installer

Sep 13th, 2023 (edited)
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1.  
  2. local width, height = term.getSize()
  3.  
  4. local totalDownloaded = 0
  5.  
  6. local function update(text)
  7.     term.setBackgroundColor(colors.black)
  8.     term.setTextColor(colors.white)
  9.     term.setCursorPos(1, 9)
  10.     term.clearLine()
  11.     term.setCursorPos(math.floor(width/2 - string.len(text)/2 + 0.5), 9)
  12.     write(text)
  13. end
  14.  
  15. local function bar(ratio)
  16.     term.setBackgroundColor(colors.gray)
  17.     term.setTextColor(colors.lime)
  18.     term.setCursorPos(1, 11)
  19.  
  20.     for i = 1, width do
  21.         if (i/width < ratio) then
  22.             write("]")
  23.         else
  24.             write(" ")
  25.         end
  26.     end
  27. end
  28.  
  29. local function download(path, attempt)
  30.     local rawData = http.get("https://raw.githubusercontent.com/Xella37/LightspeedAlpha/master/"..path)
  31.     update("Downloaded " .. path .. "!")
  32.     if not rawData then
  33.         if attempt == 3 then
  34.             error("Failed to download " .. path .. " after 3 attempts!")
  35.         end
  36.         update("Failed to download " .. path .. ". Trying again (attempt " .. (attempt+1) .. "/3)")
  37.         return download(path, attempt+1)
  38.     end
  39.     local data = rawData.readAll()
  40.     local file = fs.open(path, "w")
  41.     file.write(data)
  42.     file.close()
  43. end
  44.  
  45. local function downloadAll(downloads, total)
  46.     local nextFile = table.remove(downloads, 1)
  47.     if nextFile then
  48.         sleep(0.1)
  49.         parallel.waitForAll(function()
  50.             downloadAll(downloads, total)
  51.         end, function()
  52.             download(nextFile, 1)
  53.             totalDownloaded = totalDownloaded + 1
  54.             bar(totalDownloaded / total)
  55.         end)
  56.     end
  57. end
  58.  
  59. function install()
  60.     term.setBackgroundColor(colors.black)
  61.     term.setTextColor(colors.yellow)
  62.     term.clear()
  63.  
  64.     local str = "Lightspeed Alpha Installer"
  65.     term.setCursorPos(math.floor(width/2 - #str / 2 + 0.5), 2)
  66.     write(str)
  67.  
  68.     update("Installing...")
  69.     bar(0)
  70.  
  71.     local total = 36
  72.  
  73.     local folders = {
  74.     }
  75.  
  76.     local downloads = {
  77.         "Lightspeed",
  78.         "README.md",
  79.         "eShip1.img",
  80.         "eShip1_broken.img",
  81.         "eShip2.img",
  82.         "eShip2_broken.img",
  83.         "eShip3.img",
  84.         "eShip3_broken.img",
  85.         "eShip4.img",
  86.         "eShip4_broken.img",
  87.         "eShip5.img",
  88.         "eShip5_broken.img",
  89.         "eShip6.img",
  90.         "eShip6_broken.img",
  91.         "eShip7.img",
  92.         "eShip7_broken.img",
  93.         "eShip8.img",
  94.         "eShip8_broken.img",
  95.         "eShip9.img",
  96.         "eShip9_broken.img",
  97.         "eShip10.img",
  98.         "eShip10_broken.img",
  99.         "eShip11.img",
  100.         "eShip11_broken.img",
  101.         "eShip12.img",
  102.         "eShip12_broken.img",
  103.         "pShip1.img",
  104.         "pShip2.img",
  105.         "ui",
  106.     }
  107.  
  108.     local total = #folders + #downloads
  109.  
  110.     for i = 1, #folders do
  111.         local folder = folders[i]
  112.         update("Creating " .. folder .. " folder...")
  113.         fs.makeDir(folder)
  114.         bar(i/total)
  115.     end
  116.  
  117.     totalDownloaded = #folders
  118.     downloadAll(downloads, total)
  119.  
  120.     update("Installation finished!")
  121.  
  122.     sleep(1)
  123.  
  124.     term.setBackgroundColor(colors.black)
  125.     term.setTextColor(colors.white)
  126.     term.clear()
  127.  
  128.     term.setCursorPos(1, 1)
  129.     write("Finished installation!\nPress any key to close...")
  130.  
  131.     os.pullEventRaw()
  132.  
  133.     term.clear()
  134.     term.setCursorPos(1, 1)
  135. end
  136.  
  137. install()
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement