From 95a7d2667b7db8c468fbf5e227cb85a55e4c01e7 Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Mon, 21 May 2012 03:35:05 +0200 Subject: [PATCH] Some more Lua refactoring, started to break CLuaIHM into 2 parts. CLuaIHM will be generic, CLuaIHMRyzom will contain the Ryzom only code. --HG-- branch : gui-refactoring --- code/ryzom/client/src/commands.cpp | 3 +- .../src/interface_v3/interface_manager.cpp | 3 +- .../src/interface_v3/interface_parser.cpp | 2 + .../ryzom/client/src/interface_v3/lua_ihm.cpp | 3510 +---------------- code/ryzom/client/src/interface_v3/lua_ihm.h | 243 +- .../client/src/interface_v3/lua_ihm_ryzom.cpp | 3271 +++++++++++++++ .../client/src/interface_v3/lua_ihm_ryzom.h | 257 ++ .../client/src/interface_v3/lua_object.cpp | 7 +- .../client/src/interface_v3/view_text.cpp | 7 +- code/ryzom/client/src/r2/displayer_lua.cpp | 5 +- .../client/src/r2/dmc/com_lua_module.cpp | 3 +- code/ryzom/client/src/r2/editor.cpp | 13 +- code/ryzom/client/src/r2/instance.cpp | 3 +- .../client/src/r2/object_factory_client.cpp | 3 +- 14 files changed, 3779 insertions(+), 3551 deletions(-) create mode 100644 code/ryzom/client/src/interface_v3/lua_ihm_ryzom.cpp create mode 100644 code/ryzom/client/src/interface_v3/lua_ihm_ryzom.h diff --git a/code/ryzom/client/src/commands.cpp b/code/ryzom/client/src/commands.cpp index 6c804e93d..433bd2d49 100644 --- a/code/ryzom/client/src/commands.cpp +++ b/code/ryzom/client/src/commands.cpp @@ -94,6 +94,7 @@ #include "connection.h" #include "interface_v3/lua_object.h" #include "interface_v3/lua_ihm.h" +#include "interface_v3/lua_ihm_ryzom.h" #include "init.h" #include "interface_v3/people_interraction.h" #include "far_tp.h" @@ -5157,7 +5158,7 @@ NLMISC_COMMAND(luaObject, "Dump the content of a lua object", " [max } catch(const ELuaError &e) { - CLuaIHM::debugInfo(e.what()); + CLuaIHMRyzom::debugInfo(e.what()); return false; } luaState->pushValue(LUA_GLOBALSINDEX); diff --git a/code/ryzom/client/src/interface_v3/interface_manager.cpp b/code/ryzom/client/src/interface_v3/interface_manager.cpp index d0445ce74..e162df279 100644 --- a/code/ryzom/client/src/interface_v3/interface_manager.cpp +++ b/code/ryzom/client/src/interface_v3/interface_manager.cpp @@ -113,6 +113,7 @@ namespace NLGUI } using namespace NLGUI; #include "lua_ihm.h" +#include "lua_ihm_ryzom.h" #include "add_on_manager.h" @@ -5750,7 +5751,7 @@ bool CInterfaceManager::executeLuaScript(const std::string &luaScript, bool smal // Hamster: quick fix on AJM code but sscanf is still awfull if (sscanf(msg.c_str(), "%s: %s.lua:%d:",exceptionName, filename, &line) == 3) // NB: test not exact here, but should work in 99,9 % of cases { - msg = CLuaIHM::createGotoFileButtonTag(filename, line) + msg; + msg = CLuaIHMRyzom::createGotoFileButtonTag(filename, line) + msg; nlwarning(LuaHelperStuff::formatLuaErrorNlWarn(msg).c_str()); displaySystemInfo(LuaHelperStuff::formatLuaErrorSysInfo(msg)); } diff --git a/code/ryzom/client/src/interface_v3/interface_parser.cpp b/code/ryzom/client/src/interface_v3/interface_parser.cpp index f8371d8d2..16232de19 100644 --- a/code/ryzom/client/src/interface_v3/interface_parser.cpp +++ b/code/ryzom/client/src/interface_v3/interface_parser.cpp @@ -108,6 +108,7 @@ #include "nel/gui/lua_helper.h" using namespace NLGUI; #include "lua_ihm.h" +#include "lua_ihm_ryzom.h" #include "../r2/editor.h" #ifdef LUA_NEVRAX_VERSION @@ -4671,6 +4672,7 @@ void CInterfaceParser::initLUA() // register LUA methods CLuaIHM::registerAll(*_LuaState); + CLuaIHMRyzom::RegisterRyzomFunctions( *_LuaState ); } // *************************************************************************** diff --git a/code/ryzom/client/src/interface_v3/lua_ihm.cpp b/code/ryzom/client/src/interface_v3/lua_ihm.cpp index 61b1b4d2c..c6cf16b37 100644 --- a/code/ryzom/client/src/interface_v3/lua_ihm.cpp +++ b/code/ryzom/client/src/interface_v3/lua_ihm.cpp @@ -218,6 +218,20 @@ const CReflectedProperty *CReflectableLuaRef::getProp(const char *luaStringPtr) return prop; } +// *************************************************************************** +static sint32 getTargetSlotNr() +{ + const char *dbPath = "UI:VARIABLES:TARGET:SLOT"; + CInterfaceManager *im = CInterfaceManager::getInstance(); + CCDBNodeLeaf *node = im->getDbProp(dbPath, false); + if (!node) return 0; + if ((uint8) node->getValue32() == (uint8) CLFECOMMON::INVALID_SLOT) + { + return 0; + } + return node->getValue32(); +} + // *************************************************************************** bool CLuaIHM::pop(CLuaState &ls, NLMISC::CRGBA &dest) { @@ -324,44 +338,6 @@ void CLuaIHM::push(CLuaState &ls, const ucstring &value) // *************************************************************************** -// *************************************************************************** -class CHandlerLUA : public IActionHandler -{ -public: - void execute (CCtrlBase *pCaller, const std::string &sParams) - { - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - - // For getUI() LUA function, push the UI caller - if(pCaller) - _UICallerStack.push_back(pCaller); - - // execute a small script. NB: use a small script here because - // most often action handlers are called from xml files => lot of redundant script - pIM->executeLuaScript(sParams, true); - - // pop UI caller - if(pCaller) - _UICallerStack.pop_back(); - } - - // get the top of stack Caller to this LUA script - static CCtrlBase *getUICaller(); - -private: - static std::deque > _UICallerStack; -}; -REGISTER_ACTION_HANDLER( CHandlerLUA, "lua"); -std::deque > CHandlerLUA::_UICallerStack; - -CCtrlBase *CHandlerLUA::getUICaller() -{ - if(_UICallerStack.empty()) - return NULL; - else - return _UICallerStack.back(); -} - // *************************************************************************** // Allow also to call script from expression @@ -1125,63 +1101,6 @@ int CLuaIHM::luaUINext(CLuaState &ls) return 0; } -int CLuaIHM::getClientCfgVar(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getClientCfgVar) - const char *funcName = "getClientCfgVar"; - CLuaIHM::checkArgCount(ls, funcName, 1); - CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); - std::string varName = ls.toString(1); - - CConfigFile::CVar *v = ClientCfg.ConfigFile.getVarPtr(varName); - if (!v) return 0; - if(v->size()==1) - { - switch(v->Type) - { - case CConfigFile::CVar::T_REAL: - ls.push((double) v->asDouble()); - return 1; - break; - case CConfigFile::CVar::T_STRING: - ls.push(v->asString()); - return 1; - break; - default: // handle both T_INT && T_BOOL - case CConfigFile::CVar::T_INT: - ls.push((double) v->asInt()); - return 1; - break; - } - } - else - { - ls.newTable(); - CLuaObject result(ls); - uint count = 0; - for(uint i = 0; iStrValues.size(); i++) - { - result.setValue(toString(count).c_str(), v->StrValues[i]); - count++; - } - for(uint i = 0; iIntValues.size(); i++) - { - result.setValue(toString(count).c_str(), (double)v->IntValues[i]); - count++; - } - for(uint i = 0; iRealValues.size(); i++) - { - result.setValue(toString(count).c_str(), (double)v->RealValues[i]); - count++; - } - result.push(); - return 1; - } - - return 0; -} - - // *************************************************************************** int CLuaIHM::luaClientCfgIndex(CLuaState &ls) { @@ -1307,165 +1226,17 @@ void CLuaIHM::registerIHM(CLuaState &ls) // *** Register Functions - // Through std LUA API - ls.registerFunc("setOnDraw", setOnDraw); - ls.registerFunc("setCaptureKeyboard", setCaptureKeyboard); - ls.registerFunc("resetCaptureKeyboard", resetCaptureKeyboard); - ls.registerFunc("validMessageBox", validMessageBox); - ls.registerFunc("setTopWindow", setTopWindow); - ls.registerFunc("concatUCString", concatUCString); - ls.registerFunc("concatString", concatString); - ls.registerFunc("tableToString", tableToString); - ls.registerFunc("addOnDbChange", addOnDbChange); - ls.registerFunc("removeOnDbChange", removeOnDbChange); - ls.registerFunc("getUICaller", getUICaller); - ls.registerFunc("getCurrentWindowUnder", getCurrentWindowUnder); - ls.registerFunc("getUI", getUI); - ls.registerFunc("getIndexInDB", getIndexInDB); - ls.registerFunc("getUIId", getUIId); - ls.registerFunc("createGroupInstance", createGroupInstance); - ls.registerFunc("createRootGroupInstance", createRootGroupInstance); - ls.registerFunc("createUIElement", createUIElement); - ls.registerFunc("launchContextMenuInGame", launchContextMenuInGame); - ls.registerFunc("parseInterfaceFromString", parseInterfaceFromString); - ls.registerFunc("updateAllLocalisedElements", updateAllLocalisedElements); - ls.registerFunc("runAH", runAH); - ls.registerFunc("runExpr", runExpr); - ls.registerFunc("runFct", runFct); - ls.registerFunc("runCommand", runCommand); - ls.registerFunc("formatUI", formatUI); - ls.registerFunc("formatDB", formatDB); - ls.registerFunc("deleteUI", deleteUI); - ls.registerFunc("deleteReflectable", deleteReflectable); - ls.registerFunc("dumpUI", dumpUI); - ls.registerFunc("setKeyboardContext", setKeyboardContext); - ls.registerFunc("breakPoint", breakPoint); - ls.registerFunc("getWindowSize", getWindowSize); - ls.registerFunc("setTextFormatTaged", setTextFormatTaged); - ls.registerFunc("initEmotesMenu", initEmotesMenu); - ls.registerFunc("isUCString", isUCString); - ls.registerFunc("hideAllWindows", hideAllWindows); - ls.registerFunc("hideAllNonSavableWindows", hideAllNonSavableWindows); - ls.registerFunc("getDesktopIndex", getDesktopIndex); - ls.registerFunc("setLuaBreakPoint", setLuaBreakPoint); - ls.registerFunc("getMainPageURL", getMainPageURL); - ls.registerFunc("getCharSlot", getCharSlot); - ls.registerFunc("getPathContent", getPathContent); - ls.registerFunc("getServerSeason", getServerSeason); - ls.registerFunc("computeCurrSeason", computeCurrSeason); - ls.registerFunc("getAutoSeason", getAutoSeason); - ls.registerFunc("getTextureSize", getTextureSize); - ls.registerFunc("enableModalWindow", enableModalWindow); - ls.registerFunc("disableModalWindow", disableModalWindow); - ls.registerFunc("getPlayerPos", getPlayerPos); - ls.registerFunc("getPlayerFront", getPlayerFront); - ls.registerFunc("getPlayerDirection", getPlayerDirection); - ls.registerFunc("getPlayerGender", getPlayerGender); - ls.registerFunc("getPlayerName", getPlayerName); - ls.registerFunc("getPlayerTitleRaw", getPlayerTitleRaw); - ls.registerFunc("getPlayerTitle", getPlayerTitle); - ls.registerFunc("getTargetPos", getTargetPos); - ls.registerFunc("getTargetFront", getTargetFront); - ls.registerFunc("getTargetDirection", getTargetDirection); - ls.registerFunc("getTargetGender", getTargetGender); - ls.registerFunc("getTargetName", getTargetName); - ls.registerFunc("getTargetTitleRaw", getTargetTitleRaw); - ls.registerFunc("getTargetTitle", getTargetTitle); - ls.registerFunc("addSearchPathUser", addSearchPathUser); - ls.registerFunc("displaySystemInfo", displaySystemInfo); - ls.registerFunc("disableContextHelpForControl", disableContextHelpForControl); - ls.registerFunc("disableContextHelp", disableContextHelp); - ls.registerFunc("setWeatherValue", setWeatherValue); - ls.registerFunc("getWeatherValue", getWeatherValue); - ls.registerFunc("getCompleteIslands", getCompleteIslands); - ls.registerFunc("displayBubble", displayBubble); - ls.registerFunc("getIslandId", getIslandId); - ls.registerFunc("getClientCfgVar", getClientCfgVar); - ls.registerFunc("isPlayerFreeTrial", isPlayerFreeTrial); - ls.registerFunc("isPlayerNewbie", isPlayerNewbie); - ls.registerFunc("isInRingMode", isInRingMode); - ls.registerFunc("getUserRace", getUserRace); - ls.registerFunc("getSheet2idx", getSheet2idx); - ls.registerFunc("getTargetSlot", getTargetSlot); - ls.registerFunc("getSlotDataSetId", getSlotDataSetId); // Through LUABind API lua_State *L= ls.getStatePointer(); luabind::module(L) [ - LUABIND_FUNC(addDbProp), - LUABIND_FUNC(getDbProp), - LUABIND_FUNC(setDbProp), - LUABIND_FUNC(delDbProp), - LUABIND_FUNC(debugInfo), - LUABIND_FUNC(rawDebugInfo), - LUABIND_FUNC(dumpCallStack), - LUABIND_FUNC(getDefine), - LUABIND_FUNC(setContextHelpText), - luabind::def("messageBox", (void(*)(const ucstring &)) &messageBox), - luabind::def("messageBox", (void(*)(const ucstring &, const std::string &)) &messageBox), - luabind::def("messageBox", (void(*)(const ucstring &, const std::string &, int caseMode)) &messageBox), - luabind::def("messageBox", (void(*)(const std::string &)) &messageBox), - luabind::def("messageBoxWithHelp", (void(*)(const ucstring &)) &messageBoxWithHelp), - luabind::def("messageBoxWithHelp", (void(*)(const ucstring &, const std::string &)) &messageBoxWithHelp), - luabind::def("messageBoxWithHelp", (void(*)(const ucstring &, const std::string &, int caseMode)) &messageBoxWithHelp), - luabind::def("messageBoxWithHelp", (void(*)(const std::string &)) &messageBoxWithHelp), luabind::def("findReplaceAll", (std::string(*)(const std::string &, const std::string &, const std::string &)) &findReplaceAll), luabind::def("findReplaceAll", (ucstring(*)(const ucstring &, const ucstring &, const ucstring &)) &findReplaceAll), luabind::def("findReplaceAll", (ucstring(*)(const ucstring &, const std::string &, const std::string &)) &findReplaceAll), luabind::def("findReplaceAll", (ucstring(*)(const ucstring &, const ucstring &, const std::string &)) &findReplaceAll), luabind::def("findReplaceAll", (ucstring(*)(const ucstring &, const std::string &, const ucstring &)) &findReplaceAll), - LUABIND_FUNC(getPlayerSelectedSlot), - LUABIND_FUNC(isInGame), - LUABIND_FUNC(pauseBGDownloader), - LUABIND_FUNC(unpauseBGDownloader), - LUABIND_FUNC(requestBGDownloaderPriority), - LUABIND_FUNC(getBGDownloaderPriority), - LUABIND_FUNC(getPatchLastErrorMessage), - LUABIND_FUNC(isPlayerSlotNewbieLand), - LUABIND_FUNC(getSkillIdFromName), - LUABIND_FUNC(getSkillLocalizedName), - LUABIND_FUNC(getMaxSkillValue), - LUABIND_FUNC(getBaseSkillValueMaxChildren), - LUABIND_FUNC(getMagicResistChance), - LUABIND_FUNC(getDodgeParryChance), - LUABIND_FUNC(browseNpcWebPage), - LUABIND_FUNC(clearHtmlUndoRedo), - LUABIND_FUNC(getDynString), - LUABIND_FUNC(isDynStringAvailable), - LUABIND_FUNC(isFullyPatched), - LUABIND_FUNC(getSheetType), - LUABIND_FUNC(getSheetName), - LUABIND_FUNC(getFameIndex), - LUABIND_FUNC(getFameName), - LUABIND_FUNC(getFameDBIndex), - LUABIND_FUNC(getFirstTribeFameIndex), - LUABIND_FUNC(getNbTribeFameIndex), - LUABIND_FUNC(getClientCfg), - LUABIND_FUNC(fileExists), - LUABIND_FUNC(sendMsgToServer), - LUABIND_FUNC(sendMsgToServerPvpTag), - LUABIND_FUNC(isGuildQuitAvailable), - LUABIND_FUNC(sortGuildMembers), - LUABIND_FUNC(getNbGuildMembers), - LUABIND_FUNC(getGuildMemberName), - LUABIND_FUNC(getGuildMemberGrade), - LUABIND_FUNC(isR2Player), - LUABIND_FUNC(getR2PlayerRace), - LUABIND_FUNC(isR2PlayerMale), - LUABIND_FUNC(getCharacterSheetSkel), - LUABIND_FUNC(getSheetId), - LUABIND_FUNC(getCharacterSheetRegionForce), - LUABIND_FUNC(getCharacterSheetRegionLevel), - LUABIND_FUNC(replacePvpEffectParam), - LUABIND_FUNC(getRegionByAlias), - LUABIND_FUNC(tell), - LUABIND_FUNC(isRingAccessPointInReach), - LUABIND_FUNC(updateTooltipCoords), - LUABIND_FUNC(secondsSince1970ToHour), - LUABIND_FUNC(isCtrlKeyDown), - LUABIND_FUNC(encodeURLUnicodeParam), #if !FINAL_VERSION LUABIND_FUNC(openDoc), @@ -1473,29 +1244,9 @@ void CLuaIHM::registerIHM(CLuaState &ls) #endif luabind::def("fileLookup", CMiscFunctions::fileLookup), - luabind::def("shellExecute", CMiscFunctions::shellExecute), - - LUABIND_FUNC(getPlayerLevel), - LUABIND_FUNC(getPlayerVpa), - LUABIND_FUNC(getPlayerVpb), - LUABIND_FUNC(getPlayerVpc), - LUABIND_FUNC(getTargetLevel), - LUABIND_FUNC(getTargetForceRegion), - LUABIND_FUNC(getTargetLevelForce), - LUABIND_FUNC(getTargetSheet), - LUABIND_FUNC(getTargetVpa), - LUABIND_FUNC(getTargetVpb), - LUABIND_FUNC(getTargetVpc), - LUABIND_FUNC(isTargetNPC), - LUABIND_FUNC(isTargetPlayer), // return 'true' if the target is an npc - LUABIND_FUNC(isTargetUser), - LUABIND_FUNC(isPlayerInPVPMode), - LUABIND_FUNC(isTargetInPVPMode) + luabind::def("shellExecute", CMiscFunctions::shellExecute) ]; - LUABIND_ENUM(PVP_CLAN::TPVPClan, "game.TPVPClan", PVP_CLAN::NbClans, PVP_CLAN::toString); - LUABIND_ENUM(BONUS_MALUS::TBonusMalusSpecialTT, "game.TBonusMalusSpecialTT", BONUS_MALUS::NbSpecialTT, BONUS_MALUS::toString); - // inside i18n table luabind::module(L, "i18n") [ @@ -1677,3047 +1428,300 @@ uint32 CLuaIHM::getLocalTime() return (uint32) NLMISC::CTime::getLocalTime(); } + // *************************************************************************** -sint32 CLuaIHM::getDbProp(const std::string &dbProp) +static CEntityCL *getTargetEntity() { - //H_AUTO(Lua_CLuaIHM_getDbProp) - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - CCDBNodeLeaf *node= pIM->getDbProp(dbProp, false); - if(node) - return node->getValue32(); - else + const char *dbPath = "UI:VARIABLES:TARGET:SLOT"; + CInterfaceManager *im = CInterfaceManager::getInstance(); + CCDBNodeLeaf *node = im->getDbProp(dbPath, false); + if (!node) return NULL; + if ((uint8) node->getValue32() == (uint8) CLFECOMMON::INVALID_SLOT) { - debugInfo(toString("getDbProp(): '%s' dbProp Not found", dbProp.c_str())); - return 0; + return NULL; } + return EntitiesMngr.entity((uint) node->getValue32()); } -void CLuaIHM::setDbProp(const std::string &dbProp, sint32 value) +// *************************************************************************** +static CEntityCL *getSlotEntity(uint slot) { - //H_AUTO(Lua_CLuaIHM_setDbProp) - // Do not allow Write on SERVER: or LOCAL: - static const std::string dbServer= "SERVER:"; - static const std::string dbLocal= "LOCAL:"; - static const std::string dbLocalR2= "LOCAL:R2"; - if( (0==dbProp.compare(0, dbServer.size(), dbServer)) || - (0==dbProp.compare(0, dbLocal.size(), dbLocal)) - ) - { - if (0!=dbProp.compare(0, dbLocalR2.size(), dbLocalR2)) - { - nlstop; - throw ELuaIHMException("setDbProp(): You are not allowed to write on 'SERVER:...' or 'LOCAL:...' database"); - } - } - - // Write to the DB if found - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - CCDBNodeLeaf *node= pIM->getDbProp(dbProp, false); + return EntitiesMngr.entity(slot); +} - if(node) - node->setValue32(value); - else - debugInfo(toString("setDbProp(): '%s' dbProp Not found", dbProp.c_str())); +// *************************************************************************** +std::string CLuaIHM::findReplaceAll(const std::string &str, const std::string &search, const std::string &replace) +{ + //H_AUTO(Lua_CLuaIHM_findReplaceAll) + std::string ret= str; + while(strFindReplace(ret, search, replace)); + return ret; } -void CLuaIHM::delDbProp(const string &dbProp) +// *************************************************************************** +ucstring CLuaIHM::findReplaceAll(const ucstring &str, const ucstring &search, const ucstring &replace) { - //H_AUTO(Lua_CLuaIHM_setDbProp) - // Do not allow Write on SERVER: or LOCAL: - static const string dbServer= "SERVER:"; - static const string dbLocal= "LOCAL:"; - static const string dbLocalR2= "LOCAL:R2"; - if( (0==dbProp.compare(0, dbServer.size(), dbServer)) || - (0==dbProp.compare(0, dbLocal.size(), dbLocal)) - ) - { - if (0!=dbProp.compare(0, dbLocalR2.size(), dbLocalR2)) - { - nlstop; - throw ELuaIHMException("setDbProp(): You are not allowed to write on 'SERVER:...' or 'LOCAL:...' database"); - } - } + //H_AUTO(Lua_CLuaIHM_findReplaceAll) + ucstring ret= str; + while(strFindReplace(ret, search, replace)); + return ret; +} - // Write to the DB if found - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - pIM->delDbProp(dbProp); +// *************************************************************************** +ucstring CLuaIHM::findReplaceAll(const ucstring &str, const std::string &search, const std::string &replace) +{ + //H_AUTO(Lua_CLuaIHM_findReplaceAll) + return findReplaceAll(str, ucstring(search), ucstring(replace)); } -void CLuaIHM::addDbProp(const std::string &dbProp, sint32 value) +// *************************************************************************** +ucstring CLuaIHM::findReplaceAll(const ucstring &str, const std::string &search, const ucstring &replace) { - //H_AUTO(Lua_CLuaIHM_setDbProp) - // Do not allow Write on SERVER: or LOCAL: - static const std::string dbServer= "SERVER:"; - static const std::string dbLocal= "LOCAL:"; - static const std::string dbLocalR2= "LOCAL:R2"; - if( (0==dbProp.compare(0, dbServer.size(), dbServer)) || - (0==dbProp.compare(0, dbLocal.size(), dbLocal)) - ) - { - if (0!=dbProp.compare(0, dbLocalR2.size(), dbLocalR2)) - { - nlstop; - throw ELuaIHMException("setDbProp(): You are not allowed to write on 'SERVER:...' or 'LOCAL:...' database"); - } - } + //H_AUTO(Lua_CLuaIHM_findReplaceAll) + return findReplaceAll(str, ucstring(search), ucstring(replace)); +} - // Write to the DB if found - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - CCDBNodeLeaf *node= pIM->getDbProp(dbProp, true); - if(node) - node->setValue32(value); +// *************************************************************************** +ucstring CLuaIHM::findReplaceAll(const ucstring &str, const ucstring &search, const std::string &replace) +{ + //H_AUTO(Lua_CLuaIHM_findReplaceAll) + return findReplaceAll(str, ucstring(search), ucstring(replace)); } + // *************************************************************************** -void CLuaIHM::debugInfo(const std::string &cstDbg) +int CLuaIHM::runExprAndPushResult(CLuaState &ls, const std::string &expr) { - //H_AUTO(Lua_CLuaIHM_debugInfo) - if(ClientCfg.DisplayLuaDebugInfo) + //H_AUTO(Lua_CLuaIHM_runExprAndPushResult) + // Execute expression + CInterfaceExprValue value; + if (CInterfaceExpr::eval(expr, value, NULL)) { - std::string dbg = cstDbg; - if (ClientCfg.LuaDebugInfoGotoButtonEnabled) + switch(value.getType()) { - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - lua_State *ls = pIM->getLuaState()->getStatePointer(); - lua_Debug luaDbg; - if(lua_getstack (ls, 1, &luaDbg)) + case CInterfaceExprValue::Boolean: + ls.push(value.getBool()); + break; + case CInterfaceExprValue::Integer: + ls.push((double)value.getInteger()); + break; + case CInterfaceExprValue::Double: + ls.push(value.getDouble()); + break; + case CInterfaceExprValue::String: { - if(lua_getinfo(ls, "lS", &luaDbg)) + ucstring ucstr= value.getUCString(); + // Yoyo: dynamically decide whether must return a string or a ucstring + bool mustUseUCString= false; + for (uint i = 0; i < ucstr.size (); i++) + { + if (ucstr[i] > 255) + { + mustUseUCString= true; + break; + } + } + // push a ucstring? + if(mustUseUCString) + { +#if LUABIND_VERSION > 600 + luabind::detail::push(ls.getStatePointer(), ucstr); +#else + luabind::object obj(ls.getStatePointer(), ucstr); + obj.pushvalue(); +#endif + } + else { - // add a command button to jump to the wanted file - dbg = createGotoFileButtonTag(luaDbg.short_src, luaDbg.currentline) + dbg; + ls.push(ucstr.toString()); } + break; + } + case CInterfaceExprValue::RGBA: + { + CRGBA color = value.getRGBA(); +#if LUABIND_VERSION > 600 + luabind::detail::push(ls.getStatePointer(), color); +#else + luabind::object obj(ls.getStatePointer(), color); + obj.pushvalue(); +#endif + break; } + break; + case CInterfaceExprValue::UserType: // Yoyo: don't care UserType... + default: + ls.pushNil(); + break; } - rawDebugInfo(dbg); } + else + ls.pushNil(); + + return 1; } // *************************************************************************** -void CLuaIHM::rawDebugInfo(const std::string &dbg) +void CLuaIHM::fails(CLuaState &ls, const char *format, ...) { - //H_AUTO(Lua_CLuaIHM_rawDebugInfo) - if(ClientCfg.DisplayLuaDebugInfo) - { - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - if (!dbg.empty() && dbg[0] == '@') - { - // if color is already given use the message as it - NLMISC::InfoLog->displayRawNL(dbg.c_str()); - } - else - { - NLMISC::InfoLog->displayRawNL(LuaHelperStuff::formatLuaErrorSysInfo(dbg).c_str()); - } - #ifdef LUA_NEVRAX_VERSION - if (LuaDebuggerIDE) - { - LuaDebuggerIDE->debugInfo(dbg.c_str()); - } - #endif - pIM->displaySystemInfo( LuaHelperStuff::formatLuaErrorSysInfo(dbg)); - } + //H_AUTO(Lua_CLuaIHM_fails) + std::string reason; + NLMISC_CONVERT_VARGS (reason, format, NLMISC::MaxCStringSize); + std::string stack; + ls.getStackAsString(stack); + // use a std::exception, to avoid Nel Exception warning + throw ELuaIHMException("%s. Lua stack = \n %s", reason.c_str(), stack.c_str()); } + // *************************************************************************** -int CLuaIHM::displaySystemInfo(CLuaState &ls) +void CLuaIHM::checkArgCount(CLuaState &ls, const char* funcName, uint nArgs) { - //H_AUTO(Lua_CLuaIHM_displaySystemInfo) - const char *funcName = "displaySystemInfo"; - checkArgCount(ls, funcName, 2); - checkArgTypeUCString(ls, funcName, 1); - checkArgType(ls, funcName, 2, LUA_TSTRING); - ucstring msg; - nlverify(getUCStringOnStack(ls, 1, msg)); - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - pIM->displaySystemInfo(msg, ls.toString(2)); - return 0; + //H_AUTO(Lua_CLuaIHM_checkArgCount) + if(ls.getTop()!=(sint)nArgs) + { + fails(ls, "%s() need exactly %d arguments (tips : check between method & function call)", funcName, nArgs); + } } // *************************************************************************** -int CLuaIHM::setWeatherValue(CLuaState &ls) +void CLuaIHM::checkArgMin(CLuaState &ls, const char* funcName, uint nArgs) { - //H_AUTO(Lua_CLuaIHM_setWeatherValue) - const char *funcName = "setWeatherValue"; - checkArgMin(ls, funcName, 1); - checkArgMax(ls, funcName, 2); - checkArgType(ls, funcName, 1, LUA_TBOOLEAN); -// bool autoWeather = ls.toBoolean(1); - ClientCfg.ManualWeatherSetup = !ls.toBoolean(1); - if (ls.getTop() == 2) + //H_AUTO(Lua_CLuaIHM_checkArgMin) + if(ls.getTop()<(sint)nArgs) { - checkArgType(ls, funcName, 2, LUA_TNUMBER); - ManualWeatherValue = (float) ls.toNumber(2); + fails(ls, "%s() need at least %d arguments (tips : check between method & function call)", funcName, nArgs); } - return 0; } // *************************************************************************** -int CLuaIHM::getWeatherValue(CLuaState &ls) +void CLuaIHM::checkArgMax(CLuaState &ls,const char* funcName,uint nArgs) { - //H_AUTO(Lua_CLuaIHM_getWeatherValue) - const char *funcName = "getWeatherValue"; - checkArgCount(ls, funcName, 0); - uint64 currDay = RT.getRyzomDay(); - float currHour = (float) RT.getRyzomTime(); - ls.push(::getBlendedWeather(currDay, currHour, *WeatherFunctionParams, ContinentMngr.cur()->WeatherFunction)); - return 1; + //H_AUTO(Lua_CLuaIHM_checkArgMax) + if(ls.getTop()>(sint)nArgs) + { + fails(ls, "%s() need at most %d arguments.", funcName, nArgs); + } } -void CLuaIHM::dumpCallStack(int startStackLevel) +// *************************************************************************** +void CLuaIHM::check(CLuaState &ls, bool ok, const std::string &failReason) { - //H_AUTO(Lua_CLuaIHM_dumpCallStack) - if(ClientCfg.DisplayLuaDebugInfo) + //H_AUTO(Lua_CLuaIHM_check) + if(!ok) { - lua_Debug dbg; - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - lua_State *ls = pIM->getLuaState()->getStatePointer(); - int stackLevel = startStackLevel; - rawDebugInfo("Call stack : "); - rawDebugInfo("-------------"); - while (lua_getstack (ls, stackLevel, &dbg)) - { - if(lua_getinfo(ls, "lS", &dbg)) - { - std::string result = createGotoFileButtonTag(dbg.short_src, dbg.currentline) + NLMISC::toString("%s:%d:", dbg.short_src, dbg.currentline); - rawDebugInfo(result); - } - ++ stackLevel; - } + fails(ls, failReason.c_str()); } } // *************************************************************************** -void CLuaIHM::getCallStackAsString(int startStackLevel /*=0*/,std::string &result) +void CLuaIHM::checkArgType(CLuaState &ls, const char *funcName, uint index, int argType) { - //H_AUTO(Lua_CLuaIHM_getCallStackAsString) - result.clear(); - lua_Debug dbg; - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - lua_State *ls = pIM->getLuaState()->getStatePointer(); - int stackLevel = startStackLevel; - result += "Call stack : \n"; - result += "-------------"; - while (lua_getstack (ls, stackLevel, &dbg)) + //H_AUTO(Lua_CLuaIHM_checkArgType) + nlassert(index > 0); + if (ls.getTop() < (int) index) { - if(lua_getinfo(ls, "lS", &dbg)) - { - result += NLMISC::toString("%s:%d:", dbg.short_src, dbg.currentline); - } - ++ stackLevel; + fails(ls, "%s : argument %d of expected type %s was not defined", funcName, index, ls.getTypename(argType)); + } + if (ls.type(index) != argType) + { + fails(ls, "%s : argument %d of expected type %s has bad type : %s", funcName, index, ls.getTypename(argType), ls.getTypename(ls.type(index)), ls.type(index)); } } // *************************************************************************** -std::string CLuaIHM::createGotoFileButtonTag(const char *fileName, uint line) +void CLuaIHM::checkArgTypeRGBA(CLuaState &ls, const char *funcName, uint index) { - //H_AUTO(Lua_CLuaIHM_createGotoFileButtonTag) - if (ClientCfg.LuaDebugInfoGotoButtonEnabled) + //H_AUTO(Lua_CLuaIHM_checkArgTypeRGBA) + nlassert(index > 0); + if (ls.getTop() < (int) index) { - // TODO nico : put this in the interface - // add a command button to jump to the wanted file - return toString("/$$%s|%s|lua|%s('%s', %d)$$/", - ClientCfg.LuaDebugInfoGotoButtonTemplate.c_str(), - ClientCfg.LuaDebugInfoGotoButtonCaption.c_str(), - ClientCfg.LuaDebugInfoGotoButtonFunction.c_str(), - fileName, - line - ); + fails(ls, "%s : argument %d of expected type RGBA was not defined", funcName, index); + } + ls.pushValue(index); + CRGBA dummy; + if (!pop(ls, dummy)) + { + fails(ls, "%s : argument %d of expected type RGBA has bad type : %s", funcName, index, ls.getTypename(ls.type(index)), ls.type(index)); } - return ""; } // *************************************************************************** -std::string CLuaIHM::getDefine(const std::string &def) +void CLuaIHM::checkArgTypeUIElement(CLuaState &ls, const char *funcName, uint index) { - //H_AUTO(Lua_CLuaIHM_getDefine) - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - if(ClientCfg.DisplayLuaDebugInfo && !pIM->isDefineExist(def)) - debugInfo(toString("getDefine(): '%s' not found", def.c_str())); - return pIM->getDefine(def); -} - - - -// *************************************************************************** -static sint32 getTargetSlotNr() -{ - const char *dbPath = "UI:VARIABLES:TARGET:SLOT"; - CInterfaceManager *im = CInterfaceManager::getInstance(); - CCDBNodeLeaf *node = im->getDbProp(dbPath, false); - if (!node) return 0; - if ((uint8) node->getValue32() == (uint8) CLFECOMMON::INVALID_SLOT) - { - return 0; - } - return node->getValue32(); -} - -// *************************************************************************** -static CEntityCL *getTargetEntity() -{ - const char *dbPath = "UI:VARIABLES:TARGET:SLOT"; - CInterfaceManager *im = CInterfaceManager::getInstance(); - CCDBNodeLeaf *node = im->getDbProp(dbPath, false); - if (!node) return NULL; - if ((uint8) node->getValue32() == (uint8) CLFECOMMON::INVALID_SLOT) - { - return NULL; - } - return EntitiesMngr.entity((uint) node->getValue32()); -} - -// *************************************************************************** -static CEntityCL *getSlotEntity(uint slot) -{ - return EntitiesMngr.entity(slot); -} - -// *************************************************************************** -sint32 CLuaIHM::getPlayerLevel() -{ - if (!UserEntity) return -1; - CSkillManager *pSM= CSkillManager::getInstance(); - uint32 maxskill = pSM->getBestSkillValue(SKILLS::SC); - maxskill = std::max(maxskill, pSM->getBestSkillValue(SKILLS::SF)); - maxskill = std::max(maxskill, pSM->getBestSkillValue(SKILLS::SH)); - maxskill = std::max(maxskill, pSM->getBestSkillValue(SKILLS::SM)); - return sint32(maxskill); -} - -// *************************************************************************** -sint64 CLuaIHM::getPlayerVpa() -{ - sint64 prop = CInterfaceManager::getInstance()->getDbProp("SERVER:Entities:E0:P"+toString("%d", CLFECOMMON::PROPERTY_VPA))->getValue64(); - return prop; -} - -// *************************************************************************** -sint64 CLuaIHM::getPlayerVpb() -{ - sint64 prop = CInterfaceManager::getInstance()->getDbProp("SERVER:Entities:E0:P"+toString("%d", CLFECOMMON::PROPERTY_VPB))->getValue64(); - return prop; -} - -// *************************************************************************** -sint64 CLuaIHM::getPlayerVpc() -{ - sint64 prop = CInterfaceManager::getInstance()->getDbProp("SERVER:Entities:E0:P"+toString("%d", CLFECOMMON::PROPERTY_VPB))->getValue64(); - return prop; -} - -// *************************************************************************** -sint32 CLuaIHM::getTargetLevel() -{ - CEntityCL *target = getTargetEntity(); - if (!target) return -1; - if ( target->isPlayer() ) - { - CInterfaceManager *pIM = CInterfaceManager::getInstance(); - CCDBNodeLeaf *pDbPlayerLevel = pIM->getDbProp( pIM->getDefine("target_player_level") ); - return pDbPlayerLevel ? pDbPlayerLevel->getValue32() : -1; - } - else - { - CCharacterSheet *pCS = dynamic_cast(SheetMngr.get(target->sheetId())); - if(!pCS) return -1; - // only display the consider if the target is attackable #523 - if(!pCS->Attackable) return -1; - if(!target->properties().attackable()) return -1; - return sint32(pCS->Level); - } - return -1; -} - -// *************************************************************************** -ucstring CLuaIHM::getTargetSheet() -{ - CEntityCL *target = getTargetEntity(); - if (!target) return ucstring(); - - return target->sheetId().toString(); -} - -// *************************************************************************** -sint64 CLuaIHM::getTargetVpa() -{ - CEntityCL *target = getTargetEntity(); - if (!target) return 0; - - sint64 prop = CInterfaceManager::getInstance()->getDbProp("SERVER:Entities:E"+toString("%d", getTargetSlotNr())+":P"+toString("%d", CLFECOMMON::PROPERTY_VPA))->getValue64(); - - return prop; -} - -// *************************************************************************** -sint64 CLuaIHM::getTargetVpb() -{ - CEntityCL *target = getTargetEntity(); - if (!target) return 0; - - sint64 prop = CInterfaceManager::getInstance()->getDbProp("SERVER:Entities:E"+toString("%d", getTargetSlotNr())+":P"+toString("%d", CLFECOMMON::PROPERTY_VPB))->getValue64(); - - return prop; -} - -// *************************************************************************** -sint64 CLuaIHM::getTargetVpc() -{ - CEntityCL *target = getTargetEntity(); - if (!target) return 0; - - sint64 prop = CInterfaceManager::getInstance()->getDbProp("SERVER:Entities:E"+toString("%d", getTargetSlotNr())+":P"+toString("%d", CLFECOMMON::PROPERTY_VPB))->getValue64(); - - return prop; -} - -// *************************************************************************** -sint32 CLuaIHM::getTargetForceRegion() -{ - CEntityCL *target = getTargetEntity(); - if (!target) return -1; - if ( target->isPlayer() ) - { - CInterfaceManager *pIM = CInterfaceManager::getInstance(); - CCDBNodeLeaf *pDbPlayerLevel = pIM->getDbProp( pIM->getDefine("target_player_level") ); - if (!pDbPlayerLevel) return -1; - sint nLevel = pDbPlayerLevel->getValue32(); - if ( nLevel < 250 ) - { - return (sint32) ((nLevel < 20) ? 1 : (nLevel / 50) + 2); - } - else - { - return 8; - } - } - else - { - CCharacterSheet *pCS = dynamic_cast(SheetMngr.get(target->sheetId())); - return pCS ? (sint32) pCS->RegionForce : -1; - } - return 0; -} - -// *************************************************************************** -sint32 CLuaIHM::getTargetLevelForce() -{ - CEntityCL *target = getTargetEntity(); - if (!target) return -1; - if ( target->isPlayer() ) - { - CInterfaceManager *pIM = CInterfaceManager::getInstance(); - CCDBNodeLeaf *pDbPlayerLevel = pIM->getDbProp( pIM->getDefine("target_player_level") ); - if (!pDbPlayerLevel) return -1; - sint nLevel = pDbPlayerLevel->getValue32(); - if ( nLevel < 250 ) - { - return (sint32) (((nLevel % 50) * 5 / 50) + 1); - } - else - { - return 6; - } - } - else - { - CCharacterSheet *pCS = dynamic_cast(SheetMngr.get(target->sheetId())); - return pCS ? (sint32) pCS->ForceLevel : -1; - } - return 0; -} - -// *************************************************************************** -bool CLuaIHM::isTargetNPC() -{ - CEntityCL *target = getTargetEntity(); - if (!target) return false; - return target->isNPC(); -} - -// *************************************************************************** -bool CLuaIHM::isTargetPlayer() -{ - CEntityCL *target = getTargetEntity(); - if (!target) return false; - return target->isPlayer(); -} - - -// *************************************************************************** -bool CLuaIHM::isTargetUser() -{ - CEntityCL *target = getTargetEntity(); - if (!target) return false; - return target->isUser(); -} - -// *************************************************************************** -bool CLuaIHM::isPlayerInPVPMode() -{ - if (!UserEntity) return false; - return (UserEntity->getPvpMode() & PVP_MODE::PvpFaction || UserEntity->getPvpMode() & PVP_MODE::PvpFactionFlagged || UserEntity->getPvpMode() & PVP_MODE::PvpZoneFaction) != 0; -} - -// *************************************************************************** -bool CLuaIHM::isTargetInPVPMode() -{ - CEntityCL *target = getTargetEntity(); - if (!target) return false; - return (target->getPvpMode() & PVP_MODE::PvpFaction || target->getPvpMode() & PVP_MODE::PvpFactionFlagged || target->getPvpMode() & PVP_MODE::PvpZoneFaction) != 0; -} - -// *************************************************************************** -void CLuaIHM::pauseBGDownloader() -{ - ::pauseBGDownloader(); -} - -// *************************************************************************** -void CLuaIHM::unpauseBGDownloader() -{ - ::unpauseBGDownloader(); -} - -// *************************************************************************** -void CLuaIHM::requestBGDownloaderPriority(uint priority) -{ - if (priority >= BGDownloader::ThreadPriority_Count) - { - throw NLMISC::Exception("requestBGDownloaderPriority() : invalid priority"); - } - CBGDownloaderAccess::getInstance().requestDownloadThreadPriority((BGDownloader::TThreadPriority) priority, false); -} - -// *************************************************************************** -sint CLuaIHM::getBGDownloaderPriority() -{ - return CBGDownloaderAccess::getInstance().getDownloadThreadPriority(); -} - -// *************************************************************************** -ucstring CLuaIHM::getPatchLastErrorMessage() -{ - if (isBGDownloadEnabled()) - { - return CBGDownloaderAccess::getInstance().getLastErrorMessage(); - } - else - { - CPatchManager *pPM = CPatchManager::getInstance(); - return pPM->getLastErrorMessage(); - } -} - -// *************************************************************************** -bool CLuaIHM::isInGame() -{ - CInterfaceManager *pIM = CInterfaceManager::getInstance(); - return pIM->isInGame(); -} - -// *************************************************************************** -uint32 CLuaIHM::getPlayerSelectedSlot() -{ - return (uint32) PlayerSelectedSlot; -} - -// *************************************************************************** -bool CLuaIHM::isPlayerSlotNewbieLand(uint32 slot) -{ - if (slot > CharacterSummaries.size()) - { - throw ELuaIHMException("isPlayerSlotNewbieLand(): Invalid slot %d", (int) slot); - } - return CharacterSummaries[slot].InNewbieland; -} - - -// *************************************************************************** -void CLuaIHM::messageBox(const ucstring &text) -{ - //H_AUTO(Lua_CLuaIHM_messageBox) - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - pIM->messageBox(text); -} - -// *************************************************************************** -void CLuaIHM::messageBox(const ucstring &text, const std::string &masterGroup) -{ - //H_AUTO(Lua_CLuaIHM_messageBox) - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - pIM->messageBox(text, masterGroup); -} - -// *************************************************************************** -void CLuaIHM::messageBox(const ucstring &text, const std::string &masterGroup, int caseMode) -{ - if (caseMode < 0 || caseMode >= CaseCount) - { - throw ELuaIHMException("messageBox: case mode value is invalid."); - } - //H_AUTO(Lua_CLuaIHM_messageBox) - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - pIM->messageBox(text, masterGroup, (TCaseMode) caseMode); -} - -// *************************************************************************** -void CLuaIHM::messageBox(const std::string &text) -{ - //H_AUTO(Lua_CLuaIHM_messageBox) - static volatile bool dumpCallStack = false; - if (dumpCallStack) - { - CLuaIHM::dumpCallStack(0); - } - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - pIM->messageBox(text); -} - -// *************************************************************************** -void CLuaIHM::messageBoxWithHelp(const ucstring &text) -{ - //H_AUTO(Lua_CLuaIHM_messageBox) - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - pIM->messageBoxWithHelp(text); -} - -// *************************************************************************** -void CLuaIHM::messageBoxWithHelp(const ucstring &text, const std::string &masterGroup) -{ - //H_AUTO(Lua_CLuaIHM_messageBox) - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - pIM->messageBoxWithHelp(text, masterGroup); -} - -// *************************************************************************** -void CLuaIHM::messageBoxWithHelp(const ucstring &text, const std::string &masterGroup, int caseMode) -{ - if (caseMode < 0 || caseMode >= CaseCount) - { - throw ELuaIHMException("messageBoxWithHelp: case mode value is invalid."); - } - //H_AUTO(Lua_CLuaIHM_messageBox) - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - pIM->messageBoxWithHelp(text, masterGroup, "" ,"", (TCaseMode) caseMode); -} - -// *************************************************************************** -void CLuaIHM::messageBoxWithHelp(const std::string &text) -{ - //H_AUTO(Lua_CLuaIHM_messageBox) - static volatile bool dumpCallStack = false; - if (dumpCallStack) - { - CLuaIHM::dumpCallStack(0); - } - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - pIM->messageBoxWithHelp(text); -} - -// *************************************************************************** -void CLuaIHM::setContextHelpText(const ucstring &text) -{ - //H_AUTO(Lua_CLuaIHM_setContextHelpText) - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - pIM->setContextHelpText(text); -} - -// *************************************************************************** -std::string CLuaIHM::findReplaceAll(const std::string &str, const std::string &search, const std::string &replace) -{ - //H_AUTO(Lua_CLuaIHM_findReplaceAll) - std::string ret= str; - while(strFindReplace(ret, search, replace)); - return ret; -} - -// *************************************************************************** -ucstring CLuaIHM::findReplaceAll(const ucstring &str, const ucstring &search, const ucstring &replace) -{ - //H_AUTO(Lua_CLuaIHM_findReplaceAll) - ucstring ret= str; - while(strFindReplace(ret, search, replace)); - return ret; -} - -// *************************************************************************** -ucstring CLuaIHM::findReplaceAll(const ucstring &str, const std::string &search, const std::string &replace) -{ - //H_AUTO(Lua_CLuaIHM_findReplaceAll) - return findReplaceAll(str, ucstring(search), ucstring(replace)); -} - -// *************************************************************************** -ucstring CLuaIHM::findReplaceAll(const ucstring &str, const std::string &search, const ucstring &replace) -{ - //H_AUTO(Lua_CLuaIHM_findReplaceAll) - return findReplaceAll(str, ucstring(search), ucstring(replace)); -} - -// *************************************************************************** -ucstring CLuaIHM::findReplaceAll(const ucstring &str, const ucstring &search, const std::string &replace) -{ - //H_AUTO(Lua_CLuaIHM_findReplaceAll) - return findReplaceAll(str, ucstring(search), ucstring(replace)); -} - -// *************************************************************************** -int CLuaIHM::getUICaller(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getUICaller) - CLuaStackChecker lsc(&ls, 1); - - // params: none. - // return: CInterfaceElement* (nil if error) - CInterfaceElement *pIE= CHandlerLUA::getUICaller(); - if(!pIE) - { - ls.pushNil(); - debugInfo(toString("getUICaller(): No UICaller found. return Nil")); - } - else - { - pushUIOnStack(ls, pIE); - } - return 1; -} - -// *************************************************************************** -int CLuaIHM::getCurrentWindowUnder(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getCurrentWindowUnder) - CLuaStackChecker lsc(&ls, 1); - CInterfaceManager *im = CInterfaceManager::getInstance(); - CInterfaceElement *pIE= im->getCurrentWindowUnder(); - if(!pIE) - { - ls.pushNil(); - debugInfo(toString("getCurrentWindowUnder(): No UICaller found. return Nil")); - } - else - { - pushUIOnStack(ls, pIE); - } - return 1; -} -int CLuaIHM::getUI(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getUI) - // params: "ui:interface:...". - // return: CInterfaceElement* (nil if error) - const char *funcName = "getUI"; - check(ls, ls.getTop() == 1 || ls.getTop() == 2, funcName); - checkArgType(ls, funcName, 1, LUA_TSTRING); - bool verbose = true; - if (ls.getTop() > 1) - { - checkArgType(ls, funcName, 2, LUA_TBOOLEAN); - verbose = ls.toBoolean(2); - } - - // get the string - std::string eltStr; - ls.toString(1, eltStr); - - // return the element - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - CInterfaceElement *pIE= pIM->getElementFromId(eltStr); - if(!pIE) - { - ls.pushNil(); - if (verbose) - { - std::string stackContext; - ls.getStackContext(stackContext, 1); - debugInfo(toString("%s : getUI(): '%s' not found", stackContext.c_str(), eltStr.c_str())); - } - } - else - { - pushUIOnStack(ls, pIE); - } - return 1; -} - -// *************************************************************************** -int CLuaIHM::getUIId(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getUIId) - CLuaStackChecker lsc(&ls, 1); - - // params: CInterfaceElement* - // return: "ui:interface:...". (empty if error) - checkArgCount(ls, "getUIId", 1); - check(ls, isUIOnStack(ls, 1), "getUIId() requires a UI object in param 1"); - - // retrieve args - CInterfaceElement *pIE= getUIOnStack(ls, 1); - - // convert to id - if(pIE) - ls.push(pIE->getId()); - else - ls.push(""); - - return 1; -} - -// *************************************************************************** -int CLuaIHM::getIndexInDB(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getIndexInDB) - CLuaStackChecker lsc(&ls, 1); - - // params: CDBCtrlSheet* - // return: index in DB of a dbctrlsheet (empty if error) - checkArgCount(ls, "getIndexInDB", 1); - check(ls, isUIOnStack(ls, 1), "getIndexInDB() requires a UI object in param 1"); - - // retrieve args - CInterfaceElement *pIE= getUIOnStack(ls, 1); - CDBCtrlSheet *pCS= dynamic_cast(pIE); - - // get the index in db - if(pCS) - ls.push((double)pCS->getIndexInDB()); - else - ls.push(0.0); - - return 1; -} - -// *************************************************************************** -int CLuaIHM::createGroupInstance(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_createGroupInstance) - const char *funcName = "createGroupInstance"; - CLuaIHM::checkArgCount(ls, funcName, 3); - CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); - CLuaIHM::checkArgType(ls, funcName, 2, LUA_TSTRING); - CLuaIHM::checkArgType(ls, funcName, 3, LUA_TTABLE); - std::vector > templateParams; - CLuaObject params; - params.pop(ls); - ENUM_LUA_TABLE(params, it) - { - if (!it.nextKey().isString()) - { - nlwarning("%s : bad key encountered with type %s, string expected.", funcName, it.nextKey().getTypename()); - continue; - } - if (!it.nextValue().isString()) - { - nlwarning("%s : bad value encountered with type %s for key %s, string expected.", funcName, it.nextValue().getTypename(), it.nextKey().toString().c_str()); - continue; - } - templateParams.push_back(std::pair(it.nextKey().toString(), it.nextValue().toString())); // strange compilation bug here when I use std::make_pair ... :( - } - CInterfaceManager *im = CInterfaceManager::getInstance(); - CInterfaceGroup *result = im->createGroupInstance(ls.toString(1), ls.toString(2), templateParams); - if (!result) - { - ls.pushNil(); - } - else - { - CLuaIHM::pushUIOnStack(ls, result); - } - return 1; -} - -// *************************************************************************** -int CLuaIHM::createRootGroupInstance(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_createGroupInstance) - const char *funcName = "createRootGroupInstance"; - CLuaIHM::checkArgCount(ls, funcName, 3); - CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); - CLuaIHM::checkArgType(ls, funcName, 2, LUA_TSTRING); - CLuaIHM::checkArgType(ls, funcName, 3, LUA_TTABLE); - std::vector > templateParams; - CLuaObject params; - params.pop(ls); - ENUM_LUA_TABLE(params, it) - { - if (!it.nextKey().isString()) - { - nlwarning("%s : bad key encountered with type %s, string expected.", funcName, it.nextKey().getTypename()); - continue; - } - if (!it.nextValue().isString()) - { - nlwarning("%s : bad value encountered with type %s for key %s, string expected.", funcName, it.nextValue().getTypename(), it.nextKey().toString().c_str()); - continue; - } - templateParams.push_back(std::pair(it.nextKey().toString(), it.nextValue().toString())); // strange compilation bug here when I use std::make_pair ... :( - } - CInterfaceManager *im = CInterfaceManager::getInstance(); - CInterfaceGroup *result = im->createGroupInstance(ls.toString(1), "ui:interface:"+string(ls.toString(2)), templateParams); - if (!result) - { - ls.pushNil(); - } - else - { - result->setId("ui:interface:"+string(ls.toString(2))); - result->updateCoords(); - im->addWindowToMasterGroup("ui:interface", result); - CInterfaceGroup *pRoot = dynamic_cast(im->getElementFromId("ui:interface")); - result->setParent(pRoot); - if (pRoot) - pRoot->addGroup(result); - result->setActive(true); - CLuaIHM::pushUIOnStack(ls, result); - } - return 1; -} - -// *************************************************************************** -int CLuaIHM::createUIElement(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_createUIElement) - const char *funcName = "addUIElement"; - CLuaIHM::checkArgCount(ls, funcName, 3); - CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); - CLuaIHM::checkArgType(ls, funcName, 2, LUA_TSTRING); - CLuaIHM::checkArgType(ls, funcName, 3, LUA_TTABLE); - std::vector > templateParams; - CLuaObject params; - params.pop(ls); - ENUM_LUA_TABLE(params, it) - { - if (!it.nextKey().isString()) - { - nlwarning("%s : bad key encountered with type %s, string expected.", funcName, it.nextKey().getTypename()); - continue; - } - if (!it.nextValue().isString()) - { - nlwarning("%s : bad value encountered with type %s for key %s, string expected.", funcName, it.nextValue().getTypename(), it.nextKey().toString().c_str()); - continue; - } - templateParams.push_back(std::pair(it.nextKey().toString(), it.nextValue().toString())); // strange compilation bug here when I use std::make_pair ... :( - } - CInterfaceManager *im = CInterfaceManager::getInstance(); - CInterfaceElement *result = im->createUIElement(ls.toString(1), ls.toString(2), templateParams); - if (!result) - { - ls.pushNil(); - } - else - { - CLuaIHM::pushUIOnStack(ls, result); - } - return 1; -} - - -// *************************************************************************** -int CLuaIHM::displayBubble(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_createUIElement) - const char *funcName = "displayBubble"; - CLuaIHM::checkArgCount(ls, funcName, 3); - CLuaIHM::checkArgType(ls, funcName, 1, LUA_TNUMBER); - CLuaIHM::checkArgType(ls, funcName, 2, LUA_TSTRING); - CLuaIHM::checkArgType(ls, funcName, 3, LUA_TTABLE); - std::vector strs; - std::vector links; - CLuaObject params; - params.pop(ls); - ENUM_LUA_TABLE(params, it) - { - if (!it.nextKey().isString()) - { - nlwarning("%s : bad key encountered with type %s, string expected.", funcName, it.nextKey().getTypename()); - continue; - } - if (!it.nextValue().isString()) - { - nlwarning("%s : bad value encountered with type %s for key %s, string expected.", funcName, it.nextValue().getTypename(), it.nextKey().toString().c_str()); - continue; - } - links.push_back(it.nextValue().toString()); - strs.push_back(it.nextKey().toString()); - } - - InSceneBubbleManager.webIgChatOpen((uint32)ls.toNumber(1), ls.toString(2), strs, links); - - return 1; -} - - -// *************************************************************************** -int CLuaIHM::getCompleteIslands(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getCompleteIslands) - const char *funcName = "getCompleteIslands"; - CLuaIHM::checkArgCount(ls, funcName, 0); - - ls.newTable(); - CLuaObject result(ls); - - // load entryPoints - CScenarioEntryPoints scenarioEntryPoints = CScenarioEntryPoints::getInstance(); - const CScenarioEntryPoints::TCompleteIslands& islands = scenarioEntryPoints.getCompleteIslands(); - - CScenarioEntryPoints::TCompleteIslands::const_iterator island(islands.begin()), lastIsland(islands.end()); - for( ; island != lastIsland ; ++island) - { - ls.newTable(); - CLuaObject islandTable(ls); - islandTable.setValue("continent", island->Continent); - islandTable.setValue("xmin", (double)island->XMin); - islandTable.setValue("ymin", (double)island->YMin); - islandTable.setValue("xmax", (double)island->XMax); - islandTable.setValue("ymax", (double)island->YMax); - - ls.newTable(); - CLuaObject entrypointsTable(ls); - - for(uint e=0; eEntryPoints.size(); e++) - { - const CScenarioEntryPoints::CShortEntryPoint & entryPoint = island->EntryPoints[e]; - ls.newTable(); - CLuaObject entrypointTable(ls); - entrypointTable.setValue("x", (double)entryPoint.X); - entrypointTable.setValue("y", (double)entryPoint.Y); - - entrypointsTable.setValue(entryPoint.Location, entrypointTable); - } - islandTable.setValue("entrypoints", entrypointsTable); - - result.setValue(island->Island, islandTable); - } - - result.push(); - - return 1; -} - -// *************************************************************************** -int CLuaIHM::getIslandId(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getIslandId) - const char *funcName = "getIslandId"; - CLuaIHM::checkArgCount(ls, funcName, 1); - check(ls, ls.isString(1), "getIslandId() requires a string in param 1"); - - - CScenarioEntryPoints scenarioEntryPoints = CScenarioEntryPoints::getInstance(); - uint32 id = scenarioEntryPoints.getIslandId(ls.toString(1)); - ls.push((double)id); - - return 1; -} - -// *************************************************************************** -int CLuaIHM::launchContextMenuInGame(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_launchContextMenuInGame) - CLuaStackChecker lsc(&ls); - checkArgCount(ls, "launchContextMenuInGame", 1); - check(ls, ls.isString(1), "launchContextMenuInGame() requires a string in param 1"); - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - pIM->launchContextMenuInGame(ls.toString(1)); - return 0; -} - -// *************************************************************************** -int CLuaIHM::parseInterfaceFromString(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_parseInterfaceFromString) - CLuaStackChecker lsc(&ls, 1); - checkArgCount(ls, "parseInterfaceFromString", 1); - check(ls, ls.isString(1), "parseInterfaceFromString() requires a string in param 1"); - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - std::vector script(1); - script[0] = ls.toString(1); - ls.push(pIM->parseInterface(script, true, false)); - return 1; -} - -// *************************************************************************** -int CLuaIHM::updateAllLocalisedElements(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_updateAllLocalisedElements) - TTime startTime = CTime::getLocalTime(); - // - CLuaStackChecker lsc(&ls); - checkArgCount(ls, "updateAllLocalisedElements", 0); - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - pIM->updateAllLocalisedElements(); - // - TTime endTime = CTime::getLocalTime(); - if (ClientCfg.R2EDVerboseParseTime) - { - nlinfo("%.2f seconds for 'updateAllLocalisedElements'", (endTime - startTime) / 1000.f); - } - return 0; -} - -// *************************************************************************** -int CLuaIHM::setCaptureKeyboard(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_setCaptureKeyboard) - const char *funcName = "setCaptureKeyboard"; - checkArgCount(ls, funcName, 1); - checkArgTypeUIElement(ls, funcName, 1); - CCtrlBase *ctrl = dynamic_cast(getUIOnStack(ls, 1)); - if (!ctrl) - { - fails(ls, "%s waits a ui control as arg 1", funcName); - } - CInterfaceManager *im = CInterfaceManager::getInstance(); - im->setCaptureKeyboard(ctrl); - return 0; -} - -// *************************************************************************** -int CLuaIHM::resetCaptureKeyboard(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_resetCaptureKeyboard) - const char *funcName = "resetCaptureKeyboard"; - checkArgCount(ls, funcName, 0); - CInterfaceManager *im = CInterfaceManager::getInstance(); - im->resetCaptureKeyboard(); - return 0; -} - -// *************************************************************************** -int CLuaIHM::setOnDraw(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_setOnDraw) - CLuaStackChecker lsc(&ls, 0); - - // params: CInterfaceGroup*, "script". - // return: none - checkArgCount(ls, "setOnDraw", 2); - check(ls, isUIOnStack(ls, 1), "setOnDraw() requires a UI object in param 1"); - check(ls, ls.isString(2), "setOnDraw() requires a string in param 2"); - - // retrieve args - CInterfaceElement *pIE= getUIOnStack(ls, 1); - std::string script; - ls.toString(2, script); - - // must be a group - CInterfaceGroup *group= dynamic_cast(pIE); - if(!group) - throw ELuaIHMException("setOnDraw(): '%s' is not a group", pIE->getId().c_str()); - // Set the script to be executed at each draw - group->setLuaScriptOnDraw(script); - - return 0; -} - -// *************************************************************************** -int CLuaIHM::addOnDbChange(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_addOnDbChange) - CLuaStackChecker lsc(&ls, 0); - - // params: CInterfaceGroup*, "dblist", "script". - // return: none - checkArgCount(ls, "addOnDbChange", 3); - check(ls, isUIOnStack(ls, 1), "addOnDbChange() requires a UI object in param 1"); - check(ls, ls.isString(2), "addOnDbChange() requires a string in param 2"); - check(ls, ls.isString(3), "addOnDbChange() requires a string in param 3"); - - // retrieve args - CInterfaceElement *pIE= getUIOnStack(ls, 1); - std::string dbList, script; - ls.toString(2, dbList); - ls.toString(3, script); - - // must be a group - CInterfaceGroup *group= dynamic_cast(pIE); - if(!group) - throw ELuaIHMException("addOnDbChange(): '%s' is not a group", pIE->getId().c_str()); - // Set the script to be executed when the given DB change - group->addLuaScriptOnDBChange(dbList, script); - - return 0; -} - - -// *************************************************************************** -int CLuaIHM::removeOnDbChange(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_removeOnDbChange) - CLuaStackChecker lsc(&ls, 0); - - // params: CInterfaceGroup*, "dbList" - // return: none - checkArgCount(ls, "removeOnDbChange", 2); - check(ls, isUIOnStack(ls, 1), "removeOnDbChange() requires a UI object in param 1"); - check(ls, ls.isString(2), "removeOnDbChange() requires a string in param 2"); - - // retrieve args - CInterfaceElement *pIE= getUIOnStack(ls, 1); - std::string dbList; - ls.toString(2, dbList); - - // must be a group - CInterfaceGroup *group= dynamic_cast(pIE); - if(!group) - throw ELuaIHMException("removeOnDbChange(): '%s' is not a group", pIE->getId().c_str()); - // Remove the script to be executed when the given DB change - group->removeLuaScriptOnDBChange(dbList); - - return 0; -} - - -// *************************************************************************** -int CLuaIHM::runAH(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_runAH) - CLuaStackChecker lsc(&ls, 0); - - // params: CInterfaceElement *, "ah", "params". - // return: none - checkArgCount(ls, "runAH", 3); - check(ls, isUIOnStack(ls, 1) || ls.isNil(1), "runAH() requires a UI object in param 1 (or Nil)"); - check(ls, ls.isString(2), "runAH() requires a string in param 2"); - check(ls, ls.isString(3), "runAH() requires a string in param 3"); - - // retrieve args - CInterfaceElement *pIE= getUIOnStack(ls, 1); - std::string ah, params; - ls.toString(2, ah); - ls.toString(3, params); - - // run AH - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - // The element must be ctrl (or NULL) - CCtrlBase *ctrl= NULL; - if(pIE) - { - ctrl= dynamic_cast(pIE); - if(!ctrl) - throw ELuaIHMException("runAH(): '%s' is not a ctrl", pIE->getId().c_str()); - } - pIM->runActionHandler(ah, ctrl, params); - - return 0; -} - -// *************************************************************************** -int CLuaIHM::runExprAndPushResult(CLuaState &ls, const std::string &expr) -{ - //H_AUTO(Lua_CLuaIHM_runExprAndPushResult) - // Execute expression - CInterfaceExprValue value; - if (CInterfaceExpr::eval(expr, value, NULL)) - { - switch(value.getType()) - { - case CInterfaceExprValue::Boolean: - ls.push(value.getBool()); - break; - case CInterfaceExprValue::Integer: - ls.push((double)value.getInteger()); - break; - case CInterfaceExprValue::Double: - ls.push(value.getDouble()); - break; - case CInterfaceExprValue::String: - { - ucstring ucstr= value.getUCString(); - // Yoyo: dynamically decide whether must return a string or a ucstring - bool mustUseUCString= false; - for (uint i = 0; i < ucstr.size (); i++) - { - if (ucstr[i] > 255) - { - mustUseUCString= true; - break; - } - } - // push a ucstring? - if(mustUseUCString) - { -#if LUABIND_VERSION > 600 - luabind::detail::push(ls.getStatePointer(), ucstr); -#else - luabind::object obj(ls.getStatePointer(), ucstr); - obj.pushvalue(); -#endif - } - else - { - ls.push(ucstr.toString()); - } - break; - } - case CInterfaceExprValue::RGBA: - { - CRGBA color = value.getRGBA(); -#if LUABIND_VERSION > 600 - luabind::detail::push(ls.getStatePointer(), color); -#else - luabind::object obj(ls.getStatePointer(), color); - obj.pushvalue(); -#endif - break; - } - break; - case CInterfaceExprValue::UserType: // Yoyo: don't care UserType... - default: - ls.pushNil(); - break; - } - } - else - ls.pushNil(); - - return 1; -} - -// *************************************************************************** -int CLuaIHM::runExpr(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_runExpr) - CLuaStackChecker lsc(&ls, 1); - - // params: "expr". - // return: any of: nil, bool, string, number, RGBA, UCString - checkArgCount(ls, "runExpr", 1); - check(ls, ls.isString(1), "runExpr() requires a string in param 1"); - - // retrieve args - std::string expr; - ls.toString(1, expr); - - // run expression and push result - return runExprAndPushResult(ls, expr); -} - -// *************************************************************************** -int CLuaIHM::runFct(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_runFct) - CLuaStackChecker lsc(&ls, 1); - - // params: "expr", param1, param2... - // return: any of: nil, bool, string, number, RGBA, UCString - checkArgMin(ls, "runFct", 1); - check(ls, ls.isString(1), "runExpr() requires a string in param 1"); - - // retrieve fct - std::string expr; - ls.toString(1, expr); - expr+= "("; - - // retrieve params - uint top= ls.getTop(); - for(uint i=2;i<=top;i++) - { - if(i>2) - expr+= ", "; - - // If it is a number - if(ls.type(i)==LUA_TNUMBER) - { - std::string paramValue; - ls.toString(i, paramValue); // nb: transformed to a string in the stack - expr+= paramValue; - } - // else suppose a string - else - { - // must enclose with "'" - std::string paramValue; - ls.toString(i, paramValue); - expr+= std::string("'") + paramValue + std::string("'") ; - } - } - - // end fct call - expr+= ")"; - - - // run expression and push result - return runExprAndPushResult(ls, expr); -} - -// *************************************************************************** -int CLuaIHM::runCommand(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_runCommand) - CLuaStackChecker lsc(&ls, 1); - if (ls.empty()) - { - nlwarning("'runCommand' : Command name expected"); - ls.push(false); - return 1; - } - const char *commandName = ls.toString(1); - if (!commandName) - { - nlwarning("'runCommand' : Bad command name"); - ls.push(false); - return 1; - } - if (!NLMISC::ICommand::LocalCommands || !NLMISC::ICommand::LocalCommands->count(ls.toString(1))) - { - nlwarning("'runCommand' : Command %s not found", ls.toString(1)); - ls.push(false); - return 1; - } - std::string rawCommandString = ls.toString(1); - NLMISC::ICommand *command = (*NLMISC::ICommand::LocalCommands)[ls.toString(1)]; - nlassert(command); - std::vector args(ls.getTop() - 1); - for(uint k = 2; k <= (uint) ls.getTop(); ++k) - { - if (ls.toString(k)) - { - args[k - 2] = ls.toString(k); - rawCommandString += " " + std::string(ls.toString(k)); - } - } - - ls.push(command->execute(rawCommandString, args, g_log, false, true)); - return 1; -} - -// *************************************************************************** -int CLuaIHM::formatUI(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_formatUI) - CLuaStackChecker lsc(&ls, 1); - - // params: "expr", param1, param2.... - // return: string with # and % parsed - checkArgMin(ls, "formatUI", 1); - check(ls, ls.isString(1), "formatUI() require a string in param1"); - - // get the string to format - std::string propVal; - ls.toString(1, propVal); - - // *** format with % - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - std::string newPropVal, defError; - if(!pIM->solveDefine(propVal, newPropVal, defError)) - { - throw ELuaIHMException("formatUI(): Can't find define: '%s'", defError.c_str()); - } - - // *** format with any additional parameter and #1, #2, #3 etc... - // search backward, starting from bigger param to replace (thus avoid to replace #1 before #13 for instance...) - sint stackIndex= ls.getTop(); - while(stackIndex>1) - { - std::string paramValue; - ls.toString(stackIndex, paramValue); - - // For stack param 4, the param index is 3 (because stack param 2 is the param No 1) - sint paramIndex= stackIndex-1; - while(NLMISC::strFindReplace(newPropVal, toString("#%d", paramIndex), paramValue)); - - // next - stackIndex--; - } - - // return result - ls.push(newPropVal); - return 1; -} - -// *************************************************************************** -int CLuaIHM::formatDB(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_formatDB) - CLuaStackChecker lsc(&ls, 1); - - // params: param1, param2.... - // return: string with @ and , added - checkArgMin(ls, "formatDB", 1); - uint top= ls.getTop(); - - std::string dbRes; - for(uint i=1;i<=top;i++) - { - if(i==1) - dbRes= "@"; - else - dbRes+= ", @"; - - std::string paramValue; - ls.toString(i, paramValue); - dbRes+= paramValue; - } - - // return result - ls.push(dbRes); - return 1; -} - -// *************************************************************************** -int CLuaIHM::deleteUI(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_deleteUI) - CLuaStackChecker lsc(&ls, 0); - - // params: CInterfaceElement * - // return: none - checkArgCount(ls, "deleteUI", 1); - check(ls, isUIOnStack(ls, 1), "deleteUI() requires a UI object in param 1"); - - // retrieve args - CInterfaceElement *pIE= getUIOnStack(ls, 1); - if(!pIE) - return 0; - - // has a parent? - CInterfaceGroup *parent= pIE->getParent(); - if(parent) - { - // correctly remove from parent - parent->delElement(pIE); - } - else - { - // just delete - delete pIE; - } - - return 0; -} - -// *************************************************************************** -int CLuaIHM::deleteReflectable(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_deleteReflectable) - CLuaStackChecker lsc(&ls, 0); - - // params: CInterfaceElement * - // return: none - checkArgCount(ls, "deleteReflectable", 1); - check(ls, isReflectableOnStack(ls, 1), "deleteReflectable() requires a reflectable C++ object in param 1"); - - // retrieve args - CReflectableRefPtrTarget *pRPT= getReflectableOnStack(ls, 1); - if(!pRPT) - return 0; - - - CInterfaceElement *pIE = dynamic_cast(pRPT); - - if (pIE) - { - // has a parent? - CInterfaceGroup *parent= pIE->getParent(); - if(parent) - { - // correctly remove from parent - parent->delElement(pIE); - } - } - - // just delete - delete pIE; - - return 0; -} - -// *************************************************************************** -int CLuaIHM::dumpUI(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_dumpUI) - CLuaStackChecker lsc(&ls, 0); - - // params: CInterfaceElement * - // return: none - checkArgCount(ls, "dumpUI", 1); - check(ls, isUIOnStack(ls, 1), "dumpUI() requires a UI object in param 1"); - - // retrieve args - CInterfaceElement *pIE= getUIOnStack(ls, 1); - if(!pIE) - debugInfo("UI: NULL"); - else - { - // Display also Information on RefPtr (warning: don't modify pinfo!!!) - nlassert(pIE->pinfo); - debugInfo(NLMISC::toString("UI: %x. %s. RefPtrCount: %d", pIE, pIE->getId().c_str(), - pIE->pinfo->IsNullPtrInfo?0:pIE->pinfo->RefCount)); - } - - return 0; -} - -// *************************************************************************** -int CLuaIHM::setKeyboardContext(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_setKeyboardContext) - const char *funcName = "setKeyboardContext"; - checkArgMin(ls, funcName, 1); - checkArgType(ls, funcName, 1, LUA_TSTRING); - - ActionsContext.setContext(ls.toString(1)); - - return 0; -} - -// *************************************************************************** -void CLuaIHM::fails(CLuaState &ls, const char *format, ...) -{ - //H_AUTO(Lua_CLuaIHM_fails) - std::string reason; - NLMISC_CONVERT_VARGS (reason, format, NLMISC::MaxCStringSize); - std::string stack; - ls.getStackAsString(stack); - // use a std::exception, to avoid Nel Exception warning - throw ELuaIHMException("%s. Lua stack = \n %s", reason.c_str(), stack.c_str()); -} - -// *************************************************************************** -void CLuaIHM::checkArgCount(CLuaState &ls, const char* funcName, uint nArgs) -{ - //H_AUTO(Lua_CLuaIHM_checkArgCount) - if(ls.getTop()!=(sint)nArgs) - { - fails(ls, "%s() need exactly %d arguments (tips : check between method & function call)", funcName, nArgs); - } -} - -// *************************************************************************** -void CLuaIHM::checkArgMin(CLuaState &ls, const char* funcName, uint nArgs) -{ - //H_AUTO(Lua_CLuaIHM_checkArgMin) - if(ls.getTop()<(sint)nArgs) - { - fails(ls, "%s() need at least %d arguments (tips : check between method & function call)", funcName, nArgs); - } -} - -// *************************************************************************** -void CLuaIHM::checkArgMax(CLuaState &ls,const char* funcName,uint nArgs) -{ - //H_AUTO(Lua_CLuaIHM_checkArgMax) - if(ls.getTop()>(sint)nArgs) - { - fails(ls, "%s() need at most %d arguments.", funcName, nArgs); - } -} - -// *************************************************************************** -void CLuaIHM::check(CLuaState &ls, bool ok, const std::string &failReason) -{ - //H_AUTO(Lua_CLuaIHM_check) - if(!ok) - { - fails(ls, failReason.c_str()); - } -} - -// *************************************************************************** -void CLuaIHM::checkArgType(CLuaState &ls, const char *funcName, uint index, int argType) -{ - //H_AUTO(Lua_CLuaIHM_checkArgType) - nlassert(index > 0); - if (ls.getTop() < (int) index) - { - fails(ls, "%s : argument %d of expected type %s was not defined", funcName, index, ls.getTypename(argType)); - } - if (ls.type(index) != argType) - { - fails(ls, "%s : argument %d of expected type %s has bad type : %s", funcName, index, ls.getTypename(argType), ls.getTypename(ls.type(index)), ls.type(index)); - } -} - -// *************************************************************************** -void CLuaIHM::checkArgTypeRGBA(CLuaState &ls, const char *funcName, uint index) -{ - //H_AUTO(Lua_CLuaIHM_checkArgTypeRGBA) - nlassert(index > 0); - if (ls.getTop() < (int) index) - { - fails(ls, "%s : argument %d of expected type RGBA was not defined", funcName, index); - } - ls.pushValue(index); - CRGBA dummy; - if (!pop(ls, dummy)) - { - fails(ls, "%s : argument %d of expected type RGBA has bad type : %s", funcName, index, ls.getTypename(ls.type(index)), ls.type(index)); - } -} - -// *************************************************************************** -void CLuaIHM::checkArgTypeUIElement(CLuaState &ls, const char *funcName, uint index) -{ - //H_AUTO(Lua_CLuaIHM_checkArgTypeUIElement) - nlassert(index > 0); - if (ls.getTop() < (int) index) - { - fails(ls, "%s : argument %d of expected type ui element was not defined", funcName, index); - } - if (!isUIOnStack(ls, index)) - { - fails(ls, "%s : argument %d of expected type ui element has bad type : %s", funcName, index, ls.getTypename(ls.type(index)), ls.type(index)); - } -} - -// *************************************************************************** -void CLuaIHM::checkArgTypeUCString(CLuaState &ls, const char *funcName, uint index) -{ - //H_AUTO(Lua_CLuaIHM_checkArgTypeUCString) - nlassert(index > 0); - if (ls.getTop() < (int) index) - { - fails(ls, "%s : argument %d of expected type ucstring was not defined", funcName, index); - } - ls.pushValue(index); - ucstring dummy; - if (!pop(ls, dummy)) - { - fails(ls, "%s : argument %d of expected type ucstring has bad type : %s", funcName, index, ls.getTypename(ls.type(index)), ls.type(index)); - } -} - - -// *************************************************************************** -int CLuaIHM::validMessageBox(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_validMessageBox) - const char *funcName = "validMessageBox"; - checkArgCount(ls, funcName, 6); - ucstring msg; - ls.pushValue(1); // copy ucstring at the end of stack to pop it - check(ls, pop(ls, msg), "validMessageBox : ucstring wanted as first parameter"); - checkArgType(ls, funcName, 2, LUA_TSTRING); - checkArgType(ls, funcName, 3, LUA_TSTRING); - checkArgType(ls, funcName, 4, LUA_TSTRING); - checkArgType(ls, funcName, 5, LUA_TSTRING); - checkArgType(ls, funcName, 6, LUA_TSTRING); - CInterfaceManager *im = CInterfaceManager::getInstance(); - im->validMessageBox(CInterfaceManager::QuestionIconMsg, msg, ls.toString(2), ls.toString(3), ls.toString(4), ls.toString(5), ls.toString(6)); - return 0; -} - -// *************************************************************************** -int CLuaIHM::setTopWindow(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_setTopWindow) - const char *funcName = "setTopWindow"; - checkArgCount(ls, funcName, 1); - CInterfaceGroup *wnd = dynamic_cast(getUIOnStack(ls, 1)); - if (!wnd) - { - fails(ls, "%s : interface group expected as arg 1", funcName); - } - CInterfaceManager *im = CInterfaceManager::getInstance(); - im->setTopWindow(wnd); - return 0; -} - -// *************************************************************************** -int CLuaIHM::concatUCString(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_concatUCString) - const char *funcName = "concatUCString"; - ucstring result; - for (uint k = 1; k <= (uint) ls.getTop(); ++k) - { - //nlwarning("arg %d = %s", k, ls.getTypename(ls.type(k))); - ucstring part; - if (ls.isString(k)) - { - part.fromUtf8(ls.toString(k)); - } - else - { - CLuaIHM::checkArgTypeUCString(ls, funcName, k); - nlverify(getUCStringOnStack(ls, k, part)); - } - result += part; - } - push(ls, result); - return 1; -} - -// *************************************************************************** -int CLuaIHM::concatString(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_concatUCString) - const char *funcName = "concatString"; - std::string result; - uint stackSize = ls.getTop(); - for (uint k = 1; k <= stackSize; ++k) - { - CLuaIHM::checkArgType(ls, funcName, k, LUA_TSTRING); - result += ls.toString(k); - } - ls.push(result); - return 1; -} - -// *************************************************************************** -int CLuaIHM::tableToString(CLuaState &ls) -{ - const char *funcName = "tableToString"; - CLuaIHM::checkArgCount(ls, funcName, 1); - CLuaIHM::checkArgType(ls, funcName, 1, LUA_TTABLE); - uint length = 0; - // compute size - ls.pushNil(); - while (ls.next(-2)) - { - ls.toString(-1); - length += (uint)ls.strlen(-1); - ls.pop(2); - } - std::string result; - result.resize(length); - char *dest = &result[0]; - // concatenate - ls.pushNil(); - while (ls.next(-2)) - { - uint length = (uint)ls.strlen(-1); - if (length) - { - memcpy(dest, ls.toString(-1), length); - } - dest += length; - ls.pop(2); - } - ls.push(result); - return 1; -} - -// *************************************************************************** -sint32 CLuaIHM::getSkillIdFromName(const std::string &def) -{ - //H_AUTO(Lua_CLuaIHM_getSkillIdFromName) - SKILLS::ESkills e= SKILLS::toSkill(def); - // Avoid any bug, return SF if not found - if(e>=SKILLS::unknown) - e= SKILLS::SF; - return e; -} - -// *************************************************************************** -ucstring CLuaIHM::getSkillLocalizedName(sint32 skillId) -{ - //H_AUTO(Lua_CLuaIHM_getSkillLocalizedName) - return ucstring(STRING_MANAGER::CStringManagerClient::getSkillLocalizedName((SKILLS::ESkills)skillId)); -} - -// *************************************************************************** -sint32 CLuaIHM::getMaxSkillValue(sint32 skillId) -{ - //H_AUTO(Lua_CLuaIHM_getMaxSkillValue) - CSkillManager *pSM= CSkillManager::getInstance(); - return pSM->getMaxSkillValue((SKILLS::ESkills)skillId); -} - -// *************************************************************************** -sint32 CLuaIHM::getBaseSkillValueMaxChildren(sint32 skillId) -{ - //H_AUTO(Lua_CLuaIHM_getBaseSkillValueMaxChildren) - CSkillManager *pSM= CSkillManager::getInstance(); - return pSM->getBaseSkillValueMaxChildren((SKILLS::ESkills)skillId); -} - -// *************************************************************************** -bool CLuaIHM::executeFunctionOnStack(CLuaState &ls, int numArgs, int numRet) -{ - //H_AUTO(Lua_CLuaIHM_executeFunctionOnStack) - static volatile bool dumpFunction = false; - if (dumpFunction) - { - CLuaStackRestorer lsr(&ls, ls.getTop()); - lua_Debug ar; - ls.pushValue(-1 - numArgs); - lua_getinfo (ls.getStatePointer(), ">lS", &ar); - nlwarning((std::string(ar.what) + ", at line " + toString(ar.linedefined) + " in " + std::string(ar.source)).c_str()); - } - int result = ls.pcall(numArgs, numRet); - switch (result) - { - case LUA_ERRRUN: - case LUA_ERRMEM: - case LUA_ERRERR: - { - CLuaIHM::debugInfo(ls.toString(-1)); - ls.pop(); - return false; - } - break; - case 0: - return true; - break; - default: - nlassert(0); - break; - } - return false; -} - - -// *************************************************************************** -int CLuaIHM::breakPoint(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_breakPoint) - std::string reason; - ls.getStackContext(reason, 1); // 1 because 0 is the current C function => return 1 for script called - LuaHelperStuff::formatLuaStackContext(reason); - NLMISC::InfoLog->displayRawNL(reason.c_str()); - static volatile bool doAssert = true; - if (doAssert) // breakPoint can be discarded in case of looping assert - { - NLMISC_BREAKPOINT; - } - return 0; -} - - - -// *************************************************************************** -bool CLuaIHM::popString(CLuaState &ls, std::string & dest) -{ - //H_AUTO(Lua_CLuaIHM_popString) - try - { -#if LUABIND_VERSION > 600 - luabind::object obj(luabind::from_stack(ls.getStatePointer(), -1)); - ls.pop(); -#else - luabind::object obj(ls.getStatePointer()); - obj.set(); -#endif - dest = luabind::object_cast(obj); - } - catch(const luabind::cast_failed &) - { - return false; - } - return true; -} - -// *************************************************************************** -bool CLuaIHM::popSINT32(CLuaState &ls, sint32 & dest) -{ - //H_AUTO(Lua_CLuaIHM_popSINT32) - try - { -#if LUABIND_VERSION > 600 - luabind::object obj(luabind::from_stack(ls.getStatePointer(), -1)); - ls.pop(); -#else - luabind::object obj(ls.getStatePointer()); - obj.set(); -#endif - dest = luabind::object_cast(obj); - } - catch(const luabind::cast_failed &) - { - return false; - } - return true; -} - - -// *************************************************************************** -int CLuaIHM::getWindowSize(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getWindowSize) - checkArgCount(ls, "getWindowSize", 0); - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - uint32 w, h; - pIM->getViewRenderer().getScreenSize(w, h); - ls.push((double) w); - ls.push((double) h); - return 2; -} - - -// *************************************************************************** -int CLuaIHM::setTextFormatTaged(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_setTextFormatTaged) - // params: CViewText*, "text" (or ucstring) - // return: none - checkArgCount(ls, "setTextFormatTaged", 2); - - // *** check and retrieve param 1 - check(ls, isUIOnStack(ls, 1), "setTextFormatTaged() requires a UI object in param 1"); - CInterfaceElement *pIE= getUIOnStack(ls, 1); - - // *** check and retrieve param 2. must be a string or a ucstring - ucstring text; - if(ls.isString(2)) - { - std::string str; - ls.toString(2, str); - text= str; - } - else - { - // try to pop a ucstring from the stack - // fail? - if(!pop(ls, text)) - { - check(ls, false, "setTextFormatTaged() requires a string or a ucstring in param 2"); - } - } - - // must be a view text - CViewText *vt= dynamic_cast(pIE); - if(!vt) - throw ELuaIHMException("setTextFormatTaged(): '%s' is not a CViewText", pIE->getId().c_str()); - - // Set the text as format - vt->setTextFormatTaged(text); - - return 0; -} - -// *************************************************************************** -sint32 CLuaIHM::getMagicResistChance(bool elementalSpell, sint32 casterSpellLvl, sint32 victimResistLvl) -{ - //H_AUTO(Lua_CLuaIHM_getMagicResistChance) - CSPhraseManager *pPM= CSPhraseManager::getInstance(); - casterSpellLvl= std::max(casterSpellLvl, sint32(0)); - victimResistLvl= std::max(victimResistLvl, sint32(0)); - /* The success rate in the table is actually the "Casting Success Chance". - Thus, the relativeLevel is casterSpellLvl - victimResistLvl - Moreover, must take the "PartialSuccessMaxDraw" line because the spell is not resisted if success>0 - */ - sint32 chanceToHit= pPM->getSuccessRate(elementalSpell?CSPhraseManager::STResistMagic:CSPhraseManager::STResistMagicLink, - casterSpellLvl-victimResistLvl, true); - clamp(chanceToHit, 0, 100); - - // Thus, the resist chance is 100 - hit chance. - return 100 - chanceToHit; -} - -// *************************************************************************** -sint32 CLuaIHM::getDodgeParryChance(sint32 attLvl, sint32 defLvl) -{ - //H_AUTO(Lua_CLuaIHM_getDodgeParryChance) - CSPhraseManager *pPM = CSPhraseManager::getInstance(); - attLvl= std::max(attLvl, sint32(0)); - defLvl= std::max(defLvl, sint32(0)); - - sint32 chance = pPM->getSuccessRate(CSPhraseManager::STDodgeParry, defLvl-attLvl, false); - clamp(chance, 0, 100); - - return chance; -} - -// *************************************************************************** -void CLuaIHM::browseNpcWebPage(const std::string &htmlId, const std::string &urlIn, bool addParameters, double timeout) -{ - //H_AUTO(Lua_CLuaIHM_browseNpcWebPage) - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - CGroupHTML *groupHtml= dynamic_cast(pIM->getElementFromId(htmlId)); - if(groupHtml) - { - // if true, it means that we want to display a web page that use webig auth - bool webig = urlIn.find("http://") == 0; - - string url; - // append the WebServer to the url - if (urlIn.find("ring_access_point=1") != std::string::npos) - { - url = RingMainURL + "?" + urlIn; - } - else if(webig) - { - url = urlIn; - } - else - { - url = WebServer + urlIn; - } - - if (addParameters && !webig) - { - // append shardid, playername and language code - string userName; - string guildName; - if(UserEntity) - { - userName = UserEntity->getDisplayName ().toString(); - STRING_MANAGER::CStringManagerClient *pSMC = STRING_MANAGER::CStringManagerClient::instance(); - ucstring ucsTmp; - pSMC->getString (UserEntity->getGuildNameID(), ucsTmp); - guildName = ucsTmp.toString(); - - while (guildName.find(' ') != string::npos) - { - guildName[guildName.find(' ')] = '_'; - } - } - - url += ((url.find('?') != string::npos) ? "&" : "?") + - string("shard=") + toString(ShardId) + - string("&user_login=") + userName + - string("&lang=") + ClientCfg.getHtmlLanguageCode() + - string("&guild_name=") + guildName; - } -/* Already added by GroupHtml - if(webig) - { - // append special webig auth params - addWebIGParams(url); - } -*/ - // set the wanted timeout - groupHtml->setTimeout((float)std::max(0.0, timeout)); - - // Browse the url - groupHtml->clean(); - groupHtml->browse(url.c_str()); - // Set top of the page - CCtrlScroll *pScroll = groupHtml->getScrollBar(); - if (pScroll != NULL) - pScroll->moveTrackY(10000); - } -} - -// *************************************************************************** -void CLuaIHM::clearHtmlUndoRedo(const std::string &htmlId) -{ - //H_AUTO(Lua_CLuaIHM_clearHtmlUndoRedo) - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - CGroupHTML *groupHtml= dynamic_cast(pIM->getElementFromId(htmlId)); - if(groupHtml) - groupHtml->clearUndoRedo(); -} - -// *************************************************************************** -ucstring CLuaIHM::getDynString(sint32 dynStringId) -{ - //H_AUTO(Lua_CLuaIHM_getDynString) - ucstring result; - STRING_MANAGER::CStringManagerClient::instance()->getDynString(dynStringId, result); - return result; -} - -// *************************************************************************** -bool CLuaIHM::isDynStringAvailable(sint32 dynStringId) -{ - //H_AUTO(Lua_CLuaIHM_isDynStringAvailable) - ucstring result; - bool res = STRING_MANAGER::CStringManagerClient::instance()->getDynString(dynStringId, result); - return res; -} - -// *************************************************************************** -bool CLuaIHM::isFullyPatched() -{ - return AvailablePatchs == 0; -} - -// *************************************************************************** -std::string CLuaIHM::getSheetType(const std::string &sheet) -{ - //H_AUTO(Lua_CLuaIHM_getSheetType) - const CEntitySheet *sheetPtr = SheetMngr.get(CSheetId(sheet)); - if (!sheetPtr) return ""; - return CEntitySheet::typeToString(sheetPtr->Type); -} - - -// *************************************************************************** -std::string CLuaIHM::getSheetName(uint32 sheetId) -{ - return CSheetId(sheetId).toString(); -} - -// *************************************************************************** -sint32 CLuaIHM::getFameIndex(const std::string &factionName) -{ - //H_AUTO(Lua_CLuaIHM_getFameIndex) - return CStaticFames::getInstance().getFactionIndex(factionName); -} - -// *************************************************************************** -std::string CLuaIHM::getFameName(sint32 fameIndex) -{ - //H_AUTO(Lua_CLuaIHM_getFameName) - return CStaticFames::getInstance().getFactionName(fameIndex); -} - -// *************************************************************************** -sint32 CLuaIHM::getFameDBIndex(sint32 fameIndex) -{ - //H_AUTO(Lua_CLuaIHM_getFameDBIndex) - // Yoyo: avoid crash if fames not initialized - if(CStaticFames::getInstance().getNbFame()==0) - return 0; - else - return CStaticFames::getInstance().getDatabaseIndex(fameIndex); -} - -// *************************************************************************** -sint32 CLuaIHM::getFirstTribeFameIndex() -{ - //H_AUTO(Lua_CLuaIHM_getFirstTribeFameIndex) - return CStaticFames::getInstance().getFirstTribeFameIndex(); -} - -// *************************************************************************** -sint32 CLuaIHM::getNbTribeFameIndex() -{ - //H_AUTO(Lua_CLuaIHM_getNbTribeFameIndex) - // Yoyo: avoid crash if fames not initialized. at leasst one tribe - return std::max(1U, CStaticFames::getInstance().getNbTribeFameIndex()); -} - -// *************************************************************************** -bool CLuaIHM::fileExists(const string &fileName) -{ - //H_AUTO(Lua_CLuaIHM_fileExists) - return CPath::exists(fileName); -} - -// *************************************************************************** -string CLuaIHM::getClientCfg(const string &varName) -{ - //H_AUTO(Lua_CLuaIHM_getClientCfg) - return ClientCfg.readString(varName); -} - -// *************************************************************************** -void CLuaIHM::sendMsgToServer(const std::string &sMsg) -{ - //H_AUTO(Lua_CLuaIHM_sendMsgToServer) - ::sendMsgToServer(sMsg); -} - -// *************************************************************************** -void CLuaIHM::sendMsgToServerPvpTag(bool pvpTag) -{ - //H_AUTO(Lua_CLuaIHM_sendMsgToServerPvpTag) - uint8 tag = (uint8)pvpTag; - ::sendMsgToServer("PVP:PVP_TAG", tag); -} - - -// *************************************************************************** -bool CLuaIHM::isGuildQuitAvailable() -{ - //H_AUTO(Lua_CLuaIHM_isGuildQuitAvailable) - return CGuildManager::getInstance()->getGuild().QuitGuildAvailable; -} - -// *************************************************************************** -void CLuaIHM::sortGuildMembers() -{ - //H_AUTO(Lua_CLuaIHM_sortGuildMembers) - CGuildManager::getInstance()->sortGuildMembers(); -} - -// *************************************************************************** -sint32 CLuaIHM::getNbGuildMembers() -{ - //H_AUTO(Lua_CLuaIHM_getNbGuildMembers) - return (sint32)CGuildManager::getInstance()->getGuildMembers().size(); -} - -// *************************************************************************** -string CLuaIHM::getGuildMemberName(sint32 nMemberId) -{ - //H_AUTO(Lua_CLuaIHM_getGuildMemberName) - if ((nMemberId < 0) || (nMemberId >= getNbGuildMembers())) - return ""; - return CGuildManager::getInstance()->getGuildMembers()[nMemberId].Name.toString(); -} - -// *************************************************************************** -string CLuaIHM::getGuildMemberGrade(sint32 nMemberId) -{ - //H_AUTO(Lua_CLuaIHM_getGuildMemberGrade) - if ((nMemberId < 0) || (nMemberId >= getNbGuildMembers())) - return ""; - return EGSPD::CGuildGrade::toString(CGuildManager::getInstance()->getGuildMembers()[nMemberId].Grade); -} - -// *************************************************************************** -bool CLuaIHM::isR2Player(const std::string &sheet) -{ - //H_AUTO(Lua_CLuaIHM_isR2Player) - const CEntitySheet *entitySheet = SheetMngr.get(CSheetId(sheet)); - if (!entitySheet) return false; - const CCharacterSheet *chSheet = dynamic_cast(entitySheet); - if(!chSheet) return false; - return chSheet->R2Npc; -} - -// *************************************************************************** -std::string CLuaIHM::getR2PlayerRace(const std::string &sheet) -{ - //H_AUTO(Lua_CLuaIHM_getR2PlayerRace) - const CEntitySheet *entitySheet = SheetMngr.get(CSheetId(sheet)); - if (!entitySheet) return ""; - const CCharacterSheet *chSheet = dynamic_cast(entitySheet); - if(!chSheet) return ""; - return EGSPD::CPeople::toString(chSheet->Race); -} - -// *************************************************************************** -bool CLuaIHM::isR2PlayerMale(const std::string &sheet) -{ - //H_AUTO(Lua_CLuaIHM_isR2PlayerMale) - const CEntitySheet *entitySheet = SheetMngr.get(CSheetId(sheet)); - if (!entitySheet) return true; - const CCharacterSheet *chSheet = dynamic_cast(entitySheet); - if(!chSheet) return true; - - return (chSheet->Gender == GSGENDER::male); -} - -// *************************************************************************** -std::string CLuaIHM::getCharacterSheetSkel(const std::string &sheet, bool isMale) -{ - //H_AUTO(Lua_CLuaIHM_getCharacterSheetSkel) - const CEntitySheet *sheetPtr = SheetMngr.get(CSheetId(sheet)); - const CCharacterSheet *charSheet = dynamic_cast(sheetPtr); - if (charSheet) return charSheet->getSkelFilename(); - const CRaceStatsSheet *raceStatSheet = dynamic_cast(sheetPtr); - if (raceStatSheet) return raceStatSheet->GenderInfos[isMale ? 0 : 1].Skelfilename; - return ""; -} - -// *************************************************************************** -sint32 CLuaIHM::getSheetId(const std::string &itemName) -{ - //H_AUTO(Lua_CLuaIHM_getSheetId) - return (sint32)CSheetId(itemName).asInt(); -} - -// *************************************************************************** -sint CLuaIHM::getCharacterSheetRegionForce(const std::string &sheet) -{ - //H_AUTO(Lua_CLuaIHM_getCharacterSheetRegionForce) - const CCharacterSheet *charSheet = dynamic_cast(SheetMngr.get(CSheetId(sheet))); - if (!charSheet) return 0; - return charSheet->RegionForce; -} - -// *************************************************************************** -sint CLuaIHM::getCharacterSheetRegionLevel(const std::string &sheet) -{ - //H_AUTO(Lua_CLuaIHM_getCharacterSheetRegionLevel) - const CCharacterSheet *charSheet = dynamic_cast(SheetMngr.get(CSheetId(sheet))); - if (!charSheet) return 0; - return charSheet->RegionForce; -} - -// *************************************************************************** -bool CLuaIHM::isCtrlKeyDown() -{ - //H_AUTO(Lua_CLuaIHM_isCtrlKeyDown) - bool ctrlDown = Driver->AsyncListener.isKeyDown(KeyLCONTROL) || - Driver->AsyncListener.isKeyDown(KeyRCONTROL); - if (ctrlDown) nlwarning("ctrl down"); - else nlwarning("ctrl up"); - return ctrlDown; -} - -// *************************************************************************** -std::string CLuaIHM::encodeURLUnicodeParam(const ucstring &text) -{ - //H_AUTO(Lua_CLuaIHM_encodeURLUnicodeParam) - return convertToHTML(text.toUtf8()); -} - -// *************************************************************************** -ucstring CLuaIHM::replacePvpEffectParam(const ucstring &str, sint32 parameter) -{ - //H_AUTO(Lua_CLuaIHM_replacePvpEffectParam) - ucstring result = str; - CSString s = str.toString(); - std::string p, paramString; - - // Locate parameter and store it - p = s.splitTo('%', true); - while (p.size() > 0 && s.size() > 0) - { - if (s[0] == 'p' || s[0] == 'n' || s[0] == 'r') - { - paramString = "%"; - paramString += s[0]; - break; - } - p = s.splitTo('%', true); - } - - // Return original string if param isn't found - if (paramString.size() < 2) - return str; - - // Replace parameter based on its type - switch (paramString[1]) - { - case 'p': - p = toString("%.1f %%", parameter/100.0); - break; - case 'n': - p = toString(parameter); - break; - case 'r': - p = toString("%.1f", parameter/100.0); - break; - default: - debugInfo("Bad arguments in " + str.toString() + " : " + paramString); - } - - strFindReplace(result, paramString.c_str(), p); - - return result; -} - -// *************************************************************************** -string CLuaIHM::getRegionByAlias(uint32 alias) -{ - //H_AUTO(Lua_CLuaIHM_getRegionByAlias) - return ContinentMngr.getRegionNameByAlias(alias); -} - -struct CEmoteStruct -{ - string EmoteId; - string Path; - string Anim; - bool UsableFromClientUI; - - bool operator< (const CEmoteStruct & entry) const - { - string path1 = Path; - string path2 = entry.Path; - - for(;;) - { - string::size_type pos1 = path1.find('|'); - string::size_type pos2 = path2.find('|'); - - ucstring s1 = toUpper(CI18N::get(path1.substr(0, pos1))); - ucstring s2 = toUpper(CI18N::get(path2.substr(0, pos2))); - - sint result = s1.compare(s2); - if (result != 0) - return (result < 0); - - if (pos1 == string::npos) - return (pos2 != string::npos); - if (pos2 == string::npos) - return false; - - path1 = path1.substr(pos1 + 1); - path2 = path2.substr(pos2 + 1); - } - return false; - } -}; - -// *************************************************************************** -int CLuaIHM::initEmotesMenu(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_initEmotesMenu) - CLuaIHM::checkArgCount(ls, "initEmotesMenu", 2); - CLuaIHM::checkArgType(ls, "initEmotesMenu", 2, LUA_TSTRING); - const std::string & emoteMenu = ls.toString(1); - const std::string & luaParams = ls.toString(2); - - ls.newTable(); - CLuaObject result(ls); - std::map emoteList; - uint maxVisibleLine=10; - - CTextEmotListSheet *pTELS = dynamic_cast(SheetMngr.get(CSheetId("list.text_emotes"))); - if (pTELS == NULL) - return 0; - - std::list entries; - if (entries.empty()) - { - for (uint i = 0; i < pTELS->TextEmotList.size(); i++) - { - CEmoteStruct entry; - entry.EmoteId = pTELS->TextEmotList[i].EmoteId; - entry.Path = pTELS->TextEmotList[i].Path; - entry.Anim = pTELS->TextEmotList[i].Anim; - entry.UsableFromClientUI = pTELS->TextEmotList[i].UsableFromClientUI; - entries.push_back(entry); - } - entries.sort(); - } - - // The list of behaviour missnames emotList - CEmotListSheet *pEmotList = dynamic_cast(SheetMngr.get(CSheetId("list.emot"))); - nlassert (pEmotList != NULL); - nlassert (pEmotList->Emots.size() <= 255); - - // Get the focus beta tester flag - bool betaTester = false; - - CInterfaceManager *pIM = CInterfaceManager::getInstance(); - CSkillManager *pSM = CSkillManager::getInstance(); - - betaTester = pSM->isTitleUnblocked(CHARACTER_TITLE::FBT); - - CGroupMenu *pInitRootMenu = dynamic_cast(pIM->getElementFromId(emoteMenu)); - pInitRootMenu->reset(); - - for (std::list::const_iterator it = entries.begin(); it != entries.end(); it++) - { - std::string sEmoteId = (*it).EmoteId; - std::string sState = (*it).Anim; - std::string sName = (*it).Path; - - // Check that the emote can be added to UI - // --------------------------------------- - if( (*it).UsableFromClientUI == false ) - { - continue; - } - - // Check the emote reserved for FBT (hardcoded) - // -------------------------------------------- - if (sState == "FBT" && !betaTester) - continue; - - uint32 i, j; - // Add to the game context menu - // ---------------------------- - uint32 nbToken = 1; - for (i = 0; i < sName.size(); ++i) - if (sName[i] == '|') - nbToken++; - - CGroupMenu *pRootMenu = dynamic_cast(pIM->getElementFromId(emoteMenu)); - CGroupSubMenu *pMenu = pRootMenu->getRootMenu(); - - for (i = 0; i < nbToken; ++i) - { - if(i==0) - { - sName = sName.substr(sName.find('|')+1,sName.size()); - } - else - { - string sTmp; - if (i != (nbToken-1)) - sTmp = sName.substr(0,sName.find('|')); - else - sTmp = sName; - - - - // Look if this part of the path is already present - bool bFound = false; - for (j = 0; j < pMenu->getNumLine(); ++j) - { - if (sTmp == pMenu->getLineId(j)) - { - bFound = true; - break; - } - } - - if (!bFound) // Create it - { - if (i != (nbToken-1)) - { - pMenu->addLine (CI18N::get(sTmp), "", "", sTmp); - // Create a sub menu - CGroupSubMenu *pNewSubMenu = new CGroupSubMenu(CViewBase::TCtorParam()); - pMenu->setSubMenu(j, pNewSubMenu); - } - else - { - // Create a line - pMenu->addLine (CI18N::get(sTmp), "lua", - luaParams+"('"+sEmoteId+"', '"+toString(CI18N::get(sTmp))+"')", sTmp); - emoteList[sEmoteId] = (toLower(CI18N::get(sTmp))).toUtf8(); - } - } - - // Jump to sub menu - if (i != (nbToken-1)) - { - pMenu = pMenu->getSubMenu(j); - sName = sName.substr(sName.find('|')+1,sName.size()); - } - } - } - pMenu->setMaxVisibleLine(maxVisibleLine); - } - pInitRootMenu->setMaxVisibleLine(maxVisibleLine); - - std::map::iterator it; - for(it=emoteList.begin(); it!=emoteList.end(); it++) - { - result.setValue(it->first, it->second); - } - result.push(); - - return 1; -} - -// *************************************************************************** -int CLuaIHM::isUCString(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_isUCString) - const char *funcName = "isUCString"; - checkArgCount(ls, funcName, 1); - ls.push(isUCStringOnStack(ls, 1)); - return 1; -} - -// *************************************************************************** -int CLuaIHM::hideAllWindows(CLuaState &/* ls */) -{ - //H_AUTO(Lua_CLuaIHM_hideAllWindows) - CInterfaceManager::getInstance()->hideAllWindows(); - return 0; -} - -// *************************************************************************** -int CLuaIHM::hideAllNonSavableWindows(CLuaState &/* ls */) -{ - //H_AUTO(Lua_CLuaIHM_hideAllNonSavableWindows) - CInterfaceManager::getInstance()->hideAllNonSavableWindows(); - return 0; -} - -// *************************************************************************** -int CLuaIHM::getDesktopIndex(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getDesktopIndex) - ls.push((double) CInterfaceManager::getInstance()->getMode()); - return 1; -} - -// *************************************************************************** -int CLuaIHM::setLuaBreakPoint(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_setLuaBreakPoint) - const char *funcName = "setLuaBreakPoint"; - CLuaIHM::checkArgCount(ls, funcName, 2); - CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); - CLuaIHM::checkArgType(ls, funcName, 2, LUA_TNUMBER); - - - #ifdef LUA_NEVRAX_VERSION - if (LuaDebuggerIDE) - { - LuaDebuggerIDE->setBreakPoint(ls.toString(1), (int) ls.toNumber(2)); - } - #endif - - return 0; -} - -// *************************************************************************** -int CLuaIHM::getMainPageURL(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getMainPageURL) - const char *funcName = "getMainPageURL"; - CLuaIHM::checkArgCount(ls, funcName, 0); - ls.push(RingMainURL); - return 1; -} - -// *************************************************************************** -int CLuaIHM::getCharSlot(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getCharSlot) - const char *funcName = "getCharSlot"; - CLuaIHM::checkArgCount(ls, funcName, 0); - ls.push(double(PlayerSelectedSlot)); - return 1; -} - -// *************************************************************************** -sint32 CLuaIHM::secondsSince1970ToHour(sint32 seconds) -{ - //H_AUTO(Lua_CLuaIHM_secondsSince1970ToHour) - // convert to readable form - struct tm *tstruct; - time_t tval= seconds; - tstruct= gmtime(&tval); - if(!tstruct) + //H_AUTO(Lua_CLuaIHM_checkArgTypeUIElement) + nlassert(index > 0); + if (ls.getTop() < (int) index) { - debugInfo(toString("Bad Date Received: %d", seconds)); - return 0; + fails(ls, "%s : argument %d of expected type ui element was not defined", funcName, index); } - - return tstruct->tm_hour; // 0-23 -} - -int CLuaIHM::getPathContent(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getPathContent) - const char *funcName = "getPathContent"; - CLuaIHM::checkArgCount(ls, funcName, 1); - CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); - std::vector files; - NLMISC::CPath::getPathContent(ls.toString(1), false, false, true, files); - ls.newTable(); - for(uint k = 0; k < files.size(); ++k) + if (!isUIOnStack(ls, index)) { - ls.push((double) k); - ls.push(files[k]); - ls.setTable(-3); + fails(ls, "%s : argument %d of expected type ui element has bad type : %s", funcName, index, ls.getTypename(ls.type(index)), ls.type(index)); } - return 1; -} - -int CLuaIHM::getServerSeason(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getServerSeason) - const char *funcName = "getServerSeason"; - CLuaIHM::checkArgCount(ls, funcName, 0); - extern uint8 ServerSeasonValue; - ls.push((double) ServerSeasonValue); - return 1; -} - -int CLuaIHM::computeCurrSeason(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_computeCurrSeason) - const char *funcName = "computeCurrSeason"; - CLuaIHM::checkArgCount(ls, funcName, 0); - ls.push((double) (::computeCurrSeason() + 1)); - return 1; -} - -int CLuaIHM::getAutoSeason(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getAutoSeason) - const char *funcName = "getAutoSeason"; - CLuaIHM::checkArgCount(ls, funcName, 0); - ls.push((double) (StartupSeason + 1)); - return 1; -} - - - -int CLuaIHM::getTextureSize(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getTextureSize) - const char *funcName = "getTextureSize"; - CLuaIHM::checkArgCount(ls, funcName, 1); - CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); - std::string textureName = ls.toString(1); - - CBitmap bitmap; - CIFile fs(CPath::lookup(textureName).c_str()); - bitmap.load(fs); - - ls.push((double) bitmap.getWidth()); - ls.push((double) bitmap.getHeight()); - - return 2; } - -int CLuaIHM::enableModalWindow(CLuaState &ls) +// *************************************************************************** +void CLuaIHM::checkArgTypeUCString(CLuaState &ls, const char *funcName, uint index) { - //H_AUTO(Lua_CLuaIHM_enableModalWindow) - const char *funcName = "enableModalWindow"; - CLuaIHM::checkArgCount(ls, funcName, 2); - - check(ls, isUIOnStack(ls, 1), "enableModalWindow() requires a UI object in param 1"); - CLuaIHM::checkArgType(ls, funcName, 2, LUA_TSTRING); - - CInterfaceElement *pIE= getUIOnStack(ls, 1); - std::string modalId = ls.toString(2); - - // convert to id - if(pIE) + //H_AUTO(Lua_CLuaIHM_checkArgTypeUCString) + nlassert(index > 0); + if (ls.getTop() < (int) index) { - CCtrlBase * ctrl = dynamic_cast(pIE); - if(ctrl) - { - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - CInterfaceGroup *group= dynamic_cast( pIM->getElementFromId(modalId) ); - if(group) - { - UserControls.stopFreeLook(); - - // enable the modal - pIM->enableModalWindow(ctrl, group); - } - else - { - nlwarning(" Couldn't find group %s", modalId.c_str()); - } - - } + fails(ls, "%s : argument %d of expected type ucstring was not defined", funcName, index); + } + ls.pushValue(index); + ucstring dummy; + if (!pop(ls, dummy)) + { + fails(ls, "%s : argument %d of expected type ucstring has bad type : %s", funcName, index, ls.getTypename(ls.type(index)), ls.type(index)); } - - return 0; -} - -// *************************************************************************** -int CLuaIHM::disableModalWindow(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_disableModalWindow) - checkArgCount(ls, "disableModalWindow", 0); - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - pIM->disableModalWindow(); - return 0; -} - -// *************************************************************************** -int CLuaIHM::getPlayerPos(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_getPlayerPos) - checkArgCount(ls, "getPlayerPos", 0); - ls.push(UserEntity->pos().x); - ls.push(UserEntity->pos().y); - ls.push(UserEntity->pos().z); - return 3; -} - -// *************************************************************************** -int CLuaIHM::getPlayerFront(CLuaState &ls) -{ - checkArgCount(ls, "getPlayerFront", 0); - ls.push(atan2(UserEntity->front().y, UserEntity->front().x)); - return 1; -} - -// *************************************************************************** -int CLuaIHM::getPlayerDirection(CLuaState &ls) -{ - checkArgCount(ls, "getPlayerDirection", 0); - ls.push(atan2(UserEntity->dir().y, UserEntity->dir().x)); - return 1; -} - -// *************************************************************************** -int CLuaIHM::getPlayerGender(CLuaState &ls) -{ - checkArgCount(ls, "getPlayerGender", 0); - ls.push((lua_Number)(UserEntity->getGender())); - return 1; -} - -// *************************************************************************** -int CLuaIHM::getPlayerName(CLuaState &ls) -{ - checkArgCount(ls, "getPlayerName", 0); - ls.push(UserEntity->getEntityName().toUtf8()); - return 1; -} - -// *************************************************************************** -int CLuaIHM::getPlayerTitleRaw(CLuaState &ls) -{ - checkArgCount(ls, "getPlayerTitleRaw", 0); - ls.push(UserEntity->getTitleRaw().toUtf8()); - return 1; -} - -// *************************************************************************** -int CLuaIHM::getPlayerTitle(CLuaState &ls) -{ - checkArgCount(ls, "getPlayerTitle", 0); - ls.push(UserEntity->getTitle().toUtf8()); - return 1; -} - -// *************************************************************************** -int CLuaIHM::getTargetPos(CLuaState &ls) -{ - checkArgCount(ls, "getTargetPos", 0); - CEntityCL *target = getTargetEntity(); - if (!target) return 0; - ls.push(target->pos().x); - ls.push(target->pos().y); - ls.push(target->pos().z); - return 3; -} - -// *************************************************************************** -int CLuaIHM::getTargetFront(CLuaState &ls) -{ - checkArgCount(ls, "getTargetFront", 0); - CEntityCL *target = getTargetEntity(); - if (!target) return 0; - ls.push(atan2(target->front().y, target->front().x)); - return 1; -} - -// *************************************************************************** -int CLuaIHM::getTargetDirection(CLuaState &ls) -{ - checkArgCount(ls, "getTargetDirection", 0); - CEntityCL *target = getTargetEntity(); - if (!target) return 0; - ls.push(atan2(target->dir().y, target->dir().x)); - return 1; -} - -// *************************************************************************** -int CLuaIHM::getTargetGender(CLuaState &ls) -{ - checkArgCount(ls, "getTargetGender", 0); - CCharacterCL* target = (CCharacterCL*)getTargetEntity(); - if (!target) return (int)GSGENDER::unknown; - ls.push((lua_Number)(target->getGender())); - return 1; -} - -// *************************************************************************** -int CLuaIHM::getTargetName(CLuaState &ls) -{ - checkArgCount(ls, "getTargetName", 0); - CEntityCL *target = getTargetEntity(); - if (!target) return 0; - ls.push(target->getEntityName().toUtf8()); - return 1; } -// *************************************************************************** -int CLuaIHM::getTargetTitleRaw(CLuaState &ls) -{ - checkArgCount(ls, "getTargetTitleRaw", 0); - CEntityCL *target = getTargetEntity(); - if (!target) return 0; - ls.push(target->getTitleRaw().toUtf8()); - return 1; -} -// *************************************************************************** -int CLuaIHM::getTargetTitle(CLuaState &ls) -{ - checkArgCount(ls, "getTargetTitle", 0); - CEntityCL *target = getTargetEntity(); - if (!target) return 0; - ls.push(target->getTitle().toUtf8()); - return 1; -} // *************************************************************************** -int CLuaIHM::addSearchPathUser(CLuaState &ls) +bool CLuaIHM::popString(CLuaState &ls, std::string & dest) { - //H_AUTO(Lua_CLuaIHM_addSearchPathUser) - bool memoryCompressed = CPath::isMemoryCompressed(); - if (memoryCompressed) + //H_AUTO(Lua_CLuaIHM_popString) + try { - CPath::memoryUncompress(); +#if LUABIND_VERSION > 600 + luabind::object obj(luabind::from_stack(ls.getStatePointer(), -1)); + ls.pop(); +#else + luabind::object obj(ls.getStatePointer()); + obj.set(); +#endif + dest = luabind::object_cast(obj); } - CPath::addSearchPath("user/", true, false, NULL); - if (memoryCompressed) + catch(const luabind::cast_failed &) { - CPath::memoryCompress(); + return false; } - return 0; -} - -// *************************************************************************** -int CLuaIHM::isPlayerFreeTrial(CLuaState &ls) -{ - checkArgCount(ls, "isPlayerFreeTrial", 0); - ls.push(FreeTrial); - return 1; -} - - - -// *************************************************************************** -int CLuaIHM::disableContextHelp(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_disableContextHelp) - CLuaStackChecker lsc(&ls, 0); - checkArgCount(ls, "disableContextHelp", 0); - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - pIM->disableContextHelp(); - return 0; -} - -// *************************************************************************** -int CLuaIHM::disableContextHelpForControl(CLuaState &ls) -{ - //H_AUTO(Lua_CLuaIHM_disableContextHelpForControl) - CLuaStackChecker lsc(&ls, 0); - - // params: CCtrlBase* - // return: none - checkArgCount(ls, "disableContextHelpForControl", 1); - check(ls, isUIOnStack(ls, 1), "disableContextHelpForControl() requires a UI object in param 1"); - - // retrieve args - CInterfaceElement *pIE= getUIOnStack(ls, 1); - - // go - CInterfaceManager *pIM= CInterfaceManager::getInstance(); - pIM->disableContextHelpForControl(dynamic_cast(pIE)); - - return 0; + return true; } // *************************************************************************** -void CLuaIHM::tell(const ucstring &player, const ucstring &msg) +bool CLuaIHM::popSINT32(CLuaState &ls, sint32 & dest) { - //H_AUTO(Lua_CLuaIHM_tell) - // display a /tell command in the main chat - if (!player.empty()) + //H_AUTO(Lua_CLuaIHM_popSINT32) + try { - if (!msg.empty()) - { - // Parse any tokens in the message. - ucstring msg_modified = msg; - // Parse any tokens in the text - if ( ! CInterfaceManager::parseTokens(msg_modified)) - { - return; - } - ChatMngr.tell(player.toUtf8(), msg_modified); - } - else - { - CChatWindow *w = PeopleInterraction.ChatGroup.Window; - if (w) - { - CInterfaceManager *im = CInterfaceManager::getInstance(); - w->setKeyboardFocus(); - w->enableBlink(1); - w->setCommand(ucstring("tell ") + CEntityCL::removeTitleFromName(player) + ucstring(" "), false); - CGroupEditBox *eb = w->getEditBox(); - if (eb != NULL) - { - eb->bypassNextKey(); - } - if (w->getContainer()) - { - w->getContainer()->setActive(true); - im->setTopWindow(w->getContainer()); - } - } - } +#if LUABIND_VERSION > 600 + luabind::object obj(luabind::from_stack(ls.getStatePointer(), -1)); + ls.pop(); +#else + luabind::object obj(ls.getStatePointer()); + obj.set(); +#endif + dest = luabind::object_cast(obj); } -} - -// *************************************************************************** -bool CLuaIHM::isRingAccessPointInReach() -{ - //H_AUTO(Lua_CLuaIHM_isRingAccessPointInReach) - if (BotChatPageAll->RingSessions->RingAccessPointPos == CVector::Null) return false; - const CVectorD &vect1 = BotChatPageAll->RingSessions->RingAccessPointPos; - CVectorD vect2 = UserEntity->pos(); - double distanceSquare = pow(vect1.x-vect2.x,2) + pow(vect1.y-vect2.y,2); - return distanceSquare <= MaxTalkingDistSquare; -} - -// *************************************************************************** -void CLuaIHM::updateTooltipCoords() -{ - CInterfaceManager::getInstance()->updateTooltipCoords(); + catch(const luabind::cast_failed &) + { + return false; + } + return true; } // *************************************************************************** @@ -4732,88 +1736,12 @@ void CLuaIHM::getPoly2DOnStack(CLuaState &ls, sint index, NLMISC::CPolygon2D &de { it.nextValue().push(); NLMISC::CVector2f pos; - if (!CLuaIHM::pop(ls, pos)) + if (!pop(ls, pos)) { - CLuaIHM::fails(ls, "2D polygon expects CVector2f for poly coordinates"); + fails(ls, "2D polygon expects CVector2f for poly coordinates"); } dest.Vertices.push_back(pos); } } -// *************************************************************************** -int CLuaIHM::isPlayerNewbie(CLuaState &ls) -{ - checkArgCount(ls, "isPlayerNewbie", 0); - CInterfaceManager *im = CInterfaceManager::getInstance(); - ls.push(im->getDbProp("SERVER:USER:IS_NEWBIE")->getValueBool()); - return 1; -} - - -// *************************************************************************** -int CLuaIHM::isInRingMode(CLuaState &ls) -{ - checkArgCount(ls, "isInRingMode", 0); - extern bool IsInRingMode(); - ls.push(IsInRingMode()); - return 1; -} - -// *************************************************************************** -int CLuaIHM::getUserRace(CLuaState &ls) -{ - checkArgCount(ls, "getUserRace", 0); - if (!UserEntity || !UserEntity->playerSheet()) - { - ls.push("Unknwown"); - } - else - { - ls.push(EGSPD::CPeople::toString(UserEntity->playerSheet()->People)); - } - return 1; -} - -// *************************************************************************** -int CLuaIHM::getSheet2idx(CLuaState &ls) -{ - CLuaIHM::checkArgCount(ls, "getSheet2idx", 2); - CLuaIHM::checkArgType(ls, "getSheet2idx", 1, LUA_TSTRING); - CLuaIHM::checkArgType(ls, "getSheet2idx", 2, LUA_TNUMBER); - - const std::string & sheedtName = ls.toString(1); - uint32 slotId = (uint32)ls.toNumber(2); - - NLMISC::CSheetId sheetId; - - if (sheetId.buildSheetId(sheedtName)) - { - uint32 idx = CVisualSlotManager::getInstance()->sheet2Index(sheetId, (SLOTTYPE::EVisualSlot)slotId); - ls.push((lua_Number)idx); - } - else - return 0; - return 1; -} - -// *************************************************************************** -int CLuaIHM::getTargetSlot(CLuaState &ls) -{ - uint32 slot = (uint32)getTargetSlotNr(); - ls.push((lua_Number)slot); - return 1; -} - -// *************************************************************************** -int CLuaIHM::getSlotDataSetId(CLuaState &ls) -{ - CLuaIHM::checkArgCount(ls, "getSlotDataSetId", 1); - CLuaIHM::checkArgType(ls, "getSlotDataSetId", 1, LUA_TNUMBER); - - uint32 slot = (uint32)ls.toNumber(1); - CEntityCL *e = getSlotEntity(slot); - string id = toString(e->dataSetId()); - ls.push(id); - return 1; -} diff --git a/code/ryzom/client/src/interface_v3/lua_ihm.h b/code/ryzom/client/src/interface_v3/lua_ihm.h index 2826c1ac7..6894cc224 100644 --- a/code/ryzom/client/src/interface_v3/lua_ihm.h +++ b/code/ryzom/client/src/interface_v3/lua_ihm.h @@ -103,30 +103,6 @@ public: // helper : get a 2D poly (a table of cvector2f) from a lua table (throw on fail) static void getPoly2DOnStack(CLuaState &ls, sint index, NLMISC::CPolygon2D &dest); - // Print a message in the log. - // Lua messages must be enabled (with ClientCfg.DisplayLuaDebugInfo = 1) - // Additionnally, if ClientCfg.LuaDebugInfoGotoButtonEnabled is set, then - // a button will be created near the line to allow to goto the lua line that issued the message - // by using an external editor - static void debugInfo(const std::string &dbg); - // Print a message in the log - // No 'goto file' button is created - // Lua messages must be enabled (with ClientCfg.DisplayLuaDebugInfo = 1) - static void rawDebugInfo(const std::string &dbg); - // Dump callstack in the console - // Additionnally, if ClientCfg.LuaDebugInfoGotoButtonEnabled is set, then - // buttons will be created in fonr of eahc line to allow to goto the lua line that issued the message - // by using an external editor - static void dumpCallStack(int startStackLevel = 0); - static void getCallStackAsString(int startStackLevel, std::string &result); - - // Create a special tag that will add a 'goto' button for the given file and line - // The tag should be appended in front of a string to use with 'rawDebugInfo'. - // when the final string will be printed, a button will be created in front of it - // Requires that 'ClientCfg.LuaDebugInfoGotoButtonEnabled' is set to 1, else - // a, empty tag is returned - static std::string createGotoFileButtonTag(const char *fileName, uint line); - // argument checkin helpers static void checkArgCount(CLuaState &ls, const char* funcName, uint nArgs); // check that number of argument is exactly the one required static void checkArgMin(CLuaState &ls, const char* funcName, uint nArgs); // check that number of argument is at least the one required @@ -141,12 +117,6 @@ public: */ static void fails(CLuaState &ls, const char *format, ...); - /** execute function that is currently on the stack, possibly outputing error messages to the log - * \return true if execution succeeded - */ - static bool executeFunctionOnStack(CLuaState &ls, int numArgs, int numRet); - - // pop a sint32 from a lua stack, throw an exception on fail static bool popSINT32(CLuaState &ls, sint32 & dest); bool popString(CLuaState &ls, std::string & dest); @@ -184,231 +154,24 @@ private: // @{ // LUA exported Functions with luabind - static sint32 getPlayerLevel(); // get max level among player skills (magi, combat, crafting ,foraging) - static sint64 getPlayerVpa(); - static sint64 getPlayerVpb(); - static sint64 getPlayerVpc(); - static sint32 getTargetLevel(); // get current, precise level of the selected target, or -1 if there's no such selected target - static sint32 getTargetForceRegion(); // get 'force region' for current target, or -1 if there's no selected target - static sint32 getTargetLevelForce(); // get 'level force' for current target, or -1 if there's no selected target - static ucstring getTargetSheet(); // get the name of the target sheet (like 'zoha2old.creature') - static sint64 getTargetVpa(); - static sint64 getTargetVpb(); - static sint64 getTargetVpc(); - static bool isTargetNPC(); // return 'true' if the target is an npc - static bool isTargetPlayer(); // return 'true' if the target is a player - static bool isTargetUser(); // return 'true' if the target is the user - static bool isPlayerInPVPMode(); - static bool isTargetInPVPMode(); - - static void pauseBGDownloader(); - static void unpauseBGDownloader(); - static void requestBGDownloaderPriority(uint priority); - static sint getBGDownloaderPriority(); - static ucstring getPatchLastErrorMessage(); - static bool isInGame(); - static uint32 getPlayerSelectedSlot(); - static bool isPlayerSlotNewbieLand(uint32 slot); // test if one of the player slot is a newbieland one, if not so, client must be patched in order to continue static uint32 getLocalTime(); static double getPreciseLocalTime(); - static sint32 getDbProp(const std::string &dbProp); // return 0 if not found. - static void setDbProp(const std::string &dbProp, sint32 value); // Nb: the db prop is not created if not present. - static void addDbProp(const std::string &dbProp, sint32 value); // Nb: the db prop is created if not present. - static void delDbProp(const std::string &dbProp); - static std::string getDefine(const std::string &def); - static void messageBox(const ucstring &text); - static void messageBox(const ucstring &text, const std::string &masterGroup); - static void messageBox(const ucstring &text, const std::string &masterGroup, int caseMode); - static void messageBox(const std::string &text); - static void messageBoxWithHelp(const ucstring &text); - static void messageBoxWithHelp(const ucstring &text, const std::string &masterGroup); - static void messageBoxWithHelp(const ucstring &text, const std::string &masterGroup, int caseMode); - static void messageBoxWithHelp(const std::string &text); + static std::string findReplaceAll(const std::string &str, const std::string &search, const std::string &replace); static ucstring findReplaceAll(const ucstring &str, const ucstring &search, const ucstring &replace); - static bool fileExists(const std::string &fileName); // just for ease of use static ucstring findReplaceAll(const ucstring &str, const std::string &search, const std::string &replace); static ucstring findReplaceAll(const ucstring &str, const std::string &search, const ucstring &replace); static ucstring findReplaceAll(const ucstring &str, const ucstring &search, const std::string &replace); - static void setContextHelpText(const ucstring &text); - // GameInfo - static sint32 getSkillIdFromName(const std::string &def); - static ucstring getSkillLocalizedName(sint32 skillId); - static sint32 getMaxSkillValue(sint32 skillId); - static sint32 getBaseSkillValueMaxChildren(sint32 skillId); - static sint32 getMagicResistChance(bool elementalSpell, sint32 casterSpellLvl, sint32 victimResistLvl); - static sint32 getDodgeParryChance(sint32 attLvl, sint32 defLvl); - static void browseNpcWebPage(const std::string &htmlId, const std::string &url, bool addParameters, double timeout); - static void clearHtmlUndoRedo(const std::string &htmlId); - static ucstring getDynString(sint32 dynStringId); - static bool isDynStringAvailable(sint32 dynStringId); - static bool isFullyPatched(); - static std::string getSheetType(const std::string &sheet); - static std::string getSheetName(uint32 sheetId); - static sint32 getFameIndex(const std::string &factionName); - static std::string getFameName(sint32 fameIndex); - static sint32 getFameDBIndex(sint32 fameIndex); // convert from the fame index - static sint32 getFirstTribeFameIndex(); // fame index of the 1st tribe - static sint32 getNbTribeFameIndex(); // number of tribe fame index (which are contiguous) - static std::string getClientCfg(const std::string &varName); - static void sendMsgToServer(const std::string &msgName); - static void sendMsgToServerPvpTag(bool pvpTag); - static ucstring replacePvpEffectParam(const ucstring &str, sint32 parameter); - static std::string getRegionByAlias(uint32 alias); - - static void tell(const ucstring &player, const ucstring &msg); // open the window to do a tell to 'player', if 'msg' is not empty, then the message will be sent immediatly - // else, current command of the chat window will be replaced with tell 'player' - - static bool isGuildQuitAvailable(); - static void sortGuildMembers(); - static sint32 getNbGuildMembers(); - static std::string getGuildMemberName(sint32 nMemberId); - static std::string getGuildMemberGrade(sint32 nMemberId); - static sint32 secondsSince1970ToHour(sint32 seconds); - - // sheet access - // TODO nico : using the reflection system on sheets would allow to export them to lua without these functions ... - static std::string getCharacterSheetSkel(const std::string &sheet, bool isMale); - static sint32 getSheetId(const std::string &itemName); - static bool isR2Player(const std::string &sheet); - static std::string getR2PlayerRace(const std::string &sheet); - static bool isR2PlayerMale(const std::string &sheet); - static sint getCharacterSheetRegionForce(const std::string &sheet); - static sint getCharacterSheetRegionLevel(const std::string &sheet); - - static bool isCtrlKeyDown(); // test if the ctrl key is down (NB nico : I didn't add other key, - // because it would be too easy to write a key recorder ...) - - static std::string encodeURLUnicodeParam(const ucstring &text); - static bool isRingAccessPointInReach(); - - static void updateTooltipCoords(); - - - // LUA exported Functions with standard lua (because use ui object, use variable param number, or return dynamic-typed object) - static int setCaptureKeyboard(CLuaState &ls); - static int resetCaptureKeyboard(CLuaState &ls); - static int setOnDraw(CLuaState &ls); // params: CInterfaceGroup*, "script". return: none - static int addOnDbChange(CLuaState &ls); // params: CInterfaceGroup*, "dblist", "script". return: none - static int removeOnDbChange(CLuaState &ls);// params: CInterfaceGroup*. return: none - static int getUICaller(CLuaState &ls); // params: none. return: CInterfaceElement* (nil if error) - static int getCurrentWindowUnder(CLuaState &ls); // params: none. return: CInterfaceElement* (nil if none) - static int getUI(CLuaState &ls); // params: "ui:interface:...". return: CInterfaceElement* (nil if error), an additionnal boolean parameter - // can specify verbose display when the element is note found (default is true) - static int createGroupInstance(CLuaState &ls); // params : param 1 = template name, - // param 2 = id of parent where the instance will be inserted - // param 3 = table with ("template_param", "template_param_value") key/value pairs - // such as { id="foo", x="10" } etc. -> returns a new instance of the template, or nil on fail - static int createRootGroupInstance(CLuaState &ls); // params : param 1 = template name, - // param 2 = id of parent where the instance will be inserted - // param 3 = table with ("template_param", "template_param_value") key/value pairs - // such as { id="foo", x="10" } etc. -> returns a new instance of the template, or nil on fail - static int createUIElement(CLuaState &ls); // params : param 1 = template name, - // param 2 = id of parent where the instance will be inserted - // param 3 = table with ("template_param", "template_param_value") key/value pairs - // such as { id="foo", x="10" } etc. -> returns a new instance of the template, or nil on fail - - static int displayBubble(CLuaState &ls); // params : param 1 = bot id - // param 2 = text - // param 3 = table with all strings and urls - // {"main text"="http:///", "text option 1"="http:///", "text option 2"="http:///") etc... - static int getIndexInDB(CLuaState &ls); // params: CDBCtrlSheet*.... return: index, or 0 if error - static int getUIId(CLuaState &ls); // params: CInterfaceElement*. return: ui id (empty if error) - static int runAH(CLuaState &ls); // params: CInterfaceElement *, "ah", "params". return: none - static int runExpr(CLuaState &ls); // params: "expr". return: any of: nil,bool,string,number, RGBA, UCString - static int runFct(CLuaState &ls); // params: "expr", param1, param2.... return: any of: nil,bool,string,number, RGBA, UCString - static int runCommand(CLuaState &ls); // params: "command name", param1, param2 ... return true or false - static int formatUI(CLuaState &ls); // params: "expr", param1, param2.... return: string with # and % parsed - static int formatDB(CLuaState &ls); // params: param1, param2.... return: string with @ and , added - static int launchContextMenuInGame(CLuaState &ls); // params : menu name - static int parseInterfaceFromString(CLuaState &ls); // params : intreface script - static int updateAllLocalisedElements(CLuaState &ls); - static int breakPoint(CLuaState &ls); - static int getWindowSize(CLuaState &ls); - static int i18n(CLuaState &ls); // retrieve an unicode string from CI18N - static int setTextFormatTaged(CLuaState &ls); // set a text that may contains Tag Format infos - static int validMessageBox(CLuaState &ls); // ok/cancel type message box (can't get it to work through luabind) - static int concatUCString(CLuaState &ls); // workaround for + operator that don't work in luabind for ucstrings ... - static int concatString(CLuaState &ls); // speedup concatenation of several strings - static int tableToString(CLuaState &ls); // concat element of a table to build a string - static int setTopWindow(CLuaState &ls); // set the top window - static int initEmotesMenu(CLuaState &ls); - static int isUCString(CLuaState &ls); - static int hideAllWindows(CLuaState &ls); - static int hideAllNonSavableWindows(CLuaState &ls); - static int getDesktopIndex(CLuaState &ls); - static int setLuaBreakPoint(CLuaState &ls); // set a breakpoint in lua external debugger (file, line) - static int getMainPageURL(CLuaState &ls); - static int getCharSlot(CLuaState &ls); - static int displaySystemInfo(CLuaState &ls); - static int setWeatherValue(CLuaState &ls); // first value is a boolean to say automatic, second value ranges from of to 1 and gives the weather - static int getWeatherValue(CLuaState &ls); // get current real weather value (blend between server driven value & predicted value). Manual weather value is ignored - static int disableContextHelpForControl(CLuaState &ls); // params: CCtrlBase*. return: none - static int disableContextHelp(CLuaState &ls); - static int getPathContent(CLuaState &ls); - static int getServerSeason(CLuaState &ls); // get the last season sent by the server - // 0->auto, computed locally from the current day (or not received from server yet) - // 1->server force spring - // 2->' ' ' summer - // 3->' ' ' autumn - // 4->' ' ' winter - static int computeCurrSeason(CLuaState &ls); // compute current displayed season (1->spring, etc .) - static int getAutoSeason(CLuaState &ls); // compute automatic season that would be at this time (1->spring, etc .) - - - static int getTextureSize(CLuaState &ls); - static int enableModalWindow(CLuaState &ls); - static int disableModalWindow(CLuaState &ls); - static int getPlayerPos(CLuaState &ls); - static int getPlayerFront(CLuaState &ls); - static int getPlayerDirection(CLuaState &ls); - static int getPlayerGender(CLuaState &ls); - static int getPlayerName(CLuaState &ls); - static int getPlayerTitleRaw(CLuaState &ls); - static int getPlayerTitle(CLuaState &ls); - static int getTargetPos(CLuaState &ls); - static int getTargetFront(CLuaState &ls); - static int getTargetDirection(CLuaState &ls); - static int getTargetGender(CLuaState &ls); - static int getTargetName(CLuaState &ls); - static int getTargetTitleRaw(CLuaState &ls); - static int getTargetTitle(CLuaState &ls); - static int addSearchPathUser(CLuaState &ls); - static int getClientCfgVar(CLuaState &ls); - static int isPlayerFreeTrial(CLuaState &ls); - static int isPlayerNewbie(CLuaState &ls); - static int isInRingMode(CLuaState &ls); - static int getUserRace(CLuaState &ls); - static int getSheet2idx(CLuaState &ls); - static int getTargetSlot(CLuaState &ls); - static int getSlotDataSetId(CLuaState &ls); - - - // LUA functions exported for Dev only (debug) - static int deleteUI(CLuaState &ls); // params: CInterfaceElement*.... return: none - static int deleteReflectable(CLuaState &ls); // params: CInterfaceElement*.... return: none - static int dumpUI(CLuaState &ls); // params: CInterfaceElement*.... return: none - static int setKeyboardContext(CLuaState &ls); - - - - // @} - - // Function export tools - static int runExprAndPushResult(CLuaState &ls, const std::string &expr); // Used by runExpr and runFct - // Function to forward lua call to C++ to a 'lua method' exported from a reflected object static int luaMethodCall(lua_State *ls); - static int getCompleteIslands(CLuaState &ls); - static int getIslandId(CLuaState &ls);//TEMP - - +public: + static int runExprAndPushResult(CLuaState &ls, const std::string &expr); // Used by runExpr and runFct }; diff --git a/code/ryzom/client/src/interface_v3/lua_ihm_ryzom.cpp b/code/ryzom/client/src/interface_v3/lua_ihm_ryzom.cpp new file mode 100644 index 000000000..cb9ceae2e --- /dev/null +++ b/code/ryzom/client/src/interface_v3/lua_ihm_ryzom.cpp @@ -0,0 +1,3271 @@ +#include + +// to get rid of you_must_not_use_assert___use_nl_assert___read_debug_h_file messages +#include +#ifdef assert + #undef assert +#endif + +// Warning: cannot use namespace std, when using luabind +#ifdef NL_OS_WINDOWS +# ifndef NL_EXTENDED_FOR_SCOPE +# undef for +# endif +#endif + +#ifdef NL_DEBUG +# define assert(x) nlassert(x) +#else +# define assert(x) +#endif + +#include +// in luabind > 0.6, LUABIND_MAX_ARITY is set to 10 +#if LUABIND_MAX_ARITY == 10 +# include +// only luabind > 0.7 have version.hpp (file checked with build system) +# ifdef HAVE_LUABIND_VERSION +# include +# endif +# ifndef LUABIND_VERSION +// luabind 0.7 doesn't define LUABIND_VERSION +# define LUABIND_VERSION 700 +# endif +// luabind 0.6 doesn't define LUABIND_VERSION but LUABIND_MAX_ARITY is set to 5 +#elif LUABIND_MAX_ARITY == 5 +# define LUABIND_VERSION 600 +#else +# pragma error("luabind version not recognized") +#endif + +#include "lua_ihm_ryzom.h" +#include "interface_manager.h" +#include "nel/gui/lua_helper.h" +#include "lua_object.h" + +#include "lua_ihm.h" +#include "reflect.h" +#include "action_handler.h" +#include "action_handler_tools.h" +#include "interface_manager.h" +#include "interface_group.h" +#include "view_text.h" +#include "game_share/people_pd.h" +#include "group_tree.h" +#include "interface_link.h" +#include "interface_expr.h" +#include "people_interraction.h" +#include "nel/misc/algo.h" +#include "nel/misc/file.h" +#include "nel/misc/i18n.h" +#include "nel/misc/time_nl.h" +#include "skill_manager.h" +#include "group_html.h" +#include "../net_manager.h" +#include "../user_entity.h" +#include "sphrase_manager.h" +#include "guild_manager.h" +#include "../client_cfg.h" +#include "../sheet_manager.h" +#include "lua_object.h" +#include "game_share/emote_list_parser.h" +#include "game_share/pvp_clan.h" +#include "../weather.h" +#include "../continent_manager.h" +#include "../zone_util.h" +#include "../motion/user_controls.h" +#include "group_html_cs.h" +#include "bonus_malus.h" +#include "group_editbox.h" +#include "../entities.h" +#include "../sheet_manager.h" // for emotes +#include "../global.h" // for emotes +#include "../entity_animation_manager.h" // for emotes +#include "../net_manager.h" // for emotes +#include "../client_chat_manager.h" // for emotes +#include "../login.h" +#include "lua_object.h" +#include "../actions.h" +#include "../bg_downloader_access.h" +#include "../connection.h" +#include "../login_patch.h" + +#include "bot_chat_page_all.h" +#include "bot_chat_page_ring_sessions.h" +#include "nel/georges/u_form_loader.h" +#include "nel/georges/u_form.h" +#include "nel/georges/u_form_elm.h" +#include "nel/misc/polygon.h" +#include "game_share/scenario_entry_points.h" +#include "game_share/bg_downloader_msg.h" +#include "game_share/constants.h" +#include "game_share/visual_slot_manager.h" + +#ifdef LUA_NEVRAX_VERSION + #include "lua_ide_dll_nevrax/include/lua_ide_dll/ide_interface.h" // external debugger +#endif + + +#ifdef LUA_NEVRAX_VERSION + extern ILuaIDEInterface *LuaDebuggerIDE; +#endif + +using namespace NLMISC; +using namespace NLGUI; +using namespace R2; + +extern NLMISC::CLog g_log; +extern CContinentManager ContinentMngr; +extern CClientChatManager ChatMngr; + +// *************************************************************************** +class CHandlerLUA : public IActionHandler +{ +public: + void execute (CCtrlBase *pCaller, const std::string &sParams) + { + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + + // For getUI() LUA function, push the UI caller + if(pCaller) + _UICallerStack.push_back(pCaller); + + // execute a small script. NB: use a small script here because + // most often action handlers are called from xml files => lot of redundant script + pIM->executeLuaScript(sParams, true); + + // pop UI caller + if(pCaller) + _UICallerStack.pop_back(); + } + + // get the top of stack Caller to this LUA script + static CCtrlBase *getUICaller(); + +private: + static std::deque > _UICallerStack; +}; +REGISTER_ACTION_HANDLER( CHandlerLUA, "lua"); +std::deque > CHandlerLUA::_UICallerStack; + +CCtrlBase *CHandlerLUA::getUICaller() +{ + if(_UICallerStack.empty()) + return NULL; + else + return _UICallerStack.back(); +} + +#define LUABIND_ENUM(__enum__, __name__, __num__, __toStringFunc__) \ + createLuaEnumTable(ls, __name__); \ + for (uint e=0 ; e<__num__ ; e++) \ + { \ + std::string str = __toStringFunc__((__enum__)e); \ + std::string temp = __name__ + toString(".") + __toStringFunc__((__enum__)e) + " = " + toString("%d;", e); \ + ls.executeScript(temp); \ + } \ + + + +#define LUABIND_FUNC(__func__) luabind::def(#__func__, &__func__) + +// *************************************************************************** +void CLuaIHMRyzom::createLuaEnumTable(CLuaState &ls, const std::string &str) +{ + //H_AUTO(Lua_CLuaIHM_createLuaEnumTable) + std::string path = "", script, p; + CSString s = str; + // Create table recursively (ex: 'game.TPVPClan' will check/create the table 'game' and 'game.TPVPClan') + p = s.splitTo('.', true); + while (p.size() > 0) + { + if (path == "") + path = p; + else + path += "." + p; + script = "if (" + path + " == nil) then " + path + " = {}; end"; + ls.executeScript(script); + p = s.splitTo('.', true); + } +} + +void CLuaIHMRyzom::RegisterRyzomFunctions( NLGUI::CLuaState &ls ) +{ + CLuaStackChecker lsc( &ls ); + + ls.registerFunc( "getUI", getUI ); + ls.registerFunc("setOnDraw", setOnDraw); + ls.registerFunc("setCaptureKeyboard", setCaptureKeyboard); + ls.registerFunc("resetCaptureKeyboard", resetCaptureKeyboard); + ls.registerFunc("validMessageBox", validMessageBox); + ls.registerFunc("setTopWindow", setTopWindow); + ls.registerFunc("concatUCString", concatUCString); + ls.registerFunc("concatString", concatString); + ls.registerFunc("tableToString", tableToString); + ls.registerFunc("addOnDbChange", addOnDbChange); + ls.registerFunc("removeOnDbChange", removeOnDbChange); + ls.registerFunc("getUICaller", getUICaller); + ls.registerFunc("getCurrentWindowUnder", getCurrentWindowUnder); + ls.registerFunc("getUI", getUI); + ls.registerFunc("getIndexInDB", getIndexInDB); + ls.registerFunc("getUIId", getUIId); + ls.registerFunc("createGroupInstance", createGroupInstance); + ls.registerFunc("createRootGroupInstance", createRootGroupInstance); + ls.registerFunc("createUIElement", createUIElement); + ls.registerFunc("launchContextMenuInGame", launchContextMenuInGame); + ls.registerFunc("parseInterfaceFromString", parseInterfaceFromString); + ls.registerFunc("updateAllLocalisedElements", updateAllLocalisedElements); + ls.registerFunc("runAH", runAH); + ls.registerFunc("runExpr", runExpr); + ls.registerFunc("runFct", runFct); + ls.registerFunc("runCommand", runCommand); + ls.registerFunc("formatUI", formatUI); + ls.registerFunc("formatDB", formatDB); + ls.registerFunc("deleteUI", deleteUI); + ls.registerFunc("deleteReflectable", deleteReflectable); + ls.registerFunc("dumpUI", dumpUI); + ls.registerFunc("setKeyboardContext", setKeyboardContext); + ls.registerFunc("breakPoint", breakPoint); + ls.registerFunc("getWindowSize", getWindowSize); + ls.registerFunc("setTextFormatTaged", setTextFormatTaged); + ls.registerFunc("initEmotesMenu", initEmotesMenu); + ls.registerFunc("isUCString", isUCString); + ls.registerFunc("hideAllWindows", hideAllWindows); + ls.registerFunc("hideAllNonSavableWindows", hideAllNonSavableWindows); + ls.registerFunc("getDesktopIndex", getDesktopIndex); + ls.registerFunc("setLuaBreakPoint", setLuaBreakPoint); + ls.registerFunc("getMainPageURL", getMainPageURL); + ls.registerFunc("getCharSlot", getCharSlot); + ls.registerFunc("getPathContent", getPathContent); + ls.registerFunc("getServerSeason", getServerSeason); + ls.registerFunc("computeCurrSeason", computeCurrSeason); + ls.registerFunc("getAutoSeason", getAutoSeason); + ls.registerFunc("getTextureSize", getTextureSize); + ls.registerFunc("enableModalWindow", enableModalWindow); + ls.registerFunc("disableModalWindow", disableModalWindow); + ls.registerFunc("getPlayerPos", getPlayerPos); + ls.registerFunc("getPlayerFront", getPlayerFront); + ls.registerFunc("getPlayerDirection", getPlayerDirection); + ls.registerFunc("getPlayerGender", getPlayerGender); + ls.registerFunc("getPlayerName", getPlayerName); + ls.registerFunc("getPlayerTitleRaw", getPlayerTitleRaw); + ls.registerFunc("getPlayerTitle", getPlayerTitle); + ls.registerFunc("getTargetPos", getTargetPos); + ls.registerFunc("getTargetFront", getTargetFront); + ls.registerFunc("getTargetDirection", getTargetDirection); + ls.registerFunc("getTargetGender", getTargetGender); + ls.registerFunc("getTargetName", getTargetName); + ls.registerFunc("getTargetTitleRaw", getTargetTitleRaw); + ls.registerFunc("getTargetTitle", getTargetTitle); + ls.registerFunc("addSearchPathUser", addSearchPathUser); + ls.registerFunc("displaySystemInfo", displaySystemInfo); + ls.registerFunc("disableContextHelpForControl", disableContextHelpForControl); + ls.registerFunc("disableContextHelp", disableContextHelp); + ls.registerFunc("setWeatherValue", setWeatherValue); + ls.registerFunc("getWeatherValue", getWeatherValue); + ls.registerFunc("getCompleteIslands", getCompleteIslands); + ls.registerFunc("displayBubble", displayBubble); + ls.registerFunc("getIslandId", getIslandId); + ls.registerFunc("getClientCfgVar", getClientCfgVar); + ls.registerFunc("isPlayerFreeTrial", isPlayerFreeTrial); + ls.registerFunc("isPlayerNewbie", isPlayerNewbie); + ls.registerFunc("isInRingMode", isInRingMode); + ls.registerFunc("getUserRace", getUserRace); + ls.registerFunc("getSheet2idx", getSheet2idx); + ls.registerFunc("getTargetSlot", getTargetSlot); + ls.registerFunc("getSlotDataSetId", getSlotDataSetId); + + + lua_State *L= ls.getStatePointer(); + + LUABIND_ENUM(PVP_CLAN::TPVPClan, "game.TPVPClan", PVP_CLAN::NbClans, PVP_CLAN::toString); + LUABIND_ENUM(BONUS_MALUS::TBonusMalusSpecialTT, "game.TBonusMalusSpecialTT", BONUS_MALUS::NbSpecialTT, BONUS_MALUS::toString); + + luabind::module(L) + [ + LUABIND_FUNC(getDbProp), + LUABIND_FUNC(setDbProp), + LUABIND_FUNC(addDbProp), + LUABIND_FUNC(delDbProp), + LUABIND_FUNC(debugInfo), + LUABIND_FUNC(rawDebugInfo), + LUABIND_FUNC(dumpCallStack), + LUABIND_FUNC(getDefine), + LUABIND_FUNC(setContextHelpText), + luabind::def("messageBox", (void(*)(const ucstring &)) &messageBox), + luabind::def("messageBox", (void(*)(const ucstring &, const std::string &)) &messageBox), + luabind::def("messageBox", (void(*)(const ucstring &, const std::string &, int caseMode)) &messageBox), + luabind::def("messageBox", (void(*)(const std::string &)) &messageBox), + luabind::def("messageBoxWithHelp", (void(*)(const ucstring &)) &messageBoxWithHelp), + luabind::def("messageBoxWithHelp", (void(*)(const ucstring &, const std::string &)) &messageBoxWithHelp), + luabind::def("messageBoxWithHelp", (void(*)(const ucstring &, const std::string &, int caseMode)) &messageBoxWithHelp), + luabind::def("messageBoxWithHelp", (void(*)(const std::string &)) &messageBoxWithHelp), + LUABIND_FUNC(replacePvpEffectParam), + LUABIND_FUNC(secondsSince1970ToHour), + LUABIND_FUNC(pauseBGDownloader), + LUABIND_FUNC(unpauseBGDownloader), + LUABIND_FUNC(requestBGDownloaderPriority), + LUABIND_FUNC(getBGDownloaderPriority), + LUABIND_FUNC(getPatchLastErrorMessage), + LUABIND_FUNC(getPlayerSelectedSlot), + LUABIND_FUNC(isInGame), + LUABIND_FUNC(isPlayerSlotNewbieLand), + LUABIND_FUNC(getSkillIdFromName), + LUABIND_FUNC(getSkillLocalizedName), + LUABIND_FUNC(getMaxSkillValue), + LUABIND_FUNC(getBaseSkillValueMaxChildren), + LUABIND_FUNC(getMagicResistChance), + LUABIND_FUNC(getDodgeParryChance), + LUABIND_FUNC(browseNpcWebPage), + LUABIND_FUNC(clearHtmlUndoRedo), + LUABIND_FUNC(getDynString), + LUABIND_FUNC(isDynStringAvailable), + LUABIND_FUNC(isFullyPatched), + LUABIND_FUNC(getSheetType), + LUABIND_FUNC(getSheetName), + LUABIND_FUNC(getFameIndex), + LUABIND_FUNC(getFameName), + LUABIND_FUNC(getFameDBIndex), + LUABIND_FUNC(getFirstTribeFameIndex), + LUABIND_FUNC(getNbTribeFameIndex), + LUABIND_FUNC(getClientCfg), + LUABIND_FUNC(fileExists), + LUABIND_FUNC(sendMsgToServer), + LUABIND_FUNC(sendMsgToServerPvpTag), + LUABIND_FUNC(isGuildQuitAvailable), + LUABIND_FUNC(sortGuildMembers), + LUABIND_FUNC(getNbGuildMembers), + LUABIND_FUNC(getGuildMemberName), + LUABIND_FUNC(getGuildMemberGrade), + LUABIND_FUNC(isR2Player), + LUABIND_FUNC(getR2PlayerRace), + LUABIND_FUNC(isR2PlayerMale), + LUABIND_FUNC(getCharacterSheetSkel), + LUABIND_FUNC(getSheetId), + LUABIND_FUNC(getCharacterSheetRegionForce), + LUABIND_FUNC(getCharacterSheetRegionLevel), + LUABIND_FUNC(getRegionByAlias), + LUABIND_FUNC(tell), + LUABIND_FUNC(isRingAccessPointInReach), + LUABIND_FUNC(updateTooltipCoords), + LUABIND_FUNC(isCtrlKeyDown), + LUABIND_FUNC(encodeURLUnicodeParam), + LUABIND_FUNC(getPlayerLevel), + LUABIND_FUNC(getPlayerVpa), + LUABIND_FUNC(getPlayerVpb), + LUABIND_FUNC(getPlayerVpc), + LUABIND_FUNC(getTargetLevel), + LUABIND_FUNC(getTargetForceRegion), + LUABIND_FUNC(getTargetLevelForce), + LUABIND_FUNC(getTargetSheet), + LUABIND_FUNC(getTargetVpa), + LUABIND_FUNC(getTargetVpb), + LUABIND_FUNC(getTargetVpc), + LUABIND_FUNC(isTargetNPC), + LUABIND_FUNC(isTargetPlayer), + LUABIND_FUNC(isTargetUser), + LUABIND_FUNC(isPlayerInPVPMode), + LUABIND_FUNC(isTargetInPVPMode) + ]; + +} + +// *************************************************************************** +static sint32 getTargetSlotNr() +{ + const char *dbPath = "UI:VARIABLES:TARGET:SLOT"; + CInterfaceManager *im = CInterfaceManager::getInstance(); + CCDBNodeLeaf *node = im->getDbProp(dbPath, false); + if (!node) return 0; + if ((uint8) node->getValue32() == (uint8) CLFECOMMON::INVALID_SLOT) + { + return 0; + } + return node->getValue32(); +} + +static CEntityCL *getTargetEntity() +{ + const char *dbPath = "UI:VARIABLES:TARGET:SLOT"; + CInterfaceManager *im = CInterfaceManager::getInstance(); + CCDBNodeLeaf *node = im->getDbProp(dbPath, false); + if (!node) return NULL; + if ((uint8) node->getValue32() == (uint8) CLFECOMMON::INVALID_SLOT) + { + return NULL; + } + return EntitiesMngr.entity((uint) node->getValue32()); +} + + +static CEntityCL *getSlotEntity(uint slot) +{ + return EntitiesMngr.entity(slot); +} + + +int CLuaIHMRyzom::getUI(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getUI) + // params: "ui:interface:...". + // return: CInterfaceElement* (nil if error) + const char *funcName = "getUI"; + CLuaIHM::check(ls, ls.getTop() == 1 || ls.getTop() == 2, funcName); + CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); + bool verbose = true; + if (ls.getTop() > 1) + { + CLuaIHM::checkArgType(ls, funcName, 2, LUA_TBOOLEAN); + verbose = ls.toBoolean(2); + } + + // get the string + std::string eltStr; + ls.toString(1, eltStr); + + // return the element + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + CInterfaceElement *pIE= pIM->getElementFromId(eltStr); + if(!pIE) + { + ls.pushNil(); + if (verbose) + { + std::string stackContext; + ls.getStackContext(stackContext, 1); + debugInfo( NLMISC::toString("%s : getUI(): '%s' not found", stackContext.c_str(), eltStr.c_str())); + } + } + else + { + CLuaIHM::pushUIOnStack(ls, pIE); + } + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::setCaptureKeyboard(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_setCaptureKeyboard) + const char *funcName = "setCaptureKeyboard"; + CLuaIHM::checkArgCount(ls, funcName, 1); + CLuaIHM::checkArgTypeUIElement(ls, funcName, 1); + CCtrlBase *ctrl = dynamic_cast( CLuaIHM::getUIOnStack(ls, 1)); + if (!ctrl) + { + CLuaIHM::fails(ls, "%s waits a ui control as arg 1", funcName); + } + CInterfaceManager *im = CInterfaceManager::getInstance(); + im->setCaptureKeyboard(ctrl); + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::resetCaptureKeyboard(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_resetCaptureKeyboard) + const char *funcName = "resetCaptureKeyboard"; + CLuaIHM::checkArgCount(ls, funcName, 0); + CInterfaceManager *im = CInterfaceManager::getInstance(); + im->resetCaptureKeyboard(); + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::setOnDraw(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_setOnDraw) + CLuaStackChecker lsc(&ls, 0); + + // params: CInterfaceGroup*, "script". + // return: none + CLuaIHM::checkArgCount(ls, "setOnDraw", 2); + CLuaIHM::check(ls, CLuaIHM::isUIOnStack(ls, 1), "setOnDraw() requires a UI object in param 1"); + CLuaIHM::check(ls, ls.isString(2), "setOnDraw() requires a string in param 2"); + + // retrieve args + CInterfaceElement *pIE= CLuaIHM::getUIOnStack(ls, 1); + std::string script; + ls.toString(2, script); + + // must be a group + CInterfaceGroup *group= dynamic_cast(pIE); + if(!group) + throw ELuaIHMException("setOnDraw(): '%s' is not a group", pIE->getId().c_str()); + // Set the script to be executed at each draw + group->setLuaScriptOnDraw(script); + + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::addOnDbChange(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_addOnDbChange) + CLuaStackChecker lsc(&ls, 0); + + // params: CInterfaceGroup*, "dblist", "script". + // return: none + CLuaIHM::checkArgCount(ls, "addOnDbChange", 3); + CLuaIHM::check(ls, CLuaIHM::isUIOnStack(ls, 1), "addOnDbChange() requires a UI object in param 1"); + CLuaIHM::check(ls, ls.isString(2), "addOnDbChange() requires a string in param 2"); + CLuaIHM::check(ls, ls.isString(3), "addOnDbChange() requires a string in param 3"); + + // retrieve args + CInterfaceElement *pIE= CLuaIHM::getUIOnStack(ls, 1); + std::string dbList, script; + ls.toString(2, dbList); + ls.toString(3, script); + + // must be a group + CInterfaceGroup *group= dynamic_cast(pIE); + if(!group) + throw ELuaIHMException("addOnDbChange(): '%s' is not a group", pIE->getId().c_str()); + // Set the script to be executed when the given DB change + group->addLuaScriptOnDBChange(dbList, script); + + return 0; +} + + +// *************************************************************************** +int CLuaIHMRyzom::removeOnDbChange(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_removeOnDbChange) + CLuaStackChecker lsc(&ls, 0); + + // params: CInterfaceGroup*, "dbList" + // return: none + CLuaIHM::checkArgCount(ls, "removeOnDbChange", 2); + CLuaIHM::check(ls, CLuaIHM::isUIOnStack(ls, 1), "removeOnDbChange() requires a UI object in param 1"); + CLuaIHM::check(ls, ls.isString(2), "removeOnDbChange() requires a string in param 2"); + + // retrieve args + CInterfaceElement *pIE= CLuaIHM::getUIOnStack(ls, 1); + std::string dbList; + ls.toString(2, dbList); + + // must be a group + CInterfaceGroup *group= dynamic_cast(pIE); + if(!group) + throw ELuaIHMException("removeOnDbChange(): '%s' is not a group", pIE->getId().c_str()); + // Remove the script to be executed when the given DB change + group->removeLuaScriptOnDBChange(dbList); + + return 0; +} + + +// *************************************************************************** +int CLuaIHMRyzom::runAH(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_runAH) + CLuaStackChecker lsc(&ls, 0); + + // params: CInterfaceElement *, "ah", "params". + // return: none + CLuaIHM::checkArgCount(ls, "runAH", 3); + CLuaIHM::check(ls, CLuaIHM::isUIOnStack(ls, 1) || ls.isNil(1), "runAH() requires a UI object in param 1 (or Nil)"); + CLuaIHM::check(ls, ls.isString(2), "runAH() requires a string in param 2"); + CLuaIHM::check(ls, ls.isString(3), "runAH() requires a string in param 3"); + + // retrieve args + CInterfaceElement *pIE= CLuaIHM::getUIOnStack(ls, 1); + std::string ah, params; + ls.toString(2, ah); + ls.toString(3, params); + + // run AH + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + // The element must be ctrl (or NULL) + CCtrlBase *ctrl= NULL; + if(pIE) + { + ctrl= dynamic_cast(pIE); + if(!ctrl) + throw ELuaIHMException("runAH(): '%s' is not a ctrl", pIE->getId().c_str()); + } + pIM->runActionHandler(ah, ctrl, params); + + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::runExpr(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_runExpr) + CLuaStackChecker lsc(&ls, 1); + + // params: "expr". + // return: any of: nil, bool, string, number, RGBA, UCString + CLuaIHM::checkArgCount(ls, "runExpr", 1); + CLuaIHM::check(ls, ls.isString(1), "runExpr() requires a string in param 1"); + + // retrieve args + std::string expr; + ls.toString(1, expr); + + // run expression and push result + return CLuaIHM::runExprAndPushResult(ls, expr); +} + +// *************************************************************************** +int CLuaIHMRyzom::runFct(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_runFct) + CLuaStackChecker lsc(&ls, 1); + + // params: "expr", param1, param2... + // return: any of: nil, bool, string, number, RGBA, UCString + CLuaIHM::checkArgMin(ls, "runFct", 1); + CLuaIHM::check(ls, ls.isString(1), "runExpr() requires a string in param 1"); + + // retrieve fct + std::string expr; + ls.toString(1, expr); + expr+= "("; + + // retrieve params + uint top= ls.getTop(); + for(uint i=2;i<=top;i++) + { + if(i>2) + expr+= ", "; + + // If it is a number + if(ls.type(i)==LUA_TNUMBER) + { + std::string paramValue; + ls.toString(i, paramValue); // nb: transformed to a string in the stack + expr+= paramValue; + } + // else suppose a string + else + { + // must enclose with "'" + std::string paramValue; + ls.toString(i, paramValue); + expr+= std::string("'") + paramValue + std::string("'") ; + } + } + + // end fct call + expr+= ")"; + + + // run expression and push result + return CLuaIHM::runExprAndPushResult(ls, expr); +} + +// *************************************************************************** +int CLuaIHMRyzom::runCommand(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_runCommand) + CLuaStackChecker lsc(&ls, 1); + if (ls.empty()) + { + nlwarning("'runCommand' : Command name expected"); + ls.push(false); + return 1; + } + const char *commandName = ls.toString(1); + if (!commandName) + { + nlwarning("'runCommand' : Bad command name"); + ls.push(false); + return 1; + } + if (!NLMISC::ICommand::LocalCommands || !NLMISC::ICommand::LocalCommands->count(ls.toString(1))) + { + nlwarning("'runCommand' : Command %s not found", ls.toString(1)); + ls.push(false); + return 1; + } + std::string rawCommandString = ls.toString(1); + NLMISC::ICommand *command = (*NLMISC::ICommand::LocalCommands)[ls.toString(1)]; + nlassert(command); + std::vector args(ls.getTop() - 1); + for(uint k = 2; k <= (uint) ls.getTop(); ++k) + { + if (ls.toString(k)) + { + args[k - 2] = ls.toString(k); + rawCommandString += " " + std::string(ls.toString(k)); + } + } + + ls.push(command->execute(rawCommandString, args, g_log, false, true)); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::formatUI(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_formatUI) + CLuaStackChecker lsc(&ls, 1); + + // params: "expr", param1, param2.... + // return: string with # and % parsed + CLuaIHM::checkArgMin(ls, "formatUI", 1); + CLuaIHM::check(ls, ls.isString(1), "formatUI() require a string in param1"); + + // get the string to format + std::string propVal; + ls.toString(1, propVal); + + // *** format with % + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + std::string newPropVal, defError; + if(!pIM->solveDefine(propVal, newPropVal, defError)) + { + throw ELuaIHMException("formatUI(): Can't find define: '%s'", defError.c_str()); + } + + // *** format with any additional parameter and #1, #2, #3 etc... + // search backward, starting from bigger param to replace (thus avoid to replace #1 before #13 for instance...) + sint stackIndex= ls.getTop(); + while(stackIndex>1) + { + std::string paramValue; + ls.toString(stackIndex, paramValue); + + // For stack param 4, the param index is 3 (because stack param 2 is the param No 1) + sint paramIndex= stackIndex-1; + while(NLMISC::strFindReplace(newPropVal, NLMISC::toString("#%d", paramIndex), paramValue)); + + // next + stackIndex--; + } + + // return result + ls.push(newPropVal); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::formatDB(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_formatDB) + CLuaStackChecker lsc(&ls, 1); + + // params: param1, param2.... + // return: string with @ and , added + CLuaIHM::checkArgMin(ls, "formatDB", 1); + uint top= ls.getTop(); + + std::string dbRes; + for(uint i=1;i<=top;i++) + { + if(i==1) + dbRes= "@"; + else + dbRes+= ", @"; + + std::string paramValue; + ls.toString(i, paramValue); + dbRes+= paramValue; + } + + // return result + ls.push(dbRes); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::deleteUI(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_deleteUI) + CLuaStackChecker lsc(&ls, 0); + + // params: CInterfaceElement * + // return: none + CLuaIHM::checkArgCount(ls, "deleteUI", 1); + CLuaIHM::check(ls, CLuaIHM::isUIOnStack(ls, 1), "deleteUI() requires a UI object in param 1"); + + // retrieve args + CInterfaceElement *pIE= CLuaIHM::getUIOnStack(ls, 1); + if(!pIE) + return 0; + + // has a parent? + CInterfaceGroup *parent= pIE->getParent(); + if(parent) + { + // correctly remove from parent + parent->delElement(pIE); + } + else + { + // just delete + delete pIE; + } + + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::deleteReflectable(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_deleteReflectable) + CLuaStackChecker lsc(&ls, 0); + + // params: CInterfaceElement * + // return: none + CLuaIHM::checkArgCount(ls, "deleteReflectable", 1); + CLuaIHM::check(ls, CLuaIHM::isReflectableOnStack(ls, 1), "deleteReflectable() requires a reflectable C++ object in param 1"); + + // retrieve args + CReflectableRefPtrTarget *pRPT= CLuaIHM::getReflectableOnStack(ls, 1); + if(!pRPT) + return 0; + + + CInterfaceElement *pIE = dynamic_cast(pRPT); + + if (pIE) + { + // has a parent? + CInterfaceGroup *parent= pIE->getParent(); + if(parent) + { + // correctly remove from parent + parent->delElement(pIE); + } + } + + // just delete + delete pIE; + + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::dumpUI(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_dumpUI) + CLuaStackChecker lsc(&ls, 0); + + // params: CInterfaceElement * + // return: none + CLuaIHM::checkArgCount(ls, "dumpUI", 1); + CLuaIHM::check(ls, CLuaIHM::isUIOnStack(ls, 1), "dumpUI() requires a UI object in param 1"); + + // retrieve args + CInterfaceElement *pIE= CLuaIHM::getUIOnStack(ls, 1); + if(!pIE) + debugInfo("UI: NULL"); + else + { + // Display also Information on RefPtr (warning: don't modify pinfo!!!) + nlassert(pIE->pinfo); + debugInfo(NLMISC::toString("UI: %x. %s. RefPtrCount: %d", pIE, pIE->getId().c_str(), + pIE->pinfo->IsNullPtrInfo?0:pIE->pinfo->RefCount)); + } + + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::setKeyboardContext(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_setKeyboardContext) + const char *funcName = "setKeyboardContext"; + CLuaIHM::checkArgMin(ls, funcName, 1); + CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); + + ActionsContext.setContext(ls.toString(1)); + + return 0; +} + + +// *************************************************************************** +int CLuaIHMRyzom::validMessageBox(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_validMessageBox) + const char *funcName = "validMessageBox"; + CLuaIHM::checkArgCount(ls, funcName, 6); + ucstring msg; + ls.pushValue(1); // copy ucstring at the end of stack to pop it + CLuaIHM::check(ls, CLuaIHM::pop(ls, msg), "validMessageBox : ucstring wanted as first parameter"); + CLuaIHM::checkArgType(ls, funcName, 2, LUA_TSTRING); + CLuaIHM::checkArgType(ls, funcName, 3, LUA_TSTRING); + CLuaIHM::checkArgType(ls, funcName, 4, LUA_TSTRING); + CLuaIHM::checkArgType(ls, funcName, 5, LUA_TSTRING); + CLuaIHM::checkArgType(ls, funcName, 6, LUA_TSTRING); + CInterfaceManager *im = CInterfaceManager::getInstance(); + im->validMessageBox(CInterfaceManager::QuestionIconMsg, msg, ls.toString(2), ls.toString(3), ls.toString(4), ls.toString(5), ls.toString(6)); + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::setTopWindow(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_setTopWindow) + const char *funcName = "setTopWindow"; + CLuaIHM::checkArgCount(ls, funcName, 1); + CInterfaceGroup *wnd = dynamic_cast(CLuaIHM::getUIOnStack(ls, 1)); + if (!wnd) + { + CLuaIHM::fails(ls, "%s : interface group expected as arg 1", funcName); + } + CInterfaceManager *im = CInterfaceManager::getInstance(); + im->setTopWindow(wnd); + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::concatUCString(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_concatUCString) + const char *funcName = "concatUCString"; + ucstring result; + for (uint k = 1; k <= (uint) ls.getTop(); ++k) + { + //nlwarning("arg %d = %s", k, ls.getTypename(ls.type(k))); + ucstring part; + if (ls.isString(k)) + { + part.fromUtf8(ls.toString(k)); + } + else + { + CLuaIHM::checkArgTypeUCString(ls, funcName, k); + nlverify(CLuaIHM::getUCStringOnStack(ls, k, part)); + } + result += part; + } + CLuaIHM::push(ls, result); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::concatString(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_concatUCString) + const char *funcName = "concatString"; + std::string result; + uint stackSize = ls.getTop(); + for (uint k = 1; k <= stackSize; ++k) + { + CLuaIHM::checkArgType(ls, funcName, k, LUA_TSTRING); + result += ls.toString(k); + } + ls.push(result); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::tableToString(CLuaState &ls) +{ + const char *funcName = "tableToString"; + CLuaIHM::checkArgCount(ls, funcName, 1); + CLuaIHM::checkArgType(ls, funcName, 1, LUA_TTABLE); + uint length = 0; + // compute size + ls.pushNil(); + while (ls.next(-2)) + { + ls.toString(-1); + length += (uint)ls.strlen(-1); + ls.pop(2); + } + std::string result; + result.resize(length); + char *dest = &result[0]; + // concatenate + ls.pushNil(); + while (ls.next(-2)) + { + uint length = (uint)ls.strlen(-1); + if (length) + { + memcpy(dest, ls.toString(-1), length); + } + dest += length; + ls.pop(2); + } + ls.push(result); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::breakPoint(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_breakPoint) + std::string reason; + ls.getStackContext(reason, 1); // 1 because 0 is the current C function => return 1 for script called + LuaHelperStuff::formatLuaStackContext(reason); + NLMISC::InfoLog->displayRawNL(reason.c_str()); + static volatile bool doAssert = true; + if (doAssert) // breakPoint can be discarded in case of looping assert + { + NLMISC_BREAKPOINT; + } + return 0; +} + + + +// *************************************************************************** +int CLuaIHMRyzom::getWindowSize(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getWindowSize) + CLuaIHM::checkArgCount(ls, "getWindowSize", 0); + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + uint32 w, h; + pIM->getViewRenderer().getScreenSize(w, h); + ls.push((double) w); + ls.push((double) h); + return 2; +} + + +// *************************************************************************** +int CLuaIHMRyzom::setTextFormatTaged(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_setTextFormatTaged) + // params: CViewText*, "text" (or ucstring) + // return: none + CLuaIHM::checkArgCount(ls, "setTextFormatTaged", 2); + + // *** check and retrieve param 1 + CLuaIHM::check(ls, CLuaIHM::isUIOnStack(ls, 1), "setTextFormatTaged() requires a UI object in param 1"); + CInterfaceElement *pIE= CLuaIHM::getUIOnStack(ls, 1); + + // *** check and retrieve param 2. must be a string or a ucstring + ucstring text; + if(ls.isString(2)) + { + std::string str; + ls.toString(2, str); + text= str; + } + else + { + // try to pop a ucstring from the stack + // fail? + if(!CLuaIHM::pop(ls, text)) + { + CLuaIHM::check(ls, false, "setTextFormatTaged() requires a string or a ucstring in param 2"); + } + } + + // must be a view text + CViewText *vt= dynamic_cast(pIE); + if(!vt) + throw ELuaIHMException("setTextFormatTaged(): '%s' is not a CViewText", pIE->getId().c_str()); + + // Set the text as format + vt->setTextFormatTaged(text); + + return 0; +} + + +struct CEmoteStruct +{ + string EmoteId; + string Path; + string Anim; + bool UsableFromClientUI; + + bool operator< (const CEmoteStruct & entry) const + { + string path1 = Path; + string path2 = entry.Path; + + for(;;) + { + string::size_type pos1 = path1.find('|'); + string::size_type pos2 = path2.find('|'); + + ucstring s1 = toUpper(CI18N::get(path1.substr(0, pos1))); + ucstring s2 = toUpper(CI18N::get(path2.substr(0, pos2))); + + sint result = s1.compare(s2); + if (result != 0) + return (result < 0); + + if (pos1 == string::npos) + return (pos2 != string::npos); + if (pos2 == string::npos) + return false; + + path1 = path1.substr(pos1 + 1); + path2 = path2.substr(pos2 + 1); + } + return false; + } +}; + +// *************************************************************************** +int CLuaIHMRyzom::initEmotesMenu(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_initEmotesMenu) + CLuaIHM::checkArgCount(ls, "initEmotesMenu", 2); + CLuaIHM::checkArgType(ls, "initEmotesMenu", 2, LUA_TSTRING); + const std::string & emoteMenu = ls.toString(1); + const std::string & luaParams = ls.toString(2); + + ls.newTable(); + CLuaObject result(ls); + std::map emoteList; + uint maxVisibleLine=10; + + CTextEmotListSheet *pTELS = dynamic_cast(SheetMngr.get(CSheetId("list.text_emotes"))); + if (pTELS == NULL) + return 0; + + std::list entries; + if (entries.empty()) + { + for (uint i = 0; i < pTELS->TextEmotList.size(); i++) + { + CEmoteStruct entry; + entry.EmoteId = pTELS->TextEmotList[i].EmoteId; + entry.Path = pTELS->TextEmotList[i].Path; + entry.Anim = pTELS->TextEmotList[i].Anim; + entry.UsableFromClientUI = pTELS->TextEmotList[i].UsableFromClientUI; + entries.push_back(entry); + } + entries.sort(); + } + + // The list of behaviour missnames emotList + CEmotListSheet *pEmotList = dynamic_cast(SheetMngr.get(CSheetId("list.emot"))); + nlassert (pEmotList != NULL); + nlassert (pEmotList->Emots.size() <= 255); + + // Get the focus beta tester flag + bool betaTester = false; + + CInterfaceManager *pIM = CInterfaceManager::getInstance(); + CSkillManager *pSM = CSkillManager::getInstance(); + + betaTester = pSM->isTitleUnblocked(CHARACTER_TITLE::FBT); + + CGroupMenu *pInitRootMenu = dynamic_cast(pIM->getElementFromId(emoteMenu)); + pInitRootMenu->reset(); + + for (std::list::const_iterator it = entries.begin(); it != entries.end(); it++) + { + std::string sEmoteId = (*it).EmoteId; + std::string sState = (*it).Anim; + std::string sName = (*it).Path; + + // Check that the emote can be added to UI + // --------------------------------------- + if( (*it).UsableFromClientUI == false ) + { + continue; + } + + // Check the emote reserved for FBT (hardcoded) + // -------------------------------------------- + if (sState == "FBT" && !betaTester) + continue; + + uint32 i, j; + // Add to the game context menu + // ---------------------------- + uint32 nbToken = 1; + for (i = 0; i < sName.size(); ++i) + if (sName[i] == '|') + nbToken++; + + CGroupMenu *pRootMenu = dynamic_cast(pIM->getElementFromId(emoteMenu)); + CGroupSubMenu *pMenu = pRootMenu->getRootMenu(); + + for (i = 0; i < nbToken; ++i) + { + if(i==0) + { + sName = sName.substr(sName.find('|')+1,sName.size()); + } + else + { + string sTmp; + if (i != (nbToken-1)) + sTmp = sName.substr(0,sName.find('|')); + else + sTmp = sName; + + + + // Look if this part of the path is already present + bool bFound = false; + for (j = 0; j < pMenu->getNumLine(); ++j) + { + if (sTmp == pMenu->getLineId(j)) + { + bFound = true; + break; + } + } + + if (!bFound) // Create it + { + if (i != (nbToken-1)) + { + pMenu->addLine (CI18N::get(sTmp), "", "", sTmp); + // Create a sub menu + CGroupSubMenu *pNewSubMenu = new CGroupSubMenu(CViewBase::TCtorParam()); + pMenu->setSubMenu(j, pNewSubMenu); + } + else + { + // Create a line + pMenu->addLine (CI18N::get(sTmp), "lua", + luaParams+"('"+sEmoteId+"', '"+toString(CI18N::get(sTmp))+"')", sTmp); + emoteList[sEmoteId] = (toLower(CI18N::get(sTmp))).toUtf8(); + } + } + + // Jump to sub menu + if (i != (nbToken-1)) + { + pMenu = pMenu->getSubMenu(j); + sName = sName.substr(sName.find('|')+1,sName.size()); + } + } + } + pMenu->setMaxVisibleLine(maxVisibleLine); + } + pInitRootMenu->setMaxVisibleLine(maxVisibleLine); + + std::map::iterator it; + for(it=emoteList.begin(); it!=emoteList.end(); it++) + { + result.setValue(it->first, it->second); + } + result.push(); + + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::isUCString(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_isUCString) + const char *funcName = "isUCString"; + CLuaIHM::checkArgCount(ls, funcName, 1); + ls.push(CLuaIHM::isUCStringOnStack(ls, 1)); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::hideAllWindows(CLuaState &/* ls */) +{ + //H_AUTO(Lua_CLuaIHM_hideAllWindows) + CInterfaceManager::getInstance()->hideAllWindows(); + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::hideAllNonSavableWindows(CLuaState &/* ls */) +{ + //H_AUTO(Lua_CLuaIHM_hideAllNonSavableWindows) + CInterfaceManager::getInstance()->hideAllNonSavableWindows(); + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::getDesktopIndex(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getDesktopIndex) + ls.push((double) CInterfaceManager::getInstance()->getMode()); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::setLuaBreakPoint(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_setLuaBreakPoint) + const char *funcName = "setLuaBreakPoint"; + CLuaIHM::checkArgCount(ls, funcName, 2); + CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); + CLuaIHM::checkArgType(ls, funcName, 2, LUA_TNUMBER); + + + #ifdef LUA_NEVRAX_VERSION + if (LuaDebuggerIDE) + { + LuaDebuggerIDE->setBreakPoint(ls.toString(1), (int) ls.toNumber(2)); + } + #endif + + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::getMainPageURL(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getMainPageURL) + const char *funcName = "getMainPageURL"; + CLuaIHM::checkArgCount(ls, funcName, 0); + ls.push(RingMainURL); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getCharSlot(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getCharSlot) + const char *funcName = "getCharSlot"; + CLuaIHM::checkArgCount(ls, funcName, 0); + ls.push(double(PlayerSelectedSlot)); + return 1; +} + + +int CLuaIHMRyzom::getPathContent(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getPathContent) + const char *funcName = "getPathContent"; + CLuaIHM::checkArgCount(ls, funcName, 1); + CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); + std::vector files; + NLMISC::CPath::getPathContent(ls.toString(1), false, false, true, files); + ls.newTable(); + for(uint k = 0; k < files.size(); ++k) + { + ls.push((double) k); + ls.push(files[k]); + ls.setTable(-3); + } + return 1; +} + +int CLuaIHMRyzom::getServerSeason(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getServerSeason) + const char *funcName = "getServerSeason"; + CLuaIHM::checkArgCount(ls, funcName, 0); + extern uint8 ServerSeasonValue; + ls.push((double) ServerSeasonValue); + return 1; +} + +int CLuaIHMRyzom::computeCurrSeason(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_computeCurrSeason) + const char *funcName = "computeCurrSeason"; + CLuaIHM::checkArgCount(ls, funcName, 0); + ls.push((double) (::computeCurrSeason() + 1)); + return 1; +} + +int CLuaIHMRyzom::getAutoSeason(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getAutoSeason) + const char *funcName = "getAutoSeason"; + CLuaIHM::checkArgCount(ls, funcName, 0); + ls.push((double) (StartupSeason + 1)); + return 1; +} + + + +int CLuaIHMRyzom::getTextureSize(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getTextureSize) + const char *funcName = "getTextureSize"; + CLuaIHM::checkArgCount(ls, funcName, 1); + CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); + std::string textureName = ls.toString(1); + + CBitmap bitmap; + CIFile fs(CPath::lookup(textureName).c_str()); + bitmap.load(fs); + + ls.push((double) bitmap.getWidth()); + ls.push((double) bitmap.getHeight()); + + return 2; +} + + +int CLuaIHMRyzom::enableModalWindow(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_enableModalWindow) + const char *funcName = "enableModalWindow"; + CLuaIHM::checkArgCount(ls, funcName, 2); + + CLuaIHM::check(ls, CLuaIHM::isUIOnStack(ls, 1), "enableModalWindow() requires a UI object in param 1"); + CLuaIHM::checkArgType(ls, funcName, 2, LUA_TSTRING); + + CInterfaceElement *pIE= CLuaIHM::getUIOnStack(ls, 1); + std::string modalId = ls.toString(2); + + // convert to id + if(pIE) + { + CCtrlBase * ctrl = dynamic_cast(pIE); + if(ctrl) + { + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + CInterfaceGroup *group= dynamic_cast( pIM->getElementFromId(modalId) ); + if(group) + { + UserControls.stopFreeLook(); + + // enable the modal + pIM->enableModalWindow(ctrl, group); + } + else + { + nlwarning(" Couldn't find group %s", modalId.c_str()); + } + + } + } + + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::disableModalWindow(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_disableModalWindow) + CLuaIHM::checkArgCount(ls, "disableModalWindow", 0); + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + pIM->disableModalWindow(); + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::getPlayerPos(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getPlayerPos) + CLuaIHM::checkArgCount(ls, "getPlayerPos", 0); + ls.push(UserEntity->pos().x); + ls.push(UserEntity->pos().y); + ls.push(UserEntity->pos().z); + return 3; +} + +// *************************************************************************** +int CLuaIHMRyzom::getPlayerFront(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "getPlayerFront", 0); + ls.push(atan2(UserEntity->front().y, UserEntity->front().x)); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getPlayerDirection(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "getPlayerDirection", 0); + ls.push(atan2(UserEntity->dir().y, UserEntity->dir().x)); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getPlayerGender(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "getPlayerGender", 0); + ls.push((lua_Number)(UserEntity->getGender())); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getPlayerName(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "getPlayerName", 0); + ls.push(UserEntity->getEntityName().toUtf8()); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getPlayerTitleRaw(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "getPlayerTitleRaw", 0); + ls.push(UserEntity->getTitleRaw().toUtf8()); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getPlayerTitle(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "getPlayerTitle", 0); + ls.push(UserEntity->getTitle().toUtf8()); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getTargetPos(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "getTargetPos", 0); + CEntityCL *target = getTargetEntity(); + if (!target) return 0; + ls.push(target->pos().x); + ls.push(target->pos().y); + ls.push(target->pos().z); + return 3; +} + +// *************************************************************************** +int CLuaIHMRyzom::getTargetFront(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "getTargetFront", 0); + CEntityCL *target = getTargetEntity(); + if (!target) return 0; + ls.push(atan2(target->front().y, target->front().x)); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getTargetDirection(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "getTargetDirection", 0); + CEntityCL *target = getTargetEntity(); + if (!target) return 0; + ls.push(atan2(target->dir().y, target->dir().x)); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getTargetGender(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "getTargetGender", 0); + CCharacterCL* target = (CCharacterCL*)getTargetEntity(); + if (!target) return (int)GSGENDER::unknown; + ls.push((lua_Number)(target->getGender())); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getTargetName(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "getTargetName", 0); + CEntityCL *target = getTargetEntity(); + if (!target) return 0; + ls.push(target->getEntityName().toUtf8()); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getTargetTitleRaw(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "getTargetTitleRaw", 0); + CEntityCL *target = getTargetEntity(); + if (!target) return 0; + ls.push(target->getTitleRaw().toUtf8()); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getTargetTitle(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "getTargetTitle", 0); + CEntityCL *target = getTargetEntity(); + if (!target) return 0; + ls.push(target->getTitle().toUtf8()); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::addSearchPathUser(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_addSearchPathUser) + bool memoryCompressed = CPath::isMemoryCompressed(); + if (memoryCompressed) + { + CPath::memoryUncompress(); + } + CPath::addSearchPath("user/", true, false, NULL); + if (memoryCompressed) + { + CPath::memoryCompress(); + } + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::isPlayerFreeTrial(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "isPlayerFreeTrial", 0); + ls.push(FreeTrial); + return 1; +} + + + +// *************************************************************************** +int CLuaIHMRyzom::disableContextHelp(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_disableContextHelp) + CLuaStackChecker lsc(&ls, 0); + CLuaIHM::checkArgCount(ls, "disableContextHelp", 0); + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + pIM->disableContextHelp(); + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::disableContextHelpForControl(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_disableContextHelpForControl) + CLuaStackChecker lsc(&ls, 0); + + // params: CCtrlBase* + // return: none + CLuaIHM::checkArgCount(ls, "disableContextHelpForControl", 1); + CLuaIHM::check(ls, CLuaIHM::isUIOnStack(ls, 1), "disableContextHelpForControl() requires a UI object in param 1"); + + // retrieve args + CInterfaceElement *pIE= CLuaIHM::getUIOnStack(ls, 1); + + // go + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + pIM->disableContextHelpForControl(dynamic_cast(pIE)); + + return 0; +} + + +// *************************************************************************** +int CLuaIHMRyzom::isPlayerNewbie(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "isPlayerNewbie", 0); + CInterfaceManager *im = CInterfaceManager::getInstance(); + ls.push(im->getDbProp("SERVER:USER:IS_NEWBIE")->getValueBool()); + return 1; +} + + +// *************************************************************************** +int CLuaIHMRyzom::isInRingMode(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "isInRingMode", 0); + extern bool IsInRingMode(); + ls.push(IsInRingMode()); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getUserRace(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "getUserRace", 0); + if (!UserEntity || !UserEntity->playerSheet()) + { + ls.push("Unknwown"); + } + else + { + ls.push(EGSPD::CPeople::toString(UserEntity->playerSheet()->People)); + } + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getSheet2idx(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "getSheet2idx", 2); + CLuaIHM::checkArgType(ls, "getSheet2idx", 1, LUA_TSTRING); + CLuaIHM::checkArgType(ls, "getSheet2idx", 2, LUA_TNUMBER); + + const std::string & sheedtName = ls.toString(1); + uint32 slotId = (uint32)ls.toNumber(2); + + NLMISC::CSheetId sheetId; + + if (sheetId.buildSheetId(sheedtName)) + { + uint32 idx = CVisualSlotManager::getInstance()->sheet2Index(sheetId, (SLOTTYPE::EVisualSlot)slotId); + ls.push((lua_Number)idx); + } + else + return 0; + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getTargetSlot(CLuaState &ls) +{ + uint32 slot = (uint32)getTargetSlotNr(); + ls.push((lua_Number)slot); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getSlotDataSetId(CLuaState &ls) +{ + CLuaIHM::checkArgCount(ls, "getSlotDataSetId", 1); + CLuaIHM::checkArgType(ls, "getSlotDataSetId", 1, LUA_TNUMBER); + + uint32 slot = (uint32)ls.toNumber(1); + CEntityCL *e = getSlotEntity(slot); + string id = toString(e->dataSetId()); + ls.push(id); + return 1; +} + +int CLuaIHMRyzom::getClientCfgVar(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getClientCfgVar) + const char *funcName = "getClientCfgVar"; + CLuaIHM::checkArgCount(ls, funcName, 1); + CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); + std::string varName = ls.toString(1); + + CConfigFile::CVar *v = ClientCfg.ConfigFile.getVarPtr(varName); + if (!v) return 0; + if(v->size()==1) + { + switch(v->Type) + { + case CConfigFile::CVar::T_REAL: + ls.push((double) v->asDouble()); + return 1; + break; + case CConfigFile::CVar::T_STRING: + ls.push(v->asString()); + return 1; + break; + default: // handle both T_INT && T_BOOL + case CConfigFile::CVar::T_INT: + ls.push((double) v->asInt()); + return 1; + break; + } + } + else + { + ls.newTable(); + CLuaObject result(ls); + uint count = 0; + for(uint i = 0; iStrValues.size(); i++) + { + result.setValue(toString(count).c_str(), v->StrValues[i]); + count++; + } + for(uint i = 0; iIntValues.size(); i++) + { + result.setValue(toString(count).c_str(), (double)v->IntValues[i]); + count++; + } + for(uint i = 0; iRealValues.size(); i++) + { + result.setValue(toString(count).c_str(), (double)v->RealValues[i]); + count++; + } + result.push(); + return 1; + } + + return 0; +} + +int CLuaIHMRyzom::displaySystemInfo(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_displaySystemInfo) + const char *funcName = "displaySystemInfo"; + CLuaIHM::checkArgCount(ls, funcName, 2); + CLuaIHM::checkArgTypeUCString(ls, funcName, 1); + CLuaIHM::checkArgType(ls, funcName, 2, LUA_TSTRING); + ucstring msg; + nlverify(CLuaIHM::getUCStringOnStack(ls, 1, msg)); + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + pIM->displaySystemInfo(msg, ls.toString(2)); + return 0; +} + +int CLuaIHMRyzom::setWeatherValue(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_setWeatherValue) + const char *funcName = "setWeatherValue"; + CLuaIHM::checkArgMin(ls, funcName, 1); + CLuaIHM::checkArgMax(ls, funcName, 2); + CLuaIHM::checkArgType(ls, funcName, 1, LUA_TBOOLEAN); +// bool autoWeather = ls.toBoolean(1); + ClientCfg.ManualWeatherSetup = !ls.toBoolean(1); + if (ls.getTop() == 2) + { + CLuaIHM::checkArgType(ls, funcName, 2, LUA_TNUMBER); + ManualWeatherValue = (float) ls.toNumber(2); + } + return 0; +} + +int CLuaIHMRyzom::getWeatherValue(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getWeatherValue) + const char *funcName = "getWeatherValue"; + CLuaIHM::checkArgCount(ls, funcName, 0); + uint64 currDay = RT.getRyzomDay(); + float currHour = (float) RT.getRyzomTime(); + ls.push(::getBlendedWeather(currDay, currHour, *WeatherFunctionParams, ContinentMngr.cur()->WeatherFunction)); + return 1; +} + +int CLuaIHMRyzom::getUICaller(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getUICaller) + CLuaStackChecker lsc(&ls, 1); + + // params: none. + // return: CInterfaceElement* (nil if error) + CInterfaceElement *pIE= CHandlerLUA::getUICaller(); + if(!pIE) + { + ls.pushNil(); + debugInfo(toString("getUICaller(): No UICaller found. return Nil")); + } + else + { + CLuaIHM::pushUIOnStack(ls, pIE); + } + return 1; +} + +int CLuaIHMRyzom::getCurrentWindowUnder(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getCurrentWindowUnder) + CLuaStackChecker lsc(&ls, 1); + CInterfaceManager *im = CInterfaceManager::getInstance(); + CInterfaceElement *pIE= im->getCurrentWindowUnder(); + if(!pIE) + { + ls.pushNil(); + debugInfo(toString("getCurrentWindowUnder(): No UICaller found. return Nil")); + } + else + { + CLuaIHM::pushUIOnStack(ls, pIE); + } + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getUIId(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getUIId) + CLuaStackChecker lsc(&ls, 1); + + // params: CInterfaceElement* + // return: "ui:interface:...". (empty if error) + CLuaIHM::checkArgCount(ls, "getUIId", 1); + CLuaIHM::check(ls, CLuaIHM::isUIOnStack(ls, 1), "getUIId() requires a UI object in param 1"); + + // retrieve args + CInterfaceElement *pIE= CLuaIHM::getUIOnStack(ls, 1); + + // convert to id + if(pIE) + ls.push(pIE->getId()); + else + ls.push(""); + + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getIndexInDB(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getIndexInDB) + CLuaStackChecker lsc(&ls, 1); + + // params: CDBCtrlSheet* + // return: index in DB of a dbctrlsheet (empty if error) + CLuaIHM::checkArgCount(ls, "getIndexInDB", 1); + CLuaIHM::check(ls, CLuaIHM::isUIOnStack(ls, 1), "getIndexInDB() requires a UI object in param 1"); + + // retrieve args + CInterfaceElement *pIE= CLuaIHM::getUIOnStack(ls, 1); + CDBCtrlSheet *pCS= dynamic_cast(pIE); + + // get the index in db + if(pCS) + ls.push((double)pCS->getIndexInDB()); + else + ls.push(0.0); + + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::createGroupInstance(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_createGroupInstance) + const char *funcName = "createGroupInstance"; + CLuaIHM::checkArgCount(ls, funcName, 3); + CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); + CLuaIHM::checkArgType(ls, funcName, 2, LUA_TSTRING); + CLuaIHM::checkArgType(ls, funcName, 3, LUA_TTABLE); + std::vector > templateParams; + CLuaObject params; + params.pop(ls); + ENUM_LUA_TABLE(params, it) + { + if (!it.nextKey().isString()) + { + nlwarning("%s : bad key encountered with type %s, string expected.", funcName, it.nextKey().getTypename()); + continue; + } + if (!it.nextValue().isString()) + { + nlwarning("%s : bad value encountered with type %s for key %s, string expected.", funcName, it.nextValue().getTypename(), it.nextKey().toString().c_str()); + continue; + } + templateParams.push_back(std::pair(it.nextKey().toString(), it.nextValue().toString())); // strange compilation bug here when I use std::make_pair ... :( + } + CInterfaceManager *im = CInterfaceManager::getInstance(); + CInterfaceGroup *result = im->createGroupInstance(ls.toString(1), ls.toString(2), templateParams); + if (!result) + { + ls.pushNil(); + } + else + { + CLuaIHM::pushUIOnStack(ls, result); + } + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::createRootGroupInstance(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_createGroupInstance) + const char *funcName = "createRootGroupInstance"; + CLuaIHM::checkArgCount(ls, funcName, 3); + CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); + CLuaIHM::checkArgType(ls, funcName, 2, LUA_TSTRING); + CLuaIHM::checkArgType(ls, funcName, 3, LUA_TTABLE); + std::vector > templateParams; + CLuaObject params; + params.pop(ls); + ENUM_LUA_TABLE(params, it) + { + if (!it.nextKey().isString()) + { + nlwarning("%s : bad key encountered with type %s, string expected.", funcName, it.nextKey().getTypename()); + continue; + } + if (!it.nextValue().isString()) + { + nlwarning("%s : bad value encountered with type %s for key %s, string expected.", funcName, it.nextValue().getTypename(), it.nextKey().toString().c_str()); + continue; + } + templateParams.push_back(std::pair(it.nextKey().toString(), it.nextValue().toString())); // strange compilation bug here when I use std::make_pair ... :( + } + CInterfaceManager *im = CInterfaceManager::getInstance(); + CInterfaceGroup *result = im->createGroupInstance(ls.toString(1), "ui:interface:"+string(ls.toString(2)), templateParams); + if (!result) + { + ls.pushNil(); + } + else + { + result->setId("ui:interface:"+string(ls.toString(2))); + result->updateCoords(); + im->addWindowToMasterGroup("ui:interface", result); + CInterfaceGroup *pRoot = dynamic_cast(im->getElementFromId("ui:interface")); + result->setParent(pRoot); + if (pRoot) + pRoot->addGroup(result); + result->setActive(true); + CLuaIHM::pushUIOnStack(ls, result); + } + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::createUIElement(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_createUIElement) + const char *funcName = "addUIElement"; + CLuaIHM::checkArgCount(ls, funcName, 3); + CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); + CLuaIHM::checkArgType(ls, funcName, 2, LUA_TSTRING); + CLuaIHM::checkArgType(ls, funcName, 3, LUA_TTABLE); + std::vector > templateParams; + CLuaObject params; + params.pop(ls); + ENUM_LUA_TABLE(params, it) + { + if (!it.nextKey().isString()) + { + nlwarning("%s : bad key encountered with type %s, string expected.", funcName, it.nextKey().getTypename()); + continue; + } + if (!it.nextValue().isString()) + { + nlwarning("%s : bad value encountered with type %s for key %s, string expected.", funcName, it.nextValue().getTypename(), it.nextKey().toString().c_str()); + continue; + } + templateParams.push_back(std::pair(it.nextKey().toString(), it.nextValue().toString())); // strange compilation bug here when I use std::make_pair ... :( + } + CInterfaceManager *im = CInterfaceManager::getInstance(); + CInterfaceElement *result = im->createUIElement(ls.toString(1), ls.toString(2), templateParams); + if (!result) + { + ls.pushNil(); + } + else + { + CLuaIHM::pushUIOnStack(ls, result); + } + return 1; +} + + +// *************************************************************************** +int CLuaIHMRyzom::displayBubble(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_createUIElement) + const char *funcName = "displayBubble"; + CLuaIHM::checkArgCount(ls, funcName, 3); + CLuaIHM::checkArgType(ls, funcName, 1, LUA_TNUMBER); + CLuaIHM::checkArgType(ls, funcName, 2, LUA_TSTRING); + CLuaIHM::checkArgType(ls, funcName, 3, LUA_TTABLE); + std::vector strs; + std::vector links; + CLuaObject params; + params.pop(ls); + ENUM_LUA_TABLE(params, it) + { + if (!it.nextKey().isString()) + { + nlwarning("%s : bad key encountered with type %s, string expected.", funcName, it.nextKey().getTypename()); + continue; + } + if (!it.nextValue().isString()) + { + nlwarning("%s : bad value encountered with type %s for key %s, string expected.", funcName, it.nextValue().getTypename(), it.nextKey().toString().c_str()); + continue; + } + links.push_back(it.nextValue().toString()); + strs.push_back(it.nextKey().toString()); + } + + InSceneBubbleManager.webIgChatOpen((uint32)ls.toNumber(1), ls.toString(2), strs, links); + + return 1; +} + +int CLuaIHMRyzom::launchContextMenuInGame(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_launchContextMenuInGame) + CLuaStackChecker lsc(&ls); + CLuaIHM::checkArgCount(ls, "launchContextMenuInGame", 1); + CLuaIHM::check(ls, ls.isString(1), "launchContextMenuInGame() requires a string in param 1"); + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + pIM->launchContextMenuInGame(ls.toString(1)); + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::parseInterfaceFromString(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_parseInterfaceFromString) + CLuaStackChecker lsc(&ls, 1); + CLuaIHM::checkArgCount(ls, "parseInterfaceFromString", 1); + CLuaIHM::check(ls, ls.isString(1), "parseInterfaceFromString() requires a string in param 1"); + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + std::vector script(1); + script[0] = ls.toString(1); + ls.push(pIM->parseInterface(script, true, false)); + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::updateAllLocalisedElements(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_updateAllLocalisedElements) + TTime startTime = CTime::getLocalTime(); + // + CLuaStackChecker lsc(&ls); + CLuaIHM::checkArgCount(ls, "updateAllLocalisedElements", 0); + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + pIM->updateAllLocalisedElements(); + // + TTime endTime = CTime::getLocalTime(); + if (ClientCfg.R2EDVerboseParseTime) + { + nlinfo("%.2f seconds for 'updateAllLocalisedElements'", (endTime - startTime) / 1000.f); + } + return 0; +} + +// *************************************************************************** +int CLuaIHMRyzom::getCompleteIslands(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getCompleteIslands) + const char *funcName = "getCompleteIslands"; + CLuaIHM::checkArgCount(ls, funcName, 0); + + ls.newTable(); + CLuaObject result(ls); + + // load entryPoints + CScenarioEntryPoints scenarioEntryPoints = CScenarioEntryPoints::getInstance(); + const CScenarioEntryPoints::TCompleteIslands& islands = scenarioEntryPoints.getCompleteIslands(); + + CScenarioEntryPoints::TCompleteIslands::const_iterator island(islands.begin()), lastIsland(islands.end()); + for( ; island != lastIsland ; ++island) + { + ls.newTable(); + CLuaObject islandTable(ls); + islandTable.setValue("continent", island->Continent); + islandTable.setValue("xmin", (double)island->XMin); + islandTable.setValue("ymin", (double)island->YMin); + islandTable.setValue("xmax", (double)island->XMax); + islandTable.setValue("ymax", (double)island->YMax); + + ls.newTable(); + CLuaObject entrypointsTable(ls); + + for(uint e=0; eEntryPoints.size(); e++) + { + const CScenarioEntryPoints::CShortEntryPoint & entryPoint = island->EntryPoints[e]; + ls.newTable(); + CLuaObject entrypointTable(ls); + entrypointTable.setValue("x", (double)entryPoint.X); + entrypointTable.setValue("y", (double)entryPoint.Y); + + entrypointsTable.setValue(entryPoint.Location, entrypointTable); + } + islandTable.setValue("entrypoints", entrypointsTable); + + result.setValue(island->Island, islandTable); + } + + result.push(); + + return 1; +} + +// *************************************************************************** +int CLuaIHMRyzom::getIslandId(CLuaState &ls) +{ + //H_AUTO(Lua_CLuaIHM_getIslandId) + const char *funcName = "getIslandId"; + CLuaIHM::checkArgCount(ls, funcName, 1); + CLuaIHM::check(ls, ls.isString(1), "getIslandId() requires a string in param 1"); + + + CScenarioEntryPoints scenarioEntryPoints = CScenarioEntryPoints::getInstance(); + uint32 id = scenarioEntryPoints.getIslandId(ls.toString(1)); + ls.push((double)id); + + return 1; +} + +////////////////////////////////////////// Standard Lua stuff ends here ////////////////////////////////////// + +// *************************************************************************** +sint32 CLuaIHMRyzom::getDbProp(const std::string &dbProp) +{ + //H_AUTO(Lua_CLuaIHM_getDbProp) + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + CCDBNodeLeaf *node= pIM->getDbProp(dbProp, false); + if(node) + return node->getValue32(); + else + { + debugInfo(toString("getDbProp(): '%s' dbProp Not found", dbProp.c_str())); + return 0; + } +} + +void CLuaIHMRyzom::setDbProp(const std::string &dbProp, sint32 value) +{ + //H_AUTO(Lua_CLuaIHM_setDbProp) + // Do not allow Write on SERVER: or LOCAL: + static const std::string dbServer= "SERVER:"; + static const std::string dbLocal= "LOCAL:"; + static const std::string dbLocalR2= "LOCAL:R2"; + if( (0==dbProp.compare(0, dbServer.size(), dbServer)) || + (0==dbProp.compare(0, dbLocal.size(), dbLocal)) + ) + { + if (0!=dbProp.compare(0, dbLocalR2.size(), dbLocalR2)) + { + nlstop; + throw ELuaIHMException("setDbProp(): You are not allowed to write on 'SERVER:...' or 'LOCAL:...' database"); + } + } + + // Write to the DB if found + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + CCDBNodeLeaf *node= pIM->getDbProp(dbProp, false); + + if(node) + node->setValue32(value); + else + debugInfo(toString("setDbProp(): '%s' dbProp Not found", dbProp.c_str())); +} + +void CLuaIHMRyzom::delDbProp(const string &dbProp) +{ + //H_AUTO(Lua_CLuaIHM_setDbProp) + // Do not allow Write on SERVER: or LOCAL: + static const string dbServer= "SERVER:"; + static const string dbLocal= "LOCAL:"; + static const string dbLocalR2= "LOCAL:R2"; + if( (0==dbProp.compare(0, dbServer.size(), dbServer)) || + (0==dbProp.compare(0, dbLocal.size(), dbLocal)) + ) + { + if (0!=dbProp.compare(0, dbLocalR2.size(), dbLocalR2)) + { + nlstop; + throw ELuaIHMException("setDbProp(): You are not allowed to write on 'SERVER:...' or 'LOCAL:...' database"); + } + } + + // Write to the DB if found + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + pIM->delDbProp(dbProp); +} + +void CLuaIHMRyzom::addDbProp(const std::string &dbProp, sint32 value) +{ + //H_AUTO(Lua_CLuaIHM_setDbProp) + // Do not allow Write on SERVER: or LOCAL: + static const std::string dbServer= "SERVER:"; + static const std::string dbLocal= "LOCAL:"; + static const std::string dbLocalR2= "LOCAL:R2"; + if( (0==dbProp.compare(0, dbServer.size(), dbServer)) || + (0==dbProp.compare(0, dbLocal.size(), dbLocal)) + ) + { + if (0!=dbProp.compare(0, dbLocalR2.size(), dbLocalR2)) + { + nlstop; + throw ELuaIHMException("setDbProp(): You are not allowed to write on 'SERVER:...' or 'LOCAL:...' database"); + } + } + + // Write to the DB if found + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + CCDBNodeLeaf *node= pIM->getDbProp(dbProp, true); + if(node) + node->setValue32(value); +} + +// *************************************************************************** +void CLuaIHMRyzom::debugInfo(const std::string &cstDbg) +{ + //H_AUTO(Lua_CLuaIHM_debugInfo) + if(ClientCfg.DisplayLuaDebugInfo) + { + std::string dbg = cstDbg; + if (ClientCfg.LuaDebugInfoGotoButtonEnabled) + { + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + lua_State *ls = pIM->getLuaState()->getStatePointer(); + lua_Debug luaDbg; + if(lua_getstack (ls, 1, &luaDbg)) + { + if(lua_getinfo(ls, "lS", &luaDbg)) + { + // add a command button to jump to the wanted file + dbg = createGotoFileButtonTag(luaDbg.short_src, luaDbg.currentline) + dbg; + } + } + } + rawDebugInfo(dbg); + } +} + +// *************************************************************************** +void CLuaIHMRyzom::rawDebugInfo(const std::string &dbg) +{ + //H_AUTO(Lua_CLuaIHM_rawDebugInfo) + if(ClientCfg.DisplayLuaDebugInfo) + { + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + if (!dbg.empty() && dbg[0] == '@') + { + // if color is already given use the message as it + NLMISC::InfoLog->displayRawNL(dbg.c_str()); + } + else + { + NLMISC::InfoLog->displayRawNL(LuaHelperStuff::formatLuaErrorSysInfo(dbg).c_str()); + } + #ifdef LUA_NEVRAX_VERSION + if (LuaDebuggerIDE) + { + LuaDebuggerIDE->debugInfo(dbg.c_str()); + } + #endif + pIM->displaySystemInfo( LuaHelperStuff::formatLuaErrorSysInfo(dbg)); + } +} + + +void CLuaIHMRyzom::dumpCallStack(int startStackLevel) +{ + //H_AUTO(Lua_CLuaIHM_dumpCallStack) + if(ClientCfg.DisplayLuaDebugInfo) + { + lua_Debug dbg; + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + lua_State *ls = pIM->getLuaState()->getStatePointer(); + int stackLevel = startStackLevel; + rawDebugInfo("Call stack : "); + rawDebugInfo("-------------"); + while (lua_getstack (ls, stackLevel, &dbg)) + { + if(lua_getinfo(ls, "lS", &dbg)) + { + std::string result = createGotoFileButtonTag(dbg.short_src, dbg.currentline) + NLMISC::toString("%s:%d:", dbg.short_src, dbg.currentline); + rawDebugInfo(result); + } + ++ stackLevel; + } + } +} + +// *************************************************************************** +void CLuaIHMRyzom::getCallStackAsString(int startStackLevel /*=0*/,std::string &result) +{ + //H_AUTO(Lua_CLuaIHM_getCallStackAsString) + result.clear(); + lua_Debug dbg; + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + lua_State *ls = pIM->getLuaState()->getStatePointer(); + int stackLevel = startStackLevel; + result += "Call stack : \n"; + result += "-------------"; + while (lua_getstack (ls, stackLevel, &dbg)) + { + if(lua_getinfo(ls, "lS", &dbg)) + { + result += NLMISC::toString("%s:%d:", dbg.short_src, dbg.currentline); + } + ++ stackLevel; + } +} + +// *************************************************************************** +std::string CLuaIHMRyzom::getDefine(const std::string &def) +{ + //H_AUTO(Lua_CLuaIHM_getDefine) + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + if(ClientCfg.DisplayLuaDebugInfo && !pIM->isDefineExist(def)) + debugInfo(toString("getDefine(): '%s' not found", def.c_str())); + return pIM->getDefine(def); +} + +// *************************************************************************** +void CLuaIHMRyzom::setContextHelpText(const ucstring &text) +{ + //H_AUTO(Lua_CLuaIHM_setContextHelpText) + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + pIM->setContextHelpText(text); +} + +// *************************************************************************** +void CLuaIHMRyzom::messageBox(const ucstring &text) +{ + //H_AUTO(Lua_CLuaIHM_messageBox) + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + pIM->messageBox(text); +} + +// *************************************************************************** +void CLuaIHMRyzom::messageBox(const ucstring &text, const std::string &masterGroup) +{ + //H_AUTO(Lua_CLuaIHM_messageBox) + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + pIM->messageBox(text, masterGroup); +} + +// *************************************************************************** +void CLuaIHMRyzom::messageBox(const ucstring &text, const std::string &masterGroup, int caseMode) +{ + if (caseMode < 0 || caseMode >= CaseCount) + { + throw ELuaIHMException("messageBox: case mode value is invalid."); + } + //H_AUTO(Lua_CLuaIHM_messageBox) + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + pIM->messageBox(text, masterGroup, (TCaseMode) caseMode); +} + +// *************************************************************************** +void CLuaIHMRyzom::messageBox(const std::string &text) +{ + //H_AUTO(Lua_CLuaIHM_messageBox) + static volatile bool dumpCallStack = false; + if (dumpCallStack) + { + CLuaIHMRyzom::dumpCallStack(0); + } + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + pIM->messageBox(text); +} + +// *************************************************************************** +void CLuaIHMRyzom::messageBoxWithHelp(const ucstring &text) +{ + //H_AUTO(Lua_CLuaIHM_messageBox) + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + pIM->messageBoxWithHelp(text); +} + +// *************************************************************************** +void CLuaIHMRyzom::messageBoxWithHelp(const ucstring &text, const std::string &masterGroup) +{ + //H_AUTO(Lua_CLuaIHM_messageBox) + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + pIM->messageBoxWithHelp(text, masterGroup); +} + +// *************************************************************************** +void CLuaIHMRyzom::messageBoxWithHelp(const ucstring &text, const std::string &masterGroup, int caseMode) +{ + if (caseMode < 0 || caseMode >= CaseCount) + { + throw ELuaIHMException("messageBoxWithHelp: case mode value is invalid."); + } + //H_AUTO(Lua_CLuaIHM_messageBox) + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + pIM->messageBoxWithHelp(text, masterGroup, "" ,"", (TCaseMode) caseMode); +} + +// *************************************************************************** +void CLuaIHMRyzom::messageBoxWithHelp(const std::string &text) +{ + //H_AUTO(Lua_CLuaIHM_messageBox) + static volatile bool dumpCallStack = false; + if (dumpCallStack) + { + CLuaIHMRyzom::dumpCallStack(0); + } + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + pIM->messageBoxWithHelp(text); +} + +// *************************************************************************** +bool CLuaIHMRyzom::executeFunctionOnStack(CLuaState &ls, int numArgs, int numRet) +{ + //H_AUTO(Lua_CLuaIHM_executeFunctionOnStack) + static volatile bool dumpFunction = false; + if (dumpFunction) + { + CLuaStackRestorer lsr(&ls, ls.getTop()); + lua_Debug ar; + ls.pushValue(-1 - numArgs); + lua_getinfo (ls.getStatePointer(), ">lS", &ar); + nlwarning((std::string(ar.what) + ", at line " + toString(ar.linedefined) + " in " + std::string(ar.source)).c_str()); + } + int result = ls.pcall(numArgs, numRet); + switch (result) + { + case LUA_ERRRUN: + case LUA_ERRMEM: + case LUA_ERRERR: + { + debugInfo(ls.toString(-1)); + ls.pop(); + return false; + } + break; + case 0: + return true; + break; + default: + nlassert(0); + break; + } + return false; +} + +// *************************************************************************** +ucstring CLuaIHMRyzom::replacePvpEffectParam(const ucstring &str, sint32 parameter) +{ + //H_AUTO(Lua_CLuaIHM_replacePvpEffectParam) + ucstring result = str; + CSString s = str.toString(); + std::string p, paramString; + + // Locate parameter and store it + p = s.splitTo('%', true); + while (p.size() > 0 && s.size() > 0) + { + if (s[0] == 'p' || s[0] == 'n' || s[0] == 'r') + { + paramString = "%"; + paramString += s[0]; + break; + } + p = s.splitTo('%', true); + } + + // Return original string if param isn't found + if (paramString.size() < 2) + return str; + + // Replace parameter based on its type + switch (paramString[1]) + { + case 'p': + p = toString("%.1f %%", parameter/100.0); + break; + case 'n': + p = toString(parameter); + break; + case 'r': + p = toString("%.1f", parameter/100.0); + break; + default: + debugInfo("Bad arguments in " + str.toString() + " : " + paramString); + } + + strFindReplace(result, paramString.c_str(), p); + + return result; +} + +// *************************************************************************** +sint32 CLuaIHMRyzom::secondsSince1970ToHour(sint32 seconds) +{ + //H_AUTO(Lua_CLuaIHM_secondsSince1970ToHour) + // convert to readable form + struct tm *tstruct; + time_t tval= seconds; + tstruct= gmtime(&tval); + if(!tstruct) + { + debugInfo(toString("Bad Date Received: %d", seconds)); + return 0; + } + + return tstruct->tm_hour; // 0-23 +} + +// *************************************************************************** +void CLuaIHMRyzom::pauseBGDownloader() +{ + ::pauseBGDownloader(); +} + +// *************************************************************************** +void CLuaIHMRyzom::unpauseBGDownloader() +{ + ::unpauseBGDownloader(); +} + +// *************************************************************************** +void CLuaIHMRyzom::requestBGDownloaderPriority(uint priority) +{ + if (priority >= BGDownloader::ThreadPriority_Count) + { + throw NLMISC::Exception("requestBGDownloaderPriority() : invalid priority"); + } + CBGDownloaderAccess::getInstance().requestDownloadThreadPriority((BGDownloader::TThreadPriority) priority, false); +} + +// *************************************************************************** +sint CLuaIHMRyzom::getBGDownloaderPriority() +{ + return CBGDownloaderAccess::getInstance().getDownloadThreadPriority(); +} + +// *************************************************************************** +ucstring CLuaIHMRyzom::getPatchLastErrorMessage() +{ + if (isBGDownloadEnabled()) + { + return CBGDownloaderAccess::getInstance().getLastErrorMessage(); + } + else + { + CPatchManager *pPM = CPatchManager::getInstance(); + return pPM->getLastErrorMessage(); + } +} + +// *************************************************************************** +bool CLuaIHMRyzom::isInGame() +{ + CInterfaceManager *pIM = CInterfaceManager::getInstance(); + return pIM->isInGame(); +} + +// *************************************************************************** +uint32 CLuaIHMRyzom::getPlayerSelectedSlot() +{ + return (uint32) PlayerSelectedSlot; +} + +// *************************************************************************** +bool CLuaIHMRyzom::isPlayerSlotNewbieLand(uint32 slot) +{ + if (slot > CharacterSummaries.size()) + { + throw ELuaIHMException("isPlayerSlotNewbieLand(): Invalid slot %d", (int) slot); + } + return CharacterSummaries[slot].InNewbieland; +} + +// *************************************************************************** +sint32 CLuaIHMRyzom::getSkillIdFromName(const std::string &def) +{ + //H_AUTO(Lua_CLuaIHM_getSkillIdFromName) + SKILLS::ESkills e= SKILLS::toSkill(def); + // Avoid any bug, return SF if not found + if(e>=SKILLS::unknown) + e= SKILLS::SF; + return e; +} + +// *************************************************************************** +ucstring CLuaIHMRyzom::getSkillLocalizedName(sint32 skillId) +{ + //H_AUTO(Lua_CLuaIHM_getSkillLocalizedName) + return ucstring(STRING_MANAGER::CStringManagerClient::getSkillLocalizedName((SKILLS::ESkills)skillId)); +} + +// *************************************************************************** +sint32 CLuaIHMRyzom::getMaxSkillValue(sint32 skillId) +{ + //H_AUTO(Lua_CLuaIHM_getMaxSkillValue) + CSkillManager *pSM= CSkillManager::getInstance(); + return pSM->getMaxSkillValue((SKILLS::ESkills)skillId); +} + +// *************************************************************************** +sint32 CLuaIHMRyzom::getBaseSkillValueMaxChildren(sint32 skillId) +{ + //H_AUTO(Lua_CLuaIHM_getBaseSkillValueMaxChildren) + CSkillManager *pSM= CSkillManager::getInstance(); + return pSM->getBaseSkillValueMaxChildren((SKILLS::ESkills)skillId); +} + +// *************************************************************************** +sint32 CLuaIHMRyzom::getMagicResistChance(bool elementalSpell, sint32 casterSpellLvl, sint32 victimResistLvl) +{ + //H_AUTO(Lua_CLuaIHM_getMagicResistChance) + CSPhraseManager *pPM= CSPhraseManager::getInstance(); + casterSpellLvl= std::max(casterSpellLvl, sint32(0)); + victimResistLvl= std::max(victimResistLvl, sint32(0)); + /* The success rate in the table is actually the "Casting Success Chance". + Thus, the relativeLevel is casterSpellLvl - victimResistLvl + Moreover, must take the "PartialSuccessMaxDraw" line because the spell is not resisted if success>0 + */ + sint32 chanceToHit= pPM->getSuccessRate(elementalSpell?CSPhraseManager::STResistMagic:CSPhraseManager::STResistMagicLink, + casterSpellLvl-victimResistLvl, true); + clamp(chanceToHit, 0, 100); + + // Thus, the resist chance is 100 - hit chance. + return 100 - chanceToHit; +} + +// *************************************************************************** +sint32 CLuaIHMRyzom::getDodgeParryChance(sint32 attLvl, sint32 defLvl) +{ + //H_AUTO(Lua_CLuaIHM_getDodgeParryChance) + CSPhraseManager *pPM = CSPhraseManager::getInstance(); + attLvl= std::max(attLvl, sint32(0)); + defLvl= std::max(defLvl, sint32(0)); + + sint32 chance = pPM->getSuccessRate(CSPhraseManager::STDodgeParry, defLvl-attLvl, false); + clamp(chance, 0, 100); + + return chance; +} + +// *************************************************************************** +void CLuaIHMRyzom::browseNpcWebPage(const std::string &htmlId, const std::string &urlIn, bool addParameters, double timeout) +{ + //H_AUTO(Lua_CLuaIHM_browseNpcWebPage) + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + CGroupHTML *groupHtml= dynamic_cast(pIM->getElementFromId(htmlId)); + if(groupHtml) + { + // if true, it means that we want to display a web page that use webig auth + bool webig = urlIn.find("http://") == 0; + + string url; + // append the WebServer to the url + if (urlIn.find("ring_access_point=1") != std::string::npos) + { + url = RingMainURL + "?" + urlIn; + } + else if(webig) + { + url = urlIn; + } + else + { + url = WebServer + urlIn; + } + + if (addParameters && !webig) + { + // append shardid, playername and language code + string userName; + string guildName; + if(UserEntity) + { + userName = UserEntity->getDisplayName ().toString(); + STRING_MANAGER::CStringManagerClient *pSMC = STRING_MANAGER::CStringManagerClient::instance(); + ucstring ucsTmp; + pSMC->getString (UserEntity->getGuildNameID(), ucsTmp); + guildName = ucsTmp.toString(); + + while (guildName.find(' ') != string::npos) + { + guildName[guildName.find(' ')] = '_'; + } + } + + url += ((url.find('?') != string::npos) ? "&" : "?") + + string("shard=") + toString(ShardId) + + string("&user_login=") + userName + + string("&lang=") + ClientCfg.getHtmlLanguageCode() + + string("&guild_name=") + guildName; + } +/* Already added by GroupHtml + if(webig) + { + // append special webig auth params + addWebIGParams(url); + } +*/ + // set the wanted timeout + groupHtml->setTimeout((float)std::max(0.0, timeout)); + + // Browse the url + groupHtml->clean(); + groupHtml->browse(url.c_str()); + // Set top of the page + CCtrlScroll *pScroll = groupHtml->getScrollBar(); + if (pScroll != NULL) + pScroll->moveTrackY(10000); + } +} + + +// *************************************************************************** +void CLuaIHMRyzom::clearHtmlUndoRedo(const std::string &htmlId) +{ + //H_AUTO(Lua_CLuaIHM_clearHtmlUndoRedo) + CInterfaceManager *pIM= CInterfaceManager::getInstance(); + CGroupHTML *groupHtml= dynamic_cast(pIM->getElementFromId(htmlId)); + if(groupHtml) + groupHtml->clearUndoRedo(); +} + +// *************************************************************************** +ucstring CLuaIHMRyzom::getDynString(sint32 dynStringId) +{ + //H_AUTO(Lua_CLuaIHM_getDynString) + ucstring result; + STRING_MANAGER::CStringManagerClient::instance()->getDynString(dynStringId, result); + return result; +} + +// *************************************************************************** +bool CLuaIHMRyzom::isDynStringAvailable(sint32 dynStringId) +{ + //H_AUTO(Lua_CLuaIHM_isDynStringAvailable) + ucstring result; + bool res = STRING_MANAGER::CStringManagerClient::instance()->getDynString(dynStringId, result); + return res; +} + +// *************************************************************************** +bool CLuaIHMRyzom::isFullyPatched() +{ + return AvailablePatchs == 0; +} + +// *************************************************************************** +std::string CLuaIHMRyzom::getSheetType(const std::string &sheet) +{ + //H_AUTO(Lua_CLuaIHM_getSheetType) + const CEntitySheet *sheetPtr = SheetMngr.get(CSheetId(sheet)); + if (!sheetPtr) return ""; + return CEntitySheet::typeToString(sheetPtr->Type); +} + + +// *************************************************************************** +std::string CLuaIHMRyzom::getSheetName(uint32 sheetId) +{ + return CSheetId(sheetId).toString(); +} + +// *************************************************************************** +sint32 CLuaIHMRyzom::getFameIndex(const std::string &factionName) +{ + //H_AUTO(Lua_CLuaIHM_getFameIndex) + return CStaticFames::getInstance().getFactionIndex(factionName); +} + +// *************************************************************************** +std::string CLuaIHMRyzom::getFameName(sint32 fameIndex) +{ + //H_AUTO(Lua_CLuaIHM_getFameName) + return CStaticFames::getInstance().getFactionName(fameIndex); +} + +// *************************************************************************** +sint32 CLuaIHMRyzom::getFameDBIndex(sint32 fameIndex) +{ + //H_AUTO(Lua_CLuaIHM_getFameDBIndex) + // Yoyo: avoid crash if fames not initialized + if(CStaticFames::getInstance().getNbFame()==0) + return 0; + else + return CStaticFames::getInstance().getDatabaseIndex(fameIndex); +} + +// *************************************************************************** +sint32 CLuaIHMRyzom::getFirstTribeFameIndex() +{ + //H_AUTO(Lua_CLuaIHM_getFirstTribeFameIndex) + return CStaticFames::getInstance().getFirstTribeFameIndex(); +} + +// *************************************************************************** +sint32 CLuaIHMRyzom::getNbTribeFameIndex() +{ + //H_AUTO(Lua_CLuaIHM_getNbTribeFameIndex) + // Yoyo: avoid crash if fames not initialized. at leasst one tribe + return std::max(1U, CStaticFames::getInstance().getNbTribeFameIndex()); +} + +// *************************************************************************** +string CLuaIHMRyzom::getClientCfg(const string &varName) +{ + //H_AUTO(Lua_CLuaIHM_getClientCfg) + return ClientCfg.readString(varName); +} + +// *************************************************************************** +bool CLuaIHMRyzom::fileExists(const string &fileName) +{ + //H_AUTO(Lua_CLuaIHM_fileExists) + return CPath::exists(fileName); +} + +// *************************************************************************** +void CLuaIHMRyzom::sendMsgToServer(const std::string &sMsg) +{ + //H_AUTO(Lua_CLuaIHM_sendMsgToServer) + ::sendMsgToServer(sMsg); +} + +// *************************************************************************** +void CLuaIHMRyzom::sendMsgToServerPvpTag(bool pvpTag) +{ + //H_AUTO(Lua_CLuaIHM_sendMsgToServerPvpTag) + uint8 tag = (uint8)pvpTag; + ::sendMsgToServer("PVP:PVP_TAG", tag); +} + +// *************************************************************************** +bool CLuaIHMRyzom::isGuildQuitAvailable() +{ + //H_AUTO(Lua_CLuaIHM_isGuildQuitAvailable) + return CGuildManager::getInstance()->getGuild().QuitGuildAvailable; +} + +// *************************************************************************** +void CLuaIHMRyzom::sortGuildMembers() +{ + //H_AUTO(Lua_CLuaIHM_sortGuildMembers) + CGuildManager::getInstance()->sortGuildMembers(); +} + +// *************************************************************************** +sint32 CLuaIHMRyzom::getNbGuildMembers() +{ + //H_AUTO(Lua_CLuaIHM_getNbGuildMembers) + return (sint32)CGuildManager::getInstance()->getGuildMembers().size(); +} + +// *************************************************************************** +string CLuaIHMRyzom::getGuildMemberName(sint32 nMemberId) +{ + //H_AUTO(Lua_CLuaIHM_getGuildMemberName) + if ((nMemberId < 0) || (nMemberId >= getNbGuildMembers())) + return ""; + return CGuildManager::getInstance()->getGuildMembers()[nMemberId].Name.toString(); +} + +// *************************************************************************** +string CLuaIHMRyzom::getGuildMemberGrade(sint32 nMemberId) +{ + //H_AUTO(Lua_CLuaIHM_getGuildMemberGrade) + if ((nMemberId < 0) || (nMemberId >= getNbGuildMembers())) + return ""; + return EGSPD::CGuildGrade::toString(CGuildManager::getInstance()->getGuildMembers()[nMemberId].Grade); +} + +// *************************************************************************** +bool CLuaIHMRyzom::isR2Player(const std::string &sheet) +{ + //H_AUTO(Lua_CLuaIHM_isR2Player) + const CEntitySheet *entitySheet = SheetMngr.get(CSheetId(sheet)); + if (!entitySheet) return false; + const CCharacterSheet *chSheet = dynamic_cast(entitySheet); + if(!chSheet) return false; + return chSheet->R2Npc; +} + +// *************************************************************************** +std::string CLuaIHMRyzom::getR2PlayerRace(const std::string &sheet) +{ + //H_AUTO(Lua_CLuaIHM_getR2PlayerRace) + const CEntitySheet *entitySheet = SheetMngr.get(CSheetId(sheet)); + if (!entitySheet) return ""; + const CCharacterSheet *chSheet = dynamic_cast(entitySheet); + if(!chSheet) return ""; + return EGSPD::CPeople::toString(chSheet->Race); +} + +// *************************************************************************** +bool CLuaIHMRyzom::isR2PlayerMale(const std::string &sheet) +{ + //H_AUTO(Lua_CLuaIHM_isR2PlayerMale) + const CEntitySheet *entitySheet = SheetMngr.get(CSheetId(sheet)); + if (!entitySheet) return true; + const CCharacterSheet *chSheet = dynamic_cast(entitySheet); + if(!chSheet) return true; + + return (chSheet->Gender == GSGENDER::male); +} + +// *************************************************************************** +std::string CLuaIHMRyzom::getCharacterSheetSkel(const std::string &sheet, bool isMale) +{ + //H_AUTO(Lua_CLuaIHM_getCharacterSheetSkel) + const CEntitySheet *sheetPtr = SheetMngr.get(CSheetId(sheet)); + const CCharacterSheet *charSheet = dynamic_cast(sheetPtr); + if (charSheet) return charSheet->getSkelFilename(); + const CRaceStatsSheet *raceStatSheet = dynamic_cast(sheetPtr); + if (raceStatSheet) return raceStatSheet->GenderInfos[isMale ? 0 : 1].Skelfilename; + return ""; +} + +// *************************************************************************** +sint32 CLuaIHMRyzom::getSheetId(const std::string &itemName) +{ + //H_AUTO(Lua_CLuaIHM_getSheetId) + return (sint32)CSheetId(itemName).asInt(); +} + +// *************************************************************************** +sint CLuaIHMRyzom::getCharacterSheetRegionForce(const std::string &sheet) +{ + //H_AUTO(Lua_CLuaIHM_getCharacterSheetRegionForce) + const CCharacterSheet *charSheet = dynamic_cast(SheetMngr.get(CSheetId(sheet))); + if (!charSheet) return 0; + return charSheet->RegionForce; +} + +// *************************************************************************** +sint CLuaIHMRyzom::getCharacterSheetRegionLevel(const std::string &sheet) +{ + //H_AUTO(Lua_CLuaIHM_getCharacterSheetRegionLevel) + const CCharacterSheet *charSheet = dynamic_cast(SheetMngr.get(CSheetId(sheet))); + if (!charSheet) return 0; + return charSheet->RegionForce; +} + +// *************************************************************************** +string CLuaIHMRyzom::getRegionByAlias(uint32 alias) +{ + //H_AUTO(Lua_CLuaIHM_getRegionByAlias) + return ContinentMngr.getRegionNameByAlias(alias); +} + +// *************************************************************************** +void CLuaIHMRyzom::tell(const ucstring &player, const ucstring &msg) +{ + //H_AUTO(Lua_CLuaIHM_tell) + // display a /tell command in the main chat + if (!player.empty()) + { + if (!msg.empty()) + { + // Parse any tokens in the message. + ucstring msg_modified = msg; + // Parse any tokens in the text + if ( ! CInterfaceManager::parseTokens(msg_modified)) + { + return; + } + ChatMngr.tell(player.toUtf8(), msg_modified); + } + else + { + CChatWindow *w = PeopleInterraction.ChatGroup.Window; + if (w) + { + CInterfaceManager *im = CInterfaceManager::getInstance(); + w->setKeyboardFocus(); + w->enableBlink(1); + w->setCommand(ucstring("tell ") + CEntityCL::removeTitleFromName(player) + ucstring(" "), false); + CGroupEditBox *eb = w->getEditBox(); + if (eb != NULL) + { + eb->bypassNextKey(); + } + if (w->getContainer()) + { + w->getContainer()->setActive(true); + im->setTopWindow(w->getContainer()); + } + } + } + } +} + +// *************************************************************************** +bool CLuaIHMRyzom::isRingAccessPointInReach() +{ + //H_AUTO(Lua_CLuaIHM_isRingAccessPointInReach) + if (BotChatPageAll->RingSessions->RingAccessPointPos == CVector::Null) return false; + const CVectorD &vect1 = BotChatPageAll->RingSessions->RingAccessPointPos; + CVectorD vect2 = UserEntity->pos(); + double distanceSquare = pow(vect1.x-vect2.x,2) + pow(vect1.y-vect2.y,2); + return distanceSquare <= MaxTalkingDistSquare; +} + +// *************************************************************************** +void CLuaIHMRyzom::updateTooltipCoords() +{ + CInterfaceManager::getInstance()->updateTooltipCoords(); +} + +// *************************************************************************** +bool CLuaIHMRyzom::isCtrlKeyDown() +{ + //H_AUTO(Lua_CLuaIHM_isCtrlKeyDown) + bool ctrlDown = Driver->AsyncListener.isKeyDown(KeyLCONTROL) || + Driver->AsyncListener.isKeyDown(KeyRCONTROL); + if (ctrlDown) nlwarning("ctrl down"); + else nlwarning("ctrl up"); + return ctrlDown; +} + +// *************************************************************************** +std::string CLuaIHMRyzom::encodeURLUnicodeParam(const ucstring &text) +{ + //H_AUTO(Lua_CLuaIHM_encodeURLUnicodeParam) + return convertToHTML(text.toUtf8()); +} + +// *************************************************************************** +sint32 CLuaIHMRyzom::getPlayerLevel() +{ + if (!UserEntity) return -1; + CSkillManager *pSM= CSkillManager::getInstance(); + uint32 maxskill = pSM->getBestSkillValue(SKILLS::SC); + maxskill = std::max(maxskill, pSM->getBestSkillValue(SKILLS::SF)); + maxskill = std::max(maxskill, pSM->getBestSkillValue(SKILLS::SH)); + maxskill = std::max(maxskill, pSM->getBestSkillValue(SKILLS::SM)); + return sint32(maxskill); +} + +// *************************************************************************** +sint64 CLuaIHMRyzom::getPlayerVpa() +{ + sint64 prop = CInterfaceManager::getInstance()->getDbProp("SERVER:Entities:E0:P"+toString("%d", CLFECOMMON::PROPERTY_VPA))->getValue64(); + return prop; +} + +// *************************************************************************** +sint64 CLuaIHMRyzom::getPlayerVpb() +{ + sint64 prop = CInterfaceManager::getInstance()->getDbProp("SERVER:Entities:E0:P"+toString("%d", CLFECOMMON::PROPERTY_VPB))->getValue64(); + return prop; +} + +// *************************************************************************** +sint64 CLuaIHMRyzom::getPlayerVpc() +{ + sint64 prop = CInterfaceManager::getInstance()->getDbProp("SERVER:Entities:E0:P"+toString("%d", CLFECOMMON::PROPERTY_VPB))->getValue64(); + return prop; +} + +// *************************************************************************** +sint32 CLuaIHMRyzom::getTargetLevel() +{ + CEntityCL *target = getTargetEntity(); + if (!target) return -1; + if ( target->isPlayer() ) + { + CInterfaceManager *pIM = CInterfaceManager::getInstance(); + CCDBNodeLeaf *pDbPlayerLevel = pIM->getDbProp( pIM->getDefine("target_player_level") ); + return pDbPlayerLevel ? pDbPlayerLevel->getValue32() : -1; + } + else + { + CCharacterSheet *pCS = dynamic_cast(SheetMngr.get(target->sheetId())); + if(!pCS) return -1; + // only display the consider if the target is attackable #523 + if(!pCS->Attackable) return -1; + if(!target->properties().attackable()) return -1; + return sint32(pCS->Level); + } + return -1; +} + +// *************************************************************************** +ucstring CLuaIHMRyzom::getTargetSheet() +{ + CEntityCL *target = getTargetEntity(); + if (!target) return ucstring(); + + return target->sheetId().toString(); +} + +// *************************************************************************** +sint64 CLuaIHMRyzom::getTargetVpa() +{ + CEntityCL *target = getTargetEntity(); + if (!target) return 0; + + sint64 prop = CInterfaceManager::getInstance()->getDbProp("SERVER:Entities:E"+toString("%d", getTargetSlotNr())+":P"+toString("%d", CLFECOMMON::PROPERTY_VPA))->getValue64(); + + return prop; +} + +// *************************************************************************** +sint64 CLuaIHMRyzom::getTargetVpb() +{ + CEntityCL *target = getTargetEntity(); + if (!target) return 0; + + sint64 prop = CInterfaceManager::getInstance()->getDbProp("SERVER:Entities:E"+toString("%d", getTargetSlotNr())+":P"+toString("%d", CLFECOMMON::PROPERTY_VPB))->getValue64(); + + return prop; +} + +// *************************************************************************** +sint64 CLuaIHMRyzom::getTargetVpc() +{ + CEntityCL *target = getTargetEntity(); + if (!target) return 0; + + sint64 prop = CInterfaceManager::getInstance()->getDbProp("SERVER:Entities:E"+toString("%d", getTargetSlotNr())+":P"+toString("%d", CLFECOMMON::PROPERTY_VPB))->getValue64(); + + return prop; +} + +// *************************************************************************** +sint32 CLuaIHMRyzom::getTargetForceRegion() +{ + CEntityCL *target = getTargetEntity(); + if (!target) return -1; + if ( target->isPlayer() ) + { + CInterfaceManager *pIM = CInterfaceManager::getInstance(); + CCDBNodeLeaf *pDbPlayerLevel = pIM->getDbProp( pIM->getDefine("target_player_level") ); + if (!pDbPlayerLevel) return -1; + sint nLevel = pDbPlayerLevel->getValue32(); + if ( nLevel < 250 ) + { + return (sint32) ((nLevel < 20) ? 1 : (nLevel / 50) + 2); + } + else + { + return 8; + } + } + else + { + CCharacterSheet *pCS = dynamic_cast(SheetMngr.get(target->sheetId())); + return pCS ? (sint32) pCS->RegionForce : -1; + } + return 0; +} + +// *************************************************************************** +sint32 CLuaIHMRyzom::getTargetLevelForce() +{ + CEntityCL *target = getTargetEntity(); + if (!target) return -1; + if ( target->isPlayer() ) + { + CInterfaceManager *pIM = CInterfaceManager::getInstance(); + CCDBNodeLeaf *pDbPlayerLevel = pIM->getDbProp( pIM->getDefine("target_player_level") ); + if (!pDbPlayerLevel) return -1; + sint nLevel = pDbPlayerLevel->getValue32(); + if ( nLevel < 250 ) + { + return (sint32) (((nLevel % 50) * 5 / 50) + 1); + } + else + { + return 6; + } + } + else + { + CCharacterSheet *pCS = dynamic_cast(SheetMngr.get(target->sheetId())); + return pCS ? (sint32) pCS->ForceLevel : -1; + } + return 0; +} + +// *************************************************************************** +bool CLuaIHMRyzom::isTargetNPC() +{ + CEntityCL *target = getTargetEntity(); + if (!target) return false; + return target->isNPC(); +} + +// *************************************************************************** +bool CLuaIHMRyzom::isTargetPlayer() +{ + CEntityCL *target = getTargetEntity(); + if (!target) return false; + return target->isPlayer(); +} + + +// *************************************************************************** +bool CLuaIHMRyzom::isTargetUser() +{ + CEntityCL *target = getTargetEntity(); + if (!target) return false; + return target->isUser(); +} + +// *************************************************************************** +bool CLuaIHMRyzom::isPlayerInPVPMode() +{ + if (!UserEntity) return false; + return (UserEntity->getPvpMode() & PVP_MODE::PvpFaction || UserEntity->getPvpMode() & PVP_MODE::PvpFactionFlagged || UserEntity->getPvpMode() & PVP_MODE::PvpZoneFaction) != 0; +} + +// *************************************************************************** +bool CLuaIHMRyzom::isTargetInPVPMode() +{ + CEntityCL *target = getTargetEntity(); + if (!target) return false; + return (target->getPvpMode() & PVP_MODE::PvpFaction || target->getPvpMode() & PVP_MODE::PvpFactionFlagged || target->getPvpMode() & PVP_MODE::PvpZoneFaction) != 0; +} + +// *************************************************************************** +std::string CLuaIHMRyzom::createGotoFileButtonTag(const char *fileName, uint line) +{ + //H_AUTO(Lua_CLuaIHM_createGotoFileButtonTag) + if (ClientCfg.LuaDebugInfoGotoButtonEnabled) + { + // TODO nico : put this in the interface + // add a command button to jump to the wanted file + return toString("/$$%s|%s|lua|%s('%s', %d)$$/", + ClientCfg.LuaDebugInfoGotoButtonTemplate.c_str(), + ClientCfg.LuaDebugInfoGotoButtonCaption.c_str(), + ClientCfg.LuaDebugInfoGotoButtonFunction.c_str(), + fileName, + line + ); + } + return ""; +} diff --git a/code/ryzom/client/src/interface_v3/lua_ihm_ryzom.h b/code/ryzom/client/src/interface_v3/lua_ihm_ryzom.h new file mode 100644 index 000000000..200adbab6 --- /dev/null +++ b/code/ryzom/client/src/interface_v3/lua_ihm_ryzom.h @@ -0,0 +1,257 @@ +#ifndef LUA_IHM_RYZOM_H +#define LUA_IHM_RYZOM_H + +#include "lua_ihm.h" + +class CLuaIHMRyzom +{ +public: + static void RegisterRyzomFunctions( NLGUI::CLuaState &ls ); + +private: + static void createLuaEnumTable(CLuaState &ls, const std::string &str); + + + static int getUI(CLuaState &ls); // params: "ui:interface:...". return: CInterfaceElement* (nil if error), an additionnal boolean parameter + // LUA exported Functions with standard lua (because use ui object, use variable param number, or return dynamic-typed object) + static int setCaptureKeyboard(CLuaState &ls); + static int resetCaptureKeyboard(CLuaState &ls); + static int setOnDraw(CLuaState &ls); // params: CInterfaceGroup*, "script". return: none + static int addOnDbChange(CLuaState &ls); // params: CInterfaceGroup*, "dblist", "script". return: none + static int removeOnDbChange(CLuaState &ls);// params: CInterfaceGroup*. return: none + static int getUICaller(CLuaState &ls); // params: none. return: CInterfaceElement* (nil if error) + static int getCurrentWindowUnder(CLuaState &ls); // params: none. return: CInterfaceElement* (nil if none) + // can specify verbose display when the element is note found (default is true) + static int createGroupInstance(CLuaState &ls); // params : param 1 = template name, + // param 2 = id of parent where the instance will be inserted + // param 3 = table with ("template_param", "template_param_value") key/value pairs + // such as { id="foo", x="10" } etc. -> returns a new instance of the template, or nil on fail + static int createRootGroupInstance(CLuaState &ls); // params : param 1 = template name, + // param 2 = id of parent where the instance will be inserted + // param 3 = table with ("template_param", "template_param_value") key/value pairs + // such as { id="foo", x="10" } etc. -> returns a new instance of the template, or nil on fail + static int createUIElement(CLuaState &ls); // params : param 1 = template name, + // param 2 = id of parent where the instance will be inserted + // param 3 = table with ("template_param", "template_param_value") key/value pairs + // such as { id="foo", x="10" } etc. -> returns a new instance of the template, or nil on fail + + static int displayBubble(CLuaState &ls); // params : param 1 = bot id + // param 2 = text + // param 3 = table with all strings and urls + // {"main text"="http:///", "text option 1"="http:///", "text option 2"="http:///") etc... + static int getIndexInDB(CLuaState &ls); // params: CDBCtrlSheet*.... return: index, or 0 if error + static int getUIId(CLuaState &ls); // params: CInterfaceElement*. return: ui id (empty if error) + static int runAH(CLuaState &ls); // params: CInterfaceElement *, "ah", "params". return: none + static int runExpr(CLuaState &ls); // params: "expr". return: any of: nil,bool,string,number, RGBA, UCString + static int runFct(CLuaState &ls); // params: "expr", param1, param2.... return: any of: nil,bool,string,number, RGBA, UCString + static int runCommand(CLuaState &ls); // params: "command name", param1, param2 ... return true or false + static int formatUI(CLuaState &ls); // params: "expr", param1, param2.... return: string with # and % parsed + static int formatDB(CLuaState &ls); // params: param1, param2.... return: string with @ and , added + static int launchContextMenuInGame(CLuaState &ls); // params : menu name + static int parseInterfaceFromString(CLuaState &ls); // params : intreface script + static int updateAllLocalisedElements(CLuaState &ls); + static int breakPoint(CLuaState &ls); + static int getWindowSize(CLuaState &ls); + static int i18n(CLuaState &ls); // retrieve an unicode string from CI18N + static int setTextFormatTaged(CLuaState &ls); // set a text that may contains Tag Format infos + static int validMessageBox(CLuaState &ls); // ok/cancel type message box (can't get it to work through luabind) + static int concatUCString(CLuaState &ls); // workaround for + operator that don't work in luabind for ucstrings ... + static int concatString(CLuaState &ls); // speedup concatenation of several strings + static int tableToString(CLuaState &ls); // concat element of a table to build a string + static int setTopWindow(CLuaState &ls); // set the top window + static int initEmotesMenu(CLuaState &ls); + static int isUCString(CLuaState &ls); + static int hideAllWindows(CLuaState &ls); + static int hideAllNonSavableWindows(CLuaState &ls); + static int getDesktopIndex(CLuaState &ls); + static int setLuaBreakPoint(CLuaState &ls); // set a breakpoint in lua external debugger (file, line) + static int getMainPageURL(CLuaState &ls); + static int getCharSlot(CLuaState &ls); + static int displaySystemInfo(CLuaState &ls); + static int setWeatherValue(CLuaState &ls); // first value is a boolean to say automatic, second value ranges from of to 1 and gives the weather + static int getWeatherValue(CLuaState &ls); // get current real weather value (blend between server driven value & predicted value). Manual weather value is ignored + static int disableContextHelpForControl(CLuaState &ls); // params: CCtrlBase*. return: none + static int disableContextHelp(CLuaState &ls); + static int getPathContent(CLuaState &ls); + static int getServerSeason(CLuaState &ls); // get the last season sent by the server + // 0->auto, computed locally from the current day (or not received from server yet) + // 1->server force spring + // 2->' ' ' summer + // 3->' ' ' autumn + // 4->' ' ' winter + static int computeCurrSeason(CLuaState &ls); // compute current displayed season (1->spring, etc .) + static int getAutoSeason(CLuaState &ls); // compute automatic season that would be at this time (1->spring, etc .) + + + static int getTextureSize(CLuaState &ls); + static int enableModalWindow(CLuaState &ls); + static int disableModalWindow(CLuaState &ls); + static int getPlayerPos(CLuaState &ls); + static int getPlayerFront(CLuaState &ls); + static int getPlayerDirection(CLuaState &ls); + static int getPlayerGender(CLuaState &ls); + static int getPlayerName(CLuaState &ls); + static int getPlayerTitleRaw(CLuaState &ls); + static int getPlayerTitle(CLuaState &ls); + static int getTargetPos(CLuaState &ls); + static int getTargetFront(CLuaState &ls); + static int getTargetDirection(CLuaState &ls); + static int getTargetGender(CLuaState &ls); + static int getTargetName(CLuaState &ls); + static int getTargetTitleRaw(CLuaState &ls); + static int getTargetTitle(CLuaState &ls); + static int addSearchPathUser(CLuaState &ls); + static int getClientCfgVar(CLuaState &ls); + static int isPlayerFreeTrial(CLuaState &ls); + static int isPlayerNewbie(CLuaState &ls); + static int isInRingMode(CLuaState &ls); + static int getUserRace(CLuaState &ls); + static int getSheet2idx(CLuaState &ls); + static int getTargetSlot(CLuaState &ls); + static int getSlotDataSetId(CLuaState &ls); + + // LUA functions exported for Dev only (debug) + static int deleteUI(CLuaState &ls); // params: CInterfaceElement*.... return: none + static int deleteReflectable(CLuaState &ls); // params: CInterfaceElement*.... return: none + static int dumpUI(CLuaState &ls); // params: CInterfaceElement*.... return: none + static int setKeyboardContext(CLuaState &ls); + + static int getCompleteIslands(CLuaState &ls); + static int getIslandId(CLuaState &ls);//TEMP + + + ///////////////////////////// Standard Lua stuff ends here ////////////////////////////////////////////// + + static sint32 getDbProp(const std::string &dbProp); // return 0 if not found. + static void setDbProp(const std::string &dbProp, sint32 value); // Nb: the db prop is not created if not present. + static void addDbProp(const std::string &dbProp, sint32 value); // Nb: the db prop is created if not present. + static void delDbProp(const std::string &dbProp); + +public: + // Print a message in the log. + // Lua messages must be enabled (with ClientCfg.DisplayLuaDebugInfo = 1) + // Additionnally, if ClientCfg.LuaDebugInfoGotoButtonEnabled is set, then + // a button will be created near the line to allow to goto the lua line that issued the message + // by using an external editor + static void debugInfo(const std::string &dbg); + // Print a message in the log + // No 'goto file' button is created + // Lua messages must be enabled (with ClientCfg.DisplayLuaDebugInfo = 1) + + static void dumpCallStack(int startStackLevel = 0); + + /** execute function that is currently on the stack, possibly outputing error messages to the log + * \return true if execution succeeded + */ + static bool executeFunctionOnStack(CLuaState &ls, int numArgs, int numRet); + +private: + static void rawDebugInfo(const std::string &dbg); + // Dump callstack in the console + // Additionnally, if ClientCfg.LuaDebugInfoGotoButtonEnabled is set, then + // buttons will be created in fonr of eahc line to allow to goto the lua line that issued the message + // by using an external editor + + static void getCallStackAsString(int startStackLevel, std::string &result); + static std::string getDefine(const std::string &def); + static void setContextHelpText(const ucstring &text); + + + static void messageBox(const ucstring &text); + static void messageBox(const ucstring &text, const std::string &masterGroup); + static void messageBox(const ucstring &text, const std::string &masterGroup, int caseMode); + static void messageBox(const std::string &text); + static void messageBoxWithHelp(const ucstring &text); + static void messageBoxWithHelp(const ucstring &text, const std::string &masterGroup); + static void messageBoxWithHelp(const ucstring &text, const std::string &masterGroup, int caseMode); + static void messageBoxWithHelp(const std::string &text); + + static ucstring replacePvpEffectParam(const ucstring &str, sint32 parameter); + static sint32 secondsSince1970ToHour(sint32 seconds); + static void pauseBGDownloader(); + static void unpauseBGDownloader(); + static void requestBGDownloaderPriority(uint priority); + static sint getBGDownloaderPriority(); + static ucstring getPatchLastErrorMessage(); + static bool isInGame(); + static uint32 getPlayerSelectedSlot(); + static bool isPlayerSlotNewbieLand(uint32 slot); // test if one of the player slot is a newbieland one, if not so, client must be patched in order to continue + + // GameInfo + static sint32 getSkillIdFromName(const std::string &def); + static ucstring getSkillLocalizedName(sint32 skillId); + static sint32 getMaxSkillValue(sint32 skillId); + static sint32 getBaseSkillValueMaxChildren(sint32 skillId); + static sint32 getMagicResistChance(bool elementalSpell, sint32 casterSpellLvl, sint32 victimResistLvl); + static sint32 getDodgeParryChance(sint32 attLvl, sint32 defLvl); + static void browseNpcWebPage(const std::string &htmlId, const std::string &url, bool addParameters, double timeout); + static void clearHtmlUndoRedo(const std::string &htmlId); + static ucstring getDynString(sint32 dynStringId); + static bool isDynStringAvailable(sint32 dynStringId); + static bool isFullyPatched(); + static std::string getSheetType(const std::string &sheet); + static std::string getSheetName(uint32 sheetId); + static sint32 getFameIndex(const std::string &factionName); + static std::string getFameName(sint32 fameIndex); + static sint32 getFameDBIndex(sint32 fameIndex); // convert from the fame index + static sint32 getFirstTribeFameIndex(); // fame index of the 1st tribe + static sint32 getNbTribeFameIndex(); // number of tribe fame index (which are contiguous) + static std::string getClientCfg(const std::string &varName); + static bool fileExists(const std::string &fileName); + static void sendMsgToServer(const std::string &msgName); + static void sendMsgToServerPvpTag(bool pvpTag); + static bool isGuildQuitAvailable(); + static void sortGuildMembers(); + static sint32 getNbGuildMembers(); + static std::string getGuildMemberName(sint32 nMemberId); + static std::string getGuildMemberGrade(sint32 nMemberId); + static bool isR2Player(const std::string &sheet); + static std::string getR2PlayerRace(const std::string &sheet); + static bool isR2PlayerMale(const std::string &sheet); + // sheet access + // TODO nico : using the reflection system on sheets would allow to export them to lua without these functions ... + static std::string getCharacterSheetSkel(const std::string &sheet, bool isMale); + static sint32 getSheetId(const std::string &itemName); + static sint getCharacterSheetRegionForce(const std::string &sheet); + static sint getCharacterSheetRegionLevel(const std::string &sheet); + static std::string getRegionByAlias(uint32 alias); + // open the window to do a tell to 'player', if 'msg' is not empty, then the message will be sent immediatly + // else, current command of the chat window will be replaced with tell 'player' + static void tell(const ucstring &player, const ucstring &msg); + static bool isRingAccessPointInReach(); + static void updateTooltipCoords(); + // test if the ctrl key is down (NB nico : I didn't add other key, + // because it would be too easy to write a key recorder ...) + static bool isCtrlKeyDown(); + static std::string encodeURLUnicodeParam(const ucstring &text); + + static sint32 getPlayerLevel(); // get max level among player skills (magi, combat, crafting ,foraging) + static sint64 getPlayerVpa(); + static sint64 getPlayerVpb(); + static sint64 getPlayerVpc(); + static sint32 getTargetLevel(); // get current, precise level of the selected target, or -1 if there's no such selected target + static sint32 getTargetForceRegion(); // get 'force region' for current target, or -1 if there's no selected target + static sint32 getTargetLevelForce(); // get 'level force' for current target, or -1 if there's no selected target + static ucstring getTargetSheet(); // get the name of the target sheet (like 'zoha2old.creature') + static sint64 getTargetVpa(); + static sint64 getTargetVpb(); + static sint64 getTargetVpc(); + static bool isTargetNPC(); // return 'true' if the target is an npc + static bool isTargetPlayer(); // return 'true' if the target is a player + static bool isTargetUser(); // return 'true' if the target is the user + static bool isPlayerInPVPMode(); + static bool isTargetInPVPMode(); + + +public: + + // Create a special tag that will add a 'goto' button for the given file and line + // The tag should be appended in front of a string to use with 'rawDebugInfo'. + // when the final string will be printed, a button will be created in front of it + // Requires that 'ClientCfg.LuaDebugInfoGotoButtonEnabled' is set to 1, else + // a, empty tag is returned + static std::string createGotoFileButtonTag(const char *fileName, uint line); +}; + +#endif + diff --git a/code/ryzom/client/src/interface_v3/lua_object.cpp b/code/ryzom/client/src/interface_v3/lua_object.cpp index 17b703457..209d7ba0c 100644 --- a/code/ryzom/client/src/interface_v3/lua_object.cpp +++ b/code/ryzom/client/src/interface_v3/lua_object.cpp @@ -14,13 +14,14 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -#include "stdpch.h" +//#include "stdpch.h" #include "lua_object.h" #include "lua_ihm.h" +#include "lua_ihm_ryzom.h" #include "nel/gui/lua_helper.h" using namespace NLGUI; // -#include "interface_manager.h" +//#include "interface_manager.h" //////////////// // CLuaObject // @@ -616,7 +617,7 @@ void CLuaObject::dump(uint maxDepth /*= 20*/, std::set *alreadySee } catch(const std::exception &e) { - CLuaIHM::dumpCallStack(); + CLuaIHMRyzom::dumpCallStack(); nlwarning(e.what()); } } diff --git a/code/ryzom/client/src/interface_v3/view_text.cpp b/code/ryzom/client/src/interface_v3/view_text.cpp index 6a4dc36d6..e05349985 100644 --- a/code/ryzom/client/src/interface_v3/view_text.cpp +++ b/code/ryzom/client/src/interface_v3/view_text.cpp @@ -28,8 +28,7 @@ #include "nel/misc/xml_auto_ptr.h" #include "lua_ihm.h" - -#include "lua_ihm.h" +#include "lua_ihm_ryzom.h" using namespace std; using namespace NLMISC; @@ -2416,7 +2415,7 @@ void CViewText::setTextFormatTaged(const ucstring &text) // color format is available only if multilined if (!_MultiLine) - CLuaIHM::debugInfo(toString("ViewText isn't multilined : uc_hardtext_format will not act as wanted !\n%s", text.toString().c_str())); + CLuaIHMRyzom::debugInfo(toString("ViewText isn't multilined : uc_hardtext_format will not act as wanted !\n%s", text.toString().c_str())); } @@ -2473,7 +2472,7 @@ void CViewText::setSingleLineTextFormatTaged(const ucstring &text) // this color format is available only if not multilined if (_MultiLine) - CLuaIHM::debugInfo(toString("ViewText is multilined : uc_hardtext_single_line_format will not act as wanted !\n%s", text.toString().c_str())); + CLuaIHMRyzom::debugInfo(toString("ViewText is multilined : uc_hardtext_single_line_format will not act as wanted !\n%s", text.toString().c_str())); } diff --git a/code/ryzom/client/src/r2/displayer_lua.cpp b/code/ryzom/client/src/r2/displayer_lua.cpp index 540e1ea1f..f73960f3a 100644 --- a/code/ryzom/client/src/r2/displayer_lua.cpp +++ b/code/ryzom/client/src/r2/displayer_lua.cpp @@ -17,6 +17,7 @@ #include "stdpch.h" #include "displayer_lua.h" #include "../interface_v3/lua_ihm.h" +#include "../interface_v3/lua_ihm_ryzom.h" #include "editor.h" namespace R2 @@ -45,7 +46,7 @@ bool CDisplayerLua::init(const CLuaObject ¶meters) } CLuaState &ls = *parameters.getLuaState(); getEditor().getEnv().push(); // this is a method call - if (CLuaIHM::executeFunctionOnStack(ls, 1, 1)) + if (CLuaIHMRyzom::executeFunctionOnStack(ls, 1, 1)) { _ToLua._LuaTable.pop(ls); } @@ -94,7 +95,7 @@ void CDisplayerLua::CToLua::executeHandler(const CLuaString &eventName, int numA getEnclosing()->getDisplayedInstance()->getLuaProjection().push(); ls.insert(- numArgs - 1); if (dumpStackWanted) ls.dumpStack(); - CLuaIHM::executeFunctionOnStack(*_LuaTable.getLuaState(), numArgs + 2, 0); + CLuaIHMRyzom::executeFunctionOnStack(*_LuaTable.getLuaState(), numArgs + 2, 0); if (dumpStackWanted) ls.dumpStack(); } diff --git a/code/ryzom/client/src/r2/dmc/com_lua_module.cpp b/code/ryzom/client/src/r2/dmc/com_lua_module.cpp index 2d15b18f6..7c94d7560 100644 --- a/code/ryzom/client/src/r2/dmc/com_lua_module.cpp +++ b/code/ryzom/client/src/r2/dmc/com_lua_module.cpp @@ -37,6 +37,7 @@ #include "../editor.h" #include "../../net_manager.h" #include "../../interface_v3/lua_ihm.h" +#include "../../interface_v3/lua_ihm_ryzom.h" #include "game_share/object.h" #include "../r2_lua.h" @@ -797,7 +798,7 @@ sint CComLuaModule::luaRequestSetNode(lua_State* state) static volatile bool dumpCallstack = false; if (dumpCallstack) { - CLuaIHM::dumpCallStack(); + CLuaIHMRyzom::dumpCallStack(); } return requestSetNode(state, false); } diff --git a/code/ryzom/client/src/r2/editor.cpp b/code/ryzom/client/src/r2/editor.cpp index 9daa5db70..f2a6119e6 100644 --- a/code/ryzom/client/src/r2/editor.cpp +++ b/code/ryzom/client/src/r2/editor.cpp @@ -59,6 +59,7 @@ using namespace NLGUI; #include "../interface_v3/group_tree.h" #include "../client_cfg.h" #include "../interface_v3/lua_ihm.h" +#include "../interface_v3/lua_ihm_ryzom.h" #include "../interface_v3/lua_object.h" #include "../global.h" #include "../connection.h" @@ -3213,7 +3214,7 @@ void CEditor::initObjectProjectionMetatable() ls.push(true); return 1; } - CLuaIHM::dumpCallStack(); + CLuaIHMRyzom::dumpCallStack(); // object has been deleted but the script maintains a reference on it throw ELuaWrappedFunctionException(&ls, "Attempt to access an erased object"); } @@ -3230,14 +3231,14 @@ void CEditor::initObjectProjectionMetatable() else { // 'bad index' message already printed by CObject::getValue - CLuaIHM::dumpCallStack(); + CLuaIHMRyzom::dumpCallStack(); } } if (!ls.isString(2)) { nlwarning("String expected when accessing an object property, %s found instead", ls.getTypename(2)); - CLuaIHM::dumpCallStack(0); + CLuaIHMRyzom::dumpCallStack(0); return 0; } @@ -3554,7 +3555,7 @@ void CEditor::initObjectProjectionMetatable() { nlwarning("Duplicated key of type string found while attempting to enumerate an instance content."); nlwarning("key is %s", key.c_str()); - CLuaIHM::dumpCallStack(1); + CLuaIHMRyzom::dumpCallStack(1); CLuaIHM::fails(ls, "Aborting to avoid infinite loop."); } keys.insert(key); @@ -3623,7 +3624,7 @@ void CEditor::initObjectProjectionMetatable() static volatile bool from = false; if (from) { - CLuaIHM::dumpCallStack(0); + CLuaIHMRyzom::dumpCallStack(0); } nlassert(ls.getTop() == 2); if (!checkTag(ls)) return false; @@ -4432,7 +4433,7 @@ bool CEditor::doLuaScript(const char *filename, const char *fileDescText) std::string filename = msg.substr(0, extPos + 4); // extract filename including extension int line; fromString(&*(msg.begin() + extPos + 5), line); // line number follows - nlwarning((CLuaIHM::createGotoFileButtonTag(filename.c_str(), line) + e.what()).c_str()); + nlwarning((CLuaIHMRyzom::createGotoFileButtonTag(filename.c_str(), line) + e.what()).c_str()); } else { diff --git a/code/ryzom/client/src/r2/instance.cpp b/code/ryzom/client/src/r2/instance.cpp index c4287928a..00d32e750 100644 --- a/code/ryzom/client/src/r2/instance.cpp +++ b/code/ryzom/client/src/r2/instance.cpp @@ -26,6 +26,7 @@ #include "../entities.h" #include "../interface_v3/interface_manager.h" #include "../interface_v3/lua_ihm.h" +#include "../interface_v3/lua_ihm_ryzom.h" // #include "displayer_visual_entity.h" @@ -631,7 +632,7 @@ void CInstance::CToLua::executeHandler(const CLuaString &name, int numArgs) ls.insert(- numArgs - 1); // if (dumpStackWanted) ls.dumpStack(); - CLuaIHM::executeFunctionOnStack(ls, numArgs + 1, 0); + CLuaIHMRyzom::executeFunctionOnStack(ls, numArgs + 1, 0); if (dumpStackWanted) ls.dumpStack(); } diff --git a/code/ryzom/client/src/r2/object_factory_client.cpp b/code/ryzom/client/src/r2/object_factory_client.cpp index a8ad45e3d..81d4c085d 100644 --- a/code/ryzom/client/src/r2/object_factory_client.cpp +++ b/code/ryzom/client/src/r2/object_factory_client.cpp @@ -19,6 +19,7 @@ #include "editor.h" #include "instance.h" #include "../interface_v3/lua_ihm.h" +#include "../interface_v3/lua_ihm_ryzom.h" namespace R2 { @@ -287,7 +288,7 @@ void CObjectRefIdClient::getNameInParent(std::string &name, sint32 &indexInArray } // TMP TMP nlwarning("========================================="); - CLuaIHM::dumpCallStack(); + CLuaIHMRyzom::dumpCallStack(); nlwarning("========================================="); nlwarning("ObservedObject = %s", getValue().c_str()); CInstance *obsInstance = getEditor().getInstanceFromId(getValue().c_str());