Advertisement
LaImE_PRO

BootLoader

Jun 22nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.64 KB | None | 0 0
  1. local list={"init.lua","lib/tools/boot.lua","boot/kernel/pipes","system/kernel"}
  2. local OS,scrollingPos,select,selecttext={},0,{1,1},{"[Boot]","[Reboot]","[Shutdown]"}
  3. local gpu=component.proxy(component.list("gpu")())
  4. local set,fill,foreground,background=gpu.set,gpu.fill,gpu.setForeground,gpu.setBackground
  5. local function readfile(fs,file)
  6.  local buffer=""
  7.  local open=fs.open(file,"r")
  8.  repeat local read=fs.read(open,math.huge)
  9.   buffer=buffer..(read or "")
  10.  until not read
  11.  fs.close(open)
  12.  return buffer,"="..file
  13. end
  14. local function printselect(...)
  15.  foreground(0x000000)
  16.  background(0xFFFFFF)
  17.  set(...)
  18.  foreground(0xFFFFFF)
  19.  background(0x000000)
  20. end
  21. local function refresh()
  22.  _G._OSVERSION="FreeLoader"
  23.  gpu.setResolution(50,16)
  24.  fill(1,1,50,16," ")
  25.  set(1,1,_OSVERSION)
  26.  for i=2,15,13 do
  27.   set(1,i,string.rep("-",50))
  28.  end
  29.  set(15,3,"Select what to boot:")
  30.  OS={}
  31.  for i in component.list("filesystem") do
  32.   computer.pushSignal("component_added",i,"filesystem")
  33.  end
  34. end
  35. refresh()
  36. while true do
  37.  local isRefreshed=false
  38.  local signal={computer.pullSignal(0.001)}
  39.  if signal[1]=="component_added" and signal[3]=="filesystem" then
  40.   local fs=component.proxy(signal[2])
  41.   for i=1,#list do
  42.    if fs.exists(list[i]) then
  43.     table.insert(OS,{signal[2],(readfile(fs,list[i]):match("_OSVERSION%s*=%s*\"(.-)\"") or "Unknown"),list[i]})
  44.    end
  45.   end
  46.   isRefreshed=true
  47.  elseif signal[1]=="component_removed" and signal[3]=="filesystem" then
  48.   local check=1
  49.   repeat
  50.    if OS[check][1]==signal[2] then
  51.     table.remove(OS,check)
  52.    else
  53.     check=check+1
  54.    end
  55.   until check>#OS
  56.   isRefreshed=true
  57.  elseif signal[1]=="key_down" then
  58.   isRefreshed=true
  59.   if signal[4]==200 then
  60.    select[1]=select[1]-1
  61.   elseif signal[4]==208 then
  62.    select[1]=select[1]+1
  63.   elseif signal[4]==203 then
  64.    select[2]=select[2]-1
  65.   elseif signal[4]==205 then
  66.    select[2]=select[2]+1
  67.   elseif signal[4]==28 then
  68.    if select[2]==1 then
  69.     if #OS~=0 and select[2]==1 then
  70.      fill(1,1,50,16," ")
  71.      _G.computer.getBootAddress=function() return OS[select[1]][1] end
  72.      local boot,reason=load(readfile(component.proxy(OS[select[1]][1]),OS[select[1]][3]))
  73.      if boot then
  74.       local result={pcall(boot)}
  75.       if result[1] then
  76.        table.unpack(result,2,result.n)
  77.       else
  78.        set(1,7,"Error: "..(result[2] or "Unknown"))
  79.       end
  80.      else
  81.       set(1,7,"Error: "..(reason or "Unknown"))
  82.      end
  83.      set(15,9,"Press any key to continue.")
  84.      repeat local signal={computer.pullSignal()}
  85.      until signal[1]=="key_down"
  86.      refresh()
  87.     end
  88.    elseif select[2]==2 then
  89.     computer.shutdown(true)
  90.    elseif select[2]==3 then
  91.     computer.shutdown()
  92.    end
  93.   end
  94.  end
  95.  local size=11
  96.  if size>#OS then
  97.   size=#OS
  98.  end
  99.  if select[1]<1 then
  100.   select[1]=1
  101.  elseif select[1]>#OS and #OS~=0 then
  102.   select[1]=#OS
  103.  end
  104.  if select[1]<scrollingPos+1 then
  105.   scrollingPos=select[1]-1
  106.  elseif select[1]>scrollingPos+size then
  107.   scrollingPos=select[1]-size
  108.  end
  109.  if select[2]<1 then
  110.   select[2]=3
  111.  elseif select[2]>3 then
  112.   select[2]=1
  113.  end
  114.  if isRefreshed==true then
  115.   fill(1,4,50,11," ")
  116.   if #OS==0 then
  117.    set(15,5,"Error: Nothing to boot!")
  118.   else
  119.    for i=scrollingPos+1,scrollingPos+size do
  120.     if OS[i] then
  121.      text=(tostring(i-1)..". "..OS[i][2]..": "..OS[i][1]:sub(1,3).."/"..OS[i][3])
  122.      if i==select[1] then
  123.       printselect(1,3+i-scrollingPos,text)
  124.      else
  125.       set(1,3+i-scrollingPos,text)
  126.      end
  127.     end
  128.    end
  129.   end
  130.  end
  131.  local t=1
  132.  for i=1,3 do
  133.   if i==select[2] then
  134.    printselect(t,16,selecttext[i])
  135.   else
  136.    set(t,16,selecttext[i])
  137.   end
  138.   t=t+2+#selecttext[i]
  139.  end
  140. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement