]> git.localhorst.tv Git - l2e.git/blob - src/common/ScriptRunner.cpp
first scripting implementation
[l2e.git] / src / common / ScriptRunner.cpp
1 /*
2  * ScriptRunner.cpp
3  *
4  *  Created on: Oct 13, 2012
5  *      Author: holy
6  */
7
8 #include "ScriptRunner.h"
9
10 #include "Script.h"
11 #include "ScriptHost.h"
12
13 #include <cstdlib>
14
15 using geometry::Vector;
16
17 namespace common {
18
19 ScriptRunner::ScriptRunner()
20 : host(0)
21 , script(0)
22 , cursor(0)
23 , address0(0)
24 , address1(0)
25 , integer0(0)
26 , integer1(0)
27 , vector0(0, 0)
28 , vector1(0, 0) {
29
30 }
31
32 ScriptRunner::~ScriptRunner() {
33
34 }
35
36
37 bool ScriptRunner::Running() const {
38         return script && cursor < script->textlen;
39 }
40
41 void ScriptRunner::Run(ScriptHost &h, const Script &s) {
42         host = &h;
43         script = &s;
44         Reset();
45         while (cursor < script->textlen) {
46                 unsigned char code(script->text[cursor]);
47                 ++cursor;
48                 Exec(code);
49         }
50         host = 0;
51         script = 0;
52 }
53
54 void ScriptRunner::Reset() {
55         cursor = 0;
56         address0 = 0;
57         address1 = 0;
58         integer0 = 0;
59         integer1 = 0;
60         vector0 = Vector<int>(0, 0);
61         vector1 = Vector<int>(0, 0);
62 }
63
64 void ScriptRunner::Exec(unsigned char code) {
65         switch (code) {
66                 case Script::CODE_MOVE_A0:
67                         address0 = PopAddress();
68                         break;
69                 case Script::CODE_MOVE_A1:
70                         address1 = PopAddress();
71                         break;
72                 case Script::CODE_MOVE_I0:
73                         integer0 = PopInt();
74                         break;
75                 case Script::CODE_MOVE_I1:
76                         integer1 = PopInt();
77                         break;
78                 case Script::CODE_MOVE_V0:
79                         vector0 = PopVector();
80                         break;
81                 case Script::CODE_MOVE_V1:
82                         vector1 = PopVector();
83                         break;
84                 case Script::CODE_ADD_I0:
85                         integer0 += PopInt();
86                         break;
87                 case Script::CODE_ADD_I1:
88                         integer1 += PopInt();
89                         break;
90                 case Script::CODE_ADD_V0:
91                         vector0 += PopVector();
92                         break;
93                 case Script::CODE_ADD_V1:
94                         vector1 += PopVector();
95                         break;
96                 case Script::CODE_RAND_I0:
97                         integer0 = std::rand();
98                         break;
99                 case Script::CODE_RAND_I1:
100                         integer1 = std::rand();
101                         break;
102                 case Script::CODE_SYSCALL:
103                         host->HandleSyscall(*this);
104                         break;
105         }
106 }
107
108 void *ScriptRunner::PopAddress() {
109         void *const *addr(reinterpret_cast<void *const *>(script->text + cursor));
110         cursor += sizeof(void *);
111         return *addr;
112 }
113
114 int ScriptRunner::PopInt() {
115         const int *i(reinterpret_cast<const int *>(script->text + cursor));
116         cursor += sizeof(int);
117         return *i;
118 }
119
120 const Vector<int> &ScriptRunner::PopVector() {
121         const Vector<int> *vec(reinterpret_cast<const Vector<int> *>(script->text + cursor));
122         cursor += sizeof(Vector<int>);
123         return *vec;
124 }
125
126 }