8#include <engine2/igameeventsystem.h>
11#include <networkbasetypes.pb.h>
12#include <networksystem/inetworkmessages.h>
13#include <recipientfilter.h>
16#include <tier0/convar.h>
21static std::string_view
Text(
const char* value)
23 return value ? std::string_view(value) : std::string_view{};
27 const char* oldValue, ConVarUserInfoSet_t* )
42 :
Changed({.OnFirst = [
this] {
return RouteChanges(); }, .OnLast = [
this] { StopRoutingChanges(); }}),
43 _interfaces(interfaces)
54 if (!_interfaces.CVar)
63 auto* engine = _interfaces.Engine;
71 if (command.find_first_of(
"\r\n") != std::string_view::npos)
73 Log::Warn(
"ConVars: refused a server command with an embedded newline.");
78 std::string line(command);
79 if (line.empty() || line.back() !=
'\n')
84 engine->ServerCommand(line.c_str());
92 return std::unexpected(text.error());
95 std::string_view rest = *text;
98 const auto end = rest.find(
'\n');
99 const std::string_view raw = rest.substr(0, end);
100 rest = end == std::string_view::npos ? std::string_view{} : rest.substr(end + 1);
102 const std::string line =
Strings::Trim(raw.substr(0, raw.find(
"//")));
116NetMessageInfo_t* ConVars::SetConVarMessage()
120 return _setConVarMsg;
129 _setConVarMsg = messages->FindNetworkMessagePartial(
"SetConVar");
132 Log::Warn(
"ConVars: CNETMsg_SetConVar not found; per-client convar overrides are unavailable.");
135 return _setConVarMsg;
138bool ConVars::SendToClient(
int slot, std::string_view name, std::string_view value)
140 if (!_interfaces.GameEventSystem || !
IsValidSlot(slot) || name.empty())
145 auto* msgType = SetConVarMessage();
151 CNetMessage* msg = msgType->AllocateMessage();
157 auto* setConVar = msg->ToPB<CNETMsg_SetConVar>();
160 auto* cvar = setConVar->mutable_convars()->add_cvars();
161 cvar->set_name(std::string(name));
162 cvar->set_value(std::string(value));
164 CReliableSingleUserRecipientFilter filter(slot);
165 _interfaces.GameEventSystem->PostEventAbstract(-1,
false, &filter, msgType, msg, 0);
169 return setConVar !=
nullptr;
172bool ConVars::RouteChanges()
179 auto* cvar = _interfaces.CVar;
182 Log::Warn(
"ConVars: ICvar is not resolved; convar change handlers will not fire.");
188 _routingChanges =
true;
192void ConVars::StopRoutingChanges()
194 if (!_routingChanges)
199 if (
auto* cvar = _interfaces.CVar)
205 _routingChanges =
false;
static std::string_view Text(const char *value)
static void GlobalConVarChangeCallback(ConVarRefAbstract *ref, CSplitScreenSlot, const char *newValue, const char *oldValue, ConVarUserInfoSet_t *)
static VoltMod::ConVars * g_changeCallback
void SetByConsole(std::string_view name, std::string_view value)
Assign a convar over the console, quoting the value.
Status ExecuteFile(std::string_view path)
ConVars(Interfaces &interfaces)
Event< const ConVarChange & > Changed
void ExecuteServerCommand(std::string_view command)
static std::string Trim(std::string_view str)
static std::string QuoteConsoleArg(std::string_view value)
void Warn(std::format_string< Args... > fmt, Args &&... args)
std::expected< void, Error > Status
constexpr bool IsValidSlot(int slot)
Result< std::string > ReadAllText(std::string_view path)
Read path (resolved via ResolvePath) into a string.
static Error NotReady(std::string detail)
Centralized holder for all SDK interface pointers.
INetworkMessages * NetworkMessages