Advertisement
DOGGYWOOF

Opencomputers defualt bios code

Mar 18th, 2024 (edited)
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. local function main()
  2. local ci = component.invoke
  3. local bi = function(address, method, ...)
  4. local result = table.pack(pcall(ci, address, method, ...))
  5. if not result[1] then return nil, result[2] else return table.unpack(result, 2, result.n) end
  6. end
  7.  
  8. local e = component.list("eeprom")()
  9. computer.getBootAddress = function() return bi(e, "getData") end
  10. computer.setBootAddress = function(a) return bi(e, "setData", a) end
  11.  
  12. do
  13. local s, g = component.list("screen")(), component.list("gpu")()
  14. if g and s then bi(g, "bind", s) end
  15. end
  16.  
  17. local function tryLoad(a)
  18. local h, r = bi(a, "open", "/init.lua")
  19. if not h then return nil, "Failed to open /init.lua: " .. tostring(r) end
  20. local b = ""
  21. repeat
  22. local d, r = bi(a, "read", h, math.maxinteger or math.huge)
  23. if not d and r then
  24. bi(a, "close", h)
  25. return nil, "Failed to read /init.lua: " .. tostring(r)
  26. end
  27. b = b .. (d or "")
  28. 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. table.insert(l, {address = a, label = b})
  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 = 60, 4
  45. local boxX, boxY = math.floor((w - boxWidth) / 2), math.floor((h - boxHeight) / 2)
  46. bi(g, "setBackground", 0x888888)
  47. bi(g, "fill", boxX, boxY, boxWidth, boxHeight, " ")
  48. bi(g, "setForeground", 0xFF0000) -- Red text
  49. bi(g, "setBackground", 0x888888)
  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. end
  54.  
  55. local function displayMenu(sI)
  56. local g = component.list("gpu")()
  57. local w, h = bi(g, "getResolution")
  58. local boxWidth, boxHeight = 60, #l + 4
  59. local boxX, boxY = math.floor((w - boxWidth) / 2), math.floor((h - boxHeight) / 2)
  60. bi(g, "setBackground", 0x888888)
  61. bi(g, "fill", boxX, boxY, boxWidth, boxHeight, " ")
  62. bi(g, "setForeground", 0xFFFFFF)
  63. bi(g, "setBackground", 0x888888)
  64. bi(g, "set", boxX + 2, boxY + 1, "Select OS to boot:")
  65. for i, o in ipairs(l) do
  66. local lN = boxY + i + 2
  67. local tC = i == sI and 0xFF0000 or 0xFFFFFF
  68. bi(g, "setForeground", tC)
  69. bi(g, "set", boxX + 2, lN, o.label .. " (" .. o.address .. ")")
  70. end
  71. computer.beep(1500, 0.1) -- Shorter beep
  72. end
  73.  
  74. local sI = math.ceil(#l / 2)
  75. displayMenu(sI)
  76. while true do
  77. local e = {computer.pullSignal()}
  78. if e[1] == "key_down" then
  79. if e[4] == 200 and sI > 1 then
  80. sI = sI - 1
  81. displayMenu(sI)
  82. elseif e[4] == 208 and sI < #l then
  83. sI = sI + 1
  84. displayMenu(sI)
  85. elseif e[4] == 28 then
  86. local o = l[sI]
  87. local i, r = tryLoad(o.address)
  88. if i then
  89. computer.setBootAddress(o.address)
  90. computer.beep(1500, 0.2) -- Short beep
  91. computer.beep(1500, 0.6) -- Long beep
  92. computer.beep(1500, 0.2) -- Short beep
  93. return i()
  94. else
  95. displayError("Unable to boot device!") -- Display error message
  96. computer.beep(1500, 0.1) -- Shorter beep
  97. computer.beep(1500, 0.1) -- Shorter beep
  98. computer.beep(1500, 0.1) -- Shorter beep
  99. end
  100. end
  101. end
  102. end
  103. end
  104.  
  105. main()
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement