Skip to content

Commit

Permalink
feat: custom cfg coop messages
Browse files Browse the repository at this point in the history
for Nid to be able to send remote commands
can also help with the automation of resets/other coop things

(nid, on the receiving end, do `sar_on_cfg_message sar_expand $_sar_cfg_message` and `sar_cfg_message <thing>` to send)
(ONLY IF YOU TRUST PARTNER) (with master srconfigs you can add `cond ?partner=amj` after doing `partner_add <name>`)
  • Loading branch information
ThisAMJ committed Mar 31, 2024
1 parent 410aaff commit edf56e5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/cvars.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
|sar_cam_setang|cmd|sar_cam_setang \<pitch> \<yaw> [roll] - sets camera angle (requires camera Drive Mode)|
|sar_cam_setfov|cmd|sar_cam_setfov \<fov> - sets camera field of view (requires camera Drive Mode)|
|sar_cam_setpos|cmd|sar_cam_setpos \<x> \<y> \<z> - sets camera position (requires camera Drive Mode)|
|sar_cfg_message|cmd|sar_cfg_message \<message> - sends a config message to the other player in coop|
|sar_challenge_autostop|0|Automatically stops recording demos when the leaderboard opens after a CM run. If 2, automatically appends the run time to the demo name.|
|sar_challenge_autosubmit_reload_api_key|cmd|sar_challenge_autosubmit_reload_api_key - reload the board.portal2.sr API key from its file.|
|sar_chat|cmd|sar_chat - open the chat HUD|
Expand Down Expand Up @@ -320,6 +321,9 @@
|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_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"|
|sar_on_cfg_message_list|cmd|sar_on_cfg_message_list - lists commands registered on event "cfg_message"|
|sar_on_config_exec|cmd|sar_on_config_exec \<command> [args]... - registers a command to be run on config.cfg exec|
|sar_on_config_exec_clear|cmd|sar_on_config_exec_clear - clears commands registered on event "config_exec"|
|sar_on_config_exec_list|cmd|sar_on_config_exec_list - lists commands registered on event "config_exec"|
Expand Down
7 changes: 7 additions & 0 deletions src/Event.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <functional>
#include <string>

#define _ON_INIT1(x) \
static void _sar_init_fn_##x(); \
Expand Down Expand Up @@ -46,6 +47,7 @@ namespace Event {
TAS_START,
TAS_END,
MAYBE_AUTOSUBMIT,
CFG_MESSAGE,
};

template <EventType E>
Expand Down Expand Up @@ -88,6 +90,11 @@ namespace Event {
bool pb;
};

template <>
struct EventData<CFG_MESSAGE> {
std::string message;
};

template <EventType E>
struct _EventReg {
std::function<void(EventData<E>)> cb;
Expand Down
31 changes: 31 additions & 0 deletions src/Features/ConfigPlus.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Event.hpp"
#include "Features/NetMessage.hpp"
#include "Features/Session.hpp"
#include "Modules/Client.hpp"
#include "Modules/Engine.hpp"
Expand All @@ -19,6 +20,7 @@
#endif

#define PERSISTENT_SVAR_FILENAME "svars_persist"
#define CFG_MESSAGE_TYPE "cfgmessage"

static std::map<std::string, std::string> g_svars;
static std::unordered_set<std::string> g_persistentSvars;
Expand Down Expand Up @@ -727,6 +729,30 @@ CON_COMMAND_F(sar_get_partner_id, "sar_get_partner_id - Prints your coop partner
console->Print("%s\n", engine->GetPartnerSteamID32().c_str());
}

CON_COMMAND_F(sar_cfg_message, "sar_cfg_message <message> - sends a config message to the other player in coop\n", FCVAR_DONTRECORD) {
if (!engine->IsCoop() || engine->IsSplitscreen()) {
console->Print("This command only works in online co-op.\n");
return;
}

if (args.ArgC() < 2) {
return console->Print(sar_cfg_message.ThisPtr()->m_pszHelpString);
}

auto str = Utils::ArgContinuation(args, 1);
NetMessage::SendMsg(CFG_MESSAGE_TYPE, str, strlen(str));
}

ON_INIT {
NetMessage::RegisterHandler(CFG_MESSAGE_TYPE, +[](const void *data, size_t size) {
auto msg = std::string((char *)data, size);
// be kinda safe
msg.erase(std::remove(msg.begin(), msg.end(), '"'), msg.end());
msg.erase(std::remove(msg.begin(), msg.end(), ';'), msg.end());
Event::Trigger<Event::CFG_MESSAGE>({msg});
});
}

CON_COMMAND_F(cond, "cond <condition> <command> [args]... - runs a command only if a given condition is met\n", FCVAR_DONTRECORD) {
if (args.ArgC() < 3) {
return console->Print(cond.ThisPtr()->m_pszHelpString);
Expand Down Expand Up @@ -823,6 +849,7 @@ MK_SAR_ON(tas_start, "when TAS script playback starts", true)
MK_SAR_ON(tas_end, "when TAS script playback ends", true)
MK_SAR_ON(pb, "when auto-submitter detects PB", true)
MK_SAR_ON(not_pb, "when auto-submitter detects not PB", true)
MK_SAR_ON(cfg_message, "when partner sends a custom message (_sar_cfg_message svar)", true)

ON_EVENT_P(SESSION_START, 1000000) {
RUN_EXECS(load);
Expand Down Expand Up @@ -865,6 +892,10 @@ ON_EVENT(MAYBE_AUTOSUBMIT) {
if (event.pb) RUN_EXECS(pb);
else RUN_EXECS(not_pb);
}
ON_EVENT(CFG_MESSAGE) {
SetSvar("_sar_cfg_message", event.message);
RUN_EXECS(cfg_message);
}

struct Seq {
std::queue<std::string> commands;
Expand Down

0 comments on commit edf56e5

Please sign in to comment.