]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/environment.cpp
Centralise compile checks
[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 #include "globaldefs.h"
24
25 #include "stream/textstream.h"
26 #include "string/string.h"
27 #include "stream/stringstream.h"
28 #include "debugging/debugging.h"
29 #include "os/path.h"
30 #include "os/file.h"
31 #include "cmdlib.h"
32
33 int g_argc;
34 char const** g_argv;
35
36 void args_init( int argc, char const* argv[] ){
37         int i, j, k;
38
39         for ( i = 1; i < argc; i++ )
40         {
41                 for ( k = i; k < argc; k++ )
42                         if ( argv[k] != 0 ) {
43                                 break;
44                         }
45
46                 if ( k > i ) {
47                         k -= i;
48                         for ( j = i + k; j < argc; j++ )
49                                 argv[j - k] = argv[j];
50                         argc -= k;
51                 }
52         }
53
54         g_argc = argc;
55         g_argv = argv;
56 }
57
58 char const *gamedetect_argv_buffer[1024];
59 void gamedetect_found_game( char const *game, char *path ){
60         int argc;
61         static char buf[128];
62
63         if ( g_argv == gamedetect_argv_buffer ) {
64                 return;
65         }
66
67         globalOutputStream() << "Detected game " << game << " in " << path << "\n";
68
69         sprintf( buf, "-%s-EnginePath", game );
70         argc = 0;
71         gamedetect_argv_buffer[argc++] = "-global-gamefile";
72         gamedetect_argv_buffer[argc++] = game;
73         gamedetect_argv_buffer[argc++] = buf;
74         gamedetect_argv_buffer[argc++] = path;
75         if ( (size_t) ( argc + g_argc ) >= sizeof( gamedetect_argv_buffer ) / sizeof( *gamedetect_argv_buffer ) - 1 ) {
76                 g_argc = sizeof( gamedetect_argv_buffer ) / sizeof( *gamedetect_argv_buffer ) - g_argc - 1;
77         }
78         memcpy( gamedetect_argv_buffer + 4, g_argv, sizeof( *gamedetect_argv_buffer ) * g_argc );
79         g_argc += argc;
80         g_argv = gamedetect_argv_buffer;
81 }
82
83 bool gamedetect_check_game( char const *gamefile, const char *checkfile1, const char *checkfile2, char *buf /* must have 64 bytes free after bufpos */, int bufpos ){
84         buf[bufpos] = '/';
85
86         strcpy( buf + bufpos + 1, checkfile1 );
87         globalOutputStream() << "Checking for a game file in " << buf << "\n";
88         if ( !file_exists( buf ) ) {
89                 return false;
90         }
91
92         if ( checkfile2 ) {
93                 strcpy( buf + bufpos + 1, checkfile2 );
94                 globalOutputStream() << "Checking for a game file in " << buf << "\n";
95                 if ( !file_exists( buf ) ) {
96                         return false;
97                 }
98         }
99
100         buf[bufpos + 1] = 0;
101         gamedetect_found_game( gamefile, buf );
102         return true;
103 }
104
105 void gamedetect(){
106         // if we're inside a Nexuiz install
107         // default to nexuiz.game (unless the user used an option to inhibit this)
108         bool nogamedetect = false;
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 GDEF_OS_WINDOWS
132                         if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz.exe", buf, p - buf ) )
133 #elif GDEF_OS_MACOS
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 Quetoo installs
141                         if ( gamedetect_check_game( "quetoo.game", "default/icons/quetoo.png", 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 #if GDEF_OS_POSIX
184
185 #include <stdlib.h>
186 #include <pwd.h>
187 #include <unistd.h>
188
189 #include <glib.h>
190
191 const char* LINK_NAME =
192 #if GDEF_OS_LINUX
193         "/proc/self/exe"
194 #else // FreeBSD and OSX
195         "/proc/curproc/file"
196 #endif
197 ;
198
199 /// brief Returns the filename of the executable belonging to the current process, or 0 if not found.
200 char const* getexename( char *buf ){
201         /* Now read the symbolic link */
202         int ret = readlink( LINK_NAME, buf, PATH_MAX );
203
204         if ( ret == -1 ) {
205                 globalOutputStream() << "getexename: falling back to argv[0]: " << makeQuoted( g_argv[0] );
206                 const char* path = realpath( g_argv[0], buf );
207                 if ( path == 0 ) {
208                         /* In case of an error, leave the handling up to the caller */
209                         return "";
210                 }
211         }
212
213         /* Ensure proper NUL termination */
214         buf[ret] = 0;
215
216         /* delete the program name */
217         *( strrchr( buf, '/' ) ) = '\0';
218
219         // NOTE: we build app path with a trailing '/'
220         // it's a general convention in Radiant to have the slash at the end of directories
221         if ( buf[strlen( buf ) - 1] != '/' ) {
222                 strcat( buf, "/" );
223         }
224
225         return buf;
226 }
227
228 void environment_init( int argc, char const* argv[] ){
229         // Give away unnecessary root privileges.
230         // Important: must be done before calling gtk_init().
231         char *loginname;
232         struct passwd *pw;
233         seteuid( getuid() );
234         if ( geteuid() == 0 && ( loginname = getlogin() ) != 0 &&
235                  ( pw = getpwnam( loginname ) ) != 0 ) {
236                 setuid( pw->pw_uid );
237         }
238
239         args_init( argc, argv );
240
241         {
242                 char real[PATH_MAX];
243                 app_path = getexename( real );
244                 ASSERT_MESSAGE( !string_empty( app_path.c_str() ), "failed to deduce app path" );
245         }
246
247         if ( !portable_app_setup() ) {
248                 StringOutputStream home( 256 );
249                 home << DirectoryCleaned( g_get_user_config_dir() ) << "netradiant/";
250                 Q_mkdir( home.c_str() );
251                 home_path = home.c_str();
252         }
253         gamedetect();
254 }
255
256 #elif GDEF_OS_WINDOWS
257
258 #include <windows.h>
259
260 void environment_init( int argc, char const* argv[] ){
261         args_init( argc, argv );
262
263         {
264                 // get path to the editor
265                 char filename[MAX_PATH + 1];
266                 GetModuleFileName( 0, filename, MAX_PATH );
267                 char* last_separator = strrchr( filename, '\\' );
268                 if ( last_separator != 0 ) {
269                         *( last_separator + 1 ) = '\0';
270                 }
271                 else
272                 {
273                         filename[0] = '\0';
274                 }
275                 StringOutputStream app( 256 );
276                 app << PathCleaned( filename );
277                 app_path = app.c_str();
278         }
279
280         if ( !portable_app_setup() ) {
281                 char *appdata = getenv( "APPDATA" );
282                 StringOutputStream home( 256 );
283                 if ( !appdata || string_empty( appdata ) ) {
284                         ERROR_MESSAGE( "Application Data folder not available.\n"
285                                                    "Radiant will use C:\\ for user preferences.\n" );
286                         home << "C:";
287                 }
288                 else
289                 {
290                         home << PathCleaned( appdata );
291                 }
292                 home << "/NetRadiantSettings/";
293                 Q_mkdir( home.c_str() );
294                 home_path = home.c_str();
295         }
296         gamedetect();
297 }
298
299 #else
300 #error "unsupported platform"
301 #endif