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 <gtk/gtkmain.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"
95 void error_redirect (const gchar *domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
97 gboolean in_recursion;
101 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
102 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
103 log_level = (GLogLevelFlags) (log_level & G_LOG_LEVEL_MASK);
106 message = "(0) message";
109 strcpy (buf, domain);
116 case G_LOG_LEVEL_ERROR:
118 strcat (buf, "ERROR (recursed) **: ");
120 strcat (buf, "ERROR **: ");
122 case G_LOG_LEVEL_CRITICAL:
124 strcat (buf, "CRITICAL (recursed) **: ");
126 strcat (buf, "CRITICAL **: ");
128 case G_LOG_LEVEL_WARNING:
130 strcat (buf, "WARNING (recursed) **: ");
132 strcat (buf, "WARNING **: ");
134 case G_LOG_LEVEL_MESSAGE:
136 strcat (buf, "Message (recursed): ");
138 strcat (buf, "Message: ");
140 case G_LOG_LEVEL_INFO:
142 strcat (buf, "INFO (recursed): ");
144 strcat (buf, "INFO: ");
146 case G_LOG_LEVEL_DEBUG:
148 strcat (buf, "DEBUG (recursed): ");
150 strcat (buf, "DEBUG: ");
153 /* we are used for a log level that is not defined by GLib itself,
154 * try to make the best out of it.
157 strcat (buf, "LOG (recursed:");
159 strcat (buf, "LOG (");
162 gchar string[] = "0x00): ";
163 gchar *p = string + 2;
166 i = g_bit_nth_msf (log_level, -1);
169 *p = '0' + (i & 0xf);
173 strcat (buf, string);
178 strcat (buf, message);
180 strcat (buf, "\naborting...\n");
184 printf ("%s\n", buf);
186 ERROR_MESSAGE("GTK+ error: " << buf);
189 #if defined (_DEBUG) && defined (WIN32) && defined (_MSC_VER)
195 #if defined (_DEBUG) && defined (WIN32) && defined (_MSC_VER)
196 _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
204 Lock() : m_locked(false)
225 ScopedLock(Lock& lock) : m_lock(lock)
235 class LineLimitedTextOutputStream : public TextOutputStream
237 TextOutputStream& outputStream;
240 LineLimitedTextOutputStream(TextOutputStream& outputStream, std::size_t count)
241 : outputStream(outputStream), count(count)
244 std::size_t write(const char* buffer, std::size_t length)
248 const char* p = buffer;
249 const char* end = buffer+length;
252 p = std::find(p, end, '\n');
264 outputStream.write(buffer, length);
270 class PopupDebugMessageHandler : public DebugMessageHandler
272 StringOutputStream m_buffer;
275 TextOutputStream& getOutputStream()
281 return globalErrorStream();
285 getOutputStream() << "----------------\n";
286 LineLimitedTextOutputStream outputStream(getOutputStream(), 24);
287 write_stack_trace(outputStream);
288 getOutputStream() << "----------------\n";
289 globalErrorStream() << m_buffer.c_str();
292 ScopedLock lock(m_lock);
294 m_buffer << "Break into the debugger?\n";
295 bool handled = gtk_MessageBox(0, m_buffer.c_str(), "Radiant - Runtime Error", eMB_YESNO, eMB_ICONERROR) == eIDNO;
299 m_buffer << "Please report this error to the developers\n";
300 gtk_MessageBox(0, m_buffer.c_str(), "Radiant - Runtime Error", eMB_OK, eMB_ICONERROR);
308 typedef Static<PopupDebugMessageHandler> GlobalPopupDebugMessageHandler;
312 GlobalErrorStream::instance().setOutputStream(getSysPrintErrorStream());
313 GlobalOutputStream::instance().setOutputStream(getSysPrintOutputStream());
318 const char* home = environment_get_home_path();
322 StringOutputStream path(256);
323 path << home << RADIANT_VERSION << '/';
324 g_strSettingsPath = path.c_str();
327 Q_mkdir(g_strSettingsPath.c_str());
329 g_strAppPath = environment_get_app_path();
331 // radiant is installed in the parent dir of "tools/"
332 // NOTE: this is not very easy for debugging
333 // maybe add options to lookup in several places?
334 // (for now I had to create symlinks)
336 StringOutputStream path(256);
337 path << g_strAppPath.c_str() << "bitmaps/";
338 BitmapsPath_set(path.c_str());
341 // we will set this right after the game selection is done
342 g_strGameToolsPath = g_strAppPath;
345 bool check_version_file(const char* filename, const char* version)
347 TextFileInputStream file(filename);
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);
366 // a safe check to avoid people running broken installations
367 // (otherwise, they run it, crash it, and blame us for not forcing them hard enough to pay attention while installing)
368 // make something idiot proof and someone will make better idiots, this may be overkill
369 // let's leave it disabled in debug mode in any case
370 // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=431
372 #define CHECK_VERSION
375 // locate and open RADIANT_MAJOR and RADIANT_MINOR
376 bool bVerIsGood = true;
378 StringOutputStream ver_file_name(256);
379 ver_file_name << AppPath_get() << "RADIANT_MAJOR";
380 bVerIsGood = check_version_file(ver_file_name.c_str(), RADIANT_MAJOR_VERSION);
383 StringOutputStream ver_file_name(256);
384 ver_file_name << AppPath_get() << "RADIANT_MINOR";
385 bVerIsGood = check_version_file(ver_file_name.c_str(), RADIANT_MINOR_VERSION);
390 StringOutputStream msg(256);
391 msg << "This editor binary (" RADIANT_VERSION ") doesn't match what the latest setup has configured in this directory\n"
392 "Make sure you run the right/latest editor binary you installed\n"
394 gtk_MessageBox(0, msg.c_str(), "Radiant", eMB_OK, eMB_ICONDEFAULT);
402 void create_global_pid()
405 the global prefs loading / game selection dialog might fail for any reason we don't know about
406 we need to catch when it happens, to cleanup the stateful prefs which might be killing it
407 and to turn on console logging for lookup of the problem
408 this is the first part of the two step .pid system
409 http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=297
411 StringOutputStream g_pidFile(256); ///< the global .pid file (only for global part of the startup)
413 g_pidFile << SettingsPath_get() << "radiant.pid";
416 pid = fopen (g_pidFile.c_str(), "r");
421 if (remove (g_pidFile.c_str()) == -1)
423 StringOutputStream msg(256);
424 msg << "WARNING: Could not delete " << g_pidFile.c_str();
425 gtk_MessageBox (0, msg.c_str(), "Radiant", eMB_OK, eMB_ICONERROR );
428 // in debug, never prompt to clean registry, turn console logging auto after a failed start
430 StringOutputStream msg(256);
431 msg << "Radiant failed to start properly the last time it was run.\n"
432 "The failure may be related to current global preferences.\n"
433 "Do you want to reset global preferences to defaults?";
435 if (gtk_MessageBox (0, msg.c_str(), "Radiant - Startup Failure", eMB_YESNO, eMB_ICONQUESTION) == eIDYES)
437 g_GamesDialog.Reset();
441 msg << "Logging console output to " << SettingsPath_get() << "radiant.log\nRefer to the log if Radiant fails to start again.";
443 gtk_MessageBox (0, msg.c_str(), "Radiant - Console Log", eMB_OK);
446 // set without saving, the class is not in a coherent state yet
447 // just do the value change and call to start logging, CGamesDialog will pickup when relevant
448 g_GamesDialog.m_bForceLogConsole = true;
452 // create a primary .pid for global init run
453 pid = fopen (g_pidFile.c_str(), "w");
458 void remove_global_pid()
460 StringOutputStream g_pidFile(256);
461 g_pidFile << SettingsPath_get() << "radiant.pid";
464 if (remove (g_pidFile.c_str()) == -1)
466 StringOutputStream msg(256);
467 msg << "WARNING: Could not delete " << g_pidFile.c_str();
468 gtk_MessageBox (0, msg.c_str(), "Radiant", eMB_OK, eMB_ICONERROR );
473 now the secondary game dependant .pid file
474 http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=297
476 void create_local_pid()
478 StringOutputStream g_pidGameFile(256); ///< the game-specific .pid file
479 g_pidGameFile << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << "/radiant-game.pid";
481 FILE *pid = fopen (g_pidGameFile.c_str(), "r");
485 if (remove (g_pidGameFile.c_str()) == -1)
487 StringOutputStream msg;
488 msg << "WARNING: Could not delete " << g_pidGameFile.c_str();
489 gtk_MessageBox (0, msg.c_str(), "Radiant", eMB_OK, eMB_ICONERROR );
492 // in debug, never prompt to clean registry, turn console logging auto after a failed start
494 StringOutputStream msg;
495 msg << "Radiant failed to start properly the last time it was run.\n"
496 "The failure may be caused by current preferences.\n"
497 "Do you want to reset all preferences to defaults?";
499 if (gtk_MessageBox (0, msg.c_str(), "Radiant - Startup Failure", eMB_YESNO, eMB_ICONQUESTION) == eIDYES)
505 msg << "Logging console output to " << SettingsPath_get() << "radiant.log\nRefer to the log if Radiant fails to start again.";
507 gtk_MessageBox (0, msg.c_str(), "Radiant - Console Log", eMB_OK);
510 // force console logging on! (will go in prefs too)
511 g_GamesDialog.m_bForceLogConsole = true;
516 // create one, will remove right after entering message loop
517 pid = fopen (g_pidGameFile.c_str(), "w");
525 now the secondary game dependant .pid file
526 http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=297
528 void remove_local_pid()
530 StringOutputStream g_pidGameFile(256);
531 g_pidGameFile << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << "/radiant-game.pid";
532 remove(g_pidGameFile.c_str());
535 void user_shortcuts_init()
537 StringOutputStream path(256);
538 path << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << '/';
539 LoadCommandMap(path.c_str());
540 SaveCommandMap(path.c_str());
543 int main (int argc, char* argv[])
549 gtk_disable_setlocale();
550 gtk_init(&argc, &argv);
552 // redirect Gtk warnings to the console
553 g_log_set_handler ("Gdk", (GLogLevelFlags)(G_LOG_LEVEL_ERROR|G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING|
554 G_LOG_LEVEL_MESSAGE|G_LOG_LEVEL_INFO|G_LOG_LEVEL_DEBUG), error_redirect, 0);
555 g_log_set_handler ("Gtk", (GLogLevelFlags)(G_LOG_LEVEL_ERROR|G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING|
556 G_LOG_LEVEL_MESSAGE|G_LOG_LEVEL_INFO|G_LOG_LEVEL_DEBUG), error_redirect, 0);
557 g_log_set_handler ("GtkGLExt", (GLogLevelFlags)(G_LOG_LEVEL_ERROR|G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING|
558 G_LOG_LEVEL_MESSAGE|G_LOG_LEVEL_INFO|G_LOG_LEVEL_DEBUG), error_redirect, 0);
559 g_log_set_handler ("GLib", (GLogLevelFlags)(G_LOG_LEVEL_ERROR|G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING|
560 G_LOG_LEVEL_MESSAGE|G_LOG_LEVEL_INFO|G_LOG_LEVEL_DEBUG), error_redirect, 0);
561 g_log_set_handler (0, (GLogLevelFlags)(G_LOG_LEVEL_ERROR|G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING|
562 G_LOG_LEVEL_MESSAGE|G_LOG_LEVEL_INFO|G_LOG_LEVEL_DEBUG), error_redirect, 0);
564 GlobalDebugMessageHandler::instance().setHandler(GlobalPopupDebugMessageHandler::instance());
566 environment_init(argc, argv);
579 GlobalPreferences_Init();
581 g_GamesDialog.Init();
583 g_strGameToolsPath = g_pGameDescription->mGameToolsPath;
587 g_Preferences.Init(); // must occur before create_local_pid() to allow preferences to be reset
591 // in a very particular post-.pid startup
592 // we may have the console turned on and want to keep it that way
593 // so we use a latching system
594 if (g_GamesDialog.m_bForceLogConsole)
597 g_Console_enableLogging = true;
598 g_GamesDialog.m_bForceLogConsole = false;
602 Radiant_Initialise();
606 user_shortcuts_init();
609 g_pParentWnd = new MainFrame();
613 if (g_bLoadLastMap && !g_strLastMap.empty())
615 Map_LoadFile(g_strLastMap.c_str());
622 // load up shaders now that we have the map loaded
624 TextureBrowser_ShowStartupShaders(GlobalTextureBrowser());
631 // avoid saving prefs when the app is minimized
632 if (g_pParentWnd->IsSleeping())
634 globalOutputStream() << "Shutdown while sleeping, not saving prefs\n";
635 g_preferences_globals.disable_ini = true;
640 if (!Map_Unnamed(g_map))
642 g_strLastMap = Map_Name(g_map);
647 global_accel_destroy();
651 // close the log file if any