aboutsummaryrefslogtreecommitdiff
path: root/sp/src/public/isaverestore.h
blob: bfb40a17f58e6a1dd4b394aaae626974e4312a98 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//

#ifndef ISAVERESTORE_H
#define ISAVERESTORE_H

#include "string_t.h"
#include "datamap.h"
#include "mathlib/vmatrix.h"

#if defined( _WIN32 )
#pragma once
#endif

#ifndef CLIENT_DLL
class SINGLE_INHERITANCE CBaseEntity;
#endif

class Vector;
class VMatrix;
struct edict_t;
template< class T > class CHandle;
typedef CHandle<CBaseEntity> EHANDLE;
struct matrix3x4_t;

class CSaveRestoreData;
class CGameSaveRestoreInfo;

class ISave;
class IRestore;

//-----------------------------------------------------------------------------

#pragma pack(push,1)

struct SaveRestoreRecordHeader_t
{
	unsigned short		size;
	unsigned short		symbol;
};


#pragma pack(pop)

//-----------------------------------------------------------------------------
//
// ISaveRestoreBlockHandler
//
//-----------------------------------------------------------------------------

const int MAX_BLOCK_NAME_LEN = 31;
const int SIZE_BLOCK_NAME_BUF = 31 + 1;

//-------------------------------------

abstract_class ISaveRestoreBlockHandler
{
public:
	virtual const char *GetBlockName() = 0;

	virtual void PreSave( CSaveRestoreData * ) = 0;					// Called immediately prior to save, generally used to set up any necessary tables
	virtual void Save( ISave * ) = 0;
	virtual void WriteSaveHeaders( ISave * ) = 0;					// Called after save to allow the writing out of any dictionaries/tables/indexes generated during save
	virtual void PostSave() = 0;
	
	virtual void PreRestore() = 0;
	virtual void ReadRestoreHeaders( IRestore * ) = 0;				// Called prior to Restore()
	virtual void Restore( IRestore *, bool fCreatePlayers ) = 0;
	virtual void PostRestore() = 0;
};

//-------------------------------------

abstract_class ISaveRestoreBlockSet : public ISaveRestoreBlockHandler
{
public:
	virtual void AddBlockHandler( ISaveRestoreBlockHandler *pHandler ) = 0;
	virtual void RemoveBlockHandler( ISaveRestoreBlockHandler *pHandler ) = 0;
	virtual void CallBlockHandlerRestore( ISaveRestoreBlockHandler *pHandler, int baseFilePos, IRestore *pRestore, bool fCreatePlayers ) = 0;
};

extern ISaveRestoreBlockSet *g_pGameSaveRestoreBlockSet;

//-------------------------------------

abstract_class CDefSaveRestoreBlockHandler : public ISaveRestoreBlockHandler
{
	virtual const char *GetBlockName() = 0;

	virtual void PreSave( CSaveRestoreData * ) {}
	virtual void Save( ISave * ) {}
	virtual void WriteSaveHeaders( ISave * ) {}
	virtual void PostSave() {}
	
	virtual void PreRestore() {}
	virtual void ReadRestoreHeaders( IRestore * ) {}
	virtual void Restore( IRestore *, bool fCreatePlayers ) {}
	virtual void PostRestore() {}
};

//-----------------------------------------------------------------------------
//
// ISave
//
//-----------------------------------------------------------------------------

abstract_class ISave
{
public:

	//---------------------------------
	// Logging
	virtual void	StartLogging( const char *pszLogName ) = 0;
	virtual void	EndLogging( void ) = 0;

	//---------------------------------
	virtual bool	IsAsync() = 0;

	//---------------------------------

	virtual int		GetWritePos() const = 0;
	virtual void	SetWritePos(int pos) = 0;
	
	//---------------------------------
	// Datamap based writing
	//
	
	virtual int		WriteAll( const void *pLeafObject, datamap_t *pLeafMap ) = 0;
	virtual int		WriteFields( const char *pname, const void *pBaseData, datamap_t *pMap, typedescription_t *pFields, int fieldCount ) = 0;

	template <typename T>
	int WriteAll( const T *pLeafObject )
	{
		return WriteAll( pLeafObject, &pLeafObject->m_DataMap );
	}

	//---------------------------------
	// Block support
	//
	// Using these, one doesn't have to worry about queuing up the read pointer on restore if only
	// a subset of the data is read
	//
	
	virtual void	StartBlock( const char *pszBlockName ) = 0;
	virtual void	StartBlock() = 0;
	virtual void	EndBlock() = 0;
	
	//---------------------------------
	// Primitive types
	//
	
	virtual void	WriteShort( const short *value, int count = 1 ) = 0;
	virtual void	WriteInt( const int *value, int count = 1 ) = 0;		// Save an int
	inline void		WriteInt( const unsigned *value, int count = 1 ) { WriteInt( (int *)value, count );	}
	virtual void	WriteBool( const bool *value, int count = 1 ) = 0;		// Save a bool
	virtual void	WriteFloat( const float *value, int count = 1 ) = 0;	// Save a float
	virtual void	WriteData( const char *pdata, int size ) = 0;		// Save a binary data block
	virtual void	WriteString( const char *pstring ) = 0;			// Save a null-terminated string
	virtual void	WriteString( const string_t *stringId, int count = 1 ) = 0;	// Save a null-terminated string (engine string)
	virtual void	WriteVector( const Vector &value ) = 0;				// Save a vector
	virtual void	WriteVector( const Vector *value, int count = 1 ) = 0;	// Save a vector array
	virtual void	WriteQuaternion( const Quaternion &value ) = 0;				// Save a Quaternion
	virtual void	WriteQuaternion( const Quaternion *value, int count = 1 ) = 0;	// Save a Quaternion array

	// Note: All of the following will write out both a header and the data. On restore,
	// this needs to be cracked
	virtual void	WriteShort( const char *pname, const short *value, int count = 1 ) = 0;
	virtual void	WriteInt( const char *pname, const int *value, int count = 1 ) = 0;		// Save an int
	virtual void	WriteBool( const char *pname, const bool *value, int count = 1 ) = 0;		// Save a bool
	virtual void	WriteFloat( const char *pname, const float *value, int count = 1 ) = 0;	// Save a float
	virtual void	WriteData( const char *pname, int size, const char *pdata ) = 0;		// Save a binary data block
	virtual void	WriteString( const char *pname, const char *pstring ) = 0;			// Save a null-terminated string
	virtual void	WriteString( const char *pname, const string_t *stringId, int count = 1 ) = 0;	// Save a null-terminated string (engine string)
	virtual void	WriteVector( const char *pname, const Vector &value ) = 0;				// Save a vector
	virtual void	WriteVector( const char *pname, const Vector *value, int count = 1 ) = 0;	// Save a vector array
	virtual void	WriteQuaternion( const char *pname, const Quaternion &value ) = 0;				// Save a Quaternion
	virtual void	WriteQuaternion( const char *pname, const Quaternion *value, int count = 1 ) = 0;	// Save a Quaternion array
	
	//---------------------------------
	// Game types
	//
	
	virtual void	WriteTime( const char *pname, const float *value, int count = 1 ) = 0;	// Save a float (timevalue)
	virtual void	WriteTick( const char *pname, const int *value, int count = 1 ) = 0;	// Save a tick (timevalue)
	virtual void	WritePositionVector( const char *pname, const Vector &value ) = 0;		// Offset for landmark if necessary
	virtual void	WritePositionVector( const char *pname, const Vector *value, int count = 1 ) = 0;	// array of pos vectors
	virtual void	WriteFunction( datamap_t *pMap, const char *pname, inputfunc_t **value, int count = 1 ) = 0; // Save a function pointer

	virtual void	WriteTime( const float *value, int count = 1 ) = 0;	// Save a float (timevalue)
	virtual void	WriteTick( const int *value, int count = 1 ) = 0;	// Save a tick (timevalue)
	virtual void	WritePositionVector( const Vector &value ) = 0;		// Offset for landmark if necessary
	virtual void	WritePositionVector( const Vector *value, int count = 1 ) = 0;	// array of pos vectors

	virtual void	WriteEntityPtr( const char *pname, CBaseEntity **ppEntity, int count = 1 ) = 0;
	virtual void	WriteEdictPtr( const char *pname, edict_t **ppEdict, int count = 1 ) = 0;
	virtual void	WriteEHandle( const char *pname, const EHANDLE *pEHandle, int count = 1 ) = 0;

	virtual void	WriteEntityPtr( CBaseEntity **ppEntity, int count = 1 ) = 0;
	virtual void	WriteEdictPtr( edict_t **ppEdict, int count = 1 ) = 0;
	virtual void	WriteEHandle( const EHANDLE *pEHandle, int count = 1 ) = 0;

	//---------------------------------
	// Back door to support somewhat awkward ownership of game save/restore data
	virtual CGameSaveRestoreInfo *GetGameSaveRestoreInfo() = 0;

protected:
	virtual ~ISave() {};
};

//-----------------------------------------------------------------------------
//
// IRestore
//
//-----------------------------------------------------------------------------

abstract_class IRestore
{
public:

	//---------------------------------

	virtual int		GetReadPos() const = 0;
	virtual void	SetReadPos( int pos ) = 0;

	//---------------------------------
	// Datamap based reading
	//
	
	virtual int		ReadAll( void *pLeafObject, datamap_t *pLeafMap ) = 0;
	
	virtual int		ReadFields( const char *pname, void *pBaseData, datamap_t *pMap, typedescription_t *pFields, int fieldcount = 1 ) = 0;
	virtual void	EmptyFields( void *pBaseData, typedescription_t *pFields, int fieldcount = 1 ) = 0;

	template <typename T>
	int ReadAll( T *pLeafObject )
	{
		return ReadAll( pLeafObject, &pLeafObject->m_DataMap );
	}
	
	//---------------------------------
	// Block support
	//
	
	virtual void	StartBlock( SaveRestoreRecordHeader_t *pHeader ) = 0;
	virtual void	StartBlock( char szBlockName[SIZE_BLOCK_NAME_BUF] ) = 0;
	virtual void	StartBlock() = 0;
	virtual void	EndBlock() = 0;
	
	//---------------------------------
	// Field header cracking
	//
	
	virtual void	ReadHeader( SaveRestoreRecordHeader_t *pheader ) = 0;
	virtual int		SkipHeader() = 0; // skips the header, but returns the size of the field
	virtual const char *StringFromHeaderSymbol( int symbol ) = 0;

	//---------------------------------
	// Primitive types
	//
	
	virtual short	ReadShort( void ) = 0;
	virtual int		ReadShort( short *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
	virtual int		ReadInt( int *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
	inline  int		ReadInt( unsigned *pValue, int count = 1, int nBytesAvailable = 0 ) { return ReadInt( (int *)pValue, count, nBytesAvailable ); }
	virtual int		ReadInt( void ) = 0;
	virtual int		ReadBool( bool *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
	virtual int		ReadFloat( float *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
	virtual int		ReadData( char *pData, int size, int nBytesAvailable ) = 0;
	virtual void	ReadString( char *pDest, int nSizeDest, int nBytesAvailable ) = 0;			// A null-terminated string
	virtual int		ReadString( string_t *pString, int count = 1, int nBytesAvailable = 0 ) = 0;
	virtual int		ReadVector( Vector *pValue ) = 0;
	virtual int		ReadVector( Vector *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
	virtual int		ReadQuaternion( Quaternion *pValue ) = 0;
	virtual int		ReadQuaternion( Quaternion *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
	
	//---------------------------------
	// Game types
	//
	
	virtual int		ReadTime( float *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
	virtual int		ReadTick( int *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
	virtual int		ReadPositionVector( Vector *pValue ) = 0;
	virtual int		ReadPositionVector( Vector *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
	virtual int		ReadFunction( datamap_t *pMap, inputfunc_t **pValue, int count = 1, int nBytesAvailable = 0) = 0;
	
	virtual int		ReadEntityPtr( CBaseEntity **ppEntity, int count = 1, int nBytesAvailable = 0 ) = 0;
	virtual int		ReadEdictPtr( edict_t **ppEdict, int count = 1, int nBytesAvailable = 0 ) = 0;
	virtual int		ReadEHandle( EHANDLE *pEHandle, int count = 1, int nBytesAvailable = 0 ) = 0;
	virtual int		ReadVMatrix( VMatrix *pValue, int count = 1, int nBytesAvailable = 0) = 0;
	virtual int		ReadVMatrixWorldspace( VMatrix *pValue, int count = 1, int nBytesAvailable = 0) = 0;
	virtual int		ReadMatrix3x4Worldspace( matrix3x4_t *pValue, int nElems = 1, int nBytesAvailable = 0 ) = 0;

	//---------------------------------

	virtual bool	GetPrecacheMode( void ) = 0;

	//---------------------------------
	// Back door to support somewhat awkward ownership of game save/restore data
	virtual CGameSaveRestoreInfo *GetGameSaveRestoreInfo() = 0;

protected:
	virtual ~IRestore() {};
};

//-----------------------------------------------------------------------------
// Purpose: The operations necessary to save and restore custom types (FIELD_CUSTOM)
//
//

struct SaveRestoreFieldInfo_t
{
	void *			   pField;

	// Note that it is legal for the following two fields to be NULL,
	// though it may be disallowed by implementors of ISaveRestoreOps
	void *			   pOwner;
	typedescription_t *pTypeDesc;
};

abstract_class ISaveRestoreOps
{
public:
	// save data type interface
	virtual void Save( const SaveRestoreFieldInfo_t &fieldInfo, ISave *pSave ) = 0;
	virtual void Restore( const SaveRestoreFieldInfo_t &fieldInfo, IRestore *pRestore ) = 0;

	virtual bool IsEmpty( const SaveRestoreFieldInfo_t &fieldInfo ) = 0;
	virtual void MakeEmpty( const SaveRestoreFieldInfo_t &fieldInfo ) = 0;
	virtual bool Parse( const SaveRestoreFieldInfo_t &fieldInfo, char const* szValue ) = 0;

	//---------------------------------

	void Save( void *pField, ISave *pSave )				{ SaveRestoreFieldInfo_t fieldInfo = { pField, NULL, NULL }; Save( fieldInfo, pSave ); }
	void Restore( void *pField, IRestore *pRestore )	{ SaveRestoreFieldInfo_t fieldInfo = { pField, NULL, NULL }; Restore( fieldInfo, pRestore ); }

	bool IsEmpty( void *pField)							{ SaveRestoreFieldInfo_t fieldInfo = { pField, NULL, NULL }; return IsEmpty( fieldInfo ); }
	void MakeEmpty( void *pField)						{ SaveRestoreFieldInfo_t fieldInfo = { pField, NULL, NULL }; MakeEmpty( fieldInfo ); }
	bool Parse( void *pField, char const *pszValue )	{ SaveRestoreFieldInfo_t fieldInfo = { pField, NULL, NULL }; return Parse( fieldInfo, pszValue ); }
};

//-------------------------------------

class CDefSaveRestoreOps : public ISaveRestoreOps
{
public:
	// save data type interface
	virtual void Save( const SaveRestoreFieldInfo_t &fieldInfo, ISave *pSave ) {}
	virtual void Restore( const SaveRestoreFieldInfo_t &fieldInfo, IRestore *pRestore ) {}

	virtual bool IsEmpty( const SaveRestoreFieldInfo_t &fieldInfo ) { return false; }
	virtual void MakeEmpty( const SaveRestoreFieldInfo_t &fieldInfo ) {}
	virtual bool Parse( const SaveRestoreFieldInfo_t &fieldInfo, char const* szValue ) { return false; }
};


//-----------------------------------------------------------------------------
// Used by ops that deal with pointers
//-----------------------------------------------------------------------------
class CClassPtrSaveRestoreOps : public CDefSaveRestoreOps
{
public:
	virtual bool IsEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
	{
		void **ppClassPtr = (void **)fieldInfo.pField;
		int nObjects = fieldInfo.pTypeDesc->fieldSize;
		for ( int i = 0; i < nObjects; i++ )
		{
			if ( ppClassPtr[i] != NULL )
				return false;
		}
		return true;
	}

	virtual void MakeEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
	{
		memset( fieldInfo.pField, 0, fieldInfo.pTypeDesc->fieldSize * sizeof( void * ) );
	}
};


//=============================================================================

#endif // ISAVERESTORE_H