]> icculus.org git repositories - divverent/netradiant.git/blob - radiant/environment.cpp
compile fix ;)
[divverent/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
24 #include "stream/textstream.h"
25 #include "string/string.h"
26 #include "stream/stringstream.h"
27 #include "debugging/debugging.h"
28 #include "os/path.h"
29 #include "os/file.h"
30 #include "cmdlib.h"
31
32 int g_argc;
33 char** g_argv;
34
35 void args_init(int argc, char* argv[])
36 {
37   int i, j, k;
38
39   for (i = 1; i < argc; i++)
40   {
41     for (k = i; k < argc; k++)
42       if (argv[k] != 0)
43         break;
44
45     if (k > i)
46     {
47       k -= i;
48       for (j = i + k; j < argc; j++)
49         argv[j-k] = argv[j];
50       argc -= k;
51     }
52   }
53
54   g_argc = argc;
55   g_argv = argv;
56 }
57
58 char *gamedetect_argv_buffer[1024];
59 void gamedetect_found_game(char *game, char *path)
60 {
61   int argc;
62   static char buf[128];
63
64   if(g_argv == gamedetect_argv_buffer)
65     return;
66
67   globalOutputStream() << "Detected game " << game << " in " << path << "\n";
68
69   sprintf(buf, "-%s-EnginePath", game);
70   argc = 0;
71   gamedetect_argv_buffer[argc++] = "-global-gamefile";
72   gamedetect_argv_buffer[argc++] = game;
73   gamedetect_argv_buffer[argc++] = buf;
74   gamedetect_argv_buffer[argc++] = path;
75   if((size_t) (argc + g_argc) >= sizeof(gamedetect_argv_buffer) / sizeof(*gamedetect_argv_buffer) - 1)
76     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);
78   g_argc += argc;
79   g_argv = gamedetect_argv_buffer;
80 }
81
82 void gamedetect()
83 {
84   // if we're inside a Nexuiz install
85   // default to nexuiz.game (unless the user used an option to inhibit this)
86   bool nogamedetect = false;
87   int i;
88   for(i = 1; i < g_argc - 1; ++i)
89     if(g_argv[i][0] == '-')
90         {
91       if(!strcmp(g_argv[i], "-gamedetect"))
92             nogamedetect = !strcmp(g_argv[i+1], "false");
93           ++i;
94         }
95   if(!nogamedetect)
96   {
97         static char buf[1024 + 64];
98         strncpy(buf, environment_get_app_path(), sizeof(buf));
99         buf[sizeof(buf) - 1 - 64] = 0;
100         if(!strlen(buf))
101           return;
102
103         char *p = buf + strlen(buf) - 1; // point directly on the slash of get_app_path
104         while(p != buf)
105         {
106           // TODO add more games to this
107           // try to detect Nexuiz installs
108           strcpy(p, "/data/common-spog.pk3");
109           globalOutputStream() << "Checking for a game file in " << buf << "\n";
110           if(file_exists(buf))
111           {
112 #if defined(WIN32)
113             strcpy(p, "/nexuiz.exe");
114 #elif defined(__APPLE__)
115             strcpy(p, "/Nexuiz.app/Contents/Info.plist");
116 #else
117             strcpy(p, "/nexuiz-linux-glx.sh");
118 #endif
119                 if(file_exists(buf))
120                 {
121                   p[1] = 0;
122                   gamedetect_found_game("nexuiz.game", buf);
123                   return;
124                 }
125       }
126
127           // we found nothing
128           // go backwards
129           --p;
130           while(p != buf && *p != '/' && *p != '\\')
131             --p;
132         }
133   }
134 }
135
136 namespace
137 {
138   CopiedString home_path;
139   CopiedString app_path;
140 }
141
142 const char* environment_get_home_path()
143 {
144   return home_path.c_str();
145 }
146
147 const char* environment_get_app_path()
148 {
149   return app_path.c_str();
150 }
151
152
153 #if defined(POSIX)
154
155 #include <stdlib.h>
156 #include <pwd.h>
157 #include <unistd.h> 
158
159 #include <glib/gutils.h>
160
161 const char* LINK_NAME =
162 #if defined (__linux__)
163   "/proc/self/exe"
164 #else // FreeBSD and OSX
165   "/proc/curproc/file"
166 #endif
167 ;
168
169 /// brief Returns the filename of the executable belonging to the current process, or 0 if not found.
170 char* getexename(char *buf)
171 {
172   /* Now read the symbolic link */
173   int ret = readlink(LINK_NAME, buf, PATH_MAX);
174
175   if(ret == -1)
176   {
177     globalOutputStream() << "getexename: falling back to argv[0]: " << makeQuoted(g_argv[0]);
178     const char* path = realpath(g_argv[0], buf);
179     if(path == 0)
180     {
181       /* In case of an error, leave the handling up to the caller */
182       return "";
183     }
184   }
185
186   /* Ensure proper NUL termination */
187   buf[ret] = 0;
188
189   /* delete the program name */
190   *(strrchr(buf, '/')) = '\0';
191
192   // NOTE: we build app path with a trailing '/'
193   // it's a general convention in Radiant to have the slash at the end of directories
194   if (buf[strlen(buf)-1] != '/')
195   {
196     strcat(buf, "/");
197   }
198
199   return buf;
200 }
201
202 void environment_init(int argc, char* argv[])
203 {
204   // Give away unnecessary root privileges.
205   // Important: must be done before calling gtk_init().
206   char *loginname;
207   struct passwd *pw;
208   seteuid(getuid());
209   if (geteuid() == 0 && (loginname = getlogin()) != 0 &&
210       (pw = getpwnam(loginname)) != 0)
211     setuid(pw->pw_uid);
212
213   args_init(argc, argv);
214
215   {
216     StringOutputStream home(256);
217     home << DirectoryCleaned(g_get_home_dir()) << ".netradiant/";
218     Q_mkdir(home.c_str());
219     home_path = home.c_str();
220   }
221   {
222     char real[PATH_MAX];
223     app_path = getexename(real);
224     ASSERT_MESSAGE(!string_empty(app_path.c_str()), "failed to deduce app path");
225   }
226   gamedetect();
227 }
228
229 #elif defined(WIN32)
230
231 #include <windows.h>
232
233 void environment_init(int argc, char* argv[])
234 {
235   args_init(argc, argv);
236
237   {
238     char *appdata = getenv("APPDATA");
239
240     StringOutputStream home(256);
241     if(!appdata || string_empty(appdata))
242     {
243       ERROR_MESSAGE("Application Data folder not available.\n"
244         "Radiant will use C:\\ for user preferences.\n");
245       home << "C:";
246     }
247     else
248     {
249       home << PathCleaned(appdata);
250     }
251     home << "/NetRadiantSettings/";
252     Q_mkdir(home.c_str());
253     home_path = home.c_str();
254   }
255   {
256     // get path to the editor
257     char filename[MAX_PATH+1];
258     GetModuleFileName(0, filename, MAX_PATH);
259     char* last_separator = strrchr(filename, '\\');
260     if(last_separator != 0)
261     {
262       *(last_separator+1) = '\0';
263     }
264     else
265     {
266       filename[0] = '\0';
267     }
268     StringOutputStream app(256);
269     app << PathCleaned(filename);
270     app_path = app.c_str();
271   }
272   gamedetect();
273 }
274
275 #else
276 #error "unsupported platform"
277 #endif