Refactor local script iteration (Fixes #2806, Fixes #3108)

This should be much safer. Don't use recursion. Don't fail if mIgnore happens to be in the list twice. Don't rely on preconditions / assertions.
This commit is contained in:
scrawl 2016-02-03 16:09:20 +01:00
parent 187d2bccda
commit cc3563359e
3 changed files with 13 additions and 29 deletions

View file

@ -69,11 +69,9 @@ void OMW::Engine::executeLocalScripts()
MWWorld::LocalScripts& localScripts = mEnvironment.getWorld()->getLocalScripts(); MWWorld::LocalScripts& localScripts = mEnvironment.getWorld()->getLocalScripts();
localScripts.startIteration(); localScripts.startIteration();
std::pair<std::string, MWWorld::Ptr> script;
while (!localScripts.isFinished()) while (localScripts.getNext(script))
{ {
std::pair<std::string, MWWorld::Ptr> script = localScripts.getNext();
MWScript::InterpreterContext interpreterContext ( MWScript::InterpreterContext interpreterContext (
&script.second.getRefData().getLocals(), script.second); &script.second.getRefData().getLocals(), script.second);
mEnvironment.getScriptManager()->run (script.first, interpreterContext); mEnvironment.getScriptManager()->run (script.first, interpreterContext);

View file

@ -76,32 +76,20 @@ void MWWorld::LocalScripts::startIteration()
mIter = mScripts.begin(); mIter = mScripts.begin();
} }
bool MWWorld::LocalScripts::isFinished() const bool MWWorld::LocalScripts::getNext(std::pair<std::string, Ptr>& script)
{ {
if (mIter==mScripts.end()) while (mIter!=mScripts.end())
return true;
if (!mIgnore.isEmpty() && mIter->second==mIgnore)
{ {
std::list<std::pair<std::string, Ptr> >::iterator iter = mIter; std::list<std::pair<std::string, Ptr> >::iterator iter = mIter++;
return ++iter==mScripts.end(); if (mIgnore.isEmpty() || iter->second!=mIgnore)
{
script = *iter;
return true;
}
} }
return false; return false;
} }
std::pair<std::string, MWWorld::Ptr> MWWorld::LocalScripts::getNext()
{
assert (!isFinished());
std::list<std::pair<std::string, Ptr> >::iterator iter = mIter++;
if (mIgnore.isEmpty() || iter->second!=mIgnore)
return *iter;
return getNext();
}
void MWWorld::LocalScripts::add (const std::string& scriptName, const Ptr& ptr) void MWWorld::LocalScripts::add (const std::string& scriptName, const Ptr& ptr)
{ {
if (const ESM::Script *script = mStore.get<ESM::Script>().search (scriptName)) if (const ESM::Script *script = mStore.get<ESM::Script>().search (scriptName))

View file

@ -31,11 +31,9 @@ namespace MWWorld
void startIteration(); void startIteration();
///< Set the iterator to the begin of the script list. ///< Set the iterator to the begin of the script list.
bool isFinished() const; bool getNext(std::pair<std::string, Ptr>& script);
///< Is iteration finished? ///< Get next local script
/// @return Did we get a script?
std::pair<std::string, Ptr> getNext();
///< Get next local script (must not be called if isFinished())
void add (const std::string& scriptName, const Ptr& ptr); void add (const std::string& scriptName, const Ptr& ptr);
///< Add script to collection of active local scripts. ///< Add script to collection of active local scripts.