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