Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function main()
- local ci, computer, component = component.invoke, computer or require("computer"), require("component")
- local function bi(a, m, ...)
- local r = table.pack(pcall(ci, a, m, ...))
- return r[1] and table.unpack(r, 2, r.n) or nil, r[2]
- end
- local e = component.list("eeprom")()
- computer.getBootAddress = function() return bi(e, "getData") end
- computer.setBootAddress = function(a) return bi(e, "setData", a) end
- local s, g = component.list("screen")(), component.list("gpu")()
- if g and s then bi(g, "bind", s) end
- local function tryLoad(a)
- local h, r = bi(a, "open", "/init.lua")
- if not h then return nil, "Failed to open /init.lua: " .. tostring(r) end
- local b = ""
- repeat
- local d, r = bi(a, "read", h, math.huge)
- if not d and r then bi(a, "close", h) return nil, "Failed to read /init.lua: " .. tostring(r) end
- b = b .. (d or "")
- until not d
- bi(a, "close", h)
- local i, l = load(b, "=init")
- return i or nil, "Failed to load /init.lua: " .. tostring(l)
- end
- local l = {}
- for a in component.list("filesystem") do
- local b = bi(a, "getLabel") or "Unnamed Drive"
- if b ~= "tmpfs" then table.insert(l, { address = a, label = b }) end
- end
- local function displayError(msg)
- local g = component.list("gpu")()
- local w, h = bi(g, "getResolution")
- local boxX, boxY = math.floor((w - 80) / 2), math.floor((h - 6) / 2)
- bi(g, "setBackground", 0x001f3f) bi(g, "fill", boxX, boxY, 80, 6, " ")
- bi(g, "setForeground", 0xFF4500)
- bi(g, "set", boxX + math.floor((80 - #msg) / 2), boxY + 3, msg)
- computer.beep(800, 1) computer.beep(800, 0.5) computer.beep(800, 0.5)
- while computer.pullSignal() ~= "key_down" do end
- computer.shutdown(true)
- end
- local function displayMenu(sI)
- local g = component.list("gpu")()
- local w, h = bi(g, "getResolution")
- local boxX, boxY = math.floor((w - 60) / 2), math.floor((h - (#l + 4)) / 2)
- bi(g, "setBackground", 0x001f3f) bi(g, "fill", boxX, boxY, 60, #l + 4, " ")
- bi(g, "setForeground", 0xFFFFFF)
- bi(g, "set", boxX + 2, boxY + 1, "Select OS to boot:")
- for i, o in ipairs(l) do
- bi(g, "setForeground", i == sI and 0xFFD700 or 0xFFFFFF)
- bi(g, "set", boxX + 2, boxY + i + 2, o.label .. " (" .. o.address .. ")")
- end
- computer.beep(800, 0.1)
- end
- local sI = math.ceil(#l / 2)
- displayMenu(sI)
- while true do
- local e = { computer.pullSignal() }
- if e[1] == "key_down" then
- if e[4] == 200 and sI > 1 then sI = sI - 1 displayMenu(sI)
- elseif e[4] == 208 and sI < #l then sI = sI + 1 displayMenu(sI)
- elseif e[4] == 28 then
- local o = l[sI]
- local i, r = tryLoad(o.address)
- if i then
- computer.setBootAddress(o.address)
- computer.beep(800, 0.2) computer.beep(800, 0.6) computer.beep(800, 0.2)
- return i()
- else
- displayError("ERROR: Unable to boot from " .. o.label .. ". Check the device and try again.")
- computer.beep(800, 0.1) computer.beep(800, 0.1) computer.beep(800, 0.1)
- end
- end
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement