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