OpenComputers Von Neumann Machine Programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.7 KiB

  1. local init
  2. do
  3. local component_invoke = component.invoke
  4. local function boot_invoke(address, method, ...)
  5. local result = table.pack(pcall(component_invoke, address, method, ...))
  6. if not result[1] then
  7. return nil, result[2]
  8. else
  9. return table.unpack(result, 2, result.n)
  10. end
  11. end
  12. -- backwards compatibility, may remove later
  13. local eeprom = component.list("eeprom")()
  14. computer.getBootAddress = function()
  15. return boot_invoke(eeprom, "getData")
  16. end
  17. computer.setBootAddress = function(address)
  18. return boot_invoke(eeprom, "setData", address)
  19. end
  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 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. end
  62. init()