implemented local script execution

actorid
Marc Zinnschlag 15 years ago
parent 8e2732c60e
commit 474b412b47

@ -38,12 +38,14 @@ set(GAMEINPUT_HEADER
source_group(apps\\openmw\\mwinput FILES ${GAMEINPUT} ${GAMEINPUT_HEADER})
set(GAMESCRIPT
apps/openmw/mwscript/scriptmanager.cpp)
apps/openmw/mwscript/scriptmanager.cpp
apps/openmw/mwscript/interpretercontext.cpp)
set(GAMESCRIPT_HEADER
apps/openmw/mwscript/locals.hpp
apps/openmw/mwscript/scriptmanager.hpp
apps/openmw/mwscript/compilercontext.hpp
apps/openmw/mwscript/compilercontextscript.hpp)
apps/openmw/mwscript/compilercontextscript.hpp
apps/openmw/mwscript/interpretercontext.hpp)
source_group(apps\\openmw\\mwscript FILES ${GAMESCRIPT} ${GAMESCRIPT_HEADER})
set(APPS ${GAME} ${GAMEREND} ${GAMEINPUT} ${GAMESCRIPT})

@ -10,6 +10,7 @@
#include "mwinput/inputmanager.hpp"
#include "mwscript/scriptmanager.hpp"
#include "mwscript/compilercontextscript.hpp"
#include "mwscript/interpretercontext.hpp"
#include "world.hpp"
@ -18,7 +19,8 @@ void OMW::Engine::executeLocalScripts()
for (World::ScriptList::const_iterator iter (mWorld->getLocalScripts().begin());
iter!=mWorld->getLocalScripts().end(); ++iter)
{
mScriptManager->run (iter->first, *iter->second);
MWScript::InterpreterContext interpreterContext (*mWorld, iter->second);
mScriptManager->run (iter->first, interpreterContext);
}
}

@ -0,0 +1,72 @@
#include "interpretercontext.hpp"
#include <stdexcept>
#include <iostream>
#include "locals.hpp"
namespace MWScript
{
InterpreterContext::InterpreterContext (OMW::World& world, MWScript::Locals *locals)
: mWorld (world), mLocals (locals)
{}
int InterpreterContext::getLocalShort (int index) const
{
if (!mLocals)
throw std::runtime_error ("local variables not available in this context");
return mLocals->mShorts.at (index);
}
int InterpreterContext::getLocalLong (int index) const
{
if (!mLocals)
throw std::runtime_error ("local variables not available in this context");
return mLocals->mLongs.at (index);
}
float InterpreterContext::getLocalFloat (int index) const
{
if (!mLocals)
throw std::runtime_error ("local variables not available in this context");
return mLocals->mFloats.at (index);
}
void InterpreterContext::setLocalShort (int index, int value)
{
if (!mLocals)
throw std::runtime_error ("local variables not available in this context");
mLocals->mShorts.at (index) = value;
}
void InterpreterContext::setLocalLong (int index, int value)
{
if (!mLocals)
throw std::runtime_error ("local variables not available in this context");
mLocals->mLongs.at (index) = value;
}
void InterpreterContext::setLocalFloat (int index, float value)
{
if (!mLocals)
throw std::runtime_error ("local variables not available in this context");
mLocals->mFloats.at (index) = value;
}
void InterpreterContext::messageBox (const std::string& message,
const std::vector<std::string>& buttons)
{
std::cout << "message box: " << message << std::endl;
if (!buttons.empty())
std::cerr << "error: message box buttons not supported" << std::endl;
}
}

@ -0,0 +1,44 @@
#ifndef GAME_SCRIPT_INTERPRETERCONTEXT_H
#define GAME_SCRIPT_INTERPRETERCONTEXT_H
#include <components/interpreter/context.hpp>
namespace OMW
{
class World;
}
namespace MWScript
{
struct Locals;
class InterpreterContext : public Interpreter::Context
{
OMW::World& mWorld;
Locals *mLocals;
public:
InterpreterContext (OMW::World& world, MWScript::Locals *locals);
///< The ownership of \a locals is not transferred. 0-pointer allowed.
virtual int getLocalShort (int index) const;
virtual int getLocalLong (int index) const;
virtual float getLocalFloat (int index) const;
virtual void setLocalShort (int index, int value);
virtual void setLocalLong (int index, int value);
virtual void setLocalFloat (int index, float value);
virtual void messageBox (const std::string& message,
const std::vector<std::string>& buttons);
};
}
#endif

@ -1,8 +1,6 @@
#include "scriptmanager.hpp"
#include <components/compiler/scanner.hpp>
#include <cassert>
#include <iostream>
#include <sstream>
@ -11,6 +9,12 @@
#include <components/esm/loadscpt.hpp>
#include <components/esm_store/store.hpp>
#include <components/compiler/scanner.hpp>
#include <components/interpreter/installopcodes.hpp>
#include <components/interpreter/interpreter.hpp>
namespace MWScript
{
ScriptManager::ScriptManager (const ESMS::ESMStore& store, bool verbose,
@ -70,8 +74,7 @@ namespace MWScript
return false;
}
void ScriptManager::run (const std::string& name/*,
Interpreter::Context& interpreterContext*/, Locals& locals)
void ScriptManager::run (const std::string& name, Interpreter::Context& interpreterContext)
{
// compile script
std::map<std::string, std::vector<Interpreter::Type_Code> >::iterator iter =
@ -92,7 +95,18 @@ namespace MWScript
}
// execute script
if (!iter->second.empty())
try
{
Interpreter::Interpreter interpreter (interpreterContext);
Interpreter::installOpcodes (interpreter);
interpreter.run (&iter->second[0], iter->second.size());
}
catch (...)
{
std::cerr << "exeution of script " << name << " failed." << std::endl;
iter->second.clear(); // don't execute again.
}
}
}

@ -26,9 +26,7 @@ namespace Interpreter
}
namespace MWScript
{
struct Locals;
{
class ScriptManager
{
Compiler::StreamErrorHandler mErrorHandler;
@ -46,8 +44,7 @@ namespace MWScript
ScriptManager (const ESMS::ESMStore& store, bool verbose,
Compiler::Context& compilerContext);
void run (const std::string& name/*,
Interpreter::Context& interpreterContext*/, Locals& locals);
void run (const std::string& name, Interpreter::Context& interpreterContext);
};
};

@ -1,6 +1,9 @@
#ifndef INTERPRETER_CONTEXT_H_INCLUDED
#define INTERPRETER_CONTEXT_H_INCLUDED
#include <string>
#include <vector>
namespace Interpreter
{
class Context

Loading…
Cancel
Save