[CSM] Add method that display chat to client-sided lua. (#5089) (#5091)
authorred-001 <red-001@outlook.ie>
Sat, 21 Jan 2017 21:44:37 +0000 (21:44 +0000)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Mon, 13 Mar 2017 22:56:05 +0000 (23:56 +0100)
* squashed: [Client-sided scripting] Don't register functions that don't work. (#5091)

src/client.cpp
src/client.h
src/network/clientpackethandler.cpp
src/script/clientscripting.cpp
src/script/lua_api/l_base.cpp
src/script/lua_api/l_base.h
src/script/lua_api/l_client.cpp
src/script/lua_api/l_client.h
src/script/lua_api/l_util.cpp
src/script/lua_api/l_util.h

index faf454b359049ceffc87af7feba19bcf9ca588ed..0af6b4595e9b7d71555432072466705154dbfa8d 100644 (file)
@@ -1584,7 +1584,7 @@ void Client::typeChatMessage(const std::wstring &message)
        // Show locally
        if (message[0] == L'/')
        {
-               m_chat_queue.push((std::wstring)L"issued command: " + message);
+               pushToChatQueue((std::wstring)L"issued command: " + message);
        }
        else
        {
@@ -1593,7 +1593,7 @@ void Client::typeChatMessage(const std::wstring &message)
                        LocalPlayer *player = m_env.getLocalPlayer();
                        assert(player != NULL);
                        std::wstring name = narrow_to_wide(player->getName());
-                       m_chat_queue.push((std::wstring)L"<" + name + L"> " + message);
+                       pushToChatQueue((std::wstring)L"<" + name + L"> " + message);
                }
        }
 }
@@ -1867,7 +1867,7 @@ void Client::makeScreenshot(IrrlichtDevice *device)
                        } else {
                                sstr << "Failed to save screenshot '" << filename << "'";
                        }
-                       m_chat_queue.push(narrow_to_wide(sstr.str()));
+                       pushToChatQueue(narrow_to_wide(sstr.str()));
                        infostream << sstr.str() << std::endl;
                        image->drop();
                }
index 2fdade61a6c3317eac54a708892c3cea780abde1..584b13a90e1fc915d1a53399a30202c054e310c0 100644 (file)
@@ -557,6 +557,11 @@ public:
 
        void makeScreenshot(IrrlichtDevice *device);
 
+       inline void pushToChatQueue(const std::wstring &input)
+       {
+               m_chat_queue.push(input);
+       }
+
 private:
 
        // Virtual methods from con::PeerHandler
index f1c44c7d81ee925ab92bd689568d5c1f24ac7d51..d0642c86aa8fa94c863eb9b5dd7e0020355b77df 100644 (file)
@@ -142,7 +142,7 @@ void Client::handleCommand_AcceptSudoMode(NetworkPacket* pkt)
 }
 void Client::handleCommand_DenySudoMode(NetworkPacket* pkt)
 {
-       m_chat_queue.push(L"Password change denied. Password NOT changed.");
+       pushToChatQueue(L"Password change denied. Password NOT changed.");
        // reset everything and be sad
        deleteAuthData();
 }
@@ -414,7 +414,7 @@ void Client::handleCommand_ChatMessage(NetworkPacket* pkt)
 
        // If chat message not consummed by client lua API
        if (!m_script->on_receiving_message(wide_to_utf8(message))) {
-               m_chat_queue.push(message);
+               pushToChatQueue(message);
        }
 }
 
index 43bc6f94e4855aa089c820981572338fe3d054f3..9bf93eb83a2579f54647b342c79730e74a9c8042 100644 (file)
@@ -49,6 +49,6 @@ ClientScripting::ClientScripting(Client *client):
 
 void ClientScripting::InitializeModApi(lua_State *L, int top)
 {
-       ModApiUtil::Initialize(L, top);
+       ModApiUtil::InitializeClient(L, top);
        ModApiClient::Initialize(L, top);
 }
index 515a7d9332c1802369c9af29375d0ab96ada7a3f..f2703718a09fea3bbef804361bebff743aeaebfa 100644 (file)
@@ -37,6 +37,12 @@ Server *ModApiBase::getServer(lua_State *L)
        return getScriptApiBase(L)->getServer();
 }
 
+#ifndef SERVER
+Client *ModApiBase::getClient(lua_State *L)
+{
+       return getScriptApiBase(L)->getClient();
+}
+#endif
 Environment *ModApiBase::getEnv(lua_State *L)
 {
        return getScriptApiBase(L)->getEnv();
index 641013dfdcadf9ffe78e3f01d767e9ade2d625ff..dc1b1b2260c83991558970d7218d62abe2f3bee3 100644 (file)
@@ -28,6 +28,10 @@ extern "C" {
 #include <lauxlib.h>
 }
 
+#ifndef SERVER
+#include "client.h"
+#endif
+
 class ScriptApiBase;
 class Server;
 class Environment;
@@ -38,6 +42,10 @@ class ModApiBase {
 public:
        static ScriptApiBase*   getScriptApiBase(lua_State *L);
        static Server*          getServer(lua_State *L);
+       #ifndef SERVER
+       static Client*          getClient(lua_State *L);
+       #endif // !SERVER
+
        static Environment*     getEnv(lua_State *L);
        static GUIEngine*       getGuiEngine(lua_State *L);
        // When we are not loading the mod, this function returns "."
index 9c478602a57f36f1bd0f8a16b125c90ec13ec79b..f4c3812ac1ac51e1d0aec63939ae7c498349f48f 100644 (file)
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "l_client.h"
 #include "l_internal.h"
+#include "util/string.h"
 
 int ModApiClient::l_get_current_modname(lua_State *L)
 {
@@ -27,7 +28,18 @@ int ModApiClient::l_get_current_modname(lua_State *L)
        return 1;
 }
 
+// display_chat_message(message)
+int ModApiClient::l_display_chat_message(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+
+       std::string message = luaL_checkstring(L, 1);
+       getClient(L)->pushToChatQueue(utf8_to_wide(message));
+       return 1;
+}
+
 void ModApiClient::Initialize(lua_State *L, int top)
 {
        API_FCT(get_current_modname);
+       API_FCT(display_chat_message);
 }
index 332f001329d8dfeda0fee49615c11f3c685cbb2a..b4a57cb61ae09c88014742db6780884cd71b5dac 100644 (file)
@@ -28,6 +28,7 @@ class ModApiClient : public ModApiBase
 private:
        // get_current_modname()
        static int l_get_current_modname(lua_State *L);
+       static int l_display_chat_message(lua_State *L);
 
 public:
        static void Initialize(lua_State *L, int top);
index c267916465c39e137effde53689595f1b4cd123f..277a874bf5c7444bbc3ab78495ad86bdf2695c62 100644 (file)
@@ -526,6 +526,32 @@ void ModApiUtil::Initialize(lua_State *L, int top)
        API_FCT(get_version);
 }
 
+void ModApiUtil::InitializeClient(lua_State *L, int top)
+{
+       API_FCT(log);
+
+       API_FCT(setting_set);
+       API_FCT(setting_get);
+       API_FCT(setting_setbool);
+       API_FCT(setting_getbool);
+       API_FCT(setting_save);
+
+       API_FCT(parse_json);
+       API_FCT(write_json);
+
+       API_FCT(is_yes);
+
+       API_FCT(get_builtin_path);
+
+       API_FCT(compress);
+       API_FCT(decompress);
+
+       API_FCT(encode_base64);
+       API_FCT(decode_base64);
+
+       API_FCT(get_version);
+}
+
 void ModApiUtil::InitializeAsync(AsyncEngine& engine)
 {
        ASYNC_API_FCT(log);
index 9910704b37d96accfbe365326debb0cf1b745fb9..eef32c0a1171ac8fe4547b0e0533a58cce9eae53 100644 (file)
@@ -110,6 +110,8 @@ private:
 public:
        static void Initialize(lua_State *L, int top);
 
+       static void InitializeClient(lua_State *L, int top);
+
        static void InitializeAsync(AsyncEngine& engine);
 
 };