1+ #include " convars.h"
2+
3+ static void convarsCallback (const CCommandContext& context, const CCommand& args);
4+ std::map<std::string, bool > convarCreated;
5+
6+ FakeConVar::FakeConVar (std::string name, EConVarType type, std::any defaultValue, bool prot)
7+ {
8+ if (convarCreated.find (name) == convarCreated.end ())
9+ {
10+ convarCreated.insert ({ name, true });
11+
12+ ConCommandRefAbstract convarRef;
13+ new ConCommand (&convarRef, name.c_str (), convarsCallback, " Swiftly ConVar" , FCVAR_LINKED_CONCOMMAND | FCVAR_SPONLY | (prot ? FCVAR_PROTECTED : FCVAR_NONE));
14+ }
15+
16+ m_value = defaultValue;
17+ m_type = type;
18+ m_name = name;
19+ }
20+
21+ FakeConVar::~FakeConVar ()
22+ {
23+ }
24+
25+ EConVarType FakeConVar::GetType ()
26+ {
27+ return this ->m_type ;
28+ }
29+
30+ void FakeConVar::SetValue (std::any value)
31+ {
32+ this ->m_value = value;
33+ }
34+
35+ std::any FakeConVar::GetValue ()
36+ {
37+ return this ->m_value ;
38+ }
39+
40+ static void convarsCallback (const CCommandContext& context, const CCommand& args)
41+ {
42+ CCommand tokenizedArgs;
43+ tokenizedArgs.Tokenize (args.GetCommandString ());
44+
45+ std::string cvar = tokenizedArgs[0 ];
46+ if (fakeConvars.find (cvar) == fakeConvars.end ()) return ;
47+
48+ auto convar = fakeConvars.at (cvar);
49+
50+ if (args.ArgC () < 2 ) {
51+ std::string convarOutput = string_format (" %s {VALUE}\n " , cvar.c_str ());
52+ std::string value = " " ;
53+ if (convar->GetType () == EConVarType_Int16)
54+ value = std::to_string (std::any_cast<int16_t >(convar->GetValue ()));
55+ else if (convar->GetType () == EConVarType_UInt16)
56+ value = std::to_string (std::any_cast<uint16_t >(convar->GetValue ()));
57+ else if (convar->GetType () == EConVarType_UInt32)
58+ value = std::to_string (std::any_cast<uint32_t >(convar->GetValue ()));
59+ else if (convar->GetType () == EConVarType_Int32)
60+ value = std::to_string (std::any_cast<int32_t >(convar->GetValue ()));
61+ else if (convar->GetType () == EConVarType_UInt64)
62+ value = std::to_string (std::any_cast<uint64_t >(convar->GetValue ()));
63+ else if (convar->GetType () == EConVarType_Int64)
64+ value = std::to_string (std::any_cast<int64_t >(convar->GetValue ()));
65+ else if (convar->GetType () == EConVarType_Bool)
66+ value = (std::any_cast<bool >(convar->GetValue ()) ? " true" : " false" );
67+ else if (convar->GetType () == EConVarType_Float32)
68+ value = std::to_string (std::any_cast<float >(convar->GetValue ()));
69+ else if (convar->GetType () == EConVarType_Float64)
70+ value = std::to_string (std::any_cast<double >(convar->GetValue ()));
71+ else if (convar->GetType () == EConVarType_String)
72+ value = std::any_cast<std::string>(convar->GetValue ());
73+ else if (convar->GetType () == EConVarType_Color) {
74+ Color col = std::any_cast<Color>(convar->GetValue ());
75+ value = string_format (" %d,%d,%d,%d" , col.r (), col.g (), col.b (), col.a ());
76+ }
77+ else if (convar->GetType () == EConVarType_Vector2) {
78+ Vector2D vec = std::any_cast<Vector2D>(convar->GetValue ());
79+ value = string_format (" %f,%f" , vec.x , vec.y );
80+ }
81+ else if (convar->GetType () == EConVarType_Vector3) {
82+ Vector vec = std::any_cast<Vector>(convar->GetValue ());
83+ value = string_format (" %f,%f,%f" , vec.x , vec.y , vec.z );
84+ }
85+ else if (convar->GetType () == EConVarType_Vector4) {
86+ Vector4D vec = std::any_cast<Vector4D>(convar->GetValue ());
87+ value = string_format (" %f,%f,%f,%f" , vec.x , vec.y , vec.z , vec.w );
88+ }
89+ else if (convar->GetType () == EConVarType_Qangle) {
90+ QAngle ang = std::any_cast<QAngle>(convar->GetValue ());
91+ value = string_format (" %f,%f,%f" , ang.x , ang.y , ang.z );
92+ }
93+ else {
94+ value = " (null)" ;
95+ }
96+
97+ PLUGIN_PRINTF (" ConVar" , " %s %s\n " , cvar.c_str (), value.c_str ());
98+ }
99+ else {
100+ if (convar->GetType () == EConVarType_Int16)
101+ convar->SetValue (V_StringToInt16 (tokenizedArgs[1 ], std::any_cast<int16_t >(convar->GetValue ())));
102+ else if (convar->GetType () == EConVarType_UInt16)
103+ convar->SetValue (V_StringToUint16 (tokenizedArgs[1 ], std::any_cast<uint16_t >(convar->GetValue ())));
104+ else if (convar->GetType () == EConVarType_UInt32)
105+ convar->SetValue (V_StringToUint32 (tokenizedArgs[1 ], std::any_cast<uint32_t >(convar->GetValue ())));
106+ else if (convar->GetType () == EConVarType_Int32)
107+ convar->SetValue (V_StringToInt32 (tokenizedArgs[1 ], std::any_cast<int32_t >(convar->GetValue ())));
108+ else if (convar->GetType () == EConVarType_UInt64)
109+ convar->SetValue (V_StringToUint64 (tokenizedArgs[1 ], std::any_cast<uint64_t >(convar->GetValue ())));
110+ else if (convar->GetType () == EConVarType_Int64)
111+ convar->SetValue (V_StringToInt64 (tokenizedArgs[1 ], std::any_cast<int64_t >(convar->GetValue ())));
112+ else if (convar->GetType () == EConVarType_Bool)
113+ convar->SetValue (V_StringToBool (tokenizedArgs[1 ], std::any_cast<bool >(convar->GetValue ())));
114+ else if (convar->GetType () == EConVarType_Float32)
115+ convar->SetValue (V_StringToFloat32 (tokenizedArgs[1 ], std::any_cast<float >(convar->GetValue ())));
116+ else if (convar->GetType () == EConVarType_Float64)
117+ convar->SetValue (V_StringToFloat64 (tokenizedArgs[1 ], std::any_cast<double >(convar->GetValue ())));
118+ else if (convar->GetType () == EConVarType_String)
119+ convar->SetValue (std::string (tokenizedArgs[1 ]));
120+ else if (convar->GetType () == EConVarType_Color) {
121+ Color col = std::any_cast<Color>(convar->GetValue ());
122+ V_StringToColor (tokenizedArgs[1 ], col);
123+ convar->SetValue (col);
124+ }
125+ else if (convar->GetType () == EConVarType_Vector2) {
126+ Vector2D vec = std::any_cast<Vector2D>(convar->GetValue ());
127+ V_StringToVector2D (tokenizedArgs[1 ], vec);
128+ convar->SetValue (vec);
129+ }
130+ else if (convar->GetType () == EConVarType_Vector3) {
131+ Vector vec = std::any_cast<Vector>(convar->GetValue ());
132+ V_StringToVector (tokenizedArgs[1 ], vec);
133+ convar->SetValue (vec);
134+ }
135+ else if (convar->GetType () == EConVarType_Vector4) {
136+ Vector4D vec = std::any_cast<Vector4D>(convar->GetValue ());
137+ V_StringToVector4D (tokenizedArgs[1 ], vec);
138+ convar->SetValue (vec);
139+ }
140+ else if (convar->GetType () == EConVarType_Qangle) {
141+ QAngle ang = std::any_cast<QAngle>(convar->GetValue ());
142+ V_StringToQAngle (tokenizedArgs[1 ], ang);
143+ convar->SetValue (ang);
144+ }
145+ }
146+ }
0 commit comments