VlaD00m

VychVyzhProm Installer

Jun 24th, 2021
1,058
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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('VychVyzhProm (ВычВыжПром)')
  12. print('Copyright (C) 2019 CAT IN THE DARK')
  13. print('Copyright (C) 2021 Compys S&N Systems')
  14. print('For more information go to: https://github.com/Bs0Dd/OpenCompSoft/blob/master/VychVyzhProm/README.md\n')
  15.  
  16. print('Do you want to install VychVyzhProm? Y/N')
  17.         while true do
  18.             local _, _, _, key = event.pull("key_up")
  19.             if key == 21 then break
  20.             elseif key == 49 then return end
  21.         end
  22.  
  23. ---------------------------------------------------------------------------------------------------------------------------------
  24.  
  25. local files = {
  26.     {
  27.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Languages/English.lng",
  28.         path = "/home/vvprom/Languages/English.lng"
  29.     },
  30.     {
  31.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Languages/Russian.lng",
  32.         path = "/home/vvprom/Languages/Russian.lng"
  33.     },
  34.     {
  35.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Pictures/bcontr.pic",
  36.         path = "/home/vvprom/Pictures/bcontr.pic"
  37.     },
  38.     {
  39.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Pictures/cidlogo.pic",
  40.         path = "/home/vvprom/Pictures/cidlogo.pic"
  41.     },
  42.     {
  43.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Pictures/comlogo.pic",
  44.         path = "/home/vvprom/Pictures/comlogo.pic"
  45.     },
  46.     {
  47.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Pictures/editor.pic",
  48.         path = "/home/vvprom/Pictures/editor.pic"
  49.     },
  50.     {
  51.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Pictures/exit.pic",
  52.         path = "/home/vvprom/Pictures/exit.pic"
  53.     },
  54.     {
  55.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Pictures/help.pic",
  56.         path = "/home/vvprom/Pictures/help.pic"
  57.     },
  58.     {
  59.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Pictures/info.pic",
  60.         path = "/home/vvprom/Pictures/info.pic"
  61.     },
  62.     {
  63.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Pictures/controls.pic",
  64.         path = "/home/vvprom/Pictures/controls.pic"
  65.     },
  66.     {
  67.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Pictures/task.pic",
  68.         path = "/home/vvprom/Pictures/task.pic"
  69.     },
  70.     {
  71.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Pictures/vvprom.pic",
  72.         path = "/home/vvprom/Pictures/vvprom.pic"
  73.     },
  74.     {
  75.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Tasks/task1.vtf",
  76.         path = "/home/vvprom/Tasks/task1.vtf"
  77.     },
  78.     {
  79.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Tasks/task2.vtf",
  80.         path = "/home/vvprom/Tasks/task2.vtf"
  81.     },
  82.     {
  83.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Tasks/task3.vtf",
  84.         path = "/home/vvprom/Tasks/task3.vtf"
  85.     },
  86.     {
  87.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Tasks/task4.vtf",
  88.         path = "/home/vvprom/Tasks/task4.vtf"
  89.     },
  90.     {
  91.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Tasks/task5.vtf",
  92.         path = "/home/vvprom/Tasks/task5.vtf"
  93.     },
  94.     {
  95.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Tasks/task6.vtf",
  96.         path = "/home/vvprom/Tasks/task6.vtf"
  97.     },
  98.     {
  99.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Tasks/task7.vtf",
  100.         path = "/home/vvprom/Tasks/task7.vtf"
  101.     },
  102.     {
  103.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/Tasks/task8.vtf",
  104.         path = "/home/vvprom/Tasks/task8.vtf"
  105.     },
  106.     {
  107.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/NyaDraw/NyaDrMini.lua",
  108.         path = "/home/vvprom/NyaDraw/init.lua"
  109.     },
  110.     {
  111.         url = "https://raw.githubusercontent.com/Bs0Dd/OpenCompSoft/master/VychVyzhProm/vvprom.lua",
  112.         path = "/home/vvprom/vvprom.lua"
  113.     },
  114. }
  115.  
  116. local properties = {
  117.     GUIElementsOffset = 2,
  118.     localization = {
  119.         title = "Installing VychVyzhProm game...",
  120.         currentFile = "Downloading \"<currentFile>\"",
  121.         totalProgress = "Total progress: <totalProgress>%",
  122.         finished1 = "VychVyzhProm has been successfully installed",
  123.         finished2 = "Type \"cd /home/vvprom\" and \"vvprom\"",
  124.         finished3 = "to run a game",
  125.         finished4 = "Press any key to quit",
  126.     }
  127. }
  128.  
  129. if gpu.maxResolution() < 80 then
  130.     properties.windowWidth = 48
  131.     properties.windowX = 2
  132.     properties.colors = {
  133.         window = {
  134.             background = 1,
  135.             text = 0,
  136.             shadow = 0
  137.         },
  138.         title = {
  139.             background = 0,
  140.             text = 1,
  141.         },
  142.         progressBar = {
  143.             active = 0,
  144.             passive = 1
  145.         }
  146.     }
  147. else
  148.     properties.windowWidth = 56
  149.     properties.colors = {
  150.         window = {
  151.             background = 0xEEEEEE,
  152.             text = 0x999999,
  153.             shadow = 0x3C3C3C
  154.         },
  155.         title = {
  156.             background = 0xCCCCCC,
  157.             text = 0x555555,
  158.         },
  159.         progressBar = {
  160.             active = 0x0092FF,
  161.             passive = 0xCCCCCC
  162.         }
  163.     }
  164. end
  165.  
  166.  
  167.  
  168. ---------------------------------------------------------------------------------------------------------------------------------
  169.  
  170. local screenWidth, screenHeight = gpu.getResolution()
  171. properties.windowHeight = 8
  172.  
  173. if properties.windowWidth < 1 then
  174.     properties.windowWidth = math.floor(screenWidth * properties.windowWidth)
  175. end
  176. progressBarWidth = properties.windowWidth - properties.GUIElementsOffset * 2
  177.  
  178. if not properties.windowX then
  179.     properties.windowX = math.floor(screenWidth / 2 - properties.windowWidth / 2)
  180. end
  181.  
  182. if not properties.windowY then
  183.     properties.windowY = math.floor(screenHeight / 2 - properties.windowHeight / 2)
  184. end
  185.  
  186. local currentBackground, currentForeground
  187.  
  188. ---------------------------------------------------------------------------------------------------------------------------------
  189.  
  190. local function setBackground(color)
  191.     if currentBackground ~= color then
  192.         gpu.setBackground(color)
  193.         currentBackground = color
  194.     end
  195. end
  196.  
  197. local function setForeground(color)
  198.     if currentForeground ~= color then
  199.         gpu.setForeground(color)
  200.         currentForeground = color
  201.     end
  202. end
  203.  
  204. local function rectangle(x, y, width, height, color)
  205.     setBackground(color)
  206.     gpu.fill(x, y, width, height, " ")
  207. end
  208.  
  209. local function centerizedText(y, color, text)
  210.     local textLength = unicode.len(text)
  211.     if textLength > progressBarWidth then
  212.         text = unicode.sub(text, 1, progressBarWidth)
  213.         textLength = progressBarWidth
  214.     end
  215.  
  216.     setForeground(color)
  217.     gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep(" ", progressBarWidth))
  218.     gpu.set(math.floor(properties.windowX + properties.GUIElementsOffset + progressBarWidth / 2 - textLength / 2), y, text)
  219. end
  220.  
  221. local function progressBar(y, percent, text, totalProgress, currentProgress, currentFile)
  222.     setForeground(properties.colors.progressBar.passive)
  223.     gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep("━", progressBarWidth))
  224.     setForeground(properties.colors.progressBar.active)
  225.     gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep("━", math.ceil(progressBarWidth * percent)))
  226.  
  227.     text = text:gsub("<totalProgress>", totalProgress)
  228.     text = text:gsub("<currentProgress>", currentProgress)
  229.     text = text:gsub("<currentFile>", currentFile)
  230.  
  231.     centerizedText(y + 1, properties.colors.window.text, text)
  232. end
  233.  
  234. local function download(url, path, totalProgress)
  235.     fs.makeDirectory(fs.path(path))
  236.  
  237.     local file, fileReason = io.open(path, "w")
  238.     if file then
  239.         local pcallSuccess, requestHandle = pcall(internet.request, url)
  240.         if pcallSuccess then
  241.             if requestHandle then
  242.                 local y = properties.windowY + 2
  243.                 progressBar(y, 0, properties.localization.currentFile, totalProgress, "0", path)
  244.                
  245.                 local responseCode, responseName, responseData
  246.                 repeat
  247.                     responseCode, responseName, responseData = requestHandle:response()
  248.                 until responseCode
  249.  
  250.                 if responseData and responseData["Content-Length"] then
  251.                     local contentLength = tonumber(responseData["Content-Length"][1])
  252.                     local currentLength = 0
  253.                     while true do
  254.                         local data, reason = requestHandle.read(math.huge)
  255.                         if data then
  256.                             currentLength = currentLength + unicode.len(data)
  257.                             local percent = currentLength / contentLength
  258.                             progressBar(y, percent, properties.localization.currentFile, totalProgress, tostring(math.ceil(percent)), path)
  259.  
  260.                             file:write(data)
  261.                         else
  262.                             requestHandle:close()
  263.                             if reason then
  264.                                 error(reason)
  265.                             else
  266.                                 file:close()
  267.                                 return
  268.                             end
  269.                         end
  270.                     end
  271.                 else
  272.                     error("Response Content-Length header is missing: " .. tostring(responseCode) .. " " .. tostring(responseName))
  273.                 end
  274.             else
  275.                 error("Invalid URL-address: " .. tostring(url))
  276.             end
  277.         else
  278.             error("Usage: component.internet.request(string url)")
  279.         end
  280.  
  281.         file:close()
  282.     else
  283.         error("Failed to open file for writing: " .. tostring(fileReason))
  284.     end
  285. end
  286.  
  287. ---------------------------------------------------------------------------------------------------------------------------------
  288.  
  289. local oldPixels = {}
  290. for y = properties.windowY, properties.windowY + properties.windowHeight do
  291.     oldPixels[y] = {}
  292.     for x = properties.windowX, properties.windowX + properties.windowWidth do
  293.         oldPixels[y][x] = { gpu.get(x, y) }
  294.     end
  295. end
  296.  
  297. local function shadowPixel(x, y, symbol)
  298.     setBackground(oldPixels[y][x][3])
  299.     gpu.set(x, y, symbol)
  300. end
  301.  
  302. rectangle(properties.windowX + properties.windowWidth, properties.windowY + 1, 1, properties.windowHeight - 1, properties.colors.window.shadow)
  303. setForeground(properties.colors.window.shadow)
  304. shadowPixel(properties.windowX + properties.windowWidth, properties.windowY, "▄")
  305.  
  306. for i = properties.windowX + 1, properties.windowX + properties.windowWidth do
  307.     shadowPixel(i, properties.windowY + properties.windowHeight, "▀")
  308. end
  309.  
  310. rectangle(properties.windowX, properties.windowY + 1, properties.windowWidth, properties.windowHeight - 1, properties.colors.window.background)
  311.  
  312. rectangle(properties.windowX, properties.windowY, properties.windowWidth, 1, properties.colors.title.background)
  313. centerizedText(properties.windowY, properties.colors.title.text, properties.localization.title)
  314. setBackground(properties.colors.window.background)
  315.  
  316. local y = properties.windowY + 5
  317. progressBar(y, 0, properties.localization.totalProgress, "0", "0", files[1].path)
  318. for i = 1, #files do
  319.     local percent = i / #files
  320.     local totalProgress = tostring(math.ceil(percent * 100))
  321.     download(files[i].url, files[i].path, totalProgress)
  322.     progressBar(y, percent, properties.localization.totalProgress, totalProgress, "0", files[i].path)
  323. end
  324.  
  325. if properties.localization.finished1 then
  326.     rectangle(properties.windowX, properties.windowY + 1, properties.windowWidth, properties.windowHeight - 1, properties.colors.window.background)
  327.     centerizedText(properties.windowY + 2, properties.colors.window.text, properties.localization.finished1)
  328.     centerizedText(properties.windowY + 3, properties.colors.window.text, properties.localization.finished2)
  329.     centerizedText(properties.windowY + 4, properties.colors.window.text, properties.localization.finished3)
  330.     centerizedText(properties.windowY + 6, properties.colors.window.text, properties.localization.finished4)
  331.  
  332.     while true do
  333.         local eventType = event.pull()
  334.         if eventType == "key_down" or eventType == "touch" then
  335.             break
  336.         end
  337.     end
  338. end
  339.  
  340. for y = properties.windowY, properties.windowY + properties.windowHeight do
  341.     for x = properties.windowX, properties.windowX + properties.windowWidth do
  342.         setBackground(oldPixels[y][x][3])
  343.         setForeground(oldPixels[y][x][2])
  344.         gpu.set(x, y, oldPixels[y][x][1])
  345.     end
  346. end
RAW Paste Data