]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/environment.cpp
Merge commit 'a8f1237884e3741541e891724fbae07f8ff7d3b8' into master-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 #elif GDEF_OS_LINUX
144                         if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz-linux-glx.sh", buf, p - buf ) )
145 #else
146                         if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", NULL, buf, p - buf ) )
147 #endif
148                         { return; }
149
150                         // try to detect Quetoo installs
151                         if ( gamedetect_check_game( "quetoo.game", "default/icons/quetoo.png", NULL, buf, p - buf ) ) {
152                                 return;
153                         }
154
155                         // try to detect Warsow installs
156                         if ( gamedetect_check_game( "warsow.game", "basewsw/dedicated_autoexec.cfg", NULL, buf, p - buf ) ) {
157                                 return;
158                         }
159
160                         // we found nothing
161                         // go backwards
162                         --p;
163                         while ( p != buf && *p != '/' && *p != '\\' )
164                                 --p;
165                 }
166         }
167 }
168
169 namespace
170 {
171         // executable file path
172         CopiedString app_filepath;
173         // directory paths
174         CopiedString home_path;
175         CopiedString app_path;
176         CopiedString lib_path;
177         CopiedString data_path;
178 }
179
180 const char* environment_get_app_filepath(){
181         return app_filepath.c_str();
182 }
183
184 const char* environment_get_home_path(){
185         return home_path.c_str();
186 }
187
188 const char* environment_get_app_path(){
189         return app_path.c_str();
190 }
191
192 const char *environment_get_lib_path()
193 {
194         return lib_path.c_str();
195 }
196
197 const char *environment_get_data_path()
198 {
199         return data_path.c_str();
200 }
201
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();
207                 return true;
208         }
209         return false;
210 }
211
212
213 const char* openCmdMap;
214
215 void cmdMap(){
216         openCmdMap = NULL;
217         for ( int i = 1; i < g_argc; ++i )
218         {
219                 //if ( !stricmp( g_argv[i] + strlen(g_argv[i]) - 4, ".map" ) ){
220                 if( string_equal_suffix_nocase( g_argv[i], ".map" ) ){
221                         openCmdMap = g_argv[i];
222                 }
223         }
224 }
225
226 #if GDEF_OS_POSIX
227
228 #include <stdlib.h>
229 #include <pwd.h>
230 #include <unistd.h>
231
232 #include <glib.h>
233
234 const char* LINK_NAME =
235 #if GDEF_OS_LINUX
236         "/proc/self/exe"
237 #else // FreeBSD and macOS
238         "/proc/curproc/file"
239 #endif
240 ;
241
242 /// brief Returns the filename of the executable belonging to the current process, or empty string if not found.
243 char const* getexename( char *buf ){
244         /* Now read the symbolic link */
245         const int ret = readlink( LINK_NAME, buf, PATH_MAX );
246
247         if ( ret == -1 ) {
248                 globalOutputStream() << "getexename: falling back to argv[0]: " << makeQuoted( g_argv[0] );
249                 if( realpath( g_argv[0], buf ) == 0 ) {
250                         /* In case of an error, leave the handling up to the caller */
251                         *buf = '\0';
252                 }
253         }
254         else {
255                 /* Ensure proper NUL termination */
256                 buf[ret] = 0;
257         }
258
259         return buf;
260 }
261
262 char const* getexepath( char *buf ) {
263         /* delete the program name */
264         *( strrchr( buf, '/' ) ) = '\0';
265
266         // NOTE: we build app path with a trailing '/'
267         // it's a general convention in Radiant to have the slash at the end of directories
268         if ( buf[strlen( buf ) - 1] != '/' ) {
269                 strcat( buf, "/" );
270         }
271
272         return buf;
273 }
274
275 void environment_init( int argc, char const* argv[] ){
276         // Give away unnecessary root privileges.
277         // Important: must be done before calling gtk_init().
278         char *loginname;
279         struct passwd *pw;
280         seteuid( getuid() );
281         if ( geteuid() == 0 && ( loginname = getlogin() ) != 0 &&
282                  ( pw = getpwnam( loginname ) ) != 0 ) {
283                 setuid( pw->pw_uid );
284         }
285
286         args_init( argc, argv );
287
288         {
289                 char real[PATH_MAX];
290                 app_filepath = getexename( real );
291                 ASSERT_MESSAGE( !string_empty( app_filepath.c_str() ), "failed to deduce app path" );
292
293                 strncpy( real, app_filepath.c_str(), strlen( app_filepath.c_str() ) );
294                 app_path = getexepath( real );
295         }
296
297         {
298 #if defined(RADIANT_FHS_INSTALL)
299                 StringOutputStream buffer;
300         #if defined(RADIANT_ADDONS_DIR)
301                 buffer << RADIANT_ADDONS_DIR << "/";
302         #else
303                 buffer << app_path.c_str() << "../lib/";
304                 buffer << RADIANT_LIB_ARCH << "/";
305                 buffer << RADIANT_BASENAME << "/";
306         #endif
307                 lib_path = buffer.c_str();
308 #else
309                 lib_path = app_path.c_str();
310 #endif
311         }
312
313         {
314 #if defined(RADIANT_FHS_INSTALL)
315                 StringOutputStream buffer;
316         #if defined(RADIANT_DATA_DIR)
317                 buffer << RADIANT_DATA_DIR << "/";
318         #else
319                 buffer << app_path.c_str() << "../share/";
320                 buffer << RADIANT_BASENAME << "/";
321         #endif
322                 data_path = buffer.c_str();
323 #else
324                 data_path = app_path.c_str();
325 #endif
326         }
327
328         if ( !portable_app_setup() ) {
329                 StringOutputStream home( 256 );
330 #if GDEF_OS_MACOS
331                 /* This is used on macOS, this will produce
332                 ~/Library/Application Support/NetRadiant folder. */
333                 home << DirectoryCleaned( g_get_home_dir() );
334                 Q_mkdir( home.c_str() );
335                 home << "Library/";
336                 Q_mkdir( home.c_str() );
337                 home << "Application Support/";
338                 Q_mkdir( home.c_str() );
339                 home << RADIANT_NAME << "/";
340 #else // if GDEF_OS_XDG
341                 /* This is used on both Linux and FreeBSD,
342                 this will produce ~/.config/netradiant folder
343                 when environment has default settings, the
344                 XDG_CONFIG_HOME variable modifies it. */
345                 home << DirectoryCleaned( g_get_user_config_dir() );
346                 Q_mkdir( home.c_str() );
347                 home << RADIANT_BASENAME << "/";
348 #endif // ! GDEF_OS_MACOS
349                 Q_mkdir( home.c_str() );
350                 home_path = home.c_str();
351         }
352
353         gamedetect();
354         cmdMap();
355 }
356
357 #elif GDEF_OS_WINDOWS
358
359 #include <windows.h>
360
361 void environment_init( int argc, char const* argv[] ){
362         args_init( argc, argv );
363
364         {
365                 // get path to the editor
366                 char filename[MAX_PATH + 1];
367                 StringOutputStream app_filepath_stream( 256 );
368                 StringOutputStream app_path_stream( 256 );
369
370                 GetModuleFileName( 0, filename, MAX_PATH );
371                 
372                 app_filepath_stream << PathCleaned( filename );
373                 app_filepath = app_filepath_stream.c_str();
374
375                 char* last_separator = strrchr( filename, '\\' );
376                 if ( last_separator != 0 ) {
377                         *( last_separator + 1 ) = '\0';
378                 }
379                 else
380                 {
381                         filename[0] = '\0';
382                 }
383
384                 app_path_stream << PathCleaned( filename );
385                 app_path = app_path_stream.c_str();
386
387                 lib_path = app_path;
388                 data_path = app_path;
389         }
390
391         if ( !portable_app_setup() ) {
392                 char *appdata = getenv( "APPDATA" );
393
394                 StringOutputStream home( 256 );
395                 home << PathCleaned( appdata );
396                 home << "/";
397                 home << RADIANT_NAME;
398                 home << "/";
399
400                 Q_mkdir( home.c_str() );
401                 home_path = home.c_str();
402         }
403         gamedetect();
404         cmdMap();
405 }
406
407 #else
408 #error "unsupported platform"
409 #endif