11/* *
22 * =============================================================================
33 * CS2Fixes
4- * Copyright (C) 2023 Source2ZE
4+ * Copyright (C) 2023-2025 Source2ZE
55 * =============================================================================
66 *
77 * This program is free software; you can redistribute it and/or modify it under
1919
2020#include " schema.h"
2121
22- #include " schemasystem/schemasystem.h"
23- #include " tier1/utlmap.h"
24- #include " tier0/memdbgon.h"
25- #include " ../utils/plat.h"
2622#include " entity/cbaseentity.h"
23+ #include " ../utils/plat.h"
24+ #include " schemasystem/schemasystem.h"
2725
28- extern CSchemaSystem *g_pSchemaSystem2;
29- extern CGlobalVars *gpGlobals;
30-
31- using SchemaKeyValueMap_t = CUtlMap<uint32_t , SchemaKey>;
32- using SchemaTableMap_t = CUtlMap<uint32_t , SchemaKeyValueMap_t*>;
26+ #include " tier0/memdbgon.h"
3327
28+ using SchemaKeyValueMap_t = std::map<uint32_t , SchemaKey>;
29+ using SchemaTableMap_t = std::map<uint32_t , SchemaKeyValueMap_t>;
3430
3531static bool IsFieldNetworked (SchemaClassFieldData_t& field)
3632{
37- for (int i = 0 ; i < field.m_nStaticMetadataCount ; i++)
38- {
39- static auto networkEnabled = hash_32_fnv1a_const (" MNetworkEnable" );
40- if (networkEnabled == hash_32_fnv1a_const (field.m_pStaticMetadata [i].m_pszName ))
41- return true ;
42- }
43-
44- return false ;
45- }
33+ for (int i = 0 ; i < field.m_nStaticMetadataCount ; i++)
34+ {
35+ static auto networkEnabled = hash_32_fnv1a_const (" MNetworkEnable" );
36+ if (networkEnabled == hash_32_fnv1a_const (field.m_pStaticMetadata [i].m_pszName ))
37+ return true ;
38+ }
39+
40+ return false ;
41+ }
4642
47- static bool InitSchemaFieldsForClass (SchemaTableMap_t * tableMap, const char * className, uint32_t classKey)
43+ static bool InitSchemaFieldsForClass (SchemaTableMap_t& tableMap, const char * className, uint32_t classKey)
4844{
49- CSchemaSystemTypeScope* pType = g_pSchemaSystem2 ->FindTypeScopeForModule (MODULE_PREFIX " server" MODULE_EXT);
45+ CSchemaSystemTypeScope* pType = g_pSchemaSystem ->FindTypeScopeForModule (MODULE_PREFIX " server" MODULE_EXT);
5046
51- if (!pType)
52- return false ;
47+ if (!pType)
48+ return false ;
5349
54- SchemaClassInfoData_t * pClassInfo = pType->FindDeclaredClass (className).Get ();
50+ SchemaClassInfoData_t* pClassInfo = pType->FindDeclaredClass (className).Get ();
5551
56- if (!pClassInfo)
57- {
58- SchemaKeyValueMap_t * map = new SchemaKeyValueMap_t ( 0 , 0 , DefLessFunc ( uint32_t )) ;
59- tableMap-> Insert ( classKey, map);
52+ if (!pClassInfo)
53+ {
54+ SchemaKeyValueMap_t map;
55+ tableMap. insert ( std::make_pair ( classKey, map) );
6056
61- Warning (" InitSchemaFieldsForClass(): '%s' was not found!\n " , className);
62- return false ;
63- }
57+ Warning (" InitSchemaFieldsForClass(): '%s' was not found!\n " , className);
58+ return false ;
59+ }
6460
65- short fieldsSize = pClassInfo->m_nFieldCount ;
66- SchemaClassFieldData_t* pFields = pClassInfo->m_pFields ;
61+ short fieldsSize = pClassInfo->m_nFieldCount ;
62+ SchemaClassFieldData_t* pFields = pClassInfo->m_pFields ;
6763
68- SchemaKeyValueMap_t *keyValueMap = new SchemaKeyValueMap_t (0 , 0 , DefLessFunc (uint32_t ));
69- keyValueMap->EnsureCapacity (fieldsSize);
70- tableMap->Insert (classKey, keyValueMap);
64+ SchemaKeyValueMap_t &keyValueMap = tableMap.insert (std::make_pair (classKey, SchemaKeyValueMap_t ())).first ->second ;
7165
72- for (int i = 0 ; i < fieldsSize; ++i)
73- {
74- SchemaClassFieldData_t& field = pFields[i];
66+ for (int i = 0 ; i < fieldsSize; ++i)
67+ {
68+ SchemaClassFieldData_t& field = pFields[i];
7569
7670#ifdef _DEBUG
77- Message (" %s::%s found at -> 0x%X - %llx\n " , className, field.m_name , field.m_single_inheritance_offset , &field);
71+ Message (" %s::%s found at -> 0x%X - %llx\n " , className, field.m_pszName , field.m_nSingleInheritanceOffset , &field);
7872#endif
7973
80- keyValueMap->Insert (hash_32_fnv1a_const (field.m_pszName ), {field.m_nSingleInheritanceOffset , IsFieldNetworked (field)});
81- }
74+ std::pair<uint32_t , SchemaKey> keyValuePair;
75+ keyValuePair.first = hash_32_fnv1a_const (field.m_pszName );
76+ keyValuePair.second .offset = field.m_nSingleInheritanceOffset ;
77+ keyValuePair.second .networked = IsFieldNetworked (field);
78+
79+ keyValueMap.insert (keyValuePair);
80+ }
8281
83- return true ;
82+ return true ;
8483}
8584
8685int16_t schema::FindChainOffset (const char * className)
8786{
88- CSchemaSystemTypeScope* pType = g_pSchemaSystem2 ->FindTypeScopeForModule (MODULE_PREFIX " server" MODULE_EXT);
87+ CSchemaSystemTypeScope* pType = g_pSchemaSystem ->FindTypeScopeForModule (MODULE_PREFIX " server" MODULE_EXT);
8988
90- if (!pType)
91- return false ;
89+ if (!pType)
90+ return false ;
9291
93- SchemaClassInfoData_t* pClassInfo = pType->FindDeclaredClass (className).Get ();
92+ SchemaClassInfoData_t* pClassInfo = pType->FindDeclaredClass (className).Get ();
9493
95- do
96- {
97- SchemaClassFieldData_t* pFields = pClassInfo->m_pFields ;
98- short fieldsSize = pClassInfo->m_nFieldCount ;
99- for (int i = 0 ; i < fieldsSize; ++i)
100- {
101- SchemaClassFieldData_t& field = pFields[i];
94+ do
95+ {
96+ SchemaClassFieldData_t* pFields = pClassInfo->m_pFields ;
97+ short fieldsSize = pClassInfo->m_nFieldCount ;
98+ for (int i = 0 ; i < fieldsSize; ++i)
99+ {
100+ SchemaClassFieldData_t& field = pFields[i];
102101
103- if (V_strcmp (field.m_pszName , " __m_pChainEntity" ) == 0 )
104- {
105- return field.m_nSingleInheritanceOffset ;
106- }
107- }
108- } while ((pClassInfo = pClassInfo->m_pBaseClasses ? pClassInfo->m_pBaseClasses ->m_pClass : nullptr ) != nullptr );
102+ if (V_strcmp (field.m_pszName , " __m_pChainEntity" ) == 0 )
103+ return field.m_nSingleInheritanceOffset ;
104+ }
105+ } while ((pClassInfo = pClassInfo->m_pBaseClasses ? pClassInfo->m_pBaseClasses ->m_pClass : nullptr ) != nullptr );
109106
110- return 0 ;
107+ return 0 ;
111108}
112109
113110SchemaKey schema::GetOffset (const char * className, uint32_t classKey, const char * memberName, uint32_t memberKey)
114111{
115- static SchemaTableMap_t schemaTableMap ( 0 , 0 , DefLessFunc ( uint32_t )) ;
116- int16_t tableMapIndex = schemaTableMap. Find (classKey);
117- if (!schemaTableMap.IsValidIndex (tableMapIndex ))
118- {
119- if (InitSchemaFieldsForClass (& schemaTableMap, className, classKey))
120- return GetOffset (className, classKey, memberName, memberKey);
121-
122- return { 0 , 0 };
123- }
124-
125- SchemaKeyValueMap_t * tableMap = schemaTableMap[tableMapIndex ];
126- int16_t memberIndex = tableMap-> Find (memberKey);
127- if (!tableMap-> IsValidIndex (memberIndex ))
128- {
129- Warning (" schema::GetOffset(): '%s' was not found in '%s'!\n " , memberName, className);
130- return { 0 , 0 };
131- }
132-
133- return tableMap-> Element (memberIndex) ;
112+ static SchemaTableMap_t schemaTableMap;
113+
114+ if (!schemaTableMap.contains (classKey ))
115+ {
116+ if (InitSchemaFieldsForClass (schemaTableMap, className, classKey))
117+ return GetOffset (className, classKey, memberName, memberKey);
118+
119+ return {0 , 0 };
120+ }
121+
122+ SchemaKeyValueMap_t tableMap = schemaTableMap[classKey ];
123+
124+ if (!tableMap. contains (memberKey ))
125+ {
126+ Warning (" schema::GetOffset(): '%s' was not found in '%s'!\n " , memberName, className);
127+ return {0 , 0 };
128+ }
129+
130+ return tableMap[memberKey] ;
134131}
0 commit comments