-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathfilesystem_init.cpp
1299 lines (1110 loc) · 38.6 KB
/
filesystem_init.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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#undef PROTECTED_THINGS_ENABLE
#undef PROTECT_FILEIO_FUNCTIONS
#ifndef POSIX
#undef fopen
#endif
#if defined( _WIN32 ) && !defined( _X360 )
#include <windows.h>
#include <direct.h>
#include <io.h>
#include <process.h>
#elif defined( POSIX )
#include <unistd.h>
#define _chdir chdir
#define _access access
#endif
#include <stdio.h>
#include <sys/stat.h>
#include "tier1/strtools.h"
#include "tier1/utlbuffer.h"
#include "filesystem_init.h"
#include "tier0/icommandline.h"
#include "KeyValues.h"
#include "appframework/IAppSystemGroup.h"
#include "tier1/smartptr.h"
#if defined( _X360 )
#include "xbox\xbox_win32stubs.h"
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
#if !defined( _X360 )
#define GAMEINFO_FILENAME "gameinfo.txt"
#else
// The .xtx file is a TCR requirement, as .txt files cannot live on the DVD.
// The .xtx file only exists outside the zips (same as .txt and is made during the image build) and is read to setup the search paths.
// So all other code should be able to safely expect gameinfo.txt after the zip is mounted as the .txt file exists inside the zips.
// The .xtx concept is private and should only have to occurr here. As a safety measure, if the .xtx file is not found
// a retry is made with the original .txt name
#define GAMEINFO_FILENAME "gameinfo.xtx"
#endif
#define GAMEINFO_FILENAME_ALTERNATE "gameinfo.txt"
static char g_FileSystemError[256];
static bool s_bUseVProjectBinDir = false;
static FSErrorMode_t g_FileSystemErrorMode = FS_ERRORMODE_VCONFIG;
// Call this to use a bin directory relative to VPROJECT
void FileSystem_UseVProjectBinDir( bool bEnable )
{
s_bUseVProjectBinDir = bEnable;
}
// This class lets you modify environment variables, and it restores the original value
// when it goes out of scope.
class CTempEnvVar
{
public:
CTempEnvVar( const char *pVarName )
{
m_bRestoreOriginalValue = true;
m_pVarName = pVarName;
const char *pValue = NULL;
#ifdef _WIN32
// Use GetEnvironmentVariable instead of getenv because getenv doesn't pick up changes
// to the process environment after the DLL was loaded.
char szBuf[ 4096 ];
if ( GetEnvironmentVariable( m_pVarName, szBuf, sizeof( szBuf ) ) != 0)
{
pValue = szBuf;
}
#else
// LINUX BUG: see above
pValue = getenv( pVarName );
#endif
if ( pValue )
{
m_bExisted = true;
m_OriginalValue.SetSize( Q_strlen( pValue ) + 1 );
memcpy( m_OriginalValue.Base(), pValue, m_OriginalValue.Count() );
}
else
{
m_bExisted = false;
}
}
~CTempEnvVar()
{
if ( m_bRestoreOriginalValue )
{
// Restore the original value.
if ( m_bExisted )
{
SetValue( "%s", m_OriginalValue.Base() );
}
else
{
ClearValue();
}
}
}
void SetRestoreOriginalValue( bool bRestore )
{
m_bRestoreOriginalValue = bRestore;
}
int GetValue(char *pszBuf, int nBufSize )
{
if ( !pszBuf || ( nBufSize <= 0 ) )
return 0;
#ifdef _WIN32
// Use GetEnvironmentVariable instead of getenv because getenv doesn't pick up changes
// to the process environment after the DLL was loaded.
return GetEnvironmentVariable( m_pVarName, pszBuf, nBufSize );
#else
// LINUX BUG: see above
const char *pszOut = getenv( m_pVarName );
if ( !pszOut )
{
*pszBuf = '\0';
return 0;
}
Q_strncpy( pszBuf, pszOut, nBufSize );
return Q_strlen( pszBuf );
#endif
}
void SetValue( const char *pValue, ... )
{
char valueString[4096];
va_list marker;
va_start( marker, pValue );
Q_vsnprintf( valueString, sizeof( valueString ), pValue, marker );
va_end( marker );
#ifdef WIN32
char str[4096];
Q_snprintf( str, sizeof( str ), "%s=%s", m_pVarName, valueString );
_putenv( str );
#else
setenv( m_pVarName, valueString, 1 );
#endif
}
void ClearValue()
{
#ifdef WIN32
char str[512];
Q_snprintf( str, sizeof( str ), "%s=", m_pVarName );
_putenv( str );
#else
setenv( m_pVarName, "", 1 );
#endif
}
private:
bool m_bRestoreOriginalValue;
const char *m_pVarName;
bool m_bExisted;
CUtlVector<char> m_OriginalValue;
};
class CSteamEnvVars
{
public:
CSteamEnvVars() :
m_SteamAppId( "SteamAppId" ),
m_SteamUserPassphrase( "SteamUserPassphrase" ),
m_SteamAppUser( "SteamAppUser" ),
m_Path( "path" )
{
}
void SetRestoreOriginalValue_ALL( bool bRestore )
{
m_SteamAppId.SetRestoreOriginalValue( bRestore );
m_SteamUserPassphrase.SetRestoreOriginalValue( bRestore );
m_SteamAppUser.SetRestoreOriginalValue( bRestore );
m_Path.SetRestoreOriginalValue( bRestore );
}
CTempEnvVar m_SteamAppId;
CTempEnvVar m_SteamUserPassphrase;
CTempEnvVar m_SteamAppUser;
CTempEnvVar m_Path;
};
// ---------------------------------------------------------------------------------------------------- //
// Helpers.
// ---------------------------------------------------------------------------------------------------- //
void Q_getwd( char *out, int outSize )
{
#if defined( _WIN32 ) || defined( WIN32 )
_getcwd( out, outSize );
Q_strncat( out, "\\", outSize, COPY_ALL_CHARACTERS );
#else
getcwd( out, outSize );
strcat( out, "/" );
#endif
Q_FixSlashes( out );
}
// ---------------------------------------------------------------------------------------------------- //
// Module interface.
// ---------------------------------------------------------------------------------------------------- //
CFSSearchPathsInit::CFSSearchPathsInit()
{
m_pDirectoryName = NULL;
m_pLanguage = NULL;
m_ModPath[0] = 0;
m_bMountHDContent = m_bLowViolence = false;
}
CFSSteamSetupInfo::CFSSteamSetupInfo()
{
m_pDirectoryName = NULL;
m_bOnlyUseDirectoryName = false;
m_bSteam = false;
m_bToolsMode = true;
m_bNoGameInfo = false;
}
CFSLoadModuleInfo::CFSLoadModuleInfo()
{
m_pFileSystemDLLName = NULL;
m_pFileSystem = NULL;
m_pModule = NULL;
}
CFSMountContentInfo::CFSMountContentInfo()
{
m_bToolsMode = true;
m_pDirectoryName = NULL;
m_pFileSystem = NULL;
}
const char *FileSystem_GetLastErrorString()
{
return g_FileSystemError;
}
KeyValues* ReadKeyValuesFile( const char *pFilename )
{
// Read in the gameinfo.txt file and null-terminate it.
FILE *fp = fopen( pFilename, "rb" );
if ( !fp )
return NULL;
CUtlVector<char> buf;
fseek( fp, 0, SEEK_END );
buf.SetSize( ftell( fp ) + 1 );
fseek( fp, 0, SEEK_SET );
fread( buf.Base(), 1, buf.Count()-1, fp );
fclose( fp );
buf[buf.Count()-1] = 0;
KeyValues *kv = new KeyValues( "" );
if ( !kv->LoadFromBuffer( pFilename, buf.Base() ) )
{
kv->deleteThis();
return NULL;
}
return kv;
}
static bool Sys_GetExecutableName( char *out, int len )
{
#if defined( _WIN32 )
if ( !::GetModuleFileName( ( HINSTANCE )GetModuleHandle( NULL ), out, len ) )
{
return false;
}
#else
if ( CommandLine()->GetParm(0) )
{
Q_MakeAbsolutePath( out, len, CommandLine()->GetParm(0) );
}
else
{
return false;
}
#endif
return true;
}
bool FileSystem_GetExecutableDir( char *exedir, int exeDirLen )
{
#ifdef ANDROID
Q_snprintf( exedir, exeDirLen, "%s", getenv("APP_LIB_PATH") );
#else
exedir[0] = 0;
if ( s_bUseVProjectBinDir )
{
const char *pProject = GetVProjectCmdLineValue();
if ( !pProject )
{
// Check their registry.
pProject = getenv( GAMEDIR_TOKEN );
}
if ( pProject )
{
Q_snprintf( exedir, exeDirLen, "%s%c..%cbin", pProject, CORRECT_PATH_SEPARATOR, CORRECT_PATH_SEPARATOR );
return true;
}
return false;
}
if ( !Sys_GetExecutableName( exedir, exeDirLen ) )
return false;
Q_StripFilename( exedir );
if ( IsX360() )
{
// The 360 can have its exe and dlls reside on different volumes
// use the optional basedir as the exe dir
if ( CommandLine()->FindParm( "-basedir" ) )
{
strcpy( exedir, CommandLine()->ParmValue( "-basedir", "" ) );
}
}
Q_FixSlashes( exedir );
#ifdef PLATFORM_HAIKU
const char* libDir = "lib";
#else
const char* libDir = "bin";
#endif
// Return the bin directory as the executable dir if it's not in there
// because that's really where we're running from...
char ext[MAX_PATH];
Q_StrRight( exedir, 4, ext, sizeof( ext ) );
if ( ext[0] != CORRECT_PATH_SEPARATOR || Q_stricmp( ext+1, libDir ) != 0 )
{
Q_strncat( exedir, CORRECT_PATH_SEPARATOR_S, exeDirLen, COPY_ALL_CHARACTERS );
Q_strncat( exedir, libDir, exeDirLen, COPY_ALL_CHARACTERS );
Q_FixSlashes( exedir );
}
#endif
return true;
}
static bool FileSystem_GetBaseDir( char *baseDir, int baseDirLen )
{
#ifdef ANDROID
strncpy(baseDir, getenv("VALVE_GAME_PATH"), baseDirLen);
return true;
#else
if ( FileSystem_GetExecutableDir( baseDir, baseDirLen ) )
{
Q_StripFilename( baseDir );
return true;
}
return false;
#endif
}
void LaunchVConfig()
{
#if defined( _WIN32 ) && !defined( _X360 )
char vconfigExe[MAX_PATH];
FileSystem_GetExecutableDir( vconfigExe, sizeof( vconfigExe ) );
Q_AppendSlash( vconfigExe, sizeof( vconfigExe ) );
Q_strncat( vconfigExe, "vconfig.exe", sizeof( vconfigExe ), COPY_ALL_CHARACTERS );
char *argv[] =
{
vconfigExe,
"-allowdebug",
NULL
};
_spawnv( _P_NOWAIT, vconfigExe, argv );
#elif defined( _X360 )
Msg( "Launching vconfig.exe not supported\n" );
#endif
}
const char* GetVProjectCmdLineValue()
{
return CommandLine()->ParmValue( "-vproject", CommandLine()->ParmValue( "-game" ) );
}
FSReturnCode_t SetupFileSystemError( bool bRunVConfig, FSReturnCode_t retVal, const char *pMsg, ... )
{
va_list marker;
va_start( marker, pMsg );
Q_vsnprintf( g_FileSystemError, sizeof( g_FileSystemError ), pMsg, marker );
va_end( marker );
Warning( "%s\n", g_FileSystemError );
// Run vconfig?
// Don't do it if they specifically asked for it not to, or if they manually specified a vconfig with -game or -vproject.
if ( bRunVConfig && g_FileSystemErrorMode == FS_ERRORMODE_VCONFIG && !CommandLine()->FindParm( CMDLINEOPTION_NOVCONFIG ) && !GetVProjectCmdLineValue() )
{
LaunchVConfig();
}
if ( g_FileSystemErrorMode == FS_ERRORMODE_AUTO || g_FileSystemErrorMode == FS_ERRORMODE_VCONFIG )
{
Error( "%s\n", g_FileSystemError );
}
return retVal;
}
FSReturnCode_t LoadGameInfoFile(
const char *pDirectoryName,
KeyValues *&pMainFile,
KeyValues *&pFileSystemInfo,
KeyValues *&pSearchPaths )
{
// If GameInfo.txt exists under pBaseDir, then this is their game directory.
// All the filesystem mappings will be in this file.
char gameinfoFilename[MAX_PATH];
Q_strncpy( gameinfoFilename, pDirectoryName, sizeof( gameinfoFilename ) );
Q_AppendSlash( gameinfoFilename, sizeof( gameinfoFilename ) );
Q_strncat( gameinfoFilename, GAMEINFO_FILENAME, sizeof( gameinfoFilename ), COPY_ALL_CHARACTERS );
Q_FixSlashes( gameinfoFilename );
pMainFile = ReadKeyValuesFile( gameinfoFilename );
if ( IsX360() && !pMainFile )
{
// try again
Q_strncpy( gameinfoFilename, pDirectoryName, sizeof( gameinfoFilename ) );
Q_AppendSlash( gameinfoFilename, sizeof( gameinfoFilename ) );
Q_strncat( gameinfoFilename, GAMEINFO_FILENAME_ALTERNATE, sizeof( gameinfoFilename ), COPY_ALL_CHARACTERS );
Q_FixSlashes( gameinfoFilename );
pMainFile = ReadKeyValuesFile( gameinfoFilename );
}
if ( !pMainFile )
{
return SetupFileSystemError( true, FS_MISSING_GAMEINFO_FILE, "%s is missing.", gameinfoFilename );
}
pFileSystemInfo = pMainFile->FindKey( "FileSystem" );
if ( !pFileSystemInfo )
{
pMainFile->deleteThis();
return SetupFileSystemError( true, FS_INVALID_GAMEINFO_FILE, "%s is not a valid format.", gameinfoFilename );
}
// Now read in all the search paths.
pSearchPaths = pFileSystemInfo->FindKey( "SearchPaths" );
if ( !pSearchPaths )
{
pMainFile->deleteThis();
return SetupFileSystemError( true, FS_INVALID_GAMEINFO_FILE, "%s is not a valid format.", gameinfoFilename );
}
return FS_OK;
}
static void FileSystem_AddLoadedSearchPath(
CFSSearchPathsInit &initInfo,
const char *pPathID,
const char *fullLocationPath,
bool bLowViolence )
{
// Check for mounting LV game content in LV builds only
if ( V_stricmp( pPathID, "game_lv" ) == 0 )
{
// Not in LV build, don't mount
if ( !initInfo.m_bLowViolence )
return;
// Mount, as a game path
pPathID = "game";
}
// Check for mounting HD game content if enabled
if ( V_stricmp( pPathID, "game_hd" ) == 0 )
{
// Not in LV build, don't mount
if ( !initInfo.m_bMountHDContent )
return;
// Mount, as a game path
pPathID = "game";
}
// Special processing for ordinary game folders
if ( V_stristr( fullLocationPath, ".vpk" ) == NULL && Q_stricmp( pPathID, "game" ) == 0 )
{
if ( CommandLine()->FindParm( "-tempcontent" ) != 0 )
{
char szPath[MAX_PATH];
Q_snprintf( szPath, sizeof(szPath), "%s_tempcontent", fullLocationPath );
initInfo.m_pFileSystem->AddSearchPath( szPath, pPathID, PATH_ADD_TO_TAIL );
}
}
if ( initInfo.m_pLanguage &&
Q_stricmp( initInfo.m_pLanguage, "english" ) &&
V_strstr( fullLocationPath, "_english" ) != NULL )
{
char szPath[MAX_PATH];
char szLangString[MAX_PATH];
// Need to add a language version of this path first
Q_snprintf( szLangString, sizeof(szLangString), "_%s", initInfo.m_pLanguage);
V_StrSubst( fullLocationPath, "_english", szLangString, szPath, sizeof( szPath ), true );
initInfo.m_pFileSystem->AddSearchPath( szPath, pPathID, PATH_ADD_TO_TAIL );
}
initInfo.m_pFileSystem->AddSearchPath( fullLocationPath, pPathID, PATH_ADD_TO_TAIL );
}
static int SortStricmp( char * const * sz1, char * const * sz2 )
{
return V_stricmp( *sz1, *sz2 );
}
FSReturnCode_t FileSystem_LoadSearchPaths( CFSSearchPathsInit &initInfo )
{
if ( !initInfo.m_pFileSystem || !initInfo.m_pDirectoryName )
return SetupFileSystemError( false, FS_INVALID_PARAMETERS, "FileSystem_LoadSearchPaths: Invalid parameters specified." );
KeyValues *pMainFile, *pFileSystemInfo, *pSearchPaths;
FSReturnCode_t retVal = LoadGameInfoFile( initInfo.m_pDirectoryName, pMainFile, pFileSystemInfo, pSearchPaths );
if ( retVal != FS_OK )
return retVal;
// All paths except those marked with |gameinfo_path| are relative to the base dir.
char baseDir[MAX_PATH];
if ( !FileSystem_GetBaseDir( baseDir, sizeof( baseDir ) ) )
return SetupFileSystemError( false, FS_INVALID_PARAMETERS, "FileSystem_GetBaseDir failed." );
Msg("filesystem BaseDir: %s\n", baseDir);
// The MOD directory is always the one that contains gameinfo.txt
Q_strncpy( initInfo.m_ModPath, initInfo.m_pDirectoryName, sizeof( initInfo.m_ModPath ) );
#define GAMEINFOPATH_TOKEN "|gameinfo_path|"
#define BASESOURCEPATHS_TOKEN "|all_source_engine_paths|"
const char *pszExtraSearchPath = CommandLine()->ParmValue( "-insert_search_path" );
if ( pszExtraSearchPath )
{
CUtlStringList vecPaths;
V_SplitString( pszExtraSearchPath, ",", vecPaths );
FOR_EACH_VEC( vecPaths, idxExtraPath )
{
char szAbsSearchPath[MAX_PATH];
Q_StripPrecedingAndTrailingWhitespace( vecPaths[ idxExtraPath ] );
V_MakeAbsolutePath( szAbsSearchPath, sizeof( szAbsSearchPath ), vecPaths[ idxExtraPath ], baseDir );
V_FixSlashes( szAbsSearchPath );
if ( !V_RemoveDotSlashes( szAbsSearchPath ) )
Error( "Bad -insert_search_path - Can't resolve pathname for '%s'", szAbsSearchPath );
V_StripTrailingSlash( szAbsSearchPath );
FileSystem_AddLoadedSearchPath( initInfo, "GAME", szAbsSearchPath, false );
FileSystem_AddLoadedSearchPath( initInfo, "MOD", szAbsSearchPath, false );
}
}
const char *ExtraVpkPaths = getenv( "EXTRAS_VPK_PATH" );
char szAbsSearchPath[MAX_PATH];
if( ExtraVpkPaths )
{
CUtlStringList vecPaths;
V_SplitString( ExtraVpkPaths, ",", vecPaths );
FOR_EACH_VEC( vecPaths, idxExtraPath )
{
FileSystem_AddLoadedSearchPath( initInfo, "GAME", vecPaths[idxExtraPath], false );
}
}
bool bLowViolence = initInfo.m_bLowViolence;
for ( KeyValues *pCur=pSearchPaths->GetFirstValue(); pCur; pCur=pCur->GetNextValue() )
{
const char *pLocation = pCur->GetString();
const char *pszBaseDir = baseDir;
if ( Q_stristr( pLocation, GAMEINFOPATH_TOKEN ) == pLocation )
{
pLocation += strlen( GAMEINFOPATH_TOKEN );
pszBaseDir = initInfo.m_pDirectoryName;
}
else if ( Q_stristr( pLocation, BASESOURCEPATHS_TOKEN ) == pLocation )
{
// This is a special identifier that tells it to add the specified path for all source engine versions equal to or prior to this version.
// So in Orange Box, if they specified:
// |all_source_engine_paths|hl2
// it would add the ep2\hl2 folder and the base (ep1-era) hl2 folder.
//
// We need a special identifier in the gameinfo.txt here because the base hl2 folder exists in different places.
// In the case of a game or a Steam-launched dedicated server, all the necessary prior engine content is mapped in with the Steam depots,
// so we can just use the path as-is.
pLocation += strlen( BASESOURCEPATHS_TOKEN );
}
CUtlStringList vecFullLocationPaths;
V_MakeAbsolutePath( szAbsSearchPath, sizeof( szAbsSearchPath ), pLocation, pszBaseDir );
// Now resolve any ./'s.
V_FixSlashes( szAbsSearchPath );
if ( !V_RemoveDotSlashes( szAbsSearchPath ) )
Error( "FileSystem_AddLoadedSearchPath - Can't resolve pathname for '%s'", szAbsSearchPath );
V_StripTrailingSlash( szAbsSearchPath );
// Don't bother doing any wildcard expansion unless it has wildcards. This avoids the weird
// thing with xxx_dir.vpk files being referred to simply as xxx.vpk.
if ( V_stristr( pLocation, "?") == NULL && V_stristr( pLocation, "*") == NULL )
{
vecFullLocationPaths.CopyAndAddToTail( szAbsSearchPath );
}
else
{
FileFindHandle_t findHandle = NULL;
const char *pszFoundShortName = initInfo.m_pFileSystem->FindFirst( szAbsSearchPath, &findHandle );
if ( pszFoundShortName )
{
do
{
// We only know how to mount VPK's and directories
if ( pszFoundShortName[0] != '.' && ( initInfo.m_pFileSystem->FindIsDirectory( findHandle ) || V_stristr( pszFoundShortName, ".vpk" ) ) )
{
char szAbsName[MAX_PATH];
V_ExtractFilePath( szAbsSearchPath, szAbsName, sizeof( szAbsName ) );
V_AppendSlash( szAbsName, sizeof(szAbsName) );
V_strcat_safe( szAbsName, pszFoundShortName );
vecFullLocationPaths.CopyAndAddToTail( szAbsName );
// Check for a common mistake
if (
!V_stricmp( pszFoundShortName, "materials" )
|| !V_stricmp( pszFoundShortName, "maps" )
|| !V_stricmp( pszFoundShortName, "resource" )
|| !V_stricmp( pszFoundShortName, "scripts" )
|| !V_stricmp( pszFoundShortName, "sound" )
|| !V_stricmp( pszFoundShortName, "models" ) )
{
char szReadme[MAX_PATH];
V_ExtractFilePath( szAbsSearchPath, szReadme, sizeof( szReadme ) );
V_AppendSlash( szReadme, sizeof(szReadme) );
V_strcat_safe( szReadme, "readme.txt" );
Error(
"Tried to add %s as a search path.\n"
"\nThis is probably not what you intended.\n"
"\nCheck %s for more info\n",
szAbsName, szReadme );
}
}
pszFoundShortName = initInfo.m_pFileSystem->FindNext( findHandle );
} while ( pszFoundShortName );
initInfo.m_pFileSystem->FindClose( findHandle );
}
// Sort alphabetically. Also note that this will put
// all the xxx_000.vpk packs just before the corresponding
// xxx_dir.vpk
vecFullLocationPaths.Sort( SortStricmp );
// Now for any _dir.vpk files, remove the _nnn.vpk ones.
int idx = vecFullLocationPaths.Count()-1;
while ( idx > 0 )
{
char szTemp[ MAX_PATH ];
V_strcpy_safe( szTemp, vecFullLocationPaths[ idx ] );
--idx;
char *szDirVpk = V_stristr( szTemp, "_dir.vpk" );
if ( szDirVpk != NULL )
{
*szDirVpk = '\0';
while ( idx >= 0 )
{
char *pszPath = vecFullLocationPaths[ idx ];
if ( V_stristr( pszPath, szTemp ) != pszPath )
break;
delete pszPath;
vecFullLocationPaths.Remove( idx );
--idx;
}
}
}
}
// Parse Path ID list
CUtlStringList vecPathIDs;
V_SplitString( pCur->GetName(), "+", vecPathIDs );
FOR_EACH_VEC( vecPathIDs, idxPathID )
{
Q_StripPrecedingAndTrailingWhitespace( vecPathIDs[ idxPathID ] );
}
// Mount them.
FOR_EACH_VEC( vecFullLocationPaths, idxLocation )
{
FOR_EACH_VEC( vecPathIDs, idxPathID )
{
FileSystem_AddLoadedSearchPath( initInfo, vecPathIDs[ idxPathID ], vecFullLocationPaths[ idxLocation ], bLowViolence );
}
}
}
pMainFile->deleteThis();
// Also, mark specific path IDs as "by request only". That way, we won't waste time searching in them
// when people forget to specify a search path.
initInfo.m_pFileSystem->MarkPathIDByRequestOnly( "executable_path", true );
initInfo.m_pFileSystem->MarkPathIDByRequestOnly( "gamebin", true );
initInfo.m_pFileSystem->MarkPathIDByRequestOnly( "download", true );
initInfo.m_pFileSystem->MarkPathIDByRequestOnly( "mod", true );
initInfo.m_pFileSystem->MarkPathIDByRequestOnly( "game_write", true );
initInfo.m_pFileSystem->MarkPathIDByRequestOnly( "mod_write", true );
#ifdef _DEBUG
// initInfo.m_pFileSystem->PrintSearchPaths();
#endif
return FS_OK;
}
bool DoesFileExistIn( const char *pDirectoryName, const char *pFilename )
{
char filename[MAX_PATH];
Q_strncpy( filename, pDirectoryName, sizeof( filename ) );
Q_AppendSlash( filename, sizeof( filename ) );
Q_strncat( filename, pFilename, sizeof( filename ), COPY_ALL_CHARACTERS );
Q_FixSlashes( filename );
bool bExist = ( _access( filename, 0 ) == 0 );
return ( bExist );
}
namespace
{
SuggestGameInfoDirFn_t & GetSuggestGameInfoDirFn( void )
{
static SuggestGameInfoDirFn_t s_pfnSuggestGameInfoDir = NULL;
return s_pfnSuggestGameInfoDir;
}
}; // `anonymous` namespace
SuggestGameInfoDirFn_t SetSuggestGameInfoDirFn( SuggestGameInfoDirFn_t pfnNewFn )
{
SuggestGameInfoDirFn_t &rfn = GetSuggestGameInfoDirFn();
SuggestGameInfoDirFn_t pfnOldFn = rfn;
rfn = pfnNewFn;
return pfnOldFn;
}
static FSReturnCode_t TryLocateGameInfoFile( char *pOutDir, int outDirLen, bool bBubbleDir )
{
// Retain a copy of suggested path for further attempts
CArrayAutoPtr < char > spchCopyNameBuffer( new char [ outDirLen ] );
Q_strncpy( spchCopyNameBuffer.Get(), pOutDir, outDirLen );
spchCopyNameBuffer[ outDirLen - 1 ] = 0;
// Make appropriate slashes ('/' - Linux style)
for ( char *pchFix = spchCopyNameBuffer.Get(),
*pchEnd = pchFix + outDirLen;
pchFix < pchEnd; ++ pchFix )
{
if ( '\\' == *pchFix )
{
*pchFix = '/';
}
}
// Have a look in supplied path
do
{
if ( DoesFileExistIn( pOutDir, GAMEINFO_FILENAME ) )
{
return FS_OK;
}
if ( IsX360() && DoesFileExistIn( pOutDir, GAMEINFO_FILENAME_ALTERNATE ) )
{
return FS_OK;
}
}
while ( bBubbleDir && Q_StripLastDir( pOutDir, outDirLen ) );
// Make an attempt to resolve from "content -> game" directory
Q_strncpy( pOutDir, spchCopyNameBuffer.Get(), outDirLen );
pOutDir[ outDirLen - 1 ] = 0;
if ( char *pchContentFix = Q_stristr( pOutDir, "/content/" ) )
{
sprintf( pchContentFix, "/game/" );
memmove( pchContentFix + 6, pchContentFix + 9, pOutDir + outDirLen - (pchContentFix + 9) );
// Try in the mapped "game" directory
do
{
if ( DoesFileExistIn( pOutDir, GAMEINFO_FILENAME ) )
{
return FS_OK;
}
if ( IsX360() && DoesFileExistIn( pOutDir, GAMEINFO_FILENAME_ALTERNATE ) )
{
return FS_OK;
}
}
while ( bBubbleDir && Q_StripLastDir( pOutDir, outDirLen ) );
}
// Could not find it here
return FS_MISSING_GAMEINFO_FILE;
}
FSReturnCode_t LocateGameInfoFile( const CFSSteamSetupInfo &fsInfo, char *pOutDir, int outDirLen )
{
// Engine and Hammer don't want to search around for it.
if ( fsInfo.m_bOnlyUseDirectoryName )
{
if ( !fsInfo.m_pDirectoryName )
return SetupFileSystemError( false, FS_MISSING_GAMEINFO_FILE, "bOnlyUseDirectoryName=1 and pDirectoryName=NULL." );
bool bExists = DoesFileExistIn( fsInfo.m_pDirectoryName, GAMEINFO_FILENAME );
if ( IsX360() && !bExists )
{
bExists = DoesFileExistIn( fsInfo.m_pDirectoryName, GAMEINFO_FILENAME_ALTERNATE );
}
if ( !bExists )
{
if ( IsX360() && CommandLine()->FindParm( "-basedir" ) )
{
char basePath[MAX_PATH];
strcpy( basePath, CommandLine()->ParmValue( "-basedir", "" ) );
Q_AppendSlash( basePath, sizeof( basePath ) );
Q_strncat( basePath, fsInfo.m_pDirectoryName, sizeof( basePath ), COPY_ALL_CHARACTERS );
if ( DoesFileExistIn( basePath, GAMEINFO_FILENAME ) )
{
Q_strncpy( pOutDir, basePath, outDirLen );
return FS_OK;
}
if ( IsX360() && DoesFileExistIn( basePath, GAMEINFO_FILENAME_ALTERNATE ) )
{
Q_strncpy( pOutDir, basePath, outDirLen );
return FS_OK;
}
}
return SetupFileSystemError( true, FS_MISSING_GAMEINFO_FILE, "Setup file '%s' doesn't exist in subdirectory '%s'.\nCheck your -game parameter or VCONFIG setting.", GAMEINFO_FILENAME, fsInfo.m_pDirectoryName );
}
Q_strncpy( pOutDir, fsInfo.m_pDirectoryName, outDirLen );
return FS_OK;
}
// First, check for overrides on the command line or environment variables.
const char *pProject = GetVProjectCmdLineValue();
if ( pProject )
{
if ( DoesFileExistIn( pProject, GAMEINFO_FILENAME ) )
{
Q_MakeAbsolutePath( pOutDir, outDirLen, pProject );
return FS_OK;
}
if ( IsX360() && DoesFileExistIn( pProject, GAMEINFO_FILENAME_ALTERNATE ) )
{
Q_MakeAbsolutePath( pOutDir, outDirLen, pProject );
return FS_OK;
}
if ( IsX360() && CommandLine()->FindParm( "-basedir" ) )
{
char basePath[MAX_PATH];
strcpy( basePath, CommandLine()->ParmValue( "-basedir", "" ) );
Q_AppendSlash( basePath, sizeof( basePath ) );
Q_strncat( basePath, pProject, sizeof( basePath ), COPY_ALL_CHARACTERS );
if ( DoesFileExistIn( basePath, GAMEINFO_FILENAME ) )
{
Q_strncpy( pOutDir, basePath, outDirLen );
return FS_OK;
}
if ( DoesFileExistIn( basePath, GAMEINFO_FILENAME_ALTERNATE ) )
{
Q_strncpy( pOutDir, basePath, outDirLen );
return FS_OK;
}
}
if ( fsInfo.m_bNoGameInfo )
{
// fsInfo.m_bNoGameInfo is set by the Steam dedicated server, before it knows which mod to use.
// Steam dedicated server doesn't need a gameinfo.txt, because we'll ask which mod to use, even if
// -game is supplied on the command line.
Q_strncpy( pOutDir, "", outDirLen );
return FS_OK;
}
else
{
// They either specified vproject on the command line or it's in their registry. Either way,
// we don't want to continue if they've specified it but it's not valid.
goto ShowError;
}
}
if ( fsInfo.m_bNoGameInfo )
{
Q_strncpy( pOutDir, "", outDirLen );
return FS_OK;
}
// Ask the application if it can provide us with a game info directory
{
bool bBubbleDir = true;
SuggestGameInfoDirFn_t pfnSuggestGameInfoDirFn = GetSuggestGameInfoDirFn();
if ( pfnSuggestGameInfoDirFn &&
( * pfnSuggestGameInfoDirFn )( &fsInfo, pOutDir, outDirLen, &bBubbleDir ) &&
FS_OK == TryLocateGameInfoFile( pOutDir, outDirLen, bBubbleDir ) )
return FS_OK;
}
// Try to use the environment variable / registry
if ( ( pProject = getenv( GAMEDIR_TOKEN ) ) != NULL &&
( Q_MakeAbsolutePath( pOutDir, outDirLen, pProject ), 1 ) &&
FS_OK == TryLocateGameInfoFile( pOutDir, outDirLen, false ) )
return FS_OK;
if ( IsPC() )
{
Warning( "Warning: falling back to auto detection of vproject directory.\n" );
// Now look for it in the directory they passed in.
if ( fsInfo.m_pDirectoryName )
Q_MakeAbsolutePath( pOutDir, outDirLen, fsInfo.m_pDirectoryName );
else
Q_MakeAbsolutePath( pOutDir, outDirLen, "." );
if ( FS_OK == TryLocateGameInfoFile( pOutDir, outDirLen, true ) )
return FS_OK;
// Use the CWD
Q_getwd( pOutDir, outDirLen );
if ( FS_OK == TryLocateGameInfoFile( pOutDir, outDirLen, true ) )
return FS_OK;
}
ShowError:
return SetupFileSystemError( true, FS_MISSING_GAMEINFO_FILE,
"Unable to find %s. Solutions:\n\n"
"1. Read http://www.valve-erc.com/srcsdk/faq.html#NoGameDir\n"
"2. Run vconfig to specify which game you're working on.\n"
"3. Add -game <path> on the command line where <path> is the directory that %s is in.\n",
GAMEINFO_FILENAME, GAMEINFO_FILENAME );
}
bool DoesPathExistAlready( const char *pPathEnvVar, const char *pTestPath )
{
// Fix the slashes in the input arguments.
char correctedPathEnvVar[8192], correctedTestPath[MAX_PATH];
Q_strncpy( correctedPathEnvVar, pPathEnvVar, sizeof( correctedPathEnvVar ) );
Q_FixSlashes( correctedPathEnvVar );
pPathEnvVar = correctedPathEnvVar;
Q_strncpy( correctedTestPath, pTestPath, sizeof( correctedTestPath ) );
Q_FixSlashes( correctedTestPath );
if ( strlen( correctedTestPath ) > 0 && PATHSEPARATOR( correctedTestPath[strlen(correctedTestPath)-1] ) )
correctedTestPath[ strlen(correctedTestPath) - 1 ] = 0;
pTestPath = correctedTestPath;
const char *pCurPos = pPathEnvVar;