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();
localScripts.startIteration();
while (!localScripts.isFinished())
std::pair<std::string, MWWorld::Ptr> script;
while (localScripts.getNext(script))
{
std::pair<std::string, MWWorld::Ptr> script = localScripts.getNext();
MWScript::InterpreterContext interpreterContext (
&script.second.getRefData().getLocals(), script.second);
mEnvironment.getScriptManager()->run (script.first, interpreterContext);

View file

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

View file

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