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 MemberCaller0 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
65 #include "globaldefs.h"
69 #include "debugging/debugging.h"
73 #include "uilib/uilib.h"
78 #include "stream/stringstream.h"
79 #include "stream/textfilestream.h"
81 #include "gtkutil/messagebox.h"
82 #include "gtkutil/image.h"
84 #include "texwindow.h"
86 #include "mainframe.h"
88 #include "preferences.h"
89 #include "environment.h"
90 #include "referencecache.h"
91 #include "stacktrace.h"
100 void error_redirect( const gchar *domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data ){
101 gboolean in_recursion;
105 in_recursion = ( log_level & G_LOG_FLAG_RECURSION ) != 0;
106 is_fatal = ( log_level & G_LOG_FLAG_FATAL ) != 0;
107 log_level = (GLogLevelFlags) ( log_level & G_LOG_LEVEL_MASK );
110 message = "(0) message";
114 strcpy( buf, domain );
123 case G_LOG_LEVEL_ERROR:
124 if ( in_recursion ) {
125 strcat( buf, "ERROR (recursed) **: " );
128 strcat( buf, "ERROR **: " );
131 case G_LOG_LEVEL_CRITICAL:
132 if ( in_recursion ) {
133 strcat( buf, "CRITICAL (recursed) **: " );
136 strcat( buf, "CRITICAL **: " );
139 case G_LOG_LEVEL_WARNING:
140 if ( in_recursion ) {
141 strcat( buf, "WARNING (recursed) **: " );
144 strcat( buf, "WARNING **: " );
147 case G_LOG_LEVEL_MESSAGE:
148 if ( in_recursion ) {
149 strcat( buf, "Message (recursed): " );
152 strcat( buf, "Message: " );
155 case G_LOG_LEVEL_INFO:
156 if ( in_recursion ) {
157 strcat( buf, "INFO (recursed): " );
160 strcat( buf, "INFO: " );
163 case G_LOG_LEVEL_DEBUG:
164 if ( in_recursion ) {
165 strcat( buf, "DEBUG (recursed): " );
168 strcat( buf, "DEBUG: " );
172 /* we are used for a log level that is not defined by GLib itself,
173 * try to make the best out of it.
175 if ( in_recursion ) {
176 strcat( buf, "LOG (recursed:" );
179 strcat( buf, "LOG (" );
182 gchar string[] = "0x00): ";
183 gchar *p = string + 2;
186 i = g_bit_nth_msf( log_level, -1 );
189 *p = '0' + ( i & 0xf );
194 strcat( buf, string );
197 strcat( buf, "): " );
201 strcat( buf, message );
203 strcat( buf, "\naborting...\n" );
210 globalErrorStream() << buf << "\n";
213 ERROR_MESSAGE( "GTK+ error: " << buf );
217 #if GDEF_COMPILER_MSVC && GDEF_DEBUG
222 #if GDEF_COMPILER_MSVC && GDEF_DEBUG
223 _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
231 Lock() : m_locked( false ){
239 bool locked() const {
248 ScopedLock( Lock& lock ) : m_lock( lock ){
256 class LineLimitedTextOutputStream : public TextOutputStream
258 TextOutputStream& outputStream;
261 LineLimitedTextOutputStream( TextOutputStream& outputStream, std::size_t count )
262 : outputStream( outputStream ), count( count ){
264 std::size_t write( const char* buffer, std::size_t length ){
266 const char* p = buffer;
267 const char* end = buffer + length;
270 p = std::find( p, end, '\n' );
275 if ( --count == 0 ) {
280 outputStream.write( buffer, length );
286 class PopupDebugMessageHandler : public DebugMessageHandler
288 StringOutputStream m_buffer;
291 TextOutputStream& getOutputStream(){
292 if ( !m_lock.locked() ) {
295 return globalErrorStream();
297 bool handleMessage(){
298 getOutputStream() << "----------------\n";
299 LineLimitedTextOutputStream outputStream( getOutputStream(), 24 );
300 write_stack_trace( outputStream );
301 getOutputStream() << "----------------\n";
302 globalErrorStream() << m_buffer.c_str();
303 if ( !m_lock.locked() ) {
304 ScopedLock lock( m_lock );
306 m_buffer << "Break into the debugger?\n";
307 bool handled = ui::alert(ui::root, m_buffer.c_str(), RADIANT_NAME " - Runtime Error", ui::alert_type::YESNO, ui::alert_icon::Error) == ui::alert_response::NO;
311 m_buffer << "Please report this error to the developers\n";
312 ui::alert(ui::root, m_buffer.c_str(), RADIANT_NAME " - Runtime Error", ui::alert_type::OK, ui::alert_icon::Error);
320 typedef Static<PopupDebugMessageHandler> GlobalPopupDebugMessageHandler;
323 GlobalErrorStream::instance().setOutputStream( getSysPrintErrorStream() );
324 GlobalOutputStream::instance().setOutputStream( getSysPrintOutputStream() );
328 g_strSettingsPath = environment_get_home_path();
330 Q_mkdir( g_strSettingsPath.c_str() );
332 g_strAppFilePath = environment_get_app_filepath();
333 g_strAppPath = environment_get_app_path();
334 g_strLibPath = environment_get_lib_path();
335 g_strDataPath = environment_get_data_path();
337 // radiant is installed in the parent dir of "tools/"
338 // NOTE: this is not very easy for debugging
339 // maybe add options to lookup in several places?
340 // (for now I had to create symlinks)
342 StringOutputStream path( 256 );
343 path << g_strDataPath.c_str() << "bitmaps/";
344 BitmapsPath_set( path.c_str() );
347 // we will set this right after the game selection is done
348 g_strGameToolsPath = g_strDataPath;
351 bool check_version_file( const char* filename, const char* version ){
352 TextFileInputStream file( filename );
353 if ( !file.failed() ) {
355 buf[file.read( buf, 9 )] = '\0';
357 // chomp it (the hard way)
359 while ( buf[chomp] >= '0' && buf[chomp] <= '9' )
363 return string_equal( buf, version );
368 void create_global_pid(){
370 the global prefs loading / game selection dialog might fail for any reason we don't know about
371 we need to catch when it happens, to cleanup the stateful prefs which might be killing it
372 and to turn on console logging for lookup of the problem
373 this is the first part of the two step .pid system
374 http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=297
376 StringOutputStream g_pidFile( 256 ); ///< the global .pid file (only for global part of the startup)
378 g_pidFile << SettingsPath_get() << "radiant.pid";
381 pid = fopen( g_pidFile.c_str(), "r" );
385 if ( remove( g_pidFile.c_str() ) == -1 ) {
386 StringOutputStream msg( 256 );
387 msg << "WARNING: Could not delete " << g_pidFile.c_str();
388 ui::alert( ui::root, msg.c_str(), RADIANT_NAME, ui::alert_type::OK, ui::alert_icon::Error );
391 // in debug, never prompt to clean registry, turn console logging auto after a failed start
393 StringOutputStream msg(256);
394 msg << RADIANT_NAME " failed to start properly the last time it was run.\n"
395 "The failure may be related to current global preferences.\n"
396 "Do you want to reset global preferences to defaults?";
398 if (ui::alert(ui::root, msg.c_str(), RADIANT_NAME " - Startup Failure", ui::alert_type::YESNO, ui::alert_icon::Question) == ui::alert_response::YES) {
399 g_GamesDialog.Reset();
403 msg << "Logging console output to " << SettingsPath_get()
404 << "radiant.log\nRefer to the log if " RADIANT_NAME " fails to start again.";
406 ui::alert(ui::root, msg.c_str(), RADIANT_NAME " - Console Log", ui::alert_type::OK);
409 // set without saving, the class is not in a coherent state yet
410 // just do the value change and call to start logging, CGamesDialog will pickup when relevant
411 g_GamesDialog.m_bForceLogConsole = true;
415 // create a primary .pid for global init run
416 pid = fopen( g_pidFile.c_str(), "w" );
422 void remove_global_pid(){
423 StringOutputStream g_pidFile( 256 );
424 g_pidFile << SettingsPath_get() << "radiant.pid";
427 if ( remove( g_pidFile.c_str() ) == -1 ) {
428 StringOutputStream msg( 256 );
429 msg << "WARNING: Could not delete " << g_pidFile.c_str();
430 ui::alert( ui::root, msg.c_str(), RADIANT_NAME, ui::alert_type::OK, ui::alert_icon::Error );
435 now the secondary game dependant .pid file
436 http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=297
438 void create_local_pid(){
439 StringOutputStream g_pidGameFile( 256 ); ///< the game-specific .pid file
440 g_pidGameFile << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << "/radiant-game.pid";
442 FILE *pid = fopen( g_pidGameFile.c_str(), "r" );
445 if ( remove( g_pidGameFile.c_str() ) == -1 ) {
446 StringOutputStream msg;
447 msg << "WARNING: Could not delete " << g_pidGameFile.c_str();
448 ui::alert( ui::root, msg.c_str(), RADIANT_NAME, ui::alert_type::OK, ui::alert_icon::Error );
451 // in debug, never prompt to clean registry, turn console logging auto after a failed start
453 StringOutputStream msg;
454 msg << RADIANT_NAME " failed to start properly the last time it was run.\n"
455 "The failure may be caused by current preferences.\n"
456 "Do you want to reset all preferences to defaults?";
458 if (ui::alert(ui::root, msg.c_str(), RADIANT_NAME " - Startup Failure", ui::alert_type::YESNO, ui::alert_icon::Question) == ui::alert_response::YES) {
463 msg << "Logging console output to " << SettingsPath_get()
464 << "radiant.log\nRefer to the log if " RADIANT_NAME " fails to start again.";
466 ui::alert(ui::root, msg.c_str(), RADIANT_NAME " - Console Log", ui::alert_type::OK);
469 // force console logging on! (will go in prefs too)
470 g_GamesDialog.m_bForceLogConsole = true;
475 // create one, will remove right after entering message loop
476 pid = fopen( g_pidGameFile.c_str(), "w" );
485 now the secondary game dependant .pid file
486 http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=297
488 void remove_local_pid(){
489 StringOutputStream g_pidGameFile( 256 );
490 g_pidGameFile << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << "/radiant-game.pid";
491 remove( g_pidGameFile.c_str() );
494 void user_shortcuts_init(){
495 StringOutputStream path( 256 );
496 path << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << '/';
497 LoadCommandMap( path.c_str() );
498 SaveCommandMap( path.c_str() );
501 void user_shortcuts_save(){
502 StringOutputStream path( 256 );
503 path << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << '/';
504 SaveCommandMap( path.c_str() );
507 int main( int argc, char* argv[] ){
514 lib = LoadLibrary( "dwmapi.dll" );
516 void ( WINAPI *qDwmEnableComposition )( bool bEnable ) = ( void (WINAPI *) ( bool bEnable ) )GetProcAddress( lib, "DwmEnableComposition" );
517 if ( qDwmEnableComposition ) {
518 qDwmEnableComposition( FALSE );
524 const char* mapname = NULL;
527 StringOutputStream mapname_buffer( 256 );
530 char const *error = NULL;
532 if ( !ui::init( &argc, &argv, "<filename.map>", &error) ) {
533 g_print( "%s\n", error );
537 // Gtk already removed parsed `--options`
539 if ( strlen( argv[ 1 ] ) > 1 ) {
540 if ( g_str_has_suffix( argv[ 1 ], ".map" ) ) {
543 if ( !g_path_is_absolute( mapname ) ) {
544 mapname = g_build_filename( g_get_current_dir(), mapname, NULL );
548 mapname_buffer << PathCleaned( mapname );
549 mapname = mapname_buffer.c_str();
553 g_print( "bad file name, will not load: %s\n", mapname );
557 else if ( argc > 2 ) {
558 g_print ( "%s\n", "too many arguments" );
562 // redirect Gtk warnings to the console
563 g_log_set_handler( "Gdk", (GLogLevelFlags)( G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING |
564 G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION ), error_redirect, 0 );
565 g_log_set_handler( "Gtk", (GLogLevelFlags)( G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING |
566 G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION ), error_redirect, 0 );
567 g_log_set_handler( "GtkGLExt", (GLogLevelFlags)( G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING |
568 G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION ), error_redirect, 0 );
569 g_log_set_handler( "GLib", (GLogLevelFlags)( G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING |
570 G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION ), error_redirect, 0 );
571 g_log_set_handler( 0, (GLogLevelFlags)( G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING |
572 G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION ), error_redirect, 0 );
574 GlobalDebugMessageHandler::instance().setHandler( GlobalPopupDebugMessageHandler::instance() );
576 environment_init(argc, (char const **) argv);
584 GlobalPreferences_Init();
586 g_GamesDialog.Init();
588 g_strGameToolsPath = g_pGameDescription->mGameToolsPath;
592 g_Preferences.Init(); // must occur before create_local_pid() to allow preferences to be reset
596 // in a very particular post-.pid startup
597 // we may have the console turned on and want to keep it that way
598 // so we use a latching system
599 if ( g_GamesDialog.m_bForceLogConsole ) {
601 g_Console_enableLogging = true;
602 g_GamesDialog.m_bForceLogConsole = false;
606 Radiant_Initialise();
608 user_shortcuts_init();
611 g_pParentWnd = new MainFrame();
615 if ( mapname != NULL ) {
616 Map_LoadFile( mapname );
618 else if ( g_bLoadLastMap && !g_strLastMap.empty() ) {
619 Map_LoadFile( g_strLastMap.c_str() );
626 // load up shaders now that we have the map loaded
628 TextureBrowser_ShowStartupShaders( GlobalTextureBrowser() );
635 // avoid saving prefs when the app is minimized
636 if ( g_pParentWnd->IsSleeping() ) {
637 globalOutputStream() << "Shutdown while sleeping, not saving prefs\n";
638 g_preferences_globals.disable_ini = true;
643 if ( !Map_Unnamed( g_map ) ) {
644 g_strLastMap = Map_Name( g_map );
649 user_shortcuts_save();
653 // close the log file if any
654 Sys_LogFile( false );