]> git.localhorst.tv Git - l2e.git/blob - src/loader/Interpreter.cpp
parse scripts
[l2e.git] / src / loader / Interpreter.cpp
1 /*
2  * Interpreter.cpp
3  *
4  *  Created on: Aug 26, 2012
5  *      Author: holy
6  */
7
8 #include "Interpreter.h"
9
10 #include "ParsedSource.h"
11 #include "../battle/Hero.h"
12 #include "../battle/Monster.h"
13 #include "../battle/PartyLayout.h"
14 #include "../battle/Resources.h"
15 #include "../common/Ikari.h"
16 #include "../common/Item.h"
17 #include "../common/Spell.h"
18 #include "../common/Stats.h"
19 #include "../common/TargetingMode.h"
20 #include "../graphics/ComplexAnimation.h"
21 #include "../graphics/Font.h"
22 #include "../graphics/Frame.h"
23 #include "../graphics/Gauge.h"
24 #include "../graphics/Menu.h"
25 #include "../graphics/SimpleAnimation.h"
26 #include "../graphics/Sprite.h"
27
28 #include <algorithm>
29 #include <cstring>
30 #include <SDL_image.h>
31
32 using battle::Hero;
33 using battle::Monster;
34 using battle::PartyLayout;
35 using common::Ikari;
36 using common::Item;
37 using common::Spell;
38 using common::Stats;
39 using common::TargetingMode;
40 using graphics::Animation;
41 using graphics::Color;
42 using graphics::Font;
43 using graphics::Frame;
44 using graphics::Gauge;
45 using graphics::ComplexAnimation;
46 using graphics::SimpleAnimation;
47 using graphics::Sprite;
48 using geometry::Vector;
49 using std::make_pair;
50 using std::set;
51 using std::string;
52 using std::vector;
53
54 namespace loader {
55
56 Interpreter::~Interpreter() {
57         for (std::map<string, SDL_Surface *>::const_iterator i(imageCache.begin()), end(imageCache.end()); i != end; ++i) {
58                 SDL_FreeSurface(i->second);
59         }
60 }
61
62
63 const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) const {
64         return parsedDefinitions.at(identifier);
65 }
66
67
68 void *Interpreter::GetObject(int typeId, const std::string &name) {
69         std::map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
70         if (i != parsedDefinitions.end()) {
71                 const TypeDescription &requested(TypeDescription::Get(typeId));
72                 const TypeDescription &actual(TypeDescription::Get(i->second.type));
73                 if (requested.TypeId() == actual.TypeId()) {
74                         return values[actual.TypeId()][i->second.id];
75                 } else if (actual.IsSubtypeOf(requested)) {
76                         char *sub(reinterpret_cast<char *>(values[actual.TypeId()][i->second.id]));
77                         std::ptrdiff_t offset(actual.SupertypeOffset(requested));
78                         return sub - offset;
79                 } else {
80                         throw Error("cannot cast " + actual.TypeName() + " to " + requested.TypeName());
81                 }
82         } else {
83                 throw Error("access to undefined object " + name);
84         }
85 }
86
87
88 void Interpreter::ReadSource() {
89         for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
90                 ReadDefinition(source.GetDefinition(*i));
91         }
92 }
93
94 void Interpreter::ReadDefinition(const Definition &dfn) {
95         if (parsedDefinitions.find(dfn.Identifier()) != parsedDefinitions.end()) {
96                 return;
97         }
98         if (dfn.HasLiteralValue()) {
99                 ReadLiteral(dfn);
100         } else {
101                 ReadObject(dfn);
102         }
103 }
104
105 void Interpreter::ReadLiteral(const Definition &dfn) {
106         const string &typeName(dfn.GetLiteral()->IsArray() ? "Array" : dfn.GetLiteral()->GetTypeName());
107         int typeId(TypeDescription::GetTypeId(typeName));
108         int id(values[typeId].size());
109         const TypeDescription &td(TypeDescription::Get(typeId));
110         int size(
111                         (dfn.GetLiteral()->GetType() == Literal::PATH
112                                         || dfn.GetLiteral()->GetType() == Literal::STRING)
113                         ? dfn.GetLiteral()->GetString().size() : td.Size());
114         char *object(alloc.Alloc(size));
115         if (dfn.GetLiteral()->GetType() == Literal::OBJECT) {
116                 ReadObject(typeId, id, object, *dfn.GetLiteral()->GetProperties());
117         } else {
118                 ReadLiteral(typeId, id, object, *dfn.GetLiteral());
119         }
120         values[typeId].push_back(object);
121         parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
122 }
123
124 void Interpreter::ReadLiteral(int typeId, int id, char *object, const Literal &literal) {
125         switch (literal.GetType()) {
126                 case Literal::ARRAY_VALUES:
127                         throw Error("named value arrays are not supported, sorry");
128                         break;
129                 case Literal::ARRAY_PROPS:
130                         throw Error("named property list arrays are not supported, sorry");
131                         break;
132                 case Literal::BOOLEAN:
133                         new (object) bool(literal.GetBoolean());
134                         break;
135                 case Literal::COLOR:
136                         new (object) Color(literal.GetRed(), literal.GetGreen(), literal.GetBlue(), literal.GetAlpha());
137                         break;
138                 case Literal::NUMBER:
139                         new (object) int(literal.GetNumber());
140                         break;
141                 case Literal::PATH:
142                         std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
143                         object[literal.GetString().size()] = '\0';
144                         break;
145                 case Literal::SCRIPT:
146                         throw Error("script compiler not implemented");
147                 case Literal::STRING:
148                         std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
149                         object[literal.GetString().size()] = '\0';
150                         break;
151                 case Literal::VECTOR:
152                         new (object) Vector<int>(literal.GetX(), literal.GetY());
153                         break;
154                 case Literal::OBJECT:
155                         throw Error("illogical branch: read literal object as non-object literal");
156         }
157 }
158
159
160 void *Interpreter::GetObject(int typeId, const Value &v) {
161         if (v.IsLiteral()) {
162                 if (v.GetLiteral().IsObject()) {
163                         int typeId(TypeDescription::GetTypeId(v.GetLiteral().GetTypeName()));
164                         const TypeDescription &td(TypeDescription::Get(typeId));
165                         char *object(alloc.Alloc(td.Size()));
166                         td.Construct(object);
167                         int id(values[typeId].size());
168                         values[typeId].push_back(object);
169                         ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
170                         return object;
171                 } else {
172                         int typeId(0), id(0);
173                         switch (v.GetLiteral().GetType()) {
174                                 case Literal::ARRAY_VALUES:
175                                         throw Error("cannot copy value arrays, sorry");
176                                         break;
177                                 case Literal::ARRAY_PROPS:
178                                         throw Error("cannot copy property list arrays, sorry");
179                                         break;
180                                 case Literal::BOOLEAN:
181                                         {
182                                                 typeId = TypeDescription::GetTypeId("Boolean");
183                                                 id = values[typeId].size();
184                                                 const TypeDescription &td(TypeDescription::Get(typeId));
185                                                 char *object(alloc.Alloc(td.Size()));
186                                                 values[typeId].push_back(new (object) bool(v.GetLiteral().GetBoolean()));
187                                         }
188                                         break;
189                                 case Literal::COLOR:
190                                         {
191                                                 typeId = TypeDescription::GetTypeId("Color");
192                                                 id = values[typeId].size();
193                                                 const TypeDescription &td(TypeDescription::Get(typeId));
194                                                 char *object(alloc.Alloc(td.Size()));
195                                                 values[typeId].push_back(new (object) Color(v.GetLiteral().GetRed(), v.GetLiteral().GetGreen(), v.GetLiteral().GetBlue(), v.GetLiteral().GetAlpha()));
196                                         }
197                                         break;
198                                 case Literal::NUMBER:
199                                         {
200                                                 typeId = TypeDescription::GetTypeId("Number");
201                                                 id = values[typeId].size();
202                                                 const TypeDescription &td(TypeDescription::Get(typeId));
203                                                 char *object(alloc.Alloc(td.Size()));
204                                                 values[typeId].push_back(new (object) int(v.GetLiteral().GetNumber()));
205                                         }
206                                         break;
207                                 case Literal::PATH:
208                                         {
209                                                 typeId = TypeDescription::GetTypeId("Path");
210                                                 id = values[typeId].size();
211                                                 char *str(alloc.Alloc(v.GetLiteral().GetString().size() + 1));
212                                                 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
213                                                 str[v.GetLiteral().GetString().size()] = '\0';
214                                                 values[typeId].push_back(str);
215                                         }
216                                         break;
217                                 case Literal::SCRIPT:
218                                         throw Error("script compiler not implemented");
219                                 case Literal::STRING:
220                                         {
221                                                 typeId = TypeDescription::GetTypeId("String");
222                                                 id = values[typeId].size();
223                                                 char *str(alloc.Alloc(v.GetLiteral().GetString().size() + 1));
224                                                 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
225                                                 str[v.GetLiteral().GetString().size()] = '\0';
226                                                 values[typeId].push_back(str);
227                                         }
228                                         break;
229                                 case Literal::VECTOR:
230                                         {
231                                                 typeId = TypeDescription::GetTypeId("Vector");
232                                                 id = values[typeId].size();
233                                                 const TypeDescription &td(TypeDescription::Get(typeId));
234                                                 char *object(alloc.Alloc(td.Size()));
235                                                 values[typeId].push_back(new (object) Vector<int>(v.GetLiteral().GetX(), v.GetLiteral().GetY()));
236                                         }
237                                         break;
238                                 case Literal::OBJECT:
239                                         {
240                                                 typeId = TypeDescription::GetTypeId(v.GetLiteral().GetTypeName());
241                                                 const TypeDescription &td(TypeDescription::Get(typeId));
242                                                 id = values[typeId].size();
243                                                 char *object(alloc.Alloc(td.Size()));
244                                                 td.Construct(object);
245                                                 ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
246                                         }
247                                         break;
248                         }
249                         return values[typeId][id];
250                 }
251         } else {
252                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
253                 return GetObject(typeId, v.GetIdentifier());
254         }
255 }
256
257
258 void Interpreter::ReadObject(const Definition &dfn) {
259         int typeId(TypeDescription::GetTypeId(dfn.TypeName()));
260         const TypeDescription &td(TypeDescription::Get(typeId));
261         int id(values[typeId].size());
262         char *object(alloc.Alloc(td.Size()));
263         td.Construct(object);
264         values[typeId].push_back(object);
265         ReadObject(typeId, id, object, *dfn.GetProperties());
266         parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
267 }
268
269
270 void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyList &props) {
271         const TypeDescription &td(TypeDescription::Get(typeId));
272         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
273                 const FieldDescription &fd(td.GetField(i->first));
274                 const TypeDescription &fieldType(TypeDescription::Get(fd.TypeId()));
275                 if (CanLink(*i->second)) {
276                         char *dest(object + fd.Offset());
277                         if (fd.IsAggregate()) {
278                                 int arraySize(i->second->GetLiteral().ArraySize());
279                                 char *aggregate(alloc.Alloc(fieldType.Size() * arraySize));
280                                 char *iter(aggregate);
281                                 if (i->second->GetLiteral().GetType() == Literal::ARRAY_PROPS) {
282                                         const vector<PropertyList *> &list(i->second->GetLiteral().GetPropertyLists());
283                                         for (vector<PropertyList *>::const_iterator j(list.begin()), end(list.end()); j != end; ++j, iter += fieldType.Size()) {
284                                                 fieldType.Construct(iter);
285                                                 ReadObject(fieldType.TypeId(), -1, iter, **j);
286                                         }
287                                 } else {
288                                         const vector<Value *> &list(i->second->GetLiteral().GetValues());
289                                         for (vector<Value *>::const_iterator j(list.begin()), end(list.end()); j != end; ++j, iter += fieldType.Size()) {
290                                                 fieldType.Construct(iter);
291                                                 ReadLiteral(fieldType.TypeId(), -1, iter, (*j)->GetLiteral());
292                                         }
293                                 }
294                                 if (fd.IsReferenced()) {
295                                         std::memcpy(dest, &aggregate, sizeof(char *));
296                                         dest += sizeof(char *);
297                                         std::memcpy(dest, &arraySize, sizeof(int));
298                                 } else {
299                                         throw Error("aggregate type fields must be referenced");
300                                 }
301                         } else if (i->second->IsLiteral() && !fd.IsReferenced()) {
302                                 // inline literals
303                                 if (i->second->GetLiteral().IsObject()) {
304                                         ReadObject(fd.TypeId(), -1, dest, *i->second->GetLiteral().GetProperties());
305                                 } else {
306                                         ReadLiteral(fd.TypeId(), -1, dest, i->second->GetLiteral());
307                                 }
308                         } else {
309                                 char *src(reinterpret_cast<char *>(GetObject(fd.TypeId(), *i->second)));
310                                 if (fd.TypeId() == TypeDescription::GetTypeId("Image")) {
311                                         src = reinterpret_cast<char *>(GetImage(src));
312                                 }
313                                 if (fd.IsReferenced()) {
314                                         std::memcpy(dest, &src, sizeof(char *));
315                                 } else {
316                                         std::memcpy(dest, src, fieldType.Size());
317                                 }
318                         }
319                 } else {
320                         Postpone(typeId, id, fd.Offset(), i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced());
321                 }
322         }
323         td.Load(object);
324 }
325
326
327 SDL_Surface *Interpreter::GetImage(const string &path) {
328         std::map<string, SDL_Surface *>::const_iterator result(imageCache.find(path));
329         if (result != imageCache.end()) {
330                 return result->second;
331         } else {
332                 SDL_Surface *image(IMG_Load(path.c_str()));
333                 imageCache.insert(make_pair(path, image));
334                 return image;
335         }
336 }
337
338
339 bool Interpreter::CanLink(const Value &v) const {
340         return v.IsLiteral() || source.IsDefined(v.GetIdentifier());
341 }
342
343 void Interpreter::Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType, bool inlined) {
344         char *str(alloc.Alloc(identifier.size() + 1));
345         std::memcpy(str, identifier.c_str(), identifier.size());
346         str[identifier.size()] = '\0';
347         postponedDefinitions.push_back(PostponedDefinition(type, id, offset, str, linkedType, inlined));
348 }
349
350
351 void Interpreter::CreateTypeDescriptions() {
352         {
353                 TypeDescription &td(TypeDescription::CreateOrGet("Boolean"));
354                 td.SetDescription("Logical value which can be either true or false.");
355                 td.SetSize(sizeof(bool));
356         }
357         {
358                 TypeDescription &td(TypeDescription::CreateOrGet("Color"));
359                 td.SetDescription(
360                                 "A color in RGB format with an optional alpha channel.\n"
361                                 "Components range from 0 to 255.\n"
362                                 "Alpha defaults to 255 if omitted.");
363                 td.SetSize(sizeof(Color));
364         }
365         {
366                 TypeDescription &td(TypeDescription::CreateOrGet("Image"));
367                 td.SetDescription("Path to a PNG file with image data.");
368                 td.SetSize(sizeof(SDL_Surface));
369         }
370         {
371                 TypeDescription &td(TypeDescription::CreateOrGet("Number"));
372                 td.SetDescription("A signed integer.");
373                 td.SetSize(sizeof(int));
374         }
375         {
376                 int stringId(TypeDescription::GetTypeId("String"));
377                 TypeDescription &td(TypeDescription::CreateOrGet("Path"));
378                 td.SetDescription("A path in the filesystem which is interpreted relative to the source file's location.");
379                 td.SetSize(1);
380                 td.AddSupertype(stringId, 0);
381         }
382         {
383                 TypeDescription &td(TypeDescription::CreateOrGet("String"));
384                 td.SetDescription("Some characters.");
385                 td.SetSize(1);
386         }
387         {
388                 TypeDescription &td(TypeDescription::CreateOrGet("Vector"));
389                 td.SetDescription("A pair of numbers usually describing a 2D translation or offset.");
390                 td.SetSize(sizeof(Vector<int>));
391         }
392 }
393
394 }