2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
22 /*! \mainpage GtkRadiant Documentation Index
24 \section intro_sec Introduction
26 This documentation is generated from comments in the source code.
28 \section links_sec Useful Links
30 \link include/itextstream.h include/itextstream.h \endlink - Global output and error message streams, similar to std::cout and std::cerr. \n
32 FileInputStream - similar to std::ifstream (binary mode) \n
33 FileOutputStream - similar to std::ofstream (binary mode) \n
34 TextFileInputStream - similar to std::ifstream (text mode) \n
35 TextFileOutputStream - similar to std::ofstream (text mode) \n
36 StringOutputStream - similar to std::stringstream \n
38 \link string/string.h string/string.h \endlink - C-style string comparison and memory management. \n
39 \link os/path.h os/path.h \endlink - Path manipulation for radiant's standard path format \n
40 \link os/file.h os/file.h \endlink - OS file-system access. \n
42 ::CopiedString - automatic string memory management \n
43 Array - automatic array memory management \n
44 HashTable - generic hashtable, similar to std::hash_map \n
46 \link math/vector.h math/vector.h \endlink - Vectors \n
47 \link math/matrix.h math/matrix.h \endlink - Matrices \n
48 \link math/quaternion.h math/quaternion.h \endlink - Quaternions \n
49 \link math/plane.h math/plane.h \endlink - Planes \n
50 \link math/aabb.h math/aabb.h \endlink - AABBs \n
52 Callback MemberCaller FunctionCaller - callbacks similar to using boost::function with boost::bind \n
53 SmartPointer SmartReference - smart-pointer and smart-reference similar to Loki's SmartPtr \n
55 \link generic/bitfield.h generic/bitfield.h \endlink - Type-safe bitfield \n
56 \link generic/enumeration.h generic/enumeration.h \endlink - Type-safe enumeration \n
58 DefaultAllocator - Memory allocation using new/delete, compliant with std::allocator interface \n
60 \link debugging/debugging.h debugging/debugging.h \endlink - Debugging macros \n
68 #include "debugging/debugging.h"
72 #include "uilib/uilib.h"
77 #include "stream/stringstream.h"
78 #include "stream/textfilestream.h"
80 #include "gtkutil/messagebox.h"
81 #include "gtkutil/image.h"
83 #include "texwindow.h"
85 #include "mainframe.h"
87 #include "preferences.h"
88 #include "environment.h"
89 #include "referencecache.h"
90 #include "stacktrace.h"
99 void error_redirect( const gchar *domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data ){
100 gboolean in_recursion;
104 in_recursion = ( log_level & G_LOG_FLAG_RECURSION ) != 0;
105 is_fatal = ( log_level & G_LOG_FLAG_FATAL ) != 0;
106 log_level = (GLogLevelFlags) ( log_level & G_LOG_LEVEL_MASK );
109 message = "(0) message";
113 strcpy( buf, domain );
122 case G_LOG_LEVEL_ERROR:
123 if ( in_recursion ) {
124 strcat( buf, "ERROR (recursed) **: " );
127 strcat( buf, "ERROR **: " );
130 case G_LOG_LEVEL_CRITICAL:
131 if ( in_recursion ) {
132 strcat( buf, "CRITICAL (recursed) **: " );
135 strcat( buf, "CRITICAL **: " );
138 case G_LOG_LEVEL_WARNING:
139 if ( in_recursion ) {
140 strcat( buf, "WARNING (recursed) **: " );
143 strcat( buf, "WARNING **: " );
146 case G_LOG_LEVEL_MESSAGE:
147 if ( in_recursion ) {
148 strcat( buf, "Message (recursed): " );
151 strcat( buf, "Message: " );
154 case G_LOG_LEVEL_INFO:
155 if ( in_recursion ) {
156 strcat( buf, "INFO (recursed): " );
159 strcat( buf, "INFO: " );
162 case G_LOG_LEVEL_DEBUG:
163 if ( in_recursion ) {
164 strcat( buf, "DEBUG (recursed): " );
167 strcat( buf, "DEBUG: " );
171 /* we are used for a log level that is not defined by GLib itself,
172 * try to make the best out of it.
174 if ( in_recursion ) {
175 strcat( buf, "LOG (recursed:" );
178 strcat( buf, "LOG (" );
181 gchar string[] = "0x00): ";
182 gchar *p = string + 2;
185 i = g_bit_nth_msf( log_level, -1 );
188 *p = '0' + ( i & 0xf );
193 strcat( buf, string );
196 strcat( buf, "): " );
200 strcat( buf, message );
202 strcat( buf, "\naborting...\n" );
209 globalErrorStream() << buf << "\n";
212 ERROR_MESSAGE( "GTK+ error: " << buf );
216 #if defined ( _DEBUG ) && defined ( WIN32 ) && defined ( _MSC_VER )
221 #if defined ( _DEBUG ) && defined ( WIN32 ) && defined ( _MSC_VER )
222 _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
230 Lock() : m_locked( false ){
238 bool locked() const {
247 ScopedLock( Lock& lock ) : m_lock( lock ){
255 class LineLimitedTextOutputStream : public TextOutputStream
257 TextOutputStream& outputStream;
260 LineLimitedTextOutputStream( TextOutputStream& outputStream, std::size_t count )
261 : outputStream( outputStream ), count( count ){
263 std::size_t write( const char* buffer, std::size_t length ){
265 const char* p = buffer;
266 const char* end = buffer + length;
269 p = std::find( p, end, '\n' );
274 if ( --count == 0 ) {
279 outputStream.write( buffer, length );
285 class PopupDebugMessageHandler : public DebugMessageHandler
287 StringOutputStream m_buffer;
290 TextOutputStream& getOutputStream(){
291 if ( !m_lock.locked() ) {
294 return globalErrorStream();
296 bool handleMessage(){
297 getOutputStream() << "----------------\n";
298 LineLimitedTextOutputStream outputStream( getOutputStream(), 24 );
299 write_stack_trace( outputStream );
300 getOutputStream() << "----------------\n";
301 globalErrorStream() << m_buffer.c_str();
302 if ( !m_lock.locked() ) {
303 ScopedLock lock( m_lock );
305 m_buffer << "Break into the debugger?\n";
306 bool handled = ui::root.alert( m_buffer.c_str(), "Radiant - Runtime Error", ui::alert_type::YESNO, ui::alert_icon::Error ) == ui::alert_response::NO;
310 m_buffer << "Please report this error to the developers\n";
311 ui::root.alert( m_buffer.c_str(), "Radiant - Runtime Error", ui::alert_type::OK, ui::alert_icon::Error );
319 typedef Static<PopupDebugMessageHandler> GlobalPopupDebugMessageHandler;
322 GlobalErrorStream::instance().setOutputStream( getSysPrintErrorStream() );
323 GlobalOutputStream::instance().setOutputStream( getSysPrintOutputStream() );
327 g_strSettingsPath = environment_get_home_path();
329 Q_mkdir( g_strSettingsPath.c_str() );
331 g_strAppPath = environment_get_app_path();
333 // radiant is installed in the parent dir of "tools/"
334 // NOTE: this is not very easy for debugging
335 // maybe add options to lookup in several places?
336 // (for now I had to create symlinks)
338 StringOutputStream path( 256 );
339 path << g_strAppPath.c_str() << "bitmaps/";
340 BitmapsPath_set( path.c_str() );
343 // we will set this right after the game selection is done
344 g_strGameToolsPath = g_strAppPath;
347 bool check_version_file( const char* filename, const char* version ){
348 TextFileInputStream file( filename );
349 if ( !file.failed() ) {
351 buf[file.read( buf, 9 )] = '\0';
353 // chomp it (the hard way)
355 while ( buf[chomp] >= '0' && buf[chomp] <= '9' )
359 return string_equal( buf, version );
364 bool check_version(){
365 // a safe check to avoid people running broken installations
366 // (otherwise, they run it, crash it, and blame us for not forcing them hard enough to pay attention while installing)
367 // make something idiot proof and someone will make better idiots, this may be overkill
368 // let's leave it disabled in debug mode in any case
369 // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=431
371 #define CHECK_VERSION
374 // locate and open RADIANT_MAJOR and RADIANT_MINOR
375 bool bVerIsGood = true;
377 StringOutputStream ver_file_name( 256 );
378 ver_file_name << AppPath_get() << "RADIANT_MAJOR";
379 bVerIsGood = check_version_file( ver_file_name.c_str(), RADIANT_MAJOR_VERSION );
382 StringOutputStream ver_file_name( 256 );
383 ver_file_name << AppPath_get() << "RADIANT_MINOR";
384 bVerIsGood = check_version_file( ver_file_name.c_str(), RADIANT_MINOR_VERSION );
388 StringOutputStream msg( 256 );
389 msg << "This editor binary (" RADIANT_VERSION ") doesn't match what the latest setup has configured in this directory\n"
390 "Make sure you run the right/latest editor binary you installed\n"
392 ui::root.alert( msg.c_str(), "Radiant", ui::alert_type::OK, ui::alert_icon::Default);
400 void create_global_pid(){
402 the global prefs loading / game selection dialog might fail for any reason we don't know about
403 we need to catch when it happens, to cleanup the stateful prefs which might be killing it
404 and to turn on console logging for lookup of the problem
405 this is the first part of the two step .pid system
406 http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=297
408 StringOutputStream g_pidFile( 256 ); ///< the global .pid file (only for global part of the startup)
410 g_pidFile << SettingsPath_get() << "radiant.pid";
413 pid = fopen( g_pidFile.c_str(), "r" );
417 if ( remove( g_pidFile.c_str() ) == -1 ) {
418 StringOutputStream msg( 256 );
419 msg << "WARNING: Could not delete " << g_pidFile.c_str();
420 ui::root.alert( msg.c_str(), "Radiant", ui::alert_type::OK, ui::alert_icon::Error );
423 // in debug, never prompt to clean registry, turn console logging auto after a failed start
424 #if !defined( _DEBUG )
425 StringOutputStream msg( 256 );
426 msg << "Radiant failed to start properly the last time it was run.\n"
427 "The failure may be related to current global preferences.\n"
428 "Do you want to reset global preferences to defaults?";
430 if ( ui::root.alert( msg.c_str(), "Radiant - Startup Failure", ui::alert_type::YESNO, ui::alert_icon::Question ) == ui::alert_response::YES ) {
431 g_GamesDialog.Reset();
435 msg << "Logging console output to " << SettingsPath_get() << "radiant.log\nRefer to the log if Radiant fails to start again.";
437 ui::root.alert( msg.c_str(), "Radiant - Console Log", ui::alert_type::OK );
440 // set without saving, the class is not in a coherent state yet
441 // just do the value change and call to start logging, CGamesDialog will pickup when relevant
442 g_GamesDialog.m_bForceLogConsole = true;
446 // create a primary .pid for global init run
447 pid = fopen( g_pidFile.c_str(), "w" );
453 void remove_global_pid(){
454 StringOutputStream g_pidFile( 256 );
455 g_pidFile << SettingsPath_get() << "radiant.pid";
458 if ( remove( g_pidFile.c_str() ) == -1 ) {
459 StringOutputStream msg( 256 );
460 msg << "WARNING: Could not delete " << g_pidFile.c_str();
461 ui::root.alert( msg.c_str(), "Radiant", ui::alert_type::OK, ui::alert_icon::Error );
466 now the secondary game dependant .pid file
467 http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=297
469 void create_local_pid(){
470 StringOutputStream g_pidGameFile( 256 ); ///< the game-specific .pid file
471 g_pidGameFile << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << "/radiant-game.pid";
473 FILE *pid = fopen( g_pidGameFile.c_str(), "r" );
476 if ( remove( g_pidGameFile.c_str() ) == -1 ) {
477 StringOutputStream msg;
478 msg << "WARNING: Could not delete " << g_pidGameFile.c_str();
479 ui::root.alert( msg.c_str(), "Radiant", ui::alert_type::OK, ui::alert_icon::Error );
482 // in debug, never prompt to clean registry, turn console logging auto after a failed start
483 #if !defined( _DEBUG )
484 StringOutputStream msg;
485 msg << "Radiant failed to start properly the last time it was run.\n"
486 "The failure may be caused by current preferences.\n"
487 "Do you want to reset all preferences to defaults?";
489 if ( ui::root.alert( msg.c_str(), "Radiant - Startup Failure", ui::alert_type::YESNO, ui::alert_icon::Question ) == ui::alert_response::YES ) {
494 msg << "Logging console output to " << SettingsPath_get() << "radiant.log\nRefer to the log if Radiant fails to start again.";
496 ui::root.alert( msg.c_str(), "Radiant - Console Log", ui::alert_type::OK );
499 // force console logging on! (will go in prefs too)
500 g_GamesDialog.m_bForceLogConsole = true;
505 // create one, will remove right after entering message loop
506 pid = fopen( g_pidGameFile.c_str(), "w" );
515 now the secondary game dependant .pid file
516 http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=297
518 void remove_local_pid(){
519 StringOutputStream g_pidGameFile( 256 );
520 g_pidGameFile << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << "/radiant-game.pid";
521 remove( g_pidGameFile.c_str() );
524 void user_shortcuts_init(){
525 StringOutputStream path( 256 );
526 path << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << '/';
527 LoadCommandMap( path.c_str() );
528 SaveCommandMap( path.c_str() );
531 void user_shortcuts_save(){
532 StringOutputStream path( 256 );
533 path << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << '/';
534 SaveCommandMap( path.c_str() );
537 int main( int argc, char* argv[] ){
544 lib = LoadLibrary( "dwmapi.dll" );
546 void ( WINAPI *qDwmEnableComposition )( bool bEnable ) = ( void (WINAPI *) ( bool bEnable ) )GetProcAddress( lib, "DwmEnableComposition" );
547 if ( qDwmEnableComposition ) {
548 qDwmEnableComposition( FALSE );
554 const char* mapname = NULL;
555 char const *error = NULL;
556 if ( !ui::init( &argc, &argv, "<filename.map>", &error) ) {
557 g_print( "%s\n", error );
561 // Gtk already removed parsed `--options`
563 if ( strlen( argv[1] ) > 1 ) {
564 if ( g_str_has_suffix( argv[1], ".map" ) ) {
565 if ( g_path_is_absolute( argv[1] ) ) {
569 mapname = g_build_filename( g_get_current_dir(), argv[1], NULL );
573 g_print( "bad file name, will not load: %s\n", argv[1] );
578 g_print ( "%s\n", "too many arguments" );
582 // redirect Gtk warnings to the console
583 g_log_set_handler( "Gdk", (GLogLevelFlags)( G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING |
584 G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION ), error_redirect, 0 );
585 g_log_set_handler( "Gtk", (GLogLevelFlags)( G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING |
586 G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION ), error_redirect, 0 );
587 g_log_set_handler( "GtkGLExt", (GLogLevelFlags)( G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING |
588 G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION ), error_redirect, 0 );
589 g_log_set_handler( "GLib", (GLogLevelFlags)( G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING |
590 G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION ), error_redirect, 0 );
591 g_log_set_handler( 0, (GLogLevelFlags)( G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING |
592 G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION ), error_redirect, 0 );
594 GlobalDebugMessageHandler::instance().setHandler( GlobalPopupDebugMessageHandler::instance() );
596 environment_init(argc, (char const **) argv);
600 if ( !check_version() ) {
608 GlobalPreferences_Init();
610 g_GamesDialog.Init();
612 g_strGameToolsPath = g_pGameDescription->mGameToolsPath;
616 g_Preferences.Init(); // must occur before create_local_pid() to allow preferences to be reset
620 // in a very particular post-.pid startup
621 // we may have the console turned on and want to keep it that way
622 // so we use a latching system
623 if ( g_GamesDialog.m_bForceLogConsole ) {
625 g_Console_enableLogging = true;
626 g_GamesDialog.m_bForceLogConsole = false;
630 Radiant_Initialise();
632 user_shortcuts_init();
635 g_pParentWnd = new MainFrame();
639 if ( mapname != NULL ) {
640 Map_LoadFile( mapname );
642 else if ( g_bLoadLastMap && !g_strLastMap.empty() ) {
643 Map_LoadFile( g_strLastMap.c_str() );
650 // load up shaders now that we have the map loaded
652 TextureBrowser_ShowStartupShaders( GlobalTextureBrowser() );
659 // avoid saving prefs when the app is minimized
660 if ( g_pParentWnd->IsSleeping() ) {
661 globalOutputStream() << "Shutdown while sleeping, not saving prefs\n";
662 g_preferences_globals.disable_ini = true;
667 if ( !Map_Unnamed( g_map ) ) {
668 g_strLastMap = Map_Name( g_map );
673 user_shortcuts_save();
677 // close the log file if any
678 Sys_LogFile( false );