Advertisement
znepb

zOS Installer

Dec 31st, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.92 KB | None | 0 0
  1. --[[
  2.     zOS Installer
  3.     1.0.2
  4. ]]--
  5.  
  6. local baseURL = "https://raw.githubusercontent.com/znepb/zOS/master/"
  7. local args = {...}
  8. if args[1] == "--dev" then
  9.     baseURL = "https://raw.githubusercontent.com/znepb/zOS/development/"
  10. end
  11.  
  12. print('zOS Installer')
  13. fs.makeDir('/.temp/')
  14. print('Downloading JSON API...')
  15. local data = http.get(baseURL.."/system/zOS/System/API/json.lua")
  16. local jsonAPI = fs.open("/.temp/json.lua", "w")
  17. jsonAPI.write(data.readAll())
  18. jsonAPI.close()
  19. os.loadAPI('/.temp/json.lua')
  20. print('Loading installer-files.json...')
  21. local data = http.get(baseURL.."/installer-files.json")
  22. local installerInfo = json.decode(data.readAll())
  23. data.close()
  24.  
  25. print('Required size: '.. installerInfo.requiredSpace)
  26. print('Space available: ' .. fs.getFreeSpace('/'))
  27.  
  28. if installerInfo.requiredSpace < fs.getFreeSpace('/') then
  29.     print('Space OK')
  30.     sleep(0.5)
  31.     local w, h = term.getSize()
  32.     local progress = 0
  33.     local function mainDraw()
  34.         term.setBackgroundColor(colors.black)
  35.         term.clear()
  36.         if fs.exists("/zOS/Images/zOS.nfp") then
  37.             paintutils.drawImage(paintutils.loadImage("/zOS/Images/zOS.nfp"),w/2-20/2,h/2-7/2*1.2)
  38.         end
  39.         term.setCursorPos(w/2+20/2-string.len("v. 0"),h/2-7/2*1.2+7)
  40.         term.setBackgroundColor(colors.black)
  41.     end
  42.  
  43.     local function updateLoadingBar(prog)
  44.         progress = prog
  45.         paintutils.drawLine(w/2-20/2, h/2*1.5, w/2+20/2+0.5, h/2*1.5, colors.lightGray)
  46.         paintutils.drawLine(w/2-20/2, h/2*1.5, w/2-20/2+((progress/100)*20), h/2*1.5, colors.lightBlue)
  47.     end
  48.  
  49.     local function updateText(text)
  50.         term.setCursorPos(w/2-string.len(text)/2,h/2*1.5)
  51.         term.write(text)
  52.     end
  53.  
  54.     local function dlText(text)
  55.         term.setCursorPos(w/2-string.len(text)/2,h/2*1.5+2)
  56.         term.write(text)
  57.     end
  58.  
  59.     local progress = 0
  60.     mainDraw()
  61.     updateText('Creating directories...')
  62.     for i, v in pairs(installerInfo.createDirs) do
  63.         fs.makeDir(v)
  64.     end
  65.     mainDraw()
  66.     for i, v in pairs(installerInfo.downloadFiles) do
  67.         mainDraw()
  68.         updateLoadingBar(i/#installerInfo.downloadFiles*100)
  69.         term.setBackgroundColor(colors.black)
  70.         dlText('Downloading '..v)
  71.         local data = http.get(baseURL.."/system"..v)
  72.         local fileH = fs.open(v, "w")
  73.         fileH.write(data.readAll())
  74.         fileH.close()
  75.     end
  76.    
  77.     local function setSetting(name, value)
  78.         local f = fs.open("/zOS/Configuration/configuration.txt", "r")
  79.         local configData = textutils.unserialize(f.readAll())
  80.         f.close()
  81.  
  82.         local f = fs.open("/zOS/Configuration/configuration.txt", "w")
  83.         configData[name] = value
  84.         f.write(textutils.serialize(configData))
  85.         f.close()
  86.  
  87.         return true
  88.     end
  89.    
  90.     setSetting('branch', 'master')
  91.     if args[1] == "--dev" then
  92.         setSetting('branch', 'development')
  93.     end
  94.    
  95.     mainDraw()
  96.     updateText('Restarting...')
  97.     sleep(0.5)
  98.     os.reboot()
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement