]> git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/path_init.c
Merge commit 'bf6dd1f2d186c799adf11f1e744a1ff57aa8d335' into garux-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
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
125         char temp[ MAX_OS_PATH ];
126         char *path;
127         char *last;
128         qboolean found;
129
130
131         path = getenv( "PATH" );
132
133         /* do some path divining */
134         Q_strncpyz( temp, argv0, sizeof( temp ) );
135         if ( strrchr( temp, '/' ) ) {
136                 argv0 = strrchr( argv0, '/' ) + 1;
137         }
138         else if ( path ) {
139
140                 /*
141                    This code has a special behavior when q3map2 is a symbolic link.
142
143                    For each dir in ${PATH} (example: "/usr/bin", "/usr/local/bin" if ${PATH} == "/usr/bin:/usr/local/bin"),
144                    it looks for "${dir}/q3map2" (file exists and is executable),
145                    then it uses "dirname(realpath("${dir}/q3map2"))/../" as installPath.
146
147                    So, if "/usr/bin/q3map2" is a symbolic link to "/opt/radiant/tools/q3map2",
148                    it will find the installPath "/usr/share/radiant/",
149                    so q3map2 will look for "/opt/radiant/baseq3" to find paks.
150
151                    More precisely, it looks for "${dir}/${argv[0]}",
152                    so if "/usr/bin/q3map2" is a symbolic link to "/opt/radiant/tools/q3map2",
153                    and if "/opt/radiant/tools/q3ma2" is a symbolic link to "/opt/radiant/tools/q3map2.x86_64",
154                    it will use "dirname("/opt/radiant/tools/q3map2.x86_64")/../" as path,
155                    so it will use "/opt/radiant/" as installPath, which will be expanded later to "/opt/radiant/baseq3" to find paks.
156                 */
157
158                 found = qfalse;
159                 last = path;
160
161                 /* go through each : segment of path */
162                 while ( last[ 0 ] != '\0' && found == qfalse )
163                 {
164                         /* null out temp */
165                         temp[ 0 ] = '\0';
166
167                         /* find next chunk */
168                         last = strchr( path, ':' );
169                         if ( last == NULL ) {
170                                 last = path + strlen( path );
171                         }
172
173                         /* found home dir candidate */
174                         if ( *path == '~' ) {
175                                 Q_strncpyz( temp, home, sizeof( temp ) );
176                                 path++;
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         if ( EnginePath[0] == '\0' ) strcpy( EnginePath, basePaths[ numBasePaths ] );
287         numBasePaths++;
288 }
289
290
291
292 /*
293    AddHomeBasePath() - ydnar
294    adds a base path to the beginning of the list, prefixed by ~/
295  */
296
297 void AddHomeBasePath( char *path ){
298         int i;
299         char temp[ MAX_OS_PATH ];
300         int homePathLen;
301
302         if ( !homePath ) {
303                 return;
304         }
305
306         /* dummy check */
307         if ( path == NULL || path[ 0 ] == '\0' ) {
308                 return;
309         }
310
311         /* strip leading dot, if homePath does not end in /. */
312         homePathLen = strlen( homePath );
313         if ( !strcmp( path, "." ) ) {
314                 /* -fs_homebase . means that -fs_home is to be used as is */
315                 strcpy( temp, homePath );
316         }
317         else if ( homePathLen >= 2 && !strcmp( homePath + homePathLen - 2, "/." ) ) {
318                 /* remove trailing /. of homePath */
319                 homePathLen -= 2;
320
321                 /* concatenate home dir and path */
322                 sprintf( temp, "%.*s/%s", homePathLen, homePath, path );
323         }
324         else
325         {
326                 /* remove leading . of path */
327                 if ( path[0] == '.' ) {
328                         ++path;
329                 }
330
331                 /* concatenate home dir and path */
332                 sprintf( temp, "%s/%s", homePath, path );
333         }
334
335         /* make a hole */
336         for ( i = ( MAX_BASE_PATHS - 2 ); i >= 0; i-- )
337                 basePaths[ i + 1 ] = basePaths[ i ];
338
339         /* add it to the list */
340         basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 );
341         strcpy( basePaths[ 0 ], temp );
342         CleanPath( basePaths[ 0 ] );
343         numBasePaths++;
344 }
345
346
347
348 /*
349    AddGamePath() - ydnar
350    adds a game path to the list
351  */
352
353 void AddGamePath( char *path ){
354         int i;
355
356         /* dummy check */
357         if ( path == NULL || path[ 0 ] == '\0' || numGamePaths >= MAX_GAME_PATHS ) {
358                 return;
359         }
360
361         /* add it to the list */
362         gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 );
363         strcpy( gamePaths[ numGamePaths ], path );
364         CleanPath( gamePaths[ numGamePaths ] );
365         numGamePaths++;
366
367         /* don't add it if it's already there */
368         for ( i = 0; i < numGamePaths - 1; i++ )
369         {
370                 if ( strcmp( gamePaths[i], gamePaths[numGamePaths - 1] ) == 0 ) {
371                         free( gamePaths[numGamePaths - 1] );
372                         gamePaths[numGamePaths - 1] = NULL;
373                         numGamePaths--;
374                         break;
375                 }
376         }
377
378 }
379
380
381 /*
382    AddPakPath()
383    adds a pak path to the list
384  */
385
386 void AddPakPath( char *path ){
387         /* dummy check */
388         if ( path == NULL || path[ 0 ] == '\0' || numPakPaths >= MAX_PAK_PATHS ) {
389                 return;
390         }
391
392         /* add it to the list */
393         pakPaths[ numPakPaths ] = safe_malloc( strlen( path ) + 1 );
394         strcpy( pakPaths[ numPakPaths ], path );
395         CleanPath( pakPaths[ numPakPaths ] );
396         numPakPaths++;
397 }
398
399
400
401 /*
402    InitPaths() - ydnar
403    cleaned up some of the path initialization code from bsp.c
404    will remove any arguments it uses
405  */
406
407 void InitPaths( int *argc, char **argv ){
408         int i, j, k, len, len2;
409         char temp[ MAX_OS_PATH ];
410
411         int noBasePath = 0;
412         int noHomePath = 0;
413         int noMagicPath = 0;
414
415         /* note it */
416         Sys_FPrintf( SYS_VRB, "--- InitPaths ---\n" );
417
418         /* get the install path for backup */
419         LokiInitPaths( argv[ 0 ] );
420
421         /* set game to default (q3a) */
422         game = &games[ 0 ];
423         numBasePaths = 0;
424         numGamePaths = 0;
425
426         EnginePath[0] = '\0';
427
428         /* parse through the arguments and extract those relevant to paths */
429         for ( i = 0; i < *argc; i++ )
430         {
431                 /* check for null */
432                 if ( argv[ i ] == NULL ) {
433                         continue;
434                 }
435
436                 /* -game */
437                 if ( strcmp( argv[ i ], "-game" ) == 0 ) {
438                         if ( ++i >= *argc ) {
439                                 Error( "Out of arguments: No game specified after %s", argv[ i - 1 ] );
440                         }
441                         argv[ i - 1 ] = NULL;
442                         game = GetGame( argv[ i ] );
443                         if ( game == NULL ) {
444                                 game = &games[ 0 ];
445                         }
446                         argv[ i ] = NULL;
447                 }
448
449                 /* -fs_forbiddenpath */
450                 else if ( strcmp( argv[ i ], "-fs_forbiddenpath" ) == 0 ) {
451                         if ( ++i >= *argc ) {
452                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
453                         }
454                         argv[ i - 1 ] = NULL;
455                         if ( g_numForbiddenDirs < VFS_MAXDIRS ) {
456                                 strncpy( g_strForbiddenDirs[g_numForbiddenDirs], argv[i], PATH_MAX );
457                                 g_strForbiddenDirs[g_numForbiddenDirs][PATH_MAX] = 0;
458                                 ++g_numForbiddenDirs;
459                         }
460                         argv[ i ] = NULL;
461                 }
462
463                 /* -fs_nobasepath */
464                 else if ( strcmp( argv[ i ], "-fs_nobasepath" ) == 0 ) {
465                         noBasePath = 1;
466                         // we don't want any basepath, neither guessed ones
467                         noMagicPath = 1;
468                         argv[ i ] = NULL;
469                 }               
470
471                 /* -fs_nomagicpath */
472                 else if ( strcmp( argv[ i ], "-fs_nomagicpath") == 0) {
473                         noMagicPath = 1;
474                         argv[ i ] = NULL;
475                 }
476
477                 /* -fs_basepath */
478                 else if ( strcmp( argv[ i ], "-fs_basepath" ) == 0 ) {
479                         if ( ++i >= *argc ) {
480                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
481                         }
482                         argv[ i - 1 ] = NULL;
483                         AddBasePath( argv[ i ] );
484                         argv[ i ] = NULL;
485                 }
486
487                 /* -fs_game */
488                 else if ( strcmp( argv[ i ], "-fs_game" ) == 0 ) {
489                         if ( ++i >= *argc ) {
490                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
491                         }
492                         argv[ i - 1 ] = NULL;
493                         AddGamePath( argv[ i ] );
494                         argv[ i ] = NULL;
495                 }
496
497                 /* -fs_home */
498                 else if ( strcmp( argv[ i ], "-fs_home" ) == 0 ) {
499                         if ( ++i >= *argc ) {
500                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
501                         }
502                         argv[ i - 1 ] = NULL;
503                         homePath = argv[i];
504                         argv[ i ] = NULL;
505                 }
506
507                 /* -fs_nohomepath */
508                 else if ( strcmp( argv[ i ], "-fs_nohomepath" ) == 0 ) {
509                         noHomePath = 1;
510                         argv[ i ] = NULL;
511                 }               
512
513                 /* -fs_homebase */
514                 else if ( strcmp( argv[ i ], "-fs_homebase" ) == 0 ) {
515                         if ( ++i >= *argc ) {
516                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
517                         }
518                         argv[ i - 1 ] = NULL;
519                         homeBasePath = argv[i];
520                         argv[ i ] = NULL;
521                 }
522
523                 /* -fs_homepath - sets both of them */
524                 else if ( strcmp( argv[ i ], "-fs_homepath" ) == 0 ) {
525                         if ( ++i >= *argc ) {
526                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
527                         }
528                         argv[ i - 1 ] = NULL;
529                         homePath = argv[i];
530                         homeBasePath = ".";
531                         argv[ i ] = NULL;
532                 }
533
534                 /* -fs_pakpath */
535                 else if ( strcmp( argv[ i ], "-fs_pakpath" ) == 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                         AddPakPath( argv[ i ] );
541                         argv[ i ] = NULL;
542                 }
543         }
544
545         /* remove processed arguments */
546         for ( i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++ )
547         {
548                 for ( ; j < *argc && argv[ j ] == NULL; j++ ) ;
549                 argv[ i ] = argv[ j ];
550                 if ( argv[ i ] != NULL ) {
551                         k++;
552                 }
553         }
554         *argc = k;
555
556         /* add standard game path */
557         AddGamePath( game->gamePath );
558
559         /* if there is no base path set, figure it out unless fs_nomagicpath is set */
560         if ( numBasePaths == 0 && noBasePath == 0 && noMagicPath == 0 ) {
561                 /* this is another crappy replacement for SetQdirFromPath() */
562                 len2 = strlen( game->magic );
563                 for ( i = 0; i < *argc && numBasePaths == 0; i++ )
564                 {
565                         /* extract the arg */
566                         strcpy( temp, argv[ i ] );
567                         CleanPath( temp );
568                         len = strlen( temp );
569                         Sys_FPrintf( SYS_VRB, "Searching for \"%s\" in \"%s\" (%d)...\n", game->magic, temp, i );
570
571                         /* this is slow, but only done once */
572                         for ( j = 0; j < ( len - len2 ); j++ )
573                         {
574                                 /* check for the game's magic word */
575                                 if ( Q_strncasecmp( &temp[ j ], game->magic, len2 ) == 0 ) {
576                                         /* now find the next slash and nuke everything after it */
577                                         while ( temp[ ++j ] != '/' && temp[ j ] != '\0' ) ;
578                                         temp[ j ] = '\0';
579
580                                         /* add this as a base path */
581                                         AddBasePath( temp );
582                                         break;
583                                 }
584                         }
585                 }
586
587                 /* add install path */
588                 if ( numBasePaths == 0 ) {
589                         AddBasePath( installPath );
590                 }
591
592                 /* check again */
593                 if ( numBasePaths == 0 ) {
594                         Error( "Failed to find a valid base path." );
595                 }
596         }
597
598         if ( noBasePath == 1 ) {
599                 numBasePaths = 0;
600         }
601
602         if ( noHomePath == 0 ) {
603                 /* this only affects unix */
604                 if ( homeBasePath ) {
605                         AddHomeBasePath( homeBasePath );
606                 }
607                 else{
608                         AddHomeBasePath( game->homeBasePath );
609                 }
610         }
611
612         /* initialize vfs paths */
613         if ( numBasePaths > MAX_BASE_PATHS ) {
614                 numBasePaths = MAX_BASE_PATHS;
615         }
616         if ( numGamePaths > MAX_GAME_PATHS ) {
617                 numGamePaths = MAX_GAME_PATHS;
618         }
619
620         /* walk the list of game paths */
621         for ( j = 0; j < numGamePaths; j++ )
622         {
623                 /* walk the list of base paths */
624                 for ( i = 0; i < numBasePaths; i++ )
625                 {
626                         /* create a full path and initialize it */
627                         sprintf( temp, "%s/%s/", basePaths[ i ], gamePaths[ j ] );
628                         vfsInitDirectory( temp );
629                 }
630         }
631
632         /* initialize vfs paths */
633         if ( numPakPaths > MAX_PAK_PATHS ) {
634                 numPakPaths = MAX_PAK_PATHS;
635         }
636
637         /* walk the list of pak paths */
638         for ( i = 0; i < numPakPaths; i++ )
639         {
640                 /* initialize this pak path */
641                 vfsInitDirectory( pakPaths[ i ] );
642         }
643
644         /* done */
645         Sys_Printf( "\n" );
646 }