]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/qe3.cpp
809df39ca4107dc7bd607124a8811ce86993bcec
[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 && !g_disableHomePath ) {
90                         StringOutputStream userGamePath( 256 );
91                         userGamePath << userRoot << gamename << '/';
92                         GlobalFileSystem().initDirectory( userGamePath.c_str() );
93                 }
94
95                 // <fs_basepath>/<fs_game>
96                 if ( !g_disableEnginePath ) {
97                         StringOutputStream globalGamePath( 256 );
98                         globalGamePath << globalRoot << gamename << '/';
99                         GlobalFileSystem().initDirectory( globalGamePath.c_str() );
100                 }
101         }
102
103         // ~/.<gameprefix>/<fs_main>
104         if ( userRoot && !g_disableHomePath ) {
105                 StringOutputStream userBasePath( 256 );
106                 userBasePath << userRoot << basegame << '/';
107                 GlobalFileSystem().initDirectory( userBasePath.c_str() );
108         }
109
110         // <fs_basepath>/<fs_main>
111         if ( !g_disableEnginePath ) {
112                 StringOutputStream globalBasePath( 256 );
113                 globalBasePath << globalRoot << basegame << '/';
114                 GlobalFileSystem().initDirectory( globalBasePath.c_str() );
115         }
116
117         // extra pakpaths
118         for ( int i = 0; i < g_pakPathCount; i++ ) {
119                 if (g_strcmp0( g_strPakPath[i].c_str(), "")) {
120                         GlobalFileSystem().initDirectory( g_strPakPath[i].c_str() );
121                 }
122         }
123 }
124
125 int g_numbrushes = 0;
126 int g_numentities = 0;
127
128 void QE_UpdateStatusBar(){
129         char buffer[128];
130         sprintf( buffer, "Brushes: %d Entities: %d", g_numbrushes, g_numentities );
131         g_pParentWnd->SetStatusText( g_pParentWnd->m_brushcount_status, buffer );
132 }
133
134 SimpleCounter g_brushCount;
135
136 void QE_brushCountChanged(){
137         g_numbrushes = int(g_brushCount.get() );
138         QE_UpdateStatusBar();
139 }
140
141 SimpleCounter g_entityCount;
142
143 void QE_entityCountChanged(){
144         g_numentities = int(g_entityCount.get() );
145         QE_UpdateStatusBar();
146 }
147
148 bool ConfirmModified( const char* title ){
149         if ( !Map_Modified( g_map ) ) {
150                 return true;
151         }
152
153         auto result = ui::alert( MainFrame_getWindow(), "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 );
154         if ( result == ui::alert_response::CANCEL ) {
155                 return false;
156         }
157         if ( result == ui::alert_response::YES ) {
158                 if ( Map_Unnamed( g_map ) ) {
159                         return Map_SaveAs();
160                 }
161                 else
162                 {
163                         return Map_Save();
164                 }
165         }
166         return true;
167 }
168
169 void bsp_init(){
170         build_set_variable( "RadiantPath", AppPath_get() );
171         build_set_variable( "ExecutableType", RADIANT_EXECUTABLE );
172         build_set_variable( "EnginePath", EnginePath_get() );
173         build_set_variable( "UserEnginePath", g_qeglobals.m_userEnginePath.c_str() );
174         build_set_variable( "MonitorAddress", ( g_WatchBSP_Enabled ) ? "127.0.0.1:39000" : "" );
175         build_set_variable( "GameName", gamename_get() );
176
177         StringBuffer ExtraQ3map2Args;
178         // extra pakpaths
179         for ( int i = 0; i < g_pakPathCount; i++ ) {
180                 if ( g_strcmp0( g_strPakPath[i].c_str(), "") ) {
181                         ExtraQ3map2Args.push_string( " -fs_pakpath \"" );
182                         ExtraQ3map2Args.push_string( g_strPakPath[i].c_str() );
183                         ExtraQ3map2Args.push_string( "\"" );
184                 }
185         }
186
187         // extra switches
188         if ( g_disableEnginePath ) {
189                 ExtraQ3map2Args.push_string( " -fs_nobasepath " );
190         }
191
192         if ( g_disableHomePath ) {
193                 ExtraQ3map2Args.push_string( " -fs_nohomepath " );
194         }
195
196         build_set_variable( "ExtraQ3map2Args", ExtraQ3map2Args.c_str() );
197
198         const char* mapname = Map_Name( g_map );
199         StringOutputStream name( 256 );
200         name << StringRange( mapname, path_get_filename_base_end( mapname ) ) << ".bsp";
201
202         build_set_variable( "MapFile", mapname );
203         build_set_variable( "BspFile", name.c_str() );
204 }
205
206 void bsp_shutdown(){
207         build_clear_variables();
208 }
209
210 class ArrayCommandListener : public CommandListener
211 {
212 GPtrArray* m_array;
213 public:
214 ArrayCommandListener(){
215         m_array = g_ptr_array_new();
216 }
217 ~ArrayCommandListener(){
218         g_ptr_array_free( m_array, TRUE );
219 }
220
221 void execute( const char* command ){
222         g_ptr_array_add( m_array, g_strdup( command ) );
223 }
224
225 GPtrArray* array() const {
226         return m_array;
227 }
228 };
229
230 class BatchCommandListener : public CommandListener
231 {
232 TextOutputStream& m_file;
233 std::size_t m_commandCount;
234 const char* m_outputRedirect;
235 public:
236 BatchCommandListener( TextOutputStream& file, const char* outputRedirect ) : m_file( file ), m_commandCount( 0 ), m_outputRedirect( outputRedirect ){
237 }
238
239 void execute( const char* command ){
240         m_file << command;
241         if ( m_commandCount == 0 ) {
242                 m_file << " > ";
243         }
244         else
245         {
246                 m_file << " >> ";
247         }
248         m_file << "\"" << m_outputRedirect << "\"";
249         m_file << "\n";
250         ++m_commandCount;
251 }
252 };
253
254 bool Region_cameraValid(){
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                         return false;
261                 }
262         }
263         return true;
264 }
265
266
267 void RunBSP( const char* name ){
268         // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=503
269         // make sure we don't attempt to region compile a map with the camera outside the region
270         if ( region_active && !Region_cameraValid() ) {
271                 globalErrorStream() << "The camera must be in the region to start a region compile.\n";
272                 return;
273         }
274
275         SaveMap();
276
277         if ( Map_Unnamed( g_map ) ) {
278                 globalOutputStream() << "build cancelled\n";
279                 return;
280         }
281
282         if ( g_SnapShots_Enabled && !Map_Unnamed( g_map ) && Map_Modified( g_map ) ) {
283                 Map_Snapshot();
284         }
285
286         if ( region_active ) {
287                 const char* mapname = Map_Name( g_map );
288                 StringOutputStream name( 256 );
289                 name << StringRange( mapname, path_get_filename_base_end( mapname ) ) << ".reg";
290                 Map_SaveRegion( name.c_str() );
291         }
292
293         Pointfile_Delete();
294
295         bsp_init();
296
297         if ( g_WatchBSP_Enabled ) {
298                 ArrayCommandListener listener;
299                 build_run( name, listener );
300                 // grab the file name for engine running
301                 const char* fullname = Map_Name( g_map );
302                 StringOutputStream bspname( 64 );
303                 bspname << StringRange( path_get_filename_start( fullname ), path_get_filename_base_end( fullname ) );
304                 BuildMonitor_Run( listener.array(), bspname.c_str() );
305         }
306         else
307         {
308                 char junkpath[PATH_MAX];
309                 strcpy( junkpath, SettingsPath_get() );
310                 strcat( junkpath, "junk.txt" );
311
312                 char batpath[PATH_MAX];
313 #if GDEF_OS_POSIX
314                 strcpy( batpath, SettingsPath_get() );
315                 strcat( batpath, "qe3bsp.sh" );
316 #elif GDEF_OS_WINDOWS
317                 strcpy( batpath, SettingsPath_get() );
318                 strcat( batpath, "qe3bsp.bat" );
319 #else
320 #error "unsupported platform"
321 #endif
322                 bool written = false;
323                 {
324                         TextFileOutputStream batchFile( batpath );
325                         if ( !batchFile.failed() ) {
326 #if GDEF_OS_POSIX
327                                 batchFile << "#!/bin/sh \n\n";
328 #endif
329                                 BatchCommandListener listener( batchFile, junkpath );
330                                 build_run( name, listener );
331                                 written = true;
332                         }
333                 }
334                 if ( written ) {
335 #if GDEF_OS_POSIX
336                         chmod( batpath, 0744 );
337 #endif
338                         globalOutputStream() << "Writing the compile script to '" << batpath << "'\n";
339                         globalOutputStream() << "The build output will be saved in '" << junkpath << "'\n";
340                         Q_Exec( batpath, NULL, NULL, true, false );
341                 }
342         }
343
344         bsp_shutdown();
345 }
346
347 // =============================================================================
348 // Sys_ functions
349
350 void Sys_SetTitle( const char *text, bool modified ){
351         StringOutputStream title;
352         title << text;
353
354         if ( modified ) {
355                 title << " *";
356         }
357
358         gtk_window_set_title(MainFrame_getWindow(), title.c_str() );
359 }
360
361 bool g_bWaitCursor = false;
362
363 void Sys_BeginWait( void ){
364         ScreenUpdates_Disable( "Processing...", "Please Wait" );
365         GdkCursor *cursor = gdk_cursor_new( GDK_WATCH );
366         gdk_window_set_cursor( gtk_widget_get_window(MainFrame_getWindow()), cursor );
367         gdk_cursor_unref( cursor );
368         g_bWaitCursor = true;
369 }
370
371 void Sys_EndWait( void ){
372         ScreenUpdates_Enable();
373         gdk_window_set_cursor(gtk_widget_get_window(MainFrame_getWindow()), 0 );
374         g_bWaitCursor = false;
375 }
376
377 void Sys_Beep( void ){
378         gdk_beep();
379 }