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