VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
ClientConVars.hpp
Go to the documentation of this file.
1#pragma once
2
9#include <cstddef>
10#include <functional>
11#include <memory>
12#include <string>
13#include <string_view>
14
15namespace VoltMod
16{
17
18/** @brief How the client answered a convar query (CCLCMsg_RespondCvarValue::status_code). */
20{
21 Answered = 0, ///< The client reported the convar's current value.
22 NotFound, ///< The client has no convar by that name.
23 NotAConVar, ///< The name exists on the client but is a command, not a convar.
24 Protected ///< The convar is marked protected; the client withholds its value.
25};
26
27/**
28 * @brief Asks a connected client what one of its own convars is set to.
29 *
30 * The server sends `CSVCMsg_GetCvarValue` with a cookie. The client answers later with
31 * `CCLCMsg_RespondCvarValue`, intercepted through a vtable hook on `CServerSideClient`.
32 * Queries are asynchronous, unordered, and may never complete.
33 *
34 * A modified client can answer with anything, so treat the result as evidence, not proof.
35 *
36 * This optional load step depends on an RTTI or symbol lookup of the `CServerSideClient` vtable. These values drift
37 * with engine updates. On failure @ref Available carries the reason.
38 *
39 * @code
40 * runtime.ClientConVars.Query(slot, "sensitivity",
41 * [](int slot, ClientConVarStatus status, std::string_view name, std::string_view value) {
42 * if (status == ClientConVarStatus::Answered)
43 * Log::Info("{} = {}", name, value);
44 * });
45 * @endcode
46 */
48{
49public:
50 /**
51 * Invoked on the game thread when the client answers. @p name and @p value borrow the decoded
52 * message, so copy what you keep. @p value is empty unless @p status is Answered.
53 */
55 std::function<void(int slot, ClientConVarStatus status, std::string_view name, std::string_view value)>;
56
57 /** @p interfaces and @p bindings drive the response hook and the query send path. @p slots
58 * tells the service when a slot changes hands, so an answer can never reach the callback of
59 * whoever held the slot before. All three must outlive it; the Runtime declares them above.
60 * Installs the response hook; a failure leaves the service inert and names itself in
61 * @ref Available. */
62 ClientConVars(Interfaces& interfaces, const Bindings& bindings, SlotEvents& slots);
64 ClientConVars(const ClientConVars&) = delete;
66
67 /** Why queries cannot be sent: the error the hook install returned. */
68 Status Available() const;
69
70 /**
71 * Ask @p slot for its value of @p cvarName. False when the service is not @ref Available, the
72 * slot holds a bot or nobody, the per-slot pending cap is reached, or the message could not
73 * be sent.
74 *
75 * A convar already in flight for that slot re-targets the outstanding request rather than
76 * sending a second one, so polling cannot flood a client. Pending entries expire silently
77 * after 10 seconds.
78 */
79 bool Query(int slot, std::string_view cvarName, QueryCallback callback);
80
81 /** Number of queries awaiting an answer on @p slot. Diagnostics only. */
82 size_t PendingCount(int slot) const;
83
84 /** @internal Called by the framework's StartupServer hook: drops every pending query. */
85 void OnServerStartup();
86
87private:
88 Status Install();
89
90 void OnRespondCvarValue(const CServerSideClientBase& client, const CCLCMsg_RespondCvarValue& msg);
91
92 /** Send a query to one connected human client. */
93 bool Send(int slot, std::string_view cvarName, int cookie);
94
95 Interfaces& _interfaces;
96 const Bindings& _bindings;
97 /** Behind a pointer only so its header stays under src/, where its tests live. */
98 std::unique_ptr<PendingConVarQueries> _pending;
99 NetMessageInfo_t* _getCvarValue = nullptr;
100 Error _failure = Error::NotReady("client convar queries are not initialized");
101 Subscription _hook;
102 /** Declared after _pending so it unregisters before the table its callback clears. */
103 Subscription _slotListener;
104};
105
106} // namespace VoltMod
std::function< void(int slot, ClientConVarStatus status, std::string_view name, std::string_view value)> QueryCallback
ClientConVars(const ClientConVars &)=delete
bool Query(int slot, std::string_view cvarName, QueryCallback callback)
ClientConVars(Interfaces &interfaces, const Bindings &bindings, SlotEvents &slots)
size_t PendingCount(int slot) const
ClientConVars & operator=(const ClientConVars &)=delete
"The occupant of this slot changed" - raised by PlayerManager, consumed below it.
Owns one registration and releases it on destruction.
std::expected< void, Error > Status
Definition Result.hpp:67
@ NotFound
The named thing does not exist (no such player, convar, row).
Definition Result.hpp:23
ClientConVarStatus
How the client answered a convar query (CCLCMsg_RespondCvarValue::status_code).
@ 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.
One failure: a code to branch on, text for the log, and an optional translation key.
Definition Result.hpp:42
static Error NotReady(std::string detail)
Definition Result.hpp:50
Centralized holder for all SDK interface pointers.