Advertisement
Taruu

inst.lua

Feb 28th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.48 KB | None | 0 0
  1. --pastebin run yeyeqngr inst.lua
  2. local component = require("component")
  3. local unicode = require("unicode")
  4. local fs = require("filesystem")
  5. local event = require("event")
  6. local gpu = component.gpu
  7. local internet = component.internet
  8.  
  9. ---------------------------------------------------------------------------------------------------------------------------------
  10.  
  11. -- Specify required files for downloading
  12. local files = {
  13.     {
  14.         url = "https://raw.githubusercontent.com/Krutoy242/InfoPanel/master/InfoPanel.lua",
  15.         path = "/home/info/InfoPanel.lua"
  16.     },
  17.     {
  18.         url = "https://raw.githubusercontent.com/Krutoy242/InfoPanel/master/xml/XmlParser.lua",
  19.         path = "/home/info/xml/XmlParser.lua"
  20.     },
  21.     {
  22.         url = "https://raw.githubusercontent.com/Krutoy242/InfoPanel/master/gml/canvas.lua",
  23.         path = "/home/info/gml/canvas.lua"
  24.     },
  25.     {
  26.         url = "https://raw.githubusercontent.com/Krutoy242/InfoPanel/master/gml/colorutils.lua",
  27.         path = "/home/info/gml/colorutils.lua"
  28.     },
  29.     {
  30.         url = "https://raw.githubusercontent.com/Krutoy242/InfoPanel/master/gml/default.gss",
  31.         path = "/home/info/gml/default.gss"
  32.     },
  33.     {
  34.         url = "https://raw.githubusercontent.com/Krutoy242/InfoPanel/master/gml/gfxbuffer.lua",
  35.         path = "/home/info/gml/gfxbuffer.lua"
  36.     },
  37.     {
  38.         url = "https://raw.githubusercontent.com/Krutoy242/InfoPanel/master/gml/gml.lua",
  39.         path = "/home/info/gml/gml.lua"
  40.     },
  41.     {
  42.         url = "https://raw.githubusercontent.com/Krutoy242/InfoPanel/master/gml/gmlDialogs.lua",
  43.         path = "/home/info/gml/gmlDialogs.lua"
  44.     },
  45.     {
  46.         url = "https://raw.githubusercontent.com/Krutoy242/InfoPanel/master/gml/style.gss",
  47.         path = "/home/info/gml/style.gss"
  48.     },
  49. }
  50.  
  51. local properties = {
  52.     -- Comment any coordinate to calculate it automatically (will centerize window on screen by specified axis)
  53.     -- windowX = 2,
  54.     -- windowY = 2,
  55.     -- Set window width value lower than zero (0.5 for example) to calculate it dependent on screen width
  56.     windowWidth = 54,
  57.     -- Customize offset by X axis from window corners
  58.     GUIElementsOffset = 2,
  59.     -- Customize localization as you want to
  60.     localization = {
  61.         -- Specify title of your installer
  62.         title = "GUI framework installer",
  63.         -- Use <currentProgress>, <totalProgress> and <currentFile> text insertions to automatically display their values
  64.         currentFile = "Downloading \"<currentFile>\"",
  65.         totalProgress = "Total progress: <totalProgress>%",
  66.         -- Comment this lines to automatically close installer window
  67.         finished1 = "GUI framework has been successfully installed",
  68.         finished2 = "Press any key to quit"
  69.     },
  70.     -- Customize color scheme as you want to
  71.     colors = {
  72.         window = {
  73.             background = 0xEEEEEE,
  74.             text = 0x999999,
  75.             shadow = 0x3C3C3C
  76.         },
  77.         title = {
  78.             background = 0xCCCCCC,
  79.             text = 0x555555,
  80.         },
  81.         progressBar = {
  82.             active = 0x0092FF,
  83.             passive = 0xCCCCCC
  84.         }
  85.     }
  86. }
  87.  
  88. ---------------------------------------------------------------------------------------------------------------------------------
  89.  
  90. local screenWidth, screenHeight = gpu.getResolution()
  91. properties.windowHeight = 8
  92.  
  93. if properties.windowWidth < 1 then
  94.     properties.windowWidth = math.floor(screenWidth * properties.windowWidth)
  95. end
  96. progressBarWidth = properties.windowWidth - properties.GUIElementsOffset * 2
  97.  
  98. if not properties.windowX then
  99.     properties.windowX = math.floor(screenWidth / 2 - properties.windowWidth / 2)
  100. end
  101.  
  102. if not properties.windowY then
  103.     properties.windowY = math.floor(screenHeight / 2 - properties.windowHeight / 2)
  104. end
  105.  
  106. local currentBackground, currentForeground
  107.  
  108. ---------------------------------------------------------------------------------------------------------------------------------
  109.  
  110. local function setBackground(color)
  111.     if currentBackground ~= color then
  112.         gpu.setBackground(color)
  113.         currentBackground = color
  114.     end
  115. end
  116.  
  117. local function setForeground(color)
  118.     if currentForeground ~= color then
  119.         gpu.setForeground(color)
  120.         currentForeground = color
  121.     end
  122. end
  123.  
  124. local function rectangle(x, y, width, height, color)
  125.     setBackground(color)
  126.     gpu.fill(x, y, width, height, " ")
  127. end
  128.  
  129. local function centerizedText(y, color, text)
  130.     local textLength = unicode.len(text)
  131.     if textLength > progressBarWidth then
  132.         text = unicode.sub(text, 1, progressBarWidth)
  133.         textLength = progressBarWidth
  134.     end
  135.  
  136.     setForeground(color)
  137.     gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep(" ", progressBarWidth))
  138.     gpu.set(math.floor(properties.windowX + properties.GUIElementsOffset + progressBarWidth / 2 - textLength / 2), y, text)
  139. end
  140.  
  141. local function progressBar(y, percent, text, totalProgress, currentProgress, currentFile)
  142.     setForeground(properties.colors.progressBar.passive)
  143.     gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep("━", progressBarWidth))
  144.     setForeground(properties.colors.progressBar.active)
  145.     gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep("━", math.ceil(progressBarWidth * percent)))
  146.  
  147.     text = text:gsub("<totalProgress>", totalProgress)
  148.     text = text:gsub("<currentProgress>", currentProgress)
  149.     text = text:gsub("<currentFile>", currentFile)
  150.  
  151.     centerizedText(y + 1, properties.colors.window.text, text)
  152. end
  153.  
  154. local function download(url, path, totalProgress)
  155.     fs.makeDirectory(fs.path(path))
  156.  
  157.     local file, fileReason = io.open(path, "w")
  158.     if file then
  159.         local pcallSuccess, requestHandle = pcall(internet.request, url)
  160.         if pcallSuccess then
  161.             if requestHandle then
  162.                 -- Drawing progressbar once with zero percentage
  163.                 local y = properties.windowY + 2
  164.                 progressBar(y, 0, properties.localization.currentFile, totalProgress, "0", path)
  165.                
  166.                 -- Waiting for any response code
  167.                 local responseCode, responseName, responseData
  168.                 repeat
  169.                     responseCode, responseName, responseData = requestHandle:response()
  170.                 until responseCode
  171.  
  172.                 -- Downloading file by chunks
  173.                 if responseData and responseData["Content-Length"] then
  174.                     local contentLength = tonumber(responseData["Content-Length"][1])
  175.                     local currentLength = 0
  176.                     while true do
  177.                         local data, reason = requestHandle.read(math.huge)
  178.                         if data then
  179.                             currentLength = currentLength + unicode.len(data)
  180.                             local percent = currentLength / contentLength
  181.                             progressBar(y, percent, properties.localization.currentFile, totalProgress, tostring(math.ceil(percent)), path)
  182.  
  183.                             file:write(data)
  184.                         else
  185.                             requestHandle:close()
  186.                             if reason then
  187.                                 error(reason)
  188.                             else
  189.                                 file:close()
  190.                                 return
  191.                             end
  192.                         end
  193.                     end
  194.                 else
  195.                     error("Response Content-Length header is missing: " .. tostring(responseCode) .. " " .. tostring(responseName))
  196.                 end
  197.             else
  198.                 error("Invalid URL-address: " .. tostring(url))
  199.             end
  200.         else
  201.             error("Usage: component.internet.request(string url)")
  202.         end
  203.  
  204.         file:close()
  205.     else
  206.         error("Failed to open file for writing: " .. tostring(fileReason))
  207.     end
  208. end
  209.  
  210. ---------------------------------------------------------------------------------------------------------------------------------
  211.  
  212. -- Copying current screen data
  213. local oldPixels = {}
  214. for y = properties.windowY, properties.windowY + properties.windowHeight do
  215.     oldPixels[y] = {}
  216.     for x = properties.windowX, properties.windowX + properties.windowWidth do
  217.         oldPixels[y][x] = { gpu.get(x, y) }
  218.     end
  219. end
  220.  
  221. local function shadowPixel(x, y, symbol)
  222.     setBackground(oldPixels[y][x][3])
  223.     gpu.set(x, y, symbol)
  224. end
  225.  
  226. -- Vertical shadow
  227. rectangle(properties.windowX + properties.windowWidth, properties.windowY + 1, 1, properties.windowHeight - 1, properties.colors.window.shadow)
  228. setForeground(properties.colors.window.shadow)
  229. shadowPixel(properties.windowX + properties.windowWidth, properties.windowY, "▄")
  230.  
  231. -- Horizontal shadow
  232. for i = properties.windowX + 1, properties.windowX + properties.windowWidth do
  233.     shadowPixel(i, properties.windowY + properties.windowHeight, "▀")
  234. end
  235.  
  236. -- Window background
  237. rectangle(properties.windowX, properties.windowY + 1, properties.windowWidth, properties.windowHeight - 1, properties.colors.window.background)
  238.  
  239. -- Title
  240. rectangle(properties.windowX, properties.windowY, properties.windowWidth, 1, properties.colors.title.background)
  241. centerizedText(properties.windowY, properties.colors.title.text, properties.localization.title)
  242. setBackground(properties.colors.window.background)
  243.  
  244. -- Downloading
  245. local y = properties.windowY + 5
  246. progressBar(y, 0, properties.localization.totalProgress, "0", "0", files[1].path)
  247. for i = 1, #files do
  248.     local percent = i / #files
  249.     local totalProgress = tostring(math.ceil(percent * 100))
  250.     download(files[i].url, files[i].path, totalProgress)
  251.     progressBar(y, percent, properties.localization.totalProgress, totalProgress, "0", files[i].path)
  252. end
  253.  
  254. -- On exit
  255. if properties.localization.finished1 then
  256.     rectangle(properties.windowX, properties.windowY + 1, properties.windowWidth, properties.windowHeight - 1, properties.colors.window.background)
  257.     centerizedText(properties.windowY + 3, properties.colors.window.text, properties.localization.finished1)
  258.     centerizedText(properties.windowY + 4, properties.colors.window.text, properties.localization.finished2)
  259.  
  260.     while true do
  261.         local eventType = event.pull()
  262.         if eventType == "key_down" or eventType == "touch" then
  263.             break
  264.         end
  265.     end
  266. end
  267.  
  268. -- Redraw old pixels
  269. for y = properties.windowY, properties.windowY + properties.windowHeight do
  270.     for x = properties.windowX, properties.windowX + properties.windowWidth do
  271.         setBackground(oldPixels[y][x][3])
  272.         setForeground(oldPixels[y][x][2])
  273.         gpu.set(x, y, oldPixels[y][x][1])
  274.     end
  275. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement