VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
ConVar.cpp
Go to the documentation of this file.
2
4#include <format>
5#include <string>
6#include <string_view>
7#include <tier0/convar.h>
8#include <type_traits>
9
10namespace VoltMod
11{
12
13// Keep the SDK-independent type mirror aligned with the engine.
14static_assert(static_cast<int>(ConVarType::Invalid) == EConVarType_Invalid);
15static_assert(static_cast<int>(ConVarType::Bool) == EConVarType_Bool);
16static_assert(static_cast<int>(ConVarType::Int16) == EConVarType_Int16);
17static_assert(static_cast<int>(ConVarType::UInt16) == EConVarType_UInt16);
18static_assert(static_cast<int>(ConVarType::Int32) == EConVarType_Int32);
19static_assert(static_cast<int>(ConVarType::UInt32) == EConVarType_UInt32);
20static_assert(static_cast<int>(ConVarType::Int64) == EConVarType_Int64);
21static_assert(static_cast<int>(ConVarType::UInt64) == EConVarType_UInt64);
22static_assert(static_cast<int>(ConVarType::Float32) == EConVarType_Float32);
23static_assert(static_cast<int>(ConVarType::Float64) == EConVarType_Float64);
24static_assert(static_cast<int>(ConVarType::String) == EConVarType_String);
25static_assert(static_cast<int>(ConVarType::Color) == EConVarType_Color);
26static_assert(static_cast<int>(ConVarType::Vector2) == EConVarType_Vector2);
27static_assert(static_cast<int>(ConVarType::Vector3) == EConVarType_Vector3);
28static_assert(static_cast<int>(ConVarType::Vector4) == EConVarType_Vector4);
29static_assert(static_cast<int>(ConVarType::QAngle) == EConVarType_Qangle);
30static_assert(static_cast<int>(ConVarType::VectorWS) == EConVarType_VectorWS);
31
32template <ConVarValue T>
33Result<ConVar<T>> ConVars::Find(std::string_view name)
34{
35 const std::string owned(name);
36 ConVarRefAbstract ref(owned.c_str());
37 if (!ref.IsValidRef() || !ref.IsConVarDataAvailable())
38 {
39 return std::unexpected(Error::NotFound(std::format("no convar '{}'", owned)));
40 }
41
42 const auto type = static_cast<ConVarType>(ref.GetType());
43 if (!ConVarTypeMatches<T>(type))
44 {
45 return std::unexpected(Error::Invalid(
46 std::format("convar '{}' is engine type {}, not the requested one", owned, static_cast<int>(type))));
47 }
48
49 CVValue_t* storage = ref.GetConVarData()->Value(CSplitScreenSlot(0));
50 if (!storage)
51 {
52 return std::unexpected(Error::Engine(std::format("convar '{}' has no value storage", owned)));
53 }
54
55 ConVar<T> handle;
56 handle._service = this;
57 handle._name = owned;
58 handle._storage = storage;
59 handle._type = ref.GetType();
60 return handle;
61}
62
63template <class T>
65{
66 if (!_storage)
67 {
68 return T{};
69 }
70
71 const auto type = static_cast<ConVarType>(_type);
72
73 if constexpr (std::is_same_v<T, bool>)
74 {
75 return _storage->m_bValue;
76 }
77 else if constexpr (std::is_same_v<T, std::string>)
78 {
79 const char* text = _storage->m_StringValue.Get();
80 return text ? std::string(text) : std::string{};
81 }
82 else if constexpr (std::is_same_v<T, float>)
83 {
84 return _storage->m_fl32Value;
85 }
86 else
87 {
88 // Read the declared width to avoid adjacent bytes.
89 switch (type)
90 {
92 return _storage->m_i16Value;
94 return _storage->m_u16Value;
95 default:
96 return _storage->m_i32Value;
97 }
98 }
99}
100
101template <class T>
103{
104 if (!_storage || !_service)
105 {
106 return std::unexpected(Error::NotReady("convar handle is unresolved"));
107 }
108
109 // ConVarText renders unquoted: the console path quotes in SetByConsole, while the
110 // replicated payload at SendToClient below must stay unquoted.
111 _service->SetByConsole(_name, ConVarText(value));
112 return {};
113}
114
115template <class T>
116void ConVar<T>::SetRaw(const T& value)
117 requires RawConVarValue<T>
118{
119 if (!_storage)
120 {
121 return;
122 }
123
124 const auto type = static_cast<ConVarType>(_type);
125 if constexpr (std::is_same_v<T, bool>)
126 {
127 _storage->m_bValue = value;
128 }
129 else if constexpr (std::is_same_v<T, float>)
130 {
131 _storage->m_fl32Value = value;
132 }
133 else
134 {
135 switch (type)
136 {
138 _storage->m_i16Value = static_cast<int16_t>(value);
139 break;
141 _storage->m_u16Value = static_cast<uint16_t>(value);
142 break;
143 default:
144 _storage->m_i32Value = value;
145 break;
146 }
147 }
148}
149
150template <class T>
151void ConVar<T>::SendTo(int slot, const T& value) const
152{
153 if (_storage && _service)
154 {
155 _service->SendToClient(slot, _name, ConVarText(value));
156 }
157}
158
159template <class T>
161 requires RawConVarValue<T>
162 : _cvar(cvar ? &cvar : nullptr), _previous(cvar.Get())
163{
164 cvar.SetRaw(value);
165}
166
167template <class T>
169{
170 if (_cvar)
171 {
172 _cvar->SetRaw(_previous);
173 }
174}
175
176// The templates above are defined only here, so these instantiations are what makes the handles
177// exist. The lists mirror the ConVarValue and RawConVarValue concepts; a type in a concept but not
178// here is a link error at the consumer rather than a silently different handle.
179template class ConVar<bool>;
180template class ConVar<int>;
181template class ConVar<float>;
182template class ConVar<std::string>;
183template class ConVarRawScope<bool>;
184template class ConVarRawScope<int>;
185template class ConVarRawScope<float>;
186template Result<ConVar<bool>> ConVars::Find<bool>(std::string_view);
187template Result<ConVar<int>> ConVars::Find<int>(std::string_view);
188template Result<ConVar<float>> ConVars::Find<float>(std::string_view);
190
191} // namespace VoltMod
ConVarRawScope(ConVar< T > &cvar, const T &value)
Definition ConVar.cpp:160
T Get() const
Definition ConVar.cpp:64
Status Set(const T &value)
Definition ConVar.cpp:102
void SendTo(int slot, const T &value) const
Definition ConVar.cpp:151
Result< ConVar< T > > Find(std::string_view name)
Definition ConVar.cpp:33
friend class ConVar
Definition ConVar.hpp:149
std::string ConVarText(const T &value)
std::expected< void, Error > Status
Definition Result.hpp:67
std::expected< T, Error > Result
Definition Result.hpp:64
bool ConVarTypeMatches(ConVarType type)
static Error Invalid(std::string detail)
Definition Result.hpp:51
static Error Engine(std::string detail)
Definition Result.hpp:58
static Error NotFound(std::string detail)
Definition Result.hpp:49
static Error NotReady(std::string detail)
Definition Result.hpp:50