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