introduce filesystem API & adjust meta API
parent
37b4080423
commit
2b28ee800f
23
api.lua
23
api.lua
|
@ -8,8 +8,29 @@ function digicompute.refresh(pos)
|
|||
meta:set_string("formspec", digicompute.formspec(meta:get_string("input"), meta:get_string("output")))
|
||||
end
|
||||
|
||||
dofile(modpath.."/env.lua") -- do env file
|
||||
dofile(modpath.."/fs.lua") -- do fs api file
|
||||
dofile(modpath.."/env.lua") -- do env file
|
||||
|
||||
-- [function] run file under env
|
||||
function digicompute.fs.run_file(pos, lpath, fields, replace)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local lpath = path.."/"..meta:get_string("owner").."/"..meta:get_string("name").."/"..lpath
|
||||
local env = digicompute.create_env(pos, fields) -- environment
|
||||
local f = loadfile(lpath) -- load func
|
||||
local e = digicompute.run(f, env) -- run function
|
||||
-- if error, call error handle function and re-run start
|
||||
if e and replace then
|
||||
datalib.copy(lpath, lpath..".old")
|
||||
datalib.copy(modpath.."/bios/"..replace, lpath)
|
||||
meta:set_string("output", "Error: \n"..msg.."\n\nRestoring OS, modified files will remain.") -- set output
|
||||
digicompute.refresh(pos) -- refresh
|
||||
minetest.after(13, function() -- after 13 seconds, run start
|
||||
local s = loadfile(path.."/"..meta:get_string("owner").."/"..meta:get_string("name").."/os/start.lua") -- load func
|
||||
digicompute.run(s, env) -- run func
|
||||
return false
|
||||
end)
|
||||
elseif e and not replace then return e end -- elseif no replace and error, return error msg
|
||||
end
|
||||
|
||||
-- turn on
|
||||
function digicompute.on(pos, node)
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
set_output(get_output()..get_field("input").."\n"..get_string("name")..":~$ ") -- print input
|
||||
set_output(get_output()..get_field("input").."\n"..get_attr("name")..":~$ ") -- print input
|
||||
refresh() -- refresh
|
||||
|
|
|
@ -1 +1 @@
|
|||
set_output("Welcome to BiosOS version 0.1.\n\n"..get_string("name")..":~$ ") -- print welcome
|
||||
set_output("Welcome to BiosOS version 0.1.\n\n"..get_attr("name")..":~$ ") -- print welcome
|
||||
|
|
|
@ -42,7 +42,7 @@ function digicompute.register_computer(termstring, desc)
|
|||
end,
|
||||
on_destruct = function(pos)
|
||||
local meta = minetest.get_meta(pos) -- get meta
|
||||
if meta:get_string("name") then digicompute.fs.rm(pos) end
|
||||
if meta:get_string("name") then digicompute.fs.deinit(pos) end
|
||||
end,
|
||||
})
|
||||
-- bios
|
||||
|
@ -103,7 +103,7 @@ function digicompute.register_computer(termstring, desc)
|
|||
end,
|
||||
on_destruct = function(pos)
|
||||
local meta = minetest.get_meta(pos) -- get meta
|
||||
if meta:get_string("name") then digicompute.fs.rm(pos) end
|
||||
if meta:get_string("name") then digicompute.fs.deinit(pos) end
|
||||
end,
|
||||
on_receive_fields = function(pos, formname, fields, sender) -- process formdata
|
||||
local meta = minetest.get_meta(pos) -- get meta
|
||||
|
|
126
env.lua
126
env.lua
|
@ -37,71 +37,133 @@ function digicompute.create_env(pos, fields)
|
|||
return string.find(...)
|
||||
end
|
||||
|
||||
-- [function] set
|
||||
local function set_string(key, value)
|
||||
return meta:set_string(key, value)
|
||||
-- [function] get attr (from meta)
|
||||
local function get_attr(key)
|
||||
return meta:get_string(key) or nil
|
||||
end
|
||||
-- [function] get
|
||||
local function get_string(key)
|
||||
return meta:get_string(key)
|
||||
-- [function] get userdata
|
||||
local function get_userdata(key)
|
||||
local t = minetest.deserialize(meta:get_string("userspace"))
|
||||
return t[key] or nil
|
||||
end
|
||||
-- [function] set int
|
||||
local function set_int(key, value)
|
||||
return meta:set_int(key, value)
|
||||
end
|
||||
-- [function] get int
|
||||
local function get_int(key)
|
||||
return meta:get_int(key)
|
||||
end
|
||||
-- [function] set float
|
||||
local function set_float(key, value)
|
||||
return meta:set_float(key, value)
|
||||
end
|
||||
-- [function] get float
|
||||
local function get_float(key)
|
||||
return meta:get_float(key)
|
||||
-- [function] set userdata
|
||||
local function set_userdata(key, value)
|
||||
local t = minetest.deserialize(meta:get_string("userspace"))
|
||||
t[key] = value
|
||||
return meta:set_string("userspace", minetest.serialize(t))
|
||||
end
|
||||
-- [function] get input
|
||||
local function get_input()
|
||||
return meta:get_string("input")
|
||||
return meta:get_string("input") or nil
|
||||
end
|
||||
-- [function] set input
|
||||
local function set_input(value)
|
||||
return meta:set_string("input", value)
|
||||
return meta:set_string("input", value) or nil
|
||||
end
|
||||
-- [function] get output
|
||||
local function get_output()
|
||||
return meta:get_string("output")
|
||||
return meta:get_string("output") or nil
|
||||
end
|
||||
-- [function] set output
|
||||
local function set_output(value)
|
||||
return meta:set_string("output", value)
|
||||
return meta:set_string("output", value) or nil
|
||||
end
|
||||
-- [function] get field
|
||||
local function get_field(key)
|
||||
return fields[key]
|
||||
return fields[key] or nil
|
||||
end
|
||||
-- [function] refresh
|
||||
local function refresh()
|
||||
meta:set_string("formspec", digicompute.formspec(meta:get_string("input"), meta:get_string("output")))
|
||||
return true
|
||||
end
|
||||
|
||||
-- filesystem API
|
||||
|
||||
-- [function] get file (read)
|
||||
local function get_file(path)
|
||||
local res = digicompute.fs.get_file(pos, path)
|
||||
if res then return res end
|
||||
end
|
||||
|
||||
-- [function] get directory contents
|
||||
local function get_dir(path)
|
||||
local res = digicompute.fs.get_dir(pos, path)
|
||||
if res then return res end
|
||||
end
|
||||
|
||||
-- [function] exists
|
||||
local function exists(path)
|
||||
local res = digicompute.fs.exists(pos, path)
|
||||
if res then return res end
|
||||
end
|
||||
|
||||
-- [function] mkdir
|
||||
local function mkdir(path)
|
||||
local res = digicompute.fs.exists(pos, path)
|
||||
if res then return res end
|
||||
end
|
||||
|
||||
-- [function] rmdir
|
||||
local function rmdir(path)
|
||||
local res = digicompute.fs.rmdir(pos, path)
|
||||
if res then return res end
|
||||
end
|
||||
|
||||
-- [function] mkdir
|
||||
local function mkdir(path)
|
||||
local res = digicompute.fs.exists(pos, path)
|
||||
if res then return res end
|
||||
end
|
||||
|
||||
-- [function] create file
|
||||
local function create(path)
|
||||
local res = digicompute.fs.create(pos, path)
|
||||
if res then return res end
|
||||
end
|
||||
|
||||
-- [function] write
|
||||
local function write(path, data)
|
||||
local res = digicompute.fs.write(pos, path, data)
|
||||
if res then return res end
|
||||
end
|
||||
|
||||
-- [function] append
|
||||
local function append(path, data)
|
||||
local res = digicompute.fs.append(pos, path, data)
|
||||
if res then return res end
|
||||
end
|
||||
|
||||
-- [function] copy
|
||||
local function copy(path, npath)
|
||||
local res = digicompute.fs.copy(pos, path, npath)
|
||||
if res then return res end
|
||||
end
|
||||
|
||||
-- ENVIRONMENT TABLE --
|
||||
|
||||
local env = {
|
||||
run = digicompute.run,
|
||||
set_string = set_string,
|
||||
get_string = get_string,
|
||||
set_int = set_int,
|
||||
get_int = get_int,
|
||||
set_float = set_float,
|
||||
get_float = get_float,
|
||||
get_attr = get_attr,
|
||||
get_userdata = get_userdata,
|
||||
set_userdata = set_userdata,
|
||||
get_input = get_input,
|
||||
set_input = set_input,
|
||||
get_output = get_output,
|
||||
set_output = set_output,
|
||||
get_field = get_field,
|
||||
refresh = refresh,
|
||||
fs = {
|
||||
read = get_file,
|
||||
list = get_dir,
|
||||
check = exists,
|
||||
mkdir = mkdir,
|
||||
rmdir = rmdir,
|
||||
touch = create,
|
||||
write = write,
|
||||
copy = copy,
|
||||
cp = copy,
|
||||
},
|
||||
string = {
|
||||
byte = string.byte,
|
||||
char = string.char,
|
||||
|
|
89
fs.lua
89
fs.lua
|
@ -23,7 +23,7 @@ function digicompute.fs.init(pos, cname)
|
|||
end
|
||||
|
||||
-- [function] de-initialize fs (delete)
|
||||
function digicompute.fs.rm(pos)
|
||||
function digicompute.fs.deinit(pos)
|
||||
local meta = minetest.get_meta(pos) -- meta
|
||||
local player = meta:get_string("owner") -- owner username
|
||||
local cname = meta:get_string("name") -- name
|
||||
|
@ -52,8 +52,9 @@ function digicompute.fs.rm(pos)
|
|||
end
|
||||
|
||||
-- [function] get file contents
|
||||
function digicompute.fs.get_file(pos, cname, fpath)
|
||||
function digicompute.fs.get_file(pos, fpath)
|
||||
local meta = minetest.get_meta(pos) -- meta
|
||||
local cname = meta:get_string("name") -- computer name
|
||||
local player = meta:get_string("owner") -- owner username
|
||||
local cpath = path.."/"..player.."/"..cname -- comp path
|
||||
local contents = datalib.read(cpath.."/"..fpath)
|
||||
|
@ -62,8 +63,9 @@ function digicompute.fs.get_file(pos, cname, fpath)
|
|||
end
|
||||
|
||||
-- [function] get directory contents
|
||||
function digicompute.fs.get_dir(pos, cname, dpath)
|
||||
function digicompute.fs.get_dir(pos, dpath)
|
||||
local meta = minetest.get_meta(pos) -- meta
|
||||
local cname = meta:get_string("name") -- computer name
|
||||
local player = meta:get_string("owner") -- owner username
|
||||
local cpath = path.."/"..player.."/"..cname -- comp path
|
||||
local files = minetest.get_dir_list(cpath.."/"..dpath, false)
|
||||
|
@ -72,23 +74,66 @@ function digicompute.fs.get_dir(pos, cname, dpath)
|
|||
return { error = nil, contents = { files = files, subdirs = subdirs } }
|
||||
end
|
||||
|
||||
-- [function] run file under env
|
||||
function digicompute.fs.run_file(pos, lpath, fields, replace)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local lpath = path.."/"..meta:get_string("owner").."/"..meta:get_string("name").."/"..lpath
|
||||
local env = digicompute.create_env(pos, fields) -- environment
|
||||
local f = loadfile(lpath) -- load func
|
||||
local e = digicompute.run(f, env) -- run function
|
||||
-- if error, call error handle function and re-run start
|
||||
if e and replace then
|
||||
datalib.copy(lpath, lpath..".old")
|
||||
datalib.copy(modpath.."/bios/"..replace, lpath)
|
||||
meta:set_string("output", "Error: \n"..msg.."\n\nRestoring OS, modified files will remain.") -- set output
|
||||
digicompute.refresh(pos) -- refresh
|
||||
minetest.after(13, function() -- after 13 seconds, run start
|
||||
local s = loadfile(path.."/"..meta:get_string("owner").."/"..meta:get_string("name").."/os/start.lua") -- load func
|
||||
digicompute.run(s, env) -- run func
|
||||
return false
|
||||
end)
|
||||
elseif e and not replace then return e end -- elseif no replace and error, return error msg
|
||||
-- [function] exists
|
||||
function digicompute.fs.exists(pos, fpath)
|
||||
local meta = minetest.get_meta(pos) -- meta
|
||||
local cname = meta:get_string("name") -- computer name
|
||||
local name = meta:get_string("owner") -- owner username
|
||||
local res = datalib.exists(path.."/"..name.."/"..cname.."/"..fpath)
|
||||
if res == true then return "File exists."
|
||||
else return "File does not exist."
|
||||
end
|
||||
end
|
||||
|
||||
-- [function] create directory
|
||||
function digicompute.fs.mkdir(pos, fpath)
|
||||
local meta = minetest.get_meta(pos) -- meta
|
||||
local cname = meta:get_string("name") -- computer name
|
||||
local name = meta:get_string("owner") -- owner username
|
||||
local res = datalib.mkdir(path.."/"..name.."/"..cname.."/"..fpath)
|
||||
if res == true then return "Directory already exists." end
|
||||
end
|
||||
|
||||
-- [function] remove directory
|
||||
function digicompute.fs.rmdir(pos, fpath)
|
||||
local meta = minetest.get_meta(pos) -- meta
|
||||
local cname = meta:get_string("name") -- computer name
|
||||
local name = meta:get_string("owner") -- owner username
|
||||
local res = datalib.rmdir(path.."/"..name.."/"..cname.."/"..fpath)
|
||||
if res == false then return "Directory does not exist." end
|
||||
end
|
||||
|
||||
-- [function] create file
|
||||
function digicompute.fs.create(pos, fpath)
|
||||
local meta = minetest.get_meta(pos) -- meta
|
||||
local cname = meta:get_string("name") -- computer name
|
||||
local name = meta:get_string("owner") -- owner username
|
||||
local res = datalib.create(path.."/"..name.."/"..cname.."/"..fpath)
|
||||
if res == true then return "File already exists." end
|
||||
end
|
||||
|
||||
-- [function] write
|
||||
function digicompute.fs.write(pos, fpath, data)
|
||||
local meta = minetest.get_meta(pos) -- meta
|
||||
local cname = meta:get_string("name") -- computer name
|
||||
local name = meta:get_string("owner") -- owner username
|
||||
datalib.write(path.."/"..name.."/"..cname.."/"..fpath, data, false)
|
||||
end
|
||||
|
||||
-- [function] append
|
||||
function digicompute.fs.append(pos, fpath, data)
|
||||
local meta = minetest.get_meta(pos) -- meta
|
||||
local cname = meta:get_string("name") -- computer name
|
||||
local name = meta:get_string("owner") -- owner username
|
||||
datalib.append(path.."/"..name.."/"..cname.."/"..fpath, data, false)
|
||||
end
|
||||
|
||||
-- [function] copy file
|
||||
function digicompute.fs.copy(pos, fpath, npath)
|
||||
local meta = minetest.get_meta(pos) -- meta
|
||||
local cname = meta:get_string("name") -- computer name
|
||||
local name = meta:get_string("owner") -- owner username
|
||||
local res = datalib.copy(path.."/"..name.."/"..cname.."/"..fpath, path.."/"..name.."/"..cname.."/"..npath)
|
||||
if res == false then return "Base file does not exist." end
|
||||
end
|
||||
digicompute.fs.cp = digicompute.fs.copy
|
||||
|
|
Loading…
Reference in New Issue