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