From 6e88a625710c7936f87b38ecf6094472f3a49f4f Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sat, 13 Oct 2012 20:10:49 +0200 Subject: [PATCH] first scripting implementation --- Debug/src/common/subdir.mk | 6 ++ Release/src/common/subdir.mk | 6 ++ src/common/Script.cpp | 22 ++++++ src/common/Script.h | 46 +++++++++++++ src/common/ScriptHost.h | 27 ++++++++ src/common/ScriptRunner.cpp | 126 +++++++++++++++++++++++++++++++++++ src/common/ScriptRunner.h | 57 ++++++++++++++++ src/common/fwd.h | 3 + 8 files changed, 293 insertions(+) create mode 100644 src/common/Script.cpp create mode 100644 src/common/Script.h create mode 100644 src/common/ScriptHost.h create mode 100644 src/common/ScriptRunner.cpp create mode 100644 src/common/ScriptRunner.h diff --git a/Debug/src/common/subdir.mk b/Debug/src/common/subdir.mk index e0e6bd3..ee57f9b 100644 --- a/Debug/src/common/subdir.mk +++ b/Debug/src/common/subdir.mk @@ -10,6 +10,8 @@ CPP_SRCS += \ ../src/common/Ikari.cpp \ ../src/common/Inventory.cpp \ ../src/common/Item.cpp \ +../src/common/Script.cpp \ +../src/common/ScriptRunner.cpp \ ../src/common/Spell.cpp \ ../src/common/Stats.cpp \ ../src/common/TargetingMode.cpp @@ -21,6 +23,8 @@ OBJS += \ ./src/common/Ikari.o \ ./src/common/Inventory.o \ ./src/common/Item.o \ +./src/common/Script.o \ +./src/common/ScriptRunner.o \ ./src/common/Spell.o \ ./src/common/Stats.o \ ./src/common/TargetingMode.o @@ -32,6 +36,8 @@ CPP_DEPS += \ ./src/common/Ikari.d \ ./src/common/Inventory.d \ ./src/common/Item.d \ +./src/common/Script.d \ +./src/common/ScriptRunner.d \ ./src/common/Spell.d \ ./src/common/Stats.d \ ./src/common/TargetingMode.d diff --git a/Release/src/common/subdir.mk b/Release/src/common/subdir.mk index cc64cb3..c0683d3 100644 --- a/Release/src/common/subdir.mk +++ b/Release/src/common/subdir.mk @@ -10,6 +10,8 @@ CPP_SRCS += \ ../src/common/Ikari.cpp \ ../src/common/Inventory.cpp \ ../src/common/Item.cpp \ +../src/common/Script.cpp \ +../src/common/ScriptRunner.cpp \ ../src/common/Spell.cpp \ ../src/common/Stats.cpp \ ../src/common/TargetingMode.cpp @@ -21,6 +23,8 @@ OBJS += \ ./src/common/Ikari.o \ ./src/common/Inventory.o \ ./src/common/Item.o \ +./src/common/Script.o \ +./src/common/ScriptRunner.o \ ./src/common/Spell.o \ ./src/common/Stats.o \ ./src/common/TargetingMode.o @@ -32,6 +36,8 @@ CPP_DEPS += \ ./src/common/Ikari.d \ ./src/common/Inventory.d \ ./src/common/Item.d \ +./src/common/Script.d \ +./src/common/ScriptRunner.d \ ./src/common/Spell.d \ ./src/common/Stats.d \ ./src/common/TargetingMode.d diff --git a/src/common/Script.cpp b/src/common/Script.cpp new file mode 100644 index 0000000..1797c55 --- /dev/null +++ b/src/common/Script.cpp @@ -0,0 +1,22 @@ +/* + * Script.cpp + * + * Created on: Oct 13, 2012 + * Author: holy + */ + +#include "Script.h" + +namespace common { + +Script::Script() +: text(0) +, textlen(0) { + +} + +Script::~Script() { + +} + +} diff --git a/src/common/Script.h b/src/common/Script.h new file mode 100644 index 0000000..93bc531 --- /dev/null +++ b/src/common/Script.h @@ -0,0 +1,46 @@ +/* + * Script.h + * + * Created on: Oct 13, 2012 + * Author: holy + */ + +#ifndef COMMON_SCRIPT_H_ +#define COMMON_SCRIPT_H_ + +namespace common { + +class Script { + +public: + Script(); + ~Script(); + + enum Code { + CODE_MOVE_A0, + CODE_MOVE_A1, + CODE_MOVE_I0, + CODE_MOVE_I1, + CODE_MOVE_V0, + CODE_MOVE_V1, + + CODE_ADD_I0, + CODE_ADD_I1, + CODE_ADD_V0, + CODE_ADD_V1, + + CODE_RAND_I0, + CODE_RAND_I1, + + CODE_SYSCALL, + }; + +public: + const unsigned char *text; + int textlen; + +}; + +} + +#endif /* COMMON_SCRIPT_H_ */ diff --git a/src/common/ScriptHost.h b/src/common/ScriptHost.h new file mode 100644 index 0000000..5b44b7e --- /dev/null +++ b/src/common/ScriptHost.h @@ -0,0 +1,27 @@ +/* + * ScriptHost.h + * + * Created on: Oct 13, 2012 + * Author: holy + */ + +#ifndef COMMON_SCRIPTHOST_H_ +#define COMMON_SCRIPTHOST_H_ + +#include "fwd.h" + +namespace common { + +class ScriptHost { + +public: + virtual ~ScriptHost() { } + +public: + virtual void HandleSyscall(ScriptRunner &) = 0; + +}; + +} + +#endif /* COMMON_SCRIPTHOST_H_ */ diff --git a/src/common/ScriptRunner.cpp b/src/common/ScriptRunner.cpp new file mode 100644 index 0000000..110b702 --- /dev/null +++ b/src/common/ScriptRunner.cpp @@ -0,0 +1,126 @@ +/* + * ScriptRunner.cpp + * + * Created on: Oct 13, 2012 + * Author: holy + */ + +#include "ScriptRunner.h" + +#include "Script.h" +#include "ScriptHost.h" + +#include + +using geometry::Vector; + +namespace common { + +ScriptRunner::ScriptRunner() +: host(0) +, script(0) +, cursor(0) +, address0(0) +, address1(0) +, integer0(0) +, integer1(0) +, vector0(0, 0) +, vector1(0, 0) { + +} + +ScriptRunner::~ScriptRunner() { + +} + + +bool ScriptRunner::Running() const { + return script && cursor < script->textlen; +} + +void ScriptRunner::Run(ScriptHost &h, const Script &s) { + host = &h; + script = &s; + Reset(); + while (cursor < script->textlen) { + unsigned char code(script->text[cursor]); + ++cursor; + Exec(code); + } + host = 0; + script = 0; +} + +void ScriptRunner::Reset() { + cursor = 0; + address0 = 0; + address1 = 0; + integer0 = 0; + integer1 = 0; + vector0 = Vector(0, 0); + vector1 = Vector(0, 0); +} + +void ScriptRunner::Exec(unsigned char code) { + switch (code) { + case Script::CODE_MOVE_A0: + address0 = PopAddress(); + break; + case Script::CODE_MOVE_A1: + address1 = PopAddress(); + break; + case Script::CODE_MOVE_I0: + integer0 = PopInt(); + break; + case Script::CODE_MOVE_I1: + integer1 = PopInt(); + break; + case Script::CODE_MOVE_V0: + vector0 = PopVector(); + break; + case Script::CODE_MOVE_V1: + vector1 = PopVector(); + break; + case Script::CODE_ADD_I0: + integer0 += PopInt(); + break; + case Script::CODE_ADD_I1: + integer1 += PopInt(); + break; + case Script::CODE_ADD_V0: + vector0 += PopVector(); + break; + case Script::CODE_ADD_V1: + vector1 += PopVector(); + break; + case Script::CODE_RAND_I0: + integer0 = std::rand(); + break; + case Script::CODE_RAND_I1: + integer1 = std::rand(); + break; + case Script::CODE_SYSCALL: + host->HandleSyscall(*this); + break; + } +} + +void *ScriptRunner::PopAddress() { + void *const *addr(reinterpret_cast(script->text + cursor)); + cursor += sizeof(void *); + return *addr; +} + +int ScriptRunner::PopInt() { + const int *i(reinterpret_cast(script->text + cursor)); + cursor += sizeof(int); + return *i; +} + +const Vector &ScriptRunner::PopVector() { + const Vector *vec(reinterpret_cast *>(script->text + cursor)); + cursor += sizeof(Vector); + return *vec; +} + +} diff --git a/src/common/ScriptRunner.h b/src/common/ScriptRunner.h new file mode 100644 index 0000000..4751908 --- /dev/null +++ b/src/common/ScriptRunner.h @@ -0,0 +1,57 @@ +/* + * ScriptRunner.h + * + * Created on: Oct 13, 2012 + * Author: holy + */ + +#ifndef COMMON_SCRIPTRUNNER_H_ +#define COMMON_SCRIPTRUNNER_H_ + +#include "fwd.h" +#include "../geometry/Vector.h" + +namespace common { + +class ScriptRunner { + +public: + ScriptRunner(); + ~ScriptRunner(); + +public: + void Run(ScriptHost &, const Script &); + bool Running() const; + + void *Address0() const { return address0; } + void *Address1() const { return address1; } + int Integer0() const { return integer0; } + int Integer1() const { return integer1; } + const geometry::Vector &Vector0() const { return vector0; } + const geometry::Vector &Vector1() const { return vector1; } + +private: + void Reset(); + void Exec(unsigned char code); + + void *PopAddress(); + int PopInt(); + const geometry::Vector &PopVector(); + +private: + ScriptHost *host; + const Script *script; + int cursor; + + void *address0; + void *address1; + int integer0; + int integer1; + geometry::Vector vector0; + geometry::Vector vector1; + +}; + +} + +#endif /* COMMON_SCRIPTRUNNER_H_ */ diff --git a/src/common/fwd.h b/src/common/fwd.h index 053307e..1cd0671 100644 --- a/src/common/fwd.h +++ b/src/common/fwd.h @@ -17,6 +17,9 @@ class HeroGroup; class Ikari; class Inventory; class Item; +class Script; +class ScriptHost; +class ScriptRunner; class Spell; class Stats; class TargetingMode; -- 2.39.2