-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathdmelementdictionary.cpp
More file actions
468 lines (387 loc) · 15.8 KB
/
dmelementdictionary.cpp
File metadata and controls
468 lines (387 loc) · 15.8 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#include "dmelementdictionary.h"
#include "datamodel/dmelement.h"
#include "datamodel/dmattribute.h"
#include "datamodel/dmattributevar.h"
#include "datamodel/idatamodel.h"
#include "datamodel.h"
//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
CDmElementDictionary::CDmElementDictionary()
: m_idmap( 1024, 0, 0, DmIdPair_t::Compare, DmIdPair_t::HashKey )
{
}
//-----------------------------------------------------------------------------
// Clears the dictionary
//-----------------------------------------------------------------------------
void CDmElementDictionary::Clear()
{
m_Dict.Purge();
m_Attributes.Purge();
m_ArrayAttributes.Purge();
m_elementsToDelete.Purge();
}
//-----------------------------------------------------------------------------
// Inserts an element into the table
//-----------------------------------------------------------------------------
DmElementDictHandle_t CDmElementDictionary::InsertElement( CDmElement *pElement )
{
// Insert it into the reconnection table
return m_Dict.AddToTail( pElement ? pElement->GetHandle() : DMELEMENT_HANDLE_INVALID );
}
//-----------------------------------------------------------------------------
// Returns a particular element
//-----------------------------------------------------------------------------
CDmElement *CDmElementDictionary::GetElement( DmElementDictHandle_t handle )
{
if ( handle == ELEMENT_DICT_HANDLE_INVALID )
return NULL;
return g_pDataModel->GetElement( m_Dict[ handle ] );
}
//-----------------------------------------------------------------------------
// Adds an attribute to the fixup list
//-----------------------------------------------------------------------------
void CDmElementDictionary::AddAttribute( CDmAttribute *pAttribute, const DmObjectId_t &objectId )
{
if ( m_elementsToDelete.Find( pAttribute->GetOwner()->GetHandle() ) != m_elementsToDelete.InvalidIndex() )
return; // don't add attributes if their element is being deleted
int i = m_Attributes.AddToTail();
m_Attributes[i].m_nType = AT_OBJECTID;
m_Attributes[i].m_pAttribute = pAttribute;
CopyUniqueId( objectId, &m_Attributes[i].m_ObjectId );
}
//-----------------------------------------------------------------------------
// Adds an element of an attribute array to the fixup list
//-----------------------------------------------------------------------------
void CDmElementDictionary::AddArrayAttribute( CDmAttribute *pAttribute, DmElementDictHandle_t hElement )
{
if ( m_elementsToDelete.Find( pAttribute->GetOwner()->GetHandle() ) != m_elementsToDelete.InvalidIndex() )
return; // don't add attributes if their element is being deleted
int i = m_ArrayAttributes.AddToTail();
m_ArrayAttributes[i].m_nType = AT_ELEMENT;
m_ArrayAttributes[i].m_pAttribute = pAttribute;
m_ArrayAttributes[i].m_hElement = hElement;
}
void CDmElementDictionary::AddArrayAttribute( CDmAttribute *pAttribute, const DmObjectId_t &objectId )
{
if ( m_elementsToDelete.Find( pAttribute->GetOwner()->GetHandle() ) != m_elementsToDelete.InvalidIndex() )
return; // don't add attributes if their element is being deleted
int i = m_ArrayAttributes.AddToTail();
m_ArrayAttributes[i].m_nType = AT_OBJECTID;
m_ArrayAttributes[i].m_pAttribute = pAttribute;
CopyUniqueId( objectId, &m_ArrayAttributes[i].m_ObjectId );
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CDmElementDictionary::RemoveAttributeInfosOfElement( AttributeList_t &attributes, DmElementHandle_t hElement )
{
while ( attributes.Count() > 0 && attributes.Tail().m_pAttribute->GetOwner()->GetHandle() == hElement )
{
attributes.Remove( attributes.Count() - 1 );
}
}
DmElementHandle_t CDmElementDictionary::SetElementId( DmElementDictHandle_t hDictHandle, const DmObjectId_t &newId, DmConflictResolution_t idConflictResolution )
{
DmElementHandle_t hElement = m_Dict[ hDictHandle ];
CDmElement *pElement = g_pDataModel->GetElement( hElement );
Assert( pElement );
if ( !pElement )
return DMELEMENT_HANDLE_INVALID;
const DmObjectId_t &oldId = pElement->GetId();
if ( idConflictResolution == CR_FORCE_COPY )
{
m_idmap.Insert( DmIdPair_t( newId, oldId ) ); // map the newId back to the old id, and keep the old id
return hElement;
}
DmElementHandle_t newHandle = g_pDataModelImp->ChangeElementId( hElement, oldId, newId );
if ( newHandle != DMELEMENT_HANDLE_INVALID )
{
// if ChangeElementId returns a handle, the id has been changed
if ( newHandle != hElement )
{
int i = m_Dict.Find( hElement );
if ( i != m_Dict.InvalidIndex() )
{
m_Dict[ i ] = newHandle;
}
}
return newHandle; // either keeping the old handle, with the new id, or found a new handle associated with that new id
}
// id not changed because that id is already in use
if ( idConflictResolution == CR_DELETE_NEW )
{
DmElementHandle_t hExistingElement = g_pDataModel->FindElement( newId );
int i = m_elementsToDelete.AddToTail( );
m_elementsToDelete[i].m_hDictHandle = hDictHandle;
m_elementsToDelete[i].m_hElementToDelete = hElement;
m_elementsToDelete[i].m_hReplacementElement = hExistingElement;
// remove all element ref attributes read in before the id (typically none)
RemoveAttributeInfosOfElement( m_Attributes, hElement );
RemoveAttributeInfosOfElement( m_ArrayAttributes, hElement );
return DMELEMENT_HANDLE_INVALID;
}
if ( idConflictResolution == CR_DELETE_OLD )
{
DmElementHandle_t hExistingElement = g_pDataModel->FindElement( newId );
Assert( hExistingElement != DMELEMENT_HANDLE_INVALID );
if ( hExistingElement == DMELEMENT_HANDLE_INVALID )
return DMELEMENT_HANDLE_INVALID; // unexpected error in ChangeElementId (failed due to something other than a conflict)
g_pDataModelImp->DeleteElement( hExistingElement, HR_NEVER ); // need to keep the handle around until ChangeElemendId
newHandle = g_pDataModelImp->ChangeElementId( hElement, oldId, newId );
Assert( newHandle == hExistingElement );
int i = m_Dict.Find( hElement );
if ( i != m_Dict.InvalidIndex() )
{
m_Dict[ i ] = newHandle;
}
return newHandle;
}
if ( idConflictResolution == CR_COPY_NEW )
{
m_idmap.Insert( DmIdPair_t( newId, oldId ) ); // map the newId back to the old id, and keep the old id
return hElement;
}
Assert( 0 );
return DMELEMENT_HANDLE_INVALID;
}
//-----------------------------------------------------------------------------
// Finds an element into the table
//-----------------------------------------------------------------------------
DmElementDictHandle_t CDmElementDictionary::FindElement( CDmElement *pElement )
{
return m_Dict.Find( pElement ? pElement->GetHandle() : DMELEMENT_HANDLE_INVALID );
}
//-----------------------------------------------------------------------------
// Hook up all element references (which were unserialized as object ids)
//-----------------------------------------------------------------------------
void CDmElementDictionary::HookUpElementAttributes()
{
int n = m_Attributes.Count();
for ( int i = 0; i < n; ++i )
{
Assert( m_Attributes[i].m_pAttribute->GetType() == AT_ELEMENT );
Assert( m_Attributes[i].m_nType == AT_OBJECTID );
UtlHashHandle_t h = m_idmap.Find( DmIdPair_t( m_Attributes[i].m_ObjectId ) );
DmObjectId_t &id = h == m_idmap.InvalidHandle() ? m_Attributes[i].m_ObjectId : m_idmap[ h ].m_newId;
// search id->handle table (both loaded and unloaded) for id, and if not found, create a new handle, map it to the id and return it
DmElementHandle_t hElement = g_pDataModelImp->FindOrCreateElementHandle( id );
m_Attributes[i].m_pAttribute->SetValue<DmElementHandle_t>( hElement );
}
}
//-----------------------------------------------------------------------------
// Hook up all element array references
//-----------------------------------------------------------------------------
void CDmElementDictionary::HookUpElementArrayAttributes()
{
// Find unique array attributes; we need to clear them all before adding stuff.
// This clears them of stuff added during their construction phase.
int n = m_ArrayAttributes.Count();
CUtlRBTree< CDmAttribute*, unsigned short > lookup( 0, n, DefLessFunc(CDmAttribute*) );
for ( int i = 0; i < n; ++i )
{
Assert( m_ArrayAttributes[i].m_pAttribute->GetType() == AT_ELEMENT_ARRAY );
CDmAttribute *pElementArray = m_ArrayAttributes[i].m_pAttribute;
CDmrElementArray<> array( pElementArray );
if ( lookup.Find( pElementArray ) == lookup.InvalidIndex() )
{
array.RemoveAll();
lookup.Insert( pElementArray );
}
}
for ( int i = 0; i < n; ++i )
{
Assert( m_ArrayAttributes[i].m_pAttribute->GetType() == AT_ELEMENT_ARRAY );
CDmrElementArray<> array( m_ArrayAttributes[i].m_pAttribute );
if ( m_ArrayAttributes[i].m_nType == AT_ELEMENT )
{
CDmElement *pElement = GetElement( m_ArrayAttributes[i].m_hElement );
array.AddToTail( pElement );
}
else
{
UtlHashHandle_t h = m_idmap.Find( DmIdPair_t( m_ArrayAttributes[i].m_ObjectId ) );
DmObjectId_t &id = ( h == m_idmap.InvalidHandle() ) ? m_ArrayAttributes[i].m_ObjectId : m_idmap[ h ].m_newId;
// search id->handle table (both loaded and unloaded) for id, and if not found, create a new handle, map it to the id and return it
DmElementHandle_t hElement = g_pDataModelImp->FindOrCreateElementHandle( id );
int nIndex = array.AddToTail();
array.SetHandle( nIndex, hElement );
}
}
}
//-----------------------------------------------------------------------------
// Hook up all element references (which were unserialized as object ids)
//-----------------------------------------------------------------------------
void CDmElementDictionary::HookUpElementReferences()
{
int nElementsToDelete = m_elementsToDelete.Count();
for ( int i = 0; i < nElementsToDelete; ++i )
{
DmElementDictHandle_t hDictIndex = m_elementsToDelete[i].m_hDictHandle;
DmElementHandle_t hElement = m_Dict[ hDictIndex ];
g_pDataModelImp->DeleteElement( hElement );
m_Dict[ hDictIndex ] = m_elementsToDelete[i].m_hReplacementElement;
}
HookUpElementArrayAttributes();
HookUpElementAttributes();
}
//-----------------------------------------------------------------------------
//
// Element dictionary used in serialization
//
//-----------------------------------------------------------------------------
CDmElementSerializationDictionary::CDmElementSerializationDictionary() :
m_Dict( 1024, 0, CDmElementSerializationDictionary::LessFunc )
{
}
//-----------------------------------------------------------------------------
// Used to sort the list of elements
//-----------------------------------------------------------------------------
bool CDmElementSerializationDictionary::LessFunc( const ElementInfo_t &lhs, const ElementInfo_t &rhs )
{
return lhs.m_pElement < rhs.m_pElement;
}
//-----------------------------------------------------------------------------
// Finds the handle of the element
//-----------------------------------------------------------------------------
DmElementDictHandle_t CDmElementSerializationDictionary::Find( CDmElement *pElement )
{
ElementInfo_t find;
find.m_pElement = pElement;
return m_Dict.Find( find );
}
//-----------------------------------------------------------------------------
// Creates the list of all things to serialize
//-----------------------------------------------------------------------------
void CDmElementSerializationDictionary::BuildElementList_R( CDmElement *pElement, bool bFlatMode, bool bIsRoot )
{
if ( !pElement )
return;
// FIXME: Right here we should ask the element if it's an external
// file reference and exit immediately if so.
// This means we've already encountered this guy.
// Therefore, he can never be a root element
DmElementDictHandle_t h = Find( pElement );
if ( h != m_Dict.InvalidIndex() )
{
m_Dict[h].m_bRoot = true;
return;
}
ElementInfo_t info;
info.m_bRoot = bFlatMode || bIsRoot;
info.m_pElement = pElement;
m_Dict.Insert( info );
for ( CDmAttribute *pAttribute = pElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
{
if ( pAttribute->IsFlagSet( FATTRIB_DONTSAVE ) )
continue;
switch( pAttribute->GetType() )
{
case AT_ELEMENT:
{
CDmElement *pChild = pAttribute->GetValueElement<CDmElement>();
if ( !pChild || pChild->GetFileId() != pElement->GetFileId() )
break;
BuildElementList_R( pChild, bFlatMode, false );
}
break;
case AT_ELEMENT_ARRAY:
{
CDmrElementArray<> array( pAttribute );
int nCount = array.Count();
for ( int i = 0; i < nCount; ++i )
{
CDmElement *pChild = array[i];
if ( !pChild || pChild->GetFileId() != pElement->GetFileId() )
break;
BuildElementList_R( pChild, bFlatMode, false );
}
}
break;
}
}
}
void CDmElementSerializationDictionary::BuildElementList( CDmElement *pElement, bool bFlatMode )
{
BuildElementList_R( pElement, bFlatMode, true );
}
//-----------------------------------------------------------------------------
// Should I inline the serialization of this element?
//-----------------------------------------------------------------------------
bool CDmElementSerializationDictionary::ShouldInlineElement( CDmElement *pElement )
{
// This means we've already encountered this guy.
// Therefore, he can never be a root element
DmElementDictHandle_t h = Find( pElement );
if ( h != m_Dict.InvalidIndex() )
return !m_Dict[h].m_bRoot;
// If we didn't find the element, it means it's a reference to an external
// element (or it's NULL), so don't inline ie.
return false;
}
//-----------------------------------------------------------------------------
// Clears the dictionary
//-----------------------------------------------------------------------------
void CDmElementSerializationDictionary::Clear()
{
m_Dict.RemoveAll();
}
//-----------------------------------------------------------------------------
// How many root elements do we have?
//-----------------------------------------------------------------------------
int CDmElementSerializationDictionary::RootElementCount() const
{
int nCount = 0;
DmElementDictHandle_t h = m_Dict.FirstInorder();
while( h != m_Dict.InvalidIndex() )
{
if ( m_Dict[h].m_bRoot )
{
++nCount;
}
h = m_Dict.NextInorder( h );
}
return nCount;
}
//-----------------------------------------------------------------------------
// Iterates over all root elements to serialize
//-----------------------------------------------------------------------------
DmElementDictHandle_t CDmElementSerializationDictionary::FirstRootElement() const
{
// NOTE - this code only works with BlockMemory or Memory (NOT FixedMemory)
// NOTE: I don't have to use First/NextInorder here because there
// are guaranteed to be no removals from the dictionary.
// Also, using inorder traversal won't get my actual root element to be first in the file
int nCount = m_Dict.Count();
for ( DmElementDictHandle_t h = 0; h < nCount; ++h )
{
if ( m_Dict[h].m_bRoot )
return h;
}
return ELEMENT_DICT_HANDLE_INVALID;
}
DmElementDictHandle_t CDmElementSerializationDictionary::NextRootElement( DmElementDictHandle_t h ) const
{
// NOTE - this code only works with BlockMemory or Memory (NOT FixedMemory)
// NOTE: I don't have to use First/NextInorder here because there
// are guaranteed to be no removals from the dictionary.
// Also, using inorder traversal won't get my actual root element to be first in the file
++h;
int nCount = m_Dict.Count();
for ( ; h < nCount; ++h )
{
if ( m_Dict[h].m_bRoot )
return h;
}
return ELEMENT_DICT_HANDLE_INVALID;
}
CDmElement *CDmElementSerializationDictionary::GetRootElement( DmElementDictHandle_t h )
{
Assert( m_Dict[h].m_bRoot );
return m_Dict[h].m_pElement;
}