From 01b32c92027f6b158c4bf19e31e862a93c43f21c Mon Sep 17 00:00:00 2001 From: octacian Date: Sat, 30 Jul 2016 14:16:13 -0700 Subject: [PATCH] rewrite os sytem to be more modular using a filesystem --- api.lua | 45 ++++++++++++++++++++++++++++++++------------- bios/conf.lua | 8 ++++++++ bios/main.lua | 2 ++ bios/start.lua | 1 + os.lua | 6 +++--- os/bios.lua | 19 ------------------- 6 files changed, 46 insertions(+), 35 deletions(-) create mode 100644 bios/conf.lua create mode 100644 bios/main.lua create mode 100644 bios/start.lua delete mode 100755 os/bios.lua diff --git a/api.lua b/api.lua index 46a5dd4..9064c88 100755 --- a/api.lua +++ b/api.lua @@ -1,4 +1,6 @@ -- digicompute/api.lua +local modpath = digicompute.modpath -- modpath pointer +local path = digicompute.path -- path pointer -- SYSTEM FUNCTIONS -- turn on @@ -14,7 +16,7 @@ function digicompute.on(pos, node) meta:set_string("formspec", digicompute.formspec_name("")) -- set formspec else -- use default formspec -- if not when_on, use blank - if not digicompute.os.when_on(pos) then + if not dofile(path.."/"..meta:get_string("name").."/os/start.lua") then meta:set_string("formspec", digicompute.formspec("", "")) -- set formspec end digicompute.os.refresh(pos) -- refresh @@ -40,11 +42,6 @@ end -- /SYSTEM FUNCTIONS function digicompute.register_terminal(termstring, desc) - -- check os - if not desc.os then - desc.os = "bios" - end - digicompute.os.load(desc.os) -- load os -- off minetest.register_node("digicompute:"..termstring, { drawtype = "nodebox", @@ -132,24 +129,46 @@ function digicompute.register_terminal(termstring, desc) meta:set_string("formspec", digicompute.formspec("", "")) -- clear formspec end end, + on_destruct = function(pos) + local meta = minetest.get_meta(pos) -- get meta + local name = meta:get_string("name") -- get name + if name then os.remove(path.."/"..name) end -- try to remove files + end, on_receive_fields = function(pos, formname, fields, sender) -- precess formdata local meta = minetest.get_meta(pos) -- get meta + digicompute.pos = pos -- make pos global + digicompute.input = fields.input -- make input global -- if name, set if fields.name then meta:set_string("name", fields.name) -- set name meta:set_string("setup", "true") -- set computer to configured + -- create filesystem + digicompute.file.mkdir(path.."/"..fields.name.."/os/") + local conf = digicompute.file.read(modpath.."/bios/conf.lua") + local main = digicompute.file.read(modpath.."/bios/main.lua") + local start = digicompute.file.read(modpath.."/bios/start.lua") + digicompute.file.write(path.."/"..fields.name.."/os/conf.lua", conf) + digicompute.file.write(path.."/"..fields.name.."/os/main.lua", main) + digicompute.file.write(path.."/"..fields.name.."/os/start.lua", start) + digicompute.file.mkdir(path.."/"..fields.name.."/user/") + local conf = nil -- unset + local main = nil -- unset + local start = nil -- unset -- try to run when_on - if digicompute.os.when_on(pos) then return true end - meta:set_string("formspec", digicompute.formspec(meta:get_string("input"), meta:get_string("output"))) -- update formspec - return + if not dofile(path.."/"..meta:get_string("name").."/os/start.lua") then + meta:set_string("formspec", digicompute.formspec("", "")) -- set formspec + end + digicompute.os.refresh(pos) -- refresh end + local name = meta:get_string("name") -- get name + dofile(path.."/"..name.."/os/conf.lua") -- if submitted, process basic commands, pass on to os if fields.submit then - if fields.input == digicompute.os.clear then meta:set_string("formspec", digicompute.formspec("","")) - elseif fields.input == digicompute.os.off then digicompute.off(pos, termstring) -- set off - elseif fields.input == digicompute.os.reboot then digicompute.reboot(pos, termstring) -- reboot - else digicompute.os.proc_input(pos, fields.input) end -- turn over to os + if fields.input == clear then meta:set_string("formspec", digicompute.formspec("","")) + elseif fields.input == off then digicompute.off(pos, termstring) -- set off + elseif fields.input == reboot then digicompute.reboot(pos, termstring) -- reboot + else dofile(path.."/"..name.."/os/main.lua") end -- turn over to os end end, }) diff --git a/bios/conf.lua b/bios/conf.lua new file mode 100644 index 0000000..5bf4e95 --- /dev/null +++ b/bios/conf.lua @@ -0,0 +1,8 @@ +-- bios os config +clear = "clear" -- clear command +off = "shutdown" -- shutdown command +reboot = "reboot" -- reboot command +digiline = false -- do not support digilines +network = false -- do not support network +on = "rightclick" -- on command (rightclick) +clear_on_close = false -- do not clear output on close diff --git a/bios/main.lua b/bios/main.lua new file mode 100644 index 0000000..c5e4c64 --- /dev/null +++ b/bios/main.lua @@ -0,0 +1,2 @@ +digicompute.os.set(digicompute.pos, "output", digicompute.os.get(digicompute.pos, "output")..digicompute.input.."\n"..digicompute.os.get(digicompute.pos, "name")..":~$ ") -- print input +digicompute.os.refresh(digicompute.pos) -- refresh diff --git a/bios/start.lua b/bios/start.lua new file mode 100644 index 0000000..3257a32 --- /dev/null +++ b/bios/start.lua @@ -0,0 +1 @@ +digicompute.os.set(digicompute.pos, "output", "Welcome to BiosOS version 0.1.\n\n"..digicompute.os.get(digicompute.pos, "name")..":~$ ") -- print welcome diff --git a/os.lua b/os.lua index 93c4707..dae957a 100644 --- a/os.lua +++ b/os.lua @@ -1,12 +1,12 @@ -- digicompute/os.lua digicompute.os = {} -- init os table local modpath = digicompute.modpath -- modpath pointer -local path = digicompute.datapath -- datapath pointer +local path = digicompute.path -- datapath pointer -- [function] load os function digicompute.os.load(os_name) - if datalib.dofile(modpath.."/os/"..os_name..".lua") ~= true then - if datalib.dofile(path.."/os/"..os_name.."/.lua") ~= true then + if digicompute.file.dofile(modpath.."/os/"..os_name..".lua") ~= true then + if digicompute.file.dofile(path.."/os/"..os_name.."/.lua") ~= true then -- print error digicompute.log(os_name.." os could not be found. Please place the OS file in "..modpath.."/os/ or "..path.."/os/ with extension '.lua'.", "error") end diff --git a/os/bios.lua b/os/bios.lua deleted file mode 100755 index 9799069..0000000 --- a/os/bios.lua +++ /dev/null @@ -1,19 +0,0 @@ --- bios os -digicompute.os.clear = "clear" -- clear command -digicompute.os.off = "shutdown" -- shutdown command -digicompute.os.reboot = "reboot" -- reboot command -digicompute.os.digiline = false -- do not support digilines -digicompute.os.network = false -- do not support network -digicompute.os.on = "rightclick" -- on command (rightclick) -digicompute.os.clear_on_close = false -- do not clear output on close - --- when_on -function digicompute.os.when_on(pos) - digicompute.os.set(pos, "output", "Welcome to BiosOS version 0.1.\n\n"..digicompute.os.get(pos, "name")..":~$ ") -- print welcome -end - --- process input -function digicompute.os.proc_input(pos, input) - digicompute.os.set(pos, "output", digicompute.os.get(pos, "output")..input.."\n"..digicompute.os.get(pos, "name")..":~$ ") -- print input - digicompute.os.refresh(pos) -- refresh -end