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
22 Nurail: Swiped from Q3Map2
31 #if defined( __linux__ ) || defined( __APPLE__ )
49 #define MAX_BASE_PATHS 10
50 #define MAX_GAME_PATHS 10
53 char installPath[ MAX_OS_PATH ];
56 char *basePaths[ MAX_BASE_PATHS ];
58 char *gamePaths[ MAX_GAME_PATHS ];
61 some of this code is based off the original q3map port from loki
62 and finds various paths. moved here from bsp.c for clarity.
67 gets the user's home dir (for ~/.q3a)
70 char *LokiGetHomeDir( void )
80 /* get the home environment variable */
81 home = getenv( "HOME" );
84 /* do some more digging */
87 while( (pwd = getpwent()) != NULL )
89 if( pwd->pw_uid == id )
107 initializes some paths on linux/os x
110 void LokiInitPaths( char *argv0 )
113 /* this is kinda crap, but hey */
114 strcpy( installPath, "../" );
116 char temp[ MAX_OS_PATH ];
124 home = LokiGetHomeDir();
128 /* do some path divining */
129 strcpy( temp, argv0 );
130 if( strrchr( temp, '/' ) )
131 argv0 = strrchr( argv0, '/' ) + 1;
134 /* get path environment variable */
135 path = getenv( "PATH" );
138 last[ 0 ] = path[ 0 ];
142 /* go through each : segment of path */
143 while( last[ 0 ] != '\0' && found == false )
148 /* find next chunk */
149 last = strchr( path, ':' );
151 last = path + strlen( path );
153 /* found home dir candidate */
156 strcpy( temp, home );
161 if( last > (path + 1) )
163 strncat( temp, path, (last - path) );
166 strcat( temp, "./" );
167 strcat( temp, argv0 );
169 /* verify the path */
170 if( access( temp, X_OK ) == 0 )
177 if( realpath( temp, installPath ) )
179 /* q3map is in "tools/" */
180 *(strrchr( installPath, '/' )) = '\0';
181 *(strrchr( installPath, '/' ) + 1) = '\0';
193 cleans a dos path \ -> /
196 void CleanPath( char *path )
207 AddBasePath() - ydnar
208 adds a base path to the list
211 void AddBasePath( char *path )
214 if( path == NULL || path[ 0 ] == '\0' || numBasePaths >= MAX_BASE_PATHS )
217 /* add it to the list */
218 basePaths[ numBasePaths ] = safe_malloc( strlen( path ) + 1 );
219 strcpy( basePaths[ numBasePaths ], path );
220 CleanPath( basePaths[ numBasePaths ] );
227 AddHomeBasePath() - ydnar
228 adds a base path to the beginning of the list, prefixed by ~/
231 void AddHomeBasePath( char *path )
235 char temp[ MAX_OS_PATH ];
239 if( path == NULL || path[ 0 ] == '\0' )
243 for( i = 0; i < (MAX_BASE_PATHS - 1); i++ )
244 basePaths[ i + 1 ] = basePaths[ i ];
246 /* concatenate home dir and path */
247 sprintf( temp, "%s/%s", homePath, path );
249 /* add it to the list */
250 basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 );
251 strcpy( basePaths[ 0 ], temp );
252 CleanPath( basePaths[ 0 ] );
260 AddGamePath() - ydnar
261 adds a game path to the list
264 void AddGamePath( char *path )
267 if( path == NULL || path[ 0 ] == '\0' || numGamePaths >= MAX_GAME_PATHS )
270 /* add it to the list */
271 gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 );
272 strcpy( gamePaths[ numGamePaths ], path );
273 CleanPath( gamePaths[ numGamePaths ] );
282 cleaned up some of the path initialization code from bsp.c
283 will remove any arguments it uses
286 void InitPaths( int *argc, char **argv )
288 int i, j, k, len, len2;
289 char temp[ MAX_OS_PATH ];
290 char gamePath[MAX_OS_PATH], homeBasePath[MAX_OS_PATH], game_magic[10];
292 strcpy(gamePath, "baseq2");
293 strcpy(game_magic, "quake");
294 strcpy(homeBasePath, ".quake2");
297 Sys_FPrintf( SYS_VRB, "--- InitPaths ---\n" );
299 /* get the install path for backup */
300 LokiInitPaths( argv[ 0 ] );
302 /* set game to default (q3a) */
306 /* parse through the arguments and extract those relevant to paths */
307 for( i = 0; i < *argc; i++ )
310 if( argv[ i ] == NULL )
314 if( strcmp( argv[ i ], "-fs_basepath" ) == 0 )
317 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
318 argv[ i - 1 ] = NULL;
319 AddBasePath( argv[ i ] );
325 /* remove processed arguments */
326 for( i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++ )
328 for( j; j < *argc && argv[ j ] == NULL; j++ );
329 argv[ i ] = argv[ j ];
330 if( argv[ i ] != NULL )
335 /* add standard game path */
336 AddGamePath( gamePath );
338 /* if there is no base path set, figure it out */
339 if( numBasePaths == 0 )
341 /* this is another crappy replacement for SetQdirFromPath() */
342 len2 = strlen( game_magic );
343 for( i = 0; i < *argc && numBasePaths == 0; i++ )
345 /* extract the arg */
346 strcpy( temp, argv[ i ] );
348 len = strlen( temp );
349 Sys_FPrintf( SYS_VRB, "Searching for \"%s\" in \"%s\" (%d)...\n", game_magic, temp, i );
351 /* this is slow, but only done once */
352 for( j = 0; j < (len - len2); j++ )
354 /* check for the game's magic word */
355 if( Q_strncasecmp( &temp[ j ], game_magic, len2 ) == 0 )
357 /* now find the next slash and nuke everything after it */
358 while( temp[ ++j ] != '/' && temp[ j ] != '\0' );
361 /* add this as a base path */
368 /* add install path */
369 if( numBasePaths == 0 )
370 AddBasePath( installPath );
373 if( numBasePaths == 0 )
374 Error( "Failed to find a valid base path." );
377 /* this only affects unix */
378 AddHomeBasePath( homeBasePath );
380 /* initialize vfs paths */
381 if( numBasePaths > MAX_BASE_PATHS )
382 numBasePaths = MAX_BASE_PATHS;
383 if( numGamePaths > MAX_GAME_PATHS )
384 numGamePaths = MAX_GAME_PATHS;
386 /* walk the list of game paths */
387 //for( j = 0; j < numGamePaths; j++ )
389 /* walk the list of base paths */
390 // for( i = 0; i < numBasePaths; i++ )
392 /* create a full path and initialize it */
393 // sprintf( temp, "%s/%s/", basePaths[ i ], gamePaths[ j ] );
394 // vfsInitDirectory( temp );