CHANGED: #1471 Moved some Lua related code from CInterfaceManager to CLuaManager and CInterfaceParser.

This commit is contained in:
dfighter1985 2012-07-12 09:01:35 +02:00
parent cdbcadcbaa
commit 9ceeb6c967
17 changed files with 87 additions and 151 deletions

View file

@ -299,17 +299,23 @@ namespace NLGUI
protected:
std::map< std::string, IParserModule* > moduleMap;
// LUA
// ----------------------------------------------------------------------------------
// LUA Interface State. NB: The LUA environnement is not shared between Login/OutGame/InGame
NLMISC::CSmartPtr<CLuaState> _LuaState;
virtual void initLUA();
void uninitLUA();
// List of script loaded (for reloadLua command)
std::set<std::string> _LuaFileScripts;
// Load A .lua. false if parse error. string 'error' contains the eventual error desc (but warning still displayed)
bool loadLUA(const std::string &luaFile, std::string &error);
bool cacheUIParsing;
bool luaInitialized;
public:
void initLUA();
void uninitLUA();
bool isLuaInitialized() const{ return luaInitialized; }
/// Load A .lua. false if parse error. string 'error' contains the eventual error desc (but warning still displayed)
bool loadLUA( const std::string &luaFile, std::string &error );
/// Reload all LUA scripts inserted through <lua>
void reloadAllLuaFileScripts();
};
}

View file

@ -45,13 +45,17 @@ namespace NLGUI
bool executeLuaScript( const std::string &luaScript, bool smallScript = false );
void ResetLuaState();
void forceGarbageCollect();
private:
CLuaManager();
static CLuaManager *instance;
static bool debugLua;
NLMISC::CSmartPtr< NLGUI::CLuaState > luaState;
NLGUI::CLuaState *luaState;
};
}

View file

@ -198,14 +198,12 @@ namespace NLGUI
// ----------------------------------------------------------------------------
CInterfaceParser::CInterfaceParser()
{
// LUA
_LuaState= NULL;
luaInitialized = false;
cacheUIParsing = false;
}
CInterfaceParser::~CInterfaceParser()
{
_LuaState = NULL;
removeAllModules();
}
/** Convert a string into a memstream
@ -2674,15 +2672,7 @@ namespace NLGUI
// ***************************************************************************
void CInterfaceParser::initLUA()
{
// do nothing if LUA environment already exists
if( _LuaState != NULL )
return;
// create a new LUA environnement
nlassert(_LuaState==NULL);
CLuaManager::enableLuaDebugging();
CLuaManager::getInstance();
_LuaState= CLuaManager::getInstance().getLuaState();
#ifdef LUA_NEVRAX_VERSION
extern ILuaIDEInterface *LuaDebuggerIDE;
@ -2690,18 +2680,16 @@ namespace NLGUI
#endif
// register LUA methods
CLuaIHM::registerAll(*_LuaState);
CLuaIHM::registerAll( *( CLuaManager::getInstance().getLuaState() ) );
luaInitialized = true;
}
// ***************************************************************************
void CInterfaceParser::uninitLUA()
{
// Delete all LUA environnement (and hence variables)
// delete _LuaState;
// _LuaState= NULL;
// delete all .lua file loaded
_LuaFileScripts.clear();
CLuaManager::getInstance().ResetLuaState();
luaInitialized = false;
}
// ***************************************************************************
@ -2740,10 +2728,9 @@ namespace NLGUI
}
// Parse script
nlassert(_LuaState);
try
{
_LuaState->executeFile(pathName);
CLuaManager::getInstance().getLuaState()->executeFile(pathName);
}
catch(const ELuaError &e)
{
@ -2755,5 +2742,18 @@ namespace NLGUI
return true;
}
void CInterfaceParser::reloadAllLuaFileScripts()
{
std::set< std::string >::const_iterator it;
for( it = _LuaFileScripts.begin(); it != _LuaFileScripts.end(); ++it )
{
std::string error;
// if fail to reload a script, display the error code
if( !loadLUA( *it, error ) )
{
nlwarning( LuaHelperStuff::formatLuaErrorSysInfo( error ).c_str() );
}
}
}
}

View file

@ -31,6 +31,7 @@ namespace NLGUI
CLuaManager::~CLuaManager()
{
delete luaState;
luaState = NULL;
}
@ -51,5 +52,18 @@ namespace NLGUI
return true;
}
void CLuaManager::ResetLuaState()
{
delete luaState;
luaState = new CLuaState( debugLua );
}
void CLuaManager::forceGarbageCollect()
{
nlinfo("Collecting Garbaged LUA variables");
luaState->setGCThreshold( 0 );
nlinfo( NLMISC::toString( "Memory Used : %d Kb", luaState->getGCCount() ).c_str() );
}
}

View file

@ -5105,7 +5105,7 @@ NLMISC_COMMAND(luaScript, "Execute a lua script", "direct_script_code")
}
// not smallScript because suppose var can change a lot
pIM->executeLuaScript(script, false);
CLuaManager::getInstance().executeLuaScript(script, false);
return true;
}
@ -5186,7 +5186,7 @@ NLMISC_COMMAND(luaGC, "Force a garbage collector of lua", "")
CInterfaceManager *pIM= CInterfaceManager::getInstance();
if(ClientCfg.AllowDebugLua)
{
pIM->luaGarbageCollect();
CLuaManager::getInstance().forceGarbageCollect();
return true;
}
else

View file

@ -838,7 +838,7 @@ void updateBGDownloaderUI()
{
if (LuaBGDSuccessFlag)
{
LuaBGDSuccessFlag = im->executeLuaScript("bgdownloader:setPatchSuccess()");
LuaBGDSuccessFlag = CLuaManager::getInstance().executeLuaScript("bgdownloader:setPatchSuccess()");
}
}
else
@ -858,19 +858,19 @@ void updateBGDownloaderUI()
}
if (LuaBGDSuccessFlag && bgWindowVisible)
{
LuaBGDSuccessFlag = im->executeLuaScript(toString("bgdownloader:setPatchProgress(%f)", progress));
LuaBGDSuccessFlag = CLuaManager::getInstance().executeLuaScript(toString("bgdownloader:setPatchProgress(%f)", progress));
}
// display current priority of the downloader
if (LuaBGDSuccessFlag && bgWindowVisible)
{
LuaBGDSuccessFlag = im->executeLuaScript("bgdownloader:displayPriority()");
LuaBGDSuccessFlag = CLuaManager::getInstance().executeLuaScript("bgdownloader:displayPriority()");
}
}
break;
case BGDownloader::TaskResult_Success:
if (LuaBGDSuccessFlag && bgWindowVisible)
{
LuaBGDSuccessFlag = im->executeLuaScript("bgdownloader:setPatchSuccess()");
LuaBGDSuccessFlag = CLuaManager::getInstance().executeLuaScript("bgdownloader:setPatchSuccess()");
}
// task finished
AvailablePatchs = 0;
@ -887,7 +887,7 @@ void updateBGDownloaderUI()
// error case
if (LuaBGDSuccessFlag && bgWindowVisible)
{
LuaBGDSuccessFlag = im->executeLuaScript("bgdownloader:setPatchError()");
LuaBGDSuccessFlag = CLuaManager::getInstance().executeLuaScript("bgdownloader:setPatchError()");
}
break;
}
@ -900,12 +900,12 @@ void updateBGDownloaderUI()
if (isBGDownloadEnabled())
{
// no necessary patch for now
LuaBGDSuccessFlag = im->executeLuaScript("bgdownloader:setNoNecessaryPatch()");
LuaBGDSuccessFlag = CLuaManager::getInstance().executeLuaScript("bgdownloader:setNoNecessaryPatch()");
}
else
{
// no download ui
LuaBGDSuccessFlag = im->executeLuaScript("bgdownloader:setNoDownloader()");
LuaBGDSuccessFlag = CLuaManager::getInstance().executeLuaScript("bgdownloader:setNoDownloader()");
}
}
}

View file

@ -1080,7 +1080,7 @@ public:
CInterfaceManager *pIM= CInterfaceManager::getInstance();
// start the npc web page
pIM->executeLuaScript("game:startNpcWebPage()", true);
CLuaManager::getInstance().executeLuaScript("game:startNpcWebPage()", true);
}
};
REGISTER_ACTION_HANDLER( CHandlerContextWebPage, "context_web_page");

View file

@ -1422,7 +1422,7 @@ public:
CInterfaceManager *im = CInterfaceManager::getInstance();
im->displaySystemInfo(ucstring("@{6F6F}") + playerName +ucstring(" @{FFFF}") + CI18N::get("uiRingInvitationSent"), "BC");
// force a refresh of the ui
im->executeLuaScript("CharTracking:forceRefresh()");
CLuaManager::getInstance().executeLuaScript("CharTracking:forceRefresh()");
}
}
};

View file

@ -127,7 +127,7 @@ void CGroupHTMLCS::getParameters (std::vector<CParameter> &parameters, bool enco
{
webIgReady = true;
CInterfaceManager *pIM = CInterfaceManager::getInstance();
pIM->executeLuaScript("game:onWebIgReady()");
CLuaManager::getInstance().executeLuaScript("game:onWebIgReady()");
}
// For each line

View file

@ -622,15 +622,15 @@ void CInterfaceManager::destroy ()
void CInterfaceManager::initLUA()
{
if( _LuaState != NULL )
if( isLuaInitialized() )
return;
CInterfaceParser::initLUA();
if( _LuaState == NULL )
if( !isLuaInitialized() )
return;
CLuaIHMRyzom::RegisterRyzomFunctions( *_LuaState );
CLuaIHMRyzom::RegisterRyzomFunctions( *( CLuaManager::getInstance().getLuaState() ) );
}
// ------------------------------------------------------------------------------------------------
@ -1518,7 +1518,7 @@ void CInterfaceManager::updateFrameEvents()
luaDebuggerMainLoop();
// handle gc for lua
if (_LuaState) _LuaState->handleGC();
CLuaManager::getInstance().getLuaState()->handleGC();
CBGDownloaderAccess::getInstance().update();
@ -3251,72 +3251,6 @@ void CInterfaceManager::connectYuboChat()
}
}
// ***************************************************************************
bool CInterfaceManager::executeLuaScript(const std::string &luaScript, bool smallScript)
{
H_AUTO ( RZ_Interface_executeLuaScript )
nlassert(_LuaState);
try
{
if(smallScript)
_LuaState->executeSmallScript(luaScript);
else
_LuaState->executeScript(luaScript);
}
catch(const ELuaError &e)
{
std::string msg = e.luaWhat();
char filename[MAX_PATH];
char exceptionName[MAX_PATH];
uint32 line;
// 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 = CLuaIHMRyzom::createGotoFileButtonTag(filename, line) + msg;
nlwarning(LuaHelperStuff::formatLuaErrorNlWarn(msg).c_str());
displaySystemInfo(LuaHelperStuff::formatLuaErrorSysInfo(msg));
}
else // AJM: handle the other 0.1% of cases
{
// Yoyo: seems that previous test doesn't work.... btw, must still print the message please...
std::vector<string> error;
splitString(msg.c_str(), ":", error);
if (error.size() > 3)
{
std::vector<string> contextList;
explode(luaScript, string("\n"), contextList);
fromString(error[2], line);
if (line >= 3 && contextList.size() >= line)
msg = error[0]+": \n>>> "+contextList[line-3]+"\n>>> "+contextList[line-2]+"\n>>> "+contextList[line-1]+"\nError:"+error[2]+": "+error[3];
else if (line >= 2 && contextList.size() >= line)
msg = error[0]+": \n>>>"+contextList[line-2]+"\n>>>"+contextList[line-1]+"\nError:"+error[2]+": "+error[3];
else if (line >= 1 && contextList.size() >= line)
msg = error[0]+": \n>>>"+contextList[line-1]+"\nError:"+error[2]+": "+error[3];
}
nlwarning(LuaHelperStuff::formatLuaErrorNlWarn(msg).c_str());
displaySystemInfo(LuaHelperStuff::formatLuaErrorSysInfo(msg));
}
return false;
}
return true;
}
// ***************************************************************************
void CInterfaceManager::reloadAllLuaFileScripts()
{
std::set<std::string>::const_iterator it= _LuaFileScripts.begin();
for(;it!=_LuaFileScripts.end();it++)
{
string error;
// if fail to reload a script, display the error code
if(!loadLUA(*it, error))
{
displaySystemInfo(LuaHelperStuff::formatLuaErrorSysInfo(error));
}
}
}
// ***************************************************************************
std::vector<std::string> CInterfaceManager::getInGameXMLInterfaceFiles()
{
@ -3374,8 +3308,7 @@ void CInterfaceManager::dumpLuaString(const std::string &str)
// ***************************************************************************
void CInterfaceManager::getLuaValueInfo(std::string &str, sint index)
{
nlassert(_LuaState);
CLuaState &ls= *_LuaState;
CLuaState &ls= *( CLuaManager::getInstance().getLuaState() );
sint type= ls.type(index);
if(type==LUA_TNIL)
@ -3421,8 +3354,7 @@ void CInterfaceManager::getLuaValueInfo(std::string &str, sint index)
// ***************************************************************************
void CInterfaceManager::dumpLuaKeyValueInfo(uint recursTableLevel, uint tabLevel)
{
nlassert(_LuaState);
CLuaState &ls= *_LuaState;
CLuaState &ls= *( CLuaManager::getInstance().getLuaState() );
CLuaStackChecker lsc(&ls);
// Dump Key Str
@ -3458,11 +3390,7 @@ void CInterfaceManager::dumpLuaKeyValueInfo(uint recursTableLevel, uint tabLeve
// ***************************************************************************
void CInterfaceManager::dumpLuaState(uint detail)
{
if(!_LuaState)
{
dumpLuaString("LUA State not created");
return;
}
CLuaState *_LuaState = CLuaManager::getInstance().getLuaState();
// clamp detailed info to 2 (display at max content of eaxh Env of each group)
clamp(detail, 0U, 2U);
@ -3496,16 +3424,6 @@ void CInterfaceManager::dumpLuaState(uint detail)
}
}
// ***************************************************************************
void CInterfaceManager::luaGarbageCollect()
{
if(!_LuaState)
return;
dumpLuaString("Collecting Garbaged LUA variables");
_LuaState->setGCThreshold(0);
dumpLuaString(NLMISC::toString("Memory Used : %d Kb", _LuaState->getGCCount()));
}
// ------------------------------------------------------------------------------------------------
void CInterfaceManager::createLocalBranch(const std::string &fileName, NLMISC::IProgressCallback &progressCallBack)
{

View file

@ -51,6 +51,8 @@
#include "../ingame_database_manager.h"
#include "nel/gui/lua_manager.h"
//the network database node
extern CCDBSynchronised IngameDbMngr;
@ -365,14 +367,8 @@ public:
/// \name LUA
// @{
/// Execute a lua script (smallScript for speed optimisation, see lua_helper). return false if parse/execute error (warning/sysinfo displayed)
bool executeLuaScript(const std::string &luaScript, bool smallScript= false);
/// Reload all LUA scripts inserted through <lua>
void reloadAllLuaFileScripts();
/// For debug: dump in the sysinfo and nlwarning state of lua. detail range from 0 to 2 (clamped).
void dumpLuaState(uint detail);
/// For debug: force a garbage collector
void luaGarbageCollect();
void dumpLuaState(uint detail);
// @}
// Get the list of InGame XML Interface files, with any AddOn ones

View file

@ -133,7 +133,7 @@ public:
// 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);
CLuaManager::getInstance().executeLuaScript(sParams, true);
// pop UI caller
if(pCaller)
@ -179,7 +179,7 @@ static DECLARE_INTERFACE_USER_FCT(lua)
// assign return value in retId.
script= retId + "= " + script;
// execute a small script here, because most often exprs are called from xml files => lot of redundant script
pIM->executeLuaScript(script, true);
CLuaManager::getInstance().executeLuaScript(script, true);
// *** retrieve and convert return value

View file

@ -1520,7 +1520,7 @@ bool mainLoop()
CSessionBrowserImpl::getInstance().init(CLuaManager::getInstance().getLuaState());
}
CInterfaceManager::getInstance()->executeLuaScript("game:onMainLoopBegin()");
CLuaManager::getInstance().executeLuaScript("game:onMainLoopBegin()");
if (StartInitTime != 0)
@ -1782,7 +1782,7 @@ bool mainLoop()
if (group)
group->updateAllLinks();
// send a msg to lua for specific ui update
pIM->executeLuaScript("game:onInGameDbInitialized()");
CLuaManager::getInstance().executeLuaScript("game:onInGameDbInitialized()");
}
}
@ -2896,7 +2896,7 @@ bool mainLoop()
// This code must remain at the very end of the main loop.
if(LoginSM.getCurrentState() == CLoginStateMachine::st_enter_far_tp_main_loop)
{
CInterfaceManager::getInstance()->executeLuaScript("game:onFarTpStart()");
CLuaManager::getInstance().executeLuaScript("game:onFarTpStart()");
// Will loop the network until the end of the relogging process
FarTP.farTPmainLoop();
@ -2974,7 +2974,7 @@ bool mainLoop()
lastConnectionState = CNetworkConnection::Connected;
connectionState = NetMngr.getConnectionState();
CInterfaceManager::getInstance()->executeLuaScript("game:onFarTpEnd()");
CLuaManager::getInstance().executeLuaScript("game:onFarTpEnd()");
}
} // end of main loop
@ -2982,7 +2982,7 @@ bool mainLoop()
CInterfaceManager *im = CInterfaceManager::getInstance();
if (CLuaManager::getInstance().getLuaState())
{
CInterfaceManager::getInstance()->executeLuaScript("game:onMainLoopEnd()");
CLuaManager::getInstance().executeLuaScript("game:onMainLoopEnd()");
}
// Stop Running Profiles (kick result)
@ -4550,7 +4550,7 @@ void displayDebugClusters()
void inGamePatchUncompleteWarning()
{
CInterfaceManager *im = CInterfaceManager::getInstance();
im->executeLuaScript("bgdownloader:inGamePatchUncompleteWarning()");
CLuaManager::getInstance().executeLuaScript("bgdownloader:inGamePatchUncompleteWarning()");
/*
CInterfaceManager *im = CInterfaceManager::getInstance();
CGroupContainer *gc = dynamic_cast<CGroupContainer *>(CWidgetManager::getInstance()->getElementFromId("ui:interface:bg_downloader"));

View file

@ -893,7 +893,7 @@ void CInterfaceChatDisplayer::displayTell(/*TDataSetIndex senderIndex, */const u
// for now, '&' are removed by server so use another format until a special msg is made
if (strFindReplace(finalString, ucstring("<R2_INVITE>"), ucstring()))
{
CInterfaceManager::getInstance()->executeLuaScript("RingAccessPoint:forceRefresh()");
CLuaManager::getInstance().executeLuaScript("RingAccessPoint:forceRefresh()");
}
@ -1704,8 +1704,7 @@ void impulseTeamInvitation(NLMISC::CBitMemStream &impulse)
impulse.serial(textID);
if (PermanentlyBanned) return;
CInterfaceManager *pIM = CInterfaceManager::getInstance();
pIM->executeLuaScript("game:onTeamInvation("+toString(textID)+")", 0);
CLuaManager::getInstance().executeLuaScript("game:onTeamInvation("+toString(textID)+")", 0);
}// impulseTeamInvitation //
//-----------------------------------------------

View file

@ -7646,8 +7646,7 @@ class CAHR2FreezeUnfreezeBotObjects : public IActionHandler
{
virtual void execute(CCtrlBase * /* pCaller */, const std::string &/* sParams */)
{
CInterfaceManager *im = CInterfaceManager::getInstance();
im->executeLuaScript("r2:freezeUnfreezeBotObjects()");
CLuaManager::getInstance().executeLuaScript("r2:freezeUnfreezeBotObjects()");
}
};
REGISTER_ACTION_HANDLER(CAHR2FreezeUnfreezeBotObjects, "r2ed_freeze_unfreeze_botobjects");

View file

@ -114,7 +114,7 @@ void CToolCreateEntity::commit(const NLMISC::CVector &createPosition, float crea
if (!getEditor().verifyRoomLeft(0, 1))
{
getUI().executeLuaScript("r2:checkStaticQuota(1)");
CLuaManager::getInstance().executeLuaScript("r2:checkStaticQuota(1)");
return;
}
setContextHelp(CI18N::get("uiR2EDDrawArrayContextHelp"));

View file

@ -1662,7 +1662,7 @@ void CUserEntity::moveToAction(CEntityCL *ent)
break;
// Outpost
case CUserEntity::Outpost:
IM->executeLuaScript("game:outpostBCOpenStateWindow()", 0);
CLuaManager::getInstance().executeLuaScript("game:outpostBCOpenStateWindow()", 0);
break;
// BuildTotem
case CUserEntity::BuildTotem: