Advertisement
Tag365

Lua Script System Installer

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