Advertisement
DOGGYWOOF

Untitled

Jul 20th, 2024 (edited)
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. local function main()
  2. local ci, computer, component = component.invoke, computer or require("computer"), require("component")
  3.  
  4. local function bi(a, m, ...)
  5. local r = table.pack(pcall(ci, a, m, ...))
  6. return r[1] and table.unpack(r, 2, r.n) or nil, r[2]
  7. end
  8.  
  9. local e = component.list("eeprom")()
  10. computer.getBootAddress = function() return bi(e, "getData") end
  11. computer.setBootAddress = function(a) return bi(e, "setData", a) end
  12.  
  13. local s, g = component.list("screen")(), component.list("gpu")()
  14. if g and s then bi(g, "bind", s) end
  15.  
  16. local function tryLoad(a)
  17. local h, r = bi(a, "open", "/init.lua")
  18. if not h then return nil, "Failed to open /init.lua: " .. tostring(r) end
  19. local b = ""
  20. repeat
  21. local d, r = bi(a, "read", h, math.huge)
  22. if not d and r then bi(a, "close", h) return nil, "Failed to read /init.lua: " .. tostring(r) end
  23. b = b .. (d or "")
  24. until not d
  25. bi(a, "close", h)
  26. local i, l = load(b, "=init")
  27. return i or nil, "Failed to load /init.lua: " .. tostring(l)
  28. end
  29.  
  30. local l = {}
  31. for a in component.list("filesystem") do
  32. local b = bi(a, "getLabel") or "Unnamed Drive"
  33. if b ~= "tmpfs" then table.insert(l, { address = a, label = b }) end
  34. end
  35.  
  36. local function displayError(msg)
  37. local g = component.list("gpu")()
  38. local w, h = bi(g, "getResolution")
  39. local boxX, boxY = math.floor((w - 80) / 2), math.floor((h - 6) / 2)
  40. bi(g, "setBackground", 0x001f3f) bi(g, "fill", boxX, boxY, 80, 6, " ")
  41. bi(g, "setForeground", 0xFF4500)
  42. bi(g, "set", boxX + math.floor((80 - #msg) / 2), boxY + 3, msg)
  43. computer.beep(800, 1) computer.beep(800, 0.5) computer.beep(800, 0.5)
  44. while computer.pullSignal() ~= "key_down" do end
  45. computer.shutdown(true)
  46. end
  47.  
  48. local function displayMenu(sI)
  49. local g = component.list("gpu")()
  50. local w, h = bi(g, "getResolution")
  51. local boxX, boxY = math.floor((w - 60) / 2), math.floor((h - (#l + 4)) / 2)
  52. bi(g, "setBackground", 0x001f3f) bi(g, "fill", boxX, boxY, 60, #l + 4, " ")
  53. bi(g, "setForeground", 0xFFFFFF)
  54. bi(g, "set", boxX + 2, boxY + 1, "Select OS to boot:")
  55. for i, o in ipairs(l) do
  56. bi(g, "setForeground", i == sI and 0xFFD700 or 0xFFFFFF)
  57. bi(g, "set", boxX + 2, boxY + i + 2, o.label .. " (" .. o.address .. ")")
  58. end
  59. computer.beep(800, 0.1)
  60. end
  61.  
  62. local sI = math.ceil(#l / 2)
  63. displayMenu(sI)
  64. while true do
  65. local e = { computer.pullSignal() }
  66. if e[1] == "key_down" then
  67. if e[4] == 200 and sI > 1 then sI = sI - 1 displayMenu(sI)
  68. elseif e[4] == 208 and sI < #l then sI = sI + 1 displayMenu(sI)
  69. elseif e[4] == 28 then
  70. local o = l[sI]
  71. local i, r = tryLoad(o.address)
  72. if i then
  73. computer.setBootAddress(o.address)
  74. computer.beep(800, 0.2) computer.beep(800, 0.6) computer.beep(800, 0.2)
  75. return i()
  76. else
  77. displayError("ERROR: Unable to boot from " .. o.label .. ". Check the device and try again.")
  78. computer.beep(800, 0.1) computer.beep(800, 0.1) computer.beep(800, 0.1)
  79. end
  80. end
  81. end
  82. end
  83. end
  84.  
  85. main()
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement