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.

357 lines
14 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Helper classes and functions for the save/restore system. These
  4. // classes are internally structured to distinguish simple from
  5. // complex types.
  6. //
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. #ifndef SAVERESTORE_H
  10. #define SAVERESTORE_H
  11. #include "isaverestore.h"
  12. #include "utlvector.h"
  13. #include "filesystem.h"
  14. #ifdef _WIN32
  15. #pragma once
  16. #endif
  17. //-------------------------------------
  18. class CSaveRestoreData;
  19. class CSaveRestoreSegment;
  20. class CGameSaveRestoreInfo;
  21. struct typedescription_t;
  22. struct edict_t;
  23. struct datamap_t;
  24. class CBaseEntity;
  25. struct interval_t;
  26. //-----------------------------------------------------------------------------
  27. //
  28. // CSave
  29. //
  30. //-----------------------------------------------------------------------------
  31. class CSave : public ISave
  32. {
  33. public:
  34. CSave( CSaveRestoreData *pdata );
  35. //---------------------------------
  36. // Logging
  37. void StartLogging( const char *pszLogName );
  38. void EndLogging( void );
  39. //---------------------------------
  40. bool IsAsync();
  41. //---------------------------------
  42. int GetWritePos() const;
  43. void SetWritePos(int pos);
  44. //---------------------------------
  45. // Datamap based writing
  46. //
  47. int WriteAll( const void *pLeafObject, datamap_t *pLeafMap ) { return DoWriteAll( pLeafObject, pLeafMap, pLeafMap ); }
  48. int WriteFields( const char *pname, const void *pBaseData, datamap_t *pMap, typedescription_t *pFields, int fieldCount );
  49. //---------------------------------
  50. // Block support
  51. //
  52. virtual void StartBlock( const char *pszBlockName );
  53. virtual void StartBlock();
  54. virtual void EndBlock();
  55. //---------------------------------
  56. // Primitive types
  57. //
  58. void WriteShort( const short *value, int count = 1 );
  59. void WriteInt( const int *value, int count = 1 ); // Save an int
  60. void WriteBool( const bool *value, int count = 1 ); // Save a bool
  61. void WriteFloat( const float *value, int count = 1 ); // Save a float
  62. void WriteData( const char *pdata, int size ); // Save a binary data block
  63. void WriteString( const char *pstring ); // Save a null-terminated string
  64. void WriteString( const string_t *stringId, int count = 1 ); // Save a null-terminated string (engine string)
  65. void WriteVector( const Vector &value ); // Save a vector
  66. void WriteVector( const Vector *value, int count = 1 ); // Save a vector array
  67. void WriteQuaternion( const Quaternion &value ); // Save a Quaternion
  68. void WriteQuaternion( const Quaternion *value, int count = 1 ); // Save a Quaternion array
  69. void WriteVMatrix( const VMatrix *value, int count = 1 ); // Save a vmatrix array
  70. // Note: All of the following will write out both a header and the data. On restore,
  71. // this needs to be cracked
  72. void WriteShort( const char *pname, const short *value, int count = 1 );
  73. void WriteInt( const char *pname, const int *value, int count = 1 ); // Save an int
  74. void WriteBool( const char *pname, const bool *value, int count = 1 ); // Save a bool
  75. void WriteFloat( const char *pname, const float *value, int count = 1 ); // Save a float
  76. void WriteData( const char *pname, int size, const char *pdata ); // Save a binary data block
  77. void WriteString( const char *pname, const char *pstring ); // Save a null-terminated string
  78. void WriteString( const char *pname, const string_t *stringId, int count = 1 ); // Save a null-terminated string (engine string)
  79. void WriteVector( const char *pname, const Vector &value ); // Save a vector
  80. void WriteVector( const char *pname, const Vector *value, int count = 1 ); // Save a vector array
  81. void WriteQuaternion( const char *pname, const Quaternion &value ); // Save a Quaternion
  82. void WriteQuaternion( const char *pname, const Quaternion *value, int count = 1 ); // Save a Quaternion array
  83. void WriteVMatrix( const char *pname, const VMatrix *value, int count = 1 );
  84. //---------------------------------
  85. // Game types
  86. //
  87. void WriteTime( const char *pname, const float *value, int count = 1 ); // Save a float (timevalue)
  88. void WriteTick( const char *pname, const int *value, int count = 1 ); // Save a int (timevalue)
  89. void WritePositionVector( const char *pname, const Vector &value ); // Offset for landmark if necessary
  90. void WritePositionVector( const char *pname, const Vector *value, int count = 1 ); // array of pos vectors
  91. void WriteFunction( datamap_t *pMap, const char *pname, inputfunc_t const *value, int count = 1 ); // Save a function pointer
  92. void WriteEntityPtr( const char *pname, CBaseEntity **ppEntity, int count = 1 );
  93. void WriteEdictPtr( const char *pname, edict_t **ppEdict, int count = 1 );
  94. void WriteEHandle( const char *pname, const EHANDLE *pEHandle, int count = 1 );
  95. virtual void WriteTime( const float *value, int count = 1 ); // Save a float (timevalue)
  96. virtual void WriteTick( const int *value, int count = 1 ); // Save a int (timevalue)
  97. virtual void WritePositionVector( const Vector &value ); // Offset for landmark if necessary
  98. virtual void WritePositionVector( const Vector *value, int count = 1 ); // array of pos vectors
  99. virtual void WriteEntityPtr( CBaseEntity **ppEntity, int count = 1 );
  100. virtual void WriteEdictPtr( edict_t **ppEdict, int count = 1 );
  101. virtual void WriteEHandle( const EHANDLE *pEHandle, int count = 1 );
  102. void WriteVMatrixWorldspace( const char *pname, const VMatrix *value, int count = 1 ); // Save a vmatrix array
  103. void WriteVMatrixWorldspace( const VMatrix *value, int count = 1 ); // Save a vmatrix array
  104. void WriteMatrix3x4Worldspace( const matrix3x4_t *value, int count );
  105. void WriteMatrix3x4Worldspace( const char *pname, const matrix3x4_t *value, int count );
  106. void WriteInterval( const interval_t *value, int count = 1 ); // Save an interval
  107. void WriteInterval( const char *pname, const interval_t *value, int count = 1 );
  108. //---------------------------------
  109. int EntityIndex( const CBaseEntity *pEntity );
  110. int EntityFlagsSet( int entityIndex, int flags );
  111. CGameSaveRestoreInfo *GetGameSaveRestoreInfo() { return m_pGameInfo; }
  112. private:
  113. //---------------------------------
  114. bool IsLogging( void );
  115. void Log( const char *pName, fieldtype_t fieldType, void *value, int count );
  116. //---------------------------------
  117. void BufferField( const char *pname, int size, const char *pdata );
  118. void BufferData( const char *pdata, int size );
  119. void WriteHeader( const char *pname, int size );
  120. int DoWriteAll( const void *pLeafObject, datamap_t *pLeafMap, datamap_t *pCurMap );
  121. bool WriteField( const char *pname, void *pData, datamap_t *pRootMap, typedescription_t *pField );
  122. bool WriteBasicField( const char *pname, void *pData, datamap_t *pRootMap, typedescription_t *pField );
  123. int DataEmpty( const char *pdata, int size );
  124. void BufferString( char *pdata, int len );
  125. int CountFieldsToSave( const void *pBaseData, typedescription_t *pFields, int fieldCount );
  126. bool ShouldSaveField( const void *pData, typedescription_t *pField );
  127. //---------------------------------
  128. // Game info methods
  129. //
  130. bool WriteGameField( const char *pname, void *pData, datamap_t *pRootMap, typedescription_t *pField );
  131. int EntityIndex( const edict_t *pentLookup );
  132. //---------------------------------
  133. CUtlVector<int> m_BlockStartStack;
  134. // Stream data
  135. CSaveRestoreSegment *m_pData;
  136. // Game data
  137. CGameSaveRestoreInfo *m_pGameInfo;
  138. FileHandle_t m_hLogFile;
  139. bool m_bAsync;
  140. };
  141. //-----------------------------------------------------------------------------
  142. //
  143. // CRestore
  144. //
  145. //-----------------------------------------------------------------------------
  146. class CRestore : public IRestore
  147. {
  148. public:
  149. CRestore( CSaveRestoreData *pdata );
  150. int GetReadPos() const;
  151. void SetReadPos( int pos );
  152. //---------------------------------
  153. // Datamap based reading
  154. //
  155. int ReadAll( void *pLeafObject, datamap_t *pLeafMap ) { return DoReadAll( pLeafObject, pLeafMap, pLeafMap ); }
  156. int ReadFields( const char *pname, void *pBaseData, datamap_t *pMap, typedescription_t *pFields, int fieldCount );
  157. void EmptyFields( void *pBaseData, typedescription_t *pFields, int fieldCount );
  158. //---------------------------------
  159. // Block support
  160. //
  161. virtual void StartBlock( SaveRestoreRecordHeader_t *pHeader );
  162. virtual void StartBlock( char szBlockName[SIZE_BLOCK_NAME_BUF] );
  163. virtual void StartBlock();
  164. virtual void EndBlock();
  165. //---------------------------------
  166. // Field header cracking
  167. //
  168. void ReadHeader( SaveRestoreRecordHeader_t *pheader );
  169. int SkipHeader() { SaveRestoreRecordHeader_t header; ReadHeader( &header ); return header.size; }
  170. const char * StringFromHeaderSymbol( int symbol );
  171. //---------------------------------
  172. // Primitive types
  173. //
  174. short ReadShort( void );
  175. int ReadShort( short *pValue, int count = 1, int nBytesAvailable = 0);
  176. int ReadInt( int *pValue, int count = 1, int nBytesAvailable = 0);
  177. int ReadInt( void );
  178. int ReadBool( bool *pValue, int count = 1, int nBytesAvailable = 0);
  179. int ReadFloat( float *pValue, int count = 1, int nBytesAvailable = 0);
  180. int ReadData( char *pData, int size, int nBytesAvailable );
  181. void ReadString( char *pDest, int nSizeDest, int nBytesAvailable ); // A null-terminated string
  182. int ReadString( string_t *pString, int count = 1, int nBytesAvailable = 0);
  183. int ReadVector( Vector *pValue );
  184. int ReadVector( Vector *pValue, int count = 1, int nBytesAvailable = 0);
  185. int ReadQuaternion( Quaternion *pValue );
  186. int ReadQuaternion( Quaternion *pValue, int count = 1, int nBytesAvailable = 0);
  187. int ReadVMatrix( VMatrix *pValue, int count = 1, int nBytesAvailable = 0);
  188. //---------------------------------
  189. // Game types
  190. //
  191. int ReadTime( float *pValue, int count = 1, int nBytesAvailable = 0);
  192. int ReadTick( int *pValue, int count = 1, int nBytesAvailable = 0);
  193. int ReadPositionVector( Vector *pValue );
  194. int ReadPositionVector( Vector *pValue, int count = 1, int nBytesAvailable = 0);
  195. int ReadFunction( datamap_t *pMap, inputfunc_t *pValue, int count = 1, int nBytesAvailable = 0);
  196. int ReadEntityPtr( CBaseEntity **ppEntity, int count = 1, int nBytesAvailable = 0 );
  197. int ReadEdictPtr( edict_t **ppEdict, int count = 1, int nBytesAvailable = 0 );
  198. int ReadEHandle( EHANDLE *pEHandle, int count = 1, int nBytesAvailable = 0 );
  199. int ReadVMatrixWorldspace( VMatrix *pValue, int count = 1, int nBytesAvailable = 0);
  200. int ReadMatrix3x4Worldspace( matrix3x4_t *pValue, int nElems = 1, int nBytesAvailable = 0 );
  201. int ReadInterval( interval_t *interval, int count = 1, int nBytesAvailable = 0 );
  202. //---------------------------------
  203. void SetGlobalMode( int global ) { m_global = global; }
  204. void PrecacheMode( bool mode ) { m_precache = mode; }
  205. virtual int ScanAheadForHammerID();
  206. virtual void SkipEntityData();
  207. bool GetPrecacheMode( void ) { return m_precache; }
  208. CGameSaveRestoreInfo *GetGameSaveRestoreInfo() { return m_pGameInfo; }
  209. private:
  210. //---------------------------------
  211. // Read primitives
  212. //
  213. char * BufferPointer( void );
  214. void BufferSkipBytes( int bytes );
  215. int DoReadAll( void *pLeafObject, datamap_t *pLeafMap, datamap_t *pCurMap );
  216. typedescription_t *FindField( const char *pszFieldName, typedescription_t *pFields, int fieldCount, int *pIterator );
  217. void ReadField( const SaveRestoreRecordHeader_t &header, void *pDest, datamap_t *pRootMap, typedescription_t *pField );
  218. void ReadBasicField( const SaveRestoreRecordHeader_t &header, void *pDest, datamap_t *pRootMap, typedescription_t *pField );
  219. void BufferReadBytes( char *pOutput, int size );
  220. template <typename T>
  221. int ReadSimple( T *pValue, int nElems, int nBytesAvailable ) // must be inline in class to keep MSVS happy
  222. {
  223. int desired = nElems * sizeof(T);
  224. int actual;
  225. if ( nBytesAvailable == 0 )
  226. actual = desired;
  227. else
  228. {
  229. Assert( nBytesAvailable % sizeof(T) == 0 );
  230. actual = MIN( desired, nBytesAvailable );
  231. }
  232. BufferReadBytes( (char *)pValue, actual );
  233. if ( actual < nBytesAvailable )
  234. BufferSkipBytes( nBytesAvailable - actual );
  235. return ( actual / sizeof(T) );
  236. }
  237. bool ShouldReadField( typedescription_t *pField );
  238. bool ShouldEmptyField( typedescription_t *pField );
  239. //---------------------------------
  240. // Game info methods
  241. //
  242. CBaseEntity * EntityFromIndex( int entityIndex );
  243. void ReadGameField( const SaveRestoreRecordHeader_t &header, void *pDest, datamap_t *pRootMap, typedescription_t *pField );
  244. //---------------------------------
  245. CUtlVector<int> m_BlockEndStack;
  246. // Stream data
  247. CSaveRestoreSegment *m_pData;
  248. // Game data
  249. CGameSaveRestoreInfo * m_pGameInfo;
  250. int m_global; // Restoring a global entity?
  251. bool m_precache;
  252. };
  253. //-----------------------------------------------------------------------------
  254. // An interface passed into the OnSave method of all entities
  255. //-----------------------------------------------------------------------------
  256. abstract_class IEntitySaveUtils
  257. {
  258. public:
  259. // Adds a level transition save dependency
  260. virtual void AddLevelTransitionSaveDependency( CBaseEntity *pEntity1, CBaseEntity *pEntity2 ) = 0;
  261. // Gets the # of dependencies for a particular entity
  262. virtual int GetEntityDependencyCount( CBaseEntity *pEntity ) = 0;
  263. // Gets all dependencies for a particular entity
  264. virtual int GetEntityDependencies( CBaseEntity *pEntity, int nCount, CBaseEntity **ppEntList ) = 0;
  265. };
  266. //-----------------------------------------------------------------------------
  267. // Singleton interface
  268. //-----------------------------------------------------------------------------
  269. IEntitySaveUtils *GetEntitySaveUtils();
  270. //=============================================================================
  271. #endif // SAVERESTORE_H