Skip to content

Commit

Permalink
feat: sar_netmessage_debug
Browse files Browse the repository at this point in the history
and don't try to send the queue if the partner doesn't have sar
  • Loading branch information
ThisAMJ committed Aug 7, 2024
1 parent 46797ba commit d955ec6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/cvars.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 \<command> [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"|
Expand Down
26 changes: 25 additions & 1 deletion src/Features/NetMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand All @@ -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 "\"");
}
Expand Down Expand Up @@ -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<uint8_t>((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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Features/NetMessage.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#pragma once

#include "Variable.hpp"

#include <cstddef>
#include <string>

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."

Expand Down
2 changes: 1 addition & 1 deletion src/Modules/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Platform.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#ifdef _WIN32
# include "minhook/MinHook.h"
# include "../lib/minhook/MinHook.h"
#endif

#ifdef _WIN32
Expand Down

0 comments on commit d955ec6

Please sign in to comment.