Preserve RAM across MT restarts via metadata
RAM is still accessible with the RAM table, however, as a metatable is used to ensure that whenever information is set in the RAM table it is automatically stored in the metadata, and to ensure that functions and userdata is not stored.master
parent
ec5864b645
commit
31d4360570
|
@ -3,7 +3,7 @@ OctOS is the operating system used by digicomputers. OctOS is made of many Linux
|
|||
|
||||
## Accessing the RAM (userdata)
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
## Main
|
||||
This contains a set of functions mainly for the purpose of interacting with the computer's displays.
|
||||
|
|
|
@ -172,6 +172,8 @@ function digicompute.c:off(pos, player)
|
|||
digicompute.c:print_debug(pos, "Shut down")
|
||||
-- Clear output buffer
|
||||
meta:set_string("output", "")
|
||||
-- Clear RAM
|
||||
meta:set_string("ram", minetest.serialize({}))
|
||||
-- Reset environment
|
||||
digicompute.c:remove_env(pos)
|
||||
end
|
||||
|
@ -209,6 +211,7 @@ function digicompute.register_computer(itemstring, def)
|
|||
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("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
|
||||
meta:set_int("wrap_limit", 90) -- Initialize wrap chracter limit
|
||||
|
|
|
@ -198,6 +198,32 @@ local function create_env_table(meta, pos)
|
|||
end
|
||||
end
|
||||
|
||||
--- Metatables ---
|
||||
|
||||
local ram_shadow = minetest.deserialize(meta:get_string("ram"))
|
||||
-- Define RAM metatable
|
||||
local ram_mt = {
|
||||
-- Save to meta as well as to shadow table
|
||||
__newindex = function(table, key, value)
|
||||
local vtype = type(value)
|
||||
-- Prevent saving functions and userdata
|
||||
if vtype == "function" or vtype == "userdata" then
|
||||
local msg = "Error: Functions and userdata cannot be stored in the RAM."
|
||||
env.print(msg)
|
||||
env.print_debug(msg)
|
||||
else -- else, save
|
||||
rawset(ram_shadow, key, value) -- Save to table
|
||||
-- Save to metadata
|
||||
meta:set_string("ram", minetest.serialize(ram_shadow))
|
||||
end
|
||||
end,
|
||||
-- Always fetch values from the shadow table
|
||||
__index = function(table, key)
|
||||
return ram_shadow[key]
|
||||
end,
|
||||
}
|
||||
setmetatable(env.ram, ram_mt)
|
||||
|
||||
return env -- Return custom env functions
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue