forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenericpersistentmanager.h
More file actions
712 lines (582 loc) · 18.5 KB
/
genericpersistentmanager.h
File metadata and controls
712 lines (582 loc) · 18.5 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
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
//========= Copyright Valve Corporation, All rights reserved. ============//
//
//=======================================================================================//
#ifndef GENERICPERSISTENTMANAGER_H
#define GENERICPERSISTENTMANAGER_H
#ifdef _WIN32
#pragma once
#endif
//----------------------------------------------------------------------------------------
#include "replay/replayhandle.h"
#include "replay/ienginereplay.h"
#include "replay/replayutils.h"
#include "basethinker.h"
#include "utllinkedlist.h"
#include "utlstring.h"
#include "KeyValues.h"
#include "filesystem.h"
#include "convar.h"
#include "replay/ireplayserializeable.h"
#include "replay/ireplaycontext.h"
#include "replay/shared_defs.h"
#include "replay_dbg.h"
#include "vprof.h"
#include "fmtstr.h"
#include "UtlSortVector.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//----------------------------------------------------------------------------------------
extern IEngineReplay *g_pEngine;
//----------------------------------------------------------------------------------------
template< class T >
class CGenericPersistentManager : public CBaseThinker
{
public:
CGenericPersistentManager();
virtual ~CGenericPersistentManager();
virtual bool Init( bool bLoad = true );
virtual void Shutdown();
virtual T *Create() = 0; // Create an object
T *CreateAndGenerateHandle(); // Creates a new object and generates a unique handle
void Add( T *pNewObj ); // Commit the object - NOTE: The Create*() functions don't call Add() for you
void Remove( ReplayHandle_t hObj ); // Remove() will remove the object, remove any .dmx associated with the object on disk, and usually delete attached files (like .dems or movies, etc, depending on what the manager implementation is)
void Remove( T *pObj );
void RemoveFromIndex( int it );
void Clear(); // Remove all objects - NOTE: Doesn't save right away
bool WriteObjToFile( T *pObj, const char *pFilename ); // Write object data to an arbitrary file
bool Save(); // Saves any unsaved data immediately
void FlagIndexForFlush(); // Mark index as dirty
void FlagForFlush( T *pObj, bool bForceImmediate ); // Mark an object as dirty
void FlagForUnload( T *pObj ); // Unload as soon as possible
T *Find( ReplayHandle_t hHandle );
int FindIteratorFromHandle( ReplayHandle_t hHandle );
int Count() const;
bool IsDirty( T *pNewObj );
virtual void Think(); // IReplayThinker implementation - NOTE: not meant to be called directly - called from think manager
virtual const char *GetIndexPath() const; // Should return path where index file lives
private:
class CLessFunctor
{
public:
bool Less( const T *pSrc1, const T *pSrc2, void *pContext )
{
return pSrc1->GetHandle() < pSrc2->GetHandle();
}
};
public:
typedef CUtlSortVector< T *, CLessFunctor > ObjContainer_t;
ObjContainer_t m_vecObjs;
protected:
// For derived classes to implement:
virtual IReplayContext *GetReplayContext() const = 0;
virtual const char *GetRelativeIndexPath() const = 0; // Should return relative (to replay/client or replay/server) path where index file lives - NOTE: Last char should be a slash
virtual const char *GetIndexFilename() const = 0; // Should return just the name of the file, e.g. "replays.dmx"
virtual const char *GetDebugName() const = 0;
virtual bool ShouldDeleteObjects() const { return true; } // TODO: Used by Clear() - I'm not convinced this is needed yet though.
virtual int GetVersion() const = 0;
virtual bool ShouldSerializeToIndividualFiles() const { return true; }
virtual bool ShouldSerializeIndexWithFullPath() const { return false; }
virtual bool ShouldLoadObj( const T *pObj ) const { return true; }
virtual void OnObjLoaded( T *pObj ) {}
virtual int GetHandleBase() const { return 0; } // Subclass can implement this to provide a base/minimum for handles
virtual void PreLoad() {}
const char *GetIndexFullFilename() const; // Should return the full path to the main .dmx file
bool HaveDirtyObjects() const;
bool HaveObjsToUnload() const;
bool ReadObjFromFile( const char *pFile, T *&pOut, bool bForceLoad );
bool Load();
virtual float GetNextThinkTime() const; // IReplayThinker implementation
void FlushThink();
void UnloadThink();
void CreateIndexDir();
void ReadObjFromKeyValues( KeyValues *pObjData );
T* ReadObjFromKeyValues( KeyValues *pObjData, bool bForceLoad );
bool ReadObjFromFile( const char *pFile );
void UpdateHandleSeed( ReplayHandle_t hNewHandle );
typedef CUtlLinkedList< T *, int > ListContainer_t;
ReplayHandle_t m_nHandleSeed;
int m_nVersion;
bool m_bIndexDirty;
ListContainer_t m_lstDirtyObjs;
ListContainer_t m_lstObjsToUnload;
float m_flNextFlushTime;
float m_flNextUnloadTime;
};
//----------------------------------------------------------------------------------------
template< class T >
bool CGenericPersistentManager< T >::Init( bool bLoad/*=true*/ )
{
// Make directory structure is in place
CreateIndexDir();
// Initialize handle seed to start at base
m_nHandleSeed = GetHandleBase();
return bLoad ? Load() : true;
}
template< class T >
void CGenericPersistentManager< T >::Shutdown()
{
Save();
}
template< class T >
CGenericPersistentManager< T >::CGenericPersistentManager()
: m_nHandleSeed( 0 ),
m_nVersion( -1 ),
m_bIndexDirty( false ),
m_flNextFlushTime( 0.0f ),
m_flNextUnloadTime( 0.0f )
{
}
template< class T >
CGenericPersistentManager< T >::~CGenericPersistentManager()
{
Clear();
}
template< class T >
void CGenericPersistentManager< T >::Clear()
{
if ( ShouldDeleteObjects() )
{
m_vecObjs.PurgeAndDeleteElements();
}
else
{
m_vecObjs.RemoveAll();
}
// NOTE: This list contains pointers to objects in m_vecObjs, so no destruction of elements here
m_lstDirtyObjs.RemoveAll();
m_lstObjsToUnload.RemoveAll();
}
template< class T >
int CGenericPersistentManager< T >::Count() const
{
return m_vecObjs.Count();
}
template< class T >
void CGenericPersistentManager< T >::FlagIndexForFlush()
{
m_bIndexDirty = true;
IF_REPLAY_DBG2( Warning( "%f %s: Index flagged\n", g_pEngine->GetHostTime(), GetDebugName() ) );
}
template< class T >
void CGenericPersistentManager< T >::FlagForFlush( T *pObj, bool bForceImmediate )
{
if ( !pObj )
{
AssertMsg( 0, "Trying to flag a NULL object for flush." );
return;
}
// Add to dirty list if it's not already there
if ( m_lstDirtyObjs.Find( pObj ) == m_lstDirtyObjs.InvalidIndex() )
{
m_lstDirtyObjs.AddToTail( pObj );
}
IF_REPLAY_DBG2( Warning( "%f %s: Obj %s flagged for flush\n", g_pEngine->GetHostTime(), GetDebugName(), pObj->GetDebugName() ) );
// Force write now?
if ( bForceImmediate )
{
Save();
}
}
template< class T >
void CGenericPersistentManager< T >::FlagForUnload( T *pObj )
{
AssertMsg(
ShouldSerializeToIndividualFiles(),
"This functionality should only be used for managers that write to individual files, i.e. NOT managers that maintain one monolithic index."
);
if ( !pObj )
{
AssertMsg( 0, "Trying to flag a NULL object for unload." );
return;
}
if ( m_lstObjsToUnload.Find( pObj ) == m_lstObjsToUnload.InvalidIndex() )
{
m_lstObjsToUnload.AddToTail( pObj );
}
IF_REPLAY_DBG2( Warning( "%f %s: Obj %s flagged for unload\n", g_pEngine->GetHostTime(), GetDebugName(), pObj->GetDebugName() ) );
}
template< class T >
bool CGenericPersistentManager< T >::IsDirty( T *pNewObj )
{
return m_lstDirtyObjs.Find( pNewObj ) != m_lstDirtyObjs.InvalidIndex();
}
template< class T >
void CGenericPersistentManager< T >::Add( T *pNewObj )
{
IF_REPLAY_DBG2( Warning( "Adding object with handle %i\n", pNewObj->GetHandle() ) );
Assert( m_vecObjs.Find( pNewObj ) == m_vecObjs.InvalidIndex() );
m_vecObjs.Insert( pNewObj );
FlagIndexForFlush();
FlagForFlush( pNewObj, false );
}
template< class T >
void CGenericPersistentManager< T >::Remove( ReplayHandle_t hObj )
{
int itObj = FindIteratorFromHandle( hObj );
if ( itObj == m_vecObjs.InvalidIndex() )
{
AssertMsg( 0, "Attemting to remove an object which does not exist." );
return;
}
RemoveFromIndex( itObj );
}
template< class T >
void CGenericPersistentManager< T >::Remove( T *pObj )
{
const int it = m_vecObjs.Find( pObj );
if ( it != m_vecObjs.InvalidIndex() )
{
RemoveFromIndex( it );
}
}
template< class T >
void CGenericPersistentManager< T >::RemoveFromIndex( int it )
{
T *pObj = m_vecObjs[ it ]; // NOTE: Constant speed since the implementation of
// CUtlLinkedList indexes into an array
// Remove file associated w/ this object if necessary
if ( ShouldSerializeToIndividualFiles() )
{
CUtlString strFullFilename = pObj->GetFullFilename();
bool bSimulateDelete = false;
#if _DEBUG
extern ConVar replay_fileserver_simulate_delete;
bSimulateDelete = replay_fileserver_simulate_delete.GetBool();
#endif
if ( g_pFullFileSystem->FileExists( strFullFilename.Get() ) && !bSimulateDelete )
{
g_pFullFileSystem->RemoveFile( strFullFilename.Get() );
}
}
Assert( !pObj->IsLocked() );
// Let the object do stuff before it gets deleted
pObj->OnDelete();
// If the object is in the dirty list, remove it - NOTE: this is safe
m_lstDirtyObjs.FindAndRemove( pObj );
// The object should not be in the 'objects-to-unload' list
AssertMsg( m_lstObjsToUnload.Find( pObj ) == m_lstObjsToUnload.InvalidIndex(), "The object being removed was also in the unload list - is this OK? If so, code should be added to remove from that list as well." );
// Remove the object
m_vecObjs.Remove( it );
// Free the object
delete pObj;
FlagIndexForFlush();
}
template< class T >
T *CGenericPersistentManager< T >::Find( ReplayHandle_t hHandle )
{
FOR_EACH_VEC( m_vecObjs, i )
{
T *pCurObj = m_vecObjs[ i ];
if ( hHandle == pCurObj->GetHandle() )
{
return pCurObj;
}
}
return NULL;
}
template< class T >
int CGenericPersistentManager< T >::FindIteratorFromHandle( ReplayHandle_t hHandle )
{
FOR_EACH_VEC( m_vecObjs, i )
{
T *pCurObj = m_vecObjs[ i ];
if ( hHandle == pCurObj->GetHandle() )
{
return i;
}
}
return m_vecObjs.InvalidIndex();
}
template< class T >
bool CGenericPersistentManager< T >::Load()
{
bool bResult = true;
Clear();
PreLoad();
const char *pFullFilename = GetIndexFullFilename();
// Attempt to load from disk
KeyValuesAD pRoot( pFullFilename );
if ( pRoot->LoadFromFile( g_pFullFileSystem, pFullFilename ) )
{
// Get file format version
m_nVersion = pRoot->GetInt( "version", -1 );
if ( m_nVersion != GetVersion() )
{
Warning( "File (%s) has old format (%i).\n", pFullFilename, m_nVersion );
}
// Read from individual files?
if ( ShouldSerializeToIndividualFiles() )
{
KeyValues *pFileIndex = pRoot->FindKey( "files" );
if ( pFileIndex )
{
FOR_EACH_VALUE( pFileIndex, pValue )
{
const char *pName = pValue->GetName();
if ( !ReadObjFromFile( pName ) )
{
Warning( "Failed to load data from file, \"%s\"\n", pName );
}
}
}
else
{
// Peek in directory and load files based on what's there
CFmtStr fmtPath( "%s*.%s", GetIndexPath(), GENERIC_FILE_EXTENSION );
FileFindHandle_t hFind;
const char *pFilename = g_pFullFileSystem->FindFirst( fmtPath.Access(), &hFind );
while ( pFilename )
{
// Ignore index file
if ( V_stricmp( pFilename, GetIndexFilename() ) )
{
if ( !ReadObjFromFile( pFilename ) )
{
Warning( "Failed to load data from file, \"%s\"\n", pFilename );
}
}
pFilename = g_pFullFileSystem->FindNext( hFind );
}
}
}
else
{
FOR_EACH_TRUE_SUBKEY( pRoot, pObjSubKey )
{
// Read data
m_vecObjs.Insert( ReadObjFromKeyValues( pObjSubKey, false ) );
}
}
// Let derived class do any per-object processing.
FOR_EACH_VEC( m_vecObjs, i )
{
OnObjLoaded( m_vecObjs[ i ] );
}
}
return bResult;
}
template< class T >
bool CGenericPersistentManager< T >::WriteObjToFile( T *pObj, const char *pFilename )
{
// Create a keyvalues for the object
KeyValuesAD pObjData( pObj->GetSubKeyTitle() );
// Fill the keyvalues w/ data
pObj->Write( pObjData );
// Attempt to save the current object data to a separate file
if ( !pObjData->SaveToFile( g_pFullFileSystem, pFilename ) )
{
Warning( "Failed to write file %s\n", pFilename );
return false;
}
return true;
}
template< class T >
bool CGenericPersistentManager< T >::Save()
{
IF_REPLAY_DBG2( Warning( "%f %s: Saving now...\n", g_pEngine->GetHostTime(), GetDebugName() ) );
bool bResult = true;
// Add subkey for movies
KeyValuesAD pRoot( "root" );
// Write format version
pRoot->SetInt( "version", GetVersion() );
// Write a file index instead of adding subkeys to the root?
if ( ShouldSerializeToIndividualFiles() )
{
// Go through each object in the dirty list and write to a separate file
FOR_EACH_LL( m_lstDirtyObjs, i )
{
T *pCurObj = m_lstDirtyObjs[ i ];
// Write to the file
bResult = bResult && WriteObjToFile( pCurObj, pCurObj->GetFullFilename() );
}
}
// Write all objects to one monolithic file - writes all objects (ignores "dirtyness")
else
{
FOR_EACH_VEC( m_vecObjs, i )
{
T *pCurObj = m_vecObjs[ i ];
// Create a keyvalues for the object
KeyValues *pCurObjData = new KeyValues( pCurObj->GetSubKeyTitle() );
// Fill the keyvalues w/ data
pCurObj->Write( pCurObjData );
// Add as a subkey to the root keyvalues
pRoot->AddSubKey( pCurObjData );
}
}
// Clear the dirty list
m_lstDirtyObjs.RemoveAll();
// Write the index file if dirty
if ( m_bIndexDirty )
{
return bResult && pRoot->SaveToFile( g_pFullFileSystem, GetIndexFullFilename() );
}
return bResult;
}
template< class T >
T *CGenericPersistentManager< T >::CreateAndGenerateHandle()
{
T *pNewObj = Create();
pNewObj->SetHandle( m_nHandleSeed++ ); Assert( Find( pNewObj->GetHandle() ) == NULL );
FlagIndexForFlush();
return pNewObj;
}
template< class T >
float CGenericPersistentManager< T >::GetNextThinkTime() const
{
// Always think
return 0.0f;
}
template< class T >
void CGenericPersistentManager< T >::Think()
{
VPROF_BUDGET( "CGenericPersistentManager::Think", VPROF_BUDGETGROUP_REPLAY );
CBaseThinker::Think();
FlushThink();
UnloadThink();
}
template< class T >
void CGenericPersistentManager< T >::FlushThink()
{
const float flHostTime = g_pEngine->GetHostTime();
bool bTimeToFlush = flHostTime >= m_flNextFlushTime;
if ( !bTimeToFlush || ( !m_bIndexDirty && !HaveDirtyObjects() ) )
return;
// Flush now and clear dirty objects
Save();
// Reset
m_bIndexDirty = false;
// Setup next flush think
extern ConVar replay_flushinterval;
m_flNextFlushTime = flHostTime + replay_flushinterval.GetInt();
}
template< class T >
void CGenericPersistentManager< T >::UnloadThink()
{
const float flHostTime = g_pEngine->GetHostTime();
bool bTimeToUnload = flHostTime >= m_flNextUnloadTime;
if ( !bTimeToUnload || !HaveObjsToUnload() )
return;
// Unload objects now
FOR_EACH_LL( m_lstObjsToUnload, i )
{
T *pObj = m_lstObjsToUnload[ i ];
// If the object has been marked as locked, don't unload it.
if ( pObj->IsLocked() )
continue;
// If we're waiting to flush the file, don't unload it yet
if ( IsDirty( pObj ) )
continue;
// Let the object do stuff before it gets deleted
pObj->OnUnload();
// Remove the object
m_vecObjs.FindAndRemove( pObj );
IF_REPLAY_DBG( Warning( "Unloading object %s\n", pObj->GetDebugName() ) );
// Free the object
delete pObj;
}
// Clear the list
m_lstObjsToUnload.RemoveAll();
// Think once a second
m_flNextUnloadTime = flHostTime + 1.0f;
}
template< class T >
const char *CGenericPersistentManager< T >::GetIndexPath() const
{
return Replay_va( "%s%s", GetReplayContext()->GetBaseDir(), GetRelativeIndexPath() );
}
template< class T >
const char *CGenericPersistentManager< T >::GetIndexFullFilename() const // Should return the full path to the main .dmx file
{
return Replay_va( "%s%s", GetIndexPath(), GetIndexFilename() );
}
template< class T >
bool CGenericPersistentManager< T >::HaveDirtyObjects() const
{
return m_lstDirtyObjs.Count() > 0;
}
template< class T >
bool CGenericPersistentManager< T >::HaveObjsToUnload() const
{
return m_lstObjsToUnload.Count() > 0;
}
template< class T >
void CGenericPersistentManager< T >::CreateIndexDir()
{
g_pFullFileSystem->CreateDirHierarchy( GetIndexPath(), "DEFAULT_WRITE_PATH" );
}
template< class T >
bool CGenericPersistentManager< T >::ReadObjFromFile( const char *pFile, T *&pOut, bool bForceLoad )
{
// Use the full path and filename specified, or construct it if necessary
CUtlString strFullFilename;
if ( ShouldSerializeIndexWithFullPath() )
{
strFullFilename = pFile;
}
else
{
strFullFilename.Format( "%s%s", GetIndexPath(), pFile );
}
// Attempt to load the file
KeyValuesAD pObjData( pFile );
if ( !pObjData->LoadFromFile( g_pFullFileSystem, strFullFilename.Get() ) )
{
Warning( "Failed to load from file %s\n", strFullFilename.Get() );
AssertMsg( 0, "Manager failed to load something..." );
return false;
}
// Create and read a new object
pOut = ReadObjFromKeyValues( pObjData, bForceLoad );
if ( !pOut )
return NULL;
// Add the object to the manager
m_vecObjs.Insert( pOut );
return true;
}
template< class T >
bool CGenericPersistentManager< T >::ReadObjFromFile( const char *pFile )
{
T *pNewObj;
if ( !ReadObjFromFile( pFile, pNewObj, false ) )
return false;
return true;
}
template< class T >
T* CGenericPersistentManager< T >::ReadObjFromKeyValues( KeyValues *pObjData, bool bForceLoad )
{
T *pNewObj = Create(); Assert( pNewObj );
if ( !pNewObj )
return NULL;
// Attempt to read data for the object, and fail to load this particular object if the reader
// says we should.
if ( !pNewObj->Read( pObjData ) )
{
delete pNewObj;
return NULL;
}
// This object OK to load? Only check if bForceLoad is false.
if ( !bForceLoad && !ShouldLoadObj( pNewObj ) )
{
delete pNewObj;
return NULL;
}
// Sync up handle seed
UpdateHandleSeed( pNewObj->GetHandle() );
return pNewObj;
}
template< class T >
void CGenericPersistentManager< T >::UpdateHandleSeed( ReplayHandle_t hNewHandle )
{
m_nHandleSeed = (ReplayHandle_t)( GetHandleBase() + MAX( (uint32)m_nHandleSeed, (uint32)hNewHandle ) + 1 );
#ifdef _DEBUG
FOR_EACH_VEC( m_vecObjs, i )
{
AssertMsg( m_nHandleSeed != m_vecObjs[ i ]->GetHandle(), "Handle seed collision!" );
}
#endif
}
//----------------------------------------------------------------------------------------
#define FOR_EACH_OBJ( _manager, _i ) FOR_EACH_VEC( _manager->m_vecObjs, _i )
//----------------------------------------------------------------------------------------
#endif // GENERICPERSISTENTMANAGER_H