]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake2/common/path_init.c
q3map2: -fs_nobasepath implies -fs_nomagicpath
[xonotic/netradiant.git] / tools / quake2 / common / path_init.c
1 /* -------------------------------------------------------------------------------
2
3    Copyright (C) 1999-2006 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    Nurail: Swiped from Q3Map2
23
24  */
25
26 #include "globaldefs.h"
27
28 #if GDEF_OS_LINUX || GDEF_OS_MACOS
29         #define Q_UNIX
30 #endif
31
32 #ifdef Q_UNIX
33         #include <unistd.h>
34         #include <pwd.h>
35         #include <limits.h>
36 #endif
37
38
39 /* dependencies */
40 #include "cmdlib.h"
41 #include "inout.h"
42
43
44
45 /* path support */
46 #define MAX_BASE_PATHS  10
47 #define MAX_GAME_PATHS  10
48
49 char                    *homePath;
50 char installPath[ MAX_OS_PATH ];
51
52 int numBasePaths;
53 char                    *basePaths[ MAX_BASE_PATHS ];
54 int numGamePaths;
55 char                    *gamePaths[ MAX_GAME_PATHS ];
56
57 /*
58    some of this code is based off the original q3map port from loki
59    and finds various paths. moved here from bsp.c for clarity.
60  */
61
62 /*
63    PathLokiGetHomeDir()
64    gets the user's home dir (for ~/.q3a)
65  */
66
67 char *LokiGetHomeDir( void ){
68         #ifndef Q_UNIX
69         return NULL;
70         #else
71         char            *home;
72         uid_t id;
73         struct passwd   *pwd;
74
75
76         /* get the home environment variable */
77         home = getenv( "HOME" );
78         if ( home == NULL ) {
79                 /* do some more digging */
80                 id = getuid();
81                 setpwent();
82                 while ( ( pwd = getpwent() ) != NULL )
83                 {
84                         if ( pwd->pw_uid == id ) {
85                                 home = pwd->pw_dir;
86                                 break;
87                         }
88                 }
89                 endpwent();
90         }
91
92         /* return it */
93         return home;
94         #endif
95 }
96
97
98
99 /*
100    PathLokiInitPaths()
101    initializes some paths on linux/os x
102  */
103
104 void LokiInitPaths( char *argv0 ){
105         #ifndef Q_UNIX
106         /* this is kinda crap, but hey */
107         strcpy( installPath, "../" );
108         #else
109         char temp[ MAX_OS_PATH ];
110         char        *home;
111         char        *path;
112         char        *last;
113         qboolean found;
114
115
116         /* get home dir */
117         home = LokiGetHomeDir();
118         if ( home == NULL ) {
119                 home = ".";
120         }
121
122         /* do some path divining */
123         strcpy( temp, argv0 );
124         if ( strrchr( temp, '/' ) ) {
125                 argv0 = strrchr( argv0, '/' ) + 1;
126         }
127         else
128         {
129                 /* get path environment variable */
130                 path = getenv( "PATH" );
131
132                 /* minor setup */
133                 last[ 0 ] = path[ 0 ];
134                 last[ 1 ] = '\0';
135                 found = false;
136
137                 /* go through each : segment of path */
138                 while ( last[ 0 ] != '\0' && found == false )
139                 {
140                         /* null out temp */
141                         temp[ 0 ] = '\0';
142
143                         /* find next chunk */
144                         last = strchr( path, ':' );
145                         if ( last == NULL ) {
146                                 last = path + strlen( path );
147                         }
148
149                         /* found home dir candidate */
150                         if ( *path == '~' ) {
151                                 strcpy( temp, home );
152                                 path++;
153                         }
154
155                         /* concatenate */
156                         if ( last > ( path + 1 ) ) {
157                                 strncat( temp, path, ( last - path ) );
158                                 strcat( temp, "/" );
159                         }
160                         strcat( temp, "./" );
161                         strcat( temp, argv0 );
162
163                         /* verify the path */
164                         if ( access( temp, X_OK ) == 0 ) {
165                                 found++;
166                         }
167                         path = last + 1;
168                 }
169         }
170
171         /* flake */
172         if ( realpath( temp, installPath ) ) {
173                 /* q3map is in "tools/" */
174                 *( strrchr( installPath, '/' ) ) = '\0';
175                 *( strrchr( installPath, '/' ) + 1 ) = '\0';
176         }
177
178         /* set home path */
179         homePath = home;
180         #endif
181 }
182
183
184
185 /*
186    CleanPath() - ydnar
187    cleans a dos path \ -> /
188  */
189
190 void CleanPath( char *path ){
191         while ( *path )
192         {
193                 if ( *path == '\\' ) {
194                         *path = '/';
195                 }
196                 path++;
197         }
198 }
199
200 /*
201    AddBasePath() - ydnar
202    adds a base path to the list
203  */
204
205 void AddBasePath( char *path ){
206         /* dummy check */
207         if ( path == NULL || path[ 0 ] == '\0' || numBasePaths >= MAX_BASE_PATHS ) {
208                 return;
209         }
210
211         /* add it to the list */
212         basePaths[ numBasePaths ] = safe_malloc( strlen( path ) + 1 );
213         strcpy( basePaths[ numBasePaths ], path );
214         CleanPath( basePaths[ numBasePaths ] );
215         numBasePaths++;
216 }
217
218
219
220 /*
221    AddHomeBasePath() - ydnar
222    adds a base path to the beginning of the list, prefixed by ~/
223  */
224
225 void AddHomeBasePath( char *path ){
226         #ifdef Q_UNIX
227         int i;
228         char temp[ MAX_OS_PATH ];
229
230
231         /* dummy check */
232         if ( path == NULL || path[ 0 ] == '\0' ) {
233                 return;
234         }
235
236         /* make a hole */
237         for ( i = 0; i < ( MAX_BASE_PATHS - 1 ); i++ )
238                 basePaths[ i + 1 ] = basePaths[ i ];
239
240         /* concatenate home dir and path */
241         sprintf( temp, "%s/%s", homePath, path );
242
243         /* add it to the list */
244         basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 );
245         strcpy( basePaths[ 0 ], temp );
246         CleanPath( basePaths[ 0 ] );
247         numBasePaths++;
248         #endif
249 }
250
251
252
253 /*
254    AddGamePath() - ydnar
255    adds a game path to the list
256  */
257
258 void AddGamePath( char *path ){
259         /* dummy check */
260         if ( path == NULL || path[ 0 ] == '\0' || numGamePaths >= MAX_GAME_PATHS ) {
261                 return;
262         }
263
264         /* add it to the list */
265         gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 );
266         strcpy( gamePaths[ numGamePaths ], path );
267         CleanPath( gamePaths[ numGamePaths ] );
268         numGamePaths++;
269 }
270
271
272
273
274 /*
275    InitPaths() - ydnar
276    cleaned up some of the path initialization code from bsp.c
277    will remove any arguments it uses
278  */
279
280 void InitPaths( int *argc, char **argv ){
281         int i, j, k, len, len2;
282         char temp[ MAX_OS_PATH ];
283         char gamePath[MAX_OS_PATH], homeBasePath[MAX_OS_PATH], game_magic[10];
284
285         strcpy( gamePath, "baseq2" );
286         strcpy( game_magic, "quake" );
287         strcpy( homeBasePath, ".quake2" );
288
289         /* note it */
290         Sys_FPrintf( SYS_VRB, "--- InitPaths ---\n" );
291
292         /* get the install path for backup */
293         LokiInitPaths( argv[ 0 ] );
294
295         /* set game to default (q3a) */
296         numBasePaths = 0;
297         numGamePaths = 0;
298
299         /* parse through the arguments and extract those relevant to paths */
300         for ( i = 0; i < *argc; i++ )
301         {
302                 /* check for null */
303                 if ( argv[ i ] == NULL ) {
304                         continue;
305                 }
306
307                 /* -fs_basepath */
308                 if ( strcmp( argv[ i ], "-fs_basepath" ) == 0 ) {
309                         if ( ++i >= *argc ) {
310                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
311                         }
312                         argv[ i - 1 ] = NULL;
313                         AddBasePath( argv[ i ] );
314                         argv[ i ] = NULL;
315                 }
316
317         }
318
319         /* remove processed arguments */
320         for ( i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++ )
321         {
322                 for ( j; j < *argc && argv[ j ] == NULL; j++ ) ;
323                 argv[ i ] = argv[ j ];
324                 if ( argv[ i ] != NULL ) {
325                         k++;
326                 }
327         }
328         *argc = k;
329
330         /* add standard game path */
331         AddGamePath( gamePath );
332
333         /* if there is no base path set, figure it out */
334         if ( numBasePaths == 0 ) {
335                 /* this is another crappy replacement for SetQdirFromPath() */
336                 len2 = strlen( game_magic );
337                 for ( i = 0; i < *argc && numBasePaths == 0; i++ )
338                 {
339                         /* extract the arg */
340                         strcpy( temp, argv[ i ] );
341                         CleanPath( temp );
342                         len = strlen( temp );
343                         Sys_FPrintf( SYS_VRB, "Searching for \"%s\" in \"%s\" (%d)...\n", game_magic, temp, i );
344
345                         /* this is slow, but only done once */
346                         for ( j = 0; j < ( len - len2 ); j++ )
347                         {
348                                 /* check for the game's magic word */
349                                 if ( Q_strncasecmp( &temp[ j ], game_magic, len2 ) == 0 ) {
350                                         /* now find the next slash and nuke everything after it */
351                                         while ( temp[ ++j ] != '/' && temp[ j ] != '\0' ) ;
352                                         temp[ j ] = '\0';
353
354                                         /* add this as a base path */
355                                         AddBasePath( temp );
356                                         break;
357                                 }
358                         }
359                 }
360
361                 /* add install path */
362                 if ( numBasePaths == 0 ) {
363                         AddBasePath( installPath );
364                 }
365
366                 /* check again */
367                 if ( numBasePaths == 0 ) {
368                         Error( "Failed to find a valid base path." );
369                 }
370         }
371
372         /* this only affects unix */
373         AddHomeBasePath( homeBasePath );
374
375         /* initialize vfs paths */
376         if ( numBasePaths > MAX_BASE_PATHS ) {
377                 numBasePaths = MAX_BASE_PATHS;
378         }
379         if ( numGamePaths > MAX_GAME_PATHS ) {
380                 numGamePaths = MAX_GAME_PATHS;
381         }
382
383         /* walk the list of game paths */
384         //for( j = 0; j < numGamePaths; j++ )
385         //{
386         /* walk the list of base paths */
387         //      for( i = 0; i < numBasePaths; i++ )
388         //      {
389         /* create a full path and initialize it */
390         //              sprintf( temp, "%s/%s/", basePaths[ i ], gamePaths[ j ] );
391         //              vfsInitDirectory( temp );
392         //      }
393         //}
394
395         /* done */
396         Sys_Printf( "\n" );
397 }