]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/lua_api/l_server.cpp
Performance improvement: Use std::list instead of std::vector for request_media,...
[dragonfireclient.git] / src / script / lua_api / l_server.cpp
index dc05ccbbe7f395c33f629df9a124ee7b12191be0..16331a933be2a999a15a1d89f26657b4b8b51d8e 100644 (file)
@@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "server.h"
 #include "environment.h"
 #include "player.h"
+#include "log.h"
 
 // request_shutdown()
 int ModApiServer::l_request_shutdown(lua_State *L)
@@ -52,21 +53,17 @@ int ModApiServer::l_chat_send_all(lua_State *L)
        return 0;
 }
 
-// chat_send_player(name, text, prepend)
+// chat_send_player(name, text)
 int ModApiServer::l_chat_send_player(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
        const char *name = luaL_checkstring(L, 1);
        const char *text = luaL_checkstring(L, 2);
-       bool prepend = true;
-
-       if (lua_isboolean(L, 3))
-               prepend = lua_toboolean(L, 3);
 
        // Get server from registry
        Server *server = getServer(L);
        // Send
-       server->notifyPlayer(name, narrow_to_wide(text), prepend);
+       server->notifyPlayer(name, narrow_to_wide(text));
        return 0;
 }
 
@@ -103,7 +100,7 @@ int ModApiServer::l_get_player_ip(lua_State *L)
        }
        try
        {
-               Address addr = getServer(L)->getPeerAddress(getEnv(L)->getPlayer(name)->peer_id);
+               Address addr = getServer(L)->getPeerAddress(player->peer_id);
                std::string ip_str = addr.serializeString();
                lua_pushstring(L, ip_str.c_str());
                return 1;
@@ -116,6 +113,135 @@ int ModApiServer::l_get_player_ip(lua_State *L)
        }
 }
 
+// get_player_information()
+int ModApiServer::l_get_player_information(lua_State *L)
+{
+
+       NO_MAP_LOCK_REQUIRED;
+       const char * name = luaL_checkstring(L, 1);
+       Player *player = getEnv(L)->getPlayer(name);
+       if(player == NULL)
+       {
+               lua_pushnil(L); // no such player
+               return 1;
+       }
+
+       Address addr;
+       try
+       {
+               addr = getServer(L)->getPeerAddress(player->peer_id);
+       }
+       catch(con::PeerNotFoundException) // unlikely
+       {
+               dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;
+               lua_pushnil(L); // error
+               return 1;
+       }
+
+       float min_rtt,max_rtt,avg_rtt,min_jitter,max_jitter,avg_jitter;
+       ClientState state;
+       u32 uptime;
+       u16 prot_vers;
+       u8 ser_vers,major,minor,patch;
+       std::string vers_string;
+
+#define ERET(code)                                                             \
+       if (!(code)) {                                                             \
+               dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;     \
+               lua_pushnil(L); /* error */                                            \
+               return 1;                                                              \
+       }
+
+       ERET(getServer(L)->getClientConInfo(player->peer_id,con::MIN_RTT,&min_rtt))
+       ERET(getServer(L)->getClientConInfo(player->peer_id,con::MAX_RTT,&max_rtt))
+       ERET(getServer(L)->getClientConInfo(player->peer_id,con::AVG_RTT,&avg_rtt))
+       ERET(getServer(L)->getClientConInfo(player->peer_id,con::MIN_JITTER,&min_jitter))
+       ERET(getServer(L)->getClientConInfo(player->peer_id,con::MAX_JITTER,&max_jitter))
+       ERET(getServer(L)->getClientConInfo(player->peer_id,con::AVG_JITTER,&avg_jitter))
+
+       ERET(getServer(L)->getClientInfo(player->peer_id,
+                                                                               &state, &uptime, &ser_vers, &prot_vers,
+                                                                               &major, &minor, &patch, &vers_string))
+
+       lua_newtable(L);
+       int table = lua_gettop(L);
+
+       lua_pushstring(L,"address");
+       lua_pushstring(L, addr.serializeString().c_str());
+       lua_settable(L, table);
+
+       lua_pushstring(L,"ip_version");
+       if (addr.getFamily() == AF_INET) {
+               lua_pushnumber(L, 4);
+       } else if (addr.getFamily() == AF_INET6) {
+               lua_pushnumber(L, 6);
+       } else {
+               lua_pushnumber(L, 0);
+       }
+       lua_settable(L, table);
+
+       lua_pushstring(L,"min_rtt");
+       lua_pushnumber(L, min_rtt);
+       lua_settable(L, table);
+
+       lua_pushstring(L,"max_rtt");
+       lua_pushnumber(L, max_rtt);
+       lua_settable(L, table);
+
+       lua_pushstring(L,"avg_rtt");
+       lua_pushnumber(L, avg_rtt);
+       lua_settable(L, table);
+
+       lua_pushstring(L,"min_jitter");
+       lua_pushnumber(L, min_jitter);
+       lua_settable(L, table);
+
+       lua_pushstring(L,"max_jitter");
+       lua_pushnumber(L, max_jitter);
+       lua_settable(L, table);
+
+       lua_pushstring(L,"avg_jitter");
+       lua_pushnumber(L, avg_jitter);
+       lua_settable(L, table);
+
+       lua_pushstring(L,"connection_uptime");
+       lua_pushnumber(L, uptime);
+       lua_settable(L, table);
+
+#ifndef NDEBUG
+       lua_pushstring(L,"serialization_version");
+       lua_pushnumber(L, ser_vers);
+       lua_settable(L, table);
+
+       lua_pushstring(L,"protocol_version");
+       lua_pushnumber(L, prot_vers);
+       lua_settable(L, table);
+
+       lua_pushstring(L,"major");
+       lua_pushnumber(L, major);
+       lua_settable(L, table);
+
+       lua_pushstring(L,"minor");
+       lua_pushnumber(L, minor);
+       lua_settable(L, table);
+
+       lua_pushstring(L,"patch");
+       lua_pushnumber(L, patch);
+       lua_settable(L, table);
+
+       lua_pushstring(L,"version_string");
+       lua_pushstring(L, vers_string.c_str());
+       lua_settable(L, table);
+
+       lua_pushstring(L,"state");
+       lua_pushstring(L,ClientInterface::state2Name(state).c_str());
+       lua_settable(L, table);
+#endif
+
+#undef ERET
+       return 1;
+}
+
 // get_ban_list()
 int ModApiServer::l_get_ban_list(lua_State *L)
 {
@@ -216,7 +342,7 @@ int ModApiServer::l_show_formspec(lua_State *L)
 int ModApiServer::l_get_current_modname(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
-       lua_getfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
+       lua_getfield(L, LUA_REGISTRYINDEX, "current_modname");
        return 1;
 }
 
@@ -225,14 +351,8 @@ int ModApiServer::l_get_modpath(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
        std::string modname = luaL_checkstring(L, 1);
-       // Do it
-       if(modname == "__builtin"){
-               std::string path = getServer(L)->getBuiltinLuaPath();
-               lua_pushstring(L, path.c_str());
-               return 1;
-       }
        const ModSpec *mod = getServer(L)->getModSpec(modname);
-       if(!mod){
+       if (!mod) {
                lua_pushnil(L);
                return 1;
        }
@@ -247,13 +367,14 @@ int ModApiServer::l_get_modnames(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
 
        // Get a list of mods
-       std::list<std::string> mods_unsorted, mods_sorted;
+       std::vector<std::string> mods_unsorted;
+       std::list<std::string> mods_sorted;
        getServer(L)->getModNames(mods_unsorted);
 
        // Take unsorted items from mods_unsorted and sort them into
        // mods_sorted; not great performance but the number of mods on a
        // server will likely be small.
-       for(std::list<std::string>::iterator i = mods_unsorted.begin();
+       for(std::vector<std::string>::iterator i = mods_unsorted.begin();
                        i != mods_unsorted.end(); ++i) {
                bool added = false;
                for(std::list<std::string>::iterator x = mods_sorted.begin();
@@ -330,6 +451,36 @@ int ModApiServer::l_notify_authentication_modified(lua_State *L)
        return 0;
 }
 
+#ifndef NDEBUG
+// cause_error(type_of_error)
+int ModApiServer::l_cause_error(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       std::string type_of_error = "none";
+       if(lua_isstring(L, 1))
+               type_of_error = lua_tostring(L, 1);
+
+       errorstream << "Error handler test called, errortype=" << type_of_error << std::endl;
+
+       if(type_of_error == "segv") {
+               volatile int* some_pointer = 0;
+               errorstream << "Cause a sigsegv now: " << (*some_pointer) << std::endl;
+
+       } else if (type_of_error == "zerodivision") {
+
+               unsigned int some_number = porting::getTimeS();
+               unsigned int zerovalue = 0;
+               unsigned int result = some_number / zerovalue;
+               errorstream << "Well this shouldn't ever be shown: " << result << std::endl;
+
+       } else if (type_of_error == "exception") {
+               throw BaseException("Errorhandler test fct called");
+       }
+
+       return 0;
+}
+#endif
+
 void ModApiServer::Initialize(lua_State *L, int top)
 {
        API_FCT(request_shutdown);
@@ -347,6 +498,7 @@ void ModApiServer::Initialize(lua_State *L, int top)
        API_FCT(sound_play);
        API_FCT(sound_stop);
 
+       API_FCT(get_player_information);
        API_FCT(get_player_privs);
        API_FCT(get_player_ip);
        API_FCT(get_ban_list);
@@ -355,4 +507,8 @@ void ModApiServer::Initialize(lua_State *L, int top)
        API_FCT(kick_player);
        API_FCT(unban_player_or_ip);
        API_FCT(notify_authentication_modified);
+
+#ifndef NDEBUG
+       API_FCT(cause_error);
+#endif
 }