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
27 #include "globaldefs.h"
33 #endif // !GDEF_OS_WINDOWS
40 #define MAX_BASE_PATHS 10
41 #define MAX_GAME_PATHS 10
44 char installPath[ MAX_OS_PATH ];
47 char *basePaths[ MAX_BASE_PATHS ];
49 char *gamePaths[ MAX_GAME_PATHS ];
52 some of this code is based off the original q3map port from loki
53 and finds various paths. moved here from bsp.c for clarity.
58 gets the user's home dir (for ~/.q3a)
61 char *LokiGetHomeDir( void ){
64 #else // !GDEF_OS_WINDOWS
70 /* get the home environment variable */
71 home = getenv( "HOME" );
73 /* do some more digging */
76 while ( ( pwd = getpwent() ) != NULL )
78 if ( pwd->pw_uid == id ) {
88 #endif // !GDEF_OS_WINDOWS
95 initializes some paths on linux/os x
98 void LokiInitPaths( char *argv0 ){
100 /* this is kinda crap, but hey */
101 strcpy( installPath, "../" );
102 #else // !GDEF_OS_WINDOWS
103 char temp[ MAX_OS_PATH ];
111 home = LokiGetHomeDir();
112 if ( home == NULL ) {
116 /* do some path divining */
117 strcpy( temp, argv0 );
118 if ( strrchr( temp, '/' ) ) {
119 argv0 = strrchr( argv0, '/' ) + 1;
123 /* get path environment variable */
124 path = getenv( "PATH" );
127 last[ 0 ] = path[ 0 ];
131 /* go through each : segment of path */
132 while ( last[ 0 ] != '\0' && found == false )
137 /* find next chunk */
138 last = strchr( path, ':' );
139 if ( last == NULL ) {
140 last = path + strlen( path );
143 /* found home dir candidate */
144 if ( *path == '~' ) {
145 strcpy( temp, home );
150 if ( last > ( path + 1 ) ) {
151 strncat( temp, path, ( last - path ) );
154 strcat( temp, "./" );
155 strcat( temp, argv0 );
157 /* verify the path */
158 if ( access( temp, X_OK ) == 0 ) {
166 if ( realpath( temp, installPath ) ) {
167 /* q3map is in "tools/" */
168 *( strrchr( installPath, '/' ) ) = '\0';
169 *( strrchr( installPath, '/' ) + 1 ) = '\0';
174 #endif // !GDEF_OS_WINDOWS
181 cleans a dos path \ -> /
184 void CleanPath( char *path ){
187 if ( *path == '\\' ) {
195 AddBasePath() - ydnar
196 adds a base path to the list
199 void AddBasePath( char *path ){
201 if ( path == NULL || path[ 0 ] == '\0' || numBasePaths >= MAX_BASE_PATHS ) {
205 /* add it to the list */
206 basePaths[ numBasePaths ] = safe_malloc( strlen( path ) + 1 );
207 strcpy( basePaths[ numBasePaths ], path );
208 CleanPath( basePaths[ numBasePaths ] );
215 AddHomeBasePath() - ydnar
216 adds a base path to the beginning of the list, prefixed by ~/
219 void AddHomeBasePath( char *path ){
222 char temp[ MAX_OS_PATH ];
226 if ( path == NULL || path[ 0 ] == '\0' ) {
231 for ( i = 0; i < ( MAX_BASE_PATHS - 1 ); i++ )
232 basePaths[ i + 1 ] = basePaths[ i ];
234 /* concatenate home dir and path */
235 sprintf( temp, "%s/%s", homePath, path );
237 /* add it to the list */
238 basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 );
239 strcpy( basePaths[ 0 ], temp );
240 CleanPath( basePaths[ 0 ] );
242 #endif // !GDEF_OS_WINDOWS
248 AddGamePath() - ydnar
249 adds a game path to the list
252 void AddGamePath( char *path ){
254 if ( path == NULL || path[ 0 ] == '\0' || numGamePaths >= MAX_GAME_PATHS ) {
258 /* add it to the list */
259 gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 );
260 strcpy( gamePaths[ numGamePaths ], path );
261 CleanPath( gamePaths[ numGamePaths ] );
270 cleaned up some of the path initialization code from bsp.c
271 will remove any arguments it uses
274 void InitPaths( int *argc, char **argv ){
275 int i, j, k, len, len2;
276 char temp[ MAX_OS_PATH ];
277 char gamePath[MAX_OS_PATH], homeBasePath[MAX_OS_PATH], game_magic[10];
279 strcpy( gamePath, "base" );
280 strcpy( game_magic, "h" );
281 strcpy( homeBasePath, ".heretic2" );
284 Sys_FPrintf( SYS_VRB, "--- InitPaths ---\n" );
286 /* get the install path for backup */
287 LokiInitPaths( argv[ 0 ] );
289 /* set game to default (q3a) */
293 /* parse through the arguments and extract those relevant to paths */
294 for ( i = 0; i < *argc; i++ )
297 if ( argv[ i ] == NULL ) {
302 if ( strcmp( argv[ i ], "-fs_basepath" ) == 0 ) {
303 if ( ++i >= *argc ) {
304 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
306 argv[ i - 1 ] = NULL;
307 AddBasePath( argv[ i ] );
313 /* remove processed arguments */
314 for ( i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++ )
316 for ( j; j < *argc && argv[ j ] == NULL; j++ ) ;
317 argv[ i ] = argv[ j ];
318 if ( argv[ i ] != NULL ) {
324 /* add standard game path */
325 AddGamePath( gamePath );
327 /* if there is no base path set, figure it out */
328 if ( numBasePaths == 0 ) {
329 /* this is another crappy replacement for SetQdirFromPath() */
330 len2 = strlen( game_magic );
331 for ( i = 0; i < *argc && numBasePaths == 0; i++ )
333 /* extract the arg */
334 strcpy( temp, argv[ i ] );
336 len = strlen( temp );
337 Sys_FPrintf( SYS_VRB, "Searching for \"%s\" in \"%s\" (%d)...\n", game_magic, temp, i );
339 /* this is slow, but only done once */
340 for ( j = 0; j < ( len - len2 ); j++ )
342 /* check for the game's magic word */
343 if ( Q_strncasecmp( &temp[ j ], game_magic, len2 ) == 0 ) {
344 /* now find the next slash and nuke everything after it */
345 while ( temp[ ++j ] != '/' && temp[ j ] != '\0' ) ;
348 /* add this as a base path */
355 /* add install path */
356 if ( numBasePaths == 0 ) {
357 AddBasePath( installPath );
361 if ( numBasePaths == 0 ) {
362 Error( "Failed to find a valid base path." );
366 /* this only affects unix */
367 AddHomeBasePath( homeBasePath );
369 /* initialize vfs paths */
370 if ( numBasePaths > MAX_BASE_PATHS ) {
371 numBasePaths = MAX_BASE_PATHS;
373 if ( numGamePaths > MAX_GAME_PATHS ) {
374 numGamePaths = MAX_GAME_PATHS;
377 /* walk the list of game paths */
378 //for( j = 0; j < numGamePaths; j++ )
380 /* walk the list of base paths */
381 // for( i = 0; i < numBasePaths; i++ )
383 /* create a full path and initialize it */
384 // sprintf( temp, "%s/%s/", basePaths[ i ], gamePaths[ j ] );
385 // vfsInitDirectory( temp );