X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fscript%2Fcpp_api%2Fs_server.cpp;h=1ce2f9d45d029fa8082364d966ba5544934971f6;hb=de73f989eb1397b1103236031fd91309b294583c;hp=21fe164aac0f15268f710b417a9856c7d53e3883;hpb=c4359ff65cd8e4e754442b9f2ef7051a8eaa4241;p=minetest.git diff --git a/src/script/cpp_api/s_server.cpp b/src/script/cpp_api/s_server.cpp index 21fe164aa..1ce2f9d45 100644 --- a/src/script/cpp_api/s_server.cpp +++ b/src/script/cpp_api/s_server.cpp @@ -27,14 +27,15 @@ 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("Authentication handler missing get_auth"); lua_pushstring(L, playername.c_str()); - if (lua_pcall(L, 1, 1, m_errorhandler)) - scriptError(); + 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)) @@ -68,6 +69,9 @@ void ScriptApiServer::getAuthHandler() lua_pop(L, 1); lua_getfield(L, -1, "builtin_auth_handler"); } + + setOriginFromTable(-1); + lua_remove(L, -2); // Remove core if (lua_type(L, -1) != LUA_TTABLE) throw LuaError("Authentication handler table not valid"); @@ -84,7 +88,7 @@ void ScriptApiServer::readPrivileges(int index, std::set &result) 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); + bool value = readParam(L, -1); if (value) result.insert(key); // removes value, keeps key for next iteration @@ -97,6 +101,7 @@ void ScriptApiServer::createAuth(const std::string &playername, { SCRIPTAPI_PRECHECKHEADER + int error_handler = PUSH_ERROR_HANDLER(L); getAuthHandler(); lua_getfield(L, -1, "create_auth"); lua_remove(L, -2); // Remove auth handler @@ -104,8 +109,8 @@ void ScriptApiServer::createAuth(const std::string &playername, 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, m_errorhandler)) - scriptError(); + PCALL_RES(lua_pcall(L, 2, 0, error_handler)); + lua_pop(L, 1); // Pop error handler } bool ScriptApiServer::setPassword(const std::string &playername, @@ -113,6 +118,7 @@ bool ScriptApiServer::setPassword(const std::string &playername, { SCRIPTAPI_PRECHECKHEADER + int error_handler = PUSH_ERROR_HANDLER(L); getAuthHandler(); lua_getfield(L, -1, "set_password"); lua_remove(L, -2); // Remove auth handler @@ -120,8 +126,8 @@ bool ScriptApiServer::setPassword(const std::string &playername, 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, m_errorhandler)) - scriptError(); + PCALL_RES(lua_pcall(L, 2, 1, error_handler)); + lua_remove(L, error_handler); return lua_toboolean(L, -1); } @@ -136,9 +142,19 @@ bool ScriptApiServer::on_chat_message(const std::string &name, // 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(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() @@ -149,6 +165,28 @@ void ScriptApiServer::on_shutdown() 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; +}