Advertisement
Cornchips

Opencomputers OS

May 10th, 2016
9,440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.69 KB | None | 0 0
  1. local component = require("component")
  2. local computer = require("computer")
  3. local shell = require("shell")
  4. local unicode = require("unicode")
  5. local event = require("event")
  6. local fs = require("filesystem")
  7. local internet = require("internet")
  8. local seri = require("serialization")
  9. local gpu = component.gpu
  10. shell.setWorkingDirectory("")
  11.  
  12. -----------------Computer checking for compliance with the system requirements--------------------------
  13.  
  14. --Create an array of stuff
  15. local stuff = {}
  16.  
  17. print(" ")
  18. print("Analyzing computer for matching system requirements...")
  19.  
  20. --Check whether it is a tablet
  21. if component.isAvailable("tablet") then
  22.     table.insert(stuff, "Tablet PC detected - You can't install MineOS on tablet because of primitive GPU and Screen.")
  23. end
  24.  
  25. --Check the GPU
  26. if gpu.maxResolution() < 150 then
  27.     table.insert(stuff, "Bad GPU or Screen - MineOS requires Tier 3 GPU and Tier 3 Screen.")
  28. end
  29.  
  30. --Checks the RAM
  31. if math.floor(computer.totalMemory() / 1024 ) < 1536 then
  32.     table.insert(stuff, "Not enough RAM - MineOS requires at least 1536 KB RAM.")
  33. end
  34.  
  35. if fs.get("bin/edit.lua") == nil or fs.get("bin/edit.lua").isReadOnly() then
  36.     table.insert(stuff, "You can't install MineOS on floppy disk. Run \"install\" in command line and install OpenOS from floppy to HDD first. After that you're be able to install MineOS from Pastebin.")
  37. end
  38.  
  39. --If found any discrepancy ICU requirements , then write that it is not so
  40. if #stuff> 0 then
  41.   print(" ")
  42.   for i = 1, #stuff do
  43.     print(stuff[i])
  44.   end
  45.   print(" ")
  46.   return
  47. else
  48.   print("Done, everything's good. Proceed to downloading.")
  49.   print(" ")
  50. end
  51.  
  52. ------------------------------------------------------------------------------------------
  53.  
  54. local lang
  55.  
  56. local applications
  57.  
  58. local padColor = 0x262626
  59. local installerScale = 1
  60.  
  61. local timing = 0.2
  62.  
  63. -----------------------------STAGE TRAINING-------------------------------------------
  64.  
  65.  
  66. --Gets the files from GitHub
  67. local function getFromGitHub(url, path)
  68.   local sContent = ""
  69.   local result, response = pcall(internet.request, url)
  70.   if not result then
  71.     return nil
  72.   end
  73.  
  74.   if fs.exists(path) then fs.remove(path) end
  75.   fs.makeDirectory(fs.path(path))
  76.   local file = io.open(path, "w")
  77.  
  78.   for chunk in response do
  79.     file:write(chunk)
  80.     sContent = sContent .. chunk
  81.   end
  82.  
  83.   file:close()
  84.  
  85.   return sContent
  86. end
  87.  
  88. --Save Files
  89. local function getFromGitHubSafely(url, path)
  90.   local success, sRepos = pcall(getFromGitHub, url, path)
  91.   if not success then
  92.     io.stderr:write("Can't download \"" .. url .. "\"!\n")
  93.     return -1
  94.   end
  95.   return sRepos
  96. end
  97.  
  98. --Gets files from pastebin
  99. local function getFromPastebin(paste, filename)
  100.   local cyka = ""
  101.   local f, reason = io.open(filename, "w")
  102.   if not f then
  103.     io.stderr:write("Failed opening file for writing: " .. reason)
  104.     return
  105.   end
  106.   --io.write("Downloading from pastebin.com... ")
  107.   local url = "http://pastebin.com/raw.php?i=" .. paste
  108.   local result, response = pcall(internet.request, url)
  109.   if result then
  110.     --io.write("success.\n")
  111.     for chunk in response do
  112.       --if not options.k then
  113.         --string.gsub(chunk, "\r\n", "\n")
  114.       --end
  115.       f:write(chunk)
  116.       cyka = cyka .. chunk
  117.     end
  118.     f:close()
  119.     --io.write("Saved data to " .. filename .. "\n")
  120.   else
  121.     f:close()
  122.     fs.remove(filename)
  123.     io.stderr:write("HTTP request failed: " .. response .. "\n")
  124.   end
  125.  
  126.   return cyka
  127. end
  128.  
  129. local GitHubUserUrl = "https://raw.githubusercontent.com/"
  130.  
  131.  
  132. --------------------------------- Download all the necessary items ---------------------------------
  133.  
  134.  
  135. local preLoadApi = {
  136.   { paste = "IgorTimofeev/OpenComputers/master/lib/ECSAPI.lua", path = "lib/ECSAPI.lua" },
  137.   { paste = "IgorTimofeev/OpenComputers/master/lib/colorlib.lua", path = "lib/colorlib.lua" },
  138.   { paste = "IgorTimofeev/OpenComputers/master/lib/image.lua", path = "lib/image.lua" },
  139.   { paste = "IgorTimofeev/OpenComputers/master/lib/config.lua", path = "lib/config.lua" },
  140.   { paste = "IgorTimofeev/OpenComputers/master/MineOS/Icons/Languages.pic", path = "MineOS/System/OS/Icons/Languages.pic" },
  141.   { paste = "IgorTimofeev/OpenComputers/master/MineOS/Icons/OK.pic", path = "MineOS/System/OS/Icons/OK.pic" },
  142.   { paste = "IgorTimofeev/OpenComputers/master/MineOS/Icons/Downloading.pic", path = "MineOS/System/OS/Icons/Downloading.pic" },
  143.   { paste = "IgorTimofeev/OpenComputers/master/MineOS/Icons/OS_Logo.pic", path = "MineOS/System/OS/Icons/OS_Logo.pic" },
  144. }
  145.  
  146. print("Downloading file list")
  147. applications = seri.unserialize(getFromGitHubSafely(GitHubUserUrl .. "IgorTimofeev/OpenComputers/master/Applications.txt", "MineOS/System/OS/Applications.txt"))
  148. print(" ")
  149.  
  150. for i = 1, #preLoadApi do
  151.   print("Downloading must-have files (" .. fs.name(preLoadApi[i].path) .. ")")
  152.   getFromGitHubSafely(GitHubUserUrl .. preLoadApi[i].paste, preLoadApi[i].path)
  153. end
  154.  
  155. print(" ")
  156.  
  157. package.loaded.ecs = nil
  158. package.loaded.ECSAPI = nil
  159. _G.ecs = require("ECSAPI")
  160. _G.image = require("image")
  161. _G.config = require("config")
  162.  
  163. local imageOS = image.load("MineOS/System/OS/Icons/OS_Logo.pic")
  164. local imageLanguages = image.load("MineOS/System/OS/Icons/Languages.pic")
  165. local imageDownloading = image.load("MineOS/System/OS/Icons/Downloading.pic")
  166. local imageOK = image.load("MineOS/System/OS/Icons/OK.pic")
  167.  
  168. ecs.setScale(installerScale)
  169.  
  170. local xSize, ySize = gpu.getResolution()
  171. local windowWidth = 80
  172. local windowHeight = 2 + 16 + 2 + 3 + 2
  173. local xWindow, yWindow = math.floor(xSize / 2 - windowWidth / 2), math.ceil(ySize / 2 - windowHeight / 2)
  174. local xWindowEnd, yWindowEnd = xWindow + windowWidth - 1, yWindow + windowHeight - 1
  175.  
  176.  
  177. -------------------------------------------------------------------------------------------
  178.  
  179. local function clear()
  180.   ecs.blankWindow(xWindow, yWindow, windowWidth, windowHeight)
  181. end
  182.  
  183. --ОБЪЕКТЫ
  184. local obj = {}
  185. local function newObj(class, name, ...)
  186.   obj[class] = obj[class] or {}
  187.   obj[class][name] = {...}
  188. end
  189.  
  190. local function drawButton(name, isPressed)
  191.   local buttonColor = 0x888888
  192.   if isPressed then buttonColor = ecs.colors.blue end
  193.   local d = { ecs.drawAdaptiveButton("auto", yWindowEnd - 3, 2, 1, name, buttonColor, 0xffffff) }
  194.   newObj("buttons", name, d[1], d[2], d[3], d[4])
  195. end
  196.  
  197. local function waitForClickOnButton(buttonName)
  198.   while true do
  199.     local e = { event.pull() }
  200.     if e[1] == "touch" then
  201.       if ecs.clickedAtArea(e[3], e[4], obj["buttons"][buttonName][1], obj["buttons"][buttonName][2], obj["buttons"][buttonName][3], obj["buttons"][buttonName][4]) then
  202.         drawButton(buttonName, true)
  203.         os.sleep(timing)
  204.         break
  205.       end
  206.     end
  207.   end
  208. end
  209.  
  210.  
  211. ------------------------------LANGUAGE------------------------------------
  212.  
  213. ecs.prepareToExit()
  214.  
  215. local downloadWallpapers, showHelpTips = false, false
  216.  
  217. do
  218.  
  219.   clear()
  220.  
  221.   image.draw(math.ceil(xSize / 2 - 30), yWindow + 2, imageLanguages)
  222.  
  223.   --Draw dat button
  224.   drawButton("Select language",false)
  225.  
  226.   waitForClickOnButton("Select language")
  227.  
  228.   local data = ecs.universalWindow("auto", "auto", 36, 0x262626, true, {"EmptyLine"}, {"CenterText", ecs.colors.orange, "Select language"}, {"EmptyLine"}, {"Select", 0xFFFFFF, ecs.colors.green, "Russian", "English"}, {"EmptyLine"}, {"CenterText", ecs.colors.orange, "Change some OS properties"}, {"EmptyLine"}, {"Switch", 0xF2B233, 0xffffff, 0xFFFFFF, "Download wallpapers", true}, {"EmptyLine"}, {"Switch", 0xF2B233, 0xffffff, 0xFFFFFF, "Show help tips in OS", true}, {"EmptyLine"}, {"Button", {ecs.colors.orange, 0x262626, "OK"}})
  229.   downloadWallpapers, showHelpTips = data[2], data[3]
  230.  
  231.   --Sets the desired language
  232.   _G.OSSettings = { showHelpOnApplicationStart = showHelpTips, language = data[1] }
  233.   ecs.saveOSSettings()
  234.  
  235.   --Downloads language
  236.   ecs.info("auto", "auto", " ", " Installing language packages...")
  237.   local pathToLang = "MineOS/System/OS/Installer/Language.lang"
  238.   getFromGitHubSafely(GitHubUserUrl .. "IgorTimofeev/OpenComputers/master/Installer/" .. _G.OSSettings.language .. ".lang", pathToLang)
  239.   getFromGitHubSafely(GitHubUserUrl .. "IgorTimofeev/OpenComputers/master/MineOS/License/" .. _G.OSSettings.language .. ".txt", "MineOS/System/OS/License.txt")
  240.  
  241.   --Apply the language
  242.   lang = config.readAll(pathToLang)
  243.  
  244. end
  245.  
  246.  
  247. ------------------------------Get the axis------------------------------------
  248.  
  249. do
  250.   clear()
  251.  
  252.   image.draw(math.ceil(xSize / 2 - 15), yWindow + 2, imageOS)
  253.  
  254.   --Draw the center
  255.   gpu.setBackground(ecs.windowColors.background)
  256.   gpu.setForeground(ecs.colors.gray)
  257.   ecs.centerText("x", yWindowEnd - 5 , lang.beginOsInstall)
  258.  
  259.   --Button
  260.   drawButton("->",false)
  261.  
  262.   waitForClickOnButton("->")
  263.  
  264. end
  265.  
  266. ------------------------------User License------------------------------------------
  267.  
  268. do
  269.   clear()
  270.  
  271.   --How to draw acc conditions
  272.   local from = 1
  273.   local xText, yText, TextWidth, TextHeight = xWindow + 4, yWindow + 2, windowWidth - 8, windowHeight - 7
  274.  
  275.   --Read file sogll persons
  276.   local lines = {}
  277.   local file = io.open("MineOS/System/OS/License.txt", "r")
  278.   for line in file:lines() do
  279.     table.insert(lines, line)
  280.   end
  281.   file:close()
  282.  
  283.   --Draw thing
  284.   ecs.textField(xText, yText, TextWidth, TextHeight, lines, from, 0xffffff, 0x262626, 0x888888, ecs.colors.blue)
  285.  
  286.   --Draw information
  287.   --ecs.centerText("x", yWindowEnd - 5 ,"Принимаете ли вы условия лицензионного соглашения?")
  288.  
  289.   --Button
  290.   drawButton(lang.acceptLicense, false)
  291.  
  292.   while true do
  293.     local e = { event.pull() }
  294.     if e[1] == "touch" then
  295.       if ecs.clickedAtArea(e[3], e[4], obj["buttons"][lang.acceptLicense][1], obj["buttons"][lang.acceptLicense][2], obj["buttons"][lang.acceptLicense][3], obj["buttons"][lang.acceptLicense][4]) then
  296.         drawButton(lang.acceptLicense, true)
  297.         os.sleep(timing)
  298.         break
  299.       end
  300.     elseif e[1] == "scroll" then
  301.       if e[5] == -1 then
  302.         if from < #lines then from = from + 1; ecs.textField(xText, yText, TextWidth, TextHeight, lines, from, 0xffffff, 0x262626, 0x888888, ecs.colors.blue) end
  303.       else
  304.         if from > 1 then from = from - 1; ecs.textField(xText, yText, TextWidth, TextHeight, lines, from, 0xffffff, 0x262626, 0x888888, ecs.colors.blue) end
  305.       end
  306.     end
  307.   end
  308. end
  309.  
  310. -------------------------- Prepare file system ----------------------------------
  311.  
  312. --Create ways to start , and other little things purely for aesthetics
  313. local desktopPath = "MineOS/Desktop/"
  314. local dockPath = "MineOS/System/OS/Dock/"
  315. local applicationsPath = "MineOS/Applications/"
  316. local picturesPath = "MineOS/Pictures/"
  317.  
  318. fs.remove(desktopPath)
  319. fs.remove(dockPath)
  320.  
  321. fs.makeDirectory(desktopPath .. "My files")
  322. fs.makeDirectory(picturesPath)
  323. fs.makeDirectory(dockPath)
  324.  
  325. --------------------------Stages of loading-----------------------------------
  326.  
  327. do
  328.  
  329.   local barWidth = math.floor(windowWidth * 2 / 3)
  330.   local xBar = math.floor(xSize/2-barWidth/2)
  331.   local yBar = yWindowEnd - 3
  332.  
  333.   local function drawInfo(x, y, info)
  334.     ecs.square(x, y, barWidth, 1, ecs.windowColors.background)
  335.     ecs.colorText(x, y, ecs.colors.gray, info)
  336.   end
  337.  
  338.   ecs.blankWindow(xWindow,yWindow,windowWidth,windowHeight)
  339.  
  340.   image.draw(math.floor(xSize/2 - 33), yWindow + 2, imageDownloading)
  341.  
  342.   ecs.colorTextWithBack(xBar, yBar - 1, ecs.colors.gray, ecs.windowColors.background, lang.osInstallation)
  343.   ecs.progressBar(xBar, yBar, barWidth, 1, 0xcccccc, ecs.colors.blue, 0)
  344.   os.sleep(timing)
  345.  
  346.   for app = 1, #applications do
  347.     --ВСЕ ДЛЯ ГРАФОНА
  348.     drawInfo(xBar, yBar + 1, lang.downloading .. " " .. applications[app]["name"])
  349.     local percent = app / #applications * 100
  350.     ecs.progressBar(xBar, yBar, barWidth, 1, 0xcccccc, ecs.colors.blue, percent)
  351.  
  352.     ecs.getOSApplication(applications[app], downloadWallpapers)
  353.   end
  354.  
  355.   os.sleep(timing)
  356. end
  357.  
  358. --Создаем базовые обои рабочего стола
  359. ecs.createShortCut(desktopPath .. "Pictures.lnk", picturesPath)
  360. if downloadWallpapers then ecs.createShortCut("MineOS/System/OS/Wallpaper.lnk", picturesPath .. "Girl.pic") end
  361.  
  362. --Startup
  363. local file = io.open("autorun.lua", "w")
  364. file:write("local success, reason = pcall(loadfile(\"OS.lua\")); if not success then print(\"Ошибка: \" .. tostring(reason)) end")
  365. file:close()
  366.  
  367. --------------------------Reset Computer-----------------------------------
  368.  
  369. ecs.blankWindow(xWindow,yWindow,windowWidth,windowHeight)
  370.  
  371. image.draw(math.floor(xSize/2 - 16), math.floor(ySize/2 - 11), imageOK)
  372.  
  373. --Draw Centered Background
  374. gpu.setBackground(ecs.windowColors.background)
  375. gpu.setForeground(ecs.colors.gray)
  376. ecs.centerText("x",yWindowEnd - 5, lang.needToRestart)
  377.  
  378. --Button
  379. drawButton(lang.restart, false)
  380. waitForClickOnButton(lang.restart)
  381. ecs.prepareToExit()
  382.  
  383. computer.shutdown(true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement