1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "BasePanel.h"
#include "SaveGameDialog.h"
#include "winlite.h" // FILETIME
#include "vgui/ILocalize.h"
#include "vgui/ISurface.h"
#include "vgui/ISystem.h"
#include "vgui/IVGui.h"
#include "vgui_controls/AnimationController.h"
#include "vgui_controls/ImagePanel.h"
#include "filesystem.h"
#include "KeyValues.h"
#include "ModInfo.h"
#include "EngineInterface.h"
#include "GameUI_Interface.h"
#include "vstdlib/random.h"
#include "BaseSaveGameDialog.h"
#include "SaveGameBrowserDialog.h"
using namespace vgui;
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#include "vgui_controls/Frame.h"
#include "utlvector.h"
extern const char *COM_GetModDirectory();
using namespace vgui;
CSaveGameDialogXbox::CSaveGameDialogXbox( vgui::Panel *parent )
: BaseClass( parent ),
m_bGameSaving ( false ),
m_bNewSaveAvailable( false )
{
m_bFilterAutosaves = true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CSaveGameDialogXbox::PerformSelectedAction( void )
{
BaseClass::PerformSelectedAction();
// If there are no panels, don't allow this
if ( GetNumPanels() == 0 )
return;
SetControlDisabled( true );
// Decide if this is an overwrite or a new save game
bool bNewSave = ( GetActivePanelIndex() == 0 ) && m_bNewSaveAvailable;
if ( bNewSave )
{
OnCommand( "SaveGame" );
}
else
{
BasePanel()->ShowMessageDialog( MD_SAVE_OVERWRITE, this );
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : bNewSaveSelected -
//-----------------------------------------------------------------------------
void CSaveGameDialogXbox::UpdateFooterOptions( void )
{
CFooterPanel *pFooter = GetFooterPanel();
// Show available buttons
pFooter->ClearButtons();
bool bSavePanelsActive = ( GetNumPanels() != 0 );
if ( bSavePanelsActive )
{
bool bNewSaveSelected = ( GetActivePanelIndex() == 0 ) && m_bNewSaveAvailable;
if ( bNewSaveSelected )
{
pFooter->AddNewButtonLabel( "#GameUI_SaveGame_NewSave", "#GameUI_Icons_A_BUTTON" );
}
else
{
pFooter->AddNewButtonLabel( "#GameUI_SaveGame_Overwrite", "#GameUI_Icons_A_BUTTON" );
}
}
// Always available
pFooter->AddNewButtonLabel( "#GameUI_Close", "#GameUI_Icons_B_BUTTON" );
pFooter->AddNewButtonLabel( "#GameUI_Console_StorageChange", "#GameUI_Icons_Y_BUTTON" );
}
//-----------------------------------------------------------------------------
// Purpose: perfrom the save on a separate thread
//-----------------------------------------------------------------------------
class CAsyncCtxSaveGame : public CBasePanel::CAsyncJobContext
{
public:
explicit CAsyncCtxSaveGame( CSaveGameDialogXbox *pDlg );
~CAsyncCtxSaveGame() {}
public:
virtual void ExecuteAsync();
virtual void Completed();
public:
char m_szFilename[MAX_PATH];
CSaveGameDialogXbox *m_pSaveGameDlg;
};
CAsyncCtxSaveGame::CAsyncCtxSaveGame( CSaveGameDialogXbox *pDlg ) :
CBasePanel::CAsyncJobContext( 3.0f ), // Storage device info for at least 3 seconds
m_pSaveGameDlg( pDlg )
{
NULL;
}
void CAsyncCtxSaveGame::ExecuteAsync()
{
// Sit and wait for the async save to finish
for ( ; ; )
{
if ( !engine->IsSaveInProgress() )
// Save operation is no longer in progress
break;
else
ThreadSleep( 50 );
}
}
void CAsyncCtxSaveGame::Completed()
{
m_pSaveGameDlg->SaveCompleted( this );
}
//-----------------------------------------------------------------------------
// Purpose: kicks off the async save (called on the main thread)
//-----------------------------------------------------------------------------
void CSaveGameDialogXbox::InitiateSaving()
{
// Determine whether this is a new save or overwrite
bool bNewSave = ( GetActivePanelIndex() == 0 ) && m_bNewSaveAvailable;
// Allocate the async context for saving
CAsyncCtxSaveGame *pAsyncCtx = new CAsyncCtxSaveGame( this );
// If this is an overwrite then there was an overwrite warning displayed
if ( !bNewSave )
BasePanel()->CloseMessageDialog( DIALOG_STACK_IDX_WARNING );
// Now display the saving warning
BasePanel()->ShowMessageDialog( MD_SAVING_WARNING, this );
// Kick off saving
char *szFilename = pAsyncCtx->m_szFilename;
const int maxFilenameLen = sizeof( pAsyncCtx->m_szFilename );
char szCmd[MAX_PATH];
// See if this is the "new save game" slot
if ( bNewSave )
{
// Create a new save game (name is created from the current time, which should be pretty unique)
#ifdef WIN32
FILETIME currentTime;
GetSystemTimeAsFileTime( ¤tTime );
Q_snprintf( szFilename, maxFilenameLen, "%s_%u", COM_GetModDirectory(), currentTime.dwLowDateTime );
#else
time_t currentTime = time( NULL );
Q_snprintf( szFilename, maxFilenameLen, "%s_%u", COM_GetModDirectory(), (unsigned)currentTime );
#endif
Q_snprintf( szCmd, sizeof( szCmd ), "xsave %s", szFilename );
engine->ExecuteClientCmd( szCmd );
Q_strncat( szFilename, ".360.sav", maxFilenameLen );
}
else
{
const SaveGameDescription_t *pDesc = GetActivePanelSaveDescription();
Q_strncpy( szFilename, pDesc->szShortName, maxFilenameLen );
Q_snprintf( szCmd, sizeof( szCmd ), "xsave %s", szFilename );
engine->ExecuteClientCmd( szCmd );
}
// Enqueue waiting
BasePanel()->ExecuteAsync( pAsyncCtx );
}
//-----------------------------------------------------------------------------
// Purpose: handles the end of async save (called on the main thread)
//-----------------------------------------------------------------------------
void CSaveGameDialogXbox::SaveCompleted( CAsyncCtxSaveGame *pCtx )
{
char const *szFilename = pCtx->m_szFilename;
// We should now be saved so get the new desciption back from the file
char szDirectory[MAX_PATH];
Q_snprintf( szDirectory, sizeof( szDirectory ), "%s:/%s", COM_GetModDirectory(), szFilename );
ParseSaveData( szDirectory, szFilename, &m_NewSaveDesc );
// Close the progress dialog
BasePanel()->CloseMessageDialog( DIALOG_STACK_IDX_WARNING );
bool bNewSave = ( GetActivePanelIndex() == 0 ) && m_bNewSaveAvailable;
if ( bNewSave )
{
AnimateInsertNewPanel( &m_NewSaveDesc );
}
else
{
AnimateOverwriteActivePanel( &m_NewSaveDesc );
}
m_bGameSaving = false;
}
//-----------------------------------------------------------------------------
// Purpose: handles button commands
//-----------------------------------------------------------------------------
void CSaveGameDialogXbox::OnCommand( const char *command )
{
m_KeyRepeat.Reset();
if ( !Q_stricmp( command, "SaveGame" ) )
{
if ( m_bGameSaving )
return;
m_bGameSaving = true;
SetControlDisabled( true );
// Initiate the saving operation
InitiateSaving();
}
else if ( !Q_stricmp( command, "SaveSuccess" ) )
{
vgui::surface()->PlaySound( "UI/buttonclick.wav" );
GameUI().SetSavedThisMenuSession( true );
}
else if ( !Q_stricmp( command, "CloseAndSelectResume" ) )
{
BasePanel()->ArmFirstMenuItem();
OnCommand( "Close" );
}
else if ( !Q_stricmp( command, "OverwriteGameCancelled" ) )
{
SetControlDisabled( false );
}
else if ( !Q_stricmp( command, "RefreshSaveGames" ) )
{
RefreshSaveGames();
}
else if ( !Q_stricmp( command, "ReleaseModalWindow" ) )
{
vgui::surface()->RestrictPaintToSinglePanel( NULL );
}
else if ( !m_bGameSaving )
{
BaseClass::OnCommand(command);
}
}
//-----------------------------------------------------------------------------
// Purpose: On completion of scanning, prepend a utility slot on the stack
//-----------------------------------------------------------------------------
void CSaveGameDialogXbox::OnDoneScanningSaveGames( void )
{
ConVarRef save_history_count("save_history_count" );
m_bNewSaveAvailable = false;
#ifdef _X360
if ( XBX_GetStorageDeviceId() == XBX_INVALID_STORAGE_ID || XBX_GetStorageDeviceId() == XBX_STORAGE_DECLINED )
return;
// We only allow 10 save games minus the number of autosaves, autosavedangerous, and autosave0?'s at once
if ( GetNumPanels() >= 10 - ( 2 + (unsigned)save_history_count.GetInt() ) )
return;
if ( GetStorageSpaceUsed() + XBX_SAVEGAME_BYTES > XBX_PERSISTENT_BYTES_NEEDED )
return;
m_bNewSaveAvailable = true;
SaveGameDescription_t bogusDesc = { "#GameUI_SaveGame_NewSavedGame", "#GameUI_SaveGame_NewSave", "#GameUI_SaveGame_NewSave", "#GameUI_SaveGame_NewSave", "#GameUI_SaveGame_NewSave", "#GameUI_SaveGame_NewSave", "#GameUI_SaveGame_NewSave", 0, 0 };
CGameSavePanel *newSavePanel = SETUP_PANEL( new CGameSavePanel( this, &bogusDesc, true ) );
AddPanel( newSavePanel );
#endif // _X360
}
|