]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/path_init.c
q3map2: disable magic path guess
[xonotic/netradiant.git] / tools / quake3 / q3map2 / path_init.c
1 /* -------------------------------------------------------------------------------
2
3    Copyright (C) 1999-2007 id Software, Inc. and contributors.
4    For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6    This file is part of GtkRadiant.
7
8    GtkRadiant is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    GtkRadiant is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GtkRadiant; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21
22    ----------------------------------------------------------------------------------
23
24    This code has been altered significantly from its original form, to support
25    several games based on the Quake III Arena engine, in the form of "Q3Map2."
26
27    ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define PATH_INIT_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /* path support */
42 #define MAX_BASE_PATHS  10
43 #define MAX_GAME_PATHS  10
44 #define MAX_PAK_PATHS  200
45
46 char                    *homePath;
47 char installPath[ MAX_OS_PATH ];
48
49 int numBasePaths;
50 char                    *basePaths[ MAX_BASE_PATHS ];
51 int numGamePaths;
52 char                    *gamePaths[ MAX_GAME_PATHS ];
53 int numPakPaths;
54 char                    *pakPaths[ MAX_PAK_PATHS ];
55 char                    *homeBasePath = NULL;
56
57
58 /*
59    some of this code is based off the original q3map port from loki
60    and finds various paths. moved here from bsp.c for clarity.
61  */
62
63 /*
64    PathLokiGetHomeDir()
65    gets the user's home dir (for ~/.q3a)
66  */
67
68 char *LokiGetHomeDir( void ){
69         #ifndef Q_UNIX
70         return NULL;
71         #else
72         static char     buf[ 4096 ];
73         struct passwd   pw, *pwp;
74         char            *home;
75         static char homeBuf[MAX_OS_PATH];
76
77
78         /* get the home environment variable */
79         home = getenv( "HOME" );
80
81         /* look up home dir in password database */
82         if(!home)
83         {
84                 if ( getpwuid_r( getuid(), &pw, buf, sizeof( buf ), &pwp ) == 0 ) {
85                         return pw.pw_dir;
86                 }
87         }
88
89         snprintf( homeBuf, sizeof( homeBuf ), "%s/.", home );
90
91         /* return it */
92         return homeBuf;
93         #endif
94 }
95
96
97
98 /*
99    PathLokiInitPaths()
100    initializes some paths on linux/os x
101  */
102
103 void LokiInitPaths( char *argv0 ){
104         char        *home;
105
106         if ( !homePath ) {
107                 /* get home dir */
108                 home = LokiGetHomeDir();
109                 if ( home == NULL ) {
110                         home = ".";
111                 }
112
113                 /* set home path */
114                 homePath = home;
115         }
116         else{
117                 home = homePath;
118         }
119
120         #ifndef Q_UNIX
121         /* this is kinda crap, but hey */
122         strcpy( installPath, "../" );
123         #else
124         char temp[ MAX_OS_PATH ];
125         char        *path;
126         char        *last;
127         qboolean found;
128
129
130         path = getenv( "PATH" );
131
132         /* do some path divining */
133         Q_strncpyz( temp, argv0, sizeof( temp ) );
134         if ( strrchr( temp, '/' ) ) {
135                 argv0 = strrchr( argv0, '/' ) + 1;
136         }
137         else if ( path ) {
138
139                 /*
140                    This code has a special behavior when q3map2 is a symbolic link.
141
142                    For each dir in ${PATH} (example: "/usr/bin", "/usr/local/bin" if ${PATH} == "/usr/bin:/usr/local/bin"),
143                    it looks for "${dir}/q3map2" (file exists and is executable),
144                    then it uses "dirname(realpath("${dir}/q3map2"))/../" as installPath.
145
146                    So, if "/usr/bin/q3map2" is a symbolic link to "/opt/radiant/tools/q3map2",
147                    it will find the installPath "/usr/share/radiant/",
148                    so q3map2 will look for "/opt/radiant/baseq3" to find paks.
149
150                    More precisely, it looks for "${dir}/${argv[0]}",
151                    so if "/usr/bin/q3map2" is a symbolic link to "/opt/radiant/tools/q3map2",
152                    and if "/opt/radiant/tools/q3ma2" is a symbolic link to "/opt/radiant/tools/q3map2.x86_64",
153                    it will use "dirname("/opt/radiant/tools/q3map2.x86_64")/../" as path,
154                    so it will use "/opt/radiant/" as installPath, which will be expanded later to "/opt/radiant/baseq3" to find paks.
155                 */
156
157                 found = qfalse;
158                 last = path;
159
160                 /* go through each : segment of path */
161                 while ( last[ 0 ] != '\0' && found == qfalse )
162                 {
163                         /* null out temp */
164                         temp[ 0 ] = '\0';
165
166                         /* find next chunk */
167                         last = strchr( path, ':' );
168                         if ( last == NULL ) {
169                                 last = path + strlen( path );
170                         }
171
172                         /* found home dir candidate */
173                         if ( *path == '~' ) {
174                                 Q_strncpyz( temp, home, sizeof( temp ) );
175                                 path++;
176                         }
177
178
179                         /* concatenate */
180                         if ( last > ( path + 1 ) ) {
181                                 // +1 hack: Q_strncat calls Q_strncpyz that expects a len including '\0'
182                                 // so that extraneous char will be rewritten by '\0', so it's ok.
183                                 // Also, in this case this extraneous char is always ':' or '\0', so it's ok.
184                                 Q_strncat( temp, sizeof( temp ), path, ( last - path + 1) );
185                                 Q_strcat( temp, sizeof( temp ), "/" );
186                         }
187                         Q_strcat( temp, sizeof( temp ), argv0 );
188
189                         /* verify the path */
190                         if ( access( temp, X_OK ) == 0 ) {
191                                 found = qtrue;
192                         }
193                         path = last + 1;
194                 }
195         }
196
197         /* flake */
198         if ( realpath( temp, installPath ) ) {
199                 /*
200                    if "q3map2" is "/opt/radiant/tools/q3map2",
201                    installPath is "/opt/radiant"
202                 */
203                 *( strrchr( installPath, '/' ) ) = '\0';
204                 *( strrchr( installPath, '/' ) ) = '\0';
205         }
206         #endif
207 }
208
209
210
211 /*
212    CleanPath() - ydnar
213    cleans a dos path \ -> /
214  */
215
216 void CleanPath( char *path ){
217         while ( *path )
218         {
219                 if ( *path == '\\' ) {
220                         *path = '/';
221                 }
222                 path++;
223         }
224 }
225
226
227
228 /*
229    GetGame() - ydnar
230    gets the game_t based on a -game argument
231    returns NULL if no match found
232  */
233
234 game_t *GetGame( char *arg ){
235         int i;
236
237
238         /* dummy check */
239         if ( arg == NULL || arg[ 0 ] == '\0' ) {
240                 return NULL;
241         }
242
243         /* joke */
244         if ( !Q_stricmp( arg, "quake1" ) ||
245                  !Q_stricmp( arg, "quake2" ) ||
246                  !Q_stricmp( arg, "unreal" ) ||
247                  !Q_stricmp( arg, "ut2k3" ) ||
248                  !Q_stricmp( arg, "dn3d" ) ||
249                  !Q_stricmp( arg, "dnf" ) ||
250                  !Q_stricmp( arg, "hl" ) ) {
251                 Sys_Printf( "April fools, silly rabbit!\n" );
252                 exit( 0 );
253         }
254
255         /* test it */
256         i = 0;
257         while ( games[ i ].arg != NULL )
258         {
259                 if ( Q_stricmp( arg, games[ i ].arg ) == 0 ) {
260                         return &games[ i ];
261                 }
262                 i++;
263         }
264
265         /* no matching game */
266         return NULL;
267 }
268
269
270
271 /*
272    AddBasePath() - ydnar
273    adds a base path to the list
274  */
275
276 void AddBasePath( char *path ){
277         /* dummy check */
278         if ( path == NULL || path[ 0 ] == '\0' || numBasePaths >= MAX_BASE_PATHS ) {
279                 return;
280         }
281
282         /* add it to the list */
283         basePaths[ numBasePaths ] = safe_malloc( strlen( path ) + 1 );
284         strcpy( basePaths[ numBasePaths ], path );
285         CleanPath( basePaths[ numBasePaths ] );
286         numBasePaths++;
287 }
288
289
290
291 /*
292    AddHomeBasePath() - ydnar
293    adds a base path to the beginning of the list, prefixed by ~/
294  */
295
296 void AddHomeBasePath( char *path ){
297         int i;
298         char temp[ MAX_OS_PATH ];
299         int homePathLen;
300
301         if ( !homePath ) {
302                 return;
303         }
304
305         /* dummy check */
306         if ( path == NULL || path[ 0 ] == '\0' ) {
307                 return;
308         }
309
310         /* strip leading dot, if homePath does not end in /. */
311         homePathLen = strlen( homePath );
312         if ( !strcmp( path, "." ) ) {
313                 /* -fs_homebase . means that -fs_home is to be used as is */
314                 strcpy( temp, homePath );
315         }
316         else if ( homePathLen >= 2 && !strcmp( homePath + homePathLen - 2, "/." ) ) {
317                 /* remove trailing /. of homePath */
318                 homePathLen -= 2;
319
320                 /* concatenate home dir and path */
321                 sprintf( temp, "%.*s/%s", homePathLen, homePath, path );
322         }
323         else
324         {
325                 /* remove leading . of path */
326                 if ( path[0] == '.' ) {
327                         ++path;
328                 }
329
330                 /* concatenate home dir and path */
331                 sprintf( temp, "%s/%s", homePath, path );
332         }
333
334         /* make a hole */
335         for ( i = ( MAX_BASE_PATHS - 2 ); i >= 0; i-- )
336                 basePaths[ i + 1 ] = basePaths[ i ];
337
338         /* add it to the list */
339         basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 );
340         strcpy( basePaths[ 0 ], temp );
341         CleanPath( basePaths[ 0 ] );
342         numBasePaths++;
343 }
344
345
346
347 /*
348    AddGamePath() - ydnar
349    adds a game path to the list
350  */
351
352 void AddGamePath( char *path ){
353         int i;
354
355         /* dummy check */
356         if ( path == NULL || path[ 0 ] == '\0' || numGamePaths >= MAX_GAME_PATHS ) {
357                 return;
358         }
359
360         /* add it to the list */
361         gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 );
362         strcpy( gamePaths[ numGamePaths ], path );
363         CleanPath( gamePaths[ numGamePaths ] );
364         numGamePaths++;
365
366         /* don't add it if it's already there */
367         for ( i = 0; i < numGamePaths - 1; i++ )
368         {
369                 if ( strcmp( gamePaths[i], gamePaths[numGamePaths - 1] ) == 0 ) {
370                         free( gamePaths[numGamePaths - 1] );
371                         gamePaths[numGamePaths - 1] = NULL;
372                         numGamePaths--;
373                         break;
374                 }
375         }
376
377 }
378
379
380 /*
381    AddPakPath()
382    adds a pak path to the list
383  */
384
385 void AddPakPath( char *path ){
386         /* dummy check */
387         if ( path == NULL || path[ 0 ] == '\0' || numPakPaths >= MAX_PAK_PATHS ) {
388                 return;
389         }
390
391         /* add it to the list */
392         pakPaths[ numPakPaths ] = safe_malloc( strlen( path ) + 1 );
393         strcpy( pakPaths[ numPakPaths ], path );
394         CleanPath( pakPaths[ numPakPaths ] );
395         numPakPaths++;
396 }
397
398
399
400 /*
401    InitPaths() - ydnar
402    cleaned up some of the path initialization code from bsp.c
403    will remove any arguments it uses
404  */
405
406 void InitPaths( int *argc, char **argv ){
407         int i, j, k, len, len2;
408         char temp[ MAX_OS_PATH ];
409
410         int noBasePath = 0;
411         int noHomePath = 0;
412         int noMagicPath = 0;
413
414         /* note it */
415         Sys_FPrintf( SYS_VRB, "--- InitPaths ---\n" );
416
417         /* get the install path for backup */
418         LokiInitPaths( argv[ 0 ] );
419
420         /* set game to default (q3a) */
421         game = &games[ 0 ];
422         numBasePaths = 0;
423         numGamePaths = 0;
424
425         /* parse through the arguments and extract those relevant to paths */
426         for ( i = 0; i < *argc; i++ )
427         {
428                 /* check for null */
429                 if ( argv[ i ] == NULL ) {
430                         continue;
431                 }
432
433                 /* -game */
434                 if ( strcmp( argv[ i ], "-game" ) == 0 ) {
435                         if ( ++i >= *argc ) {
436                                 Error( "Out of arguments: No game specified after %s", argv[ i - 1 ] );
437                         }
438                         argv[ i - 1 ] = NULL;
439                         game = GetGame( argv[ i ] );
440                         if ( game == NULL ) {
441                                 game = &games[ 0 ];
442                         }
443                         argv[ i ] = NULL;
444                 }
445
446                 /* -fs_forbiddenpath */
447                 else if ( strcmp( argv[ i ], "-fs_forbiddenpath" ) == 0 ) {
448                         if ( ++i >= *argc ) {
449                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
450                         }
451                         argv[ i - 1 ] = NULL;
452                         if ( g_numForbiddenDirs < VFS_MAXDIRS ) {
453                                 strncpy( g_strForbiddenDirs[g_numForbiddenDirs], argv[i], PATH_MAX );
454                                 g_strForbiddenDirs[g_numForbiddenDirs][PATH_MAX] = 0;
455                                 ++g_numForbiddenDirs;
456                         }
457                         argv[ i ] = NULL;
458                 }
459
460                 /* -fs_nobasepath */
461                 else if ( strcmp( argv[ i ], "-fs_nobasepath" ) == 0 ) {
462                         noBasePath = 1;
463                         argv[ i ] = NULL;
464                 }               
465
466                 /* -fs_nomagicpath */
467                 else if ( strcmp( argv[ i ], "-fs_nomagicpath") == 0) {
468                         noMagicPath = 1;
469                         argv[ i ] = NULL;
470                 }
471
472                 /* -fs_basepath */
473                 else if ( strcmp( argv[ i ], "-fs_basepath" ) == 0 ) {
474                         if ( ++i >= *argc ) {
475                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
476                         }
477                         argv[ i - 1 ] = NULL;
478                         AddBasePath( argv[ i ] );
479                         argv[ i ] = NULL;
480                 }
481
482                 /* -fs_game */
483                 else if ( strcmp( argv[ i ], "-fs_game" ) == 0 ) {
484                         if ( ++i >= *argc ) {
485                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
486                         }
487                         argv[ i - 1 ] = NULL;
488                         AddGamePath( argv[ i ] );
489                         argv[ i ] = NULL;
490                 }
491
492                 /* -fs_home */
493                 else if ( strcmp( argv[ i ], "-fs_home" ) == 0 ) {
494                         if ( ++i >= *argc ) {
495                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
496                         }
497                         argv[ i - 1 ] = NULL;
498                         homePath = argv[i];
499                         argv[ i ] = NULL;
500                 }
501
502                 /* -fs_nohomepath */
503                 else if ( strcmp( argv[ i ], "-fs_nohomepath" ) == 0 ) {
504                         noHomePath = 1;
505                         argv[ i ] = NULL;
506                 }               
507
508                 /* -fs_homebase */
509                 else if ( strcmp( argv[ i ], "-fs_homebase" ) == 0 ) {
510                         if ( ++i >= *argc ) {
511                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
512                         }
513                         argv[ i - 1 ] = NULL;
514                         homeBasePath = argv[i];
515                         argv[ i ] = NULL;
516                 }
517
518                 /* -fs_homepath - sets both of them */
519                 else if ( strcmp( argv[ i ], "-fs_homepath" ) == 0 ) {
520                         if ( ++i >= *argc ) {
521                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
522                         }
523                         argv[ i - 1 ] = NULL;
524                         homePath = argv[i];
525                         homeBasePath = ".";
526                         argv[ i ] = NULL;
527                 }
528
529                 /* -fs_pakpath */
530                 else if ( strcmp( argv[ i ], "-fs_pakpath" ) == 0 ) {
531                         if ( ++i >= *argc ) {
532                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
533                         }
534                         argv[ i - 1 ] = NULL;
535                         AddPakPath( argv[ i ] );
536                         argv[ i ] = NULL;
537                 }
538         }
539
540         /* remove processed arguments */
541         for ( i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++ )
542         {
543                 for ( ; j < *argc && argv[ j ] == NULL; j++ ) ;
544                 argv[ i ] = argv[ j ];
545                 if ( argv[ i ] != NULL ) {
546                         k++;
547                 }
548         }
549         *argc = k;
550
551         /* add standard game path */
552         AddGamePath( game->gamePath );
553
554         /* if there is no base path set, figure it out unless fs_nomagicpath is set */
555         if ( numBasePaths == 0 && noBasePath == 0 && noMagicPath == 0 ) {
556                 /* this is another crappy replacement for SetQdirFromPath() */
557                 len2 = strlen( game->magic );
558                 for ( i = 0; i < *argc && numBasePaths == 0; i++ )
559                 {
560                         /* extract the arg */
561                         strcpy( temp, argv[ i ] );
562                         CleanPath( temp );
563                         len = strlen( temp );
564                         Sys_FPrintf( SYS_VRB, "Searching for \"%s\" in \"%s\" (%d)...\n", game->magic, temp, i );
565
566                         /* this is slow, but only done once */
567                         for ( j = 0; j < ( len - len2 ); j++ )
568                         {
569                                 /* check for the game's magic word */
570                                 if ( Q_strncasecmp( &temp[ j ], game->magic, len2 ) == 0 ) {
571                                         /* now find the next slash and nuke everything after it */
572                                         while ( temp[ ++j ] != '/' && temp[ j ] != '\0' ) ;
573                                         temp[ j ] = '\0';
574
575                                         /* add this as a base path */
576                                         AddBasePath( temp );
577                                         break;
578                                 }
579                         }
580                 }
581
582                 /* add install path */
583                 if ( numBasePaths == 0 ) {
584                         AddBasePath( installPath );
585                 }
586
587                 /* check again */
588                 if ( numBasePaths == 0 ) {
589                         Error( "Failed to find a valid base path." );
590                 }
591         }
592
593         if ( noBasePath == 1 ) {
594                 numBasePaths = 0;
595         }
596
597         if ( noHomePath == 0 ) {
598                 /* this only affects unix */
599                 if ( homeBasePath ) {
600                         AddHomeBasePath( homeBasePath );
601                 }
602                 else{
603                         AddHomeBasePath( game->homeBasePath );
604                 }
605         }
606
607         /* initialize vfs paths */
608         if ( numBasePaths > MAX_BASE_PATHS ) {
609                 numBasePaths = MAX_BASE_PATHS;
610         }
611         if ( numGamePaths > MAX_GAME_PATHS ) {
612                 numGamePaths = MAX_GAME_PATHS;
613         }
614
615         /* walk the list of game paths */
616         for ( j = 0; j < numGamePaths; j++ )
617         {
618                 /* walk the list of base paths */
619                 for ( i = 0; i < numBasePaths; i++ )
620                 {
621                         /* create a full path and initialize it */
622                         sprintf( temp, "%s/%s/", basePaths[ i ], gamePaths[ j ] );
623                         vfsInitDirectory( temp );
624                 }
625         }
626
627         /* initialize vfs paths */
628         if ( numPakPaths > MAX_PAK_PATHS ) {
629                 numPakPaths = MAX_PAK_PATHS;
630         }
631
632         /* walk the list of pak paths */
633         for ( i = 0; i < numPakPaths; i++ )
634         {
635                 /* initialize this pak path */
636                 vfsInitDirectory( pakPaths[ i ] );
637         }
638
639         /* done */
640         Sys_Printf( "\n" );
641 }