-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschemasystem.cpp
More file actions
224 lines (176 loc) · 7.21 KB
/
schemasystem.cpp
File metadata and controls
224 lines (176 loc) · 7.21 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* =============================================================================
* CS2Fixes
* Copyright (C) 2023-2025 Source2ZE
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "schemasystem.h"
#include "platform.h"
#include "schemasystem/schemasystem.h"
#include "tier1/utlmap.h"
#include "tier0/memdbgon.h"
#include <edict.h>
#include <CBaseEntity.h>
#ifdef _WIN32
#define MODULE_PREFIX ""
#define MODULE_EXT ".dll"
#else
#define MODULE_PREFIX "lib"
#define MODULE_EXT ".so"
#endif
using SchemaKeyValueMap_t = std::map<uint32_t, SchemaKey>;
using SchemaTableMap_t = std::map<uint32_t, SchemaKeyValueMap_t>;
static constexpr uint32_t g_ChainKey = hash_32_fnv1a_const("__m_pChainEntity");
static bool IsFieldNetworked(SchemaClassFieldData_t& field)
{
for (int i = 0; i < field.m_nStaticMetadataCount; i++)
{
static auto networkEnabled = hash_32_fnv1a_const("MNetworkEnable");
if (networkEnabled == hash_32_fnv1a_const(field.m_pStaticMetadata[i].m_pszName))
return true;
}
return false;
}
// Try to recursively find __m_pChainEntity in base classes
// (e.g. CCSGameRules -> CTeamplayRules -> CMultiplayRules -> CGameRules, in this case it's in CGameRules)
static void InitChainOffset(SchemaClassInfoData_t *pClassInfo, SchemaKeyValueMap_t &keyValueMap)
{
short fieldsSize = pClassInfo->m_nFieldCount;
SchemaClassFieldData_t* pFields = pClassInfo->m_pFields;
for (int i = 0; i < fieldsSize; ++i)
{
SchemaClassFieldData_t& field = pFields[i];
if (hash_32_fnv1a_const(field.m_pszName) != g_ChainKey)
continue;
std::pair<uint32_t, SchemaKey> keyValuePair;
keyValuePair.first = g_ChainKey;
keyValuePair.second.offset = field.m_nSingleInheritanceOffset;
keyValuePair.second.networked = IsFieldNetworked(field);
keyValueMap.insert(keyValuePair);
return;
}
// Not the base class yet, keep looking
if (pClassInfo->m_nBaseClassCount)
return InitChainOffset(pClassInfo->m_pBaseClasses[0].m_pClass, keyValueMap);
}
static void InitSchemaKeyValueMap(SchemaClassInfoData_t *pClassInfo, SchemaKeyValueMap_t& keyValueMap)
{
short fieldsSize = pClassInfo->m_nFieldCount;
SchemaClassFieldData_t* pFields = pClassInfo->m_pFields;
for (int i = 0; i < fieldsSize; ++i)
{
SchemaClassFieldData_t& field = pFields[i];
std::pair<uint32_t, SchemaKey> keyValuePair;
keyValuePair.first = hash_32_fnv1a_const(field.m_pszName);
keyValuePair.second.offset = field.m_nSingleInheritanceOffset;
keyValuePair.second.networked = IsFieldNetworked(field);
keyValueMap.insert(keyValuePair);
}
// If this is a child class there might be a parent class with __m_pChainEntity
if (keyValueMap.find(g_ChainKey) == keyValueMap.end() && pClassInfo->m_nBaseClassCount)
InitChainOffset(pClassInfo->m_pBaseClasses[0].m_pClass, keyValueMap);
}
static bool InitSchemaFieldsForClass(SchemaTableMap_t& tableMap, const char* className, uint32_t classKey)
{
CSchemaSystemTypeScope* pType = g_pSchemaSystem->FindTypeScopeForModule(MODULE_PREFIX "server" MODULE_EXT);
if (!pType)
return false;
SchemaClassInfoData_t* pClassInfo = pType->FindDeclaredClass(className).Get();
if (!pClassInfo)
{
SchemaKeyValueMap_t map;
tableMap.insert(std::make_pair(classKey, map));
Warning("InitSchemaFieldsForClass(): '%s' was not found!\n", className);
return false;
}
SchemaKeyValueMap_t& keyValueMap = tableMap.insert(std::make_pair(classKey, SchemaKeyValueMap_t())).first->second;
InitSchemaKeyValueMap(pClassInfo, keyValueMap);
return true;
}
int16_t schema::FindChainOffset(const char* className, uint32_t classNameHash)
{
return schema::GetOffset(className, classNameHash, "__m_pChainEntity", g_ChainKey).offset;
}
int16_t schema::FindChainOffset(const char* className)
{
CSchemaSystemTypeScope* pType = g_pSchemaSystem->FindTypeScopeForModule(MODULE_PREFIX "server" MODULE_EXT);
if (!pType)
return false;
SchemaClassInfoData_t* pClassInfo = pType->FindDeclaredClass(className).Get();
do
{
SchemaClassFieldData_t* pFields = pClassInfo->m_pFields;
short fieldsSize = pClassInfo->m_nFieldCount;
for (int i = 0; i < fieldsSize; ++i)
{
SchemaClassFieldData_t& field = pFields[i];
if (V_strcmp(field.m_pszName, "__m_pChainEntity") == 0)
{
return field.m_nSingleInheritanceOffset;
}
}
} while ((pClassInfo = pClassInfo->m_pBaseClasses ? pClassInfo->m_pBaseClasses->m_pClass : nullptr) != nullptr);
return 0;
}
SchemaKey schema::GetOffset(const char* className, uint32_t classKey, const char* memberName, uint32_t memberKey)
{
static SchemaTableMap_t schemaTableMap;
if (schemaTableMap.find(classKey) == schemaTableMap.end())
{
if (InitSchemaFieldsForClass(schemaTableMap, className, classKey))
return GetOffset(className, classKey, memberName, memberKey);
return {0, 0};
}
SchemaKeyValueMap_t tableMap = schemaTableMap[classKey];
if (tableMap.find(memberKey) == tableMap.end())
{
if (memberKey != g_ChainKey)
Warning("schema::GetOffset(): '%s' was not found in '%s'!\n", memberName, className);
return {0, 0};
}
return tableMap[memberKey];
}
int32_t schema::GetServerOffset(const char* pszClassName, const char* pszPropName)
{
SchemaClassInfoData_t* pClassInfo = g_pSchemaSystem->FindTypeScopeForModule(MODULE_PREFIX "server" MODULE_EXT)->FindDeclaredClass(pszClassName).Get();
if (pClassInfo)
{
for (int i = 0; i < pClassInfo->m_nFieldCount; i++)
{
auto& pFieldData = pClassInfo->m_pFields[i];
if (std::strcmp(pFieldData.m_pszName, pszPropName) == 0)
{
return pFieldData.m_nSingleInheritanceOffset;
}
}
}
return -1;
}
void NetworkVarStateChanged(uintptr_t pNetworkVar, uint32_t nOffset, uint32 nNetworkStateChangedOffset)
{
NetworkStateChangedData data(nOffset);
CALL_VIRTUAL(void, nNetworkStateChangedOffset, (void*)pNetworkVar, &data);
}
void EntityNetworkStateChanged(uintptr_t pEntity, uint nOffset)
{
NetworkStateChangedData data(nOffset);
reinterpret_cast<CEntityInstance*>(pEntity)->NetworkStateChanged(NetworkStateChangedData(nOffset));
}
void ChainNetworkStateChanged(uintptr_t pNetworkVarChainer, uint nLocalOffset)
{
CEntityInstance* pEntity = reinterpret_cast<CNetworkVarChainer*>(pNetworkVarChainer)->m_pEntity;
if (pEntity)
pEntity->NetworkStateChanged(NetworkStateChangedData(nLocalOffset, -1, reinterpret_cast<CNetworkVarChainer*>(pNetworkVarChainer)->m_PathIndex));
}