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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#include <windows.h>
#include <io.h>
#include <sys/stat.h>
#include <vgui/isystem.h>
#include "min_footprint_files.h"
#include "filesystem_tools.h"
#include "KeyValues.h"
#include "sdklauncher_main.h"
#include "ModConfigsHelper.h"
#include "vgui_controls/Frame.h"
#include <vgui_controls/Label.h>
#include <vgui_controls/MessageBox.h>
#include <vgui_controls/ProgressBar.h>
#include <vgui/iinput.h>
#include <vgui/ivgui.h>
#include "SourceAppInfo.h"
extern void OpenLocalizedURL( const char *lpszLocalName );
extern FSReturnCode_t LoadGameInfoFile( const char *pDirectoryName,
KeyValues *&pMainFile,
KeyValues *&pFileSystemInfo,
KeyValues *&pSearchPaths );
using namespace vgui;
#define SHOW_MIGRATION_MARKER "show_migration_marker.txt"
#define SHOW_DEPRECATED_APP_ID_MARKER "show_deprecatedappid_marker.txt"
#define MIN_FOOTPRINT_VERSION_FILENAME "min_footprint_version.txt"
#define MIN_FOOTPRINT_FILES_FILENAME "min_footprint_file_list.txt"
#define WRITABLE_PREFIX "[make_writable]"
#define ALWAYS_COPY_PREFIX "[always_copy]"
class CTempDirName
{
public:
char m_SrcDirName[MAX_PATH];
char m_DestDirName[MAX_PATH];
};
#define MF_MAKE_WRITABLE 0x0001
#define MF_ALWAYS_COPY 0x0002
class CMinFootprintFilename
{
public:
char m_SrcFilename[MAX_PATH];
char m_DestFilename[MAX_PATH];
int m_Flags; // Combination of MF_ flags.
};
void SafeCopy( const char *pSrcFilename, const char *pDestFilename )
{
FileHandle_t fpSrc = g_pFullFileSystem->Open( pSrcFilename, "rb" );
if ( fpSrc )
{
FILE *fpDest = fopen( pDestFilename, "wb" );
if ( fpDest )
{
while ( 1 )
{
char tempData[4096];
int nBytesRead = g_pFullFileSystem->Read( tempData, sizeof( tempData ), fpSrc );
if ( nBytesRead )
fwrite( tempData, 1, nBytesRead, fpDest );
if ( nBytesRead < sizeof( tempData ) )
break;
}
fclose( fpDest );
}
else
{
Warning( "SafeCopy: can't open %s for writing.", pDestFilename );
}
g_pFullFileSystem->Close( fpSrc );
}
else
{
Warning( "SafeCopy: can't open %s for reading.", pSrcFilename );
}
}
class CMinFootprintFilesPanel : public Frame
{
public:
typedef Frame BaseClass;
CMinFootprintFilesPanel( Panel *parent, const char *panelName )
: BaseClass( parent, panelName )
{
m_pProgressBar = new ProgressBar( this, "CopyProgressBar" );
m_pProgressBar->SetProgress( 0 );
LoadControlSettings( "MinFootprintFilesPanel.res");
SetSizeable( false );
SetMenuButtonVisible( false );
SetMenuButtonResponsive( false );
SetCloseButtonVisible( false );
// Take the focus.
m_PrevAppFocusPanel = input()->GetAppModalSurface();
input()->SetAppModalSurface( GetVPanel() );
}
~CMinFootprintFilesPanel()
{
input()->SetAppModalSurface( m_PrevAppFocusPanel );
}
void StartDumpingFiles()
{
m_pProgressBar->SetProgress( 0 );
m_iCurCopyFile = 0;
ivgui()->AddTickSignal( GetVPanel() );
MoveToCenterOfScreen();
Activate();
}
void OnTick()
{
int nCopied = 0;
while ( nCopied < 10 )
{
if ( m_iCurCopyFile >= m_Filenames.Count() )
{
// Done! Write the output version and disappear.
OnFinished();
return;
}
CMinFootprintFilename *pFilename = &m_Filenames[m_iCurCopyFile++];
// If the dest file doesn't exist or it's read-only, copy our file over it.
if ( (pFilename->m_Flags & MF_ALWAYS_COPY) || (_access( pFilename->m_DestFilename, 2 ) != 0) )
{
// Get rid of the old version of the file.
_chmod( pFilename->m_DestFilename, _S_IWRITE | _S_IREAD ); // Read-only.
_unlink( pFilename->m_DestFilename );
// Copy the new version in.
SafeCopy( pFilename->m_SrcFilename, pFilename->m_DestFilename );
// Set its access permissions.
if ( pFilename->m_Flags & MF_MAKE_WRITABLE )
_chmod( pFilename->m_DestFilename, _S_IREAD | _S_IWRITE );
else
_chmod( pFilename->m_DestFilename, _S_IREAD ); // Read-only.
}
++nCopied;
}
m_pProgressBar->SetProgress( (float)m_iCurCopyFile / m_Filenames.Count() );
}
void OnFinished()
{
m_pProgressBar->SetProgress( 1 );
//
// Write out that we are at the latest version.
//
KeyValues *pVersionKV = new KeyValues( MIN_FOOTPRINT_VERSION_FILENAME );
pVersionKV->SetInt( "Version", m_iOutputVersionNumber );
pVersionKV->SaveToFile( g_pFullFileSystem, MIN_FOOTPRINT_VERSION_FILENAME );
pVersionKV->deleteThis();
// Don't tick any more.
ivgui()->RemoveTickSignal( GetVPanel() );
// Remove our panel.
PostMessage(this, new KeyValues("Close"));
ShowDeprecatedAppIDNotice();
ShowContentMigrationNotice();
}
void ShowDeprecatedAppIDNotice()
{
// Try to open the file that indicates that we've already been through this check
FileHandle_t fp = g_pFullFileSystem->Open( SHOW_DEPRECATED_APP_ID_MARKER, "rb" );
// If the file exists then we've run the check already, don't do anything.
if ( fp )
{
g_pFullFileSystem->Close( fp );
}
// We have not checked for outdated mods yet...
else
{
// Write file that ensures we never perform this check again
fp = g_pFullFileSystem->Open( SHOW_DEPRECATED_APP_ID_MARKER, "wb" );
g_pFullFileSystem->Close( fp );
//
// Look for all of the mods under 'SourceMods', check if the SteamAppId is out of date, and warn the user if
// this is the case
//
ModConfigsHelper mch;
const CUtlVector<char *> &modDirs = mch.getModDirsVector();
vgui::MessageBox *alert = NULL;
char szProblemMods[5*MAX_PATH];
bool bProblemModExists = false;
// Set up the error string
Q_strncpy(szProblemMods, "The SteamAppId values for the following mods should be changed from 220 to 215:\r\n\r\n", 5*MAX_PATH);
// Iterate through the mods and check which ones have outdated SteamAppIds
for ( int i = 0; i < modDirs.Count(); i++ )
{
// Construct full path to mod directory
char szDirPath[MAX_PATH];
char szGameConfigPath[MAX_PATH];
Q_strncpy( szDirPath, mch.getSourceModBaseDir(), MAX_PATH );
Q_strncat( szDirPath, "\\", MAX_PATH , COPY_ALL_CHARACTERS );
Q_strncat( szDirPath, modDirs[i], MAX_PATH , COPY_ALL_CHARACTERS );
// Check for the existence of gameinfo.txt
Q_strncpy( szGameConfigPath, szDirPath, MAX_PATH );
Q_strncat( szGameConfigPath, "\\gameinfo.txt", MAX_PATH , COPY_ALL_CHARACTERS );
FileHandle_t fpGameInfo = g_pFullFileSystem->Open( szGameConfigPath, "rb" );
// GameInfo.txt exists so let's inspect it...
if ( fpGameInfo )
{
// Close the file handle
g_pFullFileSystem->Close( fpGameInfo );
// Load up the "gameinfo.txt"
KeyValues *pMainFile, *pFileSystemInfo, *pSearchPaths;
LoadGameInfoFile( szDirPath, pMainFile, pFileSystemInfo, pSearchPaths );
//!! bug? check return code of loadgameinfofile??
if (NULL != pFileSystemInfo)
{
const int iAppId = pFileSystemInfo->GetInt( "SteamAppId", -1 );
// This is the one that needs replacing add this mod to the list of suspect mods
if ( GetAppSteamAppId( k_App_HL2 ) == iAppId)
{
bProblemModExists = true;
Q_strncat( szProblemMods, modDirs[i], MAX_PATH , COPY_ALL_CHARACTERS );
Q_strncat( szProblemMods, "\r\n", MAX_PATH , COPY_ALL_CHARACTERS );
}
}
}
}
// If necessary, pop up a message box informing the user that
if (bProblemModExists)
{
// Amend the warning message text
Q_strncat( szProblemMods, "\r\nIf you did not author any of the above mods you can ignore this message.\r\nIf you did author any of the above mods you should change the SteamAppId\nvalue in the gameinfo.txt file for each mod listed above.\r\n", 5*MAX_PATH , COPY_ALL_CHARACTERS );
// Pop up a message box
alert = new vgui::MessageBox( "Warning", szProblemMods );
alert->SetOKButtonText("Close");
alert->DoModal();
}
}
}
void ShowContentMigrationNotice()
{
// If we've shown it already, don't do anything.
FileHandle_t fp = g_pFullFileSystem->Open( SHOW_MIGRATION_MARKER, "rb" );
if ( fp )
{
g_pFullFileSystem->Close( fp );
return;
}
// Remember that we showed it.
fp = g_pFullFileSystem->Open( SHOW_MIGRATION_MARKER, "wb" );
g_pFullFileSystem->Close( fp );
OpenLocalizedURL( "URL_Content_Migration_Notice" );
}
public:
CUtlVector<CMinFootprintFilename> m_Filenames;
int m_iOutputVersionNumber;
private:
vgui::VPANEL m_PrevAppFocusPanel;
ProgressBar *m_pProgressBar;
int m_iCurCopyFile;
};
void AddMinFootprintFile( CUtlVector<CMinFootprintFilename> &filenames, const char *pSrcFilename, const char *pDestFilename, int flags )
{
// Just copy this one file.
CMinFootprintFilename *pOutputFilename = &filenames[filenames.AddToTail()];
Q_strncpy( pOutputFilename->m_SrcFilename, pSrcFilename, sizeof( pOutputFilename->m_SrcFilename ) );
Q_strncpy( pOutputFilename->m_DestFilename, pDestFilename, sizeof( pOutputFilename->m_DestFilename ) );
pOutputFilename->m_Flags = flags;
}
void GetMinFootprintFiles_R( CUtlVector<CMinFootprintFilename> &filenames, const char *pSrcDirName, const char *pDestDirName )
{
// pDirName\*.*
char wildcard[MAX_PATH];
Q_strncpy( wildcard, pSrcDirName, sizeof( wildcard ) );
Q_AppendSlash( wildcard, sizeof( wildcard ) );
Q_strncat( wildcard, "*.*", sizeof( wildcard ), COPY_ALL_CHARACTERS );
CUtlVector<CTempDirName> subDirs;
// Make sure the dest directory exists for when we're copying files into it.
CreateDirectory( pDestDirName, NULL );
// Look at all the files.
FileFindHandle_t findHandle;
const char *pFilename = g_pFullFileSystem->FindFirstEx( wildcard, SDKLAUNCHER_MAIN_PATH_ID, &findHandle );
while ( pFilename )
{
if ( Q_stricmp( pFilename, "." ) != 0 && Q_stricmp( pFilename, ".." ) != 0 )
{
char fullSrcFilename[MAX_PATH], fullDestFilename[MAX_PATH];
Q_snprintf( fullSrcFilename, sizeof( fullSrcFilename ), "%s%c%s", pSrcDirName, CORRECT_PATH_SEPARATOR, pFilename );
Q_snprintf( fullDestFilename, sizeof( fullDestFilename ), "%s%c%s", pDestDirName, CORRECT_PATH_SEPARATOR, pFilename );
if ( g_pFullFileSystem->FindIsDirectory( findHandle ) )
{
CTempDirName *pOut = &subDirs[subDirs.AddToTail()];
Q_strncpy( pOut->m_SrcDirName, fullSrcFilename, sizeof( pOut->m_SrcDirName ) );
Q_strncpy( pOut->m_DestDirName, fullDestFilename, sizeof( pOut->m_DestDirName ) );
}
else
{
AddMinFootprintFile( filenames, fullSrcFilename, fullDestFilename, 0 );
}
}
pFilename = g_pFullFileSystem->FindNext( findHandle );
}
g_pFullFileSystem->FindClose( findHandle );
// Recurse.
for ( int i=0; i < subDirs.Count(); i++ )
{
GetMinFootprintFiles_R( filenames, subDirs[i].m_SrcDirName, subDirs[i].m_DestDirName );
}
}
void DumpMinFootprintFiles( bool bForceRefresh )
{
if ( !g_pFullFileSystem->IsSteam() )
return;
// What version are we at now?
int curVersion = 0;
if ( !bForceRefresh )
{
KeyValues *kv = new KeyValues( "" );
if ( kv->LoadFromFile( g_pFullFileSystem, MIN_FOOTPRINT_VERSION_FILENAME ) )
{
curVersion = kv->GetInt( "Version", 0 );
}
kv->deleteThis();
}
// What is the current version?
int latestVersion = 0;
KeyValues *kv = new KeyValues( "" );
bool bValidFile = true;
if ( !kv->LoadFromFile( g_pFullFileSystem, MIN_FOOTPRINT_FILES_FILENAME ) )
bValidFile = false;
KeyValues *pFileList = NULL;
if ( bValidFile && (pFileList = kv->FindKey( "files" )) == NULL )
bValidFile = false;
if ( !bValidFile )
{
VGUIMessageBox( (vgui::Frame *) g_pMainFrame, "#Warning", "#CantLoadMinFootprintFiles" );
kv->deleteThis();
return;
}
latestVersion = kv->GetInt( "Version", 0 );
bool bVersionChanged = ( curVersion != latestVersion );
CMinFootprintFilesPanel *pDisplay = new CMinFootprintFilesPanel( (vgui::Frame *) g_pMainFrame, "MinFootprintFilesPanel" );
pDisplay->m_iOutputVersionNumber = latestVersion;
// Our version is out of date, let's copy out all the min footprint files.
// NOTE: copy files that are set to always_copy whether the version changed or not.
for ( KeyValues *pCur=pFileList->GetFirstSubKey(); pCur; pCur=pCur->GetNextKey() )
{
if ( ( Q_stricmp( pCur->GetName(), "mapping" ) == 0 ) && bVersionChanged )
{
const char *pSrcMapping = pCur->GetString( "src" );
const char *pDestMapping = pCur->GetString( "dest" );
char destDir[MAX_PATH], destDirTemp[MAX_PATH];
Q_snprintf( destDirTemp, sizeof( destDirTemp ), "%s%c%s", GetSDKLauncherBaseDirectory(), CORRECT_PATH_SEPARATOR, pDestMapping );
Q_MakeAbsolutePath( destDir, sizeof( destDir ), destDirTemp );
GetMinFootprintFiles_R( pDisplay->m_Filenames, pSrcMapping, destDir );
}
else if ( Q_stricmp( pCur->GetName(), "single_file" ) == 0 )
{
const char *pDestMapping = pCur->GetString();
// If the filename is preceded by the right prefix, then make it writable.
int flags = 0;
if ( Q_stristr( pDestMapping, WRITABLE_PREFIX ) == pDestMapping )
{
flags |= MF_MAKE_WRITABLE;
pDestMapping += strlen( WRITABLE_PREFIX );
}
// Check for [always_copy]
if ( Q_stristr( pDestMapping, ALWAYS_COPY_PREFIX ) == pDestMapping )
{
flags |= MF_ALWAYS_COPY;
pDestMapping += strlen( ALWAYS_COPY_PREFIX );
}
char destFile[MAX_PATH], destFileTemp[MAX_PATH];
Q_snprintf( destFileTemp, sizeof( destFileTemp ), "%s%c%s", GetSDKLauncherBaseDirectory(), CORRECT_PATH_SEPARATOR, pDestMapping );
Q_MakeAbsolutePath( destFile, sizeof( destFile ), destFileTemp );
if ( bVersionChanged || ( flags & MF_ALWAYS_COPY ) )
{
AddMinFootprintFile( pDisplay->m_Filenames, pDestMapping, destFile, flags );
}
}
else if ( ( Q_stricmp( pCur->GetName(), "create_directory" ) == 0 ) && bVersionChanged )
{
// Create an empty directory?
char destFile[MAX_PATH], destFileTemp[MAX_PATH];
Q_snprintf( destFileTemp, sizeof( destFileTemp ), "%s%c%s", GetSDKLauncherBaseDirectory(), CORRECT_PATH_SEPARATOR, pCur->GetString() );
Q_MakeAbsolutePath( destFile, sizeof( destFile ), destFileTemp );
CreateDirectory( destFile, NULL );
}
}
pDisplay->StartDumpingFiles();
}
|