From 472e78d3685bb14c545977c2656b71f0479ed899 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Mon, 25 Jun 2012 19:42:38 -0300 Subject: [PATCH] Make C++ exception works for lua * Throw exception when open a file fails --- src/framework/core/resourcemanager.cpp | 22 +++++++--------------- src/framework/luascript/luainterface.cpp | 8 ++++++-- src/framework/net/inputmessage.cpp | 4 ++-- src/framework/net/outputmessage.cpp | 6 +++--- src/otclient/core/thingtypemanager.cpp | 2 -- 5 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/framework/core/resourcemanager.cpp b/src/framework/core/resourcemanager.cpp index 56d8d0fb..8346cad9 100644 --- a/src/framework/core/resourcemanager.cpp +++ b/src/framework/core/resourcemanager.cpp @@ -172,8 +172,7 @@ std::string ResourceManager::loadFile(const std::string& fileName) bool ResourceManager::saveFile(const std::string& fileName, const uchar* data, uint size) { PHYSFS_file* file = PHYSFS_openWrite(fileName.c_str()); - if(!file) - { + if(!file) { g_logger.error(PHYSFS_getLastError()); return false; } @@ -205,30 +204,24 @@ FileStreamPtr ResourceManager::openFile(const std::string& fileName) { std::string fullPath = resolvePath(fileName); PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str()); - if(!file) { - g_logger.error(stdext::format("unable to open file '%s': %s", fullPath, PHYSFS_getLastError())); - return nullptr; - } + if(!file) + stdext::throw_exception(stdext::format("unable to open file '%s': %s", fullPath, PHYSFS_getLastError())); return FileStreamPtr(new FileStream(fullPath, file, false)); } FileStreamPtr ResourceManager::appendFile(const std::string& fileName) { PHYSFS_File* file = PHYSFS_openAppend(fileName.c_str()); - if(!file) { - g_logger.error(stdext::format("failed to append file '%s': %s", fileName, PHYSFS_getLastError())); - return nullptr; - } + if(!file) + stdext::throw_exception(stdext::format("failed to append file '%s': %s", fileName, PHYSFS_getLastError())); return FileStreamPtr(new FileStream(fileName, file, true)); } FileStreamPtr ResourceManager::createFile(const std::string& fileName) { PHYSFS_File* file = PHYSFS_openWrite(fileName.c_str()); - if(!file) { - g_logger.error(stdext::format("failed to create file '%s': %s", fileName, PHYSFS_getLastError())); - return nullptr; - } + if(!file) + stdext::throw_exception(stdext::format("failed to create file '%s': %s", fileName, PHYSFS_getLastError())); 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; } - bool ResourceManager::makeDir(const std::string directory) { return PHYSFS_mkdir(directory.c_str()); diff --git a/src/framework/luascript/luainterface.cpp b/src/framework/luascript/luainterface.cpp index b1c4014c..f5539383 100644 --- a/src/framework/luascript/luainterface.cpp +++ b/src/framework/luascript/luainterface.cpp @@ -607,8 +607,12 @@ int LuaInterface::luaCppFunctionCallback(lua_State* L) numRets = (*(funcPtr->get()))(&g_lua); g_lua.m_cppCallbackDepth--; assert(numRets == g_lua.stackSize()); - } catch(LuaException &e) { - g_logger.error(stdext::format("lua cpp callback failed: %s", e.what())); + } catch(stdext::exception &e) { + // 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; diff --git a/src/framework/net/inputmessage.cpp b/src/framework/net/inputmessage.cpp index 4abaea87..807af7a9 100644 --- a/src/framework/net/inputmessage.cpp +++ b/src/framework/net/inputmessage.cpp @@ -122,11 +122,11 @@ bool InputMessage::canRead(int bytes) void InputMessage::checkRead(int bytes) { if(!canRead(bytes)) - g_lua.throwError("InputMessage eof reached"); + throw stdext::exception("InputMessage eof reached"); } void InputMessage::checkWrite(int bytes) { if(bytes > BUFFER_MAXSIZE) - g_lua.throwError("InputMessage max buffer size reached"); + throw stdext::exception("InputMessage max buffer size reached"); } diff --git a/src/framework/net/outputmessage.cpp b/src/framework/net/outputmessage.cpp index d2df8581..f8a38425 100644 --- a/src/framework/net/outputmessage.cpp +++ b/src/framework/net/outputmessage.cpp @@ -71,7 +71,7 @@ void OutputMessage::addString(const std::string& buffer) { int len = buffer.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); addU16(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) { 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()); } @@ -124,5 +124,5 @@ bool OutputMessage::canWrite(int bytes) void OutputMessage::checkWrite(int bytes) { if(!canWrite(bytes)) - g_lua.throwError("OutputMessage max buffer size reached"); + throw stdext::exception("OutputMessage max buffer size reached"); } diff --git a/src/otclient/core/thingtypemanager.cpp b/src/otclient/core/thingtypemanager.cpp index d3b54d8d..1ea9ad8f 100644 --- a/src/otclient/core/thingtypemanager.cpp +++ b/src/otclient/core/thingtypemanager.cpp @@ -94,8 +94,6 @@ bool ThingTypeManager::loadOtb(const std::string& file) { try { FileStreamPtr fin = g_resources.openFile(file); - if(!fin) - stdext::throw_exception("unable to open file"); uint signature = fin->getU32(); if(signature != 0)