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 function invoke(component, method, ...)
- local result = table.pack(pcall(component[method], ...))
- if not result[1] then return nil, result[2] else return table.unpack(result, 2, result.n) end
- end
- local function tryLoad(address)
- local handle, err = invoke(component.proxy(address), "open", "/init.lua")
- if not handle then return nil, "Failed to open /init.lua: " .. tostring(err) end
- local buffer = ""
- repeat
- local data, err = invoke(component.proxy(address), "read", handle, math.maxinteger or math.huge)
- if not data and err then
- invoke(component.proxy(address), "close", handle)
- return nil, "Failed to read /init.lua: " .. tostring(err)
- end
- buffer = buffer .. (data or "")
- until not data
- invoke(component.proxy(address), "close", handle)
- local fn, err = load(buffer, "=init")
- if not fn then return nil, "Failed to load /init.lua: " .. tostring(err) end
- return fn
- end
- local function displayError(errorMessage)
- local gpu = component.list("gpu")()
- local w, h = invoke(component.proxy(gpu), "getResolution")
- local boxWidth, boxHeight = 80, 6
- local boxX, boxY = math.floor((w - boxWidth) / 2), math.floor((h - boxHeight) / 2)
- invoke(component.proxy(gpu), "setBackground", 0x001f3f)
- invoke(component.proxy(gpu), "fill", boxX, boxY, boxWidth, boxHeight, " ")
- invoke(component.proxy(gpu), "setForeground", 0xFF4500)
- invoke(component.proxy(gpu), "setBackground", 0x001f3f)
- local textX = math.floor((boxWidth - #errorMessage) / 2) + boxX
- local textY = math.floor((boxHeight - 1) / 2) + boxY
- invoke(component.proxy(gpu), "set", textX, textY, errorMessage)
- computer.beep(800, 1)
- while true do
- local event = {computer.pullSignal()}
- if event[1] == "key_down" then
- computer.shutdown(true)
- break
- end
- end
- end
- local function displayMenu(selectedIndex)
- local gpu = component.list("gpu")()
- local w, h = invoke(component.proxy(gpu), "getResolution")
- local boxWidth, boxHeight = 60, 4 + #l
- local boxX, boxY = math.floor((w - boxWidth) / 2), math.floor((h - boxHeight) / 2)
- local drives = {}
- for address, label in component.list("filesystem") do
- if label ~= "tmpfs" then
- table.insert(drives, {address = address, label = label})
- end
- end
- if #drives == 0 then
- displayError("ERROR: No bootable medium found! Insert a bootable device.")
- return
- end
- invoke(component.proxy(gpu), "setBackground", 0x001f3f)
- invoke(component.proxy(gpu), "fill", boxX, boxY, boxWidth, boxHeight, " ")
- invoke(component.proxy(gpu), "setForeground", 0xFFFFFF)
- invoke(component.proxy(gpu), "setBackground", 0x001f3f)
- invoke(component.proxy(gpu), "set", boxX + 2, boxY + 1, "Select OS to boot:")
- for i, drive in ipairs(drives) do
- local lineY = boxY + i + 2
- local textColor = i == selectedIndex and 0xFFD700 or 0xFFFFFF
- invoke(component.proxy(gpu), "setForeground", textColor)
- invoke(component.proxy(gpu), "set", boxX + 2, lineY, drive.label .. " (" .. drive.address .. ")")
- end
- computer.beep(800, 0.1)
- end
- local selectedIndex = math.ceil(#l / 2)
- displayMenu(selectedIndex)
- while true do
- local event = {computer.pullSignal()}
- if event[1] == "key_down" then
- if event[4] == 200 and selectedIndex > 1 then
- selectedIndex = selectedIndex - 1
- displayMenu(selectedIndex)
- elseif event[4] == 208 and selectedIndex < #l then
- selectedIndex = selectedIndex + 1
- displayMenu(selectedIndex)
- elseif event[4] == 28 then
- local selectedDrive = component.list("filesystem")()
- local initFn, err = tryLoad(selectedDrive)
- if initFn then
- computer.setBootAddress(selectedDrive)
- computer.beep(800, 0.2)
- computer.beep(800, 0.6)
- computer.beep(800, 0.2)
- return initFn()
- else
- displayError("ERROR: Unable to boot from " .. (l[selectedIndex] and l[selectedIndex].label or "selected device") .. ". 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