summaryrefslogtreecommitdiff
path: root/dmserializers/importvmf.cpp
blob: 7f57c6dc7d74accdfffb3cbe105bd2f19e0eb2dc (plain) (blame)
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================

#include "importkeyvaluebase.h"
#include "dmserializers.h"
#include "datamodel/idatamodel.h"
#include "datamodel/dmelement.h"
#include "tier1/KeyValues.h"
#include "tier1/utlbuffer.h"
#include "datamodel/dmattribute.h"


//-----------------------------------------------------------------------------
// Serialization class for VMF files (map files)
//-----------------------------------------------------------------------------
class CImportVMF : public CImportKeyValueBase
{
public:
	virtual const char *GetName() const { return "vmf"; }
	virtual const char *GetDescription() const { return "Valve Map File"; }
	virtual int GetCurrentVersion() const { return 0; } // doesn't store a version

	bool Serialize( CUtlBuffer &outBuf, CDmElement *pRoot );
	CDmElement* UnserializeFromKeyValues( KeyValues *pKeyValues );

private:
	// Reads a single entity
	bool UnserializeEntityKey( CDmAttribute *pEntities, KeyValues *pKeyValues );

	// Reads entity editor keys
	bool UnserializeEntityEditorKey( CDmAttribute *pEditor, KeyValues *pKeyValues );

	// Reads keys that we currently do nothing with
	bool UnserializeUnusedKeys( DmElementHandle_t hOther, KeyValues *pKeyValues );

	// Writes out all everything other than entities
	bool SerializeOther( CUtlBuffer &buf, CDmAttribute *pOther, const char **ppFilter = 0 );

	// Writes out all entities
	bool SerializeEntities( CUtlBuffer &buf, CDmAttribute *pEntities );

	// Writes out a single attribute recursively
	bool SerializeAttribute( CUtlBuffer &buf, CDmAttribute *pAttribute, bool bElementArrays );

	// Writes entity editor keys
	bool SerializeEntityEditorKey( CUtlBuffer &buf, DmElementHandle_t hEditor );

	// Updates the max hammer id
	void UpdateMaxHammerId( KeyValues *pKeyValue );

	// Max id read from the file
	int m_nMaxHammerId;
};


//-----------------------------------------------------------------------------
// Singleton instance
//-----------------------------------------------------------------------------
static CImportVMF s_ImportVMF;

void InstallVMFImporter( IDataModel *pFactory )
{
	pFactory->AddSerializer( &s_ImportVMF );
}


//-----------------------------------------------------------------------------
// Deals with poorly-named key values for the DME system
//-----------------------------------------------------------------------------
static const char *s_pKeyRemapNames[][2] = 
{
	{ "id", "__id" },
	{ "name", "__name" },
	{ "type", "__type" },
	{ NULL, NULL },
};


//-----------------------------------------------------------------------------
// Gets remap name for unserialization/serailzation
//-----------------------------------------------------------------------------
static const char *GetRemapName( const char *pName, bool bSerialization )
{
	for ( int i = 0; s_pKeyRemapNames[i][0]; ++i )
	{
		if ( !Q_stricmp( pName, s_pKeyRemapNames[i][bSerialization] ) )
			return s_pKeyRemapNames[i][1 - bSerialization];
	}
	return pName;
}


//-----------------------------------------------------------------------------
// Writes out a single attribute recursively
//-----------------------------------------------------------------------------
bool CImportVMF::SerializeAttribute( CUtlBuffer &buf, CDmAttribute *pAttribute, bool bElementArrays )
{
	if ( pAttribute->IsFlagSet( FATTRIB_STANDARD | FATTRIB_DONTSAVE ) )
		return true;

	const char *pFieldName = GetRemapName( pAttribute->GetName(), true );
	if ( !Q_stricmp( pFieldName, "editorType" ) )
		return true;

	if ( !IsArrayType( pAttribute->GetType() ) )
	{
		if ( !bElementArrays )
		{
			buf.Printf( "\"%s\" ", pFieldName );
			if ( pAttribute->GetType() != AT_STRING )
			{
				buf.Printf( "\"" );
			}
			g_pDataModel->SetSerializationDelimiter( GetCStringCharConversion() );
			pAttribute->Serialize( buf );
			g_pDataModel->SetSerializationDelimiter( NULL );
			if ( pAttribute->GetType() != AT_STRING )
			{
				buf.Printf( "\"" );
			}
			buf.Printf( "\n" );
		}
	}
	else
	{
		if ( bElementArrays )
		{
			Assert( pAttribute->GetType() == AT_ELEMENT_ARRAY );
			if ( !SerializeOther( buf, pAttribute ) )
				return false;
		}
	}

	return true;
}

	
//-----------------------------------------------------------------------------
// Writes out all everything other than entities
//-----------------------------------------------------------------------------
bool CImportVMF::SerializeOther( CUtlBuffer &buf, CDmAttribute *pOther, const char **ppFilter )
{
	CDmrElementArray<> array( pOther );
	int nCount = array.Count();
	for ( int i = 0; i < nCount; ++i )
	{
		CDmElement *pElement = array[i];
		const char *pElementName = pElement->GetName();
		if ( ppFilter )
		{
			int j;
			for ( j = 0; ppFilter[j]; ++j )
			{
				if ( !Q_stricmp( pElementName, ppFilter[j] ) )
					break;
			}

			if ( !ppFilter[j] )
				continue;
		}

		int nLen = Q_strlen( pElementName ) + 1;
		char *pTemp = (char*)_alloca( nLen );
		Q_strncpy( pTemp, pElementName, nLen );
		Q_strlower( pTemp );
		buf.Printf( "%s\n", pTemp );
		buf.Printf( "{\n" );
		buf.PushTab();

		// Normal attributes first
	    for( CDmAttribute *pAttribute = pElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
		{
			if ( !SerializeAttribute( buf, pAttribute, false ) )
				return false;
		}

		// Subkeys later
	    for( CDmAttribute *pAttribute = pElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
		{
			if ( !SerializeAttribute( buf, pAttribute, true ) )
				return false;
		}

		buf.PopTab();
		buf.Printf( "}\n" );
	}

	return true;
}


//-----------------------------------------------------------------------------
// Writes entity editor keys
//-----------------------------------------------------------------------------
bool CImportVMF::SerializeEntityEditorKey( CUtlBuffer &buf, DmElementHandle_t hEditor )
{
	CDmElement *pEditorElement = g_pDataModel->GetElement( hEditor );
	if ( !pEditorElement )
		return true;

	buf.Printf( "editor\n" );
	buf.Printf( "{\n" );
	buf.PushTab();

	{
		CDmAttribute *pAttribute = pEditorElement->GetAttribute( "color" );
		if ( pAttribute )
		{
			Color c = pAttribute->GetValue<Color>();
			buf.Printf( "\"color\" \"%d %d %d\"\n", c.r(), c.g(), c.b() );
		}
	}
	PrintIntAttribute( pEditorElement, buf, "id" ); // FIXME - id is a DmObjectId_t!!! This should never print anything!
	PrintStringAttribute( pEditorElement, buf, "comments" );
	PrintBoolAttribute( pEditorElement, buf, "visgroupshown" );
	PrintBoolAttribute( pEditorElement, buf, "visgroupautoshown" );

	for ( CDmAttribute *pAttribute = pEditorElement->FirstAttribute(); pAttribute != NULL; pAttribute = pAttribute->NextAttribute() )
	{
		if ( pAttribute->IsFlagSet( FATTRIB_STANDARD | FATTRIB_DONTSAVE ) )
			continue;

		const char *pKeyName = pAttribute->GetName();
		if ( Q_stricmp( pKeyName, "color" ) && Q_stricmp( pKeyName, "id" ) && 
			Q_stricmp( pKeyName, "comments" ) && Q_stricmp( pKeyName, "visgroupshown" ) &&
			Q_stricmp( pKeyName, "visgroupautoshown" ) )
		{
			PrintStringAttribute( pEditorElement, buf, pKeyName );
		}
	}

	buf.PopTab();
	buf.Printf( "}\n" );

	return true;
}


//-----------------------------------------------------------------------------
// Writes out all entities
//-----------------------------------------------------------------------------
bool CImportVMF::SerializeEntities( CUtlBuffer &buf, CDmAttribute *pEntities )
{
	// FIXME: Make this serialize in the order in which it appears in the FGD
	// to minimize diffs
	CDmrElementArray<> array( pEntities );

	int nCount = array.Count();
	for ( int i = 0; i < nCount; ++i )
	{
		CDmElement *pElement = array[i];
		buf.Printf( "entity\n" );
		buf.Printf( "{\n" );
		buf.PushTab();
		buf.Printf( "\"id\" \"%s\"\n", pElement->GetName() );

	    for( CDmAttribute *pAttribute = pElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
		{
			// Do 'editor' at the end to preserve ordering and not make terrible diffs
			if ( !Q_stricmp( pAttribute->GetName(), "editor" ) )
				continue;

			if ( !SerializeAttribute( buf, pAttribute, false ) )
				return false;
		}

	    for( CDmAttribute *pAttribute = pElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
		{
			// Do 'editor' at the end to preserve ordering and not make terrible diffs
			if ( !Q_stricmp( pAttribute->GetName(), "editor" ) )
				continue;

			if ( !SerializeAttribute( buf, pAttribute, true ) )
				return false;
		}

		// Do the 'editor'
		CDmAttribute *pEditor = pElement->GetAttribute( "editor" );
		if ( pEditor )
		{
			SerializeEntityEditorKey( buf, pEditor->GetValue<DmElementHandle_t>() );
		}

		buf.PopTab();
		buf.Printf( "}\n" );
	}

	return true;
}


//-----------------------------------------------------------------------------
// Writes out a new VMF file
//-----------------------------------------------------------------------------
bool CImportVMF::Serialize( CUtlBuffer &buf, CDmElement *pRoot )
{
	// This is done in this strange way (namely, serializing other twice) to minimize diffs
	const char *pOtherFilter1[] = 
	{
		"versioninfo", "visgroups", "viewsettings", "world", NULL
	};

	const char *pOtherFilter2[] = 
	{
		"cameras", "cordon", "hidden", NULL
	};

	CDmAttribute *pOther = pRoot->GetAttribute( "other" );
	if ( pOther && pOther->GetType() == AT_ELEMENT_ARRAY )
	{
		if ( !SerializeOther( buf, pOther, pOtherFilter1 ) )
			return false;
	}

	// Serialize entities
	CDmAttribute *pEntities = pRoot->GetAttribute( "entities" );
	if ( pEntities && pEntities->GetType() == AT_ELEMENT_ARRAY )
	{
		if ( !SerializeEntities( buf, pEntities ) )
			return false;
	}

	if ( pOther && pOther->GetType() == AT_ELEMENT_ARRAY )
	{
		if ( !SerializeOther( buf, pOther, pOtherFilter2 ) )
			return false;
	}

	return true;
}


//-----------------------------------------------------------------------------
// Updates the max hammer id
//-----------------------------------------------------------------------------
void CImportVMF::UpdateMaxHammerId( KeyValues *pField )
{
	if ( !Q_stricmp( pField->GetName(), "id" ) )
	{
		int nId = atoi( pField->GetString() );
		if ( nId > m_nMaxHammerId )
		{
			m_nMaxHammerId = nId;
		}
	}
}


//-----------------------------------------------------------------------------
// Reads entity editor keys
//-----------------------------------------------------------------------------
bool CImportVMF::UnserializeEntityEditorKey( CDmAttribute *pEditorAttribute, KeyValues *pKeyValues )
{
	CDmElement *pEditor;
	DmElementHandle_t hEditor = pEditorAttribute->GetValue<DmElementHandle_t>();
	if ( hEditor == DMELEMENT_HANDLE_INVALID )
	{
		pEditor = CreateDmElement( "DmElement", "editor", NULL );;
		if ( !pEditor )
			return false;
		hEditor = pEditor->GetHandle();
		pEditorAttribute->SetValue( hEditor );
	}
	else
	{
		pEditor = g_pDataModel->GetElement( hEditor );
	}

	int r, g, b;
	if ( sscanf( pKeyValues->GetString( "color", "" ), "%d %d %d", &r, &g, &b ) == 3 )
	{
		Color c( r, g, b, 255 );
		if ( !pEditor->SetValue( "color", c ) )
			return false;
	}
	KeyValues *pIdKey = pKeyValues->FindKey( "id" );
	if ( pIdKey )
	{
		UpdateMaxHammerId( pIdKey );
	}
	AddIntAttribute( pEditor, pKeyValues, "id" );
	AddStringAttribute( pEditor, pKeyValues, "comments" );
	AddBoolAttribute( pEditor, pKeyValues, "visgroupshown" );
	AddBoolAttribute( pEditor, pKeyValues, "visgroupautoshown" );

	for ( KeyValues *pUserKey = pKeyValues->GetFirstValue(); pUserKey != NULL; pUserKey = pUserKey->GetNextValue() )
	{
		const char *pKeyName = pUserKey->GetName();
		if ( Q_stricmp( pKeyName, "color" ) && Q_stricmp( pKeyName, "id" ) && 
			Q_stricmp( pKeyName, "comments" ) && Q_stricmp( pKeyName, "visgroupshown" ) &&
			Q_stricmp( pKeyName, "visgroupautoshown" ) )
		{
			AddStringAttribute( pEditor, pKeyValues, pKeyName );
		}
	}

	return true;
}

	
//-----------------------------------------------------------------------------
// Reads a single entity
//-----------------------------------------------------------------------------
bool CImportVMF::UnserializeEntityKey( CDmAttribute *pEntities, KeyValues *pKeyValues )
{
	CDmElement *pEntity = CreateDmElement( "DmeVMFEntity", pKeyValues->GetString( "id", "-1" ), NULL );
	if ( !pEntity )
		return false;

	CDmrElementArray<> array( pEntities );
	array.AddToTail( pEntity );

	// Each act busy needs to have an editortype associated with it so it displays nicely in editors
	pEntity->SetValue( "editorType", "vmfEntity" );

	const char *pClassName = pKeyValues->GetString( "classname", NULL );
	if ( !pClassName )
		return false;

	// Read the actual fields
	for ( KeyValues *pField = pKeyValues->GetFirstValue(); pField != NULL; pField = pField->GetNextValue() )
	{
		// FIXME: Knowing the FGD here would be useful for type determination.
		// Look up the field by name based on class name
		// In the meantime, just use the keyvalues type?
		char pFieldName[512];
		Q_strncpy( pFieldName, pField->GetName(), sizeof(pFieldName) );
		Q_strlower( pFieldName );

		// Don't do id: it's used as the name
		// Not to mention it's a protected name
		if ( !Q_stricmp( pFieldName, "id" ) )
		{
			UpdateMaxHammerId( pField );
			continue;
		}

		// Type, name, and editortype are protected names
		Assert( Q_stricmp( pFieldName, "type" ) && Q_stricmp( pFieldName, "name" ) && Q_stricmp( pFieldName, "editortype" ) );

		switch( pField->GetDataType() )
		{
		case KeyValues::TYPE_INT:
			if ( !AddIntAttributeFlags( pEntity, pKeyValues, pFieldName, FATTRIB_USERDEFINED ) )
				return false;
			break;

		case KeyValues::TYPE_FLOAT:
			if ( !AddFloatAttributeFlags( pEntity, pKeyValues, pFieldName, FATTRIB_USERDEFINED ) )
				return false;
			break;

		case KeyValues::TYPE_STRING:
			{
				const char* pString = pField->GetString();
				if (!pString || !pString[0])
					return false;

				// Look for vectors
				Vector4D v;
				if ( sscanf( pString, "%f %f %f %f", &v.x, &v.y, &v.z, &v.w ) == 4 )
				{
					if ( !pEntity->SetValue( pFieldName, v ) )
						return false;
					CDmAttribute *pAttribute = pEntity->GetAttribute( pFieldName );
					pAttribute->AddFlag( FATTRIB_USERDEFINED );
				}
				else if ( sscanf( pString, "%f %f %f", &v.x, &v.y, &v.z ) == 3 )
				{
					if ( !pEntity->SetValue( pFieldName, v.AsVector3D() ) )
					{
						QAngle ang( v.x, v.y, v.z );
						if ( !pEntity->SetValue( pFieldName, ang ) )
							return false;
					}
					CDmAttribute *pAttribute = pEntity->GetAttribute( pFieldName );
					pAttribute->AddFlag( FATTRIB_USERDEFINED );
				}
				else
				{
					if ( !AddStringAttributeFlags( pEntity, pKeyValues, pFieldName, FATTRIB_USERDEFINED ) )
						return false;
				}
			}
			break;
		}
	}

	// Read the subkeys
	CDmAttribute *pEditor = pEntity->AddAttribute( "editor", AT_ELEMENT );
	CDmrElementArray<> otherKeys( pEntity->AddAttribute( "other", AT_ELEMENT_ARRAY ) );
	for ( KeyValues *pSubKey = pKeyValues->GetFirstTrueSubKey(); pSubKey != NULL; pSubKey = pSubKey->GetNextTrueSubKey() )
	{
		bool bOk = false;
		if ( !Q_stricmp( pSubKey->GetName(), "editor" ) )
		{
			bOk = UnserializeEntityEditorKey( pEditor, pSubKey );
		}
		else
		{
			// We don't currently do anything with the other keys
			CDmElement *pOther = CreateDmElement( "DmElement", pSubKey->GetName(), NULL );
			otherKeys.AddToTail( pOther );
			bOk = UnserializeUnusedKeys( pOther->GetHandle(), pSubKey );
		}

		if ( !bOk )
			return false;
	}

	return true;
}


//-----------------------------------------------------------------------------
// Reads keys that we currently do nothing with
//-----------------------------------------------------------------------------
bool CImportVMF::UnserializeUnusedKeys( DmElementHandle_t hOther, KeyValues *pKeyValues )
{
	CDmElement *pOther = g_pDataModel->GetElement( hOther );

	// Read the actual fields
	for ( KeyValues *pField = pKeyValues->GetFirstValue(); pField != NULL; pField = pField->GetNextValue() )
	{
		UpdateMaxHammerId( pField );
		const char *pFieldName = GetRemapName( pField->GetName(), false );
		pOther->SetValue( pFieldName, pField->GetString() );
	}

	// Read the subkeys
	CDmrElementArray<> subKeys( pOther->AddAttribute( "subkeys", AT_ELEMENT_ARRAY ) );
	for ( KeyValues *pSubKey = pKeyValues->GetFirstTrueSubKey(); pSubKey != NULL; pSubKey = pSubKey->GetNextTrueSubKey() )
	{
		CDmElement *pSubElement = CreateDmElement( "DmElement", pSubKey->GetName(), NULL );
		subKeys.AddToTail( pSubElement );
		if ( !UnserializeUnusedKeys( pSubElement->GetHandle(), pSubKey ) )
			return false;
	}
	return true;
}

	
/*
//-----------------------------------------------------------------------------
// Reads the cordon data
//-----------------------------------------------------------------------------
bool CImportVMF::UnserializeCordonKey( IDmAttributeElement *pCordon, KeyValues *pKeyValues )
{
	DmElementHandle_t hCordon = pCordon->GetValue().Get();
	if ( hCordon == DMELEMENT_HANDLE_INVALID )
	{
		hCordon = CreateDmElement( "DmElement", "cordon", NULL );
		if ( hCordon == DMELEMENT_HANDLE_INVALID )
			return false;
		pCordon->SetValue( hCordon );
	}

	AddBoolAttribute( hCordon, pKeyValues, "active" );

	Vector v;
	if ( sscanf( pKeyValues->GetString( "mins", "" ), "(%f %f %f)", &v.x, &v.y, &v.z ) == 3 )
	{
		if ( !DmElementAddAttribute( hCordon, "mins", v ) )
			return false;
	}
	if ( sscanf( pKeyValues->GetString( "maxs", "" ), "(%f %f %f)", &v.x, &v.y, &v.z ) == 3 )
	{
		if ( !DmElementAddAttribute( hCordon, "maxs", v ) )
			return false;
	}
	return true;
}
*/


//-----------------------------------------------------------------------------
// Main entry point for the unserialization
//-----------------------------------------------------------------------------
CDmElement* CImportVMF::UnserializeFromKeyValues( KeyValues *pKeyValues )
{
	m_nMaxHammerId = 0;

	// Create the main element
	CDmElement *pElement = CreateDmElement( "DmElement", "VMF", NULL );
	if ( !pElement )
		return NULL;

	// Each vmf needs to have an editortype associated with it so it displays nicely in editors
	pElement->SetValue( "editorType", "VMF" );

	// The VMF is a series of keyvalue blocks; either 
	// 'entity', 'cameras', 'cordon', 'world', 'versioninfo', or 'viewsettings' 
	CDmAttribute *pEntityArray = pElement->AddAttribute( "entities", AT_ELEMENT_ARRAY );

	// All main keys are root keys
	CDmrElementArray<> otherKeys( pElement->AddAttribute( "other", AT_ELEMENT_ARRAY ) );
	for ( ; pKeyValues != NULL; pKeyValues = pKeyValues->GetNextKey() )
	{
		bool bOk = false;
		if ( !Q_stricmp( pKeyValues->GetName(), "entity" ) )
		{
			bOk = UnserializeEntityKey( pEntityArray, pKeyValues );
		}
		else
		{
			// We don't currently do anything with 
			CDmElement *pOther = CreateDmElement( "DmElement", pKeyValues->GetName(), NULL );
			otherKeys.AddToTail( pOther );
			bOk = UnserializeUnusedKeys( pOther->GetHandle(), pKeyValues );
		}

		if ( !bOk )
		{
			Warning( "Error importing VMF element %s\n", pKeyValues->GetName() );
			return NULL;
		}
	}

	// Resolve all element references recursively
	RecursivelyResolveElement( pElement );

	// Add the max id read in from the file to the root entity
	pElement->SetValue( "maxHammerId", m_nMaxHammerId );

	return pElement;
}