Advertisement
HangMan23

eeprom.lua

Jan 27th, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.28 KB | None | 0 0
  1. local path = "/init.lua"
  2.  
  3. ------------------------------------------------
  4.  
  5. local eeprom = component.proxy(component.list("eeprom")())
  6. local write = function() end
  7. local gpu = false
  8.  
  9. local function sleep(timeout)
  10.   checkArg(1, timeout, "number", "nil")
  11.   local deadline = computer.uptime() + (timeout or 0)
  12.   repeat
  13.     computer.pullSignal(deadline - computer.uptime())
  14.   until computer.uptime() >= deadline
  15. end
  16.  
  17. if component.list("gpu") then
  18.  
  19.   gpu = component.proxy(component.list("gpu")())
  20.  
  21.   local w, h = gpu.maxResolution()
  22.   gpu.setResolution(w, h)
  23.  
  24.   gpu.setBackground(0xffffff)
  25.   gpu.fill(1, 1, w, h, " ")
  26.  
  27.   write = function(text)
  28.  
  29.     local tag = "loading..."
  30.  
  31.     gpu.setBackground(0xffffff)
  32.     gpu.setForeground(0x000000)
  33.     gpu.set(w/2-string.len(tag)/2, h/2-1, tag)
  34.     gpu.setForeground(0x646464)
  35.     gpu.set(w/2-string.len(text)/2, h/2, text)
  36.  
  37.     sleep(0.5)
  38.  
  39.   end
  40.  
  41. end
  42.  
  43. write("Starting")
  44.  
  45. if not component.list("filesystem") then error("No bootable medium found") end
  46. local fs = component.proxy(component.list("filesystem")())
  47.  
  48. local internet = false
  49. if component.list("internet") then internet = component.proxy(component.list("internet")()) end
  50.  
  51. computer.setBootAddress = function(address)
  52.  
  53.   return eeprom.setData(address)
  54.  
  55. end
  56.  
  57. computer.getBootAddress = function()
  58.  
  59.   return eeprom.getData()
  60.  
  61. end
  62.  
  63. local function l_loadfile(path, address)
  64.  
  65.   local drive
  66.  
  67.   if address then
  68.  
  69.     drive = component.proxy(address)
  70.  
  71.   else
  72.  
  73.     drive = component.proxy(computer.getBootAddress() or component.list("filesystem")())
  74.  
  75.   end
  76.  
  77.   if not drive.exists(path) then return nil, "file does not exists" end
  78.   if drive.isDirectory(path) then return nil, "file is directory" end
  79.  
  80.   local handle, reason = drive.open(path, "r")
  81.  
  82.   if not handle then return nil, "handle error: " .. reason end
  83.  
  84.   local code = drive.read(handle, math.huge)
  85.  
  86.   return load(code)
  87.  
  88. end
  89.  
  90. local function hex(r, g, b)
  91.  
  92.   local color
  93.   if r>=0 and r<=255 and g>=0 and g<=255 and b>=0 and g<=255 then color = (r *(256^2)) + (g * 256) + b end
  94.   return color or 0
  95.  
  96. end
  97.  
  98. write("Searching bootable drive")
  99.  
  100. local fss = component.list("filesystem")
  101.  
  102. local fs_address
  103.  
  104. for fs in pairs(fss) do
  105.  
  106.   local proxy = component.proxy(fs)
  107.  
  108.   if proxy.exists(path) and not proxy.isDirectory(path) then
  109.  
  110.     local handle = proxy.open(path, "r")
  111.     local code = proxy.read(handle, math.huge)
  112.     proxy.close(handle)
  113.  
  114.     if load(code) then fs_address = fs end
  115.  
  116.   end
  117.  
  118. end
  119.  
  120. if not fs_address then
  121.  
  122.   if gpu then
  123.  
  124.     gpu.setBackground(0x0000ff)
  125.  
  126.     local w, h = gpu.getResolution()
  127.  
  128.     gpu.fill(1, 1, 1, h, " ")
  129.  
  130.     for i = 1, w do
  131.  
  132.       gpu.copy(1, 1, w, h, 1, 0)
  133.       sleep(0)
  134.  
  135.     end
  136.  
  137.     error("No bootable "..path.." found")
  138.  
  139.   else
  140.  
  141.     for i = 1, 3 do computer.beep(500, 0.1) end
  142.     error("No bootable "..path.." found")
  143.  
  144.   end
  145.  
  146. else
  147.  
  148.   write("Booting from " .. fs_address)
  149.  
  150.   computer.setBootAddress(fs_address)
  151.  
  152.   for i = 255, 1, -8 do
  153.  
  154.     local w, h = gpu.getResolution()
  155.     gpu.setBackground(hex(i, i, i))
  156.     gpu.fill(1, 1, w, h, " ")
  157.     sleep(0)
  158.  
  159.   end
  160.  
  161.   local func, reason = l_loadfile(path, computer.getBootAddress())
  162.  
  163.   if func then func() else error("Error with loading boot file: " .. reason) end
  164.  
  165. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement