-
Notifications
You must be signed in to change notification settings - Fork 2k
/
vguitextwindow.cpp
436 lines (353 loc) · 11.3 KB
/
vguitextwindow.cpp
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "vguitextwindow.h"
#include <networkstringtabledefs.h>
#include <cdll_client_int.h>
#include <clientmode_shared.h>
#include <vgui/IScheme.h>
#include <vgui/ILocalize.h>
#include <vgui/ISurface.h>
#include <filesystem.h>
#include <KeyValues.h>
#include <convar.h>
#include <vgui_controls/ImageList.h>
#include <vgui_controls/TextEntry.h>
#include <vgui_controls/Button.h>
#include <game/client/iviewport.h>
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
using namespace vgui;
extern INetworkStringTable *g_pStringTableInfoPanel;
#define TEMP_HTML_FILE "textwindow_temp.html"
ConVar cl_disablehtmlmotd( "cl_disablehtmlmotd", "0", FCVAR_ARCHIVE, "Disable HTML motds." );
//=============================================================================
// HPE_BEGIN:
// [Forrest] Replaced text window command string with TEXTWINDOW_CMD enumeration
// of options. Passing a command string is dangerous and allowed a server network
// message to run arbitrary commands on the client.
//=============================================================================
CON_COMMAND( showinfo, "Shows a info panel: <type> <title> <message> [<command number>]" )
{
if ( !gViewPortInterface )
return;
if ( args.ArgC() < 4 )
return;
IViewPortPanel * panel = gViewPortInterface->FindPanelByName( PANEL_INFO );
if ( panel )
{
KeyValues *kv = new KeyValues("data");
kv->SetInt( "type", Q_atoi(args[ 1 ]) );
kv->SetString( "title", args[ 2 ] );
kv->SetString( "message", args[ 3 ] );
if ( args.ArgC() == 5 )
kv->SetString( "command", args[ 4 ] );
panel->SetData( kv );
gViewPortInterface->ShowPanel( panel, true );
kv->deleteThis();
}
else
{
Msg("Couldn't find info panel.\n" );
}
}
//=============================================================================
// HPE_END
//=============================================================================
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CTextWindow::CTextWindow(IViewPort *pViewPort) : Frame(NULL, PANEL_INFO )
{
// initialize dialog
m_pViewPort = pViewPort;
// SetTitle("", true);
m_szTitle[0] = '\0';
m_szMessage[0] = '\0';
m_szMessageFallback[0] = '\0';
m_nExitCommand = TEXTWINDOW_CMD_NONE;
m_bShownURL = false;
m_bUnloadOnDismissal = false;
// load the new scheme early!!
SetScheme("ClientScheme");
SetMoveable(false);
SetSizeable(false);
SetProportional(true);
// hide the system buttons
SetTitleBarVisible( false );
m_pTextMessage = new TextEntry( this, "TextMessage" );
m_pHTMLMessage = new CMOTDHTML( this,"HTMLMessage" );
m_pTitleLabel = new Label( this, "MessageTitle", "Message Title" );
m_pOK = new Button(this, "ok", "#PropertyDialog_OK");
m_pOK->SetCommand("okay");
m_pTextMessage->SetMultiline( true );
m_nContentType = TYPE_TEXT;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTextWindow::ApplySchemeSettings( IScheme *pScheme )
{
BaseClass::ApplySchemeSettings( pScheme );
LoadControlSettings("Resource/UI/TextWindow.res");
Reset();
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CTextWindow::~CTextWindow()
{
// remove temp file again
g_pFullFileSystem->RemoveFile( TEMP_HTML_FILE, "DEFAULT_WRITE_PATH" );
}
void CTextWindow::Reset( void )
{
//=============================================================================
// HPE_BEGIN:
// [Forrest] Replace strange hard-coded default message with hard-coded error message.
//=============================================================================
V_strcpy_safe( m_szTitle, "Error loading info message." );
V_strcpy_safe( m_szMessage, "" );
V_strcpy_safe( m_szMessageFallback, "" );
//=============================================================================
// HPE_END
//=============================================================================
m_nExitCommand = TEXTWINDOW_CMD_NONE;
m_nContentType = TYPE_TEXT;
m_bShownURL = false;
m_bUnloadOnDismissal = false;
Update();
}
void CTextWindow::ShowText( const char *text )
{
m_pTextMessage->SetVisible( true );
m_pTextMessage->SetText( text );
m_pTextMessage->GotoTextStart();
}
void CTextWindow::ShowURL( const char *URL, bool bAllowUserToDisable )
{
#ifdef _DEBUG
Msg( "CTextWindow::ShowURL( %s )\n", URL );
#endif
ClientModeShared *mode = ( ClientModeShared * )GetClientModeNormal();
if ( ( bAllowUserToDisable && cl_disablehtmlmotd.GetBool() ) || !mode->IsHTMLInfoPanelAllowed() )
{
Warning( "Blocking HTML info panel '%s'; Using plaintext instead.\n", URL );
// User has disabled HTML TextWindows. Show the fallback as text only.
if ( g_pStringTableInfoPanel )
{
int index = g_pStringTableInfoPanel->FindStringIndex( "motd_text" );
if ( index != ::INVALID_STRING_INDEX )
{
int length = 0;
const char *data = (const char *)g_pStringTableInfoPanel->GetStringUserData( index, &length );
if ( data && data[0] )
{
m_pHTMLMessage->SetVisible( false );
ShowText( data );
}
}
}
return;
}
m_pHTMLMessage->SetVisible( true );
m_pHTMLMessage->OpenURL( URL, NULL );
m_bShownURL = true;
}
void CTextWindow::ShowIndex( const char *entry )
{
const char *data = NULL;
int length = 0;
if ( NULL == g_pStringTableInfoPanel )
return;
int index = g_pStringTableInfoPanel->FindStringIndex( m_szMessage );
if ( index != ::INVALID_STRING_INDEX )
data = (const char *)g_pStringTableInfoPanel->GetStringUserData( index, &length );
if ( !data || !data[0] )
return; // nothing to show
// is this a web URL ?
if ( !Q_strncmp( data, "http://", 7 ) || !Q_strncmp( data, "https://", 8 ) )
{
ShowURL( data );
return;
}
// try to figure out if this is HTML or not
if ( data[0] != '<' )
{
ShowText( data );
return;
}
// data is a HTML, we have to write to a file and then load the file
FileHandle_t hFile = g_pFullFileSystem->Open( TEMP_HTML_FILE, "wb", "DEFAULT_WRITE_PATH" );
if ( hFile == FILESYSTEM_INVALID_HANDLE )
return;
g_pFullFileSystem->Write( data, length, hFile );
g_pFullFileSystem->Close( hFile );
if ( g_pFullFileSystem->Size( TEMP_HTML_FILE ) != (unsigned int)length )
return; // something went wrong while writing
ShowFile( TEMP_HTML_FILE );
}
void CTextWindow::ShowFile( const char *filename )
{
if ( Q_stristr( filename, ".htm" ) || Q_stristr( filename, ".html" ) )
{
// it's a local HTML file
char localURL[ _MAX_PATH + 7 ];
Q_strncpy( localURL, "file://", sizeof( localURL ) );
char pPathData[ _MAX_PATH ];
g_pFullFileSystem->GetLocalPath( filename, pPathData, sizeof(pPathData) );
Q_strncat( localURL, pPathData, sizeof( localURL ), COPY_ALL_CHARACTERS );
ShowURL( localURL );
}
else
{
// read from local text from file
FileHandle_t f = g_pFullFileSystem->Open( m_szMessage, "rb", "GAME" );
if ( !f )
return;
char buffer[2048];
int size = MIN( g_pFullFileSystem->Size( f ), sizeof(buffer)-1 ); // just allow 2KB
g_pFullFileSystem->Read( buffer, size, f );
g_pFullFileSystem->Close( f );
buffer[size]=0; //terminate string
ShowText( buffer );
}
}
void CTextWindow::Update( void )
{
SetTitle( m_szTitle, false );
m_pTitleLabel->SetText( m_szTitle );
if ( m_pHTMLMessage )
m_pHTMLMessage->SetVisible( false );
m_pTextMessage->SetVisible( false );
if ( m_nContentType == TYPE_INDEX )
{
ShowIndex( m_szMessage );
}
else if ( m_nContentType == TYPE_URL )
{
if ( !Q_strncmp( m_szMessage, "http://", 7 ) || !Q_strncmp( m_szMessage, "https://", 8 ) || !Q_stricmp( m_szMessage, "about:blank" ) )
{
ShowURL( m_szMessage );
}
else
{
// We should have trapped this at a higher level
Assert( !"URL protocol is missing or blocked" );
}
}
else if ( m_nContentType == TYPE_FILE )
{
ShowFile( m_szMessage );
}
else if ( m_nContentType == TYPE_TEXT )
{
ShowText( m_szMessage );
}
else
{
DevMsg("CTextWindow::Update: unknown content type %i\n", m_nContentType );
}
}
void CTextWindow::OnCommand( const char *command )
{
if (!Q_strcmp(command, "okay"))
{
//=============================================================================
// HPE_BEGIN:
// [Forrest] Replaced text window command string with TEXTWINDOW_CMD enumeration
// of options. Passing a command string is dangerous and allowed a server network
// message to run arbitrary commands on the client.
//=============================================================================
const char *pszCommand = NULL;
switch ( m_nExitCommand )
{
case TEXTWINDOW_CMD_NONE:
break;
case TEXTWINDOW_CMD_JOINGAME:
pszCommand = "joingame";
break;
case TEXTWINDOW_CMD_CHANGETEAM:
pszCommand = "changeteam";
break;
case TEXTWINDOW_CMD_IMPULSE101:
pszCommand = "impulse 101";
break;
case TEXTWINDOW_CMD_MAPINFO:
pszCommand = "mapinfo";
break;
case TEXTWINDOW_CMD_CLOSED_HTMLPAGE:
pszCommand = "closed_htmlpage";
break;
case TEXTWINDOW_CMD_CHOOSETEAM:
pszCommand = "chooseteam";
break;
default:
DevMsg("CTextWindow::OnCommand: unknown exit command value %i\n", m_nExitCommand );
break;
}
if ( pszCommand != NULL )
{
engine->ClientCmd_Unrestricted( pszCommand );
}
//=============================================================================
// HPE_END
//=============================================================================
m_pViewPort->ShowPanel( this, false );
}
BaseClass::OnCommand(command);
}
void CTextWindow::OnKeyCodePressed( vgui::KeyCode code )
{
if ( code == KEY_XBUTTON_A || code == KEY_XBUTTON_B )
{
OnCommand( "okay" );
return;
}
BaseClass::OnKeyCodePressed(code);
}
void CTextWindow::SetData(KeyValues *data)
{
SetData( data->GetInt( "type" ), data->GetString( "title" ), data->GetString( "msg" ), data->GetString( "msg_fallback" ), data->GetInt( "cmd" ), data->GetBool( "unload" ) );
}
void CTextWindow::SetData( int type, const char *title, const char *message, const char *message_fallback, int command, bool bUnload )
{
Q_strncpy( m_szTitle, title, sizeof( m_szTitle ) );
Q_strncpy( m_szMessage, message, sizeof( m_szMessage ) );
Q_strncpy( m_szMessageFallback, message_fallback, sizeof( m_szMessageFallback ) );
m_nExitCommand = command;
m_nContentType = type;
m_bUnloadOnDismissal = bUnload;
Update();
}
void CTextWindow::ShowPanel( bool bShow )
{
if ( BaseClass::IsVisible() == bShow )
return;
m_pViewPort->ShowBackGround( bShow );
if ( bShow )
{
Activate();
SetMouseInputEnabled( true );
}
else
{
SetVisible( false );
SetMouseInputEnabled( false );
if ( m_bUnloadOnDismissal && m_bShownURL && m_pHTMLMessage )
{
m_pHTMLMessage->OpenURL( "about:blank", NULL );
m_bShownURL = false;
}
}
}
bool CTextWindow::CMOTDHTML::OnStartRequest( const char *url, const char *target, const char *pchPostData, bool bIsRedirect )
{
if ( Q_strstr( url, "steam://" ) )
return false;
return BaseClass::OnStartRequest( url, target, pchPostData, bIsRedirect );
}