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