SharedState: Throw exception on errors (and handle them)

First up: throw if chdir(gameFolder) fails.
This commit is contained in:
Jonas Kulla 2013-12-30 01:26:39 +01:00
parent 9759e52b3c
commit 680e407a38
3 changed files with 50 additions and 6 deletions

View File

@ -33,6 +33,7 @@
#include "eventthread.h" #include "eventthread.h"
#include "debuglogger.h" #include "debuglogger.h"
#include "debugwriter.h" #include "debugwriter.h"
#include "exception.h"
#include "binding.h" #include "binding.h"
@ -100,6 +101,7 @@ int rgssThreadFun(void *userdata)
{ {
rgssThreadError(threadData, "Error initializing glew"); rgssThreadError(threadData, "Error initializing glew");
SDL_GL_DeleteContext(glCtx); SDL_GL_DeleteContext(glCtx);
return 0; return 0;
} }
@ -114,6 +116,7 @@ int rgssThreadFun(void *userdata)
{ {
rgssThreadError(threadData, "At least OpenGL 2.0 is required"); rgssThreadError(threadData, "At least OpenGL 2.0 is required");
SDL_GL_DeleteContext(glCtx); SDL_GL_DeleteContext(glCtx);
return 0; return 0;
} }
@ -125,6 +128,7 @@ int rgssThreadFun(void *userdata)
rgssThreadError(threadData, std::string("Required GL extension \"") rgssThreadError(threadData, std::string("Required GL extension \"")
+ reqExt[i] + "\" not present"); + reqExt[i] + "\" not present");
SDL_GL_DeleteContext(glCtx); SDL_GL_DeleteContext(glCtx);
return 0; return 0;
} }
} }
@ -140,6 +144,7 @@ int rgssThreadFun(void *userdata)
{ {
rgssThreadError(threadData, "Error opening OpenAL device"); rgssThreadError(threadData, "Error opening OpenAL device");
SDL_GL_DeleteContext(glCtx); SDL_GL_DeleteContext(glCtx);
return 0; return 0;
} }
@ -150,12 +155,25 @@ int rgssThreadFun(void *userdata)
rgssThreadError(threadData, "Error creating OpenAL context"); rgssThreadError(threadData, "Error creating OpenAL context");
alcCloseDevice(alcDev); alcCloseDevice(alcDev);
SDL_GL_DeleteContext(glCtx); SDL_GL_DeleteContext(glCtx);
return 0; return 0;
} }
alcMakeContextCurrent(alcCtx); alcMakeContextCurrent(alcCtx);
SharedState::initInstance(threadData); try
{
SharedState::initInstance(threadData);
}
catch (const Exception &exc)
{
rgssThreadError(threadData, exc.msg);
alcDestroyContext(alcCtx);
alcCloseDevice(alcDev);
SDL_GL_DeleteContext(glCtx);
return 0;
}
/* Start script execution */ /* Start script execution */
scriptBinding->execute(); scriptBinding->execute();

View File

@ -35,6 +35,7 @@
#include "global-ibo.h" #include "global-ibo.h"
#include "quad.h" #include "quad.h"
#include "binding.h" #include "binding.h"
#include "exception.h"
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
@ -93,8 +94,13 @@ struct SharedStatePrivate
{ {
if (!config.gameFolder.empty()) if (!config.gameFolder.empty())
{ {
int unused = chdir(config.gameFolder.c_str()); int result = chdir(config.gameFolder.c_str());
(void) unused;
if (result != 0)
throw Exception(Exception::MKXPError,
"Unable to switch into gameFolder '%s'",
config.gameFolder.c_str());
fileSystem.addPath("."); fileSystem.addPath(".");
} }
@ -139,14 +145,31 @@ struct SharedStatePrivate
void SharedState::initInstance(RGSSThreadData *threadData) void SharedState::initInstance(RGSSThreadData *threadData)
{ {
/* This section is tricky because of dependencies:
* SharedState depends on GlobalIBO existing,
* Font depends on SharedState existing */
globalIBO = new GlobalIBO(); globalIBO = new GlobalIBO();
globalIBO->ensureSize(1); globalIBO->ensureSize(1);
SharedState *state = new SharedState(threadData); SharedState::instance = 0;
Font *defaultFont = 0;
SharedState::instance = state; try
{
SharedState::instance = new SharedState(threadData);
defaultFont = new Font();
}
catch (const Exception &exc)
{
delete globalIBO;
delete SharedState::instance;
delete defaultFont;
state->p->defaultFont = new Font(); throw exc;
}
SharedState::instance->p->defaultFont = defaultFont;
} }
void SharedState::finiInstance() void SharedState::finiInstance()

View File

@ -106,6 +106,9 @@ struct SharedState
void checkShutdown(); void checkShutdown();
static SharedState *instance; static SharedState *instance;
/* This function will throw an Exception instance
* on initialization error */
static void initInstance(RGSSThreadData *threadData); static void initInstance(RGSSThreadData *threadData);
static void finiInstance(); static void finiInstance();