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