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 "dmserializerkeyvalues.h"
#include "datamodel/idatamodel.h"
#include "datamodel.h"
#include "datamodel/dmelement.h"
#include "datamodel/dmattributevar.h"
#include "dmattributeinternal.h"
#include "tier1/KeyValues.h"
#include "tier1/utlbuffer.h"
#include "tier1/utlvector.h"
#include <limits.h>
#include "DmElementFramework.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
class CUtlBuffer;
class CBaseSceneObject;
//-----------------------------------------------------------------------------
// Used to remap keyvalues names
//-----------------------------------------------------------------------------
struct AttributeRemap_t
{
const char *m_pKeyValuesName;
const char *m_pDmeName;
};
static AttributeRemap_t s_pAttributeRemap[] =
{
{ "type", "_type" }, // FIXME - remove this once we've made type no longer be an attribute
{ "name", "_name" },
{ "id", "_id" }, // FIXME - remove this once we've made id no longer be an attribute
{ NULL, NULL }
};
//-----------------------------------------------------------------------------
// Serialization class for Key Values
//-----------------------------------------------------------------------------
class CDmSerializerKeyValues : public IDmSerializer
{
public:
// Inherited from IDMSerializer
virtual const char *GetName() const { return "keyvalues"; }
virtual const char *GetDescription() const { return "KeyValues"; }
virtual bool StoresVersionInFile() const { return false; }
virtual bool IsBinaryFormat() const { return false; }
virtual int GetCurrentVersion() const { return 0; } // doesn't store a version
virtual bool Serialize( CUtlBuffer &buf, CDmElement *pRoot );
virtual bool Unserialize( CUtlBuffer &buf, const char *pEncodingName, int nEncodingVersion,
const char *pSourceFormatName, int nSourceFormatVersion,
DmFileId_t fileid, DmConflictResolution_t idConflictResolution, CDmElement **ppRoot );
private:
// Methods related to serialization
void SerializeSubKeys( CUtlBuffer& buf, CDmAttribute *pSubKeys );
bool SerializeAttributes( CUtlBuffer& buf, CDmElement *pElement );
bool SerializeElement( CUtlBuffer& buf, CDmElement *pElement );
// Methods related to unserialization
DmElementHandle_t UnserializeElement( KeyValues *pKeyValues, int iNestingLevel );
void UnserializeAttribute( CDmElement *pElement, KeyValues *pKeyValues );
DmElementHandle_t CreateDmElement( const char *pElementType, const char *pElementName );
CDmElement* UnserializeFromKeyValues( KeyValues *pKeyValues );
// Deterimines the attribute type of a keyvalue
DmAttributeType_t DetermineAttributeType( KeyValues *pKeyValues );
// For unserialization
CUtlVector<DmElementHandle_t> m_ElementList;
DmElementHandle_t m_hRoot;
DmFileId_t m_fileid;
};
//-----------------------------------------------------------------------------
// Singleton instance
//-----------------------------------------------------------------------------
static CDmSerializerKeyValues s_DMSerializerKeyValues;
void InstallKeyValuesSerializer( IDataModel *pFactory )
{
pFactory->AddSerializer( &s_DMSerializerKeyValues );
}
//-----------------------------------------------------------------------------
// Serializes a single element attribute
//-----------------------------------------------------------------------------
void CDmSerializerKeyValues::SerializeSubKeys( CUtlBuffer& buf, CDmAttribute *pSubKeys )
{
CDmrElementArray<> array( pSubKeys );
int c = array.Count();
for ( int i = 0; i < c; ++i )
{
CDmElement *pChild = array[i];
if ( pChild )
{
SerializeElement( buf, pChild );
}
}
}
//-----------------------------------------------------------------------------
// Serializes all attributes in an element
//-----------------------------------------------------------------------------
bool CDmSerializerKeyValues::SerializeAttributes( CUtlBuffer& buf, CDmElement *pElement )
{
// Collect the attributes to be written
CDmAttribute **ppAttributes = ( CDmAttribute** )_alloca( pElement->AttributeCount() * sizeof( CDmAttribute* ) );
int nAttributes = 0;
for ( CDmAttribute *pAttribute = pElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
{
if ( pAttribute->IsFlagSet( FATTRIB_DONTSAVE | FATTRIB_STANDARD ) )
continue;
ppAttributes[ nAttributes++ ] = pAttribute;
}
// Now write them all out in reverse order, since FirstAttribute is actually the *last* attribute for perf reasons
for ( int i = nAttributes - 1; i >= 0; --i )
{
CDmAttribute *pAttribute = ppAttributes[ i ];
Assert( pAttribute );
const char *pName = pAttribute->GetName();
// Rename "_type", "_name", or "_id" fields, since they are special fields
for ( int iAttr = 0; s_pAttributeRemap[i].m_pKeyValuesName; ++i )
{
if ( !Q_stricmp( pName, s_pAttributeRemap[iAttr].m_pDmeName ) )
{
pName = s_pAttributeRemap[iAttr].m_pKeyValuesName;
break;
}
}
DmAttributeType_t nAttrType = pAttribute->GetType();
if ( ( nAttrType == AT_ELEMENT_ARRAY ) && !Q_stricmp( pName, "subkeys" ) )
{
SerializeSubKeys( buf, pAttribute );
continue;
}
buf.Printf( "\"%s\" ", pName );
switch( nAttrType )
{
case AT_VOID:
case AT_STRING_ARRAY:
case AT_VOID_ARRAY:
case AT_ELEMENT:
case AT_ELEMENT_ARRAY:
Warning("KeyValues: Can't serialize attribute of type %s into KeyValues files!\n",
g_pDataModel->GetAttributeNameForType( nAttrType ) );
buf.PutChar( '\"' );
buf.PutChar( '\"' );
break;
case AT_FLOAT:
case AT_INT:
case AT_BOOL:
pAttribute->Serialize( buf );
break;
case AT_VECTOR4:
case AT_VECTOR3:
case AT_VECTOR2:
case AT_STRING:
default:
buf.PutChar( '\"' );
buf.PushTab();
pAttribute->Serialize( buf );
buf.PopTab();
buf.PutChar( '\"' );
break;
}
buf.PutChar( '\n' );
}
return true;
}
bool CDmSerializerKeyValues::SerializeElement( CUtlBuffer& buf, CDmElement *pElement )
{
buf.Printf( "\"%s\"\n{\n", pElement->GetName() );
buf.PushTab();
SerializeAttributes( buf, pElement );
buf.PopTab();
buf.Printf( "}\n" );
return true;
}
bool CDmSerializerKeyValues::Serialize( CUtlBuffer &outBuf, CDmElement *pRoot )
{
if ( !pRoot )
return true;
CDmAttribute* pSubKeys = pRoot->GetAttribute( "subkeys" );
if ( !pSubKeys )
return true;
//SetSerializationDelimiter( GetCStringCharConversion() );
SerializeSubKeys( outBuf, pSubKeys );
//SetSerializationDelimiter( NULL );
return true;
}
//-----------------------------------------------------------------------------
// Creates a scene object, adds it to the element dictionary
//-----------------------------------------------------------------------------
DmElementHandle_t CDmSerializerKeyValues::CreateDmElement( const char *pElementType, const char *pElementName )
{
// See if we can create an element of that type
DmElementHandle_t hElement = g_pDataModel->CreateElement( pElementType, pElementName, m_fileid );
if ( hElement == DMELEMENT_HANDLE_INVALID )
{
Warning("KeyValues: Element uses unknown element type %s\n", pElementType );
return DMELEMENT_HANDLE_INVALID;
}
m_ElementList.AddToTail( hElement );
CDmElement *pElement = g_pDataModel->GetElement( hElement );
CDmeElementAccessor::MarkBeingUnserialized( pElement, true );
return hElement;
}
//-----------------------------------------------------------------------------
// Deterimines the attribute type of a keyvalue
//-----------------------------------------------------------------------------
DmAttributeType_t CDmSerializerKeyValues::DetermineAttributeType( KeyValues *pKeyValues )
{
// FIXME: Add detection of vectors/matrices?
switch( pKeyValues->GetDataType() )
{
default:
case KeyValues::TYPE_NONE:
Assert( 0 );
return AT_UNKNOWN;
case KeyValues::TYPE_STRING:
{
float f1, f2, f3, f4;
if ( sscanf( pKeyValues->GetString(), "%f %f %f %f", &f1, &f2, &f3, &f4 ) == 4 )
return AT_VECTOR4;
if ( sscanf( pKeyValues->GetString(), "%f %f %f",&f1, &f2, &f3 ) == 3 )
return AT_VECTOR3;
if ( sscanf( pKeyValues->GetString(), "%f %f", &f1, &f2 ) == 2 )
return AT_VECTOR2;
int i = pKeyValues->GetInt( NULL, INT_MAX );
if ( ( sscanf( pKeyValues->GetString(), "%d", &i ) == 1 ) &&
( !strchr( pKeyValues->GetString(), '.' ) ) )
return AT_INT;
if ( sscanf( pKeyValues->GetString(), "%f", &f1 ) == 1 )
return AT_FLOAT;
return AT_STRING;
}
case KeyValues::TYPE_INT:
return AT_INT;
case KeyValues::TYPE_FLOAT:
return AT_FLOAT;
case KeyValues::TYPE_PTR:
return AT_VOID;
case KeyValues::TYPE_COLOR:
return AT_COLOR;
}
}
//-----------------------------------------------------------------------------
// Reads an attribute for an element
//-----------------------------------------------------------------------------
void CDmSerializerKeyValues::UnserializeAttribute( CDmElement *pElement, KeyValues *pKeyValues )
{
// It's an attribute
const char *pAttributeName = pKeyValues->GetName();
const char *pAttributeValue = pKeyValues->GetString();
// Convert to lower case
CUtlString pLowerName = pAttributeName;
pLowerName.ToLower();
// Rename "type", "name", or "id" fields, since they are special fields
for ( int i = 0; s_pAttributeRemap[i].m_pKeyValuesName; ++i )
{
if ( !Q_stricmp( pLowerName, s_pAttributeRemap[i].m_pKeyValuesName ) )
{
pLowerName = s_pAttributeRemap[i].m_pDmeName;
break;
}
}
// Element types are stored out by GUID, we need to hang onto the guid and
// link it back up once all elements have been loaded from the file
DmAttributeType_t type = DetermineAttributeType( pKeyValues );
// In this case, we have an inlined element or element array attribute
if ( type == AT_UNKNOWN )
{
// Assume this is an empty attribute or attribute array element
Warning("Dm Unserialize: Attempted to read an attribute (\"%s\") of an inappropriate type!\n", pLowerName.Get() );
return;
}
CDmAttribute *pAttribute = pElement->AddAttribute( pLowerName, type );
if ( !pAttribute )
{
Warning("Dm Unserialize: Attempted to read an attribute (\"%s\") of an inappropriate type!\n", pLowerName.Get() );
return;
}
switch( type )
{
case AT_STRING:
{
// Strings have different delimiter rules for KeyValues,
// so let's just directly copy the string instead of going through unserialize
pAttribute->SetValue( pAttributeValue );
}
break;
default:
{
int nLen = Q_strlen( pAttributeValue );
CUtlBuffer buf( pAttributeValue, nLen, CUtlBuffer::TEXT_BUFFER | CUtlBuffer::READ_ONLY );
pAttribute->Unserialize( buf );
}
break;
}
}
//-----------------------------------------------------------------------------
// Reads a single element
//-----------------------------------------------------------------------------
DmElementHandle_t CDmSerializerKeyValues::UnserializeElement( KeyValues *pKeyValues, int iNestingLevel )
{
const char *pElementName = pKeyValues->GetName( );
const char *pszKeyValuesElement = g_pDataModel->GetKeyValuesElementName( pElementName, iNestingLevel );
if ( !pszKeyValuesElement )
{
pszKeyValuesElement = "DmElement";
}
DmElementHandle_t handle = CreateDmElement( pszKeyValuesElement, pElementName );
Assert( handle != DMELEMENT_HANDLE_INVALID );
iNestingLevel++;
CDmElement *pElement = g_pDataModel->GetElement( handle );
CDmrElementArray<> subKeys;
for ( KeyValues *pSub = pKeyValues->GetFirstSubKey(); pSub != NULL ; pSub = pSub->GetNextKey() )
{
// Read in a subkey
if ( pSub->GetDataType() == KeyValues::TYPE_NONE )
{
if ( !subKeys.IsValid() )
{
subKeys.Init( pElement->AddAttribute( "subkeys", AT_ELEMENT_ARRAY ) );
}
DmElementHandle_t hChild = UnserializeElement( pSub, iNestingLevel );
if ( hChild != DMELEMENT_HANDLE_INVALID )
{
subKeys.AddToTail( hChild );
}
}
else
{
UnserializeAttribute( pElement, pSub );
}
}
return handle;
}
//-----------------------------------------------------------------------------
// Main entry point for the unserialization
//-----------------------------------------------------------------------------
CDmElement* CDmSerializerKeyValues::UnserializeFromKeyValues( KeyValues *pKeyValues )
{
m_ElementList.RemoveAll();
m_hRoot = CreateDmElement( "DmElement", "root" );
CDmElement *pRoot = g_pDataModel->GetElement( m_hRoot );
CDmrElementArray<> subkeys( pRoot->AddAttribute( "subkeys", AT_ELEMENT_ARRAY ) );
int iNestingLevel = 0;
for ( KeyValues *pElementKey = pKeyValues; pElementKey != NULL; pElementKey = pElementKey->GetNextKey() )
{
DmElementHandle_t hChild = UnserializeElement( pElementKey, iNestingLevel );
if ( hChild != DMELEMENT_HANDLE_INVALID )
{
subkeys.AddToTail( hChild );
}
}
// mark all unserialized elements as done unserializing, and call Resolve()
int c = m_ElementList.Count();
for ( int i = 0; i < c; ++i )
{
CDmElement *pElement = g_pDataModel->GetElement( m_ElementList[i] );
CDmeElementAccessor::MarkBeingUnserialized( pElement, false );
}
g_pDmElementFrameworkImp->RemoveCleanElementsFromDirtyList( );
m_ElementList.RemoveAll();
return pRoot;
}
//-----------------------------------------------------------------------------
// Main entry point for the unserialization
//-----------------------------------------------------------------------------
bool CDmSerializerKeyValues::Unserialize( CUtlBuffer &buf, const char *pEncodingName, int nEncodingVersion,
const char *pSourceFormatName, int nSourceFormatVersion,
DmFileId_t fileid, DmConflictResolution_t idConflictResolution, CDmElement **ppRoot )
{
Assert( !V_stricmp( pEncodingName, "keyvalues" ) );
*ppRoot = NULL;
KeyValues *kv = new KeyValues( "keyvalues file" );
if ( !kv )
return false;
m_fileid = fileid;
bool bOk = kv->LoadFromBuffer( "keyvalues file", buf );
if ( bOk )
{
//SetSerializationDelimiter( GetCStringCharConversion() );
*ppRoot = UnserializeFromKeyValues( kv );
//SetSerializationDelimiter( NULL );
}
m_fileid = DMFILEID_INVALID;
kv->deleteThis();
return bOk;
}
|