JustDoesGames

Boot Manager | Just Does Games

May 4th, 2020
2,153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.87 KB | None | 0 0
  1. -- Boot Manager --
  2. -- Just Does Games --
  3.  
  4. local dir = "/boot"
  5. local version = "v.1.0."..fs.getSize(shell.getRunningProgram())
  6. local w,h = term.getSize()
  7. --if not term.isColor() then colors = {} end
  8.  
  9. local running, update = true, true
  10. local selected, scroll, maxscroll, scroll_space = 1, 0, 0, 2
  11.  
  12.  
  13. local settings = {}
  14. local boot = {}
  15.  
  16. -- On boot --
  17.  
  18. if fs.exists(dir.."/settings.lua") then
  19.     local file = fs.open(dir.."/settings.lua", "r")
  20.     settings = textutils.unserialize(file.readAll())
  21.     file.close()
  22. end
  23.  
  24. settings.background = settings.background or "black"
  25. settings.foreground = settings.foreground or "black"
  26. settings.border = settings.border or "white"
  27. settings.text = settings.text or "white"
  28. settings.selected = settings.selected or "gray"
  29. settings.dir = settings.dir or dir
  30. settings.extensions = settings.extensions or {"main.lua", "startup.lua", "autorun.lua"}
  31.  
  32. if not fs.exists(dir) then fs.makeDir(dir) end
  33. local file = fs.open(settings.dir.."/settings.lua", "w")
  34. file.write(textutils.serialize(settings))
  35. file.close()
  36.  
  37. if not fs.exists(settings.dir.."/help.lua") then
  38.     local file = fs.open(settings.dir.."/help.lua", "w")
  39.     file.writeLine("--[[")
  40.     file.writeLine("Boot Manager is used to boot into files from a boot folder (default '/boot').")
  41.     file.writeLine("This was more of a project to see how long it would take to make something like this.")
  42.     file.writeLine("It took me about 5 - 7 hours confortably and in no rush.")
  43.     file.writeLine("")
  44.     file.writeLine("How to use:")
  45.     file.writeLine("throw folder with an extension inside the directory specified in settings.lua")
  46.     file.writeLine("extensions and directory can be changed from the settings.lua file along with color.")
  47.     file.writeLine("")
  48.     file.writeLine("]]--")
  49.     file.close()
  50. end
  51.  
  52. -- Functions --
  53.  
  54. function updateFiles()
  55.     if not fs.exists(dir) then term.setBackgroundColor(colors.black) term.setTextColor(colors.white) term.clear() term.setCursorPos(1,1) return error("dir does not exists. ("..dir..").") end
  56.     boot = {"CraftOS"}
  57.     local tmp = fs.list(dir)
  58.     for i=1, #tmp do
  59.         for ii=1, #settings.extensions do
  60.             if fs.isDir(dir.."/"..tmp[i]) then
  61.                 if fs.exists(dir.."/"..tmp[i].."/"..settings.extensions[ii]) then
  62.                     boot[#boot+1] = tmp[i]
  63.                 end
  64.             end
  65.         end
  66.     end
  67.     selected, scroll, maxscroll = 1, 0, math.max(#boot-(h-4), 0)
  68. end
  69.  
  70. function renderGUI() -- anything not relating to boot
  71.     term.setCursorPos(1,1)
  72.     term.setBackgroundColor(colors[settings.foreground] or colors.black)
  73.     term.setTextColor(colors[settings.text] or colors.white)
  74.     write("Boot Manager "..version)
  75.     term.setCursorPos(1,h) term.setTextColor(colors.white)
  76.     write(selected.."/"..#boot.."  ")
  77. end
  78.  
  79. function renderBOOT() -- options and programs
  80.     term.setTextColor(colors[settings.text] or colors.white)
  81.     for i=1, math.min(#boot, h-4) do
  82.         term.setCursorPos(3,2+i)
  83.         if i+scroll == selected then term.setBackgroundColor(colors[settings.selected] or colors.gray) for i=1, w-4 do write(" ") end term.setCursorPos(3,2+i) else term.setBackgroundColor(colors[settings.background] or colors.black) end
  84.         if string.len(boot[i+scroll]) > w-4 then boot[i+scroll] = string.sub(boot[i+scroll], 1,w-7).."..." end
  85.         write(boot[i+scroll])
  86.         for i=1, (w-4)-string.len(boot[i+scroll]) do write(" ") end
  87.     end
  88. end
  89.  
  90. function render()
  91.     term.setCursorBlink(false)
  92.     if update then
  93.         term.setBackgroundColor(colors[settings.foreground] or colors.black) term.clear()
  94.         paintutils.drawBox(2,2,w-1,h-1,colors[settings.border] or colors.white)
  95.         term.setBackgroundColor(colors[settings.foreground] or colors.black)
  96.         term.setTextColor(colors.white)
  97.         for i=1, w-2 do
  98.             term.setCursorPos(1+i,h-1)
  99.             write(string.char(131))
  100.         end
  101.         term.setTextColor(colors[settings.background] or colors.black)
  102.         term.setBackgroundColor(colors[settings.border] or colors.white)
  103.         for i=1, w-2 do
  104.             term.setCursorPos(1+i,2)
  105.             write(string.char(131))
  106.         end
  107.         paintutils.drawFilledBox(3,3,w-2,h-2,colors[settings.background] or colors.black)
  108.         update = false
  109.     end
  110.     renderGUI()
  111.     renderBOOT()
  112. end
  113.  
  114. function checkTable(tb1, tb2)
  115.     if #tb1 ~= #tb2 then return false end
  116.     for i=1, #tb1 do
  117.         if tb1[i] ~= tb2[i] then return false end
  118.     end
  119.     return true
  120. end
  121.  
  122. function fsUpdater()
  123.     local fsOrig = fs.list(dir)
  124.     while running do
  125.         sleep(.001)
  126.         if not checkTable(fs.list(dir), fsOrig) then
  127.             updateFiles()
  128.             fsOrig = fs.list(dir)
  129.             update = true render()
  130.         end
  131.     end
  132. end
  133.  
  134. function run(id)
  135.     if id == 1 then running = false return end
  136.     term.setBackgroundColor(colors.black)
  137.     term.setTextColor(colors.white)
  138.     term.clear()
  139.     term.setCursorPos(1,1)
  140.     for i=1, #settings.extensions do
  141.         if fs.exists(dir.."/"..boot[id].."/"..settings.extensions[i]) then
  142.             shell.run(dir.."/"..boot[id].."/"..settings.extensions[i])
  143.         end
  144.     end
  145.     sleep(1) update = true render()
  146. end
  147.  
  148. function pe(...)
  149.     local events = {...}
  150.     while true do
  151.         a,b,x,y = os.pullEvent()
  152.         for i=1, #events do
  153.             if a == events[i] then return a,b,x,y end
  154.         end
  155.     end
  156. end
  157.  
  158. function controller(direction)
  159.     if direction == "up" or direction == -1 then
  160.         if selected == 1 then
  161.             selected = #boot scroll = maxscroll
  162.         elseif selected == scroll_space+scroll and scroll ~= 0 then
  163.             selected = selected - 1
  164.             scroll = scroll - 1
  165.         else
  166.             selected = selected - 1
  167.         end
  168.     elseif direction == "down" or direction == 1 then
  169.         if selected == #boot then
  170.             selected = 1 scroll = 0
  171.         elseif selected == h-4-scroll_space+scroll and scroll ~= maxscroll then
  172.             selected = selected + 1
  173.             scroll = scroll + 1
  174.         else
  175.             selected = selected + 1
  176.         end
  177.     end
  178. end
  179.  
  180. function runtime()
  181.     while running do
  182.         render()
  183.         sleep(.001)
  184.         a,b,x,y = pe("key", "mouse_click", "mouse_drag", "mouse_scroll")
  185.         if a == "key" then
  186.             if b == keys.w or b == keys.up then
  187.                 --if selected == 1 then selected = #boot else selected = selected - 1 end
  188.                 controller("up")
  189.             elseif b == keys.s or b == keys.down then
  190.                 --if selected == #boot then selected = 1 else selected = selected + 1 end
  191.                 controller("down")
  192.             elseif b == keys.enter or b == keys.e then
  193.                 run(selected)
  194.             elseif b == keys.q then
  195.                 running = false
  196.             end
  197.         elseif a == "mouse_click" or a == "mouse_drag" then
  198.             if x >= 3 and x <= w-2 and y >= 2 and y <= h-2 then
  199.                 if (y-2)+scroll == selected and a == "mouse_click" then run(selected) end
  200.                 selected = math.min((y-2)+scroll, #boot)
  201.             end
  202.         elseif a == "mouse_scroll" then
  203.             controller(b)
  204.         end
  205.     end
  206. end
  207.  
  208. function main()
  209.     parallel.waitForAny(runtime, fsUpdater)
  210. end
  211.  
  212. function splash() -- loads boot and shows startup screen
  213.     term.setBackgroundColor(colors.black)
  214.     term.setTextColor(colors.white)
  215.     term.clear()
  216.     term.setCursorPos(1,1)
  217.     print("Boot Manager "..version)
  218.     sleep(.2)
  219.     updateFiles()
  220. end
  221.  
  222. --splash()
  223. updateFiles()
  224.  
  225. main()
  226.  
  227. term.setBackgroundColor(colors.black)
  228. term.setTextColor(colors.white)
  229. term.clear()
  230. term.setCursorPos(1,1)
  231. sleep(.4)
  232. shell.run("shell")
Advertisement
Add Comment
Please, Sign In to add comment