Skip to content

Commit

Permalink
feat: ultimate partner SAR detection
Browse files Browse the repository at this point in the history
orange says hello (plaintext but unlikely to be accidental lmao)
blue receives, now knows orange has sar, sends acknowledgement back, now both players know they have sar
this happens once per partnership, not every load
  • Loading branch information
ThisAMJ committed Aug 6, 2024
1 parent c39f066 commit 8a30ff3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
31 changes: 28 additions & 3 deletions src/Features/NetMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// if orange: whether we've sent the ready packet
bool g_orangeReady = false;
bool g_partnerHasSAR = false;
bool g_session_init = false;

static size_t g_expected_len = 0;
static std::string g_partial;
Expand Down Expand Up @@ -58,9 +59,20 @@ static bool readyToSend() {
}
}

void NetMessage::SessionStarted() {
if (engine->IsCoop() && !engine->IsSplitscreen()) {
if (!g_session_init && engine->IsOrange()) {
g_session_init = true;
engine->ExecuteCommand("say \"" SAR_MSG_HELLO "\"");
}
} else {
g_session_init = false;
g_partnerHasSAR = false;
}
}

void NetMessage::SessionEnded() {
g_orangeReady = false;
g_partnerHasSAR = false;
}

struct QueuedMsg {
Expand Down Expand Up @@ -162,8 +174,8 @@ void NetMessage::SendMsg(const char *type, const void *data, size_t size) {
return;
}

// If the partner doesn't have SAR, only send sync messages
if (!readyToSend() || (!g_partnerHasSAR && !!strcmp(type, "__sync"))) {
// If the partner doesn't have SAR, don't send messages
if (!readyToSend() || !g_partnerHasSAR) {
g_queued.push({
std::string(type),
std::vector<uint8_t>((const uint8_t *)data, (const uint8_t *)data + size),
Expand Down Expand Up @@ -229,6 +241,19 @@ void NetMessage::Update() {
bool NetMessage::ChatData(std::string str) {
if (str.size() < 4) return false;

if (str == SAR_MSG_HELLO) {
if (!engine->IsOrange()) {
g_partnerHasSAR = true;
engine->ExecuteCommand("say \"" SAR_MSG_HELLO_ACK "\"");
}
return true;
} else if (str == SAR_MSG_HELLO_ACK) {
if (engine->IsOrange()) {
g_partnerHasSAR = true;
}
return true;
}

std::string prefix = str.substr(0, 4);
bool has_prefix = true;
bool cont, orange;
Expand Down
4 changes: 4 additions & 0 deletions src/Features/NetMessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
extern bool g_orangeReady;
extern bool g_partnerHasSAR;

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

namespace NetMessage {
void RegisterHandler(const char *type, void (*handler)(const void *data, size_t size));
void SendMsg(const char *type, const void *data, size_t size);
bool ChatData(std::string str);
void Update();
void SessionStarted();
void SessionEnded();
}; // namespace NetMessage
2 changes: 2 additions & 0 deletions src/Features/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ void Session::Started(bool menu) {
return;
}

NetMessage::SessionStarted();

if (menu) {
console->Print("Session started! (menu)\n");
this->Rebase(engine->GetTick());
Expand Down
5 changes: 4 additions & 1 deletion src/Modules/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,10 @@ CON_COMMAND(sar_give_betsrighter, "sar_give_betsrighter [n] - gives the player i
}
DETOUR_COMMAND(Server::say) {
auto clientidx = UTIL_GetCommandClientIndex();
if (args.ArgC() != 2 || Utils::StartsWith(args[1], "&^") || !networkManager.HandleGhostSay(args[1], clientidx)) {
if (args.ArgC() != 2 ||
Utils::StartsWith(args[1], "&^") ||
!strcmp(args[1], "\"" SAR_MSG_HELLO "\"") ||
!networkManager.HandleGhostSay(args[1], clientidx)) {
g_wasChatType = 0;
Server::say_callback(args);
}
Expand Down

0 comments on commit 8a30ff3

Please sign in to comment.