]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/qe3.cpp
- Added LocalLcPath to plugin interface
[xonotic/netradiant.git] / radiant / qe3.cpp
1 /*
2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
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 /*
23 The following source code is licensed by Id Software and subject to the terms of 
24 its LIMITED USE SOFTWARE LICENSE AGREEMENT, a copy of which is included with 
25 GtkRadiant. If you did not receive a LIMITED USE SOFTWARE LICENSE AGREEMENT, 
26 please contact Id Software immediately at info@idsoftware.com.
27 */
28
29 //
30 // Linux stuff
31 //
32 // Leonardo Zide (leo@lokigames.com)
33 //
34
35 #include "qe3.h"
36
37 #include "debugging/debugging.h"
38
39 #include "ifilesystem.h"
40 //#include "imap.h"
41
42 #include <map>
43
44 #include <gtk/gtktearoffmenuitem.h>
45
46 #include "stream/textfilestream.h"
47 #include "cmdlib.h"
48 #include "stream/stringstream.h"
49 #include "os/path.h"
50 #include "scenelib.h"
51
52 #include "gtkutil/messagebox.h"
53 #include "error.h"
54 #include "map.h"
55 #include "build.h"
56 #include "points.h"
57 #include "camwindow.h"
58 #include "mainframe.h"
59 #include "preferences.h"
60 #include "watchbsp.h"
61 #include "autosave.h"
62 #include "convert.h"
63
64 QEGlobals_t  g_qeglobals;
65
66
67 #if defined(WIN32)
68 #define PATH_MAX 260
69 #endif
70
71
72 void QE_InitVFS()
73 {
74   // VFS initialization -----------------------
75   // we will call GlobalFileSystem().initDirectory, giving the directories to look in (for files in pk3's and for standalone files)
76   // we need to call in order, the mod ones first, then the base ones .. they will be searched in this order
77   // *nix systems have a dual filesystem in ~/.q3a, which is searched first .. so we need to add that too
78
79   const char* gamename = gamename_get();
80   const char* basegame = basegame_get();
81 #if defined(POSIX)
82   const char* userRoot = g_qeglobals.m_userEnginePath.c_str();
83 #endif
84   const char* globalRoot = EnginePath_get();
85
86   // if we have a mod dir
87   if(!string_equal(gamename, basegame))
88   {
89 #if defined(POSIX)
90     // ~/.<gameprefix>/<fs_game>
91     {
92       StringOutputStream userGamePath(256);
93       userGamePath << userRoot << gamename << '/';
94       GlobalFileSystem().initDirectory(userGamePath.c_str());
95     }
96 #endif
97
98     // <fs_basepath>/<fs_game>
99     {
100       StringOutputStream globalGamePath(256);
101       globalGamePath << globalRoot << gamename << '/';
102       GlobalFileSystem().initDirectory(globalGamePath.c_str());
103     }
104   }
105
106 #if defined(POSIX)
107   // ~/.<gameprefix>/<fs_main>
108   {
109     StringOutputStream userBasePath(256);
110     userBasePath << userRoot << basegame << '/';
111     GlobalFileSystem().initDirectory(userBasePath.c_str());
112   }
113 #endif
114
115   // <fs_basepath>/<fs_main>
116   {
117     StringOutputStream globalBasePath(256);
118     globalBasePath << globalRoot << basegame << '/';
119     GlobalFileSystem().initDirectory(globalBasePath.c_str());
120   }
121 }
122
123 int g_numbrushes = 0;
124 int g_numentities = 0;
125
126 void QE_UpdateStatusBar()
127 {
128   char buffer[128];
129   sprintf(buffer, "Brushes: %d Entities: %d", g_numbrushes, g_numentities);
130   g_pParentWnd->SetStatusText(g_pParentWnd->m_brushcount_status, buffer);
131 }
132
133 SimpleCounter g_brushCount;
134
135 void QE_brushCountChanged()
136 {
137   g_numbrushes = int(g_brushCount.get());
138   QE_UpdateStatusBar();
139 }
140
141 SimpleCounter g_entityCount;
142
143 void QE_entityCountChanged()
144 {
145   g_numentities = int(g_entityCount.get());
146   QE_UpdateStatusBar();
147 }
148
149 bool ConfirmModified(const char* title)
150 {
151   if (!Map_Modified(g_map))
152     return true;
153
154   EMessageBoxReturn result = gtk_MessageBox(GTK_WIDGET(MainFrame_getWindow()), "The current map has changed since it was last saved.\nDo you want to save the current map before continuing?", title, eMB_YESNOCANCEL, eMB_ICONQUESTION);
155   if(result == eIDCANCEL)
156   {
157     return false;
158   }
159   if(result == eIDYES)
160   {
161     if(Map_Unnamed(g_map))
162     {
163       return Map_SaveAs();
164     }
165     else
166     {
167       return Map_Save();
168     }
169   }
170   return true;
171 }
172
173
174 const char* const EXECUTABLE_TYPE = 
175 #if defined(__linux__) || defined (__FreeBSD__)
176 "x86"
177 #elif defined(__APPLE__)
178 "ppc"
179 #elif defined(WIN32)
180 "exe"
181 #else
182 #error "unknown platform"
183 #endif
184 ;
185
186 void bsp_init()
187 {
188   build_set_variable("RadiantPath", AppPath_get());
189   build_set_variable("ExecutableType", EXECUTABLE_TYPE);
190   build_set_variable("EnginePath", EnginePath_get());
191   build_set_variable("MonitorAddress", (g_WatchBSP_Enabled) ? "127.0.0.1:39000" : "");
192   build_set_variable("GameName", gamename_get());
193
194   build_set_variable("MapFile", Map_Name(g_map));
195 }
196
197 void bsp_shutdown()
198 {
199   build_clear_variables();
200 }
201
202 class ArrayCommandListener : public CommandListener
203 {
204   GPtrArray* m_array;
205 public:
206   ArrayCommandListener()
207   {
208     m_array = g_ptr_array_new();
209   }
210   ~ArrayCommandListener()
211   {
212     g_ptr_array_free(m_array, TRUE);
213   }
214
215   void execute(const char* command)
216   {
217     g_ptr_array_add(m_array, g_strdup(command));
218   }
219
220   GPtrArray* array() const
221   {
222     return m_array;
223   }
224 };
225
226 class BatchCommandListener : public CommandListener
227 {
228   TextOutputStream& m_file;
229   std::size_t m_commandCount;
230   const char* m_outputRedirect;
231 public:
232   BatchCommandListener(TextOutputStream& file, const char* outputRedirect) : m_file(file), m_commandCount(0), m_outputRedirect(outputRedirect)
233   {
234   }
235
236   void execute(const char* command)
237   {
238     m_file << command;
239     if (m_commandCount == 0)
240     {
241       m_file << " > ";
242     }
243     else
244     {
245       m_file << " >> ";
246     }
247     m_file << "\"" << m_outputRedirect << "\"";
248     m_file << "\n";
249     ++m_commandCount;
250   }
251 };
252
253 bool Region_cameraValid()
254 {
255   Vector3 vOrig(vector3_snapped(Camera_getOrigin(*g_pParentWnd->GetCamWnd())));
256
257   for (int i=0 ; i<3 ; i++)
258   {
259     if (vOrig[i] > region_maxs[i] || vOrig[i] < region_mins[i])
260     {
261       return false;
262     }
263   }
264   return true;
265 }
266
267
268 void RunBSP(const char* name)
269 {
270   // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=503
271   // make sure we don't attempt to region compile a map with the camera outside the region
272   if (region_active && !Region_cameraValid())
273   {
274     globalErrorStream() << "The camera must be in the region to start a region compile.\n";
275     return;
276   }
277
278   SaveMap();
279
280   if(Map_Unnamed(g_map))
281   {
282     globalOutputStream() << "build cancelled\n";
283     return;
284   }
285
286   if (g_SnapShots_Enabled && !Map_Unnamed(g_map) && Map_Modified(g_map))
287   {
288     Map_Snapshot();
289   }
290
291   if (region_active)
292   {
293     const char* mapname = Map_Name(g_map);
294     StringOutputStream name(256);
295     name << StringRange(mapname, path_get_filename_base_end(mapname)) << ".reg";
296     Map_SaveRegion(name.c_str());
297   }
298
299   Pointfile_Delete();
300
301   bsp_init();
302
303   if (g_WatchBSP_Enabled)
304   {
305     ArrayCommandListener listener;
306     build_run(name, listener);
307     // grab the file name for engine running
308     const char* fullname = Map_Name(g_map);
309     StringOutputStream bspname(64);
310     bspname << StringRange(path_get_filename_start(fullname), path_get_filename_base_end(fullname));
311     BuildMonitor_Run( listener.array(), bspname.c_str() );
312   }
313   else
314   {
315     char junkpath[PATH_MAX];
316     strcpy(junkpath, SettingsPath_get());
317     strcat(junkpath, "junk.txt");
318
319     char batpath[PATH_MAX];
320 #if defined(POSIX)
321     strcpy(batpath, SettingsPath_get());
322     strcat(batpath, "qe3bsp.sh");
323 #elif defined(WIN32)
324     strcpy(batpath, SettingsPath_get());
325     strcat(batpath, "qe3bsp.bat");
326 #else
327 #error "unsupported platform"
328 #endif
329     bool written = false;
330     {
331       TextFileOutputStream batchFile(batpath);
332       if(!batchFile.failed())
333       {
334 #if defined (POSIX)
335         batchFile << "#!/bin/sh \n\n";
336 #endif
337         BatchCommandListener listener(batchFile, junkpath);
338         build_run(name, listener);
339         written = true;
340       }
341     }
342     if(written)
343     {
344 #if defined (POSIX)
345       chmod (batpath, 0744);
346 #endif
347       globalOutputStream() << "Writing the compile script to '" << batpath << "'\n";
348       globalOutputStream() << "The build output will be saved in '" << junkpath << "'\n";
349       Q_Exec(batpath, NULL, NULL, true);
350     }
351   }
352
353   bsp_shutdown();
354 }
355
356 // =============================================================================
357 // Sys_ functions
358
359 void Sys_SetTitle(const char *text, bool modified)
360 {
361   StringOutputStream title;
362   title << ConvertLocaleToUTF8(text);
363
364   if(modified)
365   {
366     title << " *";
367   }
368
369   gtk_window_set_title(MainFrame_getWindow(), title.c_str());
370 }
371
372 bool g_bWaitCursor = false;
373
374 void Sys_BeginWait (void)
375 {
376   ScreenUpdates_Disable("Processing...", "Please Wait");
377   GdkCursor *cursor = gdk_cursor_new (GDK_WATCH);
378   gdk_window_set_cursor(GTK_WIDGET(MainFrame_getWindow())->window, cursor);
379   gdk_cursor_unref (cursor);
380   g_bWaitCursor = true;
381 }
382
383 void Sys_EndWait (void)
384 {
385   ScreenUpdates_Enable();
386   gdk_window_set_cursor(GTK_WIDGET(MainFrame_getWindow())->window, 0);
387   g_bWaitCursor = false;
388 }
389
390 void Sys_Beep (void)
391 {
392   gdk_beep();
393 }
394