diff --git a/docs/cvars.md b/docs/cvars.md index 55f258ab0..d2a2195c6 100644 --- a/docs/cvars.md +++ b/docs/cvars.md @@ -323,6 +323,7 @@ |sar_mtrigger_legacy|0|| |sar_mtrigger_legacy_format|!seg -> !tt (!st)|Formatting of the text that is displayed in the chat (!map - for map name, !seg - for segment name, !tt - for total time, !st - for split time).| |sar_mtrigger_legacy_textcolor|255 176 0|The color of the text that is displayed in the chat.| +|sar_netmessage_debug|0|Debug NetMessages.| |sar_nextdemo|cmd|sar_nextdemo - plays the next demo in demo queue| |sar_on_cfg_message|cmd|sar_on_cfg_message \ [args]... - registers a command to be run when partner sends a custom message (_sar_cfg_message svar)| |sar_on_cfg_message_clear|cmd|sar_on_cfg_message_clear - clears commands registered on event "cfg_message"| diff --git a/src/Features/NetMessage.cpp b/src/Features/NetMessage.cpp index ba361149e..ad3296745 100644 --- a/src/Features/NetMessage.cpp +++ b/src/Features/NetMessage.cpp @@ -24,6 +24,8 @@ bool g_orangeReady = false; bool g_partnerHasSAR = false; bool g_session_init = false; +Variable sar_netmessage_debug("sar_netmessage_debug", "0", "Debug NetMessages.\n"); + static size_t g_expected_len = 0; static std::string g_partial; @@ -52,6 +54,8 @@ static inline void handleMessage(const char *type, const void *data, size_t size static bool readyToSend() { if (!engine->IsCoop()) { return false; + } else if (!g_partnerHasSAR) { + return false; } else if (engine->IsOrange()) { return session->isRunning; } else { @@ -62,6 +66,7 @@ static bool readyToSend() { void NetMessage::SessionStarted() { if (engine->IsCoop() && !engine->IsSplitscreen()) { if (!g_session_init && engine->IsOrange()) { + if (sar_netmessage_debug.GetBool()) console->Print("New coop session started as orange, saying hello!\n"); g_session_init = true; engine->ExecuteCommand("say \"" SAR_MSG_HELLO "\""); } @@ -175,13 +180,19 @@ void NetMessage::SendMsg(const char *type, const void *data, size_t size) { } // If the partner doesn't have SAR, don't send messages - if (!readyToSend() || !g_partnerHasSAR) { + if (!readyToSend()) { + if (sar_netmessage_debug.GetBool()) { + console->Print("NetMessage::SendMsg: not ready to send %s, queueing\n", type); + } g_queued.push({ std::string(type), std::vector((const uint8_t *)data, (const uint8_t *)data + size), }); return; } + if (sar_netmessage_debug.GetBool()) { + console->Print("NetMessage::SendMsg: sending %s\n", type); + } const char *init_prefix = engine->IsOrange() ? SAR_MSG_INIT_O : SAR_MSG_INIT_B; const char *cont_prefix = engine->IsOrange() ? SAR_MSG_CONT_O : SAR_MSG_CONT_B; @@ -230,25 +241,37 @@ void NetMessage::Update() { } if (readyToSend()) { + if (g_queued.size() > 0 && sar_netmessage_debug.GetBool()) { + console->Print("NetMessage::Update: sending queued messages\n"); + } for (size_t i = 0; i < g_queued.size(); ++i) { auto &msg = g_queued.front(); NetMessage::SendMsg(msg.type.c_str(), msg.data.data(), msg.data.size()); g_queued.pop(); } } + + static float last_print = 0; + if (sar_netmessage_debug.GetBool() && g_queued.size() > 0 && engine->GetHostTime() - last_print > 1) { + console->Print("NetMessage::Update: %d messages in queue\n", g_queued.size()); + last_print = engine->GetHostTime(); + } } bool NetMessage::ChatData(std::string str) { if (str.size() < 4) return false; + if (sar_netmessage_debug.GetBool()) console->Print("NetMessage::ChatData: %s\n", str.c_str()); if (str == SAR_MSG_HELLO) { if (!engine->IsOrange()) { + if (sar_netmessage_debug.GetBool()) console->Print("NetMessage::ChatData: Received hello message! Replying\n"); g_partnerHasSAR = true; engine->ExecuteCommand("say \"" SAR_MSG_HELLO_ACK "\""); } return true; } else if (str == SAR_MSG_HELLO_ACK) { if (engine->IsOrange()) { + if (sar_netmessage_debug.GetBool()) console->Print("NetMessage::ChatData: Received hello ack message!\n"); g_partnerHasSAR = true; } return true; @@ -290,6 +313,7 @@ bool NetMessage::ChatData(std::string str) { const char *type = (const char *)decoded.data(); // starts with null-terminated type size_t type_len = strlen(type); const uint8_t *data = decoded.data() + type_len + 1; + if (sar_netmessage_debug.GetBool()) console->Print("NetMessage::ChatData: received %s\n", type); handleMessage(type, data, decoded.size() - type_len - 1); } diff --git a/src/Features/NetMessage.hpp b/src/Features/NetMessage.hpp index 742212a99..ac2167844 100644 --- a/src/Features/NetMessage.hpp +++ b/src/Features/NetMessage.hpp @@ -1,11 +1,15 @@ #pragma once +#include "Variable.hpp" + #include #include extern bool g_orangeReady; extern bool g_partnerHasSAR; +extern Variable sar_netmessage_debug; + #define SAR_MSG_HELLO "Hello! I have \x07SourceAutoRecord\x07, a plugin mainly used for speedrunning." #define SAR_MSG_HELLO_ACK "&^@$Yes hello I also have \x07SourceAutoRecord\x07 thank you for checking before spamming chat." diff --git a/src/Modules/Client.cpp b/src/Modules/Client.cpp index 968699c05..20367b76b 100644 --- a/src/Modules/Client.cpp +++ b/src/Modules/Client.cpp @@ -472,7 +472,7 @@ DETOUR(Client::MsgFunc_SayText2, bf_read &msg) { if (NetMessage::ChatData(str)) { // skip the other crap, just in case it matters msg.ReadUnsigned(8); - return 0; + if (!sar_netmessage_debug.GetBool()) return 0; } msg = pre; diff --git a/src/Utils/Platform.hpp b/src/Utils/Platform.hpp index 4c700da6c..f37c670c6 100644 --- a/src/Utils/Platform.hpp +++ b/src/Utils/Platform.hpp @@ -1,6 +1,6 @@ #pragma once #ifdef _WIN32 -# include "minhook/MinHook.h" +# include "../lib/minhook/MinHook.h" #endif #ifdef _WIN32