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.

64 lines
1.7 KiB

  1. -- Stolen from OpenComputers
  2. local init
  3. do
  4. local component_invoke = component.invoke
  5. local function boot_invoke(address, method, ...)
  6. local result = table.pack(pcall(component_invoke, address, method, ...))
  7. if not result[1] then
  8. return nil, result[2]
  9. else
  10. return table.unpack(result, 2, result.n)
  11. end
  12. end
  13. -- backwards compatibility, may remove later
  14. local eeprom = component.list("eeprom")()
  15. computer.getBootAddress = function()
  16. return boot_invoke(eeprom, "getData")
  17. end
  18. computer.setBootAddress = function(address)
  19. return boot_invoke(eeprom, "setData", address)
  20. end
  21. do
  22. local screen = component.list("screen")()
  23. local gpu = component.list("gpu")()
  24. if gpu and screen then
  25. boot_invoke(gpu, "bind", screen)
  26. end
  27. end
  28. local function tryLoadFrom(address)
  29. local handle, reason = boot_invoke(address, "open", "/init.lua")
  30. if not handle then
  31. return nil, reason
  32. end
  33. local buffer = ""
  34. repeat
  35. local data, reason = boot_invoke(address, "read", handle, math.huge)
  36. if not data and reason then
  37. return nil, reason
  38. end
  39. buffer = buffer .. (data or "")
  40. until not data
  41. boot_invoke(address, "close", handle)
  42. return load(buffer, "=init")
  43. end
  44. local reason
  45. if computer.getBootAddress() then
  46. init, reason = tryLoadFrom(computer.getBootAddress())
  47. end
  48. if not init then
  49. computer.setBootAddress()
  50. for address in component.list("filesystem") do
  51. init, reason = tryLoadFrom(address)
  52. if init then
  53. computer.setBootAddress(address)
  54. break
  55. end
  56. end
  57. end
  58. if not init then
  59. error("no bootable medium found" .. (reason and (": " .. tostring(reason)) or ""), 0)
  60. end
  61. computer.beep(1000, 0.2)
  62. end
  63. init()