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