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.

265 lines
8.1 KiB

  1. //========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Save game read and write. Any *.hl? files may be stored in memory, so use
  4. // g_pSaveRestoreFileSystem when accessing them. The .sav file is always stored
  5. // on disk, so use g_pFileSystem when accessing it.
  6. //
  7. // $Workfile: $
  8. // $Date: $
  9. // $NoKeywords: $
  10. //=============================================================================//
  11. // Save / Restore System
  12. #ifndef CSAVERESTORE_H
  13. #define CSAVERESTORE_H
  14. #ifdef _PS3
  15. #include "ps3/saverestore_ps3_api_ui.h"
  16. #endif
  17. extern ConVar save_noxsave;
  18. //-----------------------------------------------------------------------------
  19. //
  20. //-----------------------------------------------------------------------------
  21. struct GAME_HEADER
  22. {
  23. DECLARE_SIMPLE_DATADESC();
  24. char mapName[32];
  25. char comment[80];
  26. int mapCount; // the number of map state files in the save file. This is usually number of maps * 3 (.hl1, .hl2, .hl3 files)
  27. char originMapName[32];
  28. char landmark[256];
  29. };
  30. struct SAVE_HEADER
  31. {
  32. DECLARE_SIMPLE_DATADESC();
  33. int saveId;
  34. int version;
  35. int skillLevel;
  36. int connectionCount;
  37. int lightStyleCount;
  38. int mapVersion;
  39. float time;
  40. char mapName[32];
  41. char skyName[32];
  42. };
  43. struct SAVELIGHTSTYLE
  44. {
  45. DECLARE_SIMPLE_DATADESC();
  46. int index;
  47. char style[64];
  48. };
  49. extern void FinishAsyncSave();
  50. struct RecentSaveInfo_t
  51. {
  52. RecentSaveInfo_t()
  53. {
  54. m_MostRecentSavePath[0] = 0;
  55. m_MostRecentSaveComment[0] = 0;
  56. m_LastAutosaveDangerousComment[0] = 0;
  57. m_bValid = false;
  58. }
  59. char m_MostRecentSavePath[MAX_OSPATH];
  60. char m_MostRecentSaveComment[80];
  61. char m_LastAutosaveDangerousComment[80];
  62. bool m_bValid;
  63. };
  64. //-----------------------------------------------------------------------------
  65. // Purpose:
  66. //-----------------------------------------------------------------------------
  67. class CSaveRestore : public ISaveRestore
  68. {
  69. public:
  70. CSaveRestore()
  71. {
  72. m_bClearSaveDir = false;
  73. m_szSaveGameScreenshotFile[0] = 0;
  74. SetMostRecentElapsedMinutes( 0 );
  75. SetMostRecentElapsedSeconds( 0 );
  76. m_szMostRecentSaveLoadGame[0] = 0;
  77. m_szSaveGameName[ 0 ] = 0;
  78. m_bIsXSave = IsX360();
  79. m_bOverrideLoadGameEntsOn = false;
  80. #ifdef _PS3
  81. m_PS3AutoSaveAsyncStatus.m_bUseSystemDialogs = true;
  82. #endif
  83. }
  84. void Init( void );
  85. void Shutdown( void );
  86. void OnFrameRendered();
  87. virtual bool SaveFileExists( const char *pName );
  88. bool LoadGame( const char *pName, bool bLetToolsOverrideLoadGameEnts );
  89. virtual bool IsOverrideLoadGameEntsOn();
  90. virtual const char *GetSaveDir(void);
  91. void ClearSaveDir( void );
  92. void DoClearSaveDir( bool bIsXSave );
  93. void RequestClearSaveDir( void );
  94. int LoadGameState( char const *level, bool createPlayers );
  95. void LoadAdjacentEnts( const char *pOldLevel, const char *pLandmarkName );
  96. const char *FindRecentSave( char *pNameBuf, int nameBufLen );
  97. void ForgetRecentSave( void );
  98. int SaveGameSlot( const char *pSaveName, const char *pSaveComment, bool onlyThisLevel, bool bSetMostRecent, const char *pszDestMap = NULL, const char *pszLandmark = NULL );
  99. bool SaveGameState( bool bTransition, ISaveRestoreDataCallback *pCallback = NULL, bool bOpenContainer = true, bool bIsAutosaveOrDangerous = false );
  100. void RestoreClientState( char const *fileName, bool adjacent );
  101. void RestoreAdjacenClientState( char const *map );
  102. int IsValidSave( void );
  103. void Finish( CSaveRestoreData *save );
  104. void ClearRestoredIndexTranslationTables();
  105. void OnFinishedClientRestore();
  106. void AutoSaveDangerousIsSafe();
  107. virtual void UpdateSaveGameScreenshots();
  108. virtual char const *GetMostRecentlyLoadedFileName();
  109. virtual char const *GetSaveFileName();
  110. virtual void SetIsXSave( bool bIsXSave );
  111. #ifdef _PS3
  112. #pragma message("TODO: fix querying of user ID in ps3 (sony has them too)")
  113. virtual bool IsXSave() { return ( m_bIsXSave && !save_noxsave.GetBool() ); }
  114. #else
  115. virtual bool IsXSave() { return ( m_bIsXSave && !save_noxsave.GetBool() && XBX_GetPrimaryUserId() != -1 ); }
  116. #endif
  117. virtual void FinishAsyncSave() { ::FinishAsyncSave(); }
  118. void AddDeferredCommand( char const *pchCommand );
  119. virtual bool StorageDeviceValid( void );
  120. virtual bool IsSaveInProgress();
  121. virtual bool IsAutoSaveDangerousInProgress();
  122. virtual bool IsAutoSaveInProgress();
  123. virtual bool SaveGame( const char *pSaveName, bool bIsXSave, char *pOutName, int nOutNameSize, char *pOutComment, int nOutCommentSize );
  124. void SetMostRecentSaveInfo( const char *pMostRecentSavePath, const char *pMostRecentSaveComment );
  125. bool GetMostRecentSaveInfo( const char **ppMostRecentSavePath, const char **ppMostRecentSaveComment, const char **ppLastAutosaveDangerousComment );
  126. void MarkMostRecentSaveInfoInvalid();
  127. protected:
  128. bool CalcSaveGameName( const char *pName, char *output, int outputStringLength );
  129. private:
  130. bool SaveClientState( const char *name );
  131. void EntityPatchWrite( CSaveRestoreData *pSaveData, const char *level, bool bAsync = false );
  132. void EntityPatchRead( CSaveRestoreData *pSaveData, const char *level );
  133. void DirectoryCount( const char *pPath, int *pResult );
  134. void DirectoryCopy( const char *pPath, const char *pDestFileName, bool bIsXSave );
  135. bool DirectoryExtract( FileHandle_t pFile, int mapCount );
  136. void DirectoryClear( const char *pPath );
  137. void AgeSaveList( const char *pName, int count, bool bIsXSave );
  138. void AgeSaveFile( const char *pName, const char *ext, int count, bool bIsXSave );
  139. int SaveReadHeader( FileHandle_t pFile, GAME_HEADER *pHeader, int readGlobalState );
  140. CSaveRestoreData *LoadSaveData( const char *level );
  141. void ParseSaveTables( CSaveRestoreData *pSaveData, SAVE_HEADER *pHeader, int updateGlobals );
  142. int FileSize( FileHandle_t pFile );
  143. CSaveRestoreData * SaveGameStateInit( void );
  144. void SaveGameStateGlobals( CSaveRestoreData *pSaveData );
  145. int SaveReadNameAndComment( FileHandle_t f, char *name, char *comment );
  146. void BuildRestoredIndexTranslationTable( char const *mapname, CSaveRestoreData *pSaveData, bool verbose );
  147. char const *GetSaveGameMapName( char const *level );
  148. void SetMostRecentSaveGame( const char *pSaveName );
  149. int GetMostRecentElapsedMinutes( void );
  150. int GetMostRecentElapsedSeconds( void );
  151. int GetMostRecentElapsedTimeSet( void );
  152. void SetMostRecentElapsedMinutes( const int min );
  153. void SetMostRecentElapsedSeconds( const int sec );
  154. struct SaveRestoreTranslate
  155. {
  156. string_t classname;
  157. int savedindex;
  158. int restoredindex;
  159. };
  160. struct RestoreLookupTable
  161. {
  162. RestoreLookupTable() :
  163. m_vecLandMarkOffset( 0, 0, 0 )
  164. {
  165. }
  166. void Clear()
  167. {
  168. lookup.RemoveAll();
  169. m_vecLandMarkOffset.Init();
  170. }
  171. RestoreLookupTable( const RestoreLookupTable& src )
  172. {
  173. int c = src.lookup.Count();
  174. for ( int i = 0 ; i < c; i++ )
  175. {
  176. lookup.AddToTail( src.lookup[ i ] );
  177. }
  178. m_vecLandMarkOffset = src.m_vecLandMarkOffset;
  179. }
  180. RestoreLookupTable& operator=( const RestoreLookupTable& src )
  181. {
  182. if ( this == &src )
  183. return *this;
  184. int c = src.lookup.Count();
  185. for ( int i = 0 ; i < c; i++ )
  186. {
  187. lookup.AddToTail( src.lookup[ i ] );
  188. }
  189. m_vecLandMarkOffset = src.m_vecLandMarkOffset;
  190. return *this;
  191. }
  192. CUtlVector< SaveRestoreTranslate > lookup;
  193. Vector m_vecLandMarkOffset;
  194. };
  195. RestoreLookupTable *FindOrAddRestoreLookupTable( char const *mapname );
  196. int LookupRestoreSpotSaveIndex( RestoreLookupTable *table, int save );
  197. void ReapplyDecal( bool adjacent, RestoreLookupTable *table, decallist_t *entry );
  198. CUtlDict< RestoreLookupTable, int > m_RestoreLookup;
  199. char m_szMostRecentSaveLoadGame[MAX_OSPATH];
  200. char m_szSaveGameName[MAX_OSPATH];
  201. char m_szSaveGameScreenshotFile[MAX_OSPATH];
  202. float m_flClientSaveRestoreTime;
  203. bool m_bClearSaveDir;
  204. int m_MostRecentElapsedMinutes;
  205. int m_MostRecentElapsedSeconds;
  206. int m_MostRecentElapsedTimeSet;
  207. bool m_bWaitingForSafeDangerousSave;
  208. bool m_bIsXSave;
  209. bool m_bOverrideLoadGameEntsOn;
  210. int m_nDeferredCommandFrames;
  211. CUtlVector< CUtlSymbol > m_sDeferredCommands;
  212. protected:
  213. RecentSaveInfo_t m_MostRecentSaveInfo;
  214. #ifdef _PS3
  215. CPS3SaveRestoreAsyncStatus m_PS3AutoSaveAsyncStatus;
  216. #endif
  217. };
  218. #endif