Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local c = component.invoke
- local computer = computer or require("computer")
- local bi = function(a, m, ...)
- local r = table.pack(pcall(c, a, m, ...))
- return r[1], table.unpack(r, 2, r.n)
- 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 tryLoad = function(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 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, l
- end
- local eraseDrive = function(a)
- local g = component.list("gpu")()
- local w, h = bi(g, "getResolution")
- local bx, by, bw, bh = math.floor((w - 60) / 2), math.floor((h - 4) / 2), 60, 4
- bi(g, "setBackground", 0x0000FF)
- bi(g, "fill", bx, by, bw, bh, " ")
- bi(g, "setForeground", 0xFFFFFF)
- bi(g, "setBackground", 0x0000FF)
- bi(g, "set", bx + 2, by + 1, "Erase drive? (Y/N)")
- while true do
- local e = { computer.pullSignal() }
- if e[1] == "key_down" then
- if e[3] == 21 or e[3] == 49 then
- bi(a, "remove", "/init.lua")
- bi(a, "remove", "/boot.lua")
- computer.beep(800, 0.2)
- computer.beep(800, 0.6)
- computer.beep(800, 0.2)
- computer.shutdown(true)
- break
- elseif e[3] == 49 or e[3] == 21 then
- break
- end
- end
- end
- end
- local drives = {}
- for a in component.list("filesystem") do
- local b = bi(a, "getLabel") or "Unnamed"
- if b ~= "tmpfs" then drives[#drives + 1] = { a, b } end
- end
- local displayError = function(msg)
- local g = component.list("gpu")()
- local w, h = bi(g, "getResolution")
- local bx, by, bw, bh = math.floor((w - 60) / 2), math.floor((h - 4) / 2), 60, 4
- bi(g, "setBackground", 0x0000FF)
- bi(g, "fill", bx, by, bw, bh, " ")
- bi(g, "setForeground", 0xFF0000)
- bi(g, "setBackground", 0x0000FF)
- local tx = math.floor((bw - #msg) / 2) + bx
- local ty = math.floor((bh - 1) / 2) + by
- bi(g, "set", tx, ty, msg)
- computer.beep(800, 1)
- computer.beep(800, 0.5)
- computer.beep(800, 0.5)
- end
- local displayMenu = function(selIndex)
- local g = component.list("gpu")()
- local w, h = bi(g, "getResolution")
- local bx, by, bw, bh = math.floor((w - 60) / 2), math.floor((h - #drives - 4) / 2), 60, #drives + 4
- bi(g, "setBackground", 0x0000FF)
- bi(g, "fill", bx, by, bw, bh, " ")
- bi(g, "setForeground", 0xFFFFFF)
- bi(g, "setBackground", 0x0000FF)
- bi(g, "set", bx + 2, by + 1, "Select OS to boot:")
- for i, drive in ipairs(drives) do
- local lineY = by + i + 2
- local textColor = i == selIndex and 0xFF0000 or 0xFFFFFF
- bi(g, "setForeground", textColor)
- bi(g, "set", bx + 2, lineY, drive[2] .. " (" .. drive[1] .. ")")
- end
- computer.beep(800, 0.1)
- end
- local selIndex = math.ceil(#drives / 2)
- displayMenu(selIndex)
- while true do
- local e = { computer.pullSignal() }
- if e[1] == "key_down" then
- if e[4] == 200 and selIndex > 1 then
- selIndex = selIndex - 1
- displayMenu(selIndex)
- elseif e[4] == 208 and selIndex < #drives then
- selIndex = selIndex + 1
- displayMenu(selIndex)
- elseif e[4] == 18 then
- eraseDrive(drives[selIndex][1])
- elseif e[4] == 28 then
- local i, r = tryLoad(drives[selIndex][1])
- if i then
- computer.setBootAddress(drives[selIndex][1])
- computer.beep(800, 0.2)
- computer.beep(800, 0.6)
- computer.beep(800, 0.2)
- return i()
- else
- displayError("Unable to boot device!")
- computer.beep(800, 0.1)
- computer.beep(800, 0.1)
- computer.beep(800, 0.1)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement