fix bug on how lua detect source paths

This commit is contained in:
Eduardo Bart
2011-05-21 15:40:06 -03:00
parent 80e42b0f96
commit f5a15939b2
9 changed files with 78 additions and 66 deletions

View File

@@ -105,10 +105,11 @@ bool Resources::directoryExists(const std::string& directoryName)
bool Resources::loadFile(const std::string& fileName, std::iostream& out)
{
std::string fullPath = resolvePath(fileName);
out.clear(std::ios::goodbit);
PHYSFS_file *file = PHYSFS_openRead(resolvePath(fileName).c_str());
PHYSFS_file *file = PHYSFS_openRead(fullPath.c_str());
if(!file) {
flogError("ERROR: Failed to load file \"%s\": %s", fileName.c_str() % PHYSFS_getLastError());
flogError("ERROR: Failed to load file \"%s\": %s", fullPath.c_str() % PHYSFS_getLastError());
out.clear(std::ios::failbit);
return false;
} else {

View File

@@ -379,20 +379,25 @@ void LuaScript::pushRef(int ref)
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
}
std::string LuaScript::getFunctionSourcePath()
std::string LuaScript::getFunctionSourcePath(bool functionIsOnStack, int level)
{
std::string path;
lua_Debug ar;
memset(&ar, 0, sizeof(ar));
lua_getinfo(L, ">Sn", &ar);
if(functionIsOnStack)
lua_getinfo(L, ">Sn", &ar);
else {
if(lua_getstack(L, level-1, &ar) == 1)
lua_getinfo(L, "Sn", &ar);
}
// c function, we must get information of level above
// c function, we must get information of a level above
if(strcmp("C", ar.what) == 0) {
memset(&ar, 0, sizeof(ar));
if(lua_getstack(L, 1, &ar) == 1) {
if(lua_getstack(L, level, &ar) == 1) {
lua_getinfo(L, "f", &ar);
return getFunctionSourcePath();
return getFunctionSourcePath(true, level+1);
}
} else {
@@ -411,7 +416,7 @@ std::string LuaScript::getFunctionSourcePath()
bool LuaScript::callFunction(int numArgs, int numRets)
{
pushValue(-numArgs - 1);
g_resources.pushCurrentPath(getFunctionSourcePath());
g_resources.pushCurrentPath(getFunctionSourcePath(true));
int size = getStackSize();
int errorIndex = -numArgs - 2;
@@ -726,8 +731,14 @@ int LuaScript::luaFunctionCallback(lua_State* L)
{
// look for function id
int id = lua_tonumber(L, lua_upvalueindex(1));
g_resources.pushCurrentPath(g_lua.getFunctionSourcePath(false));
// call the function
return (*(g_lua.m_functions[id]))();
int ret = (*(g_lua.m_functions[id]))();
g_resources.popCurrentPath();
return ret;
}
int LuaScript::luaErrorHandler(lua_State *L)

View File

@@ -104,7 +104,7 @@ public:
void pushValue(int index = -1);
void pushRef(int ref);
std::string getFunctionSourcePath();
std::string getFunctionSourcePath(bool functionIsOnStack, int level = 1);
bool callFunction(int numArgs = 0, int numRets = 0);
void callModuleField(const std::string& module, const std::string& field);

View File

@@ -71,7 +71,7 @@ UIElementPtr UILoader::loadFromFile(std::string filePath, const UIContainerPtr&
{
std::stringstream fin;
if(!g_resources.loadFile(filePath, fin)) {
flogError("ERROR: Could not load ui file \"%s", filePath.c_str());
flogError("ERROR: Could not load ui %s", filePath.c_str());
return UIElementPtr();
}
@@ -106,7 +106,7 @@ UIElementPtr UILoader::loadFromFile(std::string filePath, const UIContainerPtr&
element->onLoad();
return element;
} else {
flogError("ERROR: Failed to load ui file \"%s\":\n %s", filePath.c_str() % parser.getErrorMessage());
flogError("ERROR: Failed to load ui %s: %s", filePath.c_str() % parser.getErrorMessage());
}
return UIElementPtr();