3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "lua_api/l_rollback.h"
21 #include "lua_api/l_internal.h"
22 #include "common/c_converter.h"
24 #include "rollback_interface.h"
26 void push_RollbackNode(lua_State *L, RollbackNode &node)
28 lua_createtable(L, 0, 3);
29 lua_pushstring(L, node.name.c_str());
30 lua_setfield(L, -2, "name");
31 lua_pushnumber(L, node.param1);
32 lua_setfield(L, -2, "param1");
33 lua_pushnumber(L, node.param2);
34 lua_setfield(L, -2, "param2");
37 // rollback_get_node_actions(pos, range, seconds, limit) -> {{actor, pos, time, oldnode,
39 int ModApiRollback::l_rollback_get_node_actions(lua_State *L)
43 v3s16 pos = read_v3s16(L, 1);
44 int range = luaL_checknumber(L, 2);
45 time_t seconds = (time_t)luaL_checknumber(L, 3);
46 int limit = luaL_checknumber(L, 4);
47 Server *server = getServer(L);
48 IRollbackManager *rollback = server->getRollbackManager();
49 if (rollback == NULL) {
53 std::list<RollbackAction> actions =
54 rollback->getNodeActors(pos, range, seconds, limit);
55 std::list<RollbackAction>::iterator iter = actions.begin();
57 lua_createtable(L, actions.size(), 0);
58 for (unsigned int i = 1; iter != actions.end(); ++iter, ++i) {
59 lua_createtable(L, 0, 5); // Make a table with enough space pre-allocated
61 lua_pushstring(L, iter->actor.c_str());
62 lua_setfield(L, -2, "actor");
64 push_v3s16(L, iter->p);
65 lua_setfield(L, -2, "pos");
67 lua_pushnumber(L, iter->unix_time);
68 lua_setfield(L, -2, "time");
70 push_RollbackNode(L, iter->n_old);
71 lua_setfield(L, -2, "oldnode");
73 push_RollbackNode(L, iter->n_new);
74 lua_setfield(L, -2, "newnode");
76 lua_rawseti(L, -2, i); // Add action table to main table
82 // rollback_revert_actions_by(actor, seconds) -> bool, log messages
83 int ModApiRollback::l_rollback_revert_actions_by(lua_State *L)
87 std::string actor = luaL_checkstring(L, 1);
88 int seconds = luaL_checknumber(L, 2);
89 Server *server = getServer(L);
90 IRollbackManager *rollback = server->getRollbackManager();
92 // If rollback is disabled, tell it's not a success.
93 if (rollback == NULL) {
94 lua_pushboolean(L, false);
98 std::list<RollbackAction> actions = rollback->getRevertActions(actor, seconds);
99 std::list<std::string> log;
100 bool success = server->rollbackRevertActions(actions, &log);
101 // Push boolean result
102 lua_pushboolean(L, success);
103 lua_createtable(L, log.size(), 0);
105 for (std::list<std::string>::const_iterator iter = log.begin(); iter != log.end();
107 lua_pushnumber(L, i);
108 lua_pushstring(L, iter->c_str());
114 void ModApiRollback::Initialize(lua_State *L, int top)
116 API_FCT(rollback_get_node_actions);
117 API_FCT(rollback_revert_actions_by);