Guest User

eep.lua

a guest
Dec 10th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.58 KB | None | 0 0
  1. local component_invoke = component.invoke
  2. function boot_invoke(address, method, ...)
  3.   local result = table.pack(pcall(component_invoke, address, method, ...))
  4.   if not result[1] then
  5.     return nil, result[2]
  6.   else
  7.     return table.unpack(result, 2, result.n)
  8.   end
  9. end
  10.  
  11. -- backwards compatibility, may remove later
  12. local eeprom = component.list("eeprom")()
  13. computer.getBootAddress = function()
  14.   return boot_invoke(eeprom, "getData")
  15. end
  16. computer.setBootAddress = function(address)
  17.   return boot_invoke(eeprom, "setData", address)
  18. end
  19.  
  20. do
  21.   local screen = component.list("screen")()
  22.   local gpu = component.list("gpu")()
  23.   if gpu and screen then
  24.     boot_invoke(gpu, "bind", screen)
  25.   end
  26. end
  27. local function tryLoadFrom(address)
  28.   local handle, reason = boot_invoke(address, "open", "/init.lua")
  29.   if not handle then
  30.     return nil, reason
  31.   end
  32.   local buffer = ""
  33.   repeat
  34.     local data, reason = boot_invoke(address, "read", handle, math.huge)
  35.     if not data and reason then
  36.       return nil, reason
  37.     end
  38.     buffer = buffer .. (data or "")
  39.   until not data
  40.   boot_invoke(address, "close", handle)
  41.   return load(buffer, "=init")
  42. end
  43. local init, reason
  44. if computer.getBootAddress() then
  45.   init, reason = tryLoadFrom(computer.getBootAddress())
  46. end
  47. if not init then
  48.   computer.setBootAddress()
  49.   for address in component.list("filesystem") do
  50.     init, reason = tryLoadFrom(address)
  51.     if init then
  52.       computer.setBootAddress(address)
  53.       break
  54.     end
  55.   end
  56. end
  57. if not init then
  58.   error("no bootable medium found" .. (reason and (": " .. tostring(reason)) or ""), 0)
  59. end
  60. computer.beep(1000, 0.2)
  61. init()
Add Comment
Please, Sign In to add comment