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;
114 bool nogamedetect = true;
116 for ( i = 1; i < g_argc - 1; ++i )
118 if ( g_argv[i][0] == '-' ) {
119 if ( !strcmp( g_argv[i], "-gamedetect" ) ) {
120 nogamedetect = !strcmp( g_argv[i + 1], "false" );
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 ) ) {
133 char *p = buf + strlen( buf ) - 1; // point directly on the slash of get_app_path
136 // TODO add more games to this
138 // try to detect Nexuiz installs
140 if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz.exe", buf, p - buf ) )
142 if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "Nexuiz.app/Contents/Info.plist", buf, p - buf ) )
144 if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz-linux-glx.sh", buf, p - buf ) )
146 if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", NULL, buf, p - buf ) )
150 // try to detect Quetoo installs
151 if ( gamedetect_check_game( "quetoo.game", "default/icons/quetoo.png", NULL, buf, p - buf ) ) {
155 // try to detect Warsow installs
156 if ( gamedetect_check_game( "warsow.game", "basewsw/dedicated_autoexec.cfg", NULL, buf, p - buf ) ) {
163 while ( p != buf && *p != '/' && *p != '\\' )
171 // executable file path
172 CopiedString app_filepath;
174 CopiedString home_path;
175 CopiedString app_path;
176 CopiedString lib_path;
177 CopiedString data_path;
180 const char* environment_get_app_filepath(){
181 return app_filepath.c_str();
184 const char* environment_get_home_path(){
185 return home_path.c_str();
188 const char* environment_get_app_path(){
189 return app_path.c_str();
192 const char *environment_get_lib_path()
194 return lib_path.c_str();
197 const char *environment_get_data_path()
199 return data_path.c_str();
202 bool portable_app_setup(){
203 StringOutputStream confdir( 256 );
204 confdir << app_path.c_str() << "settings/";
205 if ( file_is_directory( confdir.c_str() ) ) {
206 home_path = confdir.c_str();
220 const char* LINK_NAME =
223 #else // FreeBSD and macOS
228 /// brief Returns the filename of the executable belonging to the current process, or empty string if not found.
229 char const* getexename( char *buf ){
230 /* Now read the symbolic link */
231 const int ret = readlink( LINK_NAME, buf, PATH_MAX );
234 globalOutputStream() << "getexename: falling back to argv[0]: " << makeQuoted( g_argv[0] );
235 if( realpath( g_argv[0], buf ) == 0 ) {
236 /* In case of an error, leave the handling up to the caller */
241 /* Ensure proper NUL termination */
248 char const* getexepath( char *buf ) {
249 /* delete the program name */
250 *( strrchr( buf, '/' ) ) = '\0';
252 // NOTE: we build app path with a trailing '/'
253 // it's a general convention in Radiant to have the slash at the end of directories
254 if ( buf[strlen( buf ) - 1] != '/' ) {
261 void environment_init( int argc, char const* argv[] ){
262 // Give away unnecessary root privileges.
263 // Important: must be done before calling gtk_init().
267 if ( geteuid() == 0 && ( loginname = getlogin() ) != 0 &&
268 ( pw = getpwnam( loginname ) ) != 0 ) {
269 setuid( pw->pw_uid );
272 args_init( argc, argv );
276 app_filepath = getexename( real );
277 ASSERT_MESSAGE( !string_empty( app_filepath.c_str() ), "failed to deduce app path" );
279 strncpy( real, app_filepath.c_str(), strlen( app_filepath.c_str() ) );
280 app_path = getexepath( real );
284 #if defined(RADIANT_FHS_INSTALL)
285 StringOutputStream buffer;
286 #if defined(RADIANT_ADDONS_DIR)
287 buffer << RADIANT_ADDONS_DIR << "/";
289 buffer << app_path.c_str() << "../lib/";
290 buffer << RADIANT_LIB_ARCH << "/";
291 buffer << RADIANT_BASENAME << "/";
293 lib_path = buffer.c_str();
295 lib_path = app_path.c_str();
300 #if defined(RADIANT_FHS_INSTALL)
301 StringOutputStream buffer;
302 #if defined(RADIANT_DATA_DIR)
303 buffer << RADIANT_DATA_DIR << "/";
305 buffer << app_path.c_str() << "../share/";
306 buffer << RADIANT_BASENAME << "/";
308 data_path = buffer.c_str();
310 data_path = app_path.c_str();
314 if ( !portable_app_setup() ) {
315 StringOutputStream home( 256 );
317 /* This is used on macOS, this will produce
318 ~/Library/Application Support/NetRadiant folder. */
319 home << DirectoryCleaned( g_get_home_dir() );
320 Q_mkdir( home.c_str() );
322 Q_mkdir( home.c_str() );
323 home << "Application Support/";
324 Q_mkdir( home.c_str() );
325 home << RADIANT_NAME << "/";
326 #else // if GDEF_OS_XDG
327 /* This is used on both Linux and FreeBSD,
328 this will produce ~/.config/netradiant folder
329 when environment has default settings, the
330 XDG_CONFIG_HOME variable modifies it. */
331 home << DirectoryCleaned( g_get_user_config_dir() );
332 Q_mkdir( home.c_str() );
333 home << RADIANT_BASENAME << "/";
334 #endif // ! GDEF_OS_MACOS
335 Q_mkdir( home.c_str() );
336 home_path = home.c_str();
342 #elif GDEF_OS_WINDOWS
346 void environment_init( int argc, char const* argv[] ){
347 args_init( argc, argv );
350 // get path to the editor
351 char filename[MAX_PATH + 1];
352 StringOutputStream app_filepath_stream( 256 );
353 StringOutputStream app_path_stream( 256 );
355 GetModuleFileName( 0, filename, MAX_PATH );
357 app_filepath_stream << PathCleaned( filename );
358 app_filepath = app_filepath_stream.c_str();
360 char* last_separator = strrchr( filename, '\\' );
361 if ( last_separator != 0 ) {
362 *( last_separator + 1 ) = '\0';
369 app_path_stream << PathCleaned( filename );
370 app_path = app_path_stream.c_str();
373 data_path = app_path;
376 if ( !portable_app_setup() ) {
377 char *appdata = getenv( "APPDATA" );
379 StringOutputStream home( 256 );
380 home << PathCleaned( appdata );
382 home << RADIANT_NAME;
385 Q_mkdir( home.c_str() );
386 home_path = home.c_str();
392 #error "unsupported platform"