Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Boot Manager --
- -- Just Does Games --
- local dir = "/boot"
- local version = "v.1.0."..fs.getSize(shell.getRunningProgram())
- local w,h = term.getSize()
- --if not term.isColor() then colors = {} end
- local running, update = true, true
- local selected, scroll, maxscroll, scroll_space = 1, 0, 0, 2
- local settings = {}
- local boot = {}
- -- On boot --
- if fs.exists(dir.."/settings.lua") then
- local file = fs.open(dir.."/settings.lua", "r")
- settings = textutils.unserialize(file.readAll())
- file.close()
- end
- settings.background = settings.background or "black"
- settings.foreground = settings.foreground or "black"
- settings.border = settings.border or "white"
- settings.text = settings.text or "white"
- settings.selected = settings.selected or "gray"
- settings.dir = settings.dir or dir
- settings.extensions = settings.extensions or {"main.lua", "startup.lua", "autorun.lua"}
- if not fs.exists(dir) then fs.makeDir(dir) end
- local file = fs.open(settings.dir.."/settings.lua", "w")
- file.write(textutils.serialize(settings))
- file.close()
- if not fs.exists(settings.dir.."/help.lua") then
- local file = fs.open(settings.dir.."/help.lua", "w")
- file.writeLine("--[[")
- file.writeLine("Boot Manager is used to boot into files from a boot folder (default '/boot').")
- file.writeLine("This was more of a project to see how long it would take to make something like this.")
- file.writeLine("It took me about 5 - 7 hours confortably and in no rush.")
- file.writeLine("")
- file.writeLine("How to use:")
- file.writeLine("throw folder with an extension inside the directory specified in settings.lua")
- file.writeLine("extensions and directory can be changed from the settings.lua file along with color.")
- file.writeLine("")
- file.writeLine("]]--")
- file.close()
- end
- -- Functions --
- function updateFiles()
- 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
- boot = {"CraftOS"}
- local tmp = fs.list(dir)
- for i=1, #tmp do
- for ii=1, #settings.extensions do
- if fs.isDir(dir.."/"..tmp[i]) then
- if fs.exists(dir.."/"..tmp[i].."/"..settings.extensions[ii]) then
- boot[#boot+1] = tmp[i]
- end
- end
- end
- end
- selected, scroll, maxscroll = 1, 0, math.max(#boot-(h-4), 0)
- end
- function renderGUI() -- anything not relating to boot
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors[settings.foreground] or colors.black)
- term.setTextColor(colors[settings.text] or colors.white)
- write("Boot Manager "..version)
- term.setCursorPos(1,h) term.setTextColor(colors.white)
- write(selected.."/"..#boot.." ")
- end
- function renderBOOT() -- options and programs
- term.setTextColor(colors[settings.text] or colors.white)
- for i=1, math.min(#boot, h-4) do
- term.setCursorPos(3,2+i)
- 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
- if string.len(boot[i+scroll]) > w-4 then boot[i+scroll] = string.sub(boot[i+scroll], 1,w-7).."..." end
- write(boot[i+scroll])
- for i=1, (w-4)-string.len(boot[i+scroll]) do write(" ") end
- end
- end
- function render()
- term.setCursorBlink(false)
- if update then
- term.setBackgroundColor(colors[settings.foreground] or colors.black) term.clear()
- paintutils.drawBox(2,2,w-1,h-1,colors[settings.border] or colors.white)
- term.setBackgroundColor(colors[settings.foreground] or colors.black)
- term.setTextColor(colors.white)
- for i=1, w-2 do
- term.setCursorPos(1+i,h-1)
- write(string.char(131))
- end
- term.setTextColor(colors[settings.background] or colors.black)
- term.setBackgroundColor(colors[settings.border] or colors.white)
- for i=1, w-2 do
- term.setCursorPos(1+i,2)
- write(string.char(131))
- end
- paintutils.drawFilledBox(3,3,w-2,h-2,colors[settings.background] or colors.black)
- update = false
- end
- renderGUI()
- renderBOOT()
- end
- function checkTable(tb1, tb2)
- if #tb1 ~= #tb2 then return false end
- for i=1, #tb1 do
- if tb1[i] ~= tb2[i] then return false end
- end
- return true
- end
- function fsUpdater()
- local fsOrig = fs.list(dir)
- while running do
- sleep(.001)
- if not checkTable(fs.list(dir), fsOrig) then
- updateFiles()
- fsOrig = fs.list(dir)
- update = true render()
- end
- end
- end
- function run(id)
- if id == 1 then running = false return end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- for i=1, #settings.extensions do
- if fs.exists(dir.."/"..boot[id].."/"..settings.extensions[i]) then
- shell.run(dir.."/"..boot[id].."/"..settings.extensions[i])
- end
- end
- sleep(1) update = true render()
- end
- function pe(...)
- local events = {...}
- while true do
- a,b,x,y = os.pullEvent()
- for i=1, #events do
- if a == events[i] then return a,b,x,y end
- end
- end
- end
- function controller(direction)
- if direction == "up" or direction == -1 then
- if selected == 1 then
- selected = #boot scroll = maxscroll
- elseif selected == scroll_space+scroll and scroll ~= 0 then
- selected = selected - 1
- scroll = scroll - 1
- else
- selected = selected - 1
- end
- elseif direction == "down" or direction == 1 then
- if selected == #boot then
- selected = 1 scroll = 0
- elseif selected == h-4-scroll_space+scroll and scroll ~= maxscroll then
- selected = selected + 1
- scroll = scroll + 1
- else
- selected = selected + 1
- end
- end
- end
- function runtime()
- while running do
- render()
- sleep(.001)
- a,b,x,y = pe("key", "mouse_click", "mouse_drag", "mouse_scroll")
- if a == "key" then
- if b == keys.w or b == keys.up then
- --if selected == 1 then selected = #boot else selected = selected - 1 end
- controller("up")
- elseif b == keys.s or b == keys.down then
- --if selected == #boot then selected = 1 else selected = selected + 1 end
- controller("down")
- elseif b == keys.enter or b == keys.e then
- run(selected)
- elseif b == keys.q then
- running = false
- end
- elseif a == "mouse_click" or a == "mouse_drag" then
- if x >= 3 and x <= w-2 and y >= 2 and y <= h-2 then
- if (y-2)+scroll == selected and a == "mouse_click" then run(selected) end
- selected = math.min((y-2)+scroll, #boot)
- end
- elseif a == "mouse_scroll" then
- controller(b)
- end
- end
- end
- function main()
- parallel.waitForAny(runtime, fsUpdater)
- end
- function splash() -- loads boot and shows startup screen
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- print("Boot Manager "..version)
- sleep(.2)
- updateFiles()
- end
- --splash()
- updateFiles()
- main()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- sleep(.4)
- shell.run("shell")
Advertisement
Add Comment
Please, Sign In to add comment