Advertisement
VlaD00m

Memphisto-OWN installer

Dec 7th, 2020 (edited)
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.61 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('Memphisto (OWN version) installer')
  12. print('Developing © 2020-2021 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/Memphisto/Common/libs/doubleBuffering.lua",
  43.         path = "/lib/doubleBuffering.lua"
  44.     },
  45.     {
  46.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/OpenWebNet/Memphisto/Common/Memphisto/home.nfp",
  47.         path = "/usr/misc/Memphisto/home.nfp"
  48.     },
  49.     {
  50.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/OpenWebNet/Memphisto/Common/Memphisto/help.nfp",
  51.         path = "/usr/misc/Memphisto/help.nfp"
  52.     },
  53.     {
  54.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/OpenWebNet/Memphisto/Common/Memphisto/404.nfp",
  55.         path = "/usr/misc/Memphisto/404.nfp"
  56.     },
  57.     {
  58.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/OpenWebNet/Memphisto/Common/Memphisto/incorrect.nfp",
  59.         path = "/usr/misc/Memphisto/incorrect.nfp"
  60.     },
  61.     {
  62.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/OpenWebNet/Memphisto/Common/Memphisto/noanswer.nfp",
  63.         path = "/usr/misc/Memphisto/noanswer.nfp"
  64.     },
  65.     {
  66.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/OpenWebNet/Memphisto/Common/Memphisto/noserver.nfp",
  67.         path = "/usr/misc/Memphisto/noserver.nfp"
  68.     },
  69.     {
  70.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/OpenWebNet/Memphisto/Common/Memphisto/mlogo.pic",
  71.         path = "/usr/misc/Memphisto/mlogo.pic"
  72.     },
  73.     {
  74.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/OpenWebNet/Memphisto/Common/Memphisto/keys.pic",
  75.         path = "/usr/misc/Memphisto/keys.pic"
  76.     },
  77.     {
  78.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/OpenWebNet/Memphisto/OWN-NFP/ownbrow.cfg",
  79.         path = "/etc/ownbrow.cfg"
  80.     },
  81.     {
  82.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/OpenWebNet/Memphisto/OWN-NFP/ddbsloc.cfg",
  83.         path = "/etc/ddbsloc.cfg"
  84.     },
  85.     {
  86.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/OpenWebNet/Memphisto/OWN-NFP/ownbrow.lua",
  87.         path = "/bin/ownbrow.lua"
  88.     },
  89. }
  90.  
  91. local properties = {
  92.     windowWidth = 56,
  93.     GUIElementsOffset = 2,
  94.     localization = {
  95.         title = "Installing Memphisto...",
  96.         currentFile = "Downloading \"<currentFile>\"",
  97.         totalProgress = "Total progress: <totalProgress>%",
  98.         finished1 = "Memphisto has been successfully installed",
  99.         finished2 = "Type in console \"ownbrow\" to run it",
  100.         finished3 = "Press any key to quit",
  101.     },
  102.     colors = {
  103.         window = {
  104.             background = 0xEEEEEE,
  105.             text = 0x999999,
  106.             shadow = 0x3C3C3C
  107.         },
  108.         title = {
  109.             background = 0xCCCCCC,
  110.             text = 0x555555,
  111.         },
  112.         progressBar = {
  113.             active = 0x0092FF,
  114.             passive = 0xCCCCCC
  115.         }
  116.     }
  117. }
  118.  
  119. ---------------------------------------------------------------------------------------------------------------------------------
  120.  
  121. local screenWidth, screenHeight = gpu.getResolution()
  122. properties.windowHeight = 8
  123.  
  124. if properties.windowWidth < 1 then
  125.     properties.windowWidth = math.floor(screenWidth * properties.windowWidth)
  126. end
  127. progressBarWidth = properties.windowWidth - properties.GUIElementsOffset * 2
  128.  
  129. if not properties.windowX then
  130.     properties.windowX = math.floor(screenWidth / 2 - properties.windowWidth / 2)
  131. end
  132.  
  133. if not properties.windowY then
  134.     properties.windowY = math.floor(screenHeight / 2 - properties.windowHeight / 2)
  135. end
  136.  
  137. local currentBackground, currentForeground
  138.  
  139. ---------------------------------------------------------------------------------------------------------------------------------
  140.  
  141. local function setBackground(color)
  142.     if currentBackground ~= color then
  143.         gpu.setBackground(color)
  144.         currentBackground = color
  145.     end
  146. end
  147.  
  148. local function setForeground(color)
  149.     if currentForeground ~= color then
  150.         gpu.setForeground(color)
  151.         currentForeground = color
  152.     end
  153. end
  154.  
  155. local function rectangle(x, y, width, height, color)
  156.     setBackground(color)
  157.     gpu.fill(x, y, width, height, " ")
  158. end
  159.  
  160. local function centerizedText(y, color, text)
  161.     local textLength = unicode.len(text)
  162.     if textLength > progressBarWidth then
  163.         text = unicode.sub(text, 1, progressBarWidth)
  164.         textLength = progressBarWidth
  165.     end
  166.  
  167.     setForeground(color)
  168.     gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep(" ", progressBarWidth))
  169.     gpu.set(math.floor(properties.windowX + properties.GUIElementsOffset + progressBarWidth / 2 - textLength / 2), y, text)
  170. end
  171.  
  172. local function progressBar(y, percent, text, totalProgress, currentProgress, currentFile)
  173.     setForeground(properties.colors.progressBar.passive)
  174.     gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep("━", progressBarWidth))
  175.     setForeground(properties.colors.progressBar.active)
  176.     gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep("━", math.ceil(progressBarWidth * percent)))
  177.  
  178.     text = text:gsub("<totalProgress>", totalProgress)
  179.     text = text:gsub("<currentProgress>", currentProgress)
  180.     text = text:gsub("<currentFile>", currentFile)
  181.  
  182.     centerizedText(y + 1, properties.colors.window.text, text)
  183. end
  184.  
  185. local function download(url, path, totalProgress)
  186.     fs.makeDirectory(fs.path(path))
  187.  
  188.     local file, fileReason = io.open(path, "w")
  189.     if file then
  190.         local pcallSuccess, requestHandle = pcall(internet.request, url)
  191.         if pcallSuccess then
  192.             if requestHandle then
  193.                 local y = properties.windowY + 2
  194.                 progressBar(y, 0, properties.localization.currentFile, totalProgress, "0", path)
  195.                
  196.                 local responseCode, responseName, responseData
  197.                 repeat
  198.                     responseCode, responseName, responseData = requestHandle:response()
  199.                 until responseCode
  200.  
  201.                 if responseData and responseData["Content-Length"] then
  202.                     local contentLength = tonumber(responseData["Content-Length"][1])
  203.                     local currentLength = 0
  204.                     while true do
  205.                         local data, reason = requestHandle.read(math.huge)
  206.                         if data then
  207.                             currentLength = currentLength + unicode.len(data)
  208.                             local percent = currentLength / contentLength
  209.                             progressBar(y, percent, properties.localization.currentFile, totalProgress, tostring(math.ceil(percent)), path)
  210.  
  211.                             file:write(data)
  212.                         else
  213.                             requestHandle:close()
  214.                             if reason then
  215.                                 error(reason)
  216.                             else
  217.                                 file:close()
  218.                                 return
  219.                             end
  220.                         end
  221.                     end
  222.                 else
  223.                     error("Response Content-Length header is missing: " .. tostring(responseCode) .. " " .. tostring(responseName))
  224.                 end
  225.             else
  226.                 error("Invalid URL-address: " .. tostring(url))
  227.             end
  228.         else
  229.             error("Usage: component.internet.request(string url)")
  230.         end
  231.  
  232.         file:close()
  233.     else
  234.         error("Failed to open file for writing: " .. tostring(fileReason))
  235.     end
  236. end
  237.  
  238. ---------------------------------------------------------------------------------------------------------------------------------
  239.  
  240. local oldPixels = {}
  241. for y = properties.windowY, properties.windowY + properties.windowHeight do
  242.     oldPixels[y] = {}
  243.     for x = properties.windowX, properties.windowX + properties.windowWidth do
  244.         oldPixels[y][x] = { gpu.get(x, y) }
  245.     end
  246. end
  247.  
  248. local function shadowPixel(x, y, symbol)
  249.     setBackground(oldPixels[y][x][3])
  250.     gpu.set(x, y, symbol)
  251. end
  252.  
  253. rectangle(properties.windowX + properties.windowWidth, properties.windowY + 1, 1, properties.windowHeight - 1, properties.colors.window.shadow)
  254. setForeground(properties.colors.window.shadow)
  255. shadowPixel(properties.windowX + properties.windowWidth, properties.windowY, "▄")
  256.  
  257. for i = properties.windowX + 1, properties.windowX + properties.windowWidth do
  258.     shadowPixel(i, properties.windowY + properties.windowHeight, "▀")
  259. end
  260.  
  261. rectangle(properties.windowX, properties.windowY + 1, properties.windowWidth, properties.windowHeight - 1, properties.colors.window.background)
  262.  
  263. rectangle(properties.windowX, properties.windowY, properties.windowWidth, 1, properties.colors.title.background)
  264. centerizedText(properties.windowY, properties.colors.title.text, properties.localization.title)
  265. setBackground(properties.colors.window.background)
  266.  
  267. local y = properties.windowY + 5
  268. progressBar(y, 0, properties.localization.totalProgress, "0", "0", files[1].path)
  269. for i = 1, #files do
  270.     local percent = i / #files
  271.     local totalProgress = tostring(math.ceil(percent * 100))
  272.     download(files[i].url, files[i].path, totalProgress)
  273.     progressBar(y, percent, properties.localization.totalProgress, totalProgress, "0", files[i].path)
  274. end
  275.  
  276. if properties.localization.finished1 then
  277.     rectangle(properties.windowX, properties.windowY + 1, properties.windowWidth, properties.windowHeight - 1, properties.colors.window.background)
  278.     centerizedText(properties.windowY + 2, properties.colors.window.text, properties.localization.finished1)
  279.     centerizedText(properties.windowY + 3, properties.colors.window.text, properties.localization.finished2)
  280.     centerizedText(properties.windowY + 5, properties.colors.window.text, properties.localization.finished3)
  281.  
  282.     while true do
  283.         local eventType = event.pull()
  284.         if eventType == "key_down" or eventType == "touch" then
  285.             break
  286.         end
  287.     end
  288. end
  289.  
  290. for y = properties.windowY, properties.windowY + properties.windowHeight do
  291.     for x = properties.windowX, properties.windowX + properties.windowWidth do
  292.         setBackground(oldPixels[y][x][3])
  293.         setForeground(oldPixels[y][x][2])
  294.         gpu.set(x, y, oldPixels[y][x][1])
  295.     end
  296. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement