From f5a15939b2cd8499969745b0addee500c130f2ce Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Sat, 21 May 2011 15:40:06 -0300 Subject: [PATCH] fix bug on how lua detect source paths --- data/modules/mainmenu/entergamewindow.yml | 22 ++++++------- data/modules/mainmenu/infowindow.yml | 26 +++++++-------- data/modules/mainmenu/mainmenu.yml | 18 +++++----- data/modules/mainmenu/optionswindow.yml | 40 +++++++++++------------ data/modules/messagebox/messagebox.lua | 2 +- src/framework/core/resources.cpp | 5 +-- src/framework/script/luascript.cpp | 25 ++++++++++---- src/framework/script/luascript.h | 2 +- src/framework/ui/uiloader.cpp | 4 +-- 9 files changed, 78 insertions(+), 66 deletions(-) diff --git a/data/modules/mainmenu/entergamewindow.yml b/data/modules/mainmenu/entergamewindow.yml index b07af1e4..228d342b 100644 --- a/data/modules/mainmenu/entergamewindow.yml +++ b/data/modules/mainmenu/entergamewindow.yml @@ -1,34 +1,34 @@ -window#enterGameWindow: +window#enterGameWindow title: Enter Game size: [236, 178] anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter onLoad: self.locked = true - label#accountNameLabel: + label#accountNameLabel text: Account name anchors.left: parent.left anchors.top: parent.top margin.left: 18 margin.top: 33 - label#passwordLabel: + label#passwordLabel text: "Password:" anchors.left: parent.left anchors.top: parent.top margin.left: 18 margin.top: 62 - label#createAccountLabel: + label#createAccountLabel text: | - If you don't have - an account yet + If you don't have + an account yet anchors.left: parent.left anchors.top: parent.top margin.left: 18 margin.top: 87 - button#createAccountButton: + button#createAccountButton text: Create Account anchors.left: parent.left anchors.top: parent.top @@ -36,7 +36,7 @@ window#enterGameWindow: margin.left: 132 onClick: messageBox("Error", "Not implemented yet") - button#okButton: + button#okButton text: Ok size: [43, 20] anchors.right: parent.right @@ -44,7 +44,7 @@ window#enterGameWindow: margin.bottom: 10 margin.right: 66 - button#cancelButton: + button#cancelButton text: Cancel size: [43, 20] anchors.right: parent.right @@ -53,13 +53,13 @@ window#enterGameWindow: margin.right: 13 onClick: self.parent:destroy() - textEdit#accountNameTextEdit: + textEdit#accountNameTextEdit anchors.right: parent.right anchors.top: parent.top margin.top: 32 margin.right: 18 - textEdit#passwordTextEdit: + textEdit#passwordTextEdit anchors.right: parent.right anchors.top: parent.top margin.top: 61 diff --git a/data/modules/mainmenu/infowindow.yml b/data/modules/mainmenu/infowindow.yml index 67d8e95b..ec6a90b9 100644 --- a/data/modules/mainmenu/infowindow.yml +++ b/data/modules/mainmenu/infowindow.yml @@ -1,11 +1,11 @@ -window#infoWindow: +window#infoWindow title: Info size: [244, 221] anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter onLoad: self.locked = true - panel#infoPanel: + panel#infoPanel skin: flatPanel size: [208, 129] anchors.left: parent.left @@ -13,31 +13,31 @@ window#infoWindow: margin.top: 32 margin.left: 18 - label#infoLabel: + label#infoLabel align: center text: |- - OTClient - Version 0.2.0 - Created by edubart + OTClient + Version 0.2.0 + Created by edubart anchors.horizontalCenter: parent.horizontalCenter anchors.top: parent.top margin.top: 20 - - lineDecoration#bottomSeparator: - size: [190,2] + + lineDecoration#bottomSeparator + size: [190, 2] anchors.left: parent.left anchors.top: parent.top margin.top: 83 margin.left: 9 - label#websiteLabel: + label#websiteLabel text: Official Website anchors.left: parent.left anchors.bottom: parent.bottom margin.bottom: 14 margin.left: 9 - button#websiteButton: + button#websiteButton text: Github Page size: [80,22] anchors.right: parent.right @@ -45,7 +45,7 @@ window#infoWindow: margin.bottom: 9 margin.right: 9 - lineDecoration#bottomSeparator: + lineDecoration#bottomSeparator anchors.left: parent.left anchors.right: parent.right anchors.bottom: parent.bottom @@ -53,7 +53,7 @@ window#infoWindow: margin.left: 13 margin.right: 13 - button#okButton: + button#okButton text: Ok size: [43, 20] anchors.left: parent.left diff --git a/data/modules/mainmenu/mainmenu.yml b/data/modules/mainmenu/mainmenu.yml index 84489f30..5815741a 100644 --- a/data/modules/mainmenu/mainmenu.yml +++ b/data/modules/mainmenu/mainmenu.yml @@ -1,11 +1,11 @@ -panel#background: +panel#background skin: mainMenuBackground anchors.left: parent.left anchors.right: parent.right anchors.top: parent.top anchors.bottom: parent.bottom - panel#icos4d: + panel#icos4d skin: image: /skins/lightness/mouse.png anchors.left: parent.left @@ -13,7 +13,7 @@ panel#background: margin.left: -2 margin.top: 70 - panel#mouse: + panel#mouse skin: image: /skins/lightness/icos4d.png anchors.right: parent.right @@ -21,7 +21,7 @@ panel#background: margin.left: 60 margin.top: 70 - panel#mainMenu: + panel#mainMenu skin: roundedGridPanel size: [117, 171] anchors.left: parent.left @@ -29,35 +29,35 @@ panel#background: margin.left: 60 margin.bottom: 70 - button#enterGameButton: + button#enterGameButton text: Enter Game anchors.top: parent.top anchors.horizontalCenter: parent.horizontalCenter margin.top: 16 onClick: UI.load("entergamewindow.yml") - button#accessAccountButton: + button#accessAccountButton text: Access Account anchors.top: parent.top anchors.horizontalCenter: parent.horizontalCenter margin.top: 46 onClick: messageBox("Error", "Not implemented yet") - button#optionsButton: + button#optionsButton text: Options anchors.top: parent.top anchors.horizontalCenter: parent.horizontalCenter margin.top: 76 onClick: UI.load("optionswindow.yml") - button#infoButton: + button#infoButton text: Info anchors.top: parent.top anchors.horizontalCenter: parent.horizontalCenter margin.top: 106 onClick: UI.load("infowindow.yml") - button#exitGameButton: + button#exitGameButton text: Exit anchors.top: parent.top anchors.horizontalCenter: parent.horizontalCenter diff --git a/data/modules/mainmenu/optionswindow.yml b/data/modules/mainmenu/optionswindow.yml index 3ef3f96c..11f380f5 100644 --- a/data/modules/mainmenu/optionswindow.yml +++ b/data/modules/mainmenu/optionswindow.yml @@ -1,4 +1,4 @@ -window#optionsWindow: +window#optionsWindow title: Options size: [286, 262] anchors.horizontalCenter: parent.horizontalCenter @@ -6,48 +6,48 @@ window#optionsWindow: onLoad: self.locked = true # general - button#generalButton: + button#generalButton text: General anchors.left: parent.left anchors.top: parent.top margin.left: 18 margin.top: 32 - label#generalLabel: + label#generalLabel text: |- - Change general - game options + Change general + game options anchors.left: parent.left anchors.top: parent.top margin.left: 117 margin.top: 29 # graphics - button#graphicsButton: + button#graphicsButton text: Graphics anchors.left: parent.left anchors.top: parent.top margin.left: 18 margin.top: 65 - label#graphicsLabel: + label#graphicsLabel text: |- - Change graphics and - performance settings + Change graphics and + performance settings anchors.left: parent.left anchors.top: parent.top margin.left: 117 margin.top: 62 # console - button#consoleButton: + button#consoleButton text: Console anchors.left: parent.left anchors.top: parent.top margin.left: 18 margin.top: 98 - label#consoleLabel: + label#consoleLabel text: Customise the console anchors.left: parent.left anchors.top: parent.top @@ -55,21 +55,21 @@ window#optionsWindow: margin.top: 95 # hotkeys - button#hotkeysButton: + button#hotkeysButton text: Hotkeys anchors.left: parent.left anchors.top: parent.top margin.left: 18 margin.top: 131 - label#hotkeysLabel: + label#hotkeysLabel text: Edit your hotkey texts anchors.left: parent.left anchors.top: parent.top margin.left: 117 margin.top: 128 - lineDecoration#middleSeparator: + lineDecoration#middleSeparator anchors.left: parent.left anchors.right: parent.right anchors.bottom: parent.bottom @@ -78,23 +78,23 @@ window#optionsWindow: margin.right: 18 # motd - button#motdButton: + button#motdButton text: Motd anchors.left: parent.left anchors.bottom: parent.bottom margin.left: 18 margin.bottom: 60 - label#motdLabel: + label#motdLabel text: |- - Show the most recent - Message of the Day + Show the most recent + Message of the Day anchors.left: parent.left anchors.bottom: parent.bottom margin.left: 117 margin.bottom: 56 - lineDecoration#bottomSeparator: + lineDecoration#bottomSeparator anchors.left: parent.left anchors.right: parent.right anchors.bottom: parent.bottom @@ -103,7 +103,7 @@ window#optionsWindow: margin.right: 13 # ok button - button#okButton: + button#okButton text: Ok size: [43, 20] anchors.right: parent.right diff --git a/data/modules/messagebox/messagebox.lua b/data/modules/messagebox/messagebox.lua index a27dd959..f4f67e14 100644 --- a/data/modules/messagebox/messagebox.lua +++ b/data/modules/messagebox/messagebox.lua @@ -7,5 +7,5 @@ function messageBox(title, text) msgBox.locked = true msgBox.title = title msgBox:child("textLabel").text = text - msgBox:child("okButton").onClick = autoDestroyParentz + msgBox:child("okButton").onClick = autoDestroyParent end diff --git a/src/framework/core/resources.cpp b/src/framework/core/resources.cpp index 354e10f8..608be2a6 100644 --- a/src/framework/core/resources.cpp +++ b/src/framework/core/resources.cpp @@ -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 { diff --git a/src/framework/script/luascript.cpp b/src/framework/script/luascript.cpp index b65f2f4b..3006b372 100644 --- a/src/framework/script/luascript.cpp +++ b/src/framework/script/luascript.cpp @@ -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) diff --git a/src/framework/script/luascript.h b/src/framework/script/luascript.h index 4ffb0a9d..0e9b615f 100644 --- a/src/framework/script/luascript.h +++ b/src/framework/script/luascript.h @@ -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); diff --git a/src/framework/ui/uiloader.cpp b/src/framework/ui/uiloader.cpp index 35a67387..133f6d16 100644 --- a/src/framework/ui/uiloader.cpp +++ b/src/framework/ui/uiloader.cpp @@ -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();