]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/path_init.c
Merge pull request #21 from merlin1991/Q3-gamepack-fix
[xonotic/netradiant.git] / tools / quake3 / q3map2 / path_init.c
1 /* -------------------------------------------------------------------------------
2
3    Copyright (C) 1999-2007 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    ----------------------------------------------------------------------------------
23
24    This code has been altered significantly from its original form, to support
25    several games based on the Quake III Arena engine, in the form of "Q3Map2."
26
27    ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define PATH_INIT_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /* path support */
42 #define MAX_BASE_PATHS  10
43 #define MAX_GAME_PATHS  10
44
45 char                    *homePath;
46 char installPath[ MAX_OS_PATH ];
47
48 int numBasePaths;
49 char                    *basePaths[ MAX_BASE_PATHS ];
50 int numGamePaths;
51 char                    *gamePaths[ MAX_GAME_PATHS ];
52
53
54
55 /*
56    some of this code is based off the original q3map port from loki
57    and finds various paths. moved here from bsp.c for clarity.
58  */
59
60 /*
61    PathLokiGetHomeDir()
62    gets the user's home dir (for ~/.q3a)
63  */
64
65 char *LokiGetHomeDir( void ){
66         #ifndef Q_UNIX
67         return NULL;
68         #else
69         static char     buf[ 4096 ];
70         struct passwd   pw, *pwp;
71         char            *home;
72
73
74         /* get the home environment variable */
75         home = getenv( "HOME" );
76         if ( home ) {
77                 return Q_strncpyz( buf, home, sizeof( buf ) );
78         }
79
80         /* look up home dir in password database */
81         if ( getpwuid_r( getuid(), &pw, buf, sizeof( buf ), &pwp ) == 0 ) {
82                 return pw.pw_dir;
83         }
84
85         return NULL;
86         #endif
87 }
88
89
90
91 /*
92    PathLokiInitPaths()
93    initializes some paths on linux/os x
94  */
95
96 void LokiInitPaths( char *argv0 ){
97         #ifndef Q_UNIX
98         /* this is kinda crap, but hey */
99         strcpy( installPath, "../" );
100         #else
101         char temp[ MAX_OS_PATH ];
102         char        *home;
103         char        *path;
104         char        *last;
105         qboolean found;
106
107
108         /* get home dir */
109         home = LokiGetHomeDir();
110         if ( home == NULL ) {
111                 home = ".";
112         }
113
114         path = getenv( "PATH" );
115
116         /* do some path divining */
117         Q_strncpyz( temp, argv0, sizeof( temp ) );
118         if ( strrchr( temp, '/' ) ) {
119                 argv0 = strrchr( argv0, '/' ) + 1;
120         }
121         else if ( path ) {
122                 found = qfalse;
123                 last = path;
124
125                 /* go through each : segment of path */
126                 while ( last[ 0 ] != '\0' && found == qfalse )
127                 {
128                         /* null out temp */
129                         temp[ 0 ] = '\0';
130
131                         /* find next chunk */
132                         last = strchr( path, ':' );
133                         if ( last == NULL ) {
134                                 last = path + strlen( path );
135                         }
136
137                         /* found home dir candidate */
138                         if ( *path == '~' ) {
139                                 Q_strncpyz( temp, home, sizeof( temp ) );
140                                 path++;
141                         }
142
143                         /* concatenate */
144                         if ( last > ( path + 1 ) ) {
145                                 Q_strncat( temp, sizeof( temp ), path, ( last - path ) );
146                                 Q_strcat( temp, sizeof( temp ), "/" );
147                         }
148                         Q_strcat( temp, sizeof( temp ), "./" );
149                         Q_strcat( temp, sizeof( temp ), argv0 );
150
151                         /* verify the path */
152                         if ( access( temp, X_OK ) == 0 ) {
153                                 found++;
154                         }
155                         path = last + 1;
156                 }
157         }
158
159         /* flake */
160         if ( realpath( temp, installPath ) ) {
161                 /* q3map is in "tools/" */
162                 *( strrchr( installPath, '/' ) ) = '\0';
163                 *( strrchr( installPath, '/' ) + 1 ) = '\0';
164         }
165
166         /* set home path */
167         homePath = home;
168         #endif
169 }
170
171
172
173 /*
174    CleanPath() - ydnar
175    cleans a dos path \ -> /
176  */
177
178 void CleanPath( char *path ){
179         while ( *path )
180         {
181                 if ( *path == '\\' ) {
182                         *path = '/';
183                 }
184                 path++;
185         }
186 }
187
188
189
190 /*
191    GetGame() - ydnar
192    gets the game_t based on a -game argument
193    returns NULL if no match found
194  */
195
196 game_t *GetGame( char *arg ){
197         int i;
198
199
200         /* dummy check */
201         if ( arg == NULL || arg[ 0 ] == '\0' ) {
202                 return NULL;
203         }
204
205         /* joke */
206         if ( !Q_stricmp( arg, "quake1" ) ||
207                  !Q_stricmp( arg, "quake2" ) ||
208                  !Q_stricmp( arg, "unreal" ) ||
209                  !Q_stricmp( arg, "ut2k3" ) ||
210                  !Q_stricmp( arg, "dn3d" ) ||
211                  !Q_stricmp( arg, "dnf" ) ||
212                  !Q_stricmp( arg, "hl" ) ) {
213                 Sys_Printf( "April fools, silly rabbit!\n" );
214                 exit( 0 );
215         }
216
217         /* test it */
218         i = 0;
219         while ( games[ i ].arg != NULL )
220         {
221                 if ( Q_stricmp( arg, games[ i ].arg ) == 0 ) {
222                         return &games[ i ];
223                 }
224                 i++;
225         }
226
227         /* no matching game */
228         return NULL;
229 }
230
231
232
233 /*
234    AddBasePath() - ydnar
235    adds a base path to the list
236  */
237
238 void AddBasePath( char *path ){
239         /* dummy check */
240         if ( path == NULL || path[ 0 ] == '\0' || numBasePaths >= MAX_BASE_PATHS ) {
241                 return;
242         }
243
244         /* add it to the list */
245         basePaths[ numBasePaths ] = safe_malloc( strlen( path ) + 1 );
246         strcpy( basePaths[ numBasePaths ], path );
247         CleanPath( basePaths[ numBasePaths ] );
248         numBasePaths++;
249 }
250
251
252
253 /*
254    AddHomeBasePath() - ydnar
255    adds a base path to the beginning of the list, prefixed by ~/
256  */
257
258 void AddHomeBasePath( char *path ){
259         #ifdef Q_UNIX
260         int i;
261         char temp[ MAX_OS_PATH ];
262
263
264         /* dummy check */
265         if ( path == NULL || path[ 0 ] == '\0' ) {
266                 return;
267         }
268
269         /* make a hole */
270         for ( i = 0; i < ( MAX_BASE_PATHS - 1 ); i++ )
271                 basePaths[ i + 1 ] = basePaths[ i ];
272
273         /* concatenate home dir and path */
274         sprintf( temp, "%s/%s", homePath, path );
275
276         /* add it to the list */
277         basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 );
278         strcpy( basePaths[ 0 ], temp );
279         CleanPath( basePaths[ 0 ] );
280         numBasePaths++;
281         #endif
282 }
283
284
285
286 /*
287    AddGamePath() - ydnar
288    adds a game path to the list
289  */
290
291 void AddGamePath( char *path ){
292         int i;
293
294         /* dummy check */
295         if ( path == NULL || path[ 0 ] == '\0' || numGamePaths >= MAX_GAME_PATHS ) {
296                 return;
297         }
298
299         /* add it to the list */
300         gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 );
301         strcpy( gamePaths[ numGamePaths ], path );
302         CleanPath( gamePaths[ numGamePaths ] );
303         numGamePaths++;
304
305         /* don't add it if it's already there */
306         for ( i = 0; i < numGamePaths - 1; i++ )
307         {
308                 if ( strcmp( gamePaths[i], gamePaths[numGamePaths - 1] ) == 0 ) {
309                         free( gamePaths[numGamePaths - 1] );
310                         gamePaths[numGamePaths - 1] = NULL;
311                         numGamePaths--;
312                         break;
313                 }
314         }
315
316 }
317
318
319
320
321 /*
322    InitPaths() - ydnar
323    cleaned up some of the path initialization code from bsp.c
324    will remove any arguments it uses
325  */
326
327 void InitPaths( int *argc, char **argv ){
328         int i, j, k, len, len2;
329         char temp[ MAX_OS_PATH ];
330
331
332         /* note it */
333         Sys_FPrintf( SYS_VRB, "--- InitPaths ---\n" );
334
335         /* get the install path for backup */
336         LokiInitPaths( argv[ 0 ] );
337
338         /* set game to default (q3a) */
339         game = &games[ 0 ];
340         numBasePaths = 0;
341         numGamePaths = 0;
342
343         /* parse through the arguments and extract those relevant to paths */
344         for ( i = 0; i < *argc; i++ )
345         {
346                 /* check for null */
347                 if ( argv[ i ] == NULL ) {
348                         continue;
349                 }
350
351                 /* -game */
352                 if ( strcmp( argv[ i ], "-game" ) == 0 ) {
353                         if ( ++i >= *argc ) {
354                                 Error( "Out of arguments: No game specified after %s", argv[ i - 1 ] );
355                         }
356                         argv[ i - 1 ] = NULL;
357                         game = GetGame( argv[ i ] );
358                         if ( game == NULL ) {
359                                 game = &games[ 0 ];
360                         }
361                         argv[ i ] = NULL;
362                 }
363
364                 /* -fs_basepath */
365                 else if ( strcmp( argv[ i ], "-fs_basepath" ) == 0 ) {
366                         if ( ++i >= *argc ) {
367                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
368                         }
369                         argv[ i - 1 ] = NULL;
370                         AddBasePath( argv[ i ] );
371                         argv[ i ] = NULL;
372                 }
373
374                 /* -fs_game */
375                 else if ( strcmp( argv[ i ], "-fs_game" ) == 0 ) {
376                         if ( ++i >= *argc ) {
377                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
378                         }
379                         argv[ i - 1 ] = NULL;
380                         AddGamePath( argv[ i ] );
381                         argv[ i ] = NULL;
382                 }
383         }
384
385         /* remove processed arguments */
386         for ( i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++ )
387         {
388                 for ( ; j < *argc && argv[ j ] == NULL; j++ ) ;
389                 argv[ i ] = argv[ j ];
390                 if ( argv[ i ] != NULL ) {
391                         k++;
392                 }
393         }
394         *argc = k;
395
396         /* add standard game path */
397         AddGamePath( game->gamePath );
398
399         /* if there is no base path set, figure it out */
400         if ( numBasePaths == 0 ) {
401                 /* this is another crappy replacement for SetQdirFromPath() */
402                 len2 = strlen( game->magic );
403                 for ( i = 0; i < *argc && numBasePaths == 0; i++ )
404                 {
405                         /* extract the arg */
406                         strcpy( temp, argv[ i ] );
407                         CleanPath( temp );
408                         len = strlen( temp );
409                         Sys_FPrintf( SYS_VRB, "Searching for \"%s\" in \"%s\" (%d)...\n", game->magic, temp, i );
410
411                         /* this is slow, but only done once */
412                         for ( j = 0; j < ( len - len2 ); j++ )
413                         {
414                                 /* check for the game's magic word */
415                                 if ( Q_strncasecmp( &temp[ j ], game->magic, len2 ) == 0 ) {
416                                         /* now find the next slash and nuke everything after it */
417                                         while ( temp[ ++j ] != '/' && temp[ j ] != '\0' ) ;
418                                         temp[ j ] = '\0';
419
420                                         /* add this as a base path */
421                                         AddBasePath( temp );
422                                         break;
423                                 }
424                         }
425                 }
426
427                 /* add install path */
428                 if ( numBasePaths == 0 ) {
429                         AddBasePath( installPath );
430                 }
431
432                 /* check again */
433                 if ( numBasePaths == 0 ) {
434                         Error( "Failed to find a valid base path." );
435                 }
436         }
437
438         /* this only affects unix */
439         AddHomeBasePath( game->homeBasePath );
440
441         /* initialize vfs paths */
442         if ( numBasePaths > MAX_BASE_PATHS ) {
443                 numBasePaths = MAX_BASE_PATHS;
444         }
445         if ( numGamePaths > MAX_GAME_PATHS ) {
446                 numGamePaths = MAX_GAME_PATHS;
447         }
448
449         /* walk the list of game paths */
450         for ( j = 0; j < numGamePaths; j++ )
451         {
452                 /* walk the list of base paths */
453                 for ( i = 0; i < numBasePaths; i++ )
454                 {
455                         /* create a full path and initialize it */
456                         sprintf( temp, "%s/%s/", basePaths[ i ], gamePaths[ j ] );
457                         vfsInitDirectory( temp );
458                 }
459         }
460
461         /* done */
462         Sys_Printf( "\n" );
463 }