From 4cfacb1abc2ddfe81cf2b922c75af01d2e754e55 Mon Sep 17 00:00:00 2001 From: unknown <24964441+wsor4035@users.noreply.github.com> Date: Sun, 13 Feb 2022 00:31:20 -0500 Subject: [PATCH] add filesystem --- computers/commands.lua | 6 +++++- computers/commands/cd/conf.json | 7 +++++++ computers/commands/cd/init.lua | 25 +++++++++++++++++++++++++ computers/commands/echo/conf.json | 3 ++- computers/commands/echo/init.lua | 2 +- computers/commands/help/conf.json | 7 +++++++ computers/commands/help/init.lua | 9 +++++++++ computers/commands/ls/conf.json | 7 +++++++ computers/commands/ls/init.lua | 9 +++++++++ computers/commands/mkdir/conf.json | 7 +++++++ computers/commands/mkdir/init.lua | 12 ++++++++++++ computers/commands/pwd/conf.json | 7 +++++++ computers/commands/pwd/init.lua | 7 +++++++ computers/commands/test/conf.json | 3 ++- computers/commands/test/init.lua | 2 +- computers/commands/touch/conf.json | 7 +++++++ computers/commands/touch/init.lua | 14 ++++++++++++++ computers/gui.lua | 24 ++++++++++++++---------- computers/init.lua | 6 +++++- 19 files changed, 148 insertions(+), 16 deletions(-) create mode 100644 computers/commands/cd/conf.json create mode 100644 computers/commands/cd/init.lua create mode 100644 computers/commands/help/conf.json create mode 100644 computers/commands/help/init.lua create mode 100644 computers/commands/ls/conf.json create mode 100644 computers/commands/ls/init.lua create mode 100644 computers/commands/mkdir/conf.json create mode 100644 computers/commands/mkdir/init.lua create mode 100644 computers/commands/pwd/conf.json create mode 100644 computers/commands/pwd/init.lua create mode 100644 computers/commands/touch/conf.json create mode 100644 computers/commands/touch/init.lua diff --git a/computers/commands.lua b/computers/commands.lua index cab340a..a89be2d 100644 --- a/computers/commands.lua +++ b/computers/commands.lua @@ -1,11 +1,15 @@ computers.registered_commands = {} +computers.registered_confs = {} function computers.register_command(modpath) local func = dofile(modpath .. "/init.lua") local f = io.open(modpath .. "/conf.json") local conf = minetest.parse_json(f:read("*all")) f:close() - if func and computers.os.version >= conf.engine then computers.registered_commands[conf.name] = func end + if func and computers.os.version >= conf.engine then + computers.registered_commands[conf.name] = func + computers.registered_confs[conf.name] = conf + end end --load default commands diff --git a/computers/commands/cd/conf.json b/computers/commands/cd/conf.json new file mode 100644 index 0000000..a67e64a --- /dev/null +++ b/computers/commands/cd/conf.json @@ -0,0 +1,7 @@ +{ + "name": "cd", + "license": "MIT", + "version": 0.1, + "engine": 0.4, + "help": "move in and out of folders" +} \ No newline at end of file diff --git a/computers/commands/cd/init.lua b/computers/commands/cd/init.lua new file mode 100644 index 0000000..4be4e85 --- /dev/null +++ b/computers/commands/cd/init.lua @@ -0,0 +1,25 @@ +function cd(pos, input, data) + local path = computers.devicepath .. "/" .. minetest.hash_node_position(pos) .. data.element.pwd + + if input and input ~= "" and not input:find("/") then + if input == ".." then + local uri = data.element.pwd:split("/") + uri[#uri] = nil + data.element.pwd = "/" .. table.concat(uri, "/") + if data.element.pwd == "/" then data.element.pwd = "" end + return "sucess" + else + for _, folder in pairs(minetest.get_dir_list(path, true)) do + if folder == input then + data.element.pwd = data.element.pwd .. "/" .. input + return "sucess" + end + end + return "ERROR: path not found" + end + else + return "invalid or missing input" + end +end + +return cd \ No newline at end of file diff --git a/computers/commands/echo/conf.json b/computers/commands/echo/conf.json index f7d1230..f09390d 100644 --- a/computers/commands/echo/conf.json +++ b/computers/commands/echo/conf.json @@ -2,5 +2,6 @@ "name": "echo", "license": "MIT", "version": 0.1, - "engine": 0.3 + "engine": 0.3, + "help": "prints out input to the terminal" } \ No newline at end of file diff --git a/computers/commands/echo/init.lua b/computers/commands/echo/init.lua index bfbc32e..85c50bc 100644 --- a/computers/commands/echo/init.lua +++ b/computers/commands/echo/init.lua @@ -1,4 +1,4 @@ -function echo(input) +function echo(pos, input, data) return input end diff --git a/computers/commands/help/conf.json b/computers/commands/help/conf.json new file mode 100644 index 0000000..b4c99b0 --- /dev/null +++ b/computers/commands/help/conf.json @@ -0,0 +1,7 @@ +{ + "name": "help", + "license": "MIT", + "version": 0.1, + "engine": 0.4, + "help": "prints out help" +} \ No newline at end of file diff --git a/computers/commands/help/init.lua b/computers/commands/help/init.lua new file mode 100644 index 0000000..d2a61b1 --- /dev/null +++ b/computers/commands/help/init.lua @@ -0,0 +1,9 @@ +function help(pos, input, data) + local output = "" + for command, def in pairs(computers.registered_confs) do + output = output .. "\n" .. command .. ": " .. def.help + end + return output +end + +return help \ No newline at end of file diff --git a/computers/commands/ls/conf.json b/computers/commands/ls/conf.json new file mode 100644 index 0000000..daa11a3 --- /dev/null +++ b/computers/commands/ls/conf.json @@ -0,0 +1,7 @@ +{ + "name": "ls", + "license": "MIT", + "version": 0.1, + "engine": 0.4, + "help": "lists out all files and folders in the current path" +} \ No newline at end of file diff --git a/computers/commands/ls/init.lua b/computers/commands/ls/init.lua new file mode 100644 index 0000000..e11d06c --- /dev/null +++ b/computers/commands/ls/init.lua @@ -0,0 +1,9 @@ +function ls(pos, input, data) + local path = computers.devicepath .. "/" .. minetest.hash_node_position(pos) .. data.element.pwd + --make dir to be safe + minetest.mkdir(path) + + return table.concat(minetest.get_dir_list(path), " ") +end + +return ls \ No newline at end of file diff --git a/computers/commands/mkdir/conf.json b/computers/commands/mkdir/conf.json new file mode 100644 index 0000000..f862bbc --- /dev/null +++ b/computers/commands/mkdir/conf.json @@ -0,0 +1,7 @@ +{ + "name": "mkdir", + "license": "MIT", + "version": 0.1, + "engine": 0.4, + "help": "makes a dir in the current path" +} \ No newline at end of file diff --git a/computers/commands/mkdir/init.lua b/computers/commands/mkdir/init.lua new file mode 100644 index 0000000..5ecfb87 --- /dev/null +++ b/computers/commands/mkdir/init.lua @@ -0,0 +1,12 @@ +function mkdir(pos, input, data) + local path = computers.devicepath .. "/" .. minetest.hash_node_position(pos) .. data.element.pwd + + if input and input ~= "" and not input:find("/") then + minetest.mkdir(path .. "/" .. input) + return "folder " .. input .. " created" + else + return "invalid or missing input" + end +end + +return mkdir \ No newline at end of file diff --git a/computers/commands/pwd/conf.json b/computers/commands/pwd/conf.json new file mode 100644 index 0000000..f410bbe --- /dev/null +++ b/computers/commands/pwd/conf.json @@ -0,0 +1,7 @@ +{ + "name": "pwd", + "license": "MIT", + "version": 0.1, + "engine": 0.4, + "help": "list the current path" +} \ No newline at end of file diff --git a/computers/commands/pwd/init.lua b/computers/commands/pwd/init.lua new file mode 100644 index 0000000..5a1763b --- /dev/null +++ b/computers/commands/pwd/init.lua @@ -0,0 +1,7 @@ +function pwd(pos, input, data) + local output = data.element.pwd + if output == "" then output = "/" end + return output +end + +return pwd \ No newline at end of file diff --git a/computers/commands/test/conf.json b/computers/commands/test/conf.json index f7bf272..89dd647 100644 --- a/computers/commands/test/conf.json +++ b/computers/commands/test/conf.json @@ -2,5 +2,6 @@ "name": "test", "license": "MIT", "version": 0.1, - "engine": 100000000 + "engine": 100000000, + "help": "literally worthless" } \ No newline at end of file diff --git a/computers/commands/test/init.lua b/computers/commands/test/init.lua index 6ad0416..6e06bd2 100644 --- a/computers/commands/test/init.lua +++ b/computers/commands/test/init.lua @@ -1,4 +1,4 @@ -function test() +function test(pos, input) --minetest.chat_send_all("test") return 0 end diff --git a/computers/commands/touch/conf.json b/computers/commands/touch/conf.json new file mode 100644 index 0000000..2501f4c --- /dev/null +++ b/computers/commands/touch/conf.json @@ -0,0 +1,7 @@ +{ + "name": "touch", + "license": "MIT", + "version": 0.1, + "engine": 0.4, + "help": "creates a file in the current path" +} \ No newline at end of file diff --git a/computers/commands/touch/init.lua b/computers/commands/touch/init.lua new file mode 100644 index 0000000..9039d79 --- /dev/null +++ b/computers/commands/touch/init.lua @@ -0,0 +1,14 @@ +function touch(pos, input, data) + local path = computers.devicepath .. "/" .. minetest.hash_node_position(pos) .. data.element.pwd + --make dir to be safe + minetest.mkdir(path) + + if input and input ~= "" and not input:find("/") then + minetest.safe_file_write(path .. "/" .. input, "") + return "file " .. input .. " created" + else + return "invalid or missing input" + end +end + +return touch \ No newline at end of file diff --git a/computers/gui.lua b/computers/gui.lua index 4134fc3..cef87e5 100644 --- a/computers/gui.lua +++ b/computers/gui.lua @@ -100,7 +100,7 @@ function computers.load_gui(pos, node, clicker) name = "terminal_output", read_only = 1, --label = "test", - default = "user:~$ test command\ntest output\n", + default = "welcome to kuto\nversion " .. computers.os.version .. "\n\nuser:~$ ", }, { type = "box", @@ -118,6 +118,7 @@ function computers.load_gui(pos, node, clicker) h = 1, name = "terminal_input", close_on_enter = false, + pwd = "", props = { border = false, }, @@ -125,27 +126,30 @@ function computers.load_gui(pos, node, clicker) local cindex = futil.get_index_by_name(form, "terminal_ctn") local eindex = futil.get_index_by_name(form[cindex], "terminal_output") local text = form[cindex][eindex].default - --form[cindex][eindex].default = text .. "user:~$ " .. value .. "\n" + local pass_table = { + element = element + } if value == "clear" then - form[cindex][eindex].default = "user:~$\n" + form[cindex][eindex].default = "user:~" .. element.pwd .."$" .."\n" end local cdata = value:split(" ", false, 1) if computers.registered_commands[cdata[1]] then - form[cindex][eindex].default = text .. "user:~$ " .. cdata[1] .. "\n" + form[cindex][eindex].default = text .. value .. "\n" text = form[cindex][eindex].default - local output = computers.registered_commands[cdata[1]](cdata[2]) - if output and output ~= "" and type(output) == "string" then - form[cindex][eindex].default = text .. output .. "\n" + local output = computers.registered_commands[cdata[1]](pos, cdata[2], pass_table) + if output and type(output) == "string" then + form[cindex][eindex].default = text .. output .. "\n" .. "user:~" .. element.pwd .."$" .." " end elseif value == "clear" then - form[cindex][eindex].default = "user:~$\n" + form[cindex][eindex].default = "user:~$ " elseif value == "" then - form[cindex][eindex].default = text .. "user:~$\n" + form[cindex][eindex].default = text .. "user:~" .. element.pwd .."$" .. "\n" else - form[cindex][eindex].default = text .. "user:~$ " .. value .. "\nERROR: command not found\n" + form[cindex][eindex].default = text .. value .. + "\nERROR: command not found\n" .. "user:~" .. element.pwd .."$" .. " " end return form diff --git a/computers/init.lua b/computers/init.lua index 29b072f..5209d04 100644 --- a/computers/init.lua +++ b/computers/init.lua @@ -1,8 +1,12 @@ computers = {} computers.modpath = minetest.get_modpath("computers") +computers.storagepath = minetest.get_worldpath() .. "/computers" +computers.devicepath = computers.storagepath .. "/devices" +computers.networkpath = computers.storagepath .. "/networks" +minetest.mkdir(computers.storagepath) --make sure it exists computers.os = { - version = 0.3, + version = 0.4, name = "kuto", authors = {"wsor", "luk3yx"}, license = "MIT",