VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
ConVarTypes.cpp
Go to the documentation of this file.
2
4#include <format>
5#include <string>
6#include <type_traits>
7
8namespace VoltMod
9{
10
11template <class T>
13{
14 // Only widths that round-trip through T. Accepting UInt32/Int64/UInt64 as int, or Float64
15 // as float, made Find() succeed and then silently narrow on every Get - a stronger promise
16 // than the handle could keep. A convar of an unrepresentable width now fails to resolve,
17 // with the type mismatch named, instead of reading wrong.
18 if constexpr (std::is_same_v<T, bool>)
19 {
20 return type == ConVarType::Bool;
21 }
22 else if constexpr (std::is_same_v<T, int>)
23 {
24 return type == ConVarType::Int16 || type == ConVarType::UInt16 || type == ConVarType::Int32;
25 }
26 else if constexpr (std::is_same_v<T, float>)
27 {
28 return type == ConVarType::Float32;
29 }
30 else
31 {
32 return type == ConVarType::String;
33 }
34}
35
40
41template <>
42std::string ConVarText<bool>(const bool& value)
43{
44 return value ? "1" : "0";
45}
46
47template <>
48std::string ConVarText<int>(const int& value)
49{
50 return std::format("{}", value);
51}
52
53template <>
54std::string ConVarText<float>(const float& value)
55{
56 return std::format("{}", value);
57}
58
59template <>
60std::string ConVarText<std::string>(const std::string& value)
61{
62 // Raw on purpose. This is also the replicated netmessage payload, which the client's own
63 // parser reads verbatim, and the value ConVarOverrides snapshots. Console quoting happens
64 // at the command boundary instead - see ConVar<T>::Set and ConVarOverrides::Write.
65 return value;
66}
67
68} // namespace VoltMod
std::string ConVarText< std::string >(const std::string &value)
std::string ConVarText< int >(const int &value)
template bool ConVarTypeMatches< float >(ConVarType)
template bool ConVarTypeMatches< std::string >(ConVarType)
template bool ConVarTypeMatches< int >(ConVarType)
template bool ConVarTypeMatches< bool >(ConVarType)
std::string ConVarText< float >(const float &value)
std::string ConVarText< bool >(const bool &value)
bool ConVarTypeMatches(ConVarType type)