aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api/l_nodetimer.h
blob: 9f8dd21c8ccbfb42be883be7c15861fd3e488c7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef L_NODETIMER_H_
#define L_NODETIMER_H_

#include "lua_api/l_base.h"
#include "irr_v3d.h"

class ServerEnvironment;

class NodeTimerRef : public ModApiBase {
private:
	v3s16 m_p;
	ServerEnvironment *m_env;

	static const char className[];
	static const luaL_reg methods[];

	static int gc_object(lua_State *L);

	static NodeTimerRef *checkobject(lua_State *L, int narg);

	static int l_set(lua_State *L);

	static int l_start(lua_State *L);

	static int l_stop(lua_State *L);

	static int l_is_started(lua_State *L);

	static int l_get_timeout(lua_State *L);

	static int l_get_elapsed(lua_State *L);

public:
	NodeTimerRef(v3s16 p, ServerEnvironment *env);
	~NodeTimerRef();

	// Creates an NodeTimerRef and leaves it on top of stack
	// Not callable from Lua; all references are created on the C side.
	static void create(lua_State *L, v3s16 p, ServerEnvironment *env);

	static void set_null(lua_State *L);

	static void Register(lua_State *L);
};



#endif /* L_NODETIMER_H_ */
-- * Booleans, numbers, strings, and nil. -- * Functions; uses interpreter-dependent (and sometimes platform-dependent) bytecode! -- * Tables; they can cantain multiple references and can be recursive, but metatables aren't saved. -- This works in two phases: -- 1. Recursively find and record multiple references and recursion. -- 2. Recursively dump the value into a string. -- @param x Value to serialize (nil is allowed). -- @return load()able string containing the value. function core.serialize(x) local local_index = 1 -- Top index of the "_" local table in the dump -- table->nil/1/2 set of tables seen. -- nil = not seen, 1 = seen once, 2 = seen multiple times. local seen = {} -- nest_points are places where a table appears within itself, directly -- or not. For instance, all of these chunks create nest points in -- table x: "x = {}; x[x] = 1", "x = {}; x[1] = x", -- "x = {}; x[1] = {y = {x}}". -- To handle those, two tables are used by mark_nest_point: -- * nested - Transient set of tables being currently traversed. -- Used for detecting nested tables. -- * nest_points - parent->{key=value, ...} table cantaining the nested -- keys and values in the parent. They're all dumped after all the -- other table operations have been performed. -- -- mark_nest_point(p, k, v) fills nest_points with information required -- to remember that key/value (k, v) creates a nest point in table -- parent. It also marks "parent" and the nested item(s) as occuring -- multiple times, since several references to it will be required in -- order to patch the nest points. local nest_points = {} local nested = {} local function mark_nest_point(parent, k, v) local nk, nv = nested[k], nested[v] local np = nest_points[parent] if not np then np = {} nest_points[parent] = np end np[k] = v seen[parent] = 2 if nk then seen[k] = 2 end if nv then seen[v] = 2 end end -- First phase, list the tables and functions which appear more than -- once in x. local function mark_multiple_occurences(x) local tp = type(x) if tp ~= "table" and tp ~= "function" then -- No identity (comparison is done by value, not by instance) return end if seen[x] == 1 then seen[x] = 2 elseif seen[x] ~= 2 then seen[x] = 1 end if tp == "table" then nested[x] = true for k, v in pairs(x) do if nested[k] or nested[v] then mark_nest_point(x, k, v) else mark_multiple_occurences(k) mark_multiple_occurences(v) end end nested[x] = nil end end local dumped = {} -- object->varname set local local_defs = {} -- Dumped local definitions as source code lines -- Mutually recursive local functions: local dump_val, dump_or_ref_val -- If x occurs multiple times, dump the local variable rather than -- the value. If it's the first time it's dumped, also dump the -- content in local_defs. function dump_or_ref_val(x) if seen[x] ~= 2 then return dump_val(x) end local var = dumped[x] if var then -- Already referenced return var end -- First occurence, create and register reference local val = dump_val(x) local i = local_index local_index = local_index + 1 var = "_["..i.."]" local_defs[#local_defs + 1] = var.." = "..val dumped[x] = var return var end -- Second phase. Dump the object; subparts occuring multiple times -- are dumped in local variables which can be referenced multiple -- times. Care is taken to dump local vars in a sensible order. function dump_val(x) local tp = type(x) if x == nil then return "nil" elseif tp == "string" then return string.format("%q", x) elseif tp == "boolean" then return x and "true" or "false" elseif tp == "function" then return string.format("loadstring(%q)", string.dump(x)) elseif tp == "number" then -- Serialize numbers reversibly with string.format return string.format("%.17g", x) elseif tp == "table" then local vals = {} local idx_dumped = {} local np = nest_points[x] for i, v in ipairs(x) do if not np or not np[i] then vals[#vals + 1] = dump_or_ref_val(v) end idx_dumped[i] = true end for k, v in pairs(x) do if (not np or not np[k]) and not idx_dumped[k] then vals[#vals + 1] = "["..dump_or_ref_val(k).."] = " ..dump_or_ref_val(v) end end return "{"..table.concat(vals, ", ").."}" else error("Can't serialize data of type "..tp) end end local function dump_nest_points() for parent, vals in pairs(nest_points) do for k, v in pairs(vals) do local_defs[#local_defs + 1] = dump_or_ref_val(parent) .."["..dump_or_ref_val(k).."] = " ..dump_or_ref_val(v) end end end mark_multiple_occurences(x) local top_level = dump_or_ref_val(x) dump_nest_points() if next(local_defs) then return "local _ = {}\n" ..table.concat(local_defs, "\n") .."\nreturn "..top_level else return "return "..top_level end end -- Deserialization local function safe_loadstring(...) local func, err = loadstring(...) if func then setfenv(func, {}) return func end return nil, err end local function dummy_func() end function core.deserialize(str, safe) if type(str) ~= "string" then return nil, "Cannot deserialize type '"..type(str) .."'. Argument must be a string." end if str:byte(1) == 0x1B then return nil, "Bytecode prohibited" end local f, err = loadstring(str) if not f then return nil, err end -- The environment is recreated every time so deseralized code cannot -- pollute it with permanent references. setfenv(f, {loadstring = safe and dummy_func or safe_loadstring}) local good, data = pcall(f) if good then return data else return nil, data end end