GameState.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: GameState.h //////////////////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, September 2002
  25. // Desc: Game state singleton from which to load and save the game state
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #pragma once
  28. #ifndef __GAME_STATE_H_
  29. #define __GAME_STATE_H_
  30. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  31. #include "Common/STLTypedefs.h"
  32. #include "Common/Snapshot.h"
  33. #include "Common/SubsystemInterface.h"
  34. #include "Common/UnicodeString.h"
  35. #include "GameNetwork/NetworkDefs.h"
  36. // FORWARD REFERENCES /////////////////////////////////////////////////////////////////////////////
  37. class GameWindow;
  38. class WindowLayout;
  39. ///////////////////////////////////////////////////////////////////////////////////////////////////
  40. typedef void (*IterateSaveFileCallback)( AsciiString filename, void *userData );
  41. // ------------------------------------------------------------------------------------------------
  42. /** The save/load window is used for a variety of formats, using this type during the
  43. * save/load menu initialization you can make that menu allow loading only, or allow
  44. * both saving and loading from the same menu */
  45. // ------------------------------------------------------------------------------------------------
  46. enum SaveLoadLayoutType
  47. {
  48. SLLT_INVALID = 0,
  49. SLLT_SAVE_AND_LOAD,
  50. SLLT_LOAD_ONLY,
  51. SLLT_SAVE_ONLY,
  52. SLLT_NUM_TYPES // keep this last, why? don't know, it's not really used, but we like it this way
  53. };
  54. // ------------------------------------------------------------------------------------------------
  55. // ------------------------------------------------------------------------------------------------
  56. struct SaveDate
  57. {
  58. SaveDate() { year = month = day = dayOfWeek = hour = minute = second = milliseconds = 0; }
  59. Bool isNewerThan( SaveDate *other );
  60. UnsignedShort year;
  61. UnsignedShort month;
  62. UnsignedShort day;
  63. UnsignedShort dayOfWeek;
  64. UnsignedShort hour;
  65. UnsignedShort minute;
  66. UnsignedShort second;
  67. UnsignedShort milliseconds;
  68. };
  69. // ------------------------------------------------------------------------------------------------
  70. // ------------------------------------------------------------------------------------------------
  71. enum SaveFileType
  72. {
  73. SAVE_FILE_TYPE_NORMAL, ///< a regular save game at any arbitrary point in the game
  74. SAVE_FILE_TYPE_MISSION, ///< a save game in between missions (a mission save)
  75. SAVE_FILE_TYPE_NUM_TYPES
  76. };
  77. // ------------------------------------------------------------------------------------------------
  78. // ------------------------------------------------------------------------------------------------
  79. class SaveGameInfo
  80. {
  81. public:
  82. SaveGameInfo( void );
  83. ~SaveGameInfo( void );
  84. AsciiString saveGameMapName; // map name of the "scratch pad" map extracted from save file
  85. AsciiString pristineMapName; // pristine map in the map or user maps directory
  86. AsciiString mapLabel; // pretty name of this level set in the editor
  87. SaveDate date; // date of file save
  88. AsciiString campaignSide; // which campaign side we're playing
  89. Int missionNumber; // mission number in campaign
  90. UnicodeString description; // user description for save game file
  91. SaveFileType saveFileType; // type of save file we're dealing with
  92. AsciiString missionMapName; // used for mission saves
  93. };
  94. // ------------------------------------------------------------------------------------------------
  95. // ------------------------------------------------------------------------------------------------
  96. struct AvailableGameInfo
  97. {
  98. AsciiString filename;
  99. SaveGameInfo saveGameInfo;
  100. AvailableGameInfo *next;
  101. AvailableGameInfo *prev;
  102. };
  103. // ------------------------------------------------------------------------------------------------
  104. // ------------------------------------------------------------------------------------------------
  105. enum SaveCode
  106. {
  107. SC_INVALID = -1,
  108. SC_OK,
  109. SC_NO_FILE_AVAILABLE,
  110. SC_FILE_NOT_FOUND,
  111. SC_UNABLE_TO_OPEN_FILE,
  112. SC_INVALID_XFER,
  113. SC_UNKNOWN_BLOCK,
  114. SC_INVALID_DATA,
  115. SC_ERROR,
  116. };
  117. enum SnapshotType {
  118. SNAPSHOT_SAVELOAD,
  119. SNAPSHOT_DEEPCRC_LOGICONLY,
  120. SNAPSHOT_DEEPCRC,
  121. SNAPSHOT_MAX
  122. };
  123. // ------------------------------------------------------------------------------------------------
  124. // ------------------------------------------------------------------------------------------------
  125. class GameState : public SubsystemInterface,
  126. public Snapshot
  127. {
  128. public:
  129. GameState( void );
  130. virtual ~GameState( void );
  131. // subsystem interface
  132. virtual void init( void );
  133. virtual void reset( void );
  134. virtual void update( void ) { }
  135. // save game methods
  136. SaveCode saveGame( AsciiString filename,
  137. UnicodeString desc,
  138. SaveFileType saveType,
  139. SnapshotType which = SNAPSHOT_SAVELOAD ); ///< save a game
  140. SaveCode missionSave( void ); ///< do a in between mission save
  141. SaveCode loadGame( AvailableGameInfo gameInfo ); ///< load a save file
  142. SaveGameInfo *getSaveGameInfo( void ) { return &m_gameInfo; }
  143. // snapshot interaction
  144. void addPostProcessSnapshot( Snapshot *snapshot ); ///< add snapshot to post process laod
  145. // manipulating files
  146. Bool doesSaveGameExist( AsciiString filename ); ///< does the save file exist
  147. void populateSaveGameListbox( GameWindow *listbox, SaveLoadLayoutType layoutType ); ///< populate listbox with available save games
  148. void getSaveGameInfoFromFile( AsciiString filename, SaveGameInfo *saveGameInfo ); ///< get save game info from file
  149. void friend_xferSaveDataForCRC( Xfer *xfer, SnapshotType which ); ///< This should only be called to DeepCRC sanity checking
  150. Bool isInLoadGame(void) { return m_isInLoadGame; } // Brutal hack to allow bone pos validation while loading games
  151. void setPristineMapName( AsciiString name ) { m_gameInfo.pristineMapName = name; }
  152. AsciiString getPristineMapName( void ) { return m_gameInfo.pristineMapName; }
  153. AsciiString getSaveDirectory() const;
  154. AsciiString getFilePathInSaveDirectory(const AsciiString& leaf) const;
  155. Bool isInSaveDirectory(const AsciiString& path) const;
  156. AsciiString realMapPathToPortableMapPath(const AsciiString& in) const;
  157. AsciiString portableMapPathToRealMapPath(const AsciiString& in) const;
  158. AsciiString getMapLeafName(const AsciiString& in) const;
  159. protected:
  160. // snapshot methods
  161. virtual void crc( Xfer *xfer ) { }
  162. virtual void xfer( Xfer *xfer );
  163. virtual void loadPostProcess( void ) { }
  164. private:
  165. AsciiString findNextSaveFilename( UnicodeString desc ); ///< find next acceptable filename for a new save game
  166. void iterateSaveFiles( IterateSaveFileCallback callback, void *userData ); ///< iterate save files on disk
  167. void xferSaveData( Xfer *xfer, SnapshotType which ); ///< save/load the file data
  168. void gameStatePostProcessLoad( void ); ///< post process entry point after a game load
  169. void clearAvailableGames( void ); ///< clear any available games resources we got in our list
  170. struct SnapshotBlock
  171. {
  172. Snapshot *snapshot; ///< the snapshot object that handles this block
  173. AsciiString blockName; ///< the block name
  174. };
  175. typedef std::list< SnapshotBlock > SnapshotBlockList;
  176. typedef SnapshotBlockList::iterator SnapshotBlockListIterator;
  177. void addSnapshotBlock( AsciiString blockName, Snapshot *snapshot, SnapshotType which );
  178. SnapshotBlock *findBlockInfoByToken( AsciiString token, SnapshotType which );
  179. SnapshotBlockList m_snapshotBlockList[SNAPSHOT_MAX]; ///< list of snapshot blocks of save file data
  180. SaveGameInfo m_gameInfo; ///< save game info struct
  181. typedef std::list< Snapshot * > SnapshotList;
  182. typedef SnapshotList::iterator SnapshotListIterator;
  183. typedef SnapshotList::reverse_iterator SnapshotListReverseIterator;
  184. SnapshotList m_snapshotPostProcessList;
  185. AvailableGameInfo *m_availableGames; ///< list of available games we can save over or load from
  186. Bool m_isInLoadGame; // Brutal hack to allow bone pos validation while loading games
  187. };
  188. // EXTERNALS //////////////////////////////////////////////////////////////////////////////////////
  189. extern GameState *TheGameState;
  190. UnicodeString getUnicodeTimeBuffer(SYSTEMTIME timeVal);
  191. UnicodeString getUnicodeDateBuffer(SYSTEMTIME timeVal);
  192. #endif // end __GAME_STATE_H_