12#include <engine2/igameeventsystem.h>
13#include <engine2/serversideclient.h>
14#include <netmessages.pb.h>
15#include <networksystem/inetchannel.h>
16#include <networksystem/inetworkmessages.h>
17#include <recipientfilter.h>
29 : _interfaces(interfaces), _bindings(bindings), _pending(std::make_unique<
PendingConVarQueries>())
32 _slotListener = slots.
Changed += [
this](
int slot) { _pending->Clear(slot); };
34 if (
Status installed = Install(); !installed)
36 _failure = installed.error();
48 return std::unexpected(_failure);
51Status ClientConVars::Install()
55 return std::unexpected(
Error::NotReady(
"engine interfaces unavailable"));
58 NetMessageInfo_t* getCvarValue = _interfaces.
NetworkMessages->FindNetworkMessagePartial(
"CSVCMsg_GetCvarValue");
61 return std::unexpected(
Error::Engine(
"the engine does not provide CSVCMsg_GetCvarValue"));
64 const auto onResponse = [
this](CServerSideClientBase& client,
65 const CNetMessagePB<CCLCMsg_RespondCvarValue>& message) {
66 OnRespondCvarValue(client, message);
68 auto hook =
HookTable(
"Client convar response", &CServerSideClientBase::ProcessRespondCvarValue,
69 _bindings.ClientTable,
nullptr, onResponse);
72 return std::unexpected(hook.error());
75 _hook = std::move(*hook);
76 _getCvarValue = getCvarValue;
77 Log::Info(
"Client convar queries enabled.");
83 if (!_hook || !
IsValidSlot(slot) || cvarName.empty() || !callback)
89 _pending->Prune(slot, now);
94 inFlight->Callback = std::move(callback);
98 if (_pending->Full(slot))
103 const int cookie = _pending->NextCookie(slot);
104 if (cookie < 0 || !Send(slot, cvarName, cookie))
109 _pending->Add(slot, cookie, std::string(cvarName), std::move(callback), now);
115 return _pending->Count(slot);
120 _pending->ClearAll();
123bool ClientConVars::Send(
int slot, std::string_view cvarName,
int cookie)
126 if (!_interfaces.
Engine->GetPlayerNetInfo(CPlayerSlot(slot)))
131 CNetMessage* message = _getCvarValue->AllocateMessage();
137 auto* request = message->ToPB<CSVCMsg_GetCvarValue>();
144 request->set_cookie(cookie);
145 request->set_cvar_name(std::string(cvarName));
147 CReliableSingleUserRecipientFilter filter(slot);
148 _interfaces.GameEventSystem->PostEventAbstract(-1,
false, &filter, _getCvarValue, request, 0);
153void ClientConVars::OnRespondCvarValue(
const CServerSideClientBase& client,
const CCLCMsg_RespondCvarValue& msg)
155 const int slot = client.GetPlayerSlot().Get();
157 if (!
IsValidSlot(slot) || !msg.has_cookie() || !msg.has_status_code() || !msg.has_name())
163 const int status = msg.status_code();
164 if (!EQueryCvarValueStatus_IsValid(status))
169 std::string_view value;
172 if (!msg.has_value() || msg.value().find(
'\0') != std::string::npos)
180 auto query = _pending->Take(slot, msg.cookie(), msg.name());
181 if (query && query->Callback)
std::function< void(int slot, ClientConVarStatus status, std::string_view name, std::string_view value)> QueryCallback
bool Query(int slot, std::string_view cvarName, QueryCallback callback)
ClientConVars(Interfaces &interfaces, const Bindings &bindings, SlotEvents &slots)
size_t PendingCount(int slot) const
Bookkeeping for ClientConVars: per-slot, cookie-keyed queries awaiting an answer.
"The occupant of this slot changed" - raised by PlayerManager, consumed below it.
static double MonotonicSeconds()
void Info(std::format_string< Args... > fmt, Args &&... args)
Result< Subscription > HookTable(std::string_view name, Ret(Object::*method)(Args...), const ClassTable &table, Before &&before, After &&after=nullptr)
Hook an SDK-declared virtual on every object sharing a gamedata-found class table.
std::expected< void, Error > Status
constexpr bool IsValidSlot(int slot)
ClientConVarStatus
How the client answered a convar query (CCLCMsg_RespondCvarValue::status_code).
@ NotFound
The client has no convar by that name.
@ Protected
The convar is marked protected; the client withholds its value.
@ Answered
The client reported the convar's current value.
@ NotAConVar
The name exists on the client but is a command, not a convar.
static Error Engine(std::string detail)
static Error NotReady(std::string detail)
Centralized holder for all SDK interface pointers.
IGameEventSystem * GameEventSystem
INetworkMessages * NetworkMessages
One outstanding client convar query, identified by the cookie sent to the client.