Replace OS APIs in favour of a system table
`get_os` and `set_os` APIs are replaced with a `system` table which stores the data and limits the valid keys and types via metatables.master
parent
31d4360570
commit
0326648b0d
|
@ -7,7 +7,7 @@ globals = {
|
|||
"digicompute",
|
||||
|
||||
-- OctOS Internals
|
||||
"ram",
|
||||
"ram", "system",
|
||||
}
|
||||
|
||||
read_globals = {
|
||||
|
@ -24,6 +24,5 @@ read_globals = {
|
|||
-- OctOS Internals (custom environment functions)
|
||||
"set_help", "get_attr", "get_output", "set_output",
|
||||
"set_output_editable", "get_input", "set_input",
|
||||
"get_os", "set_os", "refresh", "run", "loadstring",
|
||||
"set_run", "fs",
|
||||
"refresh", "run", "loadstring", "set_run", "fs",
|
||||
}
|
||||
|
|
|
@ -5,6 +5,21 @@ OctOS is the operating system used by digicomputers. OctOS is made of many Linux
|
|||
|
||||
In earlier versions of digicompute, a RAM-like storage mechanism could be accessed with a `get_userdata` and `set_userdata` API. However, this API was later removed and instead the RAM is accessed by setting values of the `ram` table within the environment. Data can also be preserved by setting a global variable within the environment, however, use of the `ram` table is recommended. This is because of how digicompute handles the environments in the background. Global variables, including the `ram` table, are stored within the environment itself, and with the environment being preserved until the computer is shut off or deinitialized, the variables are preserved as well. Please note that digicompute does not currently have a completely accurate representation of RAM, in that there is no limitation to the amount of data that can be stored. **Warning:** Functions and userdata cannot be stored within RAM.
|
||||
|
||||
## Accessing the System Information
|
||||
|
||||
Earlier versions of digicompute had an array of functions to get and set arbitrary customizable data regarding the system. This data includes the prefix, clear command, input buffer, and more. However, these APIs have been replaced with a single `system` table, available globally and saved with the help of metatables. There are a very limited amount of keys that can be written to in the system table, as documented below.
|
||||
|
||||
**Valid Keys:**
|
||||
* `clear` - `string`: command to clear the output and input.
|
||||
* `off` - `string`: command to turn the computer off.
|
||||
* `reboot` - `string`: command to reboot the computer.
|
||||
* `prefix` - `string`: prefix printed at the beginning of a new line.
|
||||
|
||||
**Example:**
|
||||
```lua
|
||||
system.reboot = "shutdown -r"
|
||||
```
|
||||
|
||||
## Main
|
||||
This contains a set of functions mainly for the purpose of interacting with the computer's displays.
|
||||
|
||||
|
@ -61,21 +76,6 @@ Returns the value of the input field. Shorthand for `get_attr("input")`.
|
|||
|
||||
Set the input field to any string. This is the write method for the input attribute.
|
||||
|
||||
#### `get_os(key)`
|
||||
**Usage:** `get_os(<data name (string)>)`
|
||||
|
||||
Gets a piece of information from the OS table. See next function for further information on this table.
|
||||
|
||||
#### `set_os(key, value)`
|
||||
**Usage:** `set_os(<data name (string)>, <value>`
|
||||
|
||||
Sets a piece of information stored in the OS table. This table stores basic values containing information global to the operating system. However, it is quite limitted, only being capable of storing a few pieces of information as listed below.
|
||||
|
||||
* `clear`: command to clear the output and input.
|
||||
* `off`: command to turn the computer off.
|
||||
* `reboot`: command to reboot the computer.
|
||||
* `prefix`: prefix printed at the beginning of a new line.
|
||||
|
||||
#### `refresh()`
|
||||
**Usage:** `refresh()`
|
||||
|
||||
|
|
|
@ -210,7 +210,7 @@ function digicompute.register_computer(itemstring, def)
|
|||
meta:set_string("input", "") -- Initialize input buffer
|
||||
meta:set_string("output", "") -- Initialize output buffer
|
||||
meta:set_string("debug", minetest.serialize({})) -- Initialize debug buffer
|
||||
meta:set_string("os", "") -- Initialize OS table
|
||||
meta:set_string("system", minetest.serialize({})) -- Initialize system table
|
||||
meta:set_string("ram", minetest.serialize({})) -- Initialize RAM preservation table
|
||||
meta:set_string("help", "Type a command and press enter.") -- Initialize help
|
||||
meta:set_string("output_editable", "false") -- Initialize uneditable output
|
||||
|
|
|
@ -13,6 +13,7 @@ local function create_env_table(meta, pos)
|
|||
local env = {
|
||||
fs = {}, -- Filesystem API
|
||||
ram = {}, -- RAM userdata table
|
||||
system = {}, -- Limited system table
|
||||
}
|
||||
|
||||
--- General Functions ---
|
||||
|
@ -80,29 +81,6 @@ local function create_env_table(meta, pos)
|
|||
return meta:set_string("input", value)
|
||||
end
|
||||
|
||||
-- [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 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,
|
||||
reboot = true,
|
||||
prefix = true,
|
||||
}
|
||||
|
||||
if allowed_keys[key] == true then
|
||||
local table = minetest.deserialize(meta:get_string("os")) or {}
|
||||
table[key] = value
|
||||
return meta:set_string("os", minetest.serialize(table))
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
-- [function] Refresh the computer formspec
|
||||
function env.refresh()
|
||||
local current_user = meta:get_string("current_user")
|
||||
|
@ -126,7 +104,7 @@ local function create_env_table(meta, pos)
|
|||
meta:set_string("run", run_path)
|
||||
end
|
||||
else
|
||||
meta:set_string("run", "os/env.lua")
|
||||
meta:set_string("run", "os/main.lua")
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -224,6 +202,40 @@ local function create_env_table(meta, pos)
|
|||
}
|
||||
setmetatable(env.ram, ram_mt)
|
||||
|
||||
local system_shadow = minetest.deserialize(meta:get_string("os"))
|
||||
-- Define OS metatable
|
||||
local system_mt = {
|
||||
-- Ensure value is allowed and save to meta as well
|
||||
__newindex = function(table, key, value)
|
||||
local allowed_keys = {
|
||||
clear = true,
|
||||
off = true,
|
||||
reboot = true,
|
||||
prefix = true,
|
||||
}
|
||||
|
||||
-- Ensure strings
|
||||
if type(value) ~= "string" then
|
||||
local msg = "Error: All values in the system table must be strings."
|
||||
env.print(msg)
|
||||
env.print_debug(msg)
|
||||
elseif not allowed_keys[key] then
|
||||
local msg = "Error: "..key.. " is not an allowed key in the system table."
|
||||
env.print(msg)
|
||||
env.print_debug(msg)
|
||||
else -- else, save
|
||||
rawset(system_shadow, key, value) -- Save to table
|
||||
-- Save to metadata
|
||||
meta:set_string("system", minetest.serialize(system_shadow))
|
||||
end
|
||||
end,
|
||||
-- Always fetch values from the shadow table
|
||||
__index = function(table, key)
|
||||
return system_shadow[key]
|
||||
end,
|
||||
}
|
||||
setmetatable(env.system, system_mt)
|
||||
|
||||
return env -- Return custom env functions
|
||||
end
|
||||
|
||||
|
|
|
@ -8,9 +8,9 @@ if get_attr("run") == "os/exec/nano.lua" then
|
|||
end
|
||||
|
||||
ram.nano_path = nil -- Clear nano path
|
||||
set_os("prefix", ram.nano_prefix) -- Restore prefix
|
||||
system.prefix = ram.nano_prefix -- Restore prefix
|
||||
ram.nano_prefix = nil -- Clear stored prefix
|
||||
set_output(ram.nano_output.."\n"..get_os("prefix")) -- Restore output
|
||||
set_output(ram.nano_output.."\n"..system.prefix) -- Restore output
|
||||
ram.nano_output = nil -- Clear stored output
|
||||
set_run() -- Clear custom run file
|
||||
|
||||
|
@ -34,8 +34,8 @@ else
|
|||
ram.nano_path = path -- Store editing path for later
|
||||
ram.nano_output = get_output() -- Store old output for later
|
||||
set_output(contents) -- Set output to contents of file or blank
|
||||
ram.nano_prefix = get_os("prefix") -- Store OS prefix for later
|
||||
set_os("prefix", "") -- Set OS prefix to a blank string
|
||||
ram.nano_prefix = system.prefix -- Store OS prefix for later
|
||||
system.prefix = "" -- Set OS prefix to a blank string
|
||||
set_help("\"save\" to save, \"discard\" to discard and exit, \"exit\" to save and exit") -- Add information to help
|
||||
set_run("os/exec/nano.lua") -- Set run file to the nano executable
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ if input[1] ~= "" then
|
|||
print(input[1]..": command not found")
|
||||
end
|
||||
|
||||
print(get_os("prefix"))
|
||||
print(system.prefix)
|
||||
|
||||
-- Clear input
|
||||
set_input("")
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
-- Set OS values
|
||||
set_os("clear", "clear")
|
||||
set_os("off", "shutdown")
|
||||
set_os("reboot", "shutdown -r")
|
||||
set_os("prefix", get_attr("name")..":~$ ")
|
||||
system.clear = "clear"
|
||||
system.off = "shutdown"
|
||||
system.reboot = "shutdown -r"
|
||||
system.prefix = get_attr("name")..":~$ "
|
||||
|
||||
-- Initialize bin table
|
||||
local bin = {}
|
||||
|
@ -22,15 +22,15 @@ for _,f in ipairs(bin_contents.files) do
|
|||
end
|
||||
|
||||
-- Add additional commands to bin
|
||||
bin[get_os("clear")] = { description = "Clear the shell output" } -- Clear shell output
|
||||
bin[get_os("off")] = { description = "Turn off computer" } -- Turn off computer
|
||||
bin[get_os("reboot")] = { description = "Reboot computer" } -- Reboot computer
|
||||
bin[system.clear] = { description = "Clear the shell output" } -- Clear shell output
|
||||
bin[system.off] = { description = "Turn off computer" } -- Turn off computer
|
||||
bin[system.reboot] = { description = "Reboot computer" } -- Reboot computer
|
||||
|
||||
-- Save bin table
|
||||
ram.bin = bin
|
||||
|
||||
-- Set initial output value
|
||||
set_output("Welcome to octOS version 0.2.\n\n"..get_os("prefix")) -- print welcome
|
||||
set_output("Welcome to octOS version 0.2.\n\n"..system.prefix) -- print welcome
|
||||
|
||||
-- Refresh view
|
||||
refresh()
|
||||
|
|
Loading…
Reference in New Issue