1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 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 ){
80 /* get the home environment variable */
81 home = getenv( "HOME" );
83 /* do some more digging */
86 while ( ( pwd = getpwent() ) != NULL )
88 if ( pwd->pw_uid == id ) {
105 initializes some paths on linux/os x
108 void LokiInitPaths( char *argv0 ){
110 /* this is kinda crap, but hey */
111 strcpy( installPath, "../" );
113 char temp[ MAX_OS_PATH ];
121 home = LokiGetHomeDir();
122 if ( home == NULL ) {
126 /* do some path divining */
127 strcpy( temp, argv0 );
128 if ( strrchr( temp, '/' ) ) {
129 argv0 = strrchr( argv0, '/' ) + 1;
133 /* get path environment variable */
134 path = getenv( "PATH" );
137 last[ 0 ] = path[ 0 ];
141 /* go through each : segment of path */
142 while ( last[ 0 ] != '\0' && found == false )
147 /* find next chunk */
148 last = strchr( path, ':' );
149 if ( last == NULL ) {
150 last = path + strlen( path );
153 /* found home dir candidate */
154 if ( *path == '~' ) {
155 strcpy( temp, home );
160 if ( last > ( path + 1 ) ) {
161 strncat( temp, path, ( last - path ) );
164 strcat( temp, "./" );
165 strcat( temp, argv0 );
167 /* verify the path */
168 if ( access( temp, X_OK ) == 0 ) {
176 if ( realpath( temp, installPath ) ) {
177 /* q3map is in "tools/" */
178 *( strrchr( installPath, '/' ) ) = '\0';
179 *( strrchr( installPath, '/' ) + 1 ) = '\0';
191 cleans a dos path \ -> /
194 void CleanPath( char *path ){
197 if ( *path == '\\' ) {
205 AddBasePath() - ydnar
206 adds a base path to the list
209 void AddBasePath( char *path ){
211 if ( path == NULL || path[ 0 ] == '\0' || numBasePaths >= MAX_BASE_PATHS ) {
215 /* add it to the list */
216 basePaths[ numBasePaths ] = safe_malloc( strlen( path ) + 1 );
217 strcpy( basePaths[ numBasePaths ], path );
218 CleanPath( basePaths[ numBasePaths ] );
225 AddHomeBasePath() - ydnar
226 adds a base path to the beginning of the list, prefixed by ~/
229 void AddHomeBasePath( char *path ){
232 char temp[ MAX_OS_PATH ];
236 if ( path == NULL || path[ 0 ] == '\0' ) {
241 for ( i = 0; i < ( MAX_BASE_PATHS - 1 ); i++ )
242 basePaths[ i + 1 ] = basePaths[ i ];
244 /* concatenate home dir and path */
245 sprintf( temp, "%s/%s", homePath, path );
247 /* add it to the list */
248 basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 );
249 strcpy( basePaths[ 0 ], temp );
250 CleanPath( basePaths[ 0 ] );
258 AddGamePath() - ydnar
259 adds a game path to the list
262 void AddGamePath( char *path ){
264 if ( path == NULL || path[ 0 ] == '\0' || numGamePaths >= MAX_GAME_PATHS ) {
268 /* add it to the list */
269 gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 );
270 strcpy( gamePaths[ numGamePaths ], path );
271 CleanPath( gamePaths[ numGamePaths ] );
280 cleaned up some of the path initialization code from bsp.c
281 will remove any arguments it uses
284 void InitPaths( int *argc, char **argv ){
285 int i, j, k, len, len2;
286 char temp[ MAX_OS_PATH ];
287 char gamePath[MAX_OS_PATH], homeBasePath[MAX_OS_PATH], game_magic[10];
289 strcpy( gamePath, "base" );
290 strcpy( game_magic, "h" );
291 strcpy( homeBasePath, ".heretic2" );
294 Sys_FPrintf( SYS_VRB, "--- InitPaths ---\n" );
296 /* get the install path for backup */
297 LokiInitPaths( argv[ 0 ] );
299 /* set game to default (q3a) */
303 /* parse through the arguments and extract those relevant to paths */
304 for ( i = 0; i < *argc; i++ )
307 if ( argv[ i ] == NULL ) {
312 if ( strcmp( argv[ i ], "-fs_basepath" ) == 0 ) {
313 if ( ++i >= *argc ) {
314 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
316 argv[ i - 1 ] = NULL;
317 AddBasePath( argv[ i ] );
323 /* remove processed arguments */
324 for ( i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++ )
326 for ( j; j < *argc && argv[ j ] == NULL; j++ ) ;
327 argv[ i ] = argv[ j ];
328 if ( argv[ i ] != NULL ) {
334 /* add standard game path */
335 AddGamePath( gamePath );
337 /* if there is no base path set, figure it out */
338 if ( numBasePaths == 0 ) {
339 /* this is another crappy replacement for SetQdirFromPath() */
340 len2 = strlen( game_magic );
341 for ( i = 0; i < *argc && numBasePaths == 0; i++ )
343 /* extract the arg */
344 strcpy( temp, argv[ i ] );
346 len = strlen( temp );
347 Sys_FPrintf( SYS_VRB, "Searching for \"%s\" in \"%s\" (%d)...\n", game_magic, temp, i );
349 /* this is slow, but only done once */
350 for ( j = 0; j < ( len - len2 ); j++ )
352 /* check for the game's magic word */
353 if ( Q_strncasecmp( &temp[ j ], game_magic, len2 ) == 0 ) {
354 /* now find the next slash and nuke everything after it */
355 while ( temp[ ++j ] != '/' && temp[ j ] != '\0' ) ;
358 /* add this as a base path */
365 /* add install path */
366 if ( numBasePaths == 0 ) {
367 AddBasePath( installPath );
371 if ( numBasePaths == 0 ) {
372 Error( "Failed to find a valid base path." );
376 /* this only affects unix */
377 AddHomeBasePath( homeBasePath );
379 /* initialize vfs paths */
380 if ( numBasePaths > MAX_BASE_PATHS ) {
381 numBasePaths = MAX_BASE_PATHS;
383 if ( numGamePaths > MAX_GAME_PATHS ) {
384 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 );