(svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
authorglx
Thu, 05 Jun 2008 20:54:52 +0000
changeset 10839 a62547c31fdb
parent 10838 1dd920e2c20d
child 10845 4cc81b7cf711
(svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
src/driver.cpp
src/fileio.cpp
src/gfxinit.cpp
src/network/network_server.cpp
src/newgrf.cpp
src/openttd.cpp
src/os2.cpp
src/roadveh_cmd.cpp
src/sound/win32_s.cpp
src/spritecache.cpp
src/stdafx.h
src/strings.cpp
src/town_cmd.cpp
src/train_cmd.cpp
src/unix.cpp
src/video/dedicated_v.cpp
src/video/sdl_v.cpp
src/video/win32_v.cpp
src/win32.cpp
--- a/src/driver.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/driver.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -87,7 +87,7 @@
 				delete newd;
 			}
 		}
-		error("Couldn't find any suitable %s driver", GetDriverTypeName(type));
+		usererror("Couldn't find any suitable %s driver", GetDriverTypeName(type));
 	} else {
 		char *parm;
 		char buffer[256];
@@ -125,7 +125,7 @@
 			const char *err = newd->Start(parms);
 			if (err != NULL) {
 				delete newd;
-				error("Unable to load driver '%s'. The error was: %s", d->name, err);
+				usererror("Unable to load driver '%s'. The error was: %s", d->name, err);
 			}
 
 			DEBUG(driver, 1, "Successfully loaded %s driver '%s'", GetDriverTypeName(type), d->name);
@@ -133,7 +133,7 @@
 			*GetActiveDriver(type) = newd;
 			return newd;
 		}
-		error("No such %s driver: %s\n", GetDriverTypeName(type), buffer);
+		usererror("No such %s driver: %s\n", GetDriverTypeName(type), buffer);
 	}
 }
 
--- a/src/fileio.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/fileio.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -186,7 +186,7 @@
 	FioFreeHandle();
 #endif /* LIMITED_FDS */
 	f = FioFOpenFile(filename);
-	if (f == NULL) error("Cannot open file '%s'", filename);
+	if (f == NULL) usererror("Cannot open file '%s'", filename);
 	uint32 pos = ftell(f);
 
 	FioCloseFile(slot); // if file was opened before, close it
--- a/src/gfxinit.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/gfxinit.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -55,7 +55,7 @@
 		load_index++;
 		sprite_id++;
 		if (load_index >= MAX_SPRITES) {
-			error("Too many sprites. Recompile with higher MAX_SPRITES value or remove some custom GRF files.");
+			usererror("Too many sprites. Recompile with higher MAX_SPRITES value or remove some custom GRF files.");
 		}
 	}
 	DEBUG(sprite, 2, "Currently %i sprites are loaded", load_index);
--- a/src/network/network_server.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/network/network_server.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -323,12 +323,12 @@
 		Packet *p;
 
 		// Make a dump of the current game
-		if (SaveOrLoad(filename, SL_SAVE, AUTOSAVE_DIR) != SL_OK) error("network savedump failed");
+		if (SaveOrLoad(filename, SL_SAVE, AUTOSAVE_DIR) != SL_OK) usererror("network savedump failed");
 
 		file_pointer = FioFOpenFile(filename, "rb", AUTOSAVE_DIR);
 		fseek(file_pointer, 0, SEEK_END);
 
-		if (ftell(file_pointer) == 0) error("network savedump failed - zero sized savegame?");
+		if (ftell(file_pointer) == 0) usererror("network savedump failed - zero sized savegame?");
 
 		// Now send the _frame_counter and how many packets are coming
 		p = NetworkSend_Init(PACKET_SERVER_MAP);
@@ -355,7 +355,7 @@
 			p->Send_uint8(MAP_PACKET_NORMAL);
 			res = (int)fread(p->buffer + p->size, 1, SEND_MTU - p->size, file_pointer);
 
-			if (ferror(file_pointer)) error("Error reading temporary network savegame!");
+			if (ferror(file_pointer)) usererror("Error reading temporary network savegame!");
 
 			p->size += res;
 			cs->Send_Packet(p);
--- a/src/newgrf.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/newgrf.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -5783,7 +5783,7 @@
 	 * processed once at initialization.  */
 	if (stage != GLS_FILESCAN && stage != GLS_SAFETYSCAN && stage != GLS_LABELSCAN) {
 		_cur_grffile = GetFileByFilename(filename);
-		if (_cur_grffile == NULL) error("File '%s' lost in cache.\n", filename);
+		if (_cur_grffile == NULL) usererror("File '%s' lost in cache.\n", filename);
 		if (stage == GLS_RESERVE && config->status != GCS_INITIALISED) return;
 		if (stage == GLS_ACTIVATION && !HasBit(config->flags, GCF_RESERVED)) return;
 		_cur_grffile->is_ottdfile = config->IsOpenTTDBaseGRF();
@@ -5989,7 +5989,7 @@
 			if (stage > GLS_INIT && HasBit(c->flags, GCF_INIT_ONLY)) continue;
 
 			/* @todo usererror() */
-			if (!FioCheckFileExists(c->filename)) error("NewGRF file is missing '%s'", c->filename);
+			if (!FioCheckFileExists(c->filename)) usererror("NewGRF file is missing '%s'", c->filename);
 
 			if (stage == GLS_LABELSCAN) InitNewGRFFile(c, _cur_spriteid);
 			LoadNewGRFFile(c, slot++, stage);
--- a/src/openttd.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/openttd.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -99,11 +99,31 @@
 
 extern void SetDifficultyLevel(int mode, DifficultySettings *gm_opt);
 extern Player* DoStartupNewPlayer(bool is_ai);
-extern void ShowOSErrorBox(const char *buf);
+extern void ShowOSErrorBox(const char *buf, bool system);
 extern void InitializeRailGUI();
 
 /**
- * Error handling for fatal errors.
+ * Error handling for fatal user errors.
+ * @param s the string to print.
+ * @note Does NEVER return.
+ */
+void CDECL usererror(const char *s, ...)
+{
+	va_list va;
+	char buf[512];
+
+	va_start(va, s);
+	vsnprintf(buf, lengthof(buf), s, va);
+	va_end(va);
+
+	ShowOSErrorBox(buf, false);
+	if (_video_driver != NULL) _video_driver->Stop();
+
+	exit(1);
+}
+
+/**
+ * Error handling for fatal non-user errors.
  * @param s the string to print.
  * @note Does NEVER return.
  */
@@ -116,7 +136,7 @@
 	vsnprintf(buf, lengthof(buf), s, va);
 	va_end(va);
 
-	ShowOSErrorBox(buf);
+	ShowOSErrorBox(buf, true);
 	if (_video_driver != NULL) _video_driver->Stop();
 
 	assert(0);
@@ -524,30 +544,30 @@
 	DEBUG(misc, 1, "Loading blitter...");
 	if (BlitterFactoryBase::SelectBlitter(_ini_blitter) == NULL)
 		StrEmpty(_ini_blitter) ?
-			error("Failed to autoprobe blitter") :
-			error("Failed to select requested blitter '%s'; does it exist?", _ini_blitter);
+			usererror("Failed to autoprobe blitter") :
+			usererror("Failed to select requested blitter '%s'; does it exist?", _ini_blitter);
 
 	DEBUG(driver, 1, "Loading drivers...");
 
 	_sound_driver = (SoundDriver*)SoundDriverFactoryBase::SelectDriver(_ini_sounddriver, Driver::DT_SOUND);
 	if (_sound_driver == NULL) {
 		StrEmpty(_ini_sounddriver) ?
-			error("Failed to autoprobe sound driver") :
-			error("Failed to select requested sound driver '%s'", _ini_sounddriver);
+			usererror("Failed to autoprobe sound driver") :
+			usererror("Failed to select requested sound driver '%s'", _ini_sounddriver);
 	}
 
 	_music_driver = (MusicDriver*)MusicDriverFactoryBase::SelectDriver(_ini_musicdriver, Driver::DT_MUSIC);
 	if (_music_driver == NULL) {
 		StrEmpty(_ini_musicdriver) ?
-			error("Failed to autoprobe music driver") :
-			error("Failed to select requested music driver '%s'", _ini_musicdriver);
+			usererror("Failed to autoprobe music driver") :
+			usererror("Failed to select requested music driver '%s'", _ini_musicdriver);
 	}
 
 	_video_driver = (VideoDriver*)VideoDriverFactoryBase::SelectDriver(_ini_videodriver, Driver::DT_VIDEO);
 	if (_video_driver == NULL) {
 		StrEmpty(_ini_videodriver) ?
-			error("Failed to autoprobe video driver") :
-			error("Failed to select requested video driver '%s'", _ini_videodriver);
+			usererror("Failed to autoprobe video driver") :
+			usererror("Failed to select requested video driver '%s'", _ini_videodriver);
 	}
 
 	_savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
--- a/src/os2.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/os2.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -145,7 +145,7 @@
 	WinTerminate(hab);
 }
 
-void ShowOSErrorBox(const char *buf)
+void ShowOSErrorBox(const char *buf, bool system)
 {
 	HAB hab;
 	HMQ hmq;
--- a/src/roadveh_cmd.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/roadveh_cmd.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -1468,7 +1468,7 @@
 		}
 
 		if (dir == INVALID_TRACKDIR) {
-			if (!IsRoadVehFront(v)) error("!Disconnecting road vehicle.");
+			if (!IsRoadVehFront(v)) error("Disconnecting road vehicle.");
 			v->cur_speed = 0;
 			return false;
 		}
--- a/src/sound/win32_s.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/sound/win32_s.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -24,7 +24,7 @@
 	hdr->lpData = MallocT<char>(_bufsize * 4);
 	if (hdr->lpData == NULL ||
 			waveOutPrepareHeader(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
-		error("waveOutPrepareHeader failed");
+		usererror("waveOutPrepareHeader failed");
 }
 
 static void FillHeaders()
@@ -35,7 +35,7 @@
 		if (!(hdr->dwFlags & WHDR_INQUEUE)) {
 			MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
 			if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
-				error("waveOutWrite failed");
+				usererror("waveOutWrite failed");
 		}
 	}
 }
--- a/src/spritecache.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/spritecache.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -169,7 +169,7 @@
 			static byte warning_level = 0;
 			DEBUG(sprite, warning_level, "Tried to load non sprite #%d as a real sprite. Probable cause: NewGRF interference", id);
 			warning_level = 6;
-			if (id == SPR_IMG_QUERY) error("Uhm, would you be so kind not to load a NewGRF that makes the 'query' sprite a non- sprite?");
+			if (id == SPR_IMG_QUERY) usererror("Uhm, would you be so kind not to load a NewGRF that makes the 'query' sprite a non- sprite?");
 			return (void*)GetSprite(SPR_IMG_QUERY);
 		}
 
@@ -246,7 +246,7 @@
 	if (!ReadSpriteHeaderSkipData()) return false;
 
 	if (load_index >= MAX_SPRITES) {
-		error("Tried to load too many sprites (#%d; max %d)", load_index, MAX_SPRITES);
+		usererror("Tried to load too many sprites (#%d; max %d)", load_index, MAX_SPRITES);
 	}
 
 	sc = AllocateSpriteCache(load_index);
--- a/src/stdafx.h	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/stdafx.h	Thu Jun 05 20:54:52 2008 +0000
@@ -317,6 +317,7 @@
 	#define CloseConnection OTTD_CloseConnection
 #endif /* __APPLE__ */
 
+void NORETURN CDECL usererror(const char *str, ...);
 void NORETURN CDECL error(const char *str, ...);
 #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
 
--- a/src/strings.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/strings.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -178,7 +178,7 @@
 
 	if (index >= _langtab_num[tab]) {
 		error(
-			"!String 0x%X is invalid. "
+			"String 0x%X is invalid. "
 			"Probably because an old version of the .lng file.\n", string
 		);
 	}
@@ -1427,7 +1427,7 @@
 		FioAppendDirectory(path, lengthof(path), sp, LANG_DIR);
 		language_count += GetLanguageList(files, language_count, lengthof(files), path);
 	}
-	if (language_count == 0) error("No available language packs (invalid versions?)");
+	if (language_count == 0) usererror("No available language packs (invalid versions?)");
 
 	/* Acquire the locale of the current system */
 	const char *lang = GetCurrentLocale("LC_MESSAGES");
@@ -1463,7 +1463,7 @@
 		dl->num++;
 	}
 
-	if (dl->num == 0) error("Invalid version of language packs");
+	if (dl->num == 0) usererror("Invalid version of language packs");
 
 	/* We haven't found the language in the config nor the one in the locale.
 	 * Now we set it to one of the fallback languages */
@@ -1471,7 +1471,7 @@
 		chosen_language = (language_fallback != -1) ? language_fallback : en_GB_fallback;
 	}
 
-	if (!ReadLanguagePack(chosen_language)) error("Can't read language pack '%s'", dl->ent[chosen_language].file);
+	if (!ReadLanguagePack(chosen_language)) usererror("Can't read language pack '%s'", dl->ent[chosen_language].file);
 }
 
 /**
--- a/src/town_cmd.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/town_cmd.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -1603,7 +1603,7 @@
 	if (num == 0 && CreateRandomTown(10000, TSM_RANDOM, 0) == NULL) {
 		if (GetNumTowns() == 0) {
 			/* XXX - can we handle that more gracefully? */
-			if (_game_mode != GM_EDITOR) error("Could not generate any town");
+			if (_game_mode != GM_EDITOR) usererror("Could not generate any town");
 
 			return false;
 		}
--- a/src/train_cmd.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/train_cmd.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -3113,7 +3113,7 @@
 
 invalid_rail:
 	/* We've reached end of line?? */
-	if (prev != NULL) error("!Disconnecting train");
+	if (prev != NULL) error("Disconnecting train");
 
 reverse_train_direction:
 	v->load_unload_time_rem = 0;
--- a/src/unix.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/unix.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -214,7 +214,7 @@
 	fprintf(stderr, "%s\n", str);
 }
 
-void ShowOSErrorBox(const char *buf)
+void ShowOSErrorBox(const char *buf, bool system)
 {
 #if defined(__APPLE__)
 	/* this creates an NSAlertPanel with the contents of 'buf'
--- a/src/video/dedicated_v.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/video/dedicated_v.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -107,10 +107,10 @@
 	/* Create event to signal when console input is ready */
 	_hInputReady = CreateEvent(NULL, false, false, NULL);
 	_hWaitForInputHandling = CreateEvent(NULL, false, false, NULL);
-	if (_hInputReady == NULL || _hWaitForInputHandling == NULL) error("Cannot create console event!");
+	if (_hInputReady == NULL || _hWaitForInputHandling == NULL) usererror("Cannot create console event!");
 
 	_hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput, NULL, 0, &dwThreadId);
-	if (_hThread == NULL) error("Cannot create console thread!");
+	if (_hThread == NULL) usererror("Cannot create console thread!");
 
 	DEBUG(driver, 2, "Windows console thread started");
 }
--- a/src/video/sdl_v.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/video/sdl_v.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -118,7 +118,7 @@
 	modes = SDL_CALL SDL_ListModes(NULL, SDL_SWSURFACE + (_fullscreen ? SDL_FULLSCREEN : 0));
 
 	if (modes == NULL)
-		error("sdl: no modes available");
+		usererror("sdl: no modes available");
 
 	_all_modes = (modes == (void*)-1);
 
@@ -198,7 +198,7 @@
 
 	DEBUG(driver, 1, "SDL: using mode %dx%dx%d", w, h, bpp);
 
-	if (bpp == 0) error("Can't use a blitter that blits 0 bpp for normal visuals");
+	if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
 
 	/* Give the application an icon */
 	icon = SDL_CALL SDL_LoadBMP(ICON_DIR PATHSEP "openttd.32.bmp");
--- a/src/video/win32_v.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/video/win32_v.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -59,7 +59,7 @@
 
 	}
 	_wnd.gdi_palette = CreatePalette(pal);
-	if (_wnd.gdi_palette == NULL) error("CreatePalette failed!\n");
+	if (_wnd.gdi_palette == NULL) usererror("CreatePalette failed!\n");
 }
 
 static void UpdatePalette(HDC dc, uint start, uint count)
@@ -292,7 +292,7 @@
 			_sntprintf(Windowtitle, sizeof(Windowtitle), _T("OpenTTD %s"), MB_TO_WIDE(_openttd_revision));
 
 			_wnd.main_wnd = CreateWindow(_T("OTTD"), Windowtitle, style, x, y, w, h, 0, 0, GetModuleHandle(NULL), 0);
-			if (_wnd.main_wnd == NULL) error("CreateWindow failed");
+			if (_wnd.main_wnd == NULL) usererror("CreateWindow failed");
 			ShowWindow(_wnd.main_wnd, showstyle);
 		}
 	}
@@ -671,7 +671,7 @@
 		};
 
 		registered = true;
-		if (!RegisterClass(&wnd)) error("RegisterClass failed");
+		if (!RegisterClass(&wnd)) usererror("RegisterClass failed");
 	}
 }
 
@@ -684,7 +684,7 @@
 	w = max(w, 64);
 	h = max(h, 64);
 
-	if (bpp == 0) error("Can't use a blitter that blits 0 bpp for normal visuals");
+	if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
 
 	if (w == _screen.width && h == _screen.height)
 		return false;
@@ -707,7 +707,7 @@
 
 	dc = GetDC(0);
 	_wnd.dib_sect = CreateDIBSection(dc, bi, DIB_RGB_COLORS, (VOID**)&_wnd.buffer_bits, NULL, 0);
-	if (_wnd.dib_sect == NULL) error("CreateDIBSection failed");
+	if (_wnd.dib_sect == NULL) usererror("CreateDIBSection failed");
 	ReleaseDC(0, dc);
 
 	return true;
--- a/src/win32.cpp	Thu Jun 05 16:55:00 2008 +0000
+++ b/src/win32.cpp	Thu Jun 05 20:54:52 2008 +0000
@@ -92,14 +92,14 @@
 }
 #endif
 
-void ShowOSErrorBox(const char *buf)
+void ShowOSErrorBox(const char *buf, bool system)
 {
 	MyShowCursor(true);
 	MessageBox(GetActiveWindow(), MB_TO_WIDE(buf), _T("Error!"), MB_ICONSTOP);
 
 /* if exception tracker is enabled, we crash here to let the exception handler handle it. */
 #if defined(WIN32_EXCEPTION_TRACKER) && !defined(_DEBUG)
-	if (*buf == '!') {
+	if (system) {
 		_exception_string = buf;
 		*(byte*)0 = 0;
 	}
@@ -961,7 +961,7 @@
 
 #if !defined(WINCE)
 	/* Check if a win9x user started the win32 version */
-	if (HasBit(GetVersion(), 31)) error("This version of OpenTTD doesn't run on windows 95/98/ME.\nPlease download the win9x binary and try again.");
+	if (HasBit(GetVersion(), 31)) usererror("This version of OpenTTD doesn't run on windows 95/98/ME.\nPlease download the win9x binary and try again.");
 #endif
 
 	/* For UNICODE we need to convert the commandline to char* _AND_