Advertisement
VlaD00m

ecs init

Oct 12th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.78 KB | None | 0 0
  1.  
  2. local backgroundColor = 0x262626
  3. local foregroundColor = 0xcccccc
  4.  
  5. do
  6.  
  7.   _G._OSVERSION = "OpenOS 1.5"
  8.  
  9.   local component = component
  10.   local computer = computer
  11.   local unicode = unicode
  12.  
  13.   -- Runlevel information.
  14.   local runlevel, shutdown = "S", computer.shutdown
  15.   computer.runlevel = function() return runlevel end
  16.   computer.shutdown = function(reboot)
  17.     runlevel = reboot and 6 or 0
  18.     if os.sleep then
  19.       computer.pushSignal("shutdown")
  20.       os.sleep(0.1) -- Allow shutdown processing.
  21.     end
  22.     shutdown(reboot)
  23.   end
  24.  
  25.   -- Low level dofile implementation to read filesystem libraries.
  26.   local rom = {}
  27.   function rom.invoke(method, ...)
  28.     return component.invoke(computer.getBootAddress(), method, ...)
  29.   end
  30.   function rom.open(file) return rom.invoke("open", file) end
  31.   function rom.read(handle) return rom.invoke("read", handle, math.huge) end
  32.   function rom.close(handle) return rom.invoke("close", handle) end
  33.   function rom.inits() return ipairs(rom.invoke("list", "boot")) end
  34.   function rom.isDirectory(path) return rom.invoke("isDirectory", path) end
  35.  
  36.   local screen = component.list('screen',true)()
  37.   for address in component.list('screen',true) do
  38.     if #component.invoke(address, 'getKeyboards') > 0 then
  39.       screen = address
  40.     end
  41.   end
  42.  
  43.   -- Report boot progress if possible.
  44.   local gpu = component.list("gpu", true)()
  45.   local w, h
  46.   if gpu and screen then
  47.     component.invoke(gpu, "bind", screen)
  48.     w, h = component.invoke(gpu, "getResolution")
  49.     component.invoke(gpu, "setResolution", w, h)
  50.     component.invoke(gpu, "setBackground", backgroundColor)
  51.     component.invoke(gpu, "setForeground", foregroundColor)
  52.     component.invoke(gpu, "fill", 1, 1, w, h, " ")
  53.   end
  54.   local y = 1
  55.   local function status(msg)
  56.  
  57.  
  58.     local yPos = math.floor(h / 2)
  59.     local length = #msg
  60.     local xPos = math.floor(w / 2 - length / 2)
  61.  
  62.     component.invoke(gpu, "fill", 1, yPos, w, 1, " ")
  63.     component.invoke(gpu, "set", xPos, yPos, msg)
  64.  
  65.     -- if gpu and screen then
  66.     --   component.invoke(gpu, "set", 1, y, msg)
  67.     --   if y == h then
  68.     --     component.invoke(gpu, "copy", 1, 2, w, h - 1, 0, -1)
  69.     --     component.invoke(gpu, "fill", 1, h, w, 1, " ")
  70.     --   else
  71.     --     y = y + 1
  72.     --   end
  73.     -- end
  74.   end
  75.  
  76.   status("Booting " .. _OSVERSION .. "...")
  77.  
  78.   -- Custom low-level loadfile/dofile implementation reading from our ROM.
  79.   local function loadfile(file)
  80.     status("> " .. file)
  81.     local handle, reason = rom.open(file)
  82.     if not handle then
  83.       error(reason)
  84.     end
  85.     local buffer = ""
  86.     repeat
  87.       local data, reason = rom.read(handle)
  88.       if not data and reason then
  89.         error(reason)
  90.       end
  91.       buffer = buffer .. (data or "")
  92.     until not data
  93.     rom.close(handle)
  94.     return load(buffer, "=" .. file)
  95.   end
  96.  
  97.   local function dofile(file)
  98.     local program, reason = loadfile(file)
  99.     if program then
  100.       local result = table.pack(pcall(program))
  101.       if result[1] then
  102.         return table.unpack(result, 2, result.n)
  103.       else
  104.         error(result[2])
  105.       end
  106.     else
  107.       error(reason)
  108.     end
  109.   end
  110.  
  111.   status("Initializing package management...")
  112.  
  113.   -- Load file system related libraries we need to load other stuff moree
  114.   -- comfortably. This is basically wrapper stuff for the file streams
  115.   -- provided by the filesystem components.
  116.   local package = dofile("/lib/package.lua")
  117.  
  118.   do
  119.     -- Unclutter global namespace now that we have the package module.
  120.     --_G.component = nil
  121.     _G.computer = nil
  122.     _G.process = nil
  123.     _G.unicode = nil
  124.  
  125.     -- Initialize the package module with some of our own APIs.
  126.     package.preload["buffer"] = loadfile("/lib/buffer.lua")
  127.     package.preload["component"] = function() return component end
  128.     package.preload["computer"] = function() return computer end
  129.     package.preload["filesystem"] = loadfile("/lib/filesystem.lua")
  130.     package.preload["io"] = loadfile("/lib/io.lua")
  131.     package.preload["unicode"] = function() return unicode end
  132.  
  133.     -- Inject the package and io modules into the global namespace, as in Lua.
  134.     _G.package = package
  135.     _G.io = require("io")
  136.        
  137.   end
  138.  
  139.   status("Initializing file system...")
  140.  
  141.   -- Mount the ROM and temporary file systems to allow working on the file
  142.   -- system module from this point on.
  143.   local filesystem = require("filesystem")
  144.   filesystem.mount(computer.getBootAddress(), "/")
  145.  
  146.   status("Running boot scripts...")
  147.  
  148.   -- Run library startup scripts. These mostly initialize event handlers.
  149.   local scripts = {}
  150.   for _, file in rom.inits() do
  151.     local path = "boot/" .. file
  152.     if not rom.isDirectory(path) then
  153.       table.insert(scripts, path)
  154.     end
  155.   end
  156.   table.sort(scripts)
  157.   for i = 1, #scripts do
  158.     dofile(scripts[i])
  159.   end
  160.  
  161.   status("Initializing components...")
  162.  
  163.   local primaries = {}
  164.   for c, t in component.list() do
  165.     local s = component.slot(c)
  166.     if (not primaries[t] or (s >= 0 and s < primaries[t].slot)) and t ~= "screen" then
  167.       primaries[t] = {address=c, slot=s}
  168.     end
  169.     computer.pushSignal("component_added", c, t)
  170.   end
  171.   for t, c in pairs(primaries) do
  172.     component.setPrimary(t, c.address)
  173.   end
  174.   os.sleep(0.5) -- Allow signal processing by libraries.
  175.   computer.pushSignal("init") -- so libs know components are initialized.
  176.  
  177.  -- status("Initializing system...")
  178.   --require("term").clear()
  179.   os.sleep(0.1) -- Allow init processing.
  180.   runlevel = 1
  181. end
  182.  
  183. local function motd()
  184.   local f = io.open("/etc/motd")
  185.   if not f then
  186.     return
  187.   end
  188.   if f:read(2) == "#!" then
  189.     f:close()
  190.     os.execute("/etc/motd")
  191.   else
  192.     f:seek("set", 0)
  193.     io.write(f:read("*a") .. "\n")
  194.     f:close()
  195.   end
  196. end
  197.  
  198. --Загружаем необходимые библиотеки, дабы избежать потерей памяти
  199. _G._OSLANGUAGE = require("System/OS/Language")
  200. _G.ecs = require("ECSAPI")
  201. _G.config = require("config")
  202. _G.context = require("context")
  203. _G.image = require("image")
  204. _G.zip = require("zip")
  205. _G.fs = require("filesystem")
  206. _G.shell = require("shell")
  207. _G.component = require("component")
  208. _G.unicode = require("unicode")
  209. _G.keyboard = require("keyboard")
  210. _G.computer = require("computer")
  211. _G.gpu = _G.component.gpu
  212.  
  213. --Oткрываем порт для беспороводных MineOS-соединений
  214. if component.isAvailable("modem") then component.modem.open(512) end
  215.  
  216. --Очищаем экран и запускаем ОС
  217. ecs.clearScreen()
  218. ecs.setScale(1)
  219. shell.execute("OS.lua")
  220.  
  221. while true do
  222.   component.gpu.setBackground(backgroundColor)
  223.   component.gpu.setForeground(foregroundColor)
  224.   local w, h = component.gpu.getResolution()
  225.   component.gpu.fill(1, 1, w, h, " ")
  226.  
  227.  
  228.  
  229.   motd()
  230.   local result, reason = os.execute(os.getenv("SHELL"))
  231.   if not result then
  232.     io.stderr:write((tostring(reason) or "unknown error") .. "\n")
  233.     io.write("Press any key to continue.\n")
  234.     os.sleep(0.5)
  235.     require("event").pull("key")
  236.   end
  237.   require("term").clear()
  238. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement