]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/environment.cpp
Radiant:
[xonotic/netradiant.git] / radiant / environment.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "environment.h"
23
24 #include "stream/textstream.h"
25 #include "string/string.h"
26 #include "stream/stringstream.h"
27 #include "debugging/debugging.h"
28 #include "os/path.h"
29 #include "os/file.h"
30 #include "cmdlib.h"
31
32 int g_argc;
33 char** g_argv;
34
35 void args_init( int argc, char* argv[] ){
36         int i, j, k;
37
38         for ( i = 1; i < argc; i++ )
39         {
40                 for ( k = i; k < argc; k++ )
41                         if ( argv[k] != 0 ) {
42                                 break;
43                         }
44
45                 if ( k > i ) {
46                         k -= i;
47                         for ( j = i + k; j < argc; j++ )
48                                 argv[j - k] = argv[j];
49                         argc -= k;
50                 }
51         }
52
53         g_argc = argc;
54         g_argv = argv;
55 }
56
57 char *gamedetect_argv_buffer[1024];
58 void gamedetect_found_game( char *game, char *path ){
59         int argc;
60         static char buf[128];
61
62         if ( g_argv == gamedetect_argv_buffer ) {
63                 return;
64         }
65
66         globalOutputStream() << "Detected game " << game << " in " << path << "\n";
67
68         sprintf( buf, "-%s-EnginePath", game );
69         argc = 0;
70         gamedetect_argv_buffer[argc++] = "-global-gamefile";
71         gamedetect_argv_buffer[argc++] = game;
72         gamedetect_argv_buffer[argc++] = buf;
73         gamedetect_argv_buffer[argc++] = path;
74         if ( (size_t) ( argc + g_argc ) >= sizeof( gamedetect_argv_buffer ) / sizeof( *gamedetect_argv_buffer ) - 1 ) {
75                 g_argc = sizeof( gamedetect_argv_buffer ) / sizeof( *gamedetect_argv_buffer ) - g_argc - 1;
76         }
77         memcpy( gamedetect_argv_buffer + 4, g_argv, sizeof( *gamedetect_argv_buffer ) * g_argc );
78         g_argc += argc;
79         g_argv = gamedetect_argv_buffer;
80 }
81
82 bool gamedetect_check_game( char *gamefile, const char *checkfile1, const char *checkfile2, char *buf /* must have 64 bytes free after bufpos */, int bufpos ){
83         buf[bufpos] = '/';
84
85         strcpy( buf + bufpos + 1, checkfile1 );
86         globalOutputStream() << "Checking for a game file in " << buf << "\n";
87         if ( !file_exists( buf ) ) {
88                 return false;
89         }
90
91         if ( checkfile2 ) {
92                 strcpy( buf + bufpos + 1, checkfile2 );
93                 globalOutputStream() << "Checking for a game file in " << buf << "\n";
94                 if ( !file_exists( buf ) ) {
95                         return false;
96                 }
97         }
98
99         buf[bufpos + 1] = 0;
100         gamedetect_found_game( gamefile, buf );
101         return true;
102 }
103
104 void gamedetect(){
105         // if we're inside a Nexuiz install
106         // default to nexuiz.game (unless the user used an option to inhibit this)
107         //bool nogamedetect = false;
108         bool nogamedetect = true;
109         int i;
110         for ( i = 1; i < g_argc - 1; ++i )
111                 if ( g_argv[i][0] == '-' ) {
112                         if ( !strcmp( g_argv[i], "-gamedetect" ) ) {
113                                 nogamedetect = !strcmp( g_argv[i + 1], "false" );
114                         }
115                         ++i;
116                 }
117         if ( !nogamedetect ) {
118                 static char buf[1024 + 64];
119                 strncpy( buf, environment_get_app_path(), sizeof( buf ) );
120                 buf[sizeof( buf ) - 1 - 64] = 0;
121                 if ( !strlen( buf ) ) {
122                         return;
123                 }
124
125                 char *p = buf + strlen( buf ) - 1; // point directly on the slash of get_app_path
126                 while ( p != buf )
127                 {
128                         // TODO add more games to this
129
130                         // try to detect Nexuiz installs
131 #if defined( WIN32 )
132                         if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz.exe", buf, p - buf ) )
133 #elif defined( __APPLE__ )
134                         if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "Nexuiz.app/Contents/Info.plist", buf, p - buf ) )
135 #else
136                         if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz-linux-glx.sh", buf, p - buf ) )
137 #endif
138                         { return; }
139
140                         // try to detect Q2World installs
141                         if ( gamedetect_check_game( "q2w.game", "default/quake2world.version", NULL, buf, p - buf ) ) {
142                                 return;
143                         }
144
145                         // try to detect Warsow installs
146                         if ( gamedetect_check_game( "warsow.game", "basewsw/dedicated_autoexec.cfg", NULL, buf, p - buf ) ) {
147                                 return;
148                         }
149
150                         // we found nothing
151                         // go backwards
152                         --p;
153                         while ( p != buf && *p != '/' && *p != '\\' )
154                                 --p;
155                 }
156         }
157 }
158
159 namespace
160 {
161 CopiedString home_path;
162 CopiedString app_path;
163 }
164
165 const char* environment_get_home_path(){
166         return home_path.c_str();
167 }
168
169 const char* environment_get_app_path(){
170         return app_path.c_str();
171 }
172
173 bool portable_app_setup(){
174         StringOutputStream confdir( 256 );
175         confdir << app_path.c_str() << "settings/";
176         if ( file_exists( confdir.c_str() ) ) {
177                 home_path = confdir.c_str();
178                 return true;
179         }
180         return false;
181 }
182
183
184 char* openCmdMap;
185
186 void cmdMap(){
187         openCmdMap = NULL;
188         for ( int i = 1; i < g_argc; ++i )
189         {
190                 //if ( !stricmp( g_argv[i] + strlen(g_argv[i]) - 4, ".map" ) ){
191                 if( string_equal_suffix_nocase( g_argv[i], ".map" ) ){
192                         openCmdMap = g_argv[i];
193                 }
194         }
195 }
196
197 #if defined( POSIX )
198
199 #include <stdlib.h>
200 #include <pwd.h>
201 #include <unistd.h>
202
203 #include <glib.h>
204
205 const char* LINK_NAME =
206 #if defined ( __linux__ )
207         "/proc/self/exe"
208 #else // FreeBSD and OSX
209         "/proc/curproc/file"
210 #endif
211 ;
212
213 /// brief Returns the filename of the executable belonging to the current process, or 0 if not found.
214 char* getexename( char *buf ){
215         /* Now read the symbolic link */
216         int ret = readlink( LINK_NAME, buf, PATH_MAX );
217
218         if ( ret == -1 ) {
219                 globalOutputStream() << "getexename: falling back to argv[0]: " << makeQuoted( g_argv[0] );
220                 const char* path = realpath( g_argv[0], buf );
221                 if ( path == 0 ) {
222                         /* In case of an error, leave the handling up to the caller */
223                         return "";
224                 }
225         }
226
227         /* Ensure proper NUL termination */
228         buf[ret] = 0;
229
230         /* delete the program name */
231         *( strrchr( buf, '/' ) ) = '\0';
232
233         // NOTE: we build app path with a trailing '/'
234         // it's a general convention in Radiant to have the slash at the end of directories
235         if ( buf[strlen( buf ) - 1] != '/' ) {
236                 strcat( buf, "/" );
237         }
238
239         return buf;
240 }
241
242 void environment_init( int argc, char* argv[] ){
243         // Give away unnecessary root privileges.
244         // Important: must be done before calling gtk_init().
245         char *loginname;
246         struct passwd *pw;
247         seteuid( getuid() );
248         if ( geteuid() == 0 && ( loginname = getlogin() ) != 0 &&
249                  ( pw = getpwnam( loginname ) ) != 0 ) {
250                 setuid( pw->pw_uid );
251         }
252
253         args_init( argc, argv );
254
255         {
256                 char real[PATH_MAX];
257                 app_path = getexename( real );
258                 ASSERT_MESSAGE( !string_empty( app_path.c_str() ), "failed to deduce app path" );
259         }
260
261         if ( !portable_app_setup() ) {
262                 StringOutputStream home( 256 );
263                 home << DirectoryCleaned( g_get_home_dir() ) << ".netradiant/";
264                 Q_mkdir( home.c_str() );
265                 home_path = home.c_str();
266         }
267         gamedetect();
268         cmdMap();
269 }
270
271 #elif defined( WIN32 )
272
273 #include <windows.h>
274
275 void environment_init( int argc, char* argv[] ){
276         args_init( argc, argv );
277
278         {
279                 // get path to the editor
280                 char filename[MAX_PATH + 1];
281                 GetModuleFileName( 0, filename, MAX_PATH );
282                 char* last_separator = strrchr( filename, '\\' );
283                 if ( last_separator != 0 ) {
284                         *( last_separator + 1 ) = '\0';
285                 }
286                 else
287                 {
288                         filename[0] = '\0';
289                 }
290                 StringOutputStream app( 256 );
291                 app << PathCleaned( filename );
292                 app_path = app.c_str();
293         }
294
295         if ( !portable_app_setup() ) {
296                 char *appdata = getenv( "APPDATA" );
297                 StringOutputStream home( 256 );
298                 if ( !appdata || string_empty( appdata ) ) {
299                         ERROR_MESSAGE( "Application Data folder not available.\n"
300                                                    "Radiant will use C:\\ for user preferences.\n" );
301                         home << "C:";
302                 }
303                 else
304                 {
305                         home << PathCleaned( appdata );
306                 }
307                 home << "/NetRadiantSettings/";
308                 Q_mkdir( home.c_str() );
309                 home_path = home.c_str();
310         }
311         gamedetect();
312         cmdMap();
313 }
314
315 #else
316 #error "unsupported platform"
317 #endif