Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

392 lines
15 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef ISAVERESTORE_H
  8. #define ISAVERESTORE_H
  9. #include "string_t.h"
  10. #include "datamap.h"
  11. #include "mathlib/vmatrix.h"
  12. #if defined( _WIN32 )
  13. #pragma once
  14. #endif
  15. #ifndef CLIENT_DLL
  16. class SINGLE_INHERITANCE CBaseEntity;
  17. #endif
  18. class Vector;
  19. class VMatrix;
  20. struct edict_t;
  21. template< class T > class CHandle;
  22. typedef CHandle<CBaseEntity> EHANDLE;
  23. struct matrix3x4_t;
  24. class CSaveRestoreData;
  25. class CGameSaveRestoreInfo;
  26. class ISave;
  27. class IRestore;
  28. //-----------------------------------------------------------------------------
  29. #pragma pack(push,1)
  30. struct SaveRestoreRecordHeader_t
  31. {
  32. unsigned short size;
  33. unsigned short symbol;
  34. };
  35. #pragma pack(pop)
  36. //-----------------------------------------------------------------------------
  37. //
  38. // ISaveRestoreBlockHandler
  39. //
  40. //-----------------------------------------------------------------------------
  41. const int MAX_BLOCK_NAME_LEN = 31;
  42. const int SIZE_BLOCK_NAME_BUF = 31 + 1;
  43. //-------------------------------------
  44. abstract_class ISaveRestoreBlockHandler
  45. {
  46. public:
  47. virtual const char *GetBlockName() = 0;
  48. virtual void PreSave( CSaveRestoreData * ) = 0; // Called immediately prior to save, generally used to set up any necessary tables
  49. virtual void Save( ISave * ) = 0;
  50. virtual void WriteSaveHeaders( ISave * ) = 0; // Called after save to allow the writing out of any dictionaries/tables/indexes generated during save
  51. virtual void PostSave() = 0;
  52. virtual void PreRestore() = 0;
  53. virtual void ReadRestoreHeaders( IRestore * ) = 0; // Called prior to Restore()
  54. virtual void Restore( IRestore *, bool fCreatePlayers ) = 0;
  55. virtual void PostRestore() = 0;
  56. };
  57. //-------------------------------------
  58. abstract_class ISaveRestoreBlockSet : public ISaveRestoreBlockHandler
  59. {
  60. public:
  61. virtual void AddBlockHandler( ISaveRestoreBlockHandler *pHandler ) = 0;
  62. virtual void RemoveBlockHandler( ISaveRestoreBlockHandler *pHandler ) = 0;
  63. virtual void CallBlockHandlerRestore( ISaveRestoreBlockHandler *pHandler, int baseFilePos, IRestore *pRestore, bool fCreatePlayers ) = 0;
  64. };
  65. extern ISaveRestoreBlockSet *g_pGameSaveRestoreBlockSet;
  66. //-------------------------------------
  67. abstract_class CDefSaveRestoreBlockHandler : public ISaveRestoreBlockHandler
  68. {
  69. virtual const char *GetBlockName() = 0;
  70. virtual void PreSave( CSaveRestoreData * ) {}
  71. virtual void Save( ISave * ) {}
  72. virtual void WriteSaveHeaders( ISave * ) {}
  73. virtual void PostSave() {}
  74. virtual void PreRestore() {}
  75. virtual void ReadRestoreHeaders( IRestore * ) {}
  76. virtual void Restore( IRestore *, bool fCreatePlayers ) {}
  77. virtual void PostRestore() {}
  78. };
  79. //-----------------------------------------------------------------------------
  80. //
  81. // ISave
  82. //
  83. //-----------------------------------------------------------------------------
  84. abstract_class ISave
  85. {
  86. public:
  87. //---------------------------------
  88. // Logging
  89. virtual void StartLogging( const char *pszLogName ) = 0;
  90. virtual void EndLogging( void ) = 0;
  91. //---------------------------------
  92. virtual bool IsAsync() = 0;
  93. //---------------------------------
  94. virtual int GetWritePos() const = 0;
  95. virtual void SetWritePos(int pos) = 0;
  96. //---------------------------------
  97. // Datamap based writing
  98. //
  99. virtual int WriteAll( const void *pLeafObject, datamap_t *pLeafMap ) = 0;
  100. virtual int WriteFields( const char *pname, const void *pBaseData, datamap_t *pMap, typedescription_t *pFields, int fieldCount ) = 0;
  101. template <typename T>
  102. int WriteAll( const T *pLeafObject )
  103. {
  104. return WriteAll( pLeafObject, &pLeafObject->m_DataMap );
  105. }
  106. //---------------------------------
  107. // Block support
  108. //
  109. // Using these, one doesn't have to worry about queuing up the read pointer on restore if only
  110. // a subset of the data is read
  111. //
  112. virtual void StartBlock( const char *pszBlockName ) = 0;
  113. virtual void StartBlock() = 0;
  114. virtual void EndBlock() = 0;
  115. //---------------------------------
  116. // Primitive types
  117. //
  118. virtual void WriteShort( const short *value, int count = 1 ) = 0;
  119. virtual void WriteInt( const int *value, int count = 1 ) = 0; // Save an int
  120. inline void WriteInt( const unsigned *value, int count = 1 ) { WriteInt( (int *)value, count ); }
  121. virtual void WriteBool( const bool *value, int count = 1 ) = 0; // Save a bool
  122. virtual void WriteFloat( const float *value, int count = 1 ) = 0; // Save a float
  123. virtual void WriteData( const char *pdata, int size ) = 0; // Save a binary data block
  124. virtual void WriteString( const char *pstring ) = 0; // Save a null-terminated string
  125. virtual void WriteString( const string_t *stringId, int count = 1 ) = 0; // Save a null-terminated string (engine string)
  126. virtual void WriteVector( const Vector &value ) = 0; // Save a vector
  127. virtual void WriteVector( const Vector *value, int count = 1 ) = 0; // Save a vector array
  128. virtual void WriteQuaternion( const Quaternion &value ) = 0; // Save a Quaternion
  129. virtual void WriteQuaternion( const Quaternion *value, int count = 1 ) = 0; // Save a Quaternion array
  130. // Note: All of the following will write out both a header and the data. On restore,
  131. // this needs to be cracked
  132. virtual void WriteShort( const char *pname, const short *value, int count = 1 ) = 0;
  133. virtual void WriteInt( const char *pname, const int *value, int count = 1 ) = 0; // Save an int
  134. virtual void WriteBool( const char *pname, const bool *value, int count = 1 ) = 0; // Save a bool
  135. virtual void WriteFloat( const char *pname, const float *value, int count = 1 ) = 0; // Save a float
  136. virtual void WriteData( const char *pname, int size, const char *pdata ) = 0; // Save a binary data block
  137. virtual void WriteString( const char *pname, const char *pstring ) = 0; // Save a null-terminated string
  138. virtual void WriteString( const char *pname, const string_t *stringId, int count = 1 ) = 0; // Save a null-terminated string (engine string)
  139. virtual void WriteVector( const char *pname, const Vector &value ) = 0; // Save a vector
  140. virtual void WriteVector( const char *pname, const Vector *value, int count = 1 ) = 0; // Save a vector array
  141. virtual void WriteQuaternion( const char *pname, const Quaternion &value ) = 0; // Save a Quaternion
  142. virtual void WriteQuaternion( const char *pname, const Quaternion *value, int count = 1 ) = 0; // Save a Quaternion array
  143. //---------------------------------
  144. // Game types
  145. //
  146. virtual void WriteTime( const char *pname, const float *value, int count = 1 ) = 0; // Save a float (timevalue)
  147. virtual void WriteTick( const char *pname, const int *value, int count = 1 ) = 0; // Save a tick (timevalue)
  148. virtual void WritePositionVector( const char *pname, const Vector &value ) = 0; // Offset for landmark if necessary
  149. virtual void WritePositionVector( const char *pname, const Vector *value, int count = 1 ) = 0; // array of pos vectors
  150. virtual void WriteFunction( datamap_t *pMap, const char *pname, inputfunc_t const *value, int count = 1 ) = 0; // Save a function pointer
  151. virtual void WriteTime( const float *value, int count = 1 ) = 0; // Save a float (timevalue)
  152. virtual void WriteTick( const int *value, int count = 1 ) = 0; // Save a tick (timevalue)
  153. virtual void WritePositionVector( const Vector &value ) = 0; // Offset for landmark if necessary
  154. virtual void WritePositionVector( const Vector *value, int count = 1 ) = 0; // array of pos vectors
  155. virtual void WriteEntityPtr( const char *pname, CBaseEntity **ppEntity, int count = 1 ) = 0;
  156. virtual void WriteEdictPtr( const char *pname, edict_t **ppEdict, int count = 1 ) = 0;
  157. virtual void WriteEHandle( const char *pname, const EHANDLE *pEHandle, int count = 1 ) = 0;
  158. virtual void WriteEntityPtr( CBaseEntity **ppEntity, int count = 1 ) = 0;
  159. virtual void WriteEdictPtr( edict_t **ppEdict, int count = 1 ) = 0;
  160. virtual void WriteEHandle( const EHANDLE *pEHandle, int count = 1 ) = 0;
  161. //---------------------------------
  162. // Back door to support somewhat awkward ownership of game save/restore data
  163. virtual CGameSaveRestoreInfo *GetGameSaveRestoreInfo() = 0;
  164. protected:
  165. virtual ~ISave() {};
  166. };
  167. //-----------------------------------------------------------------------------
  168. //
  169. // IRestore
  170. //
  171. //-----------------------------------------------------------------------------
  172. abstract_class IRestore
  173. {
  174. public:
  175. //---------------------------------
  176. virtual int GetReadPos() const = 0;
  177. virtual void SetReadPos( int pos ) = 0;
  178. //---------------------------------
  179. // Datamap based reading
  180. //
  181. virtual int ReadAll( void *pLeafObject, datamap_t *pLeafMap ) = 0;
  182. virtual int ReadFields( const char *pname, void *pBaseData, datamap_t *pMap, typedescription_t *pFields, int fieldcount = 1 ) = 0;
  183. virtual void EmptyFields( void *pBaseData, typedescription_t *pFields, int fieldcount = 1 ) = 0;
  184. template <typename T>
  185. int ReadAll( T *pLeafObject )
  186. {
  187. return ReadAll( pLeafObject, &pLeafObject->m_DataMap );
  188. }
  189. //---------------------------------
  190. // Block support
  191. //
  192. virtual void StartBlock( SaveRestoreRecordHeader_t *pHeader ) = 0;
  193. virtual void StartBlock( char szBlockName[SIZE_BLOCK_NAME_BUF] ) = 0;
  194. virtual void StartBlock() = 0;
  195. virtual void EndBlock() = 0;
  196. //---------------------------------
  197. // Field header cracking
  198. //
  199. virtual void ReadHeader( SaveRestoreRecordHeader_t *pheader ) = 0;
  200. virtual int SkipHeader() = 0; // skips the header, but returns the size of the field
  201. virtual const char *StringFromHeaderSymbol( int symbol ) = 0;
  202. //---------------------------------
  203. // Primitive types
  204. //
  205. virtual short ReadShort( void ) = 0;
  206. virtual int ReadShort( short *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
  207. virtual int ReadInt( int *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
  208. inline int ReadInt( unsigned *pValue, int count = 1, int nBytesAvailable = 0 ) { return ReadInt( (int *)pValue, count, nBytesAvailable ); }
  209. virtual int ReadInt( void ) = 0;
  210. virtual int ReadBool( bool *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
  211. virtual int ReadFloat( float *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
  212. virtual int ReadData( char *pData, int size, int nBytesAvailable ) = 0;
  213. virtual void ReadString( char *pDest, int nSizeDest, int nBytesAvailable ) = 0; // A null-terminated string
  214. virtual int ReadString( string_t *pString, int count = 1, int nBytesAvailable = 0 ) = 0;
  215. virtual int ReadVector( Vector *pValue ) = 0;
  216. virtual int ReadVector( Vector *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
  217. virtual int ReadQuaternion( Quaternion *pValue ) = 0;
  218. virtual int ReadQuaternion( Quaternion *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
  219. //---------------------------------
  220. // Game types
  221. //
  222. virtual int ReadTime( float *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
  223. virtual int ReadTick( int *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
  224. virtual int ReadPositionVector( Vector *pValue ) = 0;
  225. virtual int ReadPositionVector( Vector *pValue, int count = 1, int nBytesAvailable = 0 ) = 0;
  226. virtual int ReadFunction( datamap_t *pMap, inputfunc_t *pValue, int count = 1, int nBytesAvailable = 0) = 0;
  227. virtual int ReadEntityPtr( CBaseEntity **ppEntity, int count = 1, int nBytesAvailable = 0 ) = 0;
  228. virtual int ReadEdictPtr( edict_t **ppEdict, int count = 1, int nBytesAvailable = 0 ) = 0;
  229. virtual int ReadEHandle( EHANDLE *pEHandle, int count = 1, int nBytesAvailable = 0 ) = 0;
  230. virtual int ReadVMatrix( VMatrix *pValue, int count = 1, int nBytesAvailable = 0) = 0;
  231. virtual int ReadVMatrixWorldspace( VMatrix *pValue, int count = 1, int nBytesAvailable = 0) = 0;
  232. virtual int ReadMatrix3x4Worldspace( matrix3x4_t *pValue, int nElems = 1, int nBytesAvailable = 0 ) = 0;
  233. // Used by Foundry to restore a certain entity's data (with Foundry data) while loading a savegame.
  234. // Returns -1 if not found.
  235. virtual int ScanAheadForHammerID() = 0;
  236. virtual void SkipEntityData() = 0; // This skips the current entity's data.
  237. //---------------------------------
  238. virtual bool GetPrecacheMode( void ) = 0;
  239. //---------------------------------
  240. // Back door to support somewhat awkward ownership of game save/restore data
  241. virtual CGameSaveRestoreInfo *GetGameSaveRestoreInfo() = 0;
  242. protected:
  243. virtual ~IRestore() {};
  244. };
  245. //-----------------------------------------------------------------------------
  246. // Purpose: The operations necessary to save and restore custom types (FIELD_CUSTOM)
  247. //
  248. //
  249. struct SaveRestoreFieldInfo_t
  250. {
  251. void * pField;
  252. // Note that it is legal for the following two fields to be NULL,
  253. // though it may be disallowed by implementors of ISaveRestoreOps
  254. void * pOwner;
  255. typedescription_t *pTypeDesc;
  256. };
  257. abstract_class ISaveRestoreOps
  258. {
  259. public:
  260. // save data type interface
  261. virtual void Save( const SaveRestoreFieldInfo_t &fieldInfo, ISave *pSave ) = 0;
  262. virtual void Restore( const SaveRestoreFieldInfo_t &fieldInfo, IRestore *pRestore ) = 0;
  263. virtual bool IsEmpty( const SaveRestoreFieldInfo_t &fieldInfo ) = 0;
  264. virtual void MakeEmpty( const SaveRestoreFieldInfo_t &fieldInfo ) = 0;
  265. virtual bool Parse( const SaveRestoreFieldInfo_t &fieldInfo, char const* szValue ) = 0;
  266. //---------------------------------
  267. void Save( void *pField, ISave *pSave ) { SaveRestoreFieldInfo_t fieldInfo = { pField, NULL, NULL }; Save( fieldInfo, pSave ); }
  268. void Restore( void *pField, IRestore *pRestore ) { SaveRestoreFieldInfo_t fieldInfo = { pField, NULL, NULL }; Restore( fieldInfo, pRestore ); }
  269. bool IsEmpty( void *pField) { SaveRestoreFieldInfo_t fieldInfo = { pField, NULL, NULL }; return IsEmpty( fieldInfo ); }
  270. void MakeEmpty( void *pField) { SaveRestoreFieldInfo_t fieldInfo = { pField, NULL, NULL }; MakeEmpty( fieldInfo ); }
  271. bool Parse( void *pField, char const *pszValue ) { SaveRestoreFieldInfo_t fieldInfo = { pField, NULL, NULL }; return Parse( fieldInfo, pszValue ); }
  272. };
  273. //-------------------------------------
  274. class CDefSaveRestoreOps : public ISaveRestoreOps
  275. {
  276. public:
  277. // save data type interface
  278. virtual void Save( const SaveRestoreFieldInfo_t &fieldInfo, ISave *pSave ) {}
  279. virtual void Restore( const SaveRestoreFieldInfo_t &fieldInfo, IRestore *pRestore ) {}
  280. virtual bool IsEmpty( const SaveRestoreFieldInfo_t &fieldInfo ) { return false; }
  281. virtual void MakeEmpty( const SaveRestoreFieldInfo_t &fieldInfo ) {}
  282. virtual bool Parse( const SaveRestoreFieldInfo_t &fieldInfo, char const* szValue ) { return false; }
  283. };
  284. //-----------------------------------------------------------------------------
  285. // Used by ops that deal with pointers
  286. //-----------------------------------------------------------------------------
  287. class CClassPtrSaveRestoreOps : public CDefSaveRestoreOps
  288. {
  289. public:
  290. virtual bool IsEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
  291. {
  292. void **ppClassPtr = (void **)fieldInfo.pField;
  293. int nObjects = fieldInfo.pTypeDesc->fieldSize;
  294. for ( int i = 0; i < nObjects; i++ )
  295. {
  296. if ( ppClassPtr[i] != NULL )
  297. return false;
  298. }
  299. return true;
  300. }
  301. virtual void MakeEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
  302. {
  303. memset( fieldInfo.pField, 0, fieldInfo.pTypeDesc->fieldSize * sizeof( void * ) );
  304. }
  305. };
  306. //=============================================================================
  307. #endif // ISAVERESTORE_H