2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
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.
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.
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
22 #include "environment.h"
24 #include "stream/textstream.h"
25 #include "string/string.h"
26 #include "stream/stringstream.h"
27 #include "debugging/debugging.h"
35 void args_init( int argc, char* argv[] ){
38 for ( i = 1; i < argc; i++ )
40 for ( k = i; k < argc; k++ )
47 for ( j = i + k; j < argc; j++ )
48 argv[j - k] = argv[j];
57 char *gamedetect_argv_buffer[1024];
58 void gamedetect_found_game( char *game, char *path ){
62 if ( g_argv == gamedetect_argv_buffer ) {
66 globalOutputStream() << "Detected game " << game << " in " << path << "\n";
68 sprintf( buf, "-%s-EnginePath", game );
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;
77 memcpy( gamedetect_argv_buffer + 4, g_argv, sizeof( *gamedetect_argv_buffer ) * g_argc );
79 g_argv = gamedetect_argv_buffer;
82 bool gamedetect_check_game( char *gamefile, const char *checkfile1, const char *checkfile2, char *buf /* must have 64 bytes free after bufpos */, int bufpos ){
85 strcpy( buf + bufpos + 1, checkfile1 );
86 globalOutputStream() << "Checking for a game file in " << buf << "\n";
87 if ( !file_exists( buf ) ) {
92 strcpy( buf + bufpos + 1, checkfile2 );
93 globalOutputStream() << "Checking for a game file in " << buf << "\n";
94 if ( !file_exists( buf ) ) {
100 gamedetect_found_game( gamefile, buf );
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;
109 for ( i = 1; i < g_argc - 1; ++i )
110 if ( g_argv[i][0] == '-' ) {
111 if ( !strcmp( g_argv[i], "-gamedetect" ) ) {
112 nogamedetect = !strcmp( g_argv[i + 1], "false" );
116 if ( !nogamedetect ) {
117 static char buf[1024 + 64];
118 strncpy( buf, environment_get_app_path(), sizeof( buf ) );
119 buf[sizeof( buf ) - 1 - 64] = 0;
120 if ( !strlen( buf ) ) {
124 char *p = buf + strlen( buf ) - 1; // point directly on the slash of get_app_path
127 // TODO add more games to this
129 // try to detect Nexuiz installs
131 if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz.exe", buf, p - buf ) )
132 #elif defined( __APPLE__ )
133 if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "Nexuiz.app/Contents/Info.plist", buf, p - buf ) )
135 if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz-linux-glx.sh", buf, p - buf ) )
139 // try to detect Q2World installs
140 if ( gamedetect_check_game( "q2w.game", "default/quake2world.version", NULL, buf, p - buf ) ) {
144 // try to detect Warsow installs
145 if ( gamedetect_check_game( "warsow.game", "basewsw/dedicated_autoexec.cfg", NULL, buf, p - buf ) ) {
152 while ( p != buf && *p != '/' && *p != '\\' )
160 CopiedString home_path;
161 CopiedString app_path;
164 const char* environment_get_home_path(){
165 return home_path.c_str();
168 const char* environment_get_app_path(){
169 return app_path.c_str();
172 bool portable_app_setup(){
173 StringOutputStream confdir( 256 );
174 confdir << app_path.c_str() << "settings/";
175 if ( file_exists( confdir.c_str() ) ) {
176 home_path = confdir.c_str();
190 const char* LINK_NAME =
191 #if defined ( __linux__ )
193 #else // FreeBSD and OSX
198 /// brief Returns the filename of the executable belonging to the current process, or 0 if not found.
199 char* getexename( char *buf ){
200 /* Now read the symbolic link */
201 int ret = readlink( LINK_NAME, buf, PATH_MAX );
204 globalOutputStream() << "getexename: falling back to argv[0]: " << makeQuoted( g_argv[0] );
205 const char* path = realpath( g_argv[0], buf );
207 /* In case of an error, leave the handling up to the caller */
212 /* Ensure proper NUL termination */
215 /* delete the program name */
216 *( strrchr( buf, '/' ) ) = '\0';
218 // NOTE: we build app path with a trailing '/'
219 // it's a general convention in Radiant to have the slash at the end of directories
220 if ( buf[strlen( buf ) - 1] != '/' ) {
227 void environment_init( int argc, char* argv[] ){
228 // Give away unnecessary root privileges.
229 // Important: must be done before calling gtk_init().
233 if ( geteuid() == 0 && ( loginname = getlogin() ) != 0 &&
234 ( pw = getpwnam( loginname ) ) != 0 ) {
235 setuid( pw->pw_uid );
238 args_init( argc, argv );
242 app_path = getexename( real );
243 ASSERT_MESSAGE( !string_empty( app_path.c_str() ), "failed to deduce app path" );
246 if ( !portable_app_setup() ) {
247 StringOutputStream home( 256 );
248 home << DirectoryCleaned( g_get_home_dir() ) << ".netradiant/";
249 Q_mkdir( home.c_str() );
250 home_path = home.c_str();
255 #elif defined( WIN32 )
259 void environment_init( int argc, char* argv[] ){
260 args_init( argc, argv );
263 // get path to the editor
264 char filename[MAX_PATH + 1];
265 GetModuleFileName( 0, filename, MAX_PATH );
266 char* last_separator = strrchr( filename, '\\' );
267 if ( last_separator != 0 ) {
268 *( last_separator + 1 ) = '\0';
274 StringOutputStream app( 256 );
275 app << PathCleaned( filename );
276 app_path = app.c_str();
279 if ( !portable_app_setup() ) {
280 char *appdata = getenv( "APPDATA" );
281 StringOutputStream home( 256 );
282 if ( !appdata || string_empty( appdata ) ) {
283 ERROR_MESSAGE( "Application Data folder not available.\n"
284 "Radiant will use C:\\ for user preferences.\n" );
289 home << PathCleaned( appdata );
291 home << "/NetRadiantSettings/";
292 Q_mkdir( home.c_str() );
293 home_path = home.c_str();
299 #error "unsupported platform"