]> git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/path_init.c
16758c6db40fecf7de6df655aadc03b8f8f47e4b
[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 last0[ 2 ];
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                 found = qfalse;
140                 last = path;
141
142                 /* go through each : segment of path */
143                 while ( last[ 0 ] != '\0' && found == qfalse )
144                 {
145                         /* null out temp */
146                         temp[ 0 ] = '\0';
147
148                         /* find next chunk */
149                         last = strchr( path, ':' );
150                         if ( last == NULL ) {
151                                 last = path + strlen( path );
152                         }
153
154                         /* found home dir candidate */
155                         if ( *path == '~' ) {
156                                 Q_strncpyz( temp, home, sizeof( temp ) );
157                                 path++;
158                         }
159
160                         /* concatenate */
161                         if ( last > ( path + 1 ) ) {
162                                 Q_strncat( temp, sizeof( temp ), path, ( last - path ) );
163                                 Q_strcat( temp, sizeof( temp ), "/" );
164                         }
165                         Q_strcat( temp, sizeof( temp ), "./" );
166                         Q_strcat( temp, sizeof( temp ), argv0 );
167
168                         /* verify the path */
169                         if ( access( temp, X_OK ) == 0 ) {
170                                 found++;
171                         }
172                         path = last + 1;
173                 }
174         }
175
176         /* flake */
177         if ( realpath( temp, installPath ) ) {
178                 /* q3map is in "tools/" */
179                 *( strrchr( installPath, '/' ) ) = '\0';
180                 *( strrchr( installPath, '/' ) + 1 ) = '\0';
181         }
182         #endif
183 }
184
185
186
187 /*
188    CleanPath() - ydnar
189    cleans a dos path \ -> /
190  */
191
192 void CleanPath( char *path ){
193         while ( *path )
194         {
195                 if ( *path == '\\' ) {
196                         *path = '/';
197                 }
198                 path++;
199         }
200 }
201
202
203
204 /*
205    GetGame() - ydnar
206    gets the game_t based on a -game argument
207    returns NULL if no match found
208  */
209
210 game_t *GetGame( char *arg ){
211         int i;
212
213
214         /* dummy check */
215         if ( arg == NULL || arg[ 0 ] == '\0' ) {
216                 return NULL;
217         }
218
219         /* joke */
220         if ( !Q_stricmp( arg, "quake1" ) ||
221                  !Q_stricmp( arg, "quake2" ) ||
222                  !Q_stricmp( arg, "unreal" ) ||
223                  !Q_stricmp( arg, "ut2k3" ) ||
224                  !Q_stricmp( arg, "dn3d" ) ||
225                  !Q_stricmp( arg, "dnf" ) ||
226                  !Q_stricmp( arg, "hl" ) ) {
227                 Sys_Printf( "April fools, silly rabbit!\n" );
228                 exit( 0 );
229         }
230
231         /* test it */
232         i = 0;
233         while ( games[ i ].arg != NULL )
234         {
235                 if ( Q_stricmp( arg, games[ i ].arg ) == 0 ) {
236                         return &games[ i ];
237                 }
238                 i++;
239         }
240
241         /* no matching game */
242         return NULL;
243 }
244
245
246
247 /*
248    AddBasePath() - ydnar
249    adds a base path to the list
250  */
251
252 void AddBasePath( char *path ){
253         /* dummy check */
254         if ( path == NULL || path[ 0 ] == '\0' || numBasePaths >= MAX_BASE_PATHS ) {
255                 return;
256         }
257
258         /* add it to the list */
259         basePaths[ numBasePaths ] = safe_malloc( strlen( path ) + 1 );
260         strcpy( basePaths[ numBasePaths ], path );
261         CleanPath( basePaths[ numBasePaths ] );
262         numBasePaths++;
263 }
264
265
266
267 /*
268    AddHomeBasePath() - ydnar
269    adds a base path to the beginning of the list, prefixed by ~/
270  */
271
272 void AddHomeBasePath( char *path ){
273         int i;
274         char temp[ MAX_OS_PATH ];
275         int homePathLen;
276
277         if ( !homePath ) {
278                 return;
279         }
280
281         /* dummy check */
282         if ( path == NULL || path[ 0 ] == '\0' ) {
283                 return;
284         }
285
286         /* strip leading dot, if homePath does not end in /. */
287         homePathLen = strlen( homePath );
288         if ( !strcmp( path, "." ) ) {
289                 /* -fs_homebase . means that -fs_home is to be used as is */
290                 strcpy( temp, homePath );
291         }
292         else if ( homePathLen >= 2 && !strcmp( homePath + homePathLen - 2, "/." ) ) {
293                 /* remove trailing /. of homePath */
294                 homePathLen -= 2;
295
296                 /* concatenate home dir and path */
297                 sprintf( temp, "%.*s/%s", homePathLen, homePath, path );
298         }
299         else
300         {
301                 /* remove leading . of path */
302                 if ( path[0] == '.' ) {
303                         ++path;
304                 }
305
306                 /* concatenate home dir and path */
307                 sprintf( temp, "%s/%s", homePath, path );
308         }
309
310         /* make a hole */
311         for ( i = ( MAX_BASE_PATHS - 2 ); i >= 0; i-- )
312                 basePaths[ i + 1 ] = basePaths[ i ];
313
314         /* add it to the list */
315         basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 );
316         strcpy( basePaths[ 0 ], temp );
317         CleanPath( basePaths[ 0 ] );
318         numBasePaths++;
319 }
320
321
322
323 /*
324    AddGamePath() - ydnar
325    adds a game path to the list
326  */
327
328 void AddGamePath( char *path ){
329         int i;
330
331         /* dummy check */
332         if ( path == NULL || path[ 0 ] == '\0' || numGamePaths >= MAX_GAME_PATHS ) {
333                 return;
334         }
335
336         /* add it to the list */
337         gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 );
338         strcpy( gamePaths[ numGamePaths ], path );
339         CleanPath( gamePaths[ numGamePaths ] );
340         numGamePaths++;
341
342         /* don't add it if it's already there */
343         for ( i = 0; i < numGamePaths - 1; i++ )
344         {
345                 if ( strcmp( gamePaths[i], gamePaths[numGamePaths - 1] ) == 0 ) {
346                         free( gamePaths[numGamePaths - 1] );
347                         gamePaths[numGamePaths - 1] = NULL;
348                         numGamePaths--;
349                         break;
350                 }
351         }
352
353 }
354
355
356 /*
357    AddPakPath()
358    adds a pak path to the list
359  */
360
361 void AddPakPath( char *path ){
362         /* dummy check */
363         if ( path == NULL || path[ 0 ] == '\0' || numPakPaths >= MAX_PAK_PATHS ) {
364                 return;
365         }
366
367         /* add it to the list */
368         pakPaths[ numPakPaths ] = safe_malloc( strlen( path ) + 1 );
369         strcpy( pakPaths[ numPakPaths ], path );
370         CleanPath( pakPaths[ numPakPaths ] );
371         numPakPaths++;
372 }
373
374
375
376 /*
377    InitPaths() - ydnar
378    cleaned up some of the path initialization code from bsp.c
379    will remove any arguments it uses
380  */
381
382 void InitPaths( int *argc, char **argv ){
383         int i, j, k, len, len2;
384         char temp[ MAX_OS_PATH ];
385
386
387         /* note it */
388         Sys_FPrintf( SYS_VRB, "--- InitPaths ---\n" );
389
390         /* get the install path for backup */
391         LokiInitPaths( argv[ 0 ] );
392
393         /* set game to default (q3a) */
394         game = &games[ 0 ];
395         numBasePaths = 0;
396         numGamePaths = 0;
397
398         /* parse through the arguments and extract those relevant to paths */
399         for ( i = 0; i < *argc; i++ )
400         {
401                 /* check for null */
402                 if ( argv[ i ] == NULL ) {
403                         continue;
404                 }
405
406                 /* -game */
407                 if ( strcmp( argv[ i ], "-game" ) == 0 ) {
408                         if ( ++i >= *argc ) {
409                                 Error( "Out of arguments: No game specified after %s", argv[ i - 1 ] );
410                         }
411                         argv[ i - 1 ] = NULL;
412                         game = GetGame( argv[ i ] );
413                         if ( game == NULL ) {
414                                 game = &games[ 0 ];
415                         }
416                         argv[ i ] = NULL;
417                 }
418
419                 /* -fs_forbiddenpath */
420                 else if ( strcmp( argv[ i ], "-fs_forbiddenpath" ) == 0 ) {
421                         if ( ++i >= *argc ) {
422                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
423                         }
424                         argv[ i - 1 ] = NULL;
425                         if ( g_numForbiddenDirs < VFS_MAXDIRS ) {
426                                 strncpy( g_strForbiddenDirs[g_numForbiddenDirs], argv[i], PATH_MAX );
427                                 g_strForbiddenDirs[g_numForbiddenDirs][PATH_MAX] = 0;
428                                 ++g_numForbiddenDirs;
429                         }
430                         argv[ i ] = NULL;
431                 }
432
433                 /* -fs_basepath */
434                 else if ( strcmp( argv[ i ], "-fs_basepath" ) == 0 ) {
435                         if ( ++i >= *argc ) {
436                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
437                         }
438                         argv[ i - 1 ] = NULL;
439                         AddBasePath( argv[ i ] );
440                         argv[ i ] = NULL;
441                 }
442
443                 /* -fs_game */
444                 else if ( strcmp( argv[ i ], "-fs_game" ) == 0 ) {
445                         if ( ++i >= *argc ) {
446                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
447                         }
448                         argv[ i - 1 ] = NULL;
449                         AddGamePath( argv[ i ] );
450                         argv[ i ] = NULL;
451                 }
452
453                 /* -fs_home */
454                 else if ( strcmp( argv[ i ], "-fs_home" ) == 0 ) {
455                         if ( ++i >= *argc ) {
456                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
457                         }
458                         argv[ i - 1 ] = NULL;
459                         homePath = argv[i];
460                         argv[ i ] = NULL;
461                 }
462
463                 /* -fs_homebase */
464                 else if ( strcmp( argv[ i ], "-fs_homebase" ) == 0 ) {
465                         if ( ++i >= *argc ) {
466                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
467                         }
468                         argv[ i - 1 ] = NULL;
469                         homeBasePath = argv[i];
470                         argv[ i ] = NULL;
471                 }
472
473                 /* -fs_homepath - sets both of them */
474                 else if ( strcmp( argv[ i ], "-fs_homepath" ) == 0 ) {
475                         if ( ++i >= *argc ) {
476                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
477                         }
478                         argv[ i - 1 ] = NULL;
479                         homePath = argv[i];
480                         homeBasePath = ".";
481                         argv[ i ] = NULL;
482                 }
483
484                 /* -fs_pakpath */
485                 else if ( strcmp( argv[ i ], "-fs_pakpath" ) == 0 ) {
486                         if ( ++i >= *argc ) {
487                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
488                         }
489                         argv[ i - 1 ] = NULL;
490                         AddPakPath( argv[ i ] );
491                         argv[ i ] = NULL;
492                 }
493
494         }
495
496         /* remove processed arguments */
497         for ( i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++ )
498         {
499                 for ( ; j < *argc && argv[ j ] == NULL; j++ ) ;
500                 argv[ i ] = argv[ j ];
501                 if ( argv[ i ] != NULL ) {
502                         k++;
503                 }
504         }
505         *argc = k;
506
507         /* add standard game path */
508         AddGamePath( game->gamePath );
509
510         /* if there is no base path set, figure it out */
511         if ( numBasePaths == 0 ) {
512                 /* this is another crappy replacement for SetQdirFromPath() */
513                 len2 = strlen( game->magic );
514                 for ( i = 0; i < *argc && numBasePaths == 0; i++ )
515                 {
516                         /* extract the arg */
517                         strcpy( temp, argv[ i ] );
518                         CleanPath( temp );
519                         len = strlen( temp );
520                         Sys_FPrintf( SYS_VRB, "Searching for \"%s\" in \"%s\" (%d)...\n", game->magic, temp, i );
521
522                         /* this is slow, but only done once */
523                         for ( j = 0; j < ( len - len2 ); j++ )
524                         {
525                                 /* check for the game's magic word */
526                                 if ( Q_strncasecmp( &temp[ j ], game->magic, len2 ) == 0 ) {
527                                         /* now find the next slash and nuke everything after it */
528                                         while ( temp[ ++j ] != '/' && temp[ j ] != '\0' ) ;
529                                         temp[ j ] = '\0';
530
531                                         /* add this as a base path */
532                                         AddBasePath( temp );
533                                         break;
534                                 }
535                         }
536                 }
537
538                 /* add install path */
539                 if ( numBasePaths == 0 ) {
540                         AddBasePath( installPath );
541                 }
542
543                 /* check again */
544                 if ( numBasePaths == 0 ) {
545                         Error( "Failed to find a valid base path." );
546                 }
547         }
548
549         /* this only affects unix */
550         if ( homeBasePath ) {
551                 AddHomeBasePath( homeBasePath );
552         }
553         else{
554                 AddHomeBasePath( game->homeBasePath );
555         }
556
557         /* initialize vfs paths */
558         if ( numBasePaths > MAX_BASE_PATHS ) {
559                 numBasePaths = MAX_BASE_PATHS;
560         }
561         if ( numGamePaths > MAX_GAME_PATHS ) {
562                 numGamePaths = MAX_GAME_PATHS;
563         }
564
565         /* walk the list of game paths */
566         for ( j = 0; j < numGamePaths; j++ )
567         {
568                 /* walk the list of base paths */
569                 for ( i = 0; i < numBasePaths; i++ )
570                 {
571                         /* create a full path and initialize it */
572                         sprintf( temp, "%s/%s/", basePaths[ i ], gamePaths[ j ] );
573                         vfsInitDirectory( temp );
574                 }
575         }
576
577         /* initialize vfs paths */
578         if ( numPakPaths > MAX_PAK_PATHS ) {
579                 numPakPaths = MAX_PAK_PATHS;
580         }
581
582         /* walk the list of pak paths */
583         for ( i = 0; i < numPakPaths; i++ )
584         {
585                 /* initialize this pak path */
586                 vfsInitDirectory( pakPaths[ i ] );
587         }
588
589         /* done */
590         Sys_Printf( "\n" );
591 }