Make C++ exception works for lua

* Throw exception when open a file fails
This commit is contained in:
Eduardo Bart 2012-06-25 19:42:38 -03:00
parent 98a1b611bf
commit 472e78d368
5 changed files with 18 additions and 24 deletions

View File

@ -172,8 +172,7 @@ std::string ResourceManager::loadFile(const std::string& fileName)
bool ResourceManager::saveFile(const std::string& fileName, const uchar* data, uint size) bool ResourceManager::saveFile(const std::string& fileName, const uchar* data, uint size)
{ {
PHYSFS_file* file = PHYSFS_openWrite(fileName.c_str()); PHYSFS_file* file = PHYSFS_openWrite(fileName.c_str());
if(!file) if(!file) {
{
g_logger.error(PHYSFS_getLastError()); g_logger.error(PHYSFS_getLastError());
return false; return false;
} }
@ -205,30 +204,24 @@ FileStreamPtr ResourceManager::openFile(const std::string& fileName)
{ {
std::string fullPath = resolvePath(fileName); std::string fullPath = resolvePath(fileName);
PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str()); PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str());
if(!file) { if(!file)
g_logger.error(stdext::format("unable to open file '%s': %s", fullPath, PHYSFS_getLastError())); stdext::throw_exception(stdext::format("unable to open file '%s': %s", fullPath, PHYSFS_getLastError()));
return nullptr;
}
return FileStreamPtr(new FileStream(fullPath, file, false)); return FileStreamPtr(new FileStream(fullPath, file, false));
} }
FileStreamPtr ResourceManager::appendFile(const std::string& fileName) FileStreamPtr ResourceManager::appendFile(const std::string& fileName)
{ {
PHYSFS_File* file = PHYSFS_openAppend(fileName.c_str()); PHYSFS_File* file = PHYSFS_openAppend(fileName.c_str());
if(!file) { if(!file)
g_logger.error(stdext::format("failed to append file '%s': %s", fileName, PHYSFS_getLastError())); stdext::throw_exception(stdext::format("failed to append file '%s': %s", fileName, PHYSFS_getLastError()));
return nullptr;
}
return FileStreamPtr(new FileStream(fileName, file, true)); return FileStreamPtr(new FileStream(fileName, file, true));
} }
FileStreamPtr ResourceManager::createFile(const std::string& fileName) FileStreamPtr ResourceManager::createFile(const std::string& fileName)
{ {
PHYSFS_File* file = PHYSFS_openWrite(fileName.c_str()); PHYSFS_File* file = PHYSFS_openWrite(fileName.c_str());
if(!file) { if(!file)
g_logger.error(stdext::format("failed to create file '%s': %s", fileName, PHYSFS_getLastError())); stdext::throw_exception(stdext::format("failed to create file '%s': %s", fileName, PHYSFS_getLastError()));
return nullptr;
}
return FileStreamPtr(new FileStream(fileName, file, true)); return FileStreamPtr(new FileStream(fileName, file, true));
} }
@ -237,7 +230,6 @@ bool ResourceManager::deleteFile(const std::string& fileName)
return PHYSFS_delete(resolvePath(fileName).c_str()) != 0; return PHYSFS_delete(resolvePath(fileName).c_str()) != 0;
} }
bool ResourceManager::makeDir(const std::string directory) bool ResourceManager::makeDir(const std::string directory)
{ {
return PHYSFS_mkdir(directory.c_str()); return PHYSFS_mkdir(directory.c_str());

View File

@ -607,8 +607,12 @@ int LuaInterface::luaCppFunctionCallback(lua_State* L)
numRets = (*(funcPtr->get()))(&g_lua); numRets = (*(funcPtr->get()))(&g_lua);
g_lua.m_cppCallbackDepth--; g_lua.m_cppCallbackDepth--;
assert(numRets == g_lua.stackSize()); assert(numRets == g_lua.stackSize());
} catch(LuaException &e) { } catch(stdext::exception &e) {
g_logger.error(stdext::format("lua cpp callback failed: %s", e.what())); // cleanup stack
while(g_lua.stackSize() > 0)
g_lua.pop();
numRets = 0;
g_logger.error(stdext::format("C++ call failed: %s", g_lua.traceback(e.what())));
} }
return numRets; return numRets;

View File

@ -122,11 +122,11 @@ bool InputMessage::canRead(int bytes)
void InputMessage::checkRead(int bytes) void InputMessage::checkRead(int bytes)
{ {
if(!canRead(bytes)) if(!canRead(bytes))
g_lua.throwError("InputMessage eof reached"); throw stdext::exception("InputMessage eof reached");
} }
void InputMessage::checkWrite(int bytes) void InputMessage::checkWrite(int bytes)
{ {
if(bytes > BUFFER_MAXSIZE) if(bytes > BUFFER_MAXSIZE)
g_lua.throwError("InputMessage max buffer size reached"); throw stdext::exception("InputMessage max buffer size reached");
} }

View File

@ -71,7 +71,7 @@ void OutputMessage::addString(const std::string& buffer)
{ {
int len = buffer.length(); int len = buffer.length();
if(len > MAX_STRING_LENGTH) if(len > MAX_STRING_LENGTH)
g_lua.throwError(stdext::format("string length > %d", MAX_STRING_LENGTH)); throw stdext::exception(stdext::format("string length > %d", MAX_STRING_LENGTH));
checkWrite(len + 2); checkWrite(len + 2);
addU16(len); addU16(len);
memcpy((char*)(m_buffer + m_writePos), buffer.c_str(), len); memcpy((char*)(m_buffer + m_writePos), buffer.c_str(), len);
@ -92,7 +92,7 @@ void OutputMessage::addPaddingBytes(int bytes, uint8 byte)
void OutputMessage::encryptRSA(int size, const std::string& key) void OutputMessage::encryptRSA(int size, const std::string& key)
{ {
if(m_messageSize < size) if(m_messageSize < size)
g_lua.throwError("insufficient bytes in buffer to encrypt"); throw stdext::exception("insufficient bytes in buffer to encrypt");
RSA::encrypt((char*)m_buffer + m_writePos - size, size, key.c_str()); RSA::encrypt((char*)m_buffer + m_writePos - size, size, key.c_str());
} }
@ -124,5 +124,5 @@ bool OutputMessage::canWrite(int bytes)
void OutputMessage::checkWrite(int bytes) void OutputMessage::checkWrite(int bytes)
{ {
if(!canWrite(bytes)) if(!canWrite(bytes))
g_lua.throwError("OutputMessage max buffer size reached"); throw stdext::exception("OutputMessage max buffer size reached");
} }

View File

@ -94,8 +94,6 @@ bool ThingTypeManager::loadOtb(const std::string& file)
{ {
try { try {
FileStreamPtr fin = g_resources.openFile(file); FileStreamPtr fin = g_resources.openFile(file);
if(!fin)
stdext::throw_exception("unable to open file");
uint signature = fin->getU32(); uint signature = fin->getU32();
if(signature != 0) if(signature != 0)