]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/path_init.c
Merge commit '830125fad042fad35dc029b6eb57c8156ad7e176'
[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
45 char                    *homePath;
46 char installPath[ MAX_OS_PATH ];
47
48 int numBasePaths;
49 char                    *basePaths[ MAX_BASE_PATHS ];
50 int numGamePaths;
51 char                    *gamePaths[ MAX_GAME_PATHS ];
52 char                    *homeBasePath = NULL;
53
54
55 /*
56    some of this code is based off the original q3map port from loki
57    and finds various paths. moved here from bsp.c for clarity.
58  */
59
60 /*
61    PathLokiGetHomeDir()
62    gets the user's home dir (for ~/.q3a)
63  */
64
65 char *LokiGetHomeDir( void ){
66         #ifndef Q_UNIX
67         return NULL;
68         #else
69         char            *home;
70         uid_t id;
71         struct passwd   *pwd;
72         static char homeBuf[MAX_OS_PATH];
73
74
75         /* get the home environment variable */
76         home = getenv( "HOME" );
77         if ( home == NULL ) {
78                 /* do some more digging */
79                 id = getuid();
80                 setpwent();
81                 while ( ( pwd = getpwent() ) != NULL )
82                 {
83                         if ( pwd->pw_uid == id ) {
84                                 home = pwd->pw_dir;
85                                 break;
86                         }
87                 }
88                 endpwent();
89         }
90
91         snprintf( homeBuf, sizeof( homeBuf ), "%s/.", home );
92
93         /* return it */
94         return homeBuf;
95         #endif
96 }
97
98
99
100 /*
101    PathLokiInitPaths()
102    initializes some paths on linux/os x
103  */
104
105 void LokiInitPaths( char *argv0 ){
106         char        *home;
107
108         if ( !homePath ) {
109                 /* get home dir */
110                 home = LokiGetHomeDir();
111                 if ( home == NULL ) {
112                         home = ".";
113                 }
114
115                 /* set home path */
116                 homePath = home;
117         }
118         else{
119                 home = homePath;
120         }
121
122         #ifndef Q_UNIX
123         /* this is kinda crap, but hey */
124         strcpy( installPath, "../" );
125         #else
126         char temp[ MAX_OS_PATH ];
127         char last0[ 2 ];
128         char        *path;
129         char        *last;
130         qboolean found;
131
132
133         /* do some path divining */
134         strcpy( temp, argv0 );
135         if ( strrchr( argv0, '/' ) ) {
136                 argv0 = strrchr( argv0, '/' ) + 1;
137         }
138         else
139         {
140                 /* get path environment variable */
141                 path = getenv( "PATH" );
142
143                 /* minor setup */
144                 last = last0;
145                 last[ 0 ] = path[ 0 ];
146                 last[ 1 ] = '\0';
147                 found = qfalse;
148
149                 /* go through each : segment of path */
150                 while ( last[ 0 ] != '\0' && found == qfalse )
151                 {
152                         /* null out temp */
153                         temp[ 0 ] = '\0';
154
155                         /* find next chunk */
156                         last = strchr( path, ':' );
157                         if ( last == NULL ) {
158                                 last = path + strlen( path );
159                         }
160
161                         /* found home dir candidate */
162                         if ( *path == '~' ) {
163                                 strcpy( temp, home );
164                                 path++;
165                         }
166
167                         /* concatenate */
168                         if ( last > ( path + 1 ) ) {
169                                 strncat( temp, path, ( last - path ) );
170                                 strcat( temp, "/" );
171                         }
172                         strcat( temp, "./" );
173                         strcat( temp, argv0 );
174
175                         /* verify the path */
176                         if ( access( temp, X_OK ) == 0 ) {
177                                 found++;
178                         }
179                         path = last + 1;
180                 }
181         }
182
183         /* flake */
184         if ( realpath( temp, installPath ) ) {
185                 /* q3map is in "tools/" */
186                 *( strrchr( installPath, '/' ) ) = '\0';
187                 *( strrchr( installPath, '/' ) + 1 ) = '\0';
188         }
189         #endif
190 }
191
192
193
194 /*
195    CleanPath() - ydnar
196    cleans a dos path \ -> /
197  */
198
199 void CleanPath( char *path ){
200         while ( *path )
201         {
202                 if ( *path == '\\' ) {
203                         *path = '/';
204                 }
205                 path++;
206         }
207 }
208
209
210
211 /*
212    GetGame() - ydnar
213    gets the game_t based on a -game argument
214    returns NULL if no match found
215  */
216
217 game_t *GetGame( char *arg ){
218         int i;
219
220
221         /* dummy check */
222         if ( arg == NULL || arg[ 0 ] == '\0' ) {
223                 return NULL;
224         }
225
226         /* joke */
227         if ( !Q_stricmp( arg, "quake1" ) ||
228                  !Q_stricmp( arg, "quake2" ) ||
229                  !Q_stricmp( arg, "unreal" ) ||
230                  !Q_stricmp( arg, "ut2k3" ) ||
231                  !Q_stricmp( arg, "dn3d" ) ||
232                  !Q_stricmp( arg, "dnf" ) ||
233                  !Q_stricmp( arg, "hl" ) ) {
234                 Sys_Printf( "April fools, silly rabbit!\n" );
235                 exit( 0 );
236         }
237
238         /* test it */
239         i = 0;
240         while ( games[ i ].arg != NULL )
241         {
242                 if ( Q_stricmp( arg, games[ i ].arg ) == 0 ) {
243                         return &games[ i ];
244                 }
245                 i++;
246         }
247
248         /* no matching game */
249         return NULL;
250 }
251
252
253
254 /*
255    AddBasePath() - ydnar
256    adds a base path to the list
257  */
258
259 void AddBasePath( char *path ){
260         /* dummy check */
261         if ( path == NULL || path[ 0 ] == '\0' || numBasePaths >= MAX_BASE_PATHS ) {
262                 return;
263         }
264
265         /* add it to the list */
266         basePaths[ numBasePaths ] = safe_malloc( strlen( path ) + 1 );
267         strcpy( basePaths[ numBasePaths ], path );
268         CleanPath( basePaths[ numBasePaths ] );
269         numBasePaths++;
270 }
271
272
273
274 /*
275    AddHomeBasePath() - ydnar
276    adds a base path to the beginning of the list, prefixed by ~/
277  */
278
279 void AddHomeBasePath( char *path ){
280         int i;
281         char temp[ MAX_OS_PATH ];
282         int homePathLen;
283
284         if ( !homePath ) {
285                 return;
286         }
287
288         /* dummy check */
289         if ( path == NULL || path[ 0 ] == '\0' ) {
290                 return;
291         }
292
293         /* strip leading dot, if homePath does not end in /. */
294         homePathLen = strlen( homePath );
295         if ( !strcmp( path, "." ) ) {
296                 /* -fs_homebase . means that -fs_home is to be used as is */
297                 strcpy( temp, homePath );
298         }
299         else if ( homePathLen >= 2 && !strcmp( homePath + homePathLen - 2, "/." ) ) {
300                 /* remove trailing /. of homePath */
301                 homePathLen -= 2;
302
303                 /* concatenate home dir and path */
304                 sprintf( temp, "%.*s/%s", homePathLen, homePath, path );
305         }
306         else
307         {
308                 /* remove leading . of path */
309                 if ( path[0] == '.' ) {
310                         ++path;
311                 }
312
313                 /* concatenate home dir and path */
314                 sprintf( temp, "%s/%s", homePath, path );
315         }
316
317         /* make a hole */
318         for ( i = ( MAX_BASE_PATHS - 2 ); i >= 0; i-- )
319                 basePaths[ i + 1 ] = basePaths[ i ];
320
321         /* add it to the list */
322         basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 );
323         strcpy( basePaths[ 0 ], temp );
324         CleanPath( basePaths[ 0 ] );
325         numBasePaths++;
326 }
327
328
329
330 /*
331    AddGamePath() - ydnar
332    adds a game path to the list
333  */
334
335 void AddGamePath( char *path ){
336         int i;
337
338         /* dummy check */
339         if ( path == NULL || path[ 0 ] == '\0' || numGamePaths >= MAX_GAME_PATHS ) {
340                 return;
341         }
342
343         /* add it to the list */
344         gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 );
345         strcpy( gamePaths[ numGamePaths ], path );
346         CleanPath( gamePaths[ numGamePaths ] );
347         numGamePaths++;
348
349         /* don't add it if it's already there */
350         for ( i = 0; i < numGamePaths - 1; i++ )
351         {
352                 if ( strcmp( gamePaths[i], gamePaths[numGamePaths - 1] ) == 0 ) {
353                         free( gamePaths[numGamePaths - 1] );
354                         gamePaths[numGamePaths - 1] = NULL;
355                         numGamePaths--;
356                         break;
357                 }
358         }
359
360 }
361
362
363
364
365 /*
366    InitPaths() - ydnar
367    cleaned up some of the path initialization code from bsp.c
368    will remove any arguments it uses
369  */
370
371 void InitPaths( int *argc, char **argv ){
372         int i, j, k, len, len2;
373         char temp[ MAX_OS_PATH ];
374
375
376         /* note it */
377         Sys_FPrintf( SYS_VRB, "--- InitPaths ---\n" );
378
379         /* get the install path for backup */
380         LokiInitPaths( argv[ 0 ] );
381
382         /* set game to default (q3a) */
383         game = &games[ 0 ];
384         numBasePaths = 0;
385         numGamePaths = 0;
386
387         /* parse through the arguments and extract those relevant to paths */
388         for ( i = 0; i < *argc; i++ )
389         {
390                 /* check for null */
391                 if ( argv[ i ] == NULL ) {
392                         continue;
393                 }
394
395                 /* -game */
396                 if ( strcmp( argv[ i ], "-game" ) == 0 ) {
397                         if ( ++i >= *argc ) {
398                                 Error( "Out of arguments: No game specified after %s", argv[ i - 1 ] );
399                         }
400                         argv[ i - 1 ] = NULL;
401                         game = GetGame( argv[ i ] );
402                         if ( game == NULL ) {
403                                 game = &games[ 0 ];
404                         }
405                         argv[ i ] = NULL;
406                 }
407
408                 /* -fs_forbiddenpath */
409                 else if ( strcmp( argv[ i ], "-fs_forbiddenpath" ) == 0 ) {
410                         if ( ++i >= *argc ) {
411                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
412                         }
413                         argv[ i - 1 ] = NULL;
414                         if ( g_numForbiddenDirs < VFS_MAXDIRS ) {
415                                 strncpy( g_strForbiddenDirs[g_numForbiddenDirs], argv[i], PATH_MAX );
416                                 g_strForbiddenDirs[g_numForbiddenDirs][PATH_MAX] = 0;
417                                 ++g_numForbiddenDirs;
418                         }
419                         argv[ i ] = NULL;
420                 }
421
422                 /* -fs_basepath */
423                 else if ( strcmp( argv[ i ], "-fs_basepath" ) == 0 ) {
424                         if ( ++i >= *argc ) {
425                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
426                         }
427                         argv[ i - 1 ] = NULL;
428                         AddBasePath( argv[ i ] );
429                         argv[ i ] = NULL;
430                 }
431
432                 /* -fs_game */
433                 else if ( strcmp( argv[ i ], "-fs_game" ) == 0 ) {
434                         if ( ++i >= *argc ) {
435                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
436                         }
437                         argv[ i - 1 ] = NULL;
438                         AddGamePath( argv[ i ] );
439                         argv[ i ] = NULL;
440                 }
441
442                 /* -fs_home */
443                 else if ( strcmp( argv[ i ], "-fs_home" ) == 0 ) {
444                         if ( ++i >= *argc ) {
445                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
446                         }
447                         argv[ i - 1 ] = NULL;
448                         homePath = argv[i];
449                         argv[ i ] = NULL;
450                 }
451
452                 /* -fs_homebase */
453                 else if ( strcmp( argv[ i ], "-fs_homebase" ) == 0 ) {
454                         if ( ++i >= *argc ) {
455                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
456                         }
457                         argv[ i - 1 ] = NULL;
458                         homeBasePath = argv[i];
459                         argv[ i ] = NULL;
460                 }
461
462                 /* -fs_homepath - sets both of them */
463                 else if ( strcmp( argv[ i ], "-fs_homepath" ) == 0 ) {
464                         if ( ++i >= *argc ) {
465                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
466                         }
467                         argv[ i - 1 ] = NULL;
468                         homePath = argv[i];
469                         homeBasePath = ".";
470                         argv[ i ] = NULL;
471                 }
472         }
473
474         /* remove processed arguments */
475         for ( i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++ )
476         {
477                 for ( ; j < *argc && argv[ j ] == NULL; j++ ) ;
478                 argv[ i ] = argv[ j ];
479                 if ( argv[ i ] != NULL ) {
480                         k++;
481                 }
482         }
483         *argc = k;
484
485         /* add standard game path */
486         AddGamePath( game->gamePath );
487
488         /* if there is no base path set, figure it out */
489         if ( numBasePaths == 0 ) {
490                 /* this is another crappy replacement for SetQdirFromPath() */
491                 len2 = strlen( game->magic );
492                 for ( i = 0; i < *argc && numBasePaths == 0; i++ )
493                 {
494                         /* extract the arg */
495                         strcpy( temp, argv[ i ] );
496                         CleanPath( temp );
497                         len = strlen( temp );
498                         Sys_FPrintf( SYS_VRB, "Searching for \"%s\" in \"%s\" (%d)...\n", game->magic, temp, i );
499
500                         /* this is slow, but only done once */
501                         for ( j = 0; j < ( len - len2 ); j++ )
502                         {
503                                 /* check for the game's magic word */
504                                 if ( Q_strncasecmp( &temp[ j ], game->magic, len2 ) == 0 ) {
505                                         /* now find the next slash and nuke everything after it */
506                                         while ( temp[ ++j ] != '/' && temp[ j ] != '\0' ) ;
507                                         temp[ j ] = '\0';
508
509                                         /* add this as a base path */
510                                         AddBasePath( temp );
511                                         break;
512                                 }
513                         }
514                 }
515
516                 /* add install path */
517                 if ( numBasePaths == 0 ) {
518                         AddBasePath( installPath );
519                 }
520
521                 /* check again */
522                 if ( numBasePaths == 0 ) {
523                         Error( "Failed to find a valid base path." );
524                 }
525         }
526
527         /* this only affects unix */
528         if ( homeBasePath ) {
529                 AddHomeBasePath( homeBasePath );
530         }
531         else{
532                 AddHomeBasePath( game->homeBasePath );
533         }
534
535         /* initialize vfs paths */
536         if ( numBasePaths > MAX_BASE_PATHS ) {
537                 numBasePaths = MAX_BASE_PATHS;
538         }
539         if ( numGamePaths > MAX_GAME_PATHS ) {
540                 numGamePaths = MAX_GAME_PATHS;
541         }
542
543         /* walk the list of game paths */
544         for ( j = 0; j < numGamePaths; j++ )
545         {
546                 /* walk the list of base paths */
547                 for ( i = 0; i < numBasePaths; i++ )
548                 {
549                         /* create a full path and initialize it */
550                         sprintf( temp, "%s/%s/", basePaths[ i ], gamePaths[ j ] );
551                         vfsInitDirectory( temp );
552                 }
553         }
554
555         /* done */
556         Sys_Printf( "\n" );
557 }