forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcdirtyfield.cpp
190 lines (161 loc) · 4.14 KB
/
gcdirtyfield.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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Network dirty field marker for shared objects
//
//=============================================================================
#include "stdafx.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
namespace GCSDK
{
CSharedObjectDirtyFieldList::CSharedObjectDirtyFieldList( CSharedObject *obj )
: m_obj( obj )
, m_firstFieldBits( 0 )
, m_pExtendedFields( NULL )
{
}
CSharedObjectDirtyFieldList::~CSharedObjectDirtyFieldList()
{
if ( m_pExtendedFields )
{
delete m_pExtendedFields;
}
}
CSharedObject *CSharedObjectDirtyFieldList::Obj() const
{
return m_obj;
}
void CSharedObjectDirtyFieldList::DirtyField( int index )
{
// if the field index fits within our dirty fields struct, set a bit to track it
if ( index < 32 )
{
m_firstFieldBits |= (1 << index);
}
// if the field index is too big, store it in our backup structure; this is less efficient but more
// flexible, especially when dealing with fields that themselves contain flags so would be interpreted
// as huge numbers
else
{
if ( !m_pExtendedFields )
m_pExtendedFields = new CUtlVector<int>;
if ( !m_pExtendedFields->HasElement( index ) )
m_pExtendedFields->AddToTail( index );
}
}
void CSharedObjectDirtyFieldList::GetDirtyFieldSet( CUtlVector<int> &fieldSet ) const
{
fieldSet.Purge();
if( !m_firstFieldBits && !m_pExtendedFields )
{
return;
}
if( m_firstFieldBits )
{
for( int i = 0; i < 32; i++ )
{
// handle dirty bits
if( m_firstFieldBits & ( 1 << i ) )
{
fieldSet.AddToTail( i );
}
}
}
// handle higher order dirty-fields list if present
if ( m_pExtendedFields )
{
fieldSet.AddVectorToTail( *m_pExtendedFields );
}
}
//-----------------------------------------------------------------------------
CSharedObjectDirtyList::CSharedObjectDirtyList()
{
}
CSharedObjectDirtyList::~CSharedObjectDirtyList()
{
}
void CSharedObjectDirtyList::DirtyObjectField( CSharedObject *obj, int field )
{
MEM_ALLOC_CREDIT_CLASS();
Assert( obj != NULL );
if (!obj)
{
return;
}
int index = FindIndexByObj( obj );
if ( index == InvalidIndex() )
{
index = m_sharedObjectDirtyFieldList.AddToTail( CSharedObjectDirtyFieldList( obj ) );
}
m_sharedObjectDirtyFieldList[index].DirtyField( field );
}
int CSharedObjectDirtyList::FindIndexByObj( const CSharedObject *pObj ) const
{
// Slow method for now, we can speed this up with a map later
for ( int i = 0; i < m_sharedObjectDirtyFieldList.Count(); ++i )
{
if( m_sharedObjectDirtyFieldList[i].Obj() == pObj )
{
return i;
}
}
return InvalidIndex();
}
bool CSharedObjectDirtyList::HasElement( const CSharedObject *pObj ) const
{
return FindIndexByObj( pObj ) != InvalidIndex();
}
bool CSharedObjectDirtyList::GetDirtyFieldSetByIndex( int index, CSharedObject **ppObj, CUtlVector<int> &fieldSet ) const
{
VPROF_BUDGET( "CSharedObjectDirtyList::GetDirtyFieldSetByIndex", VPROF_BUDGETGROUP_STEAM );
if( !m_sharedObjectDirtyFieldList.IsValidIndex( index ) )
{
fieldSet.Purge();
if( ppObj )
{
*ppObj = NULL;
}
return false;
}
const CSharedObjectDirtyFieldList &fieldList = m_sharedObjectDirtyFieldList[index];
if( ppObj )
{
*ppObj = fieldList.Obj();
}
fieldList.GetDirtyFieldSet( fieldSet );
return true;
}
bool CSharedObjectDirtyList::GetDirtyFieldSetByObj( CSharedObject *pObj, CUtlVector<int> &fieldSet )
{
int index = FindIndexByObj( pObj );
if ( index == InvalidIndex() )
{
fieldSet.Purge();
return false;
}
CSharedObjectDirtyFieldList &fieldList = m_sharedObjectDirtyFieldList[index];
fieldList.GetDirtyFieldSet( fieldSet );
return true;
}
bool CSharedObjectDirtyList::FindAndRemove( CSharedObject *pObj )
{
int index = FindIndexByObj( pObj );
if ( index == InvalidIndex() )
{
return false;
}
m_sharedObjectDirtyFieldList.Remove( index );
return true;
}
void CSharedObjectDirtyList::RemoveAll()
{
m_sharedObjectDirtyFieldList.RemoveAll();
}
#ifdef DBGFLAG_VALIDATE
void CSharedObjectDirtyList::Validate( CValidator &validator, const char *pchName )
{
VALIDATE_SCOPE();
ValidateObj( m_sharedObjectDirtyFieldList );
}
#endif
} // namespace GCSDK