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"
27 // rollback_get_last_node_actor(p, range, seconds) -> actor, p, seconds
28 int ModApiRollback::l_rollback_get_last_node_actor(lua_State *L)
30 v3s16 p = read_v3s16(L, 1);
31 int range = luaL_checknumber(L, 2);
32 int seconds = luaL_checknumber(L, 3);
33 Server *server = getServer(L);
34 IRollbackManager *rollback = server->getRollbackManager();
37 std::string actor = rollback->getLastNodeActor(p, range, seconds, &act_p, &act_seconds);
38 lua_pushstring(L, actor.c_str());
40 lua_pushnumber(L, act_seconds);
44 // rollback_revert_actions_by(actor, seconds) -> bool, log messages
45 int ModApiRollback::l_rollback_revert_actions_by(lua_State *L)
47 std::string actor = luaL_checkstring(L, 1);
48 int seconds = luaL_checknumber(L, 2);
49 Server *server = getServer(L);
50 IRollbackManager *rollback = server->getRollbackManager();
51 std::list<RollbackAction> actions = rollback->getRevertActions(actor, seconds);
52 std::list<std::string> log;
53 bool success = server->rollbackRevertActions(actions, &log);
54 // Push boolean result
55 lua_pushboolean(L, success);
56 // Get the table insert function and push the log table
57 lua_getglobal(L, "table");
58 lua_getfield(L, -1, "insert");
59 int table_insert = lua_gettop(L);
61 int table = lua_gettop(L);
62 for(std::list<std::string>::const_iterator i = log.begin();
65 lua_pushvalue(L, table_insert);
66 lua_pushvalue(L, table);
67 lua_pushstring(L, i->c_str());
68 if(lua_pcall(L, 2, 0, 0))
69 script_error(L, "error: %s", lua_tostring(L, -1));
71 lua_remove(L, -2); // Remove table
72 lua_remove(L, -2); // Remove insert
76 void ModApiRollback::Initialize(lua_State *L, int top)
78 API_FCT(rollback_get_last_node_actor);
79 API_FCT(rollback_revert_actions_by);