]> git.localhorst.tv Git - l2e.git/blob - src/loader/Interpreter.cpp
better allocation in 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/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::STRING:
146                         std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
147                         object[literal.GetString().size()] = '\0';
148                         break;
149                 case Literal::VECTOR:
150                         new (object) Vector<int>(literal.GetX(), literal.GetY());
151                         break;
152                 case Literal::OBJECT:
153                         throw Error("illogical branch: read literal object as non-object literal");
154                         break;
155         }
156 }
157
158
159 void *Interpreter::GetObject(int typeId, const Value &v) {
160         if (v.IsLiteral()) {
161                 if (v.GetLiteral().IsObject()) {
162                         int typeId(TypeDescription::GetTypeId(v.GetLiteral().GetTypeName()));
163                         const TypeDescription &td(TypeDescription::Get(typeId));
164                         char *object(alloc.Alloc(td.Size()));
165                         td.Construct(object);
166                         int id(values[typeId].size());
167                         values[typeId].push_back(object);
168                         ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
169                         return object;
170                 } else {
171                         int typeId(0), id(0);
172                         switch (v.GetLiteral().GetType()) {
173                                 case Literal::ARRAY_VALUES:
174                                         throw Error("cannot copy value arrays, sorry");
175                                         break;
176                                 case Literal::ARRAY_PROPS:
177                                         throw Error("cannot copy property list arrays, sorry");
178                                         break;
179                                 case Literal::BOOLEAN:
180                                         {
181                                                 typeId = TypeDescription::GetTypeId("Boolean");
182                                                 id = values[typeId].size();
183                                                 const TypeDescription &td(TypeDescription::Get(typeId));
184                                                 char *object(alloc.Alloc(td.Size()));
185                                                 values[typeId].push_back(new (object) bool(v.GetLiteral().GetBoolean()));
186                                         }
187                                         break;
188                                 case Literal::COLOR:
189                                         {
190                                                 typeId = TypeDescription::GetTypeId("Color");
191                                                 id = values[typeId].size();
192                                                 const TypeDescription &td(TypeDescription::Get(typeId));
193                                                 char *object(alloc.Alloc(td.Size()));
194                                                 values[typeId].push_back(new (object) Color(v.GetLiteral().GetRed(), v.GetLiteral().GetGreen(), v.GetLiteral().GetBlue(), v.GetLiteral().GetAlpha()));
195                                         }
196                                         break;
197                                 case Literal::NUMBER:
198                                         {
199                                                 typeId = TypeDescription::GetTypeId("Number");
200                                                 id = values[typeId].size();
201                                                 const TypeDescription &td(TypeDescription::Get(typeId));
202                                                 char *object(alloc.Alloc(td.Size()));
203                                                 values[typeId].push_back(new (object) int(v.GetLiteral().GetNumber()));
204                                         }
205                                         break;
206                                 case Literal::PATH:
207                                         {
208                                                 typeId = TypeDescription::GetTypeId("Path");
209                                                 id = values[typeId].size();
210                                                 char *str(alloc.Alloc(v.GetLiteral().GetString().size() + 1));
211                                                 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
212                                                 str[v.GetLiteral().GetString().size()] = '\0';
213                                                 values[typeId].push_back(str);
214                                         }
215                                         break;
216                                 case Literal::STRING:
217                                         {
218                                                 typeId = TypeDescription::GetTypeId("String");
219                                                 id = values[typeId].size();
220                                                 char *str(alloc.Alloc(v.GetLiteral().GetString().size() + 1));
221                                                 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
222                                                 str[v.GetLiteral().GetString().size()] = '\0';
223                                                 values[typeId].push_back(str);
224                                         }
225                                         break;
226                                 case Literal::VECTOR:
227                                         {
228                                                 typeId = TypeDescription::GetTypeId("Vector");
229                                                 id = values[typeId].size();
230                                                 const TypeDescription &td(TypeDescription::Get(typeId));
231                                                 char *object(alloc.Alloc(td.Size()));
232                                                 values[typeId].push_back(new (object) Vector<int>(v.GetLiteral().GetX(), v.GetLiteral().GetY()));
233                                         }
234                                         break;
235                                 case Literal::OBJECT:
236                                         {
237                                                 typeId = TypeDescription::GetTypeId(v.GetLiteral().GetTypeName());
238                                                 const TypeDescription &td(TypeDescription::Get(typeId));
239                                                 id = values[typeId].size();
240                                                 char *object(alloc.Alloc(td.Size()));
241                                                 td.Construct(object);
242                                                 ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
243                                         }
244                                         break;
245                         }
246                         return values[typeId][id];
247                 }
248         } else {
249                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
250                 return GetObject(typeId, v.GetIdentifier());
251         }
252 }
253
254
255 void Interpreter::ReadObject(const Definition &dfn) {
256         int typeId(TypeDescription::GetTypeId(dfn.TypeName()));
257         const TypeDescription &td(TypeDescription::Get(typeId));
258         int id(values[typeId].size());
259         char *object(alloc.Alloc(td.Size()));
260         td.Construct(object);
261         values[typeId].push_back(object);
262         ReadObject(typeId, id, object, *dfn.GetProperties());
263         parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
264 }
265
266
267 void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyList &props) {
268         const TypeDescription &td(TypeDescription::Get(typeId));
269         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
270                 const FieldDescription &fd(td.GetField(i->first));
271                 const TypeDescription &fieldType(TypeDescription::Get(fd.TypeId()));
272                 if (CanLink(*i->second)) {
273                         char *dest(object + fd.Offset());
274                         if (fd.IsAggregate()) {
275                                 int arraySize(i->second->GetLiteral().ArraySize());
276                                 char *aggregate(alloc.Alloc(fieldType.Size() * arraySize));
277                                 char *iter(aggregate);
278                                 if (i->second->GetLiteral().GetType() == Literal::ARRAY_PROPS) {
279                                         const vector<PropertyList *> &list(i->second->GetLiteral().GetPropertyLists());
280                                         for (vector<PropertyList *>::const_iterator j(list.begin()), end(list.end()); j != end; ++j, iter += fieldType.Size()) {
281                                                 fieldType.Construct(iter);
282                                                 ReadObject(fieldType.TypeId(), -1, iter, **j);
283                                         }
284                                 } else {
285                                         const vector<Value *> &list(i->second->GetLiteral().GetValues());
286                                         for (vector<Value *>::const_iterator j(list.begin()), end(list.end()); j != end; ++j, iter += fieldType.Size()) {
287                                                 fieldType.Construct(iter);
288                                                 ReadLiteral(fieldType.TypeId(), -1, iter, (*j)->GetLiteral());
289                                         }
290                                 }
291                                 if (fd.IsReferenced()) {
292                                         std::memcpy(dest, &aggregate, sizeof(char *));
293                                         dest += sizeof(char *);
294                                         std::memcpy(dest, &arraySize, sizeof(int));
295                                 } else {
296                                         throw Error("aggregate type fields must be referenced");
297                                 }
298                         } else {
299                                 char *src(reinterpret_cast<char *>(GetObject(fd.TypeId(), *i->second)));
300                                 if (fd.TypeId() == TypeDescription::GetTypeId("Image")) {
301                                         src = reinterpret_cast<char *>(GetImage(src));
302                                 }
303                                 if (fd.IsReferenced()) {
304                                         std::memcpy(dest, &src, sizeof(char *));
305                                 } else {
306                                         std::memcpy(dest, src, fieldType.Size());
307                                 }
308                         }
309                 } else {
310                         Postpone(typeId, id, fd.Offset(), i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced());
311                 }
312         }
313         td.Load(object);
314 }
315
316
317 SDL_Surface *Interpreter::GetImage(const string &path) {
318         std::map<string, SDL_Surface *>::const_iterator result(imageCache.find(path));
319         if (result != imageCache.end()) {
320                 return result->second;
321         } else {
322                 SDL_Surface *image(IMG_Load(path.c_str()));
323                 imageCache.insert(make_pair(path, image));
324                 return image;
325         }
326 }
327
328
329 bool Interpreter::CanLink(const Value &v) const {
330         return v.IsLiteral() || source.IsDefined(v.GetIdentifier());
331 }
332
333 void Interpreter::Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType, bool inlined) {
334         char *str(alloc.Alloc(identifier.size() + 1));
335         std::memcpy(str, identifier.c_str(), identifier.size());
336         str[identifier.size()] = '\0';
337         postponedDefinitions.push_back(PostponedDefinition(type, id, offset, str, linkedType, inlined));
338 }
339
340
341 void Interpreter::CreateTypeDescriptions() {
342         {
343                 TypeDescription &td(TypeDescription::CreateOrGet("Boolean"));
344                 td.SetDescription("Logical value which can be either true or false.");
345                 td.SetSize(sizeof(bool));
346         }
347         {
348                 TypeDescription &td(TypeDescription::CreateOrGet("Color"));
349                 td.SetDescription(
350                                 "A color in RGB format with an optional alpha channel.\n"
351                                 "Components range from 0 to 255.\n"
352                                 "Alpha defaults to 255 if omitted.");
353                 td.SetSize(sizeof(Color));
354         }
355         {
356                 TypeDescription &td(TypeDescription::CreateOrGet("Image"));
357                 td.SetDescription("Path to a PNG file with image data.");
358                 td.SetSize(sizeof(SDL_Surface));
359         }
360         {
361                 TypeDescription &td(TypeDescription::CreateOrGet("Number"));
362                 td.SetDescription("A signed integer.");
363                 td.SetSize(sizeof(int));
364         }
365         {
366                 int stringId(TypeDescription::GetTypeId("String"));
367                 TypeDescription &td(TypeDescription::CreateOrGet("Path"));
368                 td.SetDescription("A path in the filesystem which is interpreted relative to the source file's location.");
369                 td.SetSize(1);
370                 td.AddSupertype(stringId, 0);
371         }
372         {
373                 TypeDescription &td(TypeDescription::CreateOrGet("String"));
374                 td.SetDescription("Some characters.");
375                 td.SetSize(1);
376         }
377         {
378                 TypeDescription &td(TypeDescription::CreateOrGet("Vector"));
379                 td.SetDescription("A pair of numbers usually describing a 2D translation or offset.");
380                 td.SetSize(sizeof(Vector<int>));
381         }
382 }
383
384 }