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