]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/qe3.cpp
fix homedir version
[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         // VFS initialization -----------------------
74         // we will call GlobalFileSystem().initDirectory, giving the directories to look in (for files in pk3's and for standalone files)
75         // we need to call in order, the mod ones first, then the base ones .. they will be searched in this order
76         // *nix systems have a dual filesystem in ~/.q3a, which is searched first .. so we need to add that too
77
78         const char* gamename = gamename_get();
79         const char* basegame = basegame_get();
80         const char* userRoot = g_qeglobals.m_userEnginePath.c_str();
81         const char* globalRoot = EnginePath_get();
82
83         // if we have a mod dir
84         if ( !string_equal( gamename, basegame ) ) {
85                 // ~/.<gameprefix>/<fs_game>
86                 if ( userRoot ) {
87                         StringOutputStream userGamePath( 256 );
88                         userGamePath << userRoot << gamename << '/';
89                         GlobalFileSystem().initDirectory( userGamePath.c_str() );
90                 }
91
92                 // <fs_basepath>/<fs_game>
93                 {
94                         StringOutputStream globalGamePath( 256 );
95                         globalGamePath << globalRoot << gamename << '/';
96                         GlobalFileSystem().initDirectory( globalGamePath.c_str() );
97                 }
98         }
99
100         // ~/.<gameprefix>/<fs_main>
101         if ( userRoot ) {
102                 StringOutputStream userBasePath( 256 );
103                 userBasePath << userRoot << basegame << '/';
104                 GlobalFileSystem().initDirectory( userBasePath.c_str() );
105         }
106
107         // <fs_basepath>/<fs_main>
108         {
109                 StringOutputStream globalBasePath( 256 );
110                 globalBasePath << globalRoot << basegame << '/';
111                 GlobalFileSystem().initDirectory( globalBasePath.c_str() );
112         }
113 }
114
115 int g_numbrushes = 0;
116 int g_numentities = 0;
117
118 void QE_UpdateStatusBar(){
119         char buffer[128];
120         sprintf( buffer, "Brushes: %d Entities: %d", g_numbrushes, g_numentities );
121         g_pParentWnd->SetStatusText( g_pParentWnd->m_brushcount_status, buffer );
122 }
123
124 SimpleCounter g_brushCount;
125
126 void QE_brushCountChanged(){
127         g_numbrushes = int(g_brushCount.get() );
128         QE_UpdateStatusBar();
129 }
130
131 SimpleCounter g_entityCount;
132
133 void QE_entityCountChanged(){
134         g_numentities = int(g_entityCount.get() );
135         QE_UpdateStatusBar();
136 }
137
138 bool ConfirmModified( const char* title ){
139         if ( !Map_Modified( g_map ) ) {
140                 return true;
141         }
142
143         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 );
144         if ( result == eIDCANCEL ) {
145                 return false;
146         }
147         if ( result == eIDYES ) {
148                 if ( Map_Unnamed( g_map ) ) {
149                         return Map_SaveAs();
150                 }
151                 else
152                 {
153                         return Map_Save();
154                 }
155         }
156         return true;
157 }
158
159 void bsp_init(){
160         build_set_variable( "RadiantPath", AppPath_get() );
161         build_set_variable( "ExecutableType", RADIANT_EXECUTABLE );
162         build_set_variable( "EnginePath", EnginePath_get() );
163         build_set_variable( "UserEnginePath", g_qeglobals.m_userEnginePath.c_str() );
164         build_set_variable( "MonitorAddress", ( g_WatchBSP_Enabled ) ? "127.0.0.1:39000" : "" );
165         build_set_variable( "GameName", gamename_get() );
166
167         const char* mapname = Map_Name( g_map );
168         StringOutputStream name( 256 );
169         name << StringRange( mapname, path_get_filename_base_end( mapname ) ) << ".bsp";
170
171         build_set_variable( "MapFile", mapname );
172         build_set_variable( "BspFile", name.c_str() );
173 }
174
175 void bsp_shutdown(){
176         build_clear_variables();
177 }
178
179 class ArrayCommandListener : public CommandListener
180 {
181 GPtrArray* m_array;
182 public:
183 ArrayCommandListener(){
184         m_array = g_ptr_array_new();
185 }
186 ~ArrayCommandListener(){
187         g_ptr_array_free( m_array, TRUE );
188 }
189
190 void execute( const char* command ){
191         g_ptr_array_add( m_array, g_strdup( command ) );
192 }
193
194 GPtrArray* array() const {
195         return m_array;
196 }
197 };
198
199 class BatchCommandListener : public CommandListener
200 {
201 TextOutputStream& m_file;
202 std::size_t m_commandCount;
203 const char* m_outputRedirect;
204 public:
205 BatchCommandListener( TextOutputStream& file, const char* outputRedirect ) : m_file( file ), m_commandCount( 0 ), m_outputRedirect( outputRedirect ){
206 }
207
208 void execute( const char* command ){
209         m_file << command;
210         if ( m_commandCount == 0 ) {
211                 m_file << " > ";
212         }
213         else
214         {
215                 m_file << " >> ";
216         }
217         m_file << "\"" << m_outputRedirect << "\"";
218         m_file << "\n";
219         ++m_commandCount;
220 }
221 };
222
223 bool Region_cameraValid(){
224         Vector3 vOrig( vector3_snapped( Camera_getOrigin( *g_pParentWnd->GetCamWnd() ) ) );
225
226         for ( int i = 0 ; i < 3 ; i++ )
227         {
228                 if ( vOrig[i] > region_maxs[i] || vOrig[i] < region_mins[i] ) {
229                         return false;
230                 }
231         }
232         return true;
233 }
234
235
236 void RunBSP( const char* name ){
237         // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=503
238         // make sure we don't attempt to region compile a map with the camera outside the region
239         if ( region_active && !Region_cameraValid() ) {
240                 globalErrorStream() << "The camera must be in the region to start a region compile.\n";
241                 return;
242         }
243
244         SaveMap();
245
246         if ( Map_Unnamed( g_map ) ) {
247                 globalOutputStream() << "build cancelled\n";
248                 return;
249         }
250
251         if ( g_SnapShots_Enabled && !Map_Unnamed( g_map ) && Map_Modified( g_map ) ) {
252                 Map_Snapshot();
253         }
254
255         if ( region_active ) {
256                 const char* mapname = Map_Name( g_map );
257                 StringOutputStream name( 256 );
258                 name << StringRange( mapname, path_get_filename_base_end( mapname ) ) << ".reg";
259                 Map_SaveRegion( name.c_str() );
260         }
261
262         Pointfile_Delete();
263
264         bsp_init();
265
266         if ( g_WatchBSP_Enabled ) {
267                 ArrayCommandListener listener;
268                 build_run( name, listener );
269                 // grab the file name for engine running
270                 const char* fullname = Map_Name( g_map );
271                 StringOutputStream bspname( 64 );
272                 bspname << StringRange( path_get_filename_start( fullname ), path_get_filename_base_end( fullname ) );
273                 BuildMonitor_Run( listener.array(), bspname.c_str() );
274         }
275         else
276         {
277                 char junkpath[PATH_MAX];
278                 strcpy( junkpath, SettingsPath_get() );
279                 strcat( junkpath, "junk.txt" );
280
281                 char batpath[PATH_MAX];
282 #if defined( POSIX )
283                 strcpy( batpath, SettingsPath_get() );
284                 strcat( batpath, "qe3bsp.sh" );
285 #elif defined( WIN32 )
286                 strcpy( batpath, SettingsPath_get() );
287                 strcat( batpath, "qe3bsp.bat" );
288 #else
289 #error "unsupported platform"
290 #endif
291                 bool written = false;
292                 {
293                         TextFileOutputStream batchFile( batpath );
294                         if ( !batchFile.failed() ) {
295 #if defined ( POSIX )
296                                 batchFile << "#!/bin/sh \n\n";
297 #endif
298                                 BatchCommandListener listener( batchFile, junkpath );
299                                 build_run( name, listener );
300                                 written = true;
301                         }
302                 }
303                 if ( written ) {
304 #if defined ( POSIX )
305                         chmod( batpath, 0744 );
306 #endif
307                         globalOutputStream() << "Writing the compile script to '" << batpath << "'\n";
308                         globalOutputStream() << "The build output will be saved in '" << junkpath << "'\n";
309                         Q_Exec( batpath, NULL, NULL, true, false );
310                 }
311         }
312
313         bsp_shutdown();
314 }
315
316 // =============================================================================
317 // Sys_ functions
318
319 void Sys_SetTitle( const char *text, bool modified ){
320         StringOutputStream title;
321         title << text;
322
323         if ( modified ) {
324                 title << " *";
325         }
326
327         gtk_window_set_title( MainFrame_getWindow(), title.c_str() );
328 }
329
330 bool g_bWaitCursor = false;
331
332 void Sys_BeginWait( void ){
333         ScreenUpdates_Disable( "Processing...", "Please Wait" );
334         GdkCursor *cursor = gdk_cursor_new( GDK_WATCH );
335         gdk_window_set_cursor( GTK_WIDGET( MainFrame_getWindow() )->window, cursor );
336         gdk_cursor_unref( cursor );
337         g_bWaitCursor = true;
338 }
339
340 void Sys_EndWait( void ){
341         ScreenUpdates_Enable();
342         gdk_window_set_cursor( GTK_WIDGET( MainFrame_getWindow() )->window, 0 );
343         g_bWaitCursor = false;
344 }
345
346 void Sys_Beep( void ){
347         gdk_beep();
348 }