-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathdirectory.cpp
526 lines (435 loc) · 17.1 KB
/
directory.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
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
//====== Copyright ©, Valve Corporation, All rights reserved. =================
//
// Purpose: Defines a directory for all the GC processes for an app
//
//=============================================================================
#include "stdafx.h"
#include "gcsdk/directory.h"
namespace GCSDK
{
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CGCDirTypeInstance::CGCDirTypeInstance( const char* pszTypeName, uint32 nType, uint32 nInstance, const KeyValues *pKVConfig, const CGCDirProcess* pProcess )
: m_nType( nType )
, m_sTypeName( pszTypeName )
, m_nInstance( nInstance )
, m_pProcess( pProcess )
{
m_pConfig = pKVConfig->MakeCopy();
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CGCDirTypeInstance::~CGCDirTypeInstance()
{
m_pConfig->deleteThis();
}
//-----------------------------------------------------------------------------
// Purpose: Returns the type for this GC
//-----------------------------------------------------------------------------
uint32 CGCDirTypeInstance::GetType() const
{
return m_nType;
}
//-----------------------------------------------------------------------------
const char* CGCDirTypeInstance::GetTypeName() const
{
return m_sTypeName;
}
//-----------------------------------------------------------------------------
// Purpose: Returns the instance index for GCs of this type
//-----------------------------------------------------------------------------
uint32 CGCDirTypeInstance::GetInstance() const
{
return m_nInstance;
}
//-----------------------------------------------------------------------------
// Purpose: Gets any additional configuration data for this GC
//-----------------------------------------------------------------------------
KeyValues * CGCDirTypeInstance::GetConfig() const
{
return m_pConfig;
}
//-----------------------------------------------------------------------------
// Purpose: Gets the box that this GC was associated with
//-----------------------------------------------------------------------------
const CGCDirProcess* CGCDirTypeInstance::GetProcess( ) const
{
return m_pProcess;
}
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CGCDirProcess::CGCDirProcess( uint32 nGCDirIndex, const char *pchName, const char* pchProcessType, const KeyValues *pKVConfig ) :
m_iDirGC( nGCDirIndex ),
m_sName( pchName ),
m_sType( pchProcessType ),
m_nTypeMask( 0 )
{
m_pConfig = pKVConfig->MakeCopy();
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CGCDirProcess::~CGCDirProcess()
{
// Don't need to delete our pointers. They were allocated by the directory
// and will be cleaned up by the directory.
}
//-----------------------------------------------------------------------------
// Purpose: Gets any additional configuration data for this GC
//-----------------------------------------------------------------------------
KeyValues * CGCDirProcess::GetConfig() const
{
return m_pConfig;
}
//-----------------------------------------------------------------------------
// Purpose: Gets the name for this box
//-----------------------------------------------------------------------------
const char *CGCDirProcess::GetName() const
{
return m_sName.Get();
}
const char *CGCDirProcess::GetProcessType() const
{
return m_sType.Get();
}
//-----------------------------------------------------------------------------
// Purpose: Gets the number of GCs assigned to this box
//-----------------------------------------------------------------------------
uint32 CGCDirProcess::GetTypeInstanceCount() const
{
return m_vecGCs.Count();
}
//-----------------------------------------------------------------------------
// Purpose: Gets the specified GC definition
//-----------------------------------------------------------------------------
const CGCDirTypeInstance *CGCDirProcess::GetTypeInstance( uint32 nGCIndex ) const
{
bool bValidIndex = m_vecGCs.IsValidIndex( nGCIndex );
Assert( bValidIndex );
if ( !bValidIndex )
return NULL;
return m_vecGCs[ nGCIndex ];
}
//-----------------------------------------------------------------------------
// Purpose: Adds a GC to this box
//-----------------------------------------------------------------------------
void CGCDirProcess::AddGC( CGCDirTypeInstance *pGC )
{
if ( !pGC )
{
Assert( false );
return;
}
//set this within our type mask
Assert( pGC->GetType() < 64 );
m_nTypeMask |= ( uint64 )1 << pGC->GetType();
m_vecGCs.AddToTail( pGC );
if( !m_vecUniqueTypeList.HasElement( pGC->GetType() ) )
{
m_vecUniqueTypeList.Insert( pGC->GetType() );
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
uint32 CGCDirProcess::GetGCDirIndex() const
{
return m_iDirGC;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CGCDirProcess::HasInstanceOfType( uint32 nType ) const
{
return ( m_nTypeMask & ( ( uint64 )1 << nType ) ) != 0;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
const CUtlSortVector< uint32 >& CGCDirProcess::GetUniqueTypeList() const
{
return m_vecUniqueTypeList;
}
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CDirectory::CDirectory()
: m_bInitialized( false )
, m_mapGCsByType( DefLessFunc( int32 ) )
, m_mapRegisteredGCTypes( DefLessFunc( int32 ) )
{
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CDirectory::~CDirectory()
{
m_vecProcesses.PurgeAndDeleteElements();
m_vecTypeInstances.PurgeAndDeleteElements();
}
//-----------------------------------------------------------------------------
// Purpose: Initializes the directory
//-----------------------------------------------------------------------------
bool CDirectory::BInit( KeyValues *pKVDirectory )
{
Assert( !m_bInitialized );
if ( m_bInitialized )
return false;
if ( NULL == pKVDirectory )
{
AssertMsg( false, "Null KV passed to CDirectory::BInit()" );
return false;
}
CUtlSymbolTable processNamesTable( 0, 16, true );
FOR_EACH_TRUE_SUBKEY( pKVDirectory, pkvProcess )
{
if( 0 != Q_stricmp( pkvProcess->GetName( ), "process" ) )
continue;
//get the name of this process and ensure that it is unique
const char *pchProcessName = pkvProcess->GetString( "name" );
if( !pchProcessName || !pchProcessName[ 0 ] )
{
AssertMsg( false, "Process defined in the config with no name" );
return false;
}
if( processNamesTable.Find( pchProcessName ).IsValid() )
{
AssertMsg( false, "Duplicate box \"%s\" encountered while parsing the config", pchProcessName );
return false;
}
processNamesTable.AddString( pchProcessName );
//get the type associated with this process
const char *pchProcessType = pkvProcess->GetString( "type" );
if( !pchProcessType || !pchProcessType[ 0 ] )
{
AssertMsg( false, "Process %s defined in the config with no process type associated with it", pchProcessName );
return false;
}
//create our new process info
CGCDirProcess *pProcess = new CGCDirProcess( m_vecProcesses.Count( ), pchProcessName, pchProcessType, pkvProcess );
m_vecProcesses.AddToTail( pProcess );
FOR_EACH_SUBKEY( pkvProcess, pkvType )
{
if( 0 != Q_stricmp( pkvType->GetName(), "gc" ) )
continue;
const char *pchGCType = NULL;
if( pkvType->GetFirstSubKey() )
{
pchGCType = pkvType->GetString( "gc" );
}
else
{
pchGCType = pkvType->GetString( );
}
if ( !pchGCType || !pchGCType[0] )
{
AssertMsg( false, "GC defined on box \"%s\" in the config with no type", pchProcessName );
return false;
}
int32 nGCType = GetGCTypeForName( pchGCType );
if ( s_nInvalidGCType == nGCType )
{
AssertMsg( false, "GC defined on box \"%s\" with unknown type \"%s\" encountered while parsing the config", pchProcessName, pchGCType );
return false;
}
//note that to get the count we can't call GetGCCountForType until after we register since otherwise we don't have a type entry
int nGCTypeIndex = m_mapGCsByType.Find( nGCType );
if ( !m_mapGCsByType.IsValidIndex( nGCType ) )
{
nGCTypeIndex = m_mapGCsByType.Insert( nGCType );
}
CGCDirTypeInstance *pGC = new CGCDirTypeInstance( pchGCType, nGCType, m_mapGCsByType[ nGCTypeIndex ].Count(), pkvType, pProcess );
pProcess->AddGC( pGC );
m_vecTypeInstances.AddToTail( pGC );
m_mapGCsByType[ nGCTypeIndex ].AddToTail( pGC );
}
}
// There must be exactly one master GC defined. Make sure it exists
int32 nMasterType = GetGCTypeForName( "master" );
if ( s_nInvalidGCType == nMasterType )
{
AssertMsg( false, "Master GC type is not registered" );
return false;
}
if ( 1 != GetGCCountForType( nMasterType ) )
{
AssertMsg( false, "Incorrect number of master GCs in the config. Expected: 1 Actual: %d", GetGCCountForType( nMasterType ) );
return false;
}
const CGCDirTypeInstance *pGC = GetGCInstanceForType( nMasterType, 0 );
if ( 0 != pGC->GetProcess()->GetGCDirIndex() )
{
AssertMsg( false, "The master GC must be contained within the first GC defined." );
return false;
}
m_bInitialized = true;
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Registers a type of GC
//-----------------------------------------------------------------------------
void CDirectory::RegisterGCType( int32 nTypeID, const char *pchName )
{
Assert( s_nInvalidGCType != nTypeID );
if ( s_nInvalidGCType == nTypeID )
return;
bool bHasElement = m_mapRegisteredGCTypes.IsValidIndex( m_mapRegisteredGCTypes.Find( nTypeID ) );
AssertMsg( !bHasElement, "A GC of type %d has already been registered", nTypeID );
if ( bHasElement )
return;
bHasElement = m_dictRegisteredGCNameToType.HasElement( pchName );
AssertMsg( !bHasElement, "A GC type with name \"%s\" has already been registered", pchName );
if ( bHasElement )
return;
RegisteredGCType_t &gcReg = m_mapRegisteredGCTypes[ m_mapRegisteredGCTypes.Insert( nTypeID ) ];
gcReg.m_strName = pchName;
m_dictRegisteredGCNameToType.Insert( pchName, nTypeID );
}
//-----------------------------------------------------------------------------
// Purpose: Gets the number of boxes hosting GCs in the universe
//-----------------------------------------------------------------------------
uint32 CDirectory::GetProcessCount() const
{
return m_vecProcesses.Count();
}
//-----------------------------------------------------------------------------
// Purpose: Gets the definition for a particular box
//-----------------------------------------------------------------------------
const CGCDirProcess *CDirectory::GetProcess( int32 nIndex ) const
{
if( !m_vecProcesses.IsValidIndex( nIndex ) )
{
Assert( false );
return NULL;
}
return m_vecProcesses[ nIndex ];
}
//-----------------------------------------------------------------------------
// Purpose: Gets the number of GCs in the universe
//-----------------------------------------------------------------------------
uint32 CDirectory::GetGCTypeInstanceCount() const
{
return m_vecTypeInstances.Count();
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
const CGCDirTypeInstance *CDirectory::GetGCTypeInstance( uint32 iDirGC ) const
{
if( iDirGC >= ( uint32 )m_vecTypeInstances.Count() )
{
Assert( false );
return NULL;
}
return m_vecTypeInstances[ iDirGC ];
}
//-----------------------------------------------------------------------------
// Purpose: Gets the number of GCs in the universe of the given type
//-----------------------------------------------------------------------------
int32 CDirectory::GetGCCountForType( int32 nTypeID ) const
{
int nIndex = m_mapGCsByType.Find( nTypeID );
if ( !m_mapGCsByType.IsValidIndex( nIndex ) )
{
EmitWarning( SPEW_GC, 2, "CDirectory::GetGCCountForType() called with unregistered type %d (%s)\n", nTypeID, GetNameForGCType( nTypeID ) );
return 0;
}
return m_mapGCsByType[nIndex].Count();
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
const CGCDirTypeInstance *CDirectory::GetGCInstanceForType( int32 nTypeID, int32 nInstance ) const
{
int nIndex = m_mapGCsByType.Find( nTypeID );
if ( !m_mapGCsByType.IsValidIndex( nIndex ) )
{
EmitWarning( SPEW_GC, 2, "CDirectory::GetGCInstanceForType() called with unregistered type %d (%s)\n", nTypeID, GetNameForGCType( nTypeID ) );
return NULL;
}
const CCopyableUtlVector<CGCDirTypeInstance *> &vecGCs = m_mapGCsByType[ nIndex ];
bool bValidIndex = vecGCs.IsValidIndex( nInstance );
Assert( bValidIndex );
if ( !bValidIndex )
return NULL;
return vecGCs[ nInstance ];
}
//-----------------------------------------------------------------------------
// Purpose: Given a type and index, this will hash it to be a valid in-range index for that type and return the directory index
//-----------------------------------------------------------------------------
int32 CDirectory::GetGCDirIndexForInstance( int32 nTypeID, uint32 nInstanceIndex ) const
{
int nIndex = m_mapGCsByType.Find( nTypeID );
if ( !m_mapGCsByType.IsValidIndex( nIndex ) )
{
EmitWarning( SPEW_GC, 2, "CDirectory::GetGCDirIndexForInstance() called with unregistered type %d (%s)\n", nTypeID, GetNameForGCType( nTypeID ) );
return -1;
}
const CCopyableUtlVector<CGCDirTypeInstance *> &vecGCs = m_mapGCsByType[ nIndex ];
uint32 nWrappedIndex = nInstanceIndex % vecGCs.Count();
return vecGCs[ nWrappedIndex ]->GetProcess()->GetGCDirIndex();
}
//-----------------------------------------------------------------------------
// Purpose: given a GC type, this will add all of the GC indices associated with that type onto the provided vector
//-----------------------------------------------------------------------------
void CDirectory::GetGCDirIndicesForType( int32 nTypeID, CUtlVector< uint32 >& vecIndices ) const
{
int nIndex = m_mapGCsByType.Find( nTypeID );
if ( m_mapGCsByType.IsValidIndex( nIndex ) )
{
const CCopyableUtlVector<CGCDirTypeInstance *> &vecGCs = m_mapGCsByType[ nIndex ];
FOR_EACH_VEC( vecGCs, nGC )
{
vecIndices.AddToTail( vecGCs[ nGC ]->GetProcess()->GetGCDirIndex() );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Gets a name for a registered GC type
//-----------------------------------------------------------------------------
const char *CDirectory::GetNameForGCType( int32 nTypeID ) const
{
int nIndex = m_mapRegisteredGCTypes.Find( nTypeID );
if ( !m_mapRegisteredGCTypes.IsValidIndex( nIndex ) )
return "unknown";
return m_mapRegisteredGCTypes[nIndex].m_strName.Get();
}
//-----------------------------------------------------------------------------
// Purpose: Gets the creation factory for a registered GC types
//-----------------------------------------------------------------------------
CDirectory::GCFactory_t CDirectory::GetFactoryForProcessType( const char* pszProcessType ) const
{
int nIndex = m_dictProcessTypeToFactory.Find( pszProcessType );
if( nIndex == m_dictProcessTypeToFactory.InvalidIndex() )
return NULL;
return m_dictProcessTypeToFactory[ nIndex ];
}
//-----------------------------------------------------------------------------
// Purpose: Registers a factory for a specific process type
//-----------------------------------------------------------------------------
void CDirectory::RegisterProcessTypeFactory( const char* pszProcessType, GCFactory_t pfnFactory )
{
m_dictProcessTypeToFactory.Insert( pszProcessType, pfnFactory );
}
//-----------------------------------------------------------------------------
// Purpose: Gets the type for a GC given a name
//-----------------------------------------------------------------------------
int32 CDirectory::GetGCTypeForName( const char *pchName ) const
{
int nIndex = m_dictRegisteredGCNameToType.Find( pchName );
if ( !m_dictRegisteredGCNameToType.IsValidIndex( nIndex ) )
return s_nInvalidGCType;
return m_dictRegisteredGCNameToType[nIndex];
}
//-----------------------------------------------------------------------------
// Purpose: Gets the global directory singleton
//-----------------------------------------------------------------------------
CDirectory *GDirectory()
{
static CDirectory g_directory;
return &g_directory;
}
} // namespace GCSDK