7#include <tier0/convar.h>
32template <ConVarValue T>
35 const std::string owned(name);
36 ConVarRefAbstract ref(owned.c_str());
37 if (!ref.IsValidRef() || !ref.IsConVarDataAvailable())
39 return std::unexpected(
Error::NotFound(std::format(
"no convar '{}'", owned)));
42 const auto type =
static_cast<ConVarType>(ref.GetType());
46 std::format(
"convar '{}' is engine type {}, not the requested one", owned,
static_cast<int>(type))));
49 CVValue_t* storage = ref.GetConVarData()->Value(CSplitScreenSlot(0));
52 return std::unexpected(
Error::Engine(std::format(
"convar '{}' has no value storage", owned)));
56 handle._service =
this;
58 handle._storage = storage;
59 handle._type = ref.GetType();
71 const auto type =
static_cast<ConVarType>(_type);
73 if constexpr (std::is_same_v<T, bool>)
75 return _storage->m_bValue;
77 else if constexpr (std::is_same_v<T, std::string>)
79 const char* text = _storage->m_StringValue.Get();
80 return text ? std::string(text) : std::string{};
82 else if constexpr (std::is_same_v<T, float>)
84 return _storage->m_fl32Value;
92 return _storage->m_i16Value;
94 return _storage->m_u16Value;
96 return _storage->m_i32Value;
104 if (!_storage || !_service)
106 return std::unexpected(
Error::NotReady(
"convar handle is unresolved"));
111 _service->SetByConsole(_name,
ConVarText(value));
116void ConVar<T>::SetRaw(
const T& value)
124 const auto type =
static_cast<ConVarType>(_type);
125 if constexpr (std::is_same_v<T, bool>)
127 _storage->m_bValue = value;
129 else if constexpr (std::is_same_v<T, float>)
131 _storage->m_fl32Value = value;
138 _storage->m_i16Value =
static_cast<int16_t
>(value);
141 _storage->m_u16Value =
static_cast<uint16_t
>(value);
144 _storage->m_i32Value = value;
153 if (_storage && _service)
155 _service->SendToClient(slot, _name,
ConVarText(value));
162 : _cvar(cvar ? &cvar :
nullptr), _previous(cvar.Get())
172 _cvar->SetRaw(_previous);
ConVarRawScope(ConVar< T > &cvar, const T &value)
Status Set(const T &value)
void SendTo(int slot, const T &value) const
Result< ConVar< T > > Find(std::string_view name)
std::string ConVarText(const T &value)
std::expected< void, Error > Status
std::expected< T, Error > Result
bool ConVarTypeMatches(ConVarType type)
static Error Invalid(std::string detail)
static Error Engine(std::string detail)
static Error NotFound(std::string detail)
static Error NotReady(std::string detail)