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.

162 lines
4.6 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "savegamedialog.h"
  8. #include "engineinterface.h"
  9. #include "gameui_interface.h"
  10. #include "vgui/ISystem.h"
  11. #include "vgui/ISurface.h"
  12. #include "vgui/IVGui.h"
  13. #include "vgui_controls/Button.h"
  14. #include "vgui_controls/ListPanel.h"
  15. #include "vgui_controls/QueryBox.h"
  16. #include "keyvalues.h"
  17. #include "filesystem.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. using namespace vgui;
  21. // memdbgon must be the last include file in a .cpp file!!!
  22. #include "tier0/memdbgon.h"
  23. #define NEW_SAVE_GAME_TIMESTAMP 0xFFFFFFFF
  24. //-----------------------------------------------------------------------------
  25. // Purpose:Constructor
  26. //-----------------------------------------------------------------------------
  27. CSaveGameDialog::CSaveGameDialog(vgui::Panel *parent) : BaseClass(parent, "SaveGameDialog")
  28. {
  29. SetDeleteSelfOnClose(true);
  30. SetBounds(0, 0, 512, 384);
  31. SetSizeable( true );
  32. SetTitle("#GameUI_SaveGame", true);
  33. vgui::Button *cancel = new vgui::Button( this, "Cancel", "#GameUI_Cancel" );
  34. cancel->SetCommand( "Close" );
  35. LoadControlSettings("Resource\\SaveGameDialog.res");
  36. }
  37. //-----------------------------------------------------------------------------
  38. // Purpose: Destructor
  39. //-----------------------------------------------------------------------------
  40. CSaveGameDialog::~CSaveGameDialog()
  41. {
  42. }
  43. //-----------------------------------------------------------------------------
  44. // Purpose: Saves game
  45. //-----------------------------------------------------------------------------
  46. void CSaveGameDialog::OnCommand( const char *command )
  47. {
  48. if ( !stricmp( command, "loadsave" ) )
  49. {
  50. int saveIndex = GetSelectedItemSaveIndex();
  51. if ( m_SaveGames.IsValidIndex(saveIndex) )
  52. {
  53. // see if we're overwriting
  54. if ( m_SaveGames[saveIndex].iTimestamp == NEW_SAVE_GAME_TIMESTAMP )
  55. {
  56. // new save game, proceed
  57. OnCommand( "SaveOverwriteConfirmed" );
  58. }
  59. else
  60. {
  61. // open confirmation dialog
  62. QueryBox *box = new QueryBox( "#GameUI_ConfirmOverwriteSaveGame_Title", "#GameUI_ConfirmOverwriteSaveGame_Info" );
  63. box->AddActionSignalTarget(this);
  64. box->SetOKButtonText("#GameUI_ConfirmOverwriteSaveGame_OK");
  65. box->SetOKCommand(new KeyValues("Command", "command", "SaveOverwriteConfirmed"));
  66. box->DoModal();
  67. }
  68. }
  69. }
  70. else if ( !stricmp( command, "SaveOverwriteConfirmed" ) )
  71. {
  72. int saveIndex = GetSelectedItemSaveIndex();
  73. if ( m_SaveGames.IsValidIndex(saveIndex) )
  74. {
  75. // delete any existing save
  76. DeleteSaveGame( m_SaveGames[saveIndex].szFileName );
  77. // save to a new name
  78. char saveName[128];
  79. FindSaveSlot( saveName, sizeof(saveName) );
  80. if ( saveName && saveName[ 0 ] )
  81. {
  82. // Load the game, return to top and switch to engine
  83. char sz[ 256 ];
  84. Q_snprintf(sz, sizeof( sz ), "save %s\n", saveName );
  85. engine->ClientCmd_Unrestricted( sz );
  86. // Close this dialog
  87. Close();
  88. // hide the UI
  89. GameUI().HideGameUI();
  90. }
  91. }
  92. }
  93. else
  94. {
  95. BaseClass::OnCommand( command );
  96. }
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Purpose: Opens the dialog and rebuilds the save list
  100. //-----------------------------------------------------------------------------
  101. void CSaveGameDialog::Activate()
  102. {
  103. BaseClass::Activate();
  104. ScanSavedGames();
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Purpose: scans for save games
  108. //-----------------------------------------------------------------------------
  109. void CSaveGameDialog::OnScanningSaveGames()
  110. {
  111. // create dummy item for current saved game
  112. SaveGameDescription_t save = { "NewSavedGame", "", "", "#GameUI_NewSaveGame", "", "", "Current", NEW_SAVE_GAME_TIMESTAMP };
  113. m_SaveGames.AddToTail(save);
  114. }
  115. //-----------------------------------------------------------------------------
  116. // Purpose: generates a new save game name
  117. //-----------------------------------------------------------------------------
  118. void CSaveGameDialog::FindSaveSlot( char *buffer, int bufsize )
  119. {
  120. buffer[0] = 0;
  121. char szFileName[512];
  122. for (int i = 0; i < 1000; i++)
  123. {
  124. Q_snprintf(szFileName, sizeof( szFileName ), "save/Half-Life-%03i.sav", i );
  125. FileHandle_t fp = g_pFullFileSystem->Open( szFileName, "rb" );
  126. if (!fp)
  127. {
  128. // clean up name
  129. Q_strncpy( buffer, szFileName + 5, bufsize );
  130. char *ext = strstr( buffer, ".sav" );
  131. if ( ext )
  132. {
  133. *ext = 0;
  134. }
  135. return;
  136. }
  137. g_pFullFileSystem->Close(fp);
  138. }
  139. Assert(!("Could not generate new save game file"));
  140. }