[CSM] Add function and chat command to disconnect from server. (#5487)
authorred-001 <red-001@outlook.ie>
Sat, 1 Apr 2017 11:40:56 +0000 (12:40 +0100)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Sat, 1 Apr 2017 11:40:56 +0000 (13:40 +0200)
clientmods/preview/init.lua
doc/client_lua_api.md
src/client.cpp
src/client.h
src/script/lua_api/l_client.cpp
src/script/lua_api/l_client.h

index c57a62155cca64d0a8af4092e91d6e9e3a418e47..07464e9271f5b282846fa263810c523107e0f3c1 100644 (file)
@@ -127,3 +127,10 @@ core.register_chatcommand("list_players", {
                core.display_chat_message(dump(core.get_player_names()))
        end
 })
+
+core.register_chatcommand("disconnect", {
+       description = "Exit to main menu",
+       func = function(param)
+               core.disconnect()
+       end,
+})
index d5ccef5d309628879bd198804b2084afea39c406..deb5bfb13536bb15772c564c0b92f3f90157038b 100644 (file)
@@ -698,6 +698,9 @@ Call these functions only at load time!
 ### Client Environment
 * `minetest.get_player_names()`
     * Returns list of player names on server
+* `minetest.disconnect()`
+    * Disconnect from the server and exit to main menu.
+    * Returns `false` if the client is already disconnecting otherwise returns `true`.
 
 ### Misc.
 * `minetest.parse_json(string[, nullvalue])`: returns something
index 8bbaa83bd51bc021d3d5c0b3148a7008e16e5f8a..e710624d5e6fbbc632083e685f10c1ed029c1050 100644 (file)
@@ -260,7 +260,8 @@ Client::Client(
        m_localdb(NULL),
        m_script(NULL),
        m_mod_storage_save_timer(10.0f),
-       m_game_ui_flags(game_ui_flags)
+       m_game_ui_flags(game_ui_flags),
+       m_shutdown(false)
 {
        // Add local player
        m_env.setLocalPlayer(new LocalPlayer(this, playername));
@@ -346,6 +347,7 @@ const ModSpec* Client::getModSpec(const std::string &modname) const
 
 void Client::Stop()
 {
+       m_shutdown = true;
        // Don't disable this part when modding is disabled, it's used in builtin
        m_script->on_shutdown();
        //request all client managed threads to stop
@@ -361,14 +363,12 @@ void Client::Stop()
 
 bool Client::isShutdown()
 {
-
-       if (!m_mesh_update_thread.isRunning()) return true;
-
-       return false;
+       return m_shutdown || !m_mesh_update_thread.isRunning();
 }
 
 Client::~Client()
 {
+       m_shutdown = true;
        m_con.Disconnect();
 
        m_mesh_update_thread.stop();
index 84adf81d8b262f84250cb92f3e7bbb0c57d43ec9..e565acd939bba12b5472247b7a9c4a68c30551de 100644 (file)
@@ -737,6 +737,7 @@ private:
        float m_mod_storage_save_timer;
        GameUIFlags *m_game_ui_flags;
 
+       bool m_shutdown;
        DISABLE_CLASS_COPY(Client);
 };
 
index 7cb89188ddbde73e2610e8185b9306034b6b7b3e..5f94747029dea11d6c4d827adab118dde05e3753 100644 (file)
@@ -25,8 +25,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "gettext.h"
 #include "l_internal.h"
 #include "lua_api/l_item.h"
+#include "mainmenumanager.h"
 #include "util/string.h"
 
+extern MainGameCallback *g_gamecallback;
+
 int ModApiClient::l_get_current_modname(lua_State *L)
 {
        lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
@@ -107,6 +110,20 @@ int ModApiClient::l_send_respawn(lua_State *L)
        return 0;
 }
 
+// disconnect()
+int ModApiClient::l_disconnect(lua_State *L)
+{
+       // Stops badly written Lua code form causing boot loops
+       if (getClient(L)->isShutdown()) {
+               lua_pushboolean(L, false);
+               return 1;
+       }
+
+       g_gamecallback->disconnect();
+       lua_pushboolean(L, true);
+       return 1;
+}
+
 // gettext(text)
 int ModApiClient::l_gettext(lua_State *L)
 {
@@ -178,4 +195,5 @@ void ModApiClient::Initialize(lua_State *L, int top)
        API_FCT(get_node);
        API_FCT(get_node_or_nil);
        API_FCT(get_wielded_item);
+       API_FCT(disconnect);
 }
index b79cc670d3d973573a3127c123098dc038d1ce72..d7f92ac1c1b5b9dc0847728745a01d3fffcd0a08 100644 (file)
@@ -41,6 +41,9 @@ private:
        // send_respawn()
        static int l_send_respawn(lua_State *L);
 
+       // disconnect()
+       static int l_disconnect(lua_State *L);
+
        // gettext(text)
        static int l_gettext(lua_State *L);