]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/environment.cpp
import some improvements from garux tree
[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         int i;
115         for ( i = 1; i < g_argc - 1; ++i )
116         {
117                 if ( g_argv[i][0] == '-' ) {
118                         if ( !strcmp( g_argv[i], "-gamedetect" ) ) {
119                                 nogamedetect = !strcmp( g_argv[i + 1], "false" );
120                         }
121                         ++i;
122                 }
123         }
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 ) ) {
129                         return;
130                 }
131
132                 char *p = buf + strlen( buf ) - 1; // point directly on the slash of get_app_path
133                 while ( p != buf )
134                 {
135                         // TODO add more games to this
136
137                         // try to detect Nexuiz installs
138 #if GDEF_OS_WINDOWS
139                         if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz.exe", buf, p - buf ) )
140 #elif GDEF_OS_MACOS
141                         if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "Nexuiz.app/Contents/Info.plist", buf, p - buf ) )
142 #elif GDEF_OS_LINUX
143                         if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz-linux-glx.sh", buf, p - buf ) )
144 #else
145                         if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", NULL, buf, p - buf ) )
146 #endif
147                         { return; }
148
149                         // try to detect Quetoo installs
150                         if ( gamedetect_check_game( "quetoo.game", "default/icons/quetoo.png", NULL, buf, p - buf ) ) {
151                                 return;
152                         }
153
154                         // try to detect Warsow installs
155                         if ( gamedetect_check_game( "warsow.game", "basewsw/dedicated_autoexec.cfg", NULL, buf, p - buf ) ) {
156                                 return;
157                         }
158
159                         // we found nothing
160                         // go backwards
161                         --p;
162                         while ( p != buf && *p != '/' && *p != '\\' )
163                                 --p;
164                 }
165         }
166 }
167
168 namespace
169 {
170         // executable file path
171         CopiedString app_filepath;
172         // directory paths
173         CopiedString home_path;
174         CopiedString app_path;
175         CopiedString lib_path;
176         CopiedString data_path;
177 }
178
179 const char* environment_get_app_filepath(){
180         return app_filepath.c_str();
181 }
182
183 const char* environment_get_home_path(){
184         return home_path.c_str();
185 }
186
187 const char* environment_get_app_path(){
188         return app_path.c_str();
189 }
190
191 const char *environment_get_lib_path()
192 {
193         return lib_path.c_str();
194 }
195
196 const char *environment_get_data_path()
197 {
198         return data_path.c_str();
199 }
200
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();
206                 return true;
207         }
208         return false;
209 }
210
211 #if GDEF_OS_POSIX
212
213 #include <stdlib.h>
214 #include <pwd.h>
215 #include <unistd.h>
216
217 #include <glib.h>
218
219 const char* LINK_NAME =
220 #if GDEF_OS_LINUX
221         "/proc/self/exe"
222 #else // FreeBSD and macOS
223         "/proc/curproc/file"
224 #endif
225 ;
226
227 /// brief Returns the filename of the executable belonging to the current process, or empty string if not found.
228 char const* getexename( char *buf ){
229         /* Now read the symbolic link */
230         const int ret = readlink( LINK_NAME, buf, PATH_MAX );
231
232         if ( ret == -1 ) {
233                 globalOutputStream() << "getexename: falling back to argv[0]: " << makeQuoted( g_argv[0] );
234                 if( realpath( g_argv[0], buf ) == 0 ) {
235                         /* In case of an error, leave the handling up to the caller */
236                         *buf = '\0';
237                 }
238         }
239         else {
240                 /* Ensure proper NUL termination */
241                 buf[ret] = 0;
242         }
243
244         return buf;
245 }
246
247 char const* getexepath( char *buf ) {
248         /* delete the program name */
249         *( strrchr( buf, '/' ) ) = '\0';
250
251         // NOTE: we build app path with a trailing '/'
252         // it's a general convention in Radiant to have the slash at the end of directories
253         if ( buf[strlen( buf ) - 1] != '/' ) {
254                 strcat( buf, "/" );
255         }
256
257         return buf;
258 }
259
260 void environment_init( int argc, char const* argv[] ){
261         // Give away unnecessary root privileges.
262         // Important: must be done before calling gtk_init().
263         char *loginname;
264         struct passwd *pw;
265         seteuid( getuid() );
266         if ( geteuid() == 0 && ( loginname = getlogin() ) != 0 &&
267                  ( pw = getpwnam( loginname ) ) != 0 ) {
268                 setuid( pw->pw_uid );
269         }
270
271         args_init( argc, argv );
272
273         {
274                 char real[PATH_MAX];
275                 app_filepath = getexename( real );
276                 ASSERT_MESSAGE( !string_empty( app_filepath.c_str() ), "failed to deduce app path" );
277
278                 strncpy( real, app_filepath.c_str(), strlen( app_filepath.c_str() ) );
279                 app_path = getexepath( real );
280         }
281
282         {
283                 StringOutputStream buffer;
284                 buffer << app_path.c_str() << "../lib/" << RADIANT_BASENAME << "/";
285                 if ( file_is_directory( buffer.c_str() ) ) {
286                         lib_path = buffer.c_str();
287                 }
288                 else {
289                         lib_path = app_path.c_str();
290                 }
291         }
292
293         {
294                 StringOutputStream buffer;
295                 buffer << app_path.c_str() << "../share/" << RADIANT_BASENAME << "/";
296                 if ( file_is_directory( buffer.c_str() ) ) {
297                         data_path = buffer.c_str();
298                 }
299                 else {
300                         data_path = app_path.c_str();
301                 }
302         }
303
304         if ( !portable_app_setup() ) {
305                 // this is used on both Linux and macOS
306                 // but a macOS specific code may be written instead
307                 StringOutputStream home( 256 );
308                 home << DirectoryCleaned(g_get_user_config_dir()) << "/" << RADIANT_BASENAME << "/";
309                 // first create ~/.config
310                 // since it may be missing on brand new home directory
311                 Q_mkdir( g_get_user_config_dir() );
312                 // then create ~/.config/netradiant
313                 Q_mkdir( home.c_str() );
314                 home_path = home.c_str();
315         }
316         gamedetect();
317 }
318
319 #elif GDEF_OS_WINDOWS
320
321 #include <windows.h>
322
323 void environment_init( int argc, char const* argv[] ){
324         args_init( argc, argv );
325
326         {
327                 // get path to the editor
328                 char filename[MAX_PATH + 1];
329                 StringOutputStream app_filepath_stream( 256 );
330                 StringOutputStream app_path_stream( 256 );
331
332                 GetModuleFileName( 0, filename, MAX_PATH );
333                 
334                 app_filepath_stream << PathCleaned( filename );
335                 app_filepath = app_filepath_stream.c_str();
336
337                 char* last_separator = strrchr( filename, '\\' );
338                 if ( last_separator != 0 ) {
339                         *( last_separator + 1 ) = '\0';
340                 }
341                 else
342                 {
343                         filename[0] = '\0';
344                 }
345
346                 app_path_stream << PathCleaned( filename );
347                 app_path = app_path_stream.c_str();
348
349                 lib_path = app_path;
350                 data_path = app_path;
351         }
352
353         if ( !portable_app_setup() ) {
354                 char *appdata = getenv( "APPDATA" );
355                 StringOutputStream home( 256 );
356                 home << PathCleaned( appdata );
357                 home << "/NetRadiantSettings/";
358                 Q_mkdir( home.c_str() );
359                 home_path = home.c_str();
360         }
361         gamedetect();
362 }
363
364 #else
365 #error "unsupported platform"
366 #endif