1
0
Fork 0

(svn r10923) -Codechange: use 'real' exception handling in saveload code

release/0.6
glx 2007-08-16 16:18:22 +00:00
parent 9c75e6d87b
commit 6a13dd4aa6
1 changed files with 158 additions and 162 deletions

View File

@ -27,7 +27,6 @@
#include "variables.h"
#include "table/strings.h"
#include "strings.h"
#include <setjmp.h>
#include <list>
extern const uint16 SAVEGAME_VERSION = 73;
@ -68,7 +67,6 @@ static struct {
void (*excpt_uninit)(); ///< the function to execute on any encountered error
StringID error_str; ///< the translateable error message to show
char *extra_msg; ///< the error message
jmp_buf excpt; ///< @todo used to jump to "exception handler"; really ugly
} _sl;
@ -143,7 +141,7 @@ static void NORETURN SlError(StringID string, const char *extra_msg = NULL)
_sl.error_str = string;
free(_sl.extra_msg);
_sl.extra_msg = (extra_msg == NULL) ? NULL : strdup(extra_msg);
longjmp(_sl.excpt, 0);
throw std::exception();
}
/** Read in a single byte from file. If the temporary buffer is full,
@ -1521,23 +1519,7 @@ static SaveOrLoadResult SaveFileToDisk(bool threaded)
uint32 hdr[2];
_sl.excpt_uninit = NULL;
/* XXX - Setup setjmp error handler if an error occurs anywhere deep during
* loading/saving execute a longjmp() and continue execution here */
if (setjmp(_sl.excpt)) {
AbortSaveLoad();
if (_sl.excpt_uninit != NULL) _sl.excpt_uninit();
ShowInfo(GetSaveLoadErrorString());
fprintf(stderr, GetSaveLoadErrorString());
if (threaded) {
OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_ERROR);
} else {
SaveFileError();
}
return SL_ERROR;
}
try {
fmt = GetSavegameFormat(_savegame_format);
/* We have written our stuff to memory, now write it to file! */
@ -1572,6 +1554,21 @@ static SaveOrLoadResult SaveFileToDisk(bool threaded)
return SL_OK;
}
catch (...) {
AbortSaveLoad();
if (_sl.excpt_uninit != NULL) _sl.excpt_uninit();
ShowInfo(GetSaveLoadErrorString());
fprintf(stderr, GetSaveLoadErrorString());
if (threaded) {
OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_ERROR);
} else {
SaveFileError();
}
return SL_ERROR;
}
}
static void* SaveFileToDiskThread(void *arg)
{
@ -1616,22 +1613,8 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb)
return SL_OK;
}
/* XXX - Setup setjmp error handler if an error occurs anywhere deep during
* loading/saving execute a longjmp() and continue execution here */
_sl.excpt_uninit = NULL;
if (setjmp(_sl.excpt)) {
AbortSaveLoad();
/* deinitialize compressor. */
if (_sl.excpt_uninit != NULL) _sl.excpt_uninit();
/* Skip the "color" character */
ShowInfoF(GetSaveLoadErrorString() + 3);
/* A saver/loader exception!! reinitialize all variables to prevent crash! */
return (mode == SL_LOAD) ? SL_REINIT : SL_ERROR;
}
try {
_sl.fh = (mode == SL_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
/* Make it a little easier to load savegames from the console */
@ -1751,6 +1734,19 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb)
return SL_OK;
}
catch (...) {
AbortSaveLoad();
/* deinitialize compressor. */
if (_sl.excpt_uninit != NULL) _sl.excpt_uninit();
/* Skip the "color" character */
ShowInfoF(GetSaveLoadErrorString() + 3);
/* A saver/loader exception!! reinitialize all variables to prevent crash! */
return (mode == SL_LOAD) ? SL_REINIT : SL_ERROR;
}
}
/** Do a save when exiting the game (patch option) _patches.autosave_on_exit */
void DoExitSave()