]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/environment.cpp
Merge commit '6f51c7f28dc9f56ae64e7da7d42dcbaa068da65a' into garux-merge
[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                 {
43                         if ( argv[k] != 0 ) {
44                                 break;
45                         }
46                 }
47
48                 if ( k > i ) {
49                         k -= i;
50                         for ( j = i + k; j < argc; j++ )
51                         {
52                                 argv[j - k] = argv[j];
53                         }
54                         argc -= k;
55                 }
56         }
57
58         g_argc = argc;
59         g_argv = argv;
60 }
61
62 char const *gamedetect_argv_buffer[1024];
63
64 void gamedetect_found_game( char const *game, char *path ){
65         int argc;
66         static char buf[128];
67
68         if ( g_argv == gamedetect_argv_buffer ) {
69                 return;
70         }
71
72         globalOutputStream() << "Detected game " << game << " in " << path << "\n";
73
74         sprintf( buf, "-%s-EnginePath", game );
75         argc = 0;
76         gamedetect_argv_buffer[argc++] = "-global-gamefile";
77         gamedetect_argv_buffer[argc++] = game;
78         gamedetect_argv_buffer[argc++] = buf;
79         gamedetect_argv_buffer[argc++] = path;
80         if ( (size_t) ( argc + g_argc ) >= sizeof( gamedetect_argv_buffer ) / sizeof( *gamedetect_argv_buffer ) - 1 ) {
81                 g_argc = sizeof( gamedetect_argv_buffer ) / sizeof( *gamedetect_argv_buffer ) - g_argc - 1;
82         }
83         memcpy( gamedetect_argv_buffer + 4, g_argv, sizeof( *gamedetect_argv_buffer ) * g_argc );
84         g_argc += argc;
85         g_argv = gamedetect_argv_buffer;
86 }
87
88 bool gamedetect_check_game( char const *gamefile, const char *checkfile1, const char *checkfile2, char *buf /* must have 64 bytes free after bufpos */, int bufpos ){
89         buf[bufpos] = '/';
90
91         strcpy( buf + bufpos + 1, checkfile1 );
92         globalOutputStream() << "Checking for a game file in " << buf << "\n";
93         if ( !file_exists( buf ) ) {
94                 return false;
95         }
96
97         if ( checkfile2 ) {
98                 strcpy( buf + bufpos + 1, checkfile2 );
99                 globalOutputStream() << "Checking for a game file in " << buf << "\n";
100                 if ( !file_exists( buf ) ) {
101                         return false;
102                 }
103         }
104
105         buf[bufpos + 1] = 0;
106         gamedetect_found_game( gamefile, buf );
107         return true;
108 }
109
110 void gamedetect(){
111         // if we're inside a Nexuiz install
112         // default to nexuiz.game (unless the user used an option to inhibit this)
113         //bool nogamedetect = false;
114         bool nogamedetect = true;
115         int i;
116         for ( i = 1; i < g_argc - 1; ++i )
117         {
118                 if ( g_argv[i][0] == '-' ) {
119                         if ( !strcmp( g_argv[i], "-gamedetect" ) ) {
120                                 nogamedetect = !strcmp( g_argv[i + 1], "false" );
121                         }
122                         ++i;
123                 }
124         }
125         if ( !nogamedetect ) {
126                 static char buf[1024 + 64];
127                 strncpy( buf, environment_get_app_path(), sizeof( buf ) );
128                 buf[sizeof( buf ) - 1 - 64] = 0;
129                 if ( !strlen( buf ) ) {
130                         return;
131                 }
132
133                 char *p = buf + strlen( buf ) - 1; // point directly on the slash of get_app_path
134                 while ( p != buf )
135                 {
136                         // TODO add more games to this
137
138                         // try to detect Nexuiz installs
139 #if GDEF_OS_WINDOWS
140                         if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz.exe", buf, p - buf ) )
141 #elif GDEF_OS_MACOS
142                         if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "Nexuiz.app/Contents/Info.plist", buf, p - buf ) )
143 #else
144                         if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz-linux-glx.sh", buf, p - buf ) )
145 #endif
146                         { return; }
147
148                         // try to detect Quetoo installs
149                         if ( gamedetect_check_game( "quetoo.game", "default/icons/quetoo.png", NULL, buf, p - buf ) ) {
150                                 return;
151                         }
152
153                         // try to detect Warsow installs
154                         if ( gamedetect_check_game( "warsow.game", "basewsw/dedicated_autoexec.cfg", NULL, buf, p - buf ) ) {
155                                 return;
156                         }
157
158                         // we found nothing
159                         // go backwards
160                         --p;
161                         while ( p != buf && *p != '/' && *p != '\\' )
162                                 --p;
163                 }
164         }
165 }
166
167 namespace
168 {
169 CopiedString home_path;
170 CopiedString app_path;
171 }
172
173 const char* environment_get_home_path(){
174         return home_path.c_str();
175 }
176
177 const char* environment_get_app_path(){
178         return app_path.c_str();
179 }
180
181 bool portable_app_setup(){
182         StringOutputStream confdir( 256 );
183         confdir << app_path.c_str() << "settings/";
184         if ( file_exists( confdir.c_str() ) ) {
185                 home_path = confdir.c_str();
186                 return true;
187         }
188         return false;
189 }
190
191 #if GDEF_OS_POSIX
192
193 #include <stdlib.h>
194 #include <pwd.h>
195 #include <unistd.h>
196
197 #include <glib.h>
198
199 const char* LINK_NAME =
200 #if GDEF_OS_LINUX
201         "/proc/self/exe"
202 #else // FreeBSD and OSX
203         "/proc/curproc/file"
204 #endif
205 ;
206
207 /// brief Returns the filename of the executable belonging to the current process, or 0 if not found.
208 char const* getexename( char *buf ){
209         /* Now read the symbolic link */
210         int ret = readlink( LINK_NAME, buf, PATH_MAX );
211
212         if ( ret == -1 ) {
213                 globalOutputStream() << "getexename: falling back to argv[0]: " << makeQuoted( g_argv[0] );
214                 const char* path = realpath( g_argv[0], buf );
215                 if ( path == 0 ) {
216                         /* In case of an error, leave the handling up to the caller */
217                         return "";
218                 }
219         }
220
221         /* Ensure proper NUL termination */
222         buf[ret] = 0;
223
224         /* delete the program name */
225         *( strrchr( buf, '/' ) ) = '\0';
226
227         // NOTE: we build app path with a trailing '/'
228         // it's a general convention in Radiant to have the slash at the end of directories
229         if ( buf[strlen( buf ) - 1] != '/' ) {
230                 strcat( buf, "/" );
231         }
232
233         return buf;
234 }
235
236 void environment_init( int argc, char const* argv[] ){
237         // Give away unnecessary root privileges.
238         // Important: must be done before calling gtk_init().
239         char *loginname;
240         struct passwd *pw;
241         seteuid( getuid() );
242         if ( geteuid() == 0 && ( loginname = getlogin() ) != 0 &&
243                  ( pw = getpwnam( loginname ) ) != 0 ) {
244                 setuid( pw->pw_uid );
245         }
246
247         args_init( argc, argv );
248
249         {
250                 char real[PATH_MAX];
251                 app_path = getexename( real );
252                 ASSERT_MESSAGE( !string_empty( app_path.c_str() ), "failed to deduce app path" );
253         }
254
255         if ( !portable_app_setup() ) {
256                 StringOutputStream home( 256 );
257                 home << DirectoryCleaned( g_get_user_config_dir() ) << "netradiant/";
258                 Q_mkdir( home.c_str() );
259                 home_path = home.c_str();
260         }
261         gamedetect();
262 }
263
264 #elif GDEF_OS_WINDOWS
265
266 #include <windows.h>
267
268 char* openCmdMap;
269
270 void cmdMap(){
271         openCmdMap = NULL;
272         for ( int i = 1; i < g_argc; ++i )
273         {
274                 if ( !stricmp( g_argv[i] + strlen(g_argv[i]) - 4, ".map" ) ){
275                         openCmdMap = g_argv[i];
276                 }
277         }
278 }
279
280 void environment_init( int argc, char const* argv[] ){
281         args_init( argc, argv );
282
283         {
284                 // get path to the editor
285                 char filename[MAX_PATH + 1];
286                 GetModuleFileName( 0, filename, MAX_PATH );
287                 char* last_separator = strrchr( filename, '\\' );
288                 if ( last_separator != 0 ) {
289                         *( last_separator + 1 ) = '\0';
290                 }
291                 else
292                 {
293                         filename[0] = '\0';
294                 }
295                 StringOutputStream app( 256 );
296                 app << PathCleaned( filename );
297                 app_path = app.c_str();
298         }
299
300         if ( !portable_app_setup() ) {
301                 char *appdata = getenv( "APPDATA" );
302                 StringOutputStream home( 256 );
303                 home << PathCleaned( appdata );
304                 home << "/NetRadiantSettings/";
305                 Q_mkdir( home.c_str() );
306                 home_path = home.c_str();
307         }
308         gamedetect();
309         cmdMap();
310 }
311
312 #else
313 #error "unsupported platform"
314 #endif