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