Advertisement
Xelostar

CCDoom installer

Dec 22nd, 2022
2,591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.91 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/CCDoom/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 = "CCDoom 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.         "models",
  75.         "images",
  76.         "levels",
  77.     }
  78.  
  79.     local downloads = {
  80.         "Doom.lua",
  81.         "Pine3D-minified.lua",
  82.         "betterblittle.lua",
  83.         "blittle",
  84.         "README.md",
  85.         "LICENSE",
  86.         "models/corpse",
  87.         "models/doorx",
  88.         "models/doorz",
  89.         "models/emerald",
  90.         "models/enemy1",
  91.         "models/enemy2",
  92.         "models/wallx",
  93.         "models/wallxz",
  94.         "models/wallz",
  95.         "levels/level1",
  96.         "levels/level2",
  97.         "levels/level3",
  98.         "levels/level4",
  99.         "levels/level5",
  100.         "levels/level6",
  101.         "levels/level7",
  102.         "levels/level8",
  103.         "levels/level9",
  104.         "images/bfire",
  105.         "images/bgun",
  106.         "images/bgunf",
  107.         "images/bheart",
  108.         "images/fire",
  109.         "images/gun",
  110.         "images/gunf",
  111.         "images/heart",
  112.         "images/logo",
  113.     }
  114.  
  115.     local total = #folders + #downloads
  116.  
  117.     for i = 1, #folders do
  118.         local folder = folders[i]
  119.         update("Creating " .. folder .. " folder...")
  120.         fs.makeDir(folder)
  121.         bar(i/total)
  122.     end
  123.  
  124.     totalDownloaded = #folders
  125.     downloadAll(downloads, total)
  126.  
  127.     update("Installation finished!")
  128.  
  129.     sleep(1)
  130.  
  131.     term.setBackgroundColor(colors.black)
  132.     term.setTextColor(colors.white)
  133.     term.clear()
  134.  
  135.     term.setCursorPos(1, 1)
  136.     write("Finished installation!\nPress any key to close...")
  137.  
  138.     os.pullEventRaw()
  139.  
  140.     term.clear()
  141.     term.setCursorPos(1, 1)
  142. end
  143.  
  144. install()
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement