Advertisement
Tag365

Flutter Install

Apr 13th, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.91 KB | None | 0 0
  1. -- Lua Script System --
  2. -- set local variables
  3. local advanced = term.isColor()
  4. local scrwidth, scrheight = term.getSize()
  5. local oldsetTextColor = term.setTextColor
  6. local oldsetBackgroundColor = term.setBackgroundColor
  7. local splabg = colors.gray
  8. local LuaSSdir = "Flutter/"
  9. local folderStructureS
  10. _G.shell = shell
  11.  
  12. -- set the new global os.pullEvent function
  13. function os.pullEvent(...)
  14.     local eventType, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10 = os.pullEventRaw(...)
  15.     if eventType == "terminate" then
  16.         error("Terminated installation", 0)
  17.         return
  18.     end
  19.     return eventType, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10
  20. end
  21.  
  22. -- change color functions to fallback when not advanced computer
  23. if not advanced then
  24.     function term.setTextColor( color )
  25.         if color == colors.black then
  26.             oldsetTextColor(color)
  27.         else
  28.             oldsetTextColor(colors.white)
  29.         end
  30.     end
  31.     function term.setBackgroundColor( color )
  32.         if color == colors.white then
  33.             oldsetBackgroundColor(color)
  34.         else
  35.             oldsetBackgroundColor(colors.black)
  36.         end
  37.     end
  38. end
  39. -- draw a loading screen --
  40. term.setBackgroundColor( splabg )
  41. term.clear()
  42. if loadingimage then
  43.     paintutils.drawImage(loadingimage, math.floor(scrwidth*.5 - 6), math.floor(scrheight*.5 - 2))
  44. end
  45. term.setCursorPos( 1, 1 )
  46.  
  47. local loadingstring, oldloadingstring = "", ""
  48. local loadingframe = -scrwidth*.2
  49. local setCursorPos = term.setCursorPos
  50. local function drawLoadingProgressIndicator()
  51.     while true do -- run a loop to prevent it from stopping the loading function before it returns.
  52.         loadingframe = loadingframe + 1
  53.         if loadingframe > scrwidth*1.2 then
  54.             loadingframe = -scrwidth*.2
  55.         end
  56.         if loadingstring ~= oldloadingstring then
  57.             setCursorPos( scrwidth*.5 - (#loadingstring*.5) + 1, scrheight - 1)
  58.             term.setBackgroundColor( splabg )
  59.             term.clearLine()
  60.             term.write(loadingstring)
  61.         end
  62.         setCursorPos( loadingframe - 1, scrheight )
  63.         term.setBackgroundColor( colors.blue )
  64.         term.clearLine()
  65.         term.setBackgroundColor( colors.lightBlue )
  66.         term.write(string.rep(" ", scrwidth*.2))
  67.         sleep()
  68.     end
  69. end
  70.  
  71. local function outOfSpaceError()
  72.     term.setBackgroundColor(colors.blue)
  73.     local outofspaceTitle = "Out of space"
  74.     term.clear()
  75.     term.setCursorPos((scrwidth - #outofspaceTitle)*.5, 1)
  76.     term.write(outofspaceTitle)
  77.     term.setCursorPos(1, 8)
  78.     print("You need to free at least "..#folderStructureS - fs.getFreeSpace("").." bytes on your hard disk to install Flutter.")
  79.     print("After freeing enough disk space restart the script.")
  80.     error("Cannot continue installing Flutter!", 0)
  81. end
  82.  
  83. local function createFolderForFile(path)
  84.     local curpath = path
  85.     local startHere = 1
  86.     while true do
  87.         local nextSlash = string.find(string.sub(path, startHere, -1), "/")
  88.         if not nextSlash then return end
  89.         nextSlash = nextSlash + startHere
  90.         local folderToCreate = string.sub(curpath, 0, nextSlash - 1)
  91.         if not fs.isDir(folderToCreate) then
  92.             local ok, err = pcall(fs.makeDir, folderToCreate)
  93.             if not ok then
  94.                 if string.find(err, "Out of space") then
  95.                     outOfSpaceError()
  96.                 end
  97.             end
  98.         end
  99.         startHere = nextSlash
  100.     end
  101. end
  102.  
  103. local function installLuaScriptSystem()
  104.     local urlToDownload = "http://pastebin.com/raw/7a3QYQ7p"
  105.     local handle = http.get(urlToDownload)
  106.     folderStructureS = handle.readAll()
  107.     handle.close()
  108.     local folderStructure = textutils.unserialize(folderStructureS)
  109.     if fs.getFreeSpace("") < #folderStructureS then
  110.         loadingstring = ""
  111.     end
  112.     for k, v in pairs(folderStructure) do
  113.         createFolderForFile(k)
  114.         local file = fs.open(k, "w")
  115.         if file then
  116.             file.write(v)
  117.             local ok, err = pcall(file.close)
  118.             if not ok then
  119.                 if string.find(err, "Out of space") then
  120.                     outOfSpaceError()
  121.                 end
  122.             end
  123.         end
  124.         sleep()
  125.     end
  126. end
  127.  
  128. parallel.waitForAny(drawLoadingProgressIndicator, installLuaScriptSystem)
  129.  
  130. local file = fs.open("startup", "w")
  131. file.write('shell.run("Flutter/startup")')
  132. file.close()
  133.  
  134. shell.run(LuaSSdir.."startup")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement