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"
23 #include "globaldefs.h"
25 #include "stream/textstream.h"
26 #include "string/string.h"
27 #include "stream/stringstream.h"
28 #include "debugging/debugging.h"
36 void args_init( int argc, char const* argv[] ){
39 for ( i = 1; i < argc; i++ )
41 for ( k = i; k < argc; k++ )
50 for ( j = i + k; j < argc; j++ )
52 argv[j - k] = argv[j];
62 char const *gamedetect_argv_buffer[1024];
64 void gamedetect_found_game( char const *game, char *path ){
68 if ( g_argv == gamedetect_argv_buffer ) {
72 globalOutputStream() << "Detected game " << game << " in " << path << "\n";
74 sprintf( buf, "-%s-EnginePath", game );
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;
83 memcpy( gamedetect_argv_buffer + 4, g_argv, sizeof( *gamedetect_argv_buffer ) * g_argc );
85 g_argv = gamedetect_argv_buffer;
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 ){
91 strcpy( buf + bufpos + 1, checkfile1 );
92 globalOutputStream() << "Checking for a game file in " << buf << "\n";
93 if ( !file_exists( buf ) ) {
98 strcpy( buf + bufpos + 1, checkfile2 );
99 globalOutputStream() << "Checking for a game file in " << buf << "\n";
100 if ( !file_exists( buf ) ) {
106 gamedetect_found_game( gamefile, buf );
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;
115 for ( i = 1; i < g_argc - 1; ++i )
117 if ( g_argv[i][0] == '-' ) {
118 if ( !strcmp( g_argv[i], "-gamedetect" ) ) {
119 nogamedetect = !strcmp( g_argv[i + 1], "false" );
124 if ( !nogamedetect ) {
125 static char buf[1024 + 64];
126 strncpy( buf, environment_get_app_path(), sizeof( buf ) );
127 buf[sizeof( buf ) - 1 - 64] = 0;
128 if ( !strlen( buf ) ) {
132 char *p = buf + strlen( buf ) - 1; // point directly on the slash of get_app_path
135 // TODO add more games to this
137 // try to detect Nexuiz installs
139 if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz.exe", buf, p - buf ) )
141 if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "Nexuiz.app/Contents/Info.plist", buf, p - buf ) )
143 if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz-linux-glx.sh", buf, p - buf ) )
145 if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", NULL, buf, p - buf ) )
149 // try to detect Quetoo installs
150 if ( gamedetect_check_game( "quetoo.game", "default/icons/quetoo.png", NULL, buf, p - buf ) ) {
154 // try to detect Warsow installs
155 if ( gamedetect_check_game( "warsow.game", "basewsw/dedicated_autoexec.cfg", NULL, buf, p - buf ) ) {
162 while ( p != buf && *p != '/' && *p != '\\' )
170 // executable file path
171 CopiedString app_filepath;
173 CopiedString home_path;
174 CopiedString app_path;
175 CopiedString lib_path;
176 CopiedString data_path;
179 const char* environment_get_app_filepath(){
180 return app_filepath.c_str();
183 const char* environment_get_home_path(){
184 return home_path.c_str();
187 const char* environment_get_app_path(){
188 return app_path.c_str();
191 const char *environment_get_lib_path()
193 return lib_path.c_str();
196 const char *environment_get_data_path()
198 return data_path.c_str();
201 bool portable_app_setup(){
202 StringOutputStream confdir( 256 );
203 confdir << app_path.c_str() << "settings/";
204 if ( file_is_directory( confdir.c_str() ) ) {
205 home_path = confdir.c_str();
219 const char* LINK_NAME =
222 #else // FreeBSD and macOS
227 /// brief Returns the filename of the executable belonging to the current process, or 0 if not found.
228 char const* getexename( char *buf ){
229 /* Now read the symbolic link */
230 int ret = readlink( LINK_NAME, buf, PATH_MAX );
233 globalOutputStream() << "getexename: falling back to argv[0]: " << makeQuoted( g_argv[0] );
234 const char* path = realpath( g_argv[0], buf );
236 /* In case of an error, leave the handling up to the caller */
241 /* Ensure proper NUL termination */
246 char const* getexepath( char *buf ) {
247 /* delete the program name */
248 *( strrchr( buf, '/' ) ) = '\0';
250 // NOTE: we build app path with a trailing '/'
251 // it's a general convention in Radiant to have the slash at the end of directories
252 if ( buf[strlen( buf ) - 1] != '/' ) {
259 void environment_init( int argc, char const* argv[] ){
260 // Give away unnecessary root privileges.
261 // Important: must be done before calling gtk_init().
265 if ( geteuid() == 0 && ( loginname = getlogin() ) != 0 &&
266 ( pw = getpwnam( loginname ) ) != 0 ) {
267 setuid( pw->pw_uid );
270 args_init( argc, argv );
274 app_filepath = getexename( real );
275 ASSERT_MESSAGE( !string_empty( app_filepath.c_str() ), "failed to deduce app path" );
277 strncpy( real, app_filepath.c_str(), strlen( app_filepath.c_str() ) );
278 app_path = getexepath( real );
282 StringOutputStream buffer;
283 buffer << app_path.c_str() << "../lib/" << RADIANT_BASENAME << "/";
284 if ( file_is_directory( buffer.c_str() ) ) {
285 lib_path = buffer.c_str();
288 lib_path = app_path.c_str();
293 StringOutputStream buffer;
294 buffer << app_path.c_str() << "../share/" << RADIANT_BASENAME << "/";
295 if ( file_is_directory( buffer.c_str() ) ) {
296 data_path = buffer.c_str();
299 data_path = app_path.c_str();
303 if ( !portable_app_setup() ) {
304 // this is used on both Linux and macOS
305 // but a macOS specific code may be written instead
306 StringOutputStream home( 256 );
307 home << DirectoryCleaned(g_get_user_config_dir()) << "/" << RADIANT_BASENAME << "/";
308 // first create ~/.config
309 // since it may be missing on brand new home directory
310 Q_mkdir( g_get_user_config_dir() );
311 // then create ~/.config/netradiant
312 Q_mkdir( home.c_str() );
313 home_path = home.c_str();
318 #elif GDEF_OS_WINDOWS
322 void environment_init( int argc, char const* argv[] ){
323 args_init( argc, argv );
326 // get path to the editor
327 char filename[MAX_PATH + 1];
328 StringOutputStream app_filepath_stream( 256 );
329 StringOutputStream app_path_stream( 256 );
331 GetModuleFileName( 0, filename, MAX_PATH );
333 app_filepath_stream << PathCleaned( filename );
334 app_filepath = app_filepath_stream.c_str();
336 char* last_separator = strrchr( filename, '\\' );
337 if ( last_separator != 0 ) {
338 *( last_separator + 1 ) = '\0';
345 app_path_stream << PathCleaned( filename );
346 app_path = app_path_stream.c_str();
349 data_path = app_path;
352 if ( !portable_app_setup() ) {
353 char *appdata = getenv( "APPDATA" );
354 StringOutputStream home( 256 );
355 home << PathCleaned( appdata );
356 home << "/NetRadiantSettings/";
357 Q_mkdir( home.c_str() );
358 home_path = home.c_str();
364 #error "unsupported platform"