]> git.lizzy.rs Git - minetest.git/blobdiff - src/network/clientpackethandler.cpp
Network cleanup (#6310)
[minetest.git] / src / network / clientpackethandler.cpp
index 9eb6d8dca2747bdae4ab7bdcaec65cf082285b67..f42f9219b78b6d8bf3249e8ca23492c25fd5992d 100644 (file)
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "client.h"
 
 #include "util/base64.h"
+#include "chatmessage.h"
 #include "clientmedia.h"
 #include "log.h"
 #include "map.h"
@@ -30,10 +31,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "server.h"
 #include "util/strfnd.h"
 #include "network/clientopcodes.h"
+#include "network/connection.h"
 #include "script/scripting_client.h"
 #include "util/serialize.h"
 #include "util/srp.h"
 #include "tileanimation.h"
+#include "gettext.h"
 
 void Client::handleCommand_Deprecated(NetworkPacket* pkt)
 {
@@ -96,7 +99,7 @@ void Client::handleCommand_Hello(NetworkPacket* pkt)
                m_chosen_auth_mech = AUTH_MECHANISM_NONE;
                m_access_denied = true;
                m_access_denied_reason = "Unknown";
-               m_con.Disconnect();
+               m_con->Disconnect();
        }
 
 }
@@ -121,7 +124,12 @@ void Client::handleCommand_AuthAccept(NetworkPacket* pkt)
                                        << m_recommended_send_interval<<std::endl;
 
        // Reply to server
-       NetworkPacket resp_pkt(TOSERVER_INIT2, 0);
+       std::string lang = gettext("LANG_CODE");
+       if (lang == "LANG_CODE")
+               lang = "";
+
+       NetworkPacket resp_pkt(TOSERVER_INIT2, sizeof(u16) + lang.size());
+       resp_pkt << lang;
        Send(&resp_pkt);
 
        m_state = LC_Init;
@@ -142,7 +150,9 @@ void Client::handleCommand_AcceptSudoMode(NetworkPacket* pkt)
 }
 void Client::handleCommand_DenySudoMode(NetworkPacket* pkt)
 {
-       pushToChatQueue(L"Password change denied. Password NOT changed.");
+       ChatMessage *chatMessage = new ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
+                       L"Password change denied. Password NOT changed.");
+       pushToChatQueue(chatMessage);
        // reset everything and be sad
        deleteAuthData();
 }
@@ -218,7 +228,7 @@ void Client::handleCommand_AccessDenied(NetworkPacket* pkt)
                if (denyCode == SERVER_ACCESSDENIED_SHUTDOWN ||
                                denyCode == SERVER_ACCESSDENIED_CRASH) {
                        *pkt >> m_access_denied_reason;
-                       if (m_access_denied_reason == "") {
+                       if (m_access_denied_reason.empty()) {
                                m_access_denied_reason = accessDeniedStrings[denyCode];
                        }
                        u8 reconnect;
@@ -234,7 +244,7 @@ void Client::handleCommand_AccessDenied(NetworkPacket* pkt)
                        // Until then (which may be never), this is outside
                        // of the defined protocol.
                        *pkt >> m_access_denied_reason;
-                       if (m_access_denied_reason == "") {
+                       if (m_access_denied_reason.empty()) {
                                m_access_denied_reason = "Unknown";
                        }
                }
@@ -395,7 +405,7 @@ void Client::handleCommand_TimeOfDay(NetworkPacket* pkt)
                        << " dr=" << dr << std::endl;
 }
 
-void Client::handleCommand_ChatMessage(NetworkPacket* pkt)
+void Client::handleCommand_ChatMessageOld(NetworkPacket *pkt)
 {
        /*
                u16 command
@@ -413,8 +423,43 @@ void Client::handleCommand_ChatMessage(NetworkPacket* pkt)
        }
 
        // If chat message not consummed by client lua API
+       // @TODO send this to CSM using ChatMessage object
        if (!moddingEnabled() || !m_script->on_receiving_message(wide_to_utf8(message))) {
-               pushToChatQueue(message);
+               pushToChatQueue(new ChatMessage(message));
+       }
+}
+
+void Client::handleCommand_ChatMessage(NetworkPacket *pkt)
+{
+       /*
+               u8 version
+               u8 message_type
+               u16 sendername length
+               wstring sendername
+               u16 length
+               wstring message
+        */
+
+       ChatMessage *chatMessage = new ChatMessage();
+       u8 version, message_type;
+       *pkt >> version >> message_type;
+
+       if (version != 1 || message_type >= CHATMESSAGE_TYPE_MAX) {
+               delete chatMessage;
+               return;
+       }
+
+       *pkt >> chatMessage->sender >> chatMessage->message >> chatMessage->timestamp;
+
+       chatMessage->type = (ChatMessageType) message_type;
+
+       // @TODO send this to CSM using ChatMessage object
+       if (!moddingEnabled() || !m_script->on_receiving_message(
+                       wide_to_utf8(chatMessage->message))) {
+               pushToChatQueue(chatMessage);
+       } else {
+               // Message was consumed by CSM and should not handled by client, destroying
+               delete chatMessage;
        }
 }
 
@@ -645,7 +690,7 @@ void Client::handleCommand_AnnounceMedia(NetworkPacket* pkt)
                Strfnd sf(str);
                while(!sf.at_end()) {
                        std::string baseurl = trim(sf.next(","));
-                       if (baseurl != "")
+                       if (!baseurl.empty())
                                m_media_downloader->addRemoteServer(baseurl);
                }
        }
@@ -683,8 +728,7 @@ void Client::handleCommand_Media(NetworkPacket* pkt)
        if (num_files == 0)
                return;
 
-       if (m_media_downloader == NULL ||
-                       !m_media_downloader->isStarted()) {
+       if (!m_media_downloader || !m_media_downloader->isStarted()) {
                const char *problem = m_media_downloader ?
                        "media has not been requested" :
                        "all media has been received already";
@@ -764,6 +808,7 @@ void Client::handleCommand_PlaySound(NetworkPacket* pkt)
                [23 + len] u16 object_id
                [25 + len] bool loop
                [26 + len] f32 fade
+               [30 + len] f32 pitch
        */
 
        s32 server_id;
@@ -774,29 +819,31 @@ void Client::handleCommand_PlaySound(NetworkPacket* pkt)
        v3f pos;
        u16 object_id;
        bool loop;
-       float fade = 0;
+       float fade = 0.0f;
+       float pitch = 1.0f;
 
        *pkt >> server_id >> name >> gain >> type >> pos >> object_id >> loop;
 
        try {
                *pkt >> fade;
+               *pkt >> pitch;
        } catch (PacketError &e) {};
 
        // Start playing
        int client_id = -1;
        switch(type) {
                case 0: // local
-                       client_id = m_sound->playSound(name, loop, gain, fade);
+                       client_id = m_sound->playSound(name, loop, gain, fade, pitch);
                        break;
                case 1: // positional
-                       client_id = m_sound->playSoundAt(name, loop, gain, pos);
+                       client_id = m_sound->playSoundAt(name, loop, gain, pos, pitch);
                        break;
                case 2:
                { // object
                        ClientActiveObject *cao = m_env.getActiveObject(object_id);
                        if (cao)
                                pos = cao->getPosition();
-                       client_id = m_sound->playSoundAt(name, loop, gain, pos);
+                       client_id = m_sound->playSoundAt(name, loop, gain, pos, pitch);
                        // TODO: Set up sound to move with object
                        break;
                }
@@ -1143,18 +1190,23 @@ void Client::handleCommand_HudSetFlags(NetworkPacket* pkt)
        assert(player != NULL);
 
        bool was_minimap_visible = player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE;
+       bool was_minimap_radar_visible = player->hud_flags & HUD_FLAG_MINIMAP_RADAR_VISIBLE;
 
        player->hud_flags &= ~mask;
        player->hud_flags |= flags;
 
        m_minimap_disabled_by_server = !(player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE);
+       bool m_minimap_radar_disabled_by_server = !(player->hud_flags & HUD_FLAG_MINIMAP_RADAR_VISIBLE);
 
        // Hide minimap if it has been disabled by the server
-       if (m_minimap && m_minimap_disabled_by_server && was_minimap_visible) {
+       if (m_minimap && m_minimap_disabled_by_server && was_minimap_visible)
                // defers a minimap update, therefore only call it if really
                // needed, by checking that minimap was visible before
                m_minimap->setMinimapMode(MINIMAP_MODE_OFF);
-       }
+
+       // Switch to surface mode if radar disabled by server
+       if (m_minimap && m_minimap_radar_disabled_by_server && was_minimap_radar_visible)
+               m_minimap->setMinimapMode(MINIMAP_MODE_SURFACEx1);
 }
 
 void Client::handleCommand_HudSetParam(NetworkPacket* pkt)
@@ -1172,9 +1224,21 @@ void Client::handleCommand_HudSetParam(NetworkPacket* pkt)
                        player->hud_hotbar_itemcount = hotbar_itemcount;
        }
        else if (param == HUD_PARAM_HOTBAR_IMAGE) {
+               // If value not empty verify image exists in texture source
+               if (!value.empty() && !getTextureSource()->isKnownSourceImage(value)) {
+                       errorstream << "Server sent wrong Hud hotbar image (sent value: '"
+                               << value << "')" << std::endl;
+                       return;
+               }
                player->hotbar_image = value;
        }
        else if (param == HUD_PARAM_HOTBAR_SELECTED_IMAGE) {
+               // If value not empty verify image exists in texture source
+               if (!value.empty() && !getTextureSource()->isKnownSourceImage(value)) {
+                       errorstream << "Server sent wrong Hud hotbar selected image (sent value: '"
+                                       << value << "')" << std::endl;
+                       return;
+               }
                player->hotbar_selected_image = value;
        }
 }
@@ -1324,3 +1388,8 @@ void Client::handleCommand_SrpBytesSandB(NetworkPacket* pkt)
        resp_pkt << std::string(bytes_M, len_M);
        Send(&resp_pkt);
 }
+
+void Client::handleCommand_CSMFlavourLimits(NetworkPacket *pkt)
+{
+       *pkt >> m_csm_flavour_limits >> m_csm_noderange_limit;
+}