This repository has been archived on 2019-04-06. You can view files and clone it, but cannot push or open issues or pull requests.
adventin/stead/vars.lua

114 lines
2.1 KiB
Lua
Raw Normal View History

2010-06-25 12:31:54 +03:00
function isForSave(k, v, s) -- k - key, v - value, s -- parent table
local i,o
2010-08-31 21:37:49 +03:00
if type(s.variables_save) == 'table' and
s.variables_save[k] then
return true
2010-06-18 12:04:41 +03:00
end
2010-06-25 12:31:54 +03:00
if type(k) == 'function' then
return false
end
if type(v) == 'function' or type(v) == 'userdata' then
return false
end
return stead.string.find(k, '_') == 1
end
2010-06-18 12:04:41 +03:00
local function __vars_add(s, v, set)
2010-06-25 15:53:17 +03:00
local k, o
for k,o in pairs(v) do
if tonumber(k) then
stead.table.insert(s.variables, o);
elseif s.variables[k] then
error ("Variable overwrites variables object: "..tostring(k))
elseif k ~= 'variable_type' then
2010-08-31 21:37:49 +03:00
if set and not isObject(o) then
2010-07-05 09:44:12 +03:00
if s[k] then
error ("Global variable conflict: "..tostring(k));
end
stead.table.insert(s.variables, k);
s[k] = o
else
s.variables[k] = o
end
2010-06-25 15:53:17 +03:00
end
end
end
local function __vars_fill(v)
2010-06-18 12:04:41 +03:00
local k,o
2010-06-21 20:18:30 +03:00
if type(v) ~= 'table' then
return
end
2010-06-25 15:53:17 +03:00
for k,o in ipairs(v) do
if type(o) == 'table' and o.variable_type then
if type(v.variables) ~= 'table' then v.variables = {} end
__vars_add(v, o);
v[k] = nil
end
end
if type(v.variables) == 'table' then
2010-06-18 12:16:07 +03:00
local k,o
local vars = {}
2010-08-31 21:37:49 +03:00
v.variables_save = {}
2010-06-25 15:53:17 +03:00
for k,o in pairs(v.variables) do
2010-06-18 12:04:41 +03:00
if tonumber(k) and type(o) == 'string' then
stead.table.insert(vars, o)
else
2010-06-18 12:50:06 +03:00
if v[k] then
error ("Variable overwrites object property: "..tostring(k));
end
2010-06-18 12:04:41 +03:00
v[k] = o
stead.table.insert(vars, k);
end
end
2010-08-31 21:37:49 +03:00
for k,o in ipairs(vars) do
v.variables_save[o] = true
end
2010-06-25 15:53:17 +03:00
v.variables = vars;
2010-06-18 12:04:41 +03:00
end
2010-06-18 12:16:07 +03:00
end
2010-06-18 13:43:44 +03:00
vars_object = obj {
nam = 'vars',
2010-07-22 13:39:48 +03:00
system_type = true,
2010-06-18 13:43:44 +03:00
ini = function(s)
2010-07-08 11:06:27 +03:00
__vars_fill(_G)
2010-06-21 20:18:30 +03:00
__vars_fill(pl)
__vars_fill(game)
2010-06-18 13:43:44 +03:00
end
}
2010-06-23 15:09:33 +03:00
obj = stead.hook(obj,
2010-06-19 20:43:55 +03:00
function(f, v, ...)
2010-06-18 12:16:07 +03:00
__vars_fill(v)
2011-02-23 12:11:27 +02:00
return f(v, ...)
2010-06-18 12:04:41 +03:00
end)
2010-06-25 15:53:17 +03:00
stead.module_init(function()
2010-07-06 11:14:42 +03:00
local k,v
if type(variables) == 'nil' then
variables = {}
return
end
if type(variables) ~= 'table' then
return
end
for k,v in ipairs(variables) do
_G[v] = nil
end
2010-06-25 15:53:17 +03:00
variables = {}
end)
function var(v)
v.variable_type = true
return v
end
function global(v)
if type(v) ~= 'table' then
error("Wrong parameter to global.", 2);
end
2010-07-05 09:44:12 +03:00
__vars_add(_G, v, true);
2010-06-25 15:53:17 +03:00
end
2010-07-05 09:44:12 +03:00
-- vim:ts=4