]> git.lizzy.rs Git - minetest.git/blobdiff - src/script/cpp_api/s_server.cpp
Overall improvements to log messages (#9598)
[minetest.git] / src / script / cpp_api / s_server.cpp
index d41805b7b166b138227433bfb9f8607699f29092..1ce2f9d45d029fa8082364d966ba5544934971f6 100644 (file)
@@ -27,31 +27,32 @@ bool ScriptApiServer::getAuth(const std::string &playername,
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
        getAuthHandler();
        lua_getfield(L, -1, "get_auth");
-       if(lua_type(L, -1) != LUA_TFUNCTION)
-               throw LuaError(L, "Authentication handler missing get_auth");
+       if (lua_type(L, -1) != LUA_TFUNCTION)
+               throw LuaError("Authentication handler missing get_auth");
        lua_pushstring(L, playername.c_str());
-       if(lua_pcall(L, 1, 1, 0))
-               scriptError("error: %s", lua_tostring(L, -1));
+       PCALL_RES(lua_pcall(L, 1, 1, error_handler));
+       lua_remove(L, -2); // Remove auth handler
+       lua_remove(L, error_handler);
 
        // nil = login not allowed
-       if(lua_isnil(L, -1))
+       if (lua_isnil(L, -1))
                return false;
        luaL_checktype(L, -1, LUA_TTABLE);
 
        std::string password;
        bool found = getstringfield(L, -1, "password", password);
-       if(!found)
-               throw LuaError(L, "Authentication handler didn't return password");
-       if(dst_password)
+       if (!found)
+               throw LuaError("Authentication handler didn't return password");
+       if (dst_password)
                *dst_password = password;
 
        lua_getfield(L, -1, "privileges");
-       if(!lua_istable(L, -1))
-               throw LuaError(L,
-                               "Authentication handler didn't return privilege table");
-       if(dst_privs)
+       if (!lua_istable(L, -1))
+               throw LuaError("Authentication handler didn't return privilege table");
+       if (dst_privs)
                readPrivileges(-1, *dst_privs);
        lua_pop(L, 1);
 
@@ -62,14 +63,18 @@ void ScriptApiServer::getAuthHandler()
 {
        lua_State *L = getStack();
 
-       lua_getglobal(L, "minetest");
+       lua_getglobal(L, "core");
        lua_getfield(L, -1, "registered_auth_handler");
-       if(lua_isnil(L, -1)){
+       if (lua_isnil(L, -1)){
                lua_pop(L, 1);
                lua_getfield(L, -1, "builtin_auth_handler");
        }
-       if(lua_type(L, -1) != LUA_TTABLE)
-               throw LuaError(L, "Authentication handler table not valid");
+
+       setOriginFromTable(-1);
+
+       lua_remove(L, -2); // Remove core
+       if (lua_type(L, -1) != LUA_TTABLE)
+               throw LuaError("Authentication handler table not valid");
 }
 
 void ScriptApiServer::readPrivileges(int index, std::set<std::string> &result)
@@ -78,13 +83,13 @@ void ScriptApiServer::readPrivileges(int index, std::set<std::string> &result)
 
        result.clear();
        lua_pushnil(L);
-       if(index < 0)
+       if (index < 0)
                index -= 1;
-       while(lua_next(L, index) != 0){
+       while (lua_next(L, index) != 0) {
                // key at index -2 and value at index -1
                std::string key = luaL_checkstring(L, -2);
-               bool value = lua_toboolean(L, -1);
-               if(value)
+               bool value = readParam<bool>(L, -1);
+               if (value)
                        result.insert(key);
                // removes value, keeps key for next iteration
                lua_pop(L, 1);
@@ -96,14 +101,16 @@ void ScriptApiServer::createAuth(const std::string &playername,
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
        getAuthHandler();
        lua_getfield(L, -1, "create_auth");
-       if(lua_type(L, -1) != LUA_TFUNCTION)
-               throw LuaError(L, "Authentication handler missing create_auth");
+       lua_remove(L, -2); // Remove auth handler
+       if (lua_type(L, -1) != LUA_TFUNCTION)
+               throw LuaError("Authentication handler missing create_auth");
        lua_pushstring(L, playername.c_str());
        lua_pushstring(L, password.c_str());
-       if(lua_pcall(L, 2, 0, 0))
-               scriptError("error: %s", lua_tostring(L, -1));
+       PCALL_RES(lua_pcall(L, 2, 0, error_handler));
+       lua_pop(L, 1); // Pop error handler
 }
 
 bool ScriptApiServer::setPassword(const std::string &playername,
@@ -111,14 +118,16 @@ bool ScriptApiServer::setPassword(const std::string &playername,
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
        getAuthHandler();
        lua_getfield(L, -1, "set_password");
-       if(lua_type(L, -1) != LUA_TFUNCTION)
-               throw LuaError(L, "Authentication handler missing set_password");
+       lua_remove(L, -2); // Remove auth handler
+       if (lua_type(L, -1) != LUA_TFUNCTION)
+               throw LuaError("Authentication handler missing set_password");
        lua_pushstring(L, playername.c_str());
        lua_pushstring(L, password.c_str());
-       if(lua_pcall(L, 2, 1, 0))
-               scriptError("error: %s", lua_tostring(L, -1));
+       PCALL_RES(lua_pcall(L, 2, 1, error_handler));
+       lua_remove(L, error_handler);
        return lua_toboolean(L, -1);
 }
 
@@ -127,15 +136,25 @@ bool ScriptApiServer::on_chat_message(const std::string &name,
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       // Get minetest.registered_on_chat_messages
-       lua_getglobal(L, "minetest");
+       // Get core.registered_on_chat_messages
+       lua_getglobal(L, "core");
        lua_getfield(L, -1, "registered_on_chat_messages");
        // Call callbacks
        lua_pushstring(L, name.c_str());
        lua_pushstring(L, message.c_str());
-       script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR_SC);
-       bool ate = lua_toboolean(L, -1);
-       return ate;
+       runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
+       return readParam<bool>(L, -1);
+}
+
+void ScriptApiServer::on_mods_loaded()
+{
+       SCRIPTAPI_PRECHECKHEADER
+
+       // Get registered shutdown hooks
+       lua_getglobal(L, "core");
+       lua_getfield(L, -1, "registered_on_mods_loaded");
+       // Call callbacks
+       runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
 }
 
 void ScriptApiServer::on_shutdown()
@@ -143,9 +162,31 @@ void ScriptApiServer::on_shutdown()
        SCRIPTAPI_PRECHECKHEADER
 
        // Get registered shutdown hooks
-       lua_getglobal(L, "minetest");
+       lua_getglobal(L, "core");
        lua_getfield(L, -1, "registered_on_shutdown");
        // Call callbacks
-       script_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST);
+       runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
 }
 
+std::string ScriptApiServer::formatChatMessage(const std::string &name,
+       const std::string &message)
+{
+       SCRIPTAPI_PRECHECKHEADER
+
+       // Push function onto stack
+       lua_getglobal(L, "core");
+       lua_getfield(L, -1, "format_chat_message");
+
+       // Push arguments onto stack
+       lua_pushstring(L, name.c_str());
+       lua_pushstring(L, message.c_str());
+
+       // Actually call the function
+       lua_call(L, 2, 1);
+
+       // Fetch return value
+       std::string ret = lua_tostring(L, -1);
+       lua_pop(L, 1);
+
+       return ret;
+}