]> git.lizzy.rs Git - minetest.git/blobdiff - src/script/lua_api/l_server.cpp
Add minetest.get_player_window_information() (#12367)
[minetest.git] / src / script / lua_api / l_server.cpp
index 9866e0bc860581135338b518eb6db0710e653840..eff2126536bb2d952e1250c513d736879575b911 100644 (file)
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "lua_api/l_internal.h"
 #include "common/c_converter.h"
 #include "common/c_content.h"
+#include "common/c_packer.h"
 #include "cpp_api/s_base.h"
 #include "cpp_api/s_security.h"
 #include "scripting_server.h"
@@ -57,6 +58,14 @@ 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;
+       GET_ENV_PTR;
+       lua_pushnumber(L, env->getMaxLagEstimate());
+       return 1;
+}
 
 // print(text)
 int ModApiServer::l_print(lua_State *L)
@@ -260,6 +269,44 @@ int ModApiServer::l_get_player_information(lua_State *L)
        return 1;
 }
 
+// get_player_window_information(name)
+int ModApiServer::l_get_player_window_information(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+
+       Server *server = getServer(L);
+
+       const char *name = luaL_checkstring(L, 1);
+       RemotePlayer *player = server->getEnv().getPlayer(name);
+       if (!player)
+               return 0;
+
+       auto dynamic = server->getClientDynamicInfo(player->getPeerId());
+
+       if (!dynamic || dynamic->render_target_size == v2u32())
+               return 0;
+
+       lua_newtable(L);
+       int dyn_table = lua_gettop(L);
+
+       lua_pushstring(L, "size");
+       push_v2u32(L, dynamic->render_target_size);
+       lua_settable(L, dyn_table);
+
+       lua_pushstring(L, "max_formspec_size");
+       push_v2f(L, dynamic->max_fs_size);
+       lua_settable(L, dyn_table);
+
+       lua_pushstring(L, "real_gui_scaling");
+       lua_pushnumber(L, dynamic->real_gui_scaling);
+       lua_settable(L, dyn_table);
+
+       lua_pushstring(L, "real_hud_scaling");
+       lua_pushnumber(L, dynamic->real_hud_scaling);
+       lua_settable(L, dyn_table);
+       return 1;
+}
+
 // get_ban_list()
 int ModApiServer::l_get_ban_list(lua_State *L)
 {
@@ -282,8 +329,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) {
@@ -297,23 +346,30 @@ 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) {
+       Server *server = getServer(L);
+
+       RemotePlayer *player = server->getEnv().getPlayer(name);
+       if (!player) {
                lua_pushboolean(L, false); // No such player
                return 1;
        }
-       getServer(L)->DenyAccess_Legacy(player->getPeerId(), utf8_to_wide(message));
+
+       server->DenyAccess(player->getPeerId(), SERVER_ACCESSDENIED_CUSTOM_STRING, message);
        lua_pushboolean(L, true);
        return 1;
 }
@@ -323,7 +379,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)
@@ -374,12 +431,11 @@ int ModApiServer::l_get_modpath(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
        std::string modname = luaL_checkstring(L, 1);
-       const ModSpec *mod = getServer(L)->getModSpec(modname);
-       if (!mod) {
+       const ModSpec *mod = getGameDef(L)->getModSpec(modname);
+       if (!mod)
                lua_pushnil(L);
-               return 1;
-       }
-       lua_pushstring(L, mod->path.c_str());
+       else
+               lua_pushstring(L, mod->path.c_str());
        return 1;
 }
 
@@ -391,13 +447,14 @@ int ModApiServer::l_get_modnames(lua_State *L)
 
        // Get a list of mods
        std::vector<std::string> modlist;
-       getServer(L)->getModNames(modlist);
+       for (auto &it : getGameDef(L)->getMods())
+               modlist.emplace_back(it.name);
 
        std::sort(modlist.begin(), modlist.end());
 
        // Package them up for Lua
        lua_createtable(L, modlist.size(), 0);
-       std::vector<std::string>::iterator iter = modlist.begin();
+       auto iter = modlist.begin();
        for (u16 i = 0; iter != modlist.end(); ++iter) {
                lua_pushstring(L, iter->c_str());
                lua_rawseti(L, -2, ++i);
@@ -405,12 +462,26 @@ int ModApiServer::l_get_modnames(lua_State *L)
        return 1;
 }
 
+// get_game_info()
+int ModApiServer::l_get_game_info(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       const SubgameSpec *game_spec = getGameDef(L)->getGameSpec();
+       assert(game_spec);
+       lua_newtable(L);
+       setstringfield(L, -1, "id", game_spec->id);
+       setstringfield(L, -1, "title", game_spec->title);
+       setstringfield(L, -1, "author", game_spec->author);
+       setstringfield(L, -1, "path", game_spec->path);
+       return 1;
+}
+
 // get_worldpath()
 int ModApiServer::l_get_worldpath(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
-       std::string worldpath = getServer(L)->getWorldPath();
-       lua_pushstring(L, worldpath.c_str());
+       const Server *srv = getServer(L);
+       lua_pushstring(L, srv->getWorldPath().c_str());
        return 1;
 }
 
@@ -418,16 +489,15 @@ int ModApiServer::l_get_worldpath(lua_State *L)
 int ModApiServer::l_sound_play(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
-       SimpleSoundSpec spec;
-       read_soundspec(L, 1, spec);
-       ServerSoundParams params;
+       ServerPlayingSound params;
+       read_soundspec(L, 1, params.spec);
        read_server_sound_params(L, 2, params);
        bool ephemeral = lua_gettop(L) > 2 && readParam<bool>(L, 3);
        if (ephemeral) {
-               getServer(L)->playSound(spec, params, true);
+               getServer(L)->playSound(params, true);
                lua_pushnil(L);
        } else {
-               s32 handle = getServer(L)->playSound(spec, params);
+               s32 handle = getServer(L)->playSound(params);
                lua_pushinteger(L, handle);
        }
        return 1;
@@ -453,29 +523,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;
 }
@@ -484,7 +562,8 @@ int ModApiServer::l_dynamic_add_media_raw(lua_State *L)
 int ModApiServer::l_is_singleplayer(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
-       lua_pushboolean(L, getServer(L)->isSingleplayer());
+       const Server *srv = getServer(L);
+       lua_pushboolean(L, srv->isSingleplayer());
        return 1;
 }
 
@@ -499,17 +578,89 @@ int ModApiServer::l_notify_authentication_modified(lua_State *L)
        return 0;
 }
 
+// do_async_callback(func, params, mod_origin)
+int ModApiServer::l_do_async_callback(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       ServerScripting *script = getScriptApi<ServerScripting>(L);
+
+       luaL_checktype(L, 1, LUA_TFUNCTION);
+       luaL_checktype(L, 2, LUA_TTABLE);
+       luaL_checktype(L, 3, LUA_TSTRING);
+
+       call_string_dump(L, 1);
+       size_t func_length;
+       const char *serialized_func_raw = lua_tolstring(L, -1, &func_length);
+
+       PackedValue *param = script_pack(L, 2);
+
+       std::string mod_origin = readParam<std::string>(L, 3);
+
+       u32 jobId = script->queueAsync(
+               std::string(serialized_func_raw, func_length),
+               param, mod_origin);
+
+       lua_settop(L, 0);
+       lua_pushinteger(L, jobId);
+       return 1;
+}
+
+// register_async_dofile(path)
+int ModApiServer::l_register_async_dofile(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+
+       std::string path = readParam<std::string>(L, 1);
+       CHECK_SECURE_PATH(L, path.c_str(), false);
+
+       // Find currently running mod name (only at init time)
+       lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
+       if (!lua_isstring(L, -1))
+               return 0;
+       std::string modname = readParam<std::string>(L, -1);
+
+       getServer(L)->m_async_init_files.emplace_back(modname, path);
+       lua_pushboolean(L, true);
+       return 1;
+}
+
+// serialize_roundtrip(value)
+// Meant for unit testing the packer from Lua
+int ModApiServer::l_serialize_roundtrip(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+
+       int top = lua_gettop(L);
+       auto *pv = script_pack(L, 1);
+       if (top != lua_gettop(L))
+               throw LuaError("stack values leaked");
+
+#ifndef NDEBUG
+       script_dump_packed(pv);
+#endif
+
+       top = lua_gettop(L);
+       script_unpack(L, pv);
+       delete pv;
+       if (top + 1 != lua_gettop(L))
+               throw LuaError("stack values leaked");
+
+       return 1;
+}
+
 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);
 
        API_FCT(get_current_modname);
        API_FCT(get_modpath);
        API_FCT(get_modnames);
+       API_FCT(get_game_info);
 
        API_FCT(print);
 
@@ -519,16 +670,32 @@ 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_window_information);
        API_FCT(get_player_privs);
        API_FCT(get_player_ip);
        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(do_async_callback);
+       API_FCT(register_async_dofile);
+       API_FCT(serialize_roundtrip);
+}
+
+void ModApiServer::InitializeAsync(lua_State *L, int top)
+{
+       API_FCT(get_worldpath);
+       API_FCT(is_singleplayer);
+
+       API_FCT(get_current_modname);
+       API_FCT(get_modpath);
+       API_FCT(get_modnames);
+       API_FCT(get_game_info);
 }