1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
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.
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.
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
24 Nurail: Swiped from Q3Map2
32 #if defined( __linux__ ) || defined( __APPLE__ )
50 #define MAX_BASE_PATHS 10
51 #define MAX_GAME_PATHS 10
54 char installPath[ MAX_OS_PATH ];
57 char *basePaths[ MAX_BASE_PATHS ];
59 char *gamePaths[ MAX_GAME_PATHS ];
62 some of this code is based off the original q3map port from loki
63 and finds various paths. moved here from bsp.c for clarity.
68 gets the user's home dir (for ~/.q3a)
71 char *LokiGetHomeDir( void )
81 /* get the home environment variable */
82 home = getenv( "HOME" );
85 /* do some more digging */
88 while( (pwd = getpwent()) != NULL )
90 if( pwd->pw_uid == id )
108 initializes some paths on linux/os x
111 void LokiInitPaths( char *argv0 )
114 /* this is kinda crap, but hey */
115 strcpy( installPath, "../" );
117 char temp[ MAX_OS_PATH ];
125 home = LokiGetHomeDir();
129 /* do some path divining */
130 strcpy( temp, argv0 );
131 if( strrchr( temp, '/' ) )
132 argv0 = strrchr( argv0, '/' ) + 1;
135 /* get path environment variable */
136 path = getenv( "PATH" );
139 last[ 0 ] = path[ 0 ];
143 /* go through each : segment of path */
144 while( last[ 0 ] != '\0' && found == false )
149 /* find next chunk */
150 last = strchr( path, ':' );
152 last = path + strlen( path );
154 /* found home dir candidate */
157 strcpy( temp, home );
162 if( last > (path + 1) )
164 strncat( temp, path, (last - path) );
167 strcat( temp, "./" );
168 strcat( temp, argv0 );
170 /* verify the path */
171 if( access( temp, X_OK ) == 0 )
178 if( realpath( temp, installPath ) )
180 /* q3map is in "tools/" */
181 *(strrchr( installPath, '/' )) = '\0';
182 *(strrchr( installPath, '/' ) + 1) = '\0';
194 cleans a dos path \ -> /
197 void CleanPath( char *path )
208 AddBasePath() - ydnar
209 adds a base path to the list
212 void AddBasePath( char *path )
215 if( path == NULL || path[ 0 ] == '\0' || numBasePaths >= MAX_BASE_PATHS )
218 /* add it to the list */
219 basePaths[ numBasePaths ] = safe_malloc( strlen( path ) + 1 );
220 strcpy( basePaths[ numBasePaths ], path );
221 CleanPath( basePaths[ numBasePaths ] );
228 AddHomeBasePath() - ydnar
229 adds a base path to the beginning of the list, prefixed by ~/
232 void AddHomeBasePath( char *path )
236 char temp[ MAX_OS_PATH ];
240 if( path == NULL || path[ 0 ] == '\0' )
244 for( i = 0; i < (MAX_BASE_PATHS - 1); i++ )
245 basePaths[ i + 1 ] = basePaths[ i ];
247 /* concatenate home dir and path */
248 sprintf( temp, "%s/%s", homePath, path );
250 /* add it to the list */
251 basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 );
252 strcpy( basePaths[ 0 ], temp );
253 CleanPath( basePaths[ 0 ] );
261 AddGamePath() - ydnar
262 adds a game path to the list
265 void AddGamePath( char *path )
268 if( path == NULL || path[ 0 ] == '\0' || numGamePaths >= MAX_GAME_PATHS )
271 /* add it to the list */
272 gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 );
273 strcpy( gamePaths[ numGamePaths ], path );
274 CleanPath( gamePaths[ numGamePaths ] );
283 cleaned up some of the path initialization code from bsp.c
284 will remove any arguments it uses
287 void InitPaths( int *argc, char **argv )
289 int i, j, k, len, len2;
290 char temp[ MAX_OS_PATH ];
291 char gamePath[MAX_OS_PATH], homeBasePath[MAX_OS_PATH], game_magic[10];
293 strcpy(gamePath, "base");
294 strcpy(game_magic, "h");
295 strcpy(homeBasePath, ".heretic2");
298 Sys_FPrintf( SYS_VRB, "--- InitPaths ---\n" );
300 /* get the install path for backup */
301 LokiInitPaths( argv[ 0 ] );
303 /* set game to default (q3a) */
307 /* parse through the arguments and extract those relevant to paths */
308 for( i = 0; i < *argc; i++ )
311 if( argv[ i ] == NULL )
315 if( strcmp( argv[ i ], "-fs_basepath" ) == 0 )
318 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
319 argv[ i - 1 ] = NULL;
320 AddBasePath( argv[ i ] );
326 /* remove processed arguments */
327 for( i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++ )
329 for( j; j < *argc && argv[ j ] == NULL; j++ );
330 argv[ i ] = argv[ j ];
331 if( argv[ i ] != NULL )
336 /* add standard game path */
337 AddGamePath( gamePath );
339 /* if there is no base path set, figure it out */
340 if( numBasePaths == 0 )
342 /* this is another crappy replacement for SetQdirFromPath() */
343 len2 = strlen( game_magic );
344 for( i = 0; i < *argc && numBasePaths == 0; i++ )
346 /* extract the arg */
347 strcpy( temp, argv[ i ] );
349 len = strlen( temp );
350 Sys_FPrintf( SYS_VRB, "Searching for \"%s\" in \"%s\" (%d)...\n", game_magic, temp, i );
352 /* this is slow, but only done once */
353 for( j = 0; j < (len - len2); j++ )
355 /* check for the game's magic word */
356 if( Q_strncasecmp( &temp[ j ], game_magic, len2 ) == 0 )
358 /* now find the next slash and nuke everything after it */
359 while( temp[ ++j ] != '/' && temp[ j ] != '\0' );
362 /* add this as a base path */
369 /* add install path */
370 if( numBasePaths == 0 )
371 AddBasePath( installPath );
374 if( numBasePaths == 0 )
375 Error( "Failed to find a valid base path." );
378 /* this only affects unix */
379 AddHomeBasePath( homeBasePath );
381 /* initialize vfs paths */
382 if( numBasePaths > MAX_BASE_PATHS )
383 numBasePaths = MAX_BASE_PATHS;
384 if( numGamePaths > MAX_GAME_PATHS )
385 numGamePaths = MAX_GAME_PATHS;
387 /* walk the list of game paths */
388 //for( j = 0; j < numGamePaths; j++ )
390 /* walk the list of base paths */
391 // for( i = 0; i < numBasePaths; i++ )
393 /* create a full path and initialize it */
394 // sprintf( temp, "%s/%s/", basePaths[ i ], gamePaths[ j ] );
395 // vfsInitDirectory( temp );