Advertisement
DOGGYWOOF

Untitled

Apr 8th, 2024
3
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. local c = component.invoke
  2. local computer = computer or require("computer")
  3.  
  4. local bi = function(a, m, ...)
  5. local r = table.pack(pcall(c, a, m, ...))
  6. return r[1], table.unpack(r, 2, r.n)
  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 tryLoad = function(a)
  14. local h, r = bi(a, "open", "/init.lua")
  15. if not h then return nil, "Failed to open /init.lua: " .. tostring(r) end
  16. local b = ""
  17. repeat
  18. local d, r = bi(a, "read", h, math.maxinteger or math.huge)
  19. if not d and r then return nil, "Failed to read /init.lua: " .. tostring(r) end
  20. b = b .. (d or "")
  21. until not d
  22. bi(a, "close", h)
  23. local i, l = load(b, "=init")
  24. return i, l
  25. end
  26.  
  27. local eraseDrive = function(a)
  28. local g = component.list("gpu")()
  29. local w, h = bi(g, "getResolution")
  30. local bx, by, bw, bh = math.floor((w - 60) / 2), math.floor((h - 4) / 2), 60, 4
  31. bi(g, "setBackground", 0x0000FF)
  32. bi(g, "fill", bx, by, bw, bh, " ")
  33. bi(g, "setForeground", 0xFFFFFF)
  34. bi(g, "setBackground", 0x0000FF)
  35. bi(g, "set", bx + 2, by + 1, "Erase drive? (Y/N)")
  36. while true do
  37. local e = { computer.pullSignal() }
  38. if e[1] == "key_down" then
  39. if e[3] == 21 or e[3] == 49 then
  40. bi(a, "remove", "/init.lua")
  41. bi(a, "remove", "/boot.lua")
  42. computer.beep(800, 0.2)
  43. computer.beep(800, 0.6)
  44. computer.beep(800, 0.2)
  45. computer.shutdown(true)
  46. break
  47. elseif e[3] == 49 or e[3] == 21 then
  48. break
  49. end
  50. end
  51. end
  52. end
  53.  
  54. local drives = {}
  55. for a in component.list("filesystem") do
  56. local b = bi(a, "getLabel") or "Unnamed"
  57. if b ~= "tmpfs" then drives[#drives + 1] = { a, b } end
  58. end
  59.  
  60. local displayError = function(msg)
  61. local g = component.list("gpu")()
  62. local w, h = bi(g, "getResolution")
  63. local bx, by, bw, bh = math.floor((w - 60) / 2), math.floor((h - 4) / 2), 60, 4
  64. bi(g, "setBackground", 0x0000FF)
  65. bi(g, "fill", bx, by, bw, bh, " ")
  66. bi(g, "setForeground", 0xFF0000)
  67. bi(g, "setBackground", 0x0000FF)
  68. local tx = math.floor((bw - #msg) / 2) + bx
  69. local ty = math.floor((bh - 1) / 2) + by
  70. bi(g, "set", tx, ty, msg)
  71. computer.beep(800, 1)
  72. computer.beep(800, 0.5)
  73. computer.beep(800, 0.5)
  74. end
  75.  
  76. local displayMenu = function(selIndex)
  77. local g = component.list("gpu")()
  78. local w, h = bi(g, "getResolution")
  79. local bx, by, bw, bh = math.floor((w - 60) / 2), math.floor((h - #drives - 4) / 2), 60, #drives + 4
  80. bi(g, "setBackground", 0x0000FF)
  81. bi(g, "fill", bx, by, bw, bh, " ")
  82. bi(g, "setForeground", 0xFFFFFF)
  83. bi(g, "setBackground", 0x0000FF)
  84. bi(g, "set", bx + 2, by + 1, "Select OS to boot:")
  85. for i, drive in ipairs(drives) do
  86. local lineY = by + i + 2
  87. local textColor = i == selIndex and 0xFF0000 or 0xFFFFFF
  88. bi(g, "setForeground", textColor)
  89. bi(g, "set", bx + 2, lineY, drive[2] .. " (" .. drive[1] .. ")")
  90. end
  91. computer.beep(800, 0.1)
  92. end
  93.  
  94. local selIndex = math.ceil(#drives / 2)
  95. displayMenu(selIndex)
  96. while true do
  97. local e = { computer.pullSignal() }
  98. if e[1] == "key_down" then
  99. if e[4] == 200 and selIndex > 1 then
  100. selIndex = selIndex - 1
  101. displayMenu(selIndex)
  102. elseif e[4] == 208 and selIndex < #drives then
  103. selIndex = selIndex + 1
  104. displayMenu(selIndex)
  105. elseif e[4] == 18 then
  106. eraseDrive(drives[selIndex][1])
  107. elseif e[4] == 28 then
  108. local i, r = tryLoad(drives[selIndex][1])
  109. if i then
  110. computer.setBootAddress(drives[selIndex][1])
  111. computer.beep(800, 0.2)
  112. computer.beep(800, 0.6)
  113. computer.beep(800, 0.2)
  114. return i()
  115. else
  116. displayError("Unable to boot device!")
  117. computer.beep(800, 0.1)
  118. computer.beep(800, 0.1)
  119. computer.beep(800, 0.1)
  120. end
  121. end
  122. end
  123. end
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement