Clean up custom environment definition
* Separate custom function definitions from make_env for better readability * Add newlines between functions * Make comments more descriptive * Move `Settings()` API to `fs.read_settings()`master
parent
642139b1b8
commit
327a6dd29e
|
@ -86,11 +86,6 @@ __Usage:__ `set_run(<file path (string)>)`
|
|||
|
||||
Set the file that is to be run when text in the input bar is submitted. Defaults to `os/main.lua`. If the `path` parameter is not provided, the run file will be reset to the default.
|
||||
|
||||
#### `Settings(path)`
|
||||
__Usage:__ `Settings(<path (string)>)`
|
||||
|
||||
Returns a userdata value containing a list of the "settings" defined in a file. Each setting should be on a new line, like variables but without `local` as a prefix. This "object" has several methods that you can use on it (e.g. `:to_table()`), however they are not documented here. Instead, see the methods section in the [documentation for node meta](http://dev.minetest.net/NodeMetaRef#Methods) as `Settings()` and node meta share the same methods.
|
||||
|
||||
## Filesystem
|
||||
This API section introduces function to interact with the computer's physical filesystem.
|
||||
|
||||
|
@ -148,3 +143,8 @@ Recursively copies a directory and all it's sub-directories and files. Returns `
|
|||
__Usage:__ `fs.run(<path to file (string)>, <additional paramters>)`
|
||||
|
||||
Attempts to read the contents of a file, treating it as Lua code to be run under the environment. Returns two parameters, the first representing success and the second being `nil` unless the operation was not successful, in which case it contains an error message. Any number of additional parameters can be provided after the path, to be accessed by the code being run.
|
||||
|
||||
#### `read_settings(path)`
|
||||
__Usage:__ `fs.read_settings(<path (string)>)`
|
||||
|
||||
Returns a userdata value containing a list of the "settings" defined in a file. Each setting should be on a new line, like variables but without `local` as a prefix. This "object" has several methods that you can use on it (e.g. `:to_table()`), however they are not documented here. Instead, see the methods section in the [documentation for node meta](http://dev.minetest.net/NodeMetaRef#Methods) as `Settings()` and node meta share the same methods.
|
||||
|
|
|
@ -2,29 +2,23 @@
|
|||
|
||||
local environments = {}
|
||||
|
||||
-----------------------
|
||||
----- ENVIRONMENT -----
|
||||
-----------------------
|
||||
-----------------------------
|
||||
----- EXPOSED FUNCTIONS -----
|
||||
-----------------------------
|
||||
|
||||
-- [function] Make environment
|
||||
function digicompute.c:make_env(pos)
|
||||
assert(pos, "digicompute.c:make_env missing position")
|
||||
local meta = minetest.get_meta(pos)
|
||||
local id = meta:get_string("id")
|
||||
-- [local function] Create the table of custom functions
|
||||
local function create_env_table(meta, pos)
|
||||
local cpath = meta:get_string("path") -- Get computer path
|
||||
-- Define basic tables
|
||||
local env = {
|
||||
fs = {}, -- Filesystem API
|
||||
ram = {}, -- RAM userdata table
|
||||
}
|
||||
|
||||
-- if an environment for this computer has already been generated, return it instead
|
||||
if environments[id] then
|
||||
return environments[id]
|
||||
end
|
||||
--- General Functions ---
|
||||
|
||||
local cpath = meta:get_string("path")
|
||||
|
||||
-- Main Environment Functions
|
||||
|
||||
local main = {}
|
||||
|
||||
-- [function] print
|
||||
function main.print(contents, newline)
|
||||
-- [function] Print to computer console
|
||||
function env.print(contents, newline)
|
||||
if type(contents) ~= "string" then
|
||||
contents = dump(contents)
|
||||
end
|
||||
|
@ -34,50 +28,60 @@ function digicompute.c:make_env(pos)
|
|||
newline = "\n"
|
||||
end
|
||||
|
||||
meta:set_string("output", meta:get_string("output")..newline..contents)
|
||||
meta:set_string("output", meta:get_string("output")..newline..
|
||||
contents)
|
||||
end
|
||||
-- [function] set help
|
||||
function main.set_help(value)
|
||||
|
||||
-- [function] Set help text shown when hovering over question mark button
|
||||
function env.set_help(value)
|
||||
if not value or type(value) ~= "string" then
|
||||
value = "Type a command and press enter."
|
||||
end
|
||||
|
||||
return meta:set_string("help", value)
|
||||
end
|
||||
-- [function] get attribute
|
||||
function main.get_attr(key)
|
||||
|
||||
-- [function] Get an attribute from the computer meta
|
||||
function env.get_attr(key)
|
||||
return meta:to_table().fields[key] or nil
|
||||
end
|
||||
-- [function] get output
|
||||
function main.get_output()
|
||||
|
||||
-- [function] Get the value of the output formspec field
|
||||
function env.get_output()
|
||||
return meta:get_string("output") or nil
|
||||
end
|
||||
-- [function] set output
|
||||
function main.set_output(value)
|
||||
|
||||
-- [function] Set the value of the output formspec field
|
||||
function env.set_output(value)
|
||||
return meta:set_string("output", value)
|
||||
end
|
||||
-- [function] set whether output is writable
|
||||
function main.set_output_editable(bool)
|
||||
|
||||
-- [function] Toggle whether the output area can be edited
|
||||
function env.set_output_editable(bool)
|
||||
if bool == true then
|
||||
meta:set_string("output_editable", "true")
|
||||
else
|
||||
meta:set_string("output_editable", "false")
|
||||
end
|
||||
end
|
||||
-- [function] get input
|
||||
function main.get_input()
|
||||
|
||||
-- [function] Get the value of the input formspec field
|
||||
function env.get_input()
|
||||
return meta:get_string("input") or nil
|
||||
end
|
||||
-- [function] set input
|
||||
function main.set_input(value)
|
||||
|
||||
-- [function] Set the value of the input formspec field
|
||||
function env.set_input(value)
|
||||
return meta:set_string("input", value)
|
||||
end
|
||||
-- [function] get os value
|
||||
function main.get_os(key)
|
||||
|
||||
-- [function] Get a value from the OS table
|
||||
function env.get_os(key)
|
||||
return minetest.deserialize(meta:get_string("os"))[key] or nil
|
||||
end
|
||||
-- [function] set os value
|
||||
function main.set_os(key, value)
|
||||
|
||||
-- [function] Set the value of one of the allowed keys in the OS table
|
||||
function env.set_os(key, value)
|
||||
local allowed_keys = {
|
||||
clear = true,
|
||||
off = true,
|
||||
|
@ -93,8 +97,9 @@ function digicompute.c:make_env(pos)
|
|||
return false
|
||||
end
|
||||
end
|
||||
-- [function] refresh
|
||||
function main.refresh()
|
||||
|
||||
-- [function] Refresh the computer formspec
|
||||
function env.refresh()
|
||||
local current_user = meta:get_string("current_user")
|
||||
if current_user ~= "" then
|
||||
local player = minetest.get_player_by_name(current_user)
|
||||
|
@ -103,91 +108,115 @@ function digicompute.c:make_env(pos)
|
|||
end
|
||||
end
|
||||
end
|
||||
-- [function] run code
|
||||
function main.run(code, ...)
|
||||
|
||||
-- [function] Run a string representing Lua code within the environment
|
||||
function env.run(code, ...)
|
||||
return digicompute.c:run_code(pos, code, ...)
|
||||
end
|
||||
-- [function] set file to be run when input is submitted
|
||||
function main.set_run(run_path)
|
||||
|
||||
-- [function] Change the file that is run when input is given
|
||||
function env.set_run(run_path)
|
||||
if run_path then
|
||||
if digicompute.builtin.exists(cpath..run_path) then
|
||||
meta:set_string("run", run_path)
|
||||
end
|
||||
else
|
||||
meta:set_string("run", "os/main.lua")
|
||||
meta:set_string("run", "os/env.lua")
|
||||
end
|
||||
end
|
||||
|
||||
-- Filesystem Environment Functions
|
||||
--- Filesystem-Related Functions ---
|
||||
|
||||
local fs = {}
|
||||
|
||||
-- [function] exists
|
||||
function fs.exists(internal_path)
|
||||
-- [function] Check if a file exists
|
||||
function env.fs.exists(internal_path)
|
||||
return digicompute.builtin.exists(cpath..internal_path)
|
||||
end
|
||||
-- [function] create file
|
||||
function fs.create(internal_path)
|
||||
|
||||
-- [function] Create a file
|
||||
function env.fs.create(internal_path)
|
||||
return digicompute.builtin.create(cpath..internal_path)
|
||||
end
|
||||
-- [function] remove file
|
||||
function fs.remove(internal_path)
|
||||
|
||||
-- [function] Remove a file
|
||||
function env.fs.remove(internal_path)
|
||||
return os.remove(cpath..internal_path)
|
||||
end
|
||||
-- [function] write to file
|
||||
function fs.write(internal_path, data, mode)
|
||||
|
||||
-- [function] Write to a file
|
||||
function env.fs.write(internal_path, data, mode)
|
||||
if type(data) ~= "string" then
|
||||
data = dump(data)
|
||||
end
|
||||
return digicompute.builtin.write(cpath..internal_path, data, mode)
|
||||
end
|
||||
-- [function] read file
|
||||
function fs.read(internal_path)
|
||||
|
||||
-- [function] Read from a file
|
||||
function env.fs.read(internal_path)
|
||||
return digicompute.builtin.read(cpath..internal_path)
|
||||
end
|
||||
-- [function] list directory contents
|
||||
function fs.list(internal_path)
|
||||
|
||||
-- [function] List the contents of a directory
|
||||
function env.fs.list(internal_path)
|
||||
return digicompute.builtin.list(cpath..internal_path)
|
||||
end
|
||||
-- [function] copy file
|
||||
function fs.copy(original, new)
|
||||
|
||||
-- [function] Copy a file
|
||||
function env.fs.copy(original, new)
|
||||
return digicompute.builtin.copy(cpath..original, cpath..new)
|
||||
end
|
||||
-- [function] create directory
|
||||
function fs.mkdir(internal_path)
|
||||
|
||||
-- [function] Create a directory
|
||||
function env.fs.mkdir(internal_path)
|
||||
return digicompute.builtin.mkdir(cpath..internal_path)
|
||||
end
|
||||
-- [function] remove directory
|
||||
function fs.rmdir(internal_path)
|
||||
|
||||
-- [function] Remove a directory
|
||||
function env.fs.rmdir(internal_path)
|
||||
return digicompute.builtin.rmdir(cpath..internal_path)
|
||||
end
|
||||
-- [function] copy directory
|
||||
function fs.cpdir(original, new)
|
||||
|
||||
-- [function] Copy a directory
|
||||
function env.fs.cpdir(original, new)
|
||||
return digicompute.builtin.cpdir(cpath..original, cpath..new)
|
||||
end
|
||||
-- [function] run file
|
||||
function fs.run(internal_path, ...)
|
||||
|
||||
-- [function] Read the contents of a file and run it as Lua code
|
||||
function env.fs.run(internal_path, ...)
|
||||
return digicompute.c:run_file(pos, internal_path, ...)
|
||||
end
|
||||
-- [function] Settings
|
||||
function main.Settings(internal_path)
|
||||
|
||||
-- [function] Create a settings object
|
||||
function env.fs.read_settings(internal_path)
|
||||
local fpath = cpath..internal_path
|
||||
if digicompute.builtin.exists(fpath) then
|
||||
return Settings(fpath)
|
||||
end
|
||||
end
|
||||
|
||||
-- Get default env table
|
||||
return env -- Return custom env functions
|
||||
end
|
||||
|
||||
local env = digicompute.env()
|
||||
---------------------------
|
||||
----- ENVIRONMENT API -----
|
||||
---------------------------
|
||||
|
||||
env.fs = fs
|
||||
-- [function] Make environment
|
||||
function digicompute.c:make_env(pos)
|
||||
assert(pos, "digicompute.c:make_env missing position")
|
||||
local meta = minetest.get_meta(pos)
|
||||
local id = meta:get_string("id")
|
||||
|
||||
for k, v in pairs(main) do
|
||||
env[k] = v
|
||||
-- if an environment for this computer has already been generated, return it instead
|
||||
if environments[id] then
|
||||
return environments[id]
|
||||
end
|
||||
|
||||
env.ram = {} -- RAM table, replacement for userdata
|
||||
local env = digicompute.env()
|
||||
local custom_env = create_env_table(meta, pos)
|
||||
-- Custom custom env with default environment table
|
||||
for k, v in pairs(custom_env) do
|
||||
env[k] = v
|
||||
end
|
||||
|
||||
environments[id] = env
|
||||
return environments[id]
|
||||
|
|
|
@ -11,7 +11,7 @@ local bin = {}
|
|||
local bin_contents = fs.list("os/bin")
|
||||
for _,f in ipairs(bin_contents.files) do
|
||||
local fpath = "os/bin/"..f
|
||||
local cmd_info = Settings(fpath):to_table()
|
||||
local cmd_info = fs.read_settings(fpath):to_table()
|
||||
local name = cmd_info.name or f
|
||||
|
||||
bin[name] = {
|
||||
|
|
Loading…
Reference in New Issue