1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-26 09:35:28 +00:00
OpenMW/apps/openmw/mwscript/globalscripts.cpp

94 lines
2.4 KiB
C++
Raw Normal View History

2010-07-04 10:43:34 +02:00
#include "globalscripts.hpp"
#include <cassert>
#include <components/misc/stringops.hpp>
2012-10-01 19:17:04 +04:00
#include "../mwworld/esmstore.hpp"
#include "../mwbase/environment.hpp"
#include "../mwbase/scriptmanager.hpp"
2010-07-04 10:43:34 +02:00
#include "interpretercontext.hpp"
namespace MWScript
{
2012-10-01 19:17:04 +04:00
GlobalScripts::GlobalScripts (const MWWorld::ESMStore& store)
: mStore (store)
2010-07-04 10:43:34 +02:00
{
addStartup();
2010-07-04 10:43:34 +02:00
}
void GlobalScripts::addScript (const std::string& name)
{
std::map<std::string, std::pair<bool, Locals> >::iterator iter =
mScripts.find (Misc::StringUtils::lowerCase (name));
if (iter==mScripts.end())
{
if (const ESM::Script *script = mStore.get<ESM::Script>().find (name))
{
2010-07-04 10:43:34 +02:00
Locals locals;
2010-07-04 10:43:34 +02:00
locals.configure (*script);
mScripts.insert (std::make_pair (name, std::make_pair (true, locals)));
2010-07-04 10:43:34 +02:00
}
}
else
iter->second.first = true;
2010-07-04 10:43:34 +02:00
}
void GlobalScripts::removeScript (const std::string& name)
2010-07-04 10:43:34 +02:00
{
std::map<std::string, std::pair<bool, Locals> >::iterator iter =
mScripts.find (Misc::StringUtils::lowerCase (name));
if (iter!=mScripts.end())
iter->second.first = false;
}
bool GlobalScripts::isRunning (const std::string& name) const
{
std::map<std::string, std::pair<bool, Locals> >::const_iterator iter =
mScripts.find (Misc::StringUtils::lowerCase (name));
if (iter==mScripts.end())
return false;
return iter->second.first;
}
void GlobalScripts::run()
{
for (std::map<std::string, std::pair<bool, Locals> >::iterator iter (mScripts.begin());
iter!=mScripts.end(); ++iter)
2010-07-04 10:43:34 +02:00
{
if (iter->second.first)
{
MWScript::InterpreterContext interpreterContext (
&iter->second.second, MWWorld::Ptr());
MWBase::Environment::get().getScriptManager()->run (iter->first, interpreterContext);
}
2010-07-04 10:43:34 +02:00
}
}
void GlobalScripts::clear()
{
mScripts.clear();
}
void GlobalScripts::addStartup()
{
addScript ("main");
for (MWWorld::Store<ESM::StartScript>::iterator iter =
mStore.get<ESM::StartScript>().begin();
iter != mStore.get<ESM::StartScript>().end(); ++iter)
{
addScript (iter->mScript);
}
}
2010-07-04 10:43:34 +02:00
}