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