Line data Source code
1 : /*
2 : Minetest
3 : Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 :
5 : This program is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU Lesser General Public License as published by
7 : the Free Software Foundation; either version 2.1 of the License, or
8 : (at your option) any later version.
9 :
10 : This program is distributed in the hope that it will be useful,
11 : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : GNU Lesser General Public License for more details.
14 :
15 : You should have received a copy of the GNU Lesser General Public License along
16 : with this program; if not, write to the Free Software Foundation, Inc.,
17 : 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 : */
19 :
20 : #include "lua_api/l_rollback.h"
21 : #include "lua_api/l_internal.h"
22 : #include "common/c_converter.h"
23 : #include "server.h"
24 : #include "rollback_interface.h"
25 :
26 :
27 0 : void push_RollbackNode(lua_State *L, RollbackNode &node)
28 : {
29 0 : lua_createtable(L, 0, 3);
30 0 : lua_pushstring(L, node.name.c_str());
31 0 : lua_setfield(L, -2, "name");
32 0 : lua_pushnumber(L, node.param1);
33 0 : lua_setfield(L, -2, "param1");
34 0 : lua_pushnumber(L, node.param2);
35 0 : lua_setfield(L, -2, "param2");
36 0 : }
37 :
38 : // rollback_get_node_actions(pos, range, seconds, limit) -> {{actor, pos, time, oldnode, newnode}, ...}
39 0 : int ModApiRollback::l_rollback_get_node_actions(lua_State *L)
40 : {
41 0 : v3s16 pos = read_v3s16(L, 1);
42 0 : int range = luaL_checknumber(L, 2);
43 0 : time_t seconds = (time_t) luaL_checknumber(L, 3);
44 0 : int limit = luaL_checknumber(L, 4);
45 0 : Server *server = getServer(L);
46 0 : IRollbackManager *rollback = server->getRollbackManager();
47 0 : if (rollback == NULL) {
48 0 : return 0;
49 : }
50 :
51 0 : std::list<RollbackAction> actions = rollback->getNodeActors(pos, range, seconds, limit);
52 0 : std::list<RollbackAction>::iterator iter = actions.begin();
53 :
54 0 : lua_createtable(L, actions.size(), 0);
55 0 : for (unsigned int i = 1; iter != actions.end(); ++iter, ++i) {
56 0 : lua_createtable(L, 0, 5); // Make a table with enough space pre-allocated
57 :
58 0 : lua_pushstring(L, iter->actor.c_str());
59 0 : lua_setfield(L, -2, "actor");
60 :
61 0 : push_v3s16(L, iter->p);
62 0 : lua_setfield(L, -2, "pos");
63 :
64 0 : lua_pushnumber(L, iter->unix_time);
65 0 : lua_setfield(L, -2, "time");
66 :
67 0 : push_RollbackNode(L, iter->n_old);
68 0 : lua_setfield(L, -2, "oldnode");
69 :
70 0 : push_RollbackNode(L, iter->n_new);
71 0 : lua_setfield(L, -2, "newnode");
72 :
73 0 : lua_rawseti(L, -2, i); // Add action table to main table
74 : }
75 :
76 0 : return 1;
77 : }
78 :
79 : // rollback_revert_actions_by(actor, seconds) -> bool, log messages
80 0 : int ModApiRollback::l_rollback_revert_actions_by(lua_State *L)
81 : {
82 0 : std::string actor = luaL_checkstring(L, 1);
83 0 : int seconds = luaL_checknumber(L, 2);
84 0 : Server *server = getServer(L);
85 0 : IRollbackManager *rollback = server->getRollbackManager();
86 :
87 : // If rollback is disabled, tell it's not a success.
88 0 : if (rollback == NULL) {
89 0 : lua_pushboolean(L, false);
90 0 : lua_newtable(L);
91 0 : return 2;
92 : }
93 0 : std::list<RollbackAction> actions = rollback->getRevertActions(actor, seconds);
94 0 : std::list<std::string> log;
95 0 : bool success = server->rollbackRevertActions(actions, &log);
96 : // Push boolean result
97 0 : lua_pushboolean(L, success);
98 0 : lua_createtable(L, log.size(), 0);
99 0 : unsigned long i = 0;
100 0 : for(std::list<std::string>::const_iterator iter = log.begin();
101 0 : iter != log.end(); ++i, ++iter) {
102 0 : lua_pushnumber(L, i);
103 0 : lua_pushstring(L, iter->c_str());
104 0 : lua_settable(L, -3);
105 : }
106 0 : return 2;
107 : }
108 :
109 0 : void ModApiRollback::Initialize(lua_State *L, int top)
110 : {
111 0 : API_FCT(rollback_get_node_actions);
112 0 : API_FCT(rollback_revert_actions_by);
113 3 : }
|