Advertisement
VlaD00m

OWN Domens DataBase Server installer

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