Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function main()
- local ci = component.invoke
- local bi = function(address, method, ...)
- local result = table.pack(pcall(ci, address, method, ...))
- if not result[1] then return nil, result[2] else return table.unpack(result, 2, result.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"
- table.insert(l, {address = a, label = b})
- end
- local function displayError(errorMessage)
- local g = component.list("gpu")()
- local w, h = bi(g, "getResolution")
- local boxWidth, boxHeight = 60, 4
- local boxX, boxY = math.floor((w - boxWidth) / 2), math.floor((h - boxHeight) / 2)
- bi(g, "setBackground", 0x888888)
- bi(g, "fill", boxX, boxY, boxWidth, boxHeight, " ")
- bi(g, "setForeground", 0xFF0000) -- Red text
- bi(g, "setBackground", 0x888888)
- local textX = math.floor((boxWidth - #errorMessage) / 2) + boxX
- local textY = math.floor((boxHeight - 1) / 2) + boxY
- bi(g, "set", textX, textY, errorMessage)
- 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)
- bi(g, "setBackground", 0x888888)
- bi(g, "fill", boxX, boxY, boxWidth, boxHeight, " ")
- bi(g, "setForeground", 0xFFFFFF)
- bi(g, "setBackground", 0x888888)
- bi(g, "set", boxX + 2, boxY + 1, "Select OS to boot:")
- for i, o in ipairs(l) do
- local lN = boxY + i + 2
- local tC = i == sI and 0xFF0000 or 0xFFFFFF
- bi(g, "setForeground", tC)
- bi(g, "set", boxX + 2, lN, o.label .. " (" .. o.address .. ")")
- end
- computer.beep(1500, 0.1) -- Shorter beep
- 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(1500, 0.2) -- Short beep
- computer.beep(1500, 0.6) -- Long beep
- computer.beep(1500, 0.2) -- Short beep
- return i()
- else
- displayError("Unable to boot device!") -- Display error message
- computer.beep(1500, 0.1) -- Shorter beep
- computer.beep(1500, 0.1) -- Shorter beep
- computer.beep(1500, 0.1) -- Shorter beep
- end
- end
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement