2011-10-06 10:29:59 +00:00
|
|
|
#include "localscripts.hpp"
|
|
|
|
|
2018-08-14 19:05:43 +00:00
|
|
|
#include <components/debug/debuglog.hpp>
|
2015-01-25 00:53:20 +00:00
|
|
|
|
2012-10-01 15:17:04 +00:00
|
|
|
#include "esmstore.hpp"
|
2012-06-29 14:48:50 +00:00
|
|
|
#include "cellstore.hpp"
|
2013-01-12 18:31:57 +00:00
|
|
|
#include "class.hpp"
|
|
|
|
#include "containerstore.hpp"
|
|
|
|
|
2011-10-07 07:52:42 +00:00
|
|
|
namespace
|
|
|
|
{
|
2015-12-06 19:30:52 +00:00
|
|
|
|
|
|
|
struct AddScriptsVisitor
|
2011-10-07 07:52:42 +00:00
|
|
|
{
|
2015-12-06 19:30:52 +00:00
|
|
|
AddScriptsVisitor(MWWorld::LocalScripts& scripts)
|
|
|
|
: mScripts(scripts)
|
2011-10-07 07:52:42 +00:00
|
|
|
{
|
|
|
|
}
|
2015-12-06 19:30:52 +00:00
|
|
|
MWWorld::LocalScripts& mScripts;
|
2013-01-12 18:31:57 +00:00
|
|
|
|
2015-12-06 19:30:52 +00:00
|
|
|
bool operator()(const MWWorld::Ptr& ptr)
|
2013-01-12 18:31:57 +00:00
|
|
|
{
|
2015-12-06 19:30:52 +00:00
|
|
|
if (ptr.getRefData().isDeleted())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
std::string script = ptr.getClass().getScript(ptr);
|
|
|
|
|
|
|
|
if (!script.empty())
|
|
|
|
mScripts.add(script, ptr);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2013-12-05 12:21:26 +00:00
|
|
|
|
2015-12-06 19:30:52 +00:00
|
|
|
struct AddContainerItemScriptsVisitor
|
|
|
|
{
|
|
|
|
AddContainerItemScriptsVisitor(MWWorld::LocalScripts& scripts)
|
|
|
|
: mScripts(scripts)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
MWWorld::LocalScripts& mScripts;
|
2013-12-05 12:21:26 +00:00
|
|
|
|
2015-12-06 19:30:52 +00:00
|
|
|
bool operator()(const MWWorld::Ptr& containerPtr)
|
|
|
|
{
|
2018-12-03 16:21:40 +00:00
|
|
|
// Ignore containers without generated content
|
|
|
|
if (containerPtr.getTypeName() == typeid(ESM::Container).name() &&
|
|
|
|
containerPtr.getRefData().getCustomData() == nullptr)
|
2020-10-13 15:46:32 +00:00
|
|
|
return true;
|
2018-12-03 16:21:40 +00:00
|
|
|
|
2014-05-22 18:37:22 +00:00
|
|
|
MWWorld::ContainerStore& container = containerPtr.getClass().getContainerStore(containerPtr);
|
2015-12-06 19:30:52 +00:00
|
|
|
for(MWWorld::ContainerStoreIterator it = container.begin(); it != container.end(); ++it)
|
2013-01-12 18:31:57 +00:00
|
|
|
{
|
2015-12-06 19:30:52 +00:00
|
|
|
std::string script = it->getClass().getScript(*it);
|
2013-01-12 18:31:57 +00:00
|
|
|
if(script != "")
|
|
|
|
{
|
2015-12-06 19:30:52 +00:00
|
|
|
MWWorld::Ptr item = *it;
|
|
|
|
item.mCell = containerPtr.getCell();
|
|
|
|
mScripts.add (script, item);
|
2013-01-12 18:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-06 19:30:52 +00:00
|
|
|
return true;
|
2013-01-12 18:31:57 +00:00
|
|
|
}
|
2015-12-06 19:30:52 +00:00
|
|
|
};
|
|
|
|
|
2011-10-07 07:52:42 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 14:22:19 +00:00
|
|
|
MWWorld::LocalScripts::LocalScripts (const MWWorld::ESMStore& store) : mStore (store)
|
|
|
|
{
|
|
|
|
mIter = mScripts.end();
|
|
|
|
}
|
2011-10-07 07:52:42 +00:00
|
|
|
|
2011-10-06 10:29:59 +00:00
|
|
|
void MWWorld::LocalScripts::startIteration()
|
|
|
|
{
|
|
|
|
mIter = mScripts.begin();
|
|
|
|
}
|
|
|
|
|
2016-02-03 15:09:20 +00:00
|
|
|
bool MWWorld::LocalScripts::getNext(std::pair<std::string, Ptr>& script)
|
2011-10-06 10:29:59 +00:00
|
|
|
{
|
2021-01-09 10:18:38 +00:00
|
|
|
if (mIter!=mScripts.end())
|
2011-10-06 10:29:59 +00:00
|
|
|
{
|
2016-02-03 15:09:20 +00:00
|
|
|
std::list<std::pair<std::string, Ptr> >::iterator iter = mIter++;
|
2016-02-26 11:59:35 +00:00
|
|
|
script = *iter;
|
|
|
|
return true;
|
2011-10-06 10:29:59 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MWWorld::LocalScripts::add (const std::string& scriptName, const Ptr& ptr)
|
|
|
|
{
|
2015-05-27 17:45:26 +00:00
|
|
|
if (const ESM::Script *script = mStore.get<ESM::Script>().search (scriptName))
|
2011-10-07 07:52:42 +00:00
|
|
|
{
|
2014-09-21 10:43:19 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
ptr.getRefData().setLocals (*script);
|
2011-10-07 07:52:42 +00:00
|
|
|
|
2016-02-03 15:06:25 +00:00
|
|
|
for (std::list<std::pair<std::string, Ptr> >::iterator iter = mScripts.begin(); iter!=mScripts.end(); ++iter)
|
|
|
|
if (iter->second==ptr)
|
|
|
|
{
|
2018-08-14 19:05:43 +00:00
|
|
|
Log(Debug::Warning) << "Error: tried to add local script twice for " << ptr.getCellRef().getRefId();
|
2016-02-03 15:06:25 +00:00
|
|
|
remove(ptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-10-17 08:26:35 +00:00
|
|
|
mScripts.emplace_back (scriptName, ptr);
|
2014-09-21 10:43:19 +00:00
|
|
|
}
|
|
|
|
catch (const std::exception& exception)
|
|
|
|
{
|
2018-08-14 19:05:43 +00:00
|
|
|
Log(Debug::Error)
|
2014-09-21 10:43:19 +00:00
|
|
|
<< "failed to add local script " << scriptName
|
2018-08-14 19:05:43 +00:00
|
|
|
<< " because an exception has been thrown: " << exception.what();
|
2014-09-21 10:43:19 +00:00
|
|
|
}
|
2011-10-07 07:52:42 +00:00
|
|
|
}
|
2015-05-27 17:45:26 +00:00
|
|
|
else
|
2018-08-14 19:05:43 +00:00
|
|
|
Log(Debug::Warning)
|
2015-05-27 17:45:26 +00:00
|
|
|
<< "failed to add local script " << scriptName
|
2018-08-14 19:05:43 +00:00
|
|
|
<< " because the script does not exist.";
|
2011-10-07 07:52:42 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 12:21:26 +00:00
|
|
|
void MWWorld::LocalScripts::addCell (CellStore *cell)
|
2011-10-07 07:52:42 +00:00
|
|
|
{
|
2015-12-06 19:30:52 +00:00
|
|
|
AddScriptsVisitor addScriptsVisitor(*this);
|
|
|
|
cell->forEach(addScriptsVisitor);
|
|
|
|
|
|
|
|
AddContainerItemScriptsVisitor addContainerItemScriptsVisitor(*this);
|
|
|
|
cell->forEachType<ESM::NPC>(addContainerItemScriptsVisitor);
|
|
|
|
cell->forEachType<ESM::Creature>(addContainerItemScriptsVisitor);
|
|
|
|
cell->forEachType<ESM::Container>(addContainerItemScriptsVisitor);
|
2011-10-06 10:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MWWorld::LocalScripts::clear()
|
|
|
|
{
|
|
|
|
mScripts.clear();
|
|
|
|
}
|
|
|
|
|
2013-12-05 12:21:26 +00:00
|
|
|
void MWWorld::LocalScripts::clearCell (CellStore *cell)
|
2011-10-06 10:29:59 +00:00
|
|
|
{
|
|
|
|
std::list<std::pair<std::string, Ptr> >::iterator iter = mScripts.begin();
|
|
|
|
|
|
|
|
while (iter!=mScripts.end())
|
|
|
|
{
|
2013-01-13 19:49:56 +00:00
|
|
|
if (iter->second.mCell==cell)
|
2011-10-06 10:29:59 +00:00
|
|
|
{
|
|
|
|
if (iter==mIter)
|
|
|
|
++mIter;
|
|
|
|
|
|
|
|
mScripts.erase (iter++);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-13 17:05:12 +00:00
|
|
|
void MWWorld::LocalScripts::remove (RefData *ref)
|
|
|
|
{
|
|
|
|
for (std::list<std::pair<std::string, Ptr> >::iterator iter = mScripts.begin();
|
|
|
|
iter!=mScripts.end(); ++iter)
|
|
|
|
if (&(iter->second.getRefData()) == ref)
|
|
|
|
{
|
|
|
|
if (iter==mIter)
|
|
|
|
++mIter;
|
|
|
|
|
|
|
|
mScripts.erase (iter);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-06 10:29:59 +00:00
|
|
|
void MWWorld::LocalScripts::remove (const Ptr& ptr)
|
|
|
|
{
|
|
|
|
for (std::list<std::pair<std::string, Ptr> >::iterator iter = mScripts.begin();
|
|
|
|
iter!=mScripts.end(); ++iter)
|
|
|
|
if (iter->second==ptr)
|
|
|
|
{
|
|
|
|
if (iter==mIter)
|
|
|
|
++mIter;
|
|
|
|
|
|
|
|
mScripts.erase (iter);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|