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