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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "SaveGameDialog.h"
#include "EngineInterface.h"
#include "GameUI_Interface.h"
#include "vgui/ISystem.h"
#include "vgui/ISurface.h"
#include "vgui/IVGui.h"
#include "vgui_controls/Button.h"
#include "vgui_controls/ListPanel.h"
#include "vgui_controls/QueryBox.h"
#include "KeyValues.h"
#include "filesystem.h"
#include <stdio.h>
#include <stdlib.h>
using namespace vgui;
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define NEW_SAVE_GAME_TIMESTAMP 0xFFFFFFFF
//-----------------------------------------------------------------------------
// Purpose:Constructor
//-----------------------------------------------------------------------------
CSaveGameDialog::CSaveGameDialog(vgui::Panel *parent) : BaseClass(parent, "SaveGameDialog")
{
SetDeleteSelfOnClose(true);
SetBounds(0, 0, 512, 384);
SetSizeable( true );
SetTitle("#GameUI_SaveGame", true);
vgui::Button *cancel = new vgui::Button( this, "Cancel", "#GameUI_Cancel" );
cancel->SetCommand( "Close" );
LoadControlSettings("Resource\\SaveGameDialog.res");
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CSaveGameDialog::~CSaveGameDialog()
{
}
//-----------------------------------------------------------------------------
// Purpose: Saves game
//-----------------------------------------------------------------------------
void CSaveGameDialog::OnCommand( const char *command )
{
if ( !stricmp( command, "loadsave" ) )
{
int saveIndex = GetSelectedItemSaveIndex();
if ( m_SaveGames.IsValidIndex(saveIndex) )
{
// see if we're overwriting
if ( m_SaveGames[saveIndex].iTimestamp == NEW_SAVE_GAME_TIMESTAMP )
{
// new save game, proceed
OnCommand( "SaveOverwriteConfirmed" );
}
else
{
// open confirmation dialog
QueryBox *box = new QueryBox( "#GameUI_ConfirmOverwriteSaveGame_Title", "#GameUI_ConfirmOverwriteSaveGame_Info" );
box->AddActionSignalTarget(this);
box->SetOKButtonText("#GameUI_ConfirmOverwriteSaveGame_OK");
box->SetOKCommand(new KeyValues("Command", "command", "SaveOverwriteConfirmed"));
box->DoModal();
}
}
}
else if ( !stricmp( command, "SaveOverwriteConfirmed" ) )
{
int saveIndex = GetSelectedItemSaveIndex();
if ( m_SaveGames.IsValidIndex(saveIndex) )
{
// delete any existing save
DeleteSaveGame( m_SaveGames[saveIndex].szFileName );
// save to a new name
char saveName[128];
FindSaveSlot( saveName, sizeof(saveName) );
if ( saveName[ 0 ] )
{
// Load the game, return to top and switch to engine
char sz[ 256 ];
Q_snprintf(sz, sizeof( sz ), "save %s\n", saveName );
engine->ClientCmd_Unrestricted( sz );
// Close this dialog
Close();
// hide the UI
GameUI().HideGameUI();
}
}
}
else
{
BaseClass::OnCommand( command );
}
}
//-----------------------------------------------------------------------------
// Purpose: Opens the dialog and rebuilds the save list
//-----------------------------------------------------------------------------
void CSaveGameDialog::Activate()
{
BaseClass::Activate();
ScanSavedGames();
}
//-----------------------------------------------------------------------------
// Purpose: scans for save games
//-----------------------------------------------------------------------------
void CSaveGameDialog::OnScanningSaveGames()
{
// create dummy item for current saved game
SaveGameDescription_t save = { "NewSavedGame", "", "", "#GameUI_NewSaveGame", "", "", "Current", NEW_SAVE_GAME_TIMESTAMP };
m_SaveGames.AddToTail(save);
}
//-----------------------------------------------------------------------------
// Purpose: generates a new save game name
//-----------------------------------------------------------------------------
void CSaveGameDialog::FindSaveSlot( char *buffer, int bufsize )
{
buffer[0] = 0;
char szFileName[512];
for (int i = 0; i < 1000; i++)
{
Q_snprintf(szFileName, sizeof( szFileName ), "save/half-life-%03i.sav", i );
FileHandle_t fp = g_pFullFileSystem->Open( szFileName, "rb" );
if (!fp)
{
// clean up name
Q_strncpy( buffer, szFileName + 5, bufsize );
char *ext = strstr( buffer, ".sav" );
if ( ext )
{
*ext = 0;
}
return;
}
g_pFullFileSystem->Close(fp);
}
Assert(!("Could not generate new save game file"));
}
|