Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function main()
- local ci = component.invoke
- local computer = computer or require("computer")
- local bi = function(a, m, ...)
- local r = table.pack(pcall(ci, a, m, ...))
- if not r[1] then return nil, r[2] else return table.unpack(r, 2, r.n) end
- end
- local e = component.list("eeprom")()
- computer.getBootAddress = function() return bi(e, "getData") end
- computer.setBootAddress = function(a) return bi(e, "setData", a) end
- do
- local s, g = component.list("screen")(), component.list("gpu")()
- if g and s then bi(g, "bind", s) end
- 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.maxinteger or 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")
- if not i then return nil, "Failed to load /init.lua: " .. tostring(l) end
- return i
- 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(errorMessage)
- local g = component.list("gpu")()
- local w, h = bi(g, "getResolution")
- local boxWidth, boxHeight = 80, 6 -- Increased width
- local boxX, boxY = math.floor((w - boxWidth) / 2), math.floor((h - boxHeight) / 2)
- bi(g, "setBackground", 0x001f3f) -- Dark blue background
- bi(g, "fill", boxX, boxY, boxWidth, boxHeight, " ")
- bi(g, "setForeground", 0xFF4500) -- Orange-red text
- bi(g, "setBackground", 0x001f3f) -- Dark blue background
- local textX = math.floor((boxWidth - #errorMessage) / 2) + boxX
- local textY = math.floor((boxHeight - 1) / 2) + boxY
- bi(g, "set", textX, textY, errorMessage)
- computer.beep(800, 1) computer.beep(800, 0.5) computer.beep(800, 0.5)
- -- Wait for any key press before rebooting
- while true do
- local e = { computer.pullSignal() }
- if e[1] == "key_down" then
- computer.shutdown(true)
- break
- end
- end
- end
- local function displayMenu(sI)
- local g = component.list("gpu")()
- local w, h = bi(g, "getResolution")
- local boxWidth, boxHeight = 60, #l + 4
- local boxX, boxY = math.floor((w - boxWidth) / 2), math.floor((h - boxHeight) / 2)
- local filteredList = {}
- for _, o in ipairs(l) do
- if o.label ~= "tmpfs" then table.insert(filteredList, o) end
- end
- if #filteredList == 0 then
- displayError("ERROR: No bootable medium found! Insert a bootable device.")
- return
- end
- bi(g, "setBackground", 0x001f3f) bi(g, "fill", boxX, boxY, boxWidth, boxHeight, " ")
- bi(g, "setForeground", 0xFFFFFF) bi(g, "setBackground", 0x001f3f)
- bi(g, "set", boxX + 2, boxY + 1, "Select OS to boot:")
- for i, o in ipairs(filteredList) do
- local lN = boxY + i + 2
- local tC = i == sI and 0xFFD700 or 0xFFFFFF
- bi(g, "setForeground", tC) bi(g, "set", boxX + 2, lN, 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