-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpoofedConvar.cpp
202 lines (179 loc) · 4.86 KB
/
SpoofedConvar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "SDK.h"
#include "MiscClasses.h"
void ConVar::SetValue(const char* value)
{
typedef void(__thiscall* OriginalFn)(void*, const char*);
return call_vfunc<OriginalFn>(this, 14)(this, value);
}
void ConVar::SetValue(float value)
{
typedef void(__thiscall* OriginalFn)(void*, float);
return call_vfunc<OriginalFn>(this, 15)(this, value);
}
void ConVar::SetValue(int value)
{
typedef void(__thiscall* OriginalFn)(void*, int);
return call_vfunc<OriginalFn>(this, 16)(this, value);
}
float ConVar::GetFloat(void) const {
return pParent->fValue;
}
int ConVar::GetInt(void) const {
return pParent->nValue;
}
const char* ConVar::GetString(void) const {
return pParent->pszDefaultValue;
}
Color ConVar::GetColor(void) const {
unsigned char *pColorElement = ((unsigned char *)&pParent->nValue);
return Color(pColorElement[0], pColorElement[1], pColorElement[2], pColorElement[3]);
}
void ConVar::SetValue(Color value)
{
typedef void(__thiscall* OriginalFn)(void*, Color);
return call_vfunc<OriginalFn>(this, 17)(this, value);
}
char* ConVar::GetName()
{
typedef char*(__thiscall* OriginalFn)(void*);
return call_vfunc<OriginalFn>(this, 5)(this);
}
char* ConVar::GetDefault()
{
return pszDefaultValue;
}
template<typename T>
inline void MinspecCvar::SetValue(T value)
{
m_pConVar->SetValue(T);
}
MinspecCvar::MinspecCvar(const char* szCVar, char* newname, float newvalue) : m_pConVar(nullptr)
{
m_pConVar = Interfaces::CVar->FindVar(szCVar);
m_newvalue = newvalue;
m_szReplacementName = newname;
Spoof();
}
MinspecCvar::~MinspecCvar()
{
if (ValidCvar())
{
Interfaces::CVar->UnregisterConCommand(m_pConVar);
m_pConVar->pszName = m_szOriginalName;
m_pConVar->SetValue(m_OriginalValue);
Interfaces::CVar->RegisterConCommand(m_pConVar);
}
}
bool MinspecCvar::ValidCvar()
{
return m_pConVar != nullptr;
}
void MinspecCvar::Spoof()
{
if (ValidCvar())
{
Interfaces::CVar->UnregisterConCommand(m_pConVar);
m_szOriginalName = m_pConVar->pszName;
m_OriginalValue = m_pConVar->GetFloat();
m_pConVar->pszName = m_szReplacementName;
Interfaces::CVar->RegisterConCommand(m_pConVar);
m_pConVar->SetValue(m_newvalue);
}
}
int MinspecCvar::GetInt()
{
if (ValidCvar()) {
return m_pConVar->GetInt();
}
return 0;
}
float MinspecCvar::GetFloat()
{
if (ValidCvar()) {
return m_pConVar->GetFloat();
}
return 0.0f;
}
const char* MinspecCvar::GetString()
{
if (ValidCvar()) {
return m_pConVar->GetString();
}
return nullptr;
}
SpoofedConvar::SpoofedConvar(const char* szCVar) {
m_pOriginalCVar = Interfaces::CVar->FindVar(szCVar);
Spoof();
}
SpoofedConvar::SpoofedConvar(ConVar* pCVar) {
m_pOriginalCVar = pCVar;
Spoof();
}
SpoofedConvar::~SpoofedConvar() {
if (IsSpoofed()) {
DWORD dwOld;
SetFlags(m_iOriginalFlags);
SetString(m_szOriginalValue);
VirtualProtect((LPVOID)m_pOriginalCVar->pszName, 128, PAGE_READWRITE, &dwOld);
strcpy((char*)m_pOriginalCVar->pszName, m_szOriginalName);
VirtualProtect((LPVOID)m_pOriginalCVar->pszName, 128, dwOld, &dwOld);
//Unregister dummy cvar
Interfaces::CVar->UnregisterConCommand(m_pDummyCVar);
free(m_pDummyCVar);
m_pDummyCVar = NULL;
}
}
bool SpoofedConvar::IsSpoofed() {
return m_pDummyCVar != NULL;
}
void SpoofedConvar::Spoof() {
if (!IsSpoofed() && m_pOriginalCVar) {
//Save old name value and flags so we can restore the cvar lates if needed
m_iOriginalFlags = m_pOriginalCVar->nFlags;
strcpy(m_szOriginalName, m_pOriginalCVar->pszName);
strcpy(m_szOriginalValue, m_pOriginalCVar->pszDefaultValue);
sprintf_s(m_szDummyName, 128, "d_%s", m_szOriginalName);
//Create the dummy cvar
m_pDummyCVar = (ConVar*)malloc(sizeof(ConVar));
if (!m_pDummyCVar) return;
memcpy(m_pDummyCVar, m_pOriginalCVar, sizeof(ConVar));
m_pDummyCVar->pNext = NULL;
//Register it
Interfaces::CVar->RegisterConCommand(m_pDummyCVar);
//Fix "write access violation" bullshit
DWORD dwOld;
VirtualProtect((LPVOID)m_pOriginalCVar->pszName, 128, PAGE_READWRITE, &dwOld);
//Rename the cvar
strcpy((char*)m_pOriginalCVar->pszName, m_szDummyName);
VirtualProtect((LPVOID)m_pOriginalCVar->pszName, 128, dwOld, &dwOld);
SetFlags(FCVAR_NONE);
}
}
void SpoofedConvar::SetFlags(int flags) {
if (IsSpoofed()) {
m_pOriginalCVar->nFlags = flags;
}
}
int SpoofedConvar::GetFlags() {
return m_pOriginalCVar->nFlags;
}
void SpoofedConvar::SetInt(int iValue) {
if (IsSpoofed()) {
m_pOriginalCVar->SetValue(iValue);
}
}
void SpoofedConvar::SetBool(bool bValue) {
if (IsSpoofed()) {
m_pOriginalCVar->SetValue(bValue);
}
}
void SpoofedConvar::SetFloat(float flValue) {
if (IsSpoofed()) {
m_pOriginalCVar->SetValue(flValue);
}
}
void SpoofedConvar::SetString(const char* szValue) {
if (IsSpoofed()) {
m_pOriginalCVar->SetValue(szValue);
}
}