Advertisement
Xelostar

NBSTunes Installer

Feb 1st, 2024
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.31 KB | None | 0 0
  1. local width, height = term.getSize()
  2.  
  3. local totalDownloaded = 0
  4.  
  5. local function update(text)
  6.     term.setBackgroundColor(colors.black)
  7.     term.setTextColor(colors.white)
  8.     term.setCursorPos(1, 9)
  9.     term.clearLine()
  10.     term.setCursorPos(math.floor(width/2 - string.len(text)/2 + 0.5), 9)
  11.     write(text)
  12. end
  13.  
  14. local function bar(ratio)
  15.     term.setBackgroundColor(colors.gray)
  16.     term.setTextColor(colors.lime)
  17.     term.setCursorPos(1, 11)
  18.  
  19.     for i = 1, width do
  20.         if (i/width < ratio) then
  21.             write("]")
  22.         else
  23.             write(" ")
  24.         end
  25.     end
  26. end
  27.  
  28. local function download(path, attempt)
  29.     local rawData = http.get("https://raw.githubusercontent.com/Xella37/NBS-Tunes-CC/main/"..path)
  30.     update("Downloaded " .. path .. "!")
  31.     if not rawData then
  32.         if attempt == 3 then
  33.             error("Failed to download " .. path .. " after 3 attempts!")
  34.         end
  35.         update("Failed to download " .. path .. ". Trying again (attempt " .. (attempt+1) .. "/3)")
  36.         return download(path, attempt+1)
  37.     end
  38.     local data = rawData.readAll()
  39.     local file = fs.open(path, "w")
  40.     file.write(data)
  41.     file.close()
  42. end
  43.  
  44. local function downloadAll(downloads, total)
  45.     local nextFile = table.remove(downloads, 1)
  46.     if nextFile then
  47.         sleep(0.1)
  48.         parallel.waitForAll(function()
  49.             downloadAll(downloads, total)
  50.         end, function()
  51.             download(nextFile, 1)
  52.             totalDownloaded = totalDownloaded + 1
  53.             bar(totalDownloaded / total)
  54.         end)
  55.     end
  56. end
  57.  
  58. function install()
  59.     term.setBackgroundColor(colors.black)
  60.     term.setTextColor(colors.yellow)
  61.     term.clear()
  62.  
  63.     local str = "NBSTunes Installer"
  64.     term.setCursorPos(math.floor(width/2 - #str / 2 + 0.5), 2)
  65.     write(str)
  66.  
  67.     update("Installing...")
  68.     bar(0)
  69.  
  70.     local folders = {
  71.     }
  72.  
  73.     local downloads = {
  74.         "100_gecs_Doritos_Fritos.nbs",
  75.         "nbsTunes.lua",
  76.         "play.lua",
  77.     }
  78.  
  79.     local total = #folders + #downloads
  80.  
  81.     for i = 1, #folders do
  82.         local folder = folders[i]
  83.         update("Creating " .. folder .. " folder...")
  84.         fs.makeDir(folder)
  85.         bar(i/total)
  86.     end
  87.  
  88.     totalDownloaded = #folders
  89.     downloadAll(downloads, total)
  90.  
  91.     update("Installation finished!")
  92.  
  93.     sleep(1)
  94.  
  95.     term.setBackgroundColor(colors.black)
  96.     term.setTextColor(colors.white)
  97.     term.clear()
  98.  
  99.     term.setCursorPos(1, 1)
  100.     write("Finished installation!\nPress any key to close...")
  101.  
  102.     os.pullEventRaw()
  103.  
  104.     term.clear()
  105.     term.setCursorPos(1, 1)
  106. end
  107.  
  108. install()
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement