Advertisement
DOGGYWOOF

DOGGY BOOTLOADER for opencomputers v2

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