aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api/l_server.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/script/lua_api/l_server.cpp')
-rw-r--r--src/script/lua_api/l_server.cpp102
1 files changed, 51 insertions, 51 deletions
diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp
index bf5292521..88ab5e16b 100644
--- a/src/script/lua_api/l_server.cpp
+++ b/src/script/lua_api/l_server.cpp
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common/c_content.h"
#include "cpp_api/s_base.h"
#include "cpp_api/s_security.h"
+#include "scripting_server.h"
#include "server.h"
#include "environment.h"
#include "remoteplayer.h"
@@ -56,6 +57,17 @@ int ModApiServer::l_get_server_uptime(lua_State *L)
return 1;
}
+// get_server_max_lag()
+int ModApiServer::l_get_server_max_lag(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ ServerEnvironment *s_env = dynamic_cast<ServerEnvironment *>(getEnv(L));
+ if (!s_env)
+ lua_pushnil(L);
+ else
+ lua_pushnumber(L, s_env->getMaxLagEstimate());
+ return 1;
+}
// print(text)
int ModApiServer::l_print(lua_State *L)
@@ -281,8 +293,10 @@ int ModApiServer::l_ban_player(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
- Server *server = getServer(L);
+ if (!getEnv(L))
+ throw LuaError("Can't ban player before server has started up");
+ Server *server = getServer(L);
const char *name = luaL_checkstring(L, 1);
RemotePlayer *player = server->getEnv().getPlayer(name);
if (!player) {
@@ -296,16 +310,20 @@ int ModApiServer::l_ban_player(lua_State *L)
return 1;
}
-// kick_player(name, [reason]) -> success
-int ModApiServer::l_kick_player(lua_State *L)
+// disconnect_player(name, [reason]) -> success
+int ModApiServer::l_disconnect_player(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
+
+ if (!getEnv(L))
+ throw LuaError("Can't kick player before server has started up");
+
const char *name = luaL_checkstring(L, 1);
- std::string message("Kicked");
+ std::string message;
if (lua_isstring(L, 2))
- message.append(": ").append(readParam<std::string>(L, 2));
+ message.append(readParam<std::string>(L, 2));
else
- message.append(".");
+ message.append("Disconnected.");
RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
if (player == NULL) {
@@ -322,7 +340,8 @@ int ModApiServer::l_remove_player(lua_State *L)
NO_MAP_LOCK_REQUIRED;
std::string name = luaL_checkstring(L, 1);
ServerEnvironment *s_env = dynamic_cast<ServerEnvironment *>(getEnv(L));
- assert(s_env);
+ if (!s_env)
+ throw LuaError("Can't remove player before server has started up");
RemotePlayer *player = s_env->getPlayer(name.c_str());
if (!player)
@@ -452,29 +471,37 @@ int ModApiServer::l_sound_fade(lua_State *L)
}
// dynamic_add_media(filepath)
-int ModApiServer::l_dynamic_add_media_raw(lua_State *L)
+int ModApiServer::l_dynamic_add_media(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
if (!getEnv(L))
throw LuaError("Dynamic media cannot be added before server has started up");
+ Server *server = getServer(L);
- std::string filepath = readParam<std::string>(L, 1);
- CHECK_SECURE_PATH(L, filepath.c_str(), false);
+ std::string filepath;
+ std::string to_player;
+ bool ephemeral = false;
- std::vector<RemotePlayer*> sent_to;
- bool ok = getServer(L)->dynamicAddMedia(filepath, sent_to);
- if (ok) {
- // (see wrapper code in builtin)
- lua_createtable(L, sent_to.size(), 0);
- int i = 0;
- for (RemotePlayer *player : sent_to) {
- lua_pushstring(L, player->getName());
- lua_rawseti(L, -2, ++i);
- }
+ if (lua_istable(L, 1)) {
+ getstringfield(L, 1, "filepath", filepath);
+ getstringfield(L, 1, "to_player", to_player);
+ getboolfield(L, 1, "ephemeral", ephemeral);
} else {
- lua_pushboolean(L, false);
+ filepath = readParam<std::string>(L, 1);
}
+ if (filepath.empty())
+ luaL_typerror(L, 1, "non-empty string");
+ luaL_checktype(L, 2, LUA_TFUNCTION);
+
+ CHECK_SECURE_PATH(L, filepath.c_str(), false);
+
+ u32 token = server->getScriptIface()->allocateDynamicMediaCallback(L, 2);
+
+ bool ok = server->dynamicAddMedia(filepath, token, to_player, ephemeral);
+ if (!ok)
+ server->getScriptIface()->freeDynamicMediaCallback(token);
+ lua_pushboolean(L, ok);
return 1;
}
@@ -498,36 +525,12 @@ int ModApiServer::l_notify_authentication_modified(lua_State *L)
return 0;
}
-// get_last_run_mod()
-int ModApiServer::l_get_last_run_mod(lua_State *L)
-{
- NO_MAP_LOCK_REQUIRED;
- lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
- std::string current_mod = readParam<std::string>(L, -1, "");
- if (current_mod.empty()) {
- lua_pop(L, 1);
- lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
- }
- return 1;
-}
-
-// set_last_run_mod(modname)
-int ModApiServer::l_set_last_run_mod(lua_State *L)
-{
- NO_MAP_LOCK_REQUIRED;
-#ifdef SCRIPTAPI_DEBUG
- const char *mod = lua_tostring(L, 1);
- getScriptApiBase(L)->setOriginDirect(mod);
- //printf(">>>> last mod set from Lua: %s\n", mod);
-#endif
- return 0;
-}
-
void ModApiServer::Initialize(lua_State *L, int top)
{
API_FCT(request_shutdown);
API_FCT(get_server_status);
API_FCT(get_server_uptime);
+ API_FCT(get_server_max_lag);
API_FCT(get_worldpath);
API_FCT(is_singleplayer);
@@ -543,7 +546,7 @@ void ModApiServer::Initialize(lua_State *L, int top)
API_FCT(sound_play);
API_FCT(sound_stop);
API_FCT(sound_fade);
- API_FCT(dynamic_add_media_raw);
+ API_FCT(dynamic_add_media);
API_FCT(get_player_information);
API_FCT(get_player_privs);
@@ -551,11 +554,8 @@ void ModApiServer::Initialize(lua_State *L, int top)
API_FCT(get_ban_list);
API_FCT(get_ban_description);
API_FCT(ban_player);
- API_FCT(kick_player);
+ API_FCT(disconnect_player);
API_FCT(remove_player);
API_FCT(unban_player_or_ip);
API_FCT(notify_authentication_modified);
-
- API_FCT(get_last_run_mod);
- API_FCT(set_last_run_mod);
}