Advertisement
VlaD00m

OWN Web-Site Server installer

Dec 7th, 2020
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.80 KB | None | 0 0
  1. local component = require("component")
  2. local unicode = require("unicode")
  3. local fs = require("filesystem")
  4. local event = require("event")
  5. local tty = require("tty")
  6. local gpu = component.gpu
  7. local internet = component.internet
  8. ---------------------------------------------------------------------------------------------------------------------------------
  9.  
  10. tty.clear()
  11. print('OWN Web-Site Server installer')
  12. print('Developing © 2020 Compys S&N Systems')
  13. print('For more information go to: https://github.com/Bs0Dd/OpenCompSoft/blob/master/OpenWebNet/README.md\n')
  14.  
  15. print('Do you want to install browser? Y/N')
  16.         while true do
  17.             _, _, _, key = event.pull("key_up")
  18.             if key == 21 then break
  19.             elseif key == 49 then return end
  20.         end
  21.  
  22. ---------------------------------------------------------------------------------------------------------------------------------
  23.  
  24. local files = {
  25.     {
  26.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/OpenWebNet/Memphisto/Common/libs/advancedLua.lua",
  27.         path = "/lib/advancedLua.lua"
  28.     },
  29.     {
  30.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/OpenWebNet/Memphisto/Common/libs/color.lua",
  31.         path = "/lib/color.lua"
  32.     },
  33.     {
  34.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/OpenWebNet/Memphisto/Common/libs/FormatModules/OCIF.lua",
  35.         path = "/lib/FormatModules/OCIF.lua"
  36.     },
  37.     {
  38.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/OpenWebNet/Memphisto/Common/libs/image.lua",
  39.         path = "/lib/image.lua"
  40.     },
  41.     {
  42.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/OpenWebNet/OWNServer/ownserv.lua",
  43.         path = "/etc/rc.d/ownserv.lua"
  44.     },
  45. }
  46.  
  47. local properties = {
  48.     windowWidth = 56,
  49.     GUIElementsOffset = 2,
  50.     localization = {
  51.         title = "Installing OWNServer...",
  52.         currentFile = "Downloading \"<currentFile>\"",
  53.         totalProgress = "Total progress: <totalProgress>%",
  54.         finished1 = "OWNServer has been successfully installed",
  55.         finished2 = "Set up and use \"rc ownserv start\" to run it",
  56.         finished3 = "Press any key to quit",
  57.     },
  58.     colors = {
  59.         window = {
  60.             background = 0xEEEEEE,
  61.             text = 0x999999,
  62.             shadow = 0x3C3C3C
  63.         },
  64.         title = {
  65.             background = 0xCCCCCC,
  66.             text = 0x555555,
  67.         },
  68.         progressBar = {
  69.             active = 0x0092FF,
  70.             passive = 0xCCCCCC
  71.         }
  72.     }
  73. }
  74.  
  75. ---------------------------------------------------------------------------------------------------------------------------------
  76.  
  77. local screenWidth, screenHeight = gpu.getResolution()
  78. properties.windowHeight = 8
  79.  
  80. if properties.windowWidth < 1 then
  81.     properties.windowWidth = math.floor(screenWidth * properties.windowWidth)
  82. end
  83. progressBarWidth = properties.windowWidth - properties.GUIElementsOffset * 2
  84.  
  85. if not properties.windowX then
  86.     properties.windowX = math.floor(screenWidth / 2 - properties.windowWidth / 2)
  87. end
  88.  
  89. if not properties.windowY then
  90.     properties.windowY = math.floor(screenHeight / 2 - properties.windowHeight / 2)
  91. end
  92.  
  93. local currentBackground, currentForeground
  94.  
  95. ---------------------------------------------------------------------------------------------------------------------------------
  96.  
  97. local function setBackground(color)
  98.     if currentBackground ~= color then
  99.         gpu.setBackground(color)
  100.         currentBackground = color
  101.     end
  102. end
  103.  
  104. local function setForeground(color)
  105.     if currentForeground ~= color then
  106.         gpu.setForeground(color)
  107.         currentForeground = color
  108.     end
  109. end
  110.  
  111. local function rectangle(x, y, width, height, color)
  112.     setBackground(color)
  113.     gpu.fill(x, y, width, height, " ")
  114. end
  115.  
  116. local function centerizedText(y, color, text)
  117.     local textLength = unicode.len(text)
  118.     if textLength > progressBarWidth then
  119.         text = unicode.sub(text, 1, progressBarWidth)
  120.         textLength = progressBarWidth
  121.     end
  122.  
  123.     setForeground(color)
  124.     gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep(" ", progressBarWidth))
  125.     gpu.set(math.floor(properties.windowX + properties.GUIElementsOffset + progressBarWidth / 2 - textLength / 2), y, text)
  126. end
  127.  
  128. local function progressBar(y, percent, text, totalProgress, currentProgress, currentFile)
  129.     setForeground(properties.colors.progressBar.passive)
  130.     gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep("━", progressBarWidth))
  131.     setForeground(properties.colors.progressBar.active)
  132.     gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep("━", math.ceil(progressBarWidth * percent)))
  133.  
  134.     text = text:gsub("<totalProgress>", totalProgress)
  135.     text = text:gsub("<currentProgress>", currentProgress)
  136.     text = text:gsub("<currentFile>", currentFile)
  137.  
  138.     centerizedText(y + 1, properties.colors.window.text, text)
  139. end
  140.  
  141. local function download(url, path, totalProgress)
  142.     fs.makeDirectory(fs.path(path))
  143.  
  144.     local file, fileReason = io.open(path, "w")
  145.     if file then
  146.         local pcallSuccess, requestHandle = pcall(internet.request, url)
  147.         if pcallSuccess then
  148.             if requestHandle then
  149.                 local y = properties.windowY + 2
  150.                 progressBar(y, 0, properties.localization.currentFile, totalProgress, "0", path)
  151.                
  152.                 local responseCode, responseName, responseData
  153.                 repeat
  154.                     responseCode, responseName, responseData = requestHandle:response()
  155.                 until responseCode
  156.  
  157.                 if responseData and responseData["Content-Length"] then
  158.                     local contentLength = tonumber(responseData["Content-Length"][1])
  159.                     local currentLength = 0
  160.                     while true do
  161.                         local data, reason = requestHandle.read(math.huge)
  162.                         if data then
  163.                             currentLength = currentLength + unicode.len(data)
  164.                             local percent = currentLength / contentLength
  165.                             progressBar(y, percent, properties.localization.currentFile, totalProgress, tostring(math.ceil(percent)), path)
  166.  
  167.                             file:write(data)
  168.                         else
  169.                             requestHandle:close()
  170.                             if reason then
  171.                                 error(reason)
  172.                             else
  173.                                 file:close()
  174.                                 return
  175.                             end
  176.                         end
  177.                     end
  178.                 else
  179.                     error("Response Content-Length header is missing: " .. tostring(responseCode) .. " " .. tostring(responseName))
  180.                 end
  181.             else
  182.                 error("Invalid URL-address: " .. tostring(url))
  183.             end
  184.         else
  185.             error("Usage: component.internet.request(string url)")
  186.         end
  187.  
  188.         file:close()
  189.     else
  190.         error("Failed to open file for writing: " .. tostring(fileReason))
  191.     end
  192. end
  193.  
  194. ---------------------------------------------------------------------------------------------------------------------------------
  195.  
  196. local oldPixels = {}
  197. for y = properties.windowY, properties.windowY + properties.windowHeight do
  198.     oldPixels[y] = {}
  199.     for x = properties.windowX, properties.windowX + properties.windowWidth do
  200.         oldPixels[y][x] = { gpu.get(x, y) }
  201.     end
  202. end
  203.  
  204. local function shadowPixel(x, y, symbol)
  205.     setBackground(oldPixels[y][x][3])
  206.     gpu.set(x, y, symbol)
  207. end
  208.  
  209. rectangle(properties.windowX + properties.windowWidth, properties.windowY + 1, 1, properties.windowHeight - 1, properties.colors.window.shadow)
  210. setForeground(properties.colors.window.shadow)
  211. shadowPixel(properties.windowX + properties.windowWidth, properties.windowY, "▄")
  212.  
  213. for i = properties.windowX + 1, properties.windowX + properties.windowWidth do
  214.     shadowPixel(i, properties.windowY + properties.windowHeight, "▀")
  215. end
  216.  
  217. rectangle(properties.windowX, properties.windowY + 1, properties.windowWidth, properties.windowHeight - 1, properties.colors.window.background)
  218.  
  219. rectangle(properties.windowX, properties.windowY, properties.windowWidth, 1, properties.colors.title.background)
  220. centerizedText(properties.windowY, properties.colors.title.text, properties.localization.title)
  221. setBackground(properties.colors.window.background)
  222.  
  223. local y = properties.windowY + 5
  224. progressBar(y, 0, properties.localization.totalProgress, "0", "0", files[1].path)
  225. for i = 1, #files do
  226.     local percent = i / #files
  227.     local totalProgress = tostring(math.ceil(percent * 100))
  228.     download(files[i].url, files[i].path, totalProgress)
  229.     progressBar(y, percent, properties.localization.totalProgress, totalProgress, "0", files[i].path)
  230. end
  231.  
  232. if properties.localization.finished1 then
  233.     rectangle(properties.windowX, properties.windowY + 1, properties.windowWidth, properties.windowHeight - 1, properties.colors.window.background)
  234.     centerizedText(properties.windowY + 2, properties.colors.window.text, properties.localization.finished1)
  235.     centerizedText(properties.windowY + 3, properties.colors.window.text, properties.localization.finished2)
  236.     centerizedText(properties.windowY + 5, properties.colors.window.text, properties.localization.finished3)
  237.  
  238.     while true do
  239.         local eventType = event.pull()
  240.         if eventType == "key_down" or eventType == "touch" then
  241.             break
  242.         end
  243.     end
  244. end
  245.  
  246. for y = properties.windowY, properties.windowY + properties.windowHeight do
  247.     for x = properties.windowX, properties.windowX + properties.windowWidth do
  248.         setBackground(oldPixels[y][x][3])
  249.         setForeground(oldPixels[y][x][2])
  250.         gpu.set(x, y, oldPixels[y][x][1])
  251.     end
  252. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement