]> git.localhorst.tv Git - l2e.git/blob - src/loader/Interpreter.cpp
converted pseudo-fields into real ones
[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         switch (dfn.GetLiteral()->GetType()) {
111                 case Literal::ARRAY_VALUES:
112                         throw Error("named value arrays are not supported, sorry");
113                         break;
114                 case Literal::ARRAY_PROPS:
115                         throw Error("named property list arrays are not supported, sorry");
116                         break;
117                 case Literal::BOOLEAN:
118                         {
119                                 int typeId(TypeDescription::GetTypeId("Boolean"));
120                                 values[typeId].push_back(new bool(dfn.GetLiteral()->GetBoolean()));
121                                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, values[typeId].size() - 1)));
122                         }
123                         break;
124                 case Literal::COLOR:
125                         {
126                                 int typeId(TypeDescription::GetTypeId("Color"));
127                                 values[typeId].push_back(new Color(dfn.GetLiteral()->GetRed(), dfn.GetLiteral()->GetGreen(), dfn.GetLiteral()->GetBlue(), dfn.GetLiteral()->GetAlpha()));
128                                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, values[typeId].size() - 1)));
129                         }
130                         break;
131                 case Literal::NUMBER:
132                         {
133                                 int typeId(TypeDescription::GetTypeId("Number"));
134                                 values[typeId].push_back(new int(dfn.GetLiteral()->GetNumber()));
135                                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, values[typeId].size() - 1)));
136                         }
137                         break;
138                 case Literal::PATH:
139                         {
140                                 int typeId(TypeDescription::GetTypeId("Path"));
141                                 char *str(new char[dfn.GetLiteral()->GetString().size() + 1]);
142                                 std::memcpy(str, dfn.GetLiteral()->GetString().c_str(), dfn.GetLiteral()->GetString().size());
143                                 str[dfn.GetLiteral()->GetString().size()] = '\0';
144                                 values[typeId].push_back(str);
145                                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, values[typeId].size() - 1)));
146                         }
147                         break;
148                 case Literal::STRING:
149                         {
150                                 int typeId(TypeDescription::GetTypeId("String"));
151                                 char *str(new char[dfn.GetLiteral()->GetString().size() + 1]);
152                                 std::memcpy(str, dfn.GetLiteral()->GetString().c_str(), dfn.GetLiteral()->GetString().size());
153                                 str[dfn.GetLiteral()->GetString().size()] = '\0';
154                                 values[typeId].push_back(str);
155                                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, values[typeId].size() - 1)));
156                         }
157                         break;
158                 case Literal::VECTOR:
159                         {
160                                 int typeId(TypeDescription::GetTypeId("Vector"));
161                                 values[typeId].push_back(new Vector<int>(dfn.GetLiteral()->GetX(), dfn.GetLiteral()->GetY()));
162                                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, values[typeId].size() - 1)));
163                         }
164                         break;
165                 case Literal::OBJECT:
166                         ReadObject(dfn);
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                         int id(values[typeId].size());
179                         values[typeId].push_back(object);
180                         ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
181                         return object;
182                 } else {
183                         int typeId(0), id(0);
184                         switch (v.GetLiteral().GetType()) {
185                                 case Literal::ARRAY_VALUES:
186                                         throw Error("cannot copy value arrays, sorry");
187                                         break;
188                                 case Literal::ARRAY_PROPS:
189                                         throw Error("cannot copy property list arrays, sorry");
190                                         break;
191                                 case Literal::BOOLEAN:
192                                         {
193                                                 typeId = TypeDescription::GetTypeId("Boolean");
194                                                 id = values[typeId].size();
195                                                 const TypeDescription &td(TypeDescription::Get(typeId));
196                                                 char *object(new char[td.Size()]);
197                                                 values[typeId].push_back(new (object) bool(v.GetLiteral().GetBoolean()));
198                                         }
199                                         break;
200                                 case Literal::COLOR:
201                                         {
202                                                 typeId = TypeDescription::GetTypeId("Color");
203                                                 id = values[typeId].size();
204                                                 const TypeDescription &td(TypeDescription::Get(typeId));
205                                                 char *object(new char[td.Size()]);
206                                                 values[typeId].push_back(new (object) Color(v.GetLiteral().GetRed(), v.GetLiteral().GetGreen(), v.GetLiteral().GetBlue(), v.GetLiteral().GetAlpha()));
207                                         }
208                                         break;
209                                 case Literal::NUMBER:
210                                         {
211                                                 typeId = TypeDescription::GetTypeId("Number");
212                                                 id = values[typeId].size();
213                                                 const TypeDescription &td(TypeDescription::Get(typeId));
214                                                 char *object(new char[td.Size()]);
215                                                 values[typeId].push_back(new (object) int(v.GetLiteral().GetNumber()));
216                                         }
217                                         break;
218                                 case Literal::PATH:
219                                         {
220                                                 typeId = TypeDescription::GetTypeId("Path");
221                                                 id = values[typeId].size();
222                                                 char *str(new char[v.GetLiteral().GetString().size() + 1]);
223                                                 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
224                                                 str[v.GetLiteral().GetString().size()] = '\0';
225                                                 values[typeId].push_back(str);
226                                         }
227                                         break;
228                                 case Literal::STRING:
229                                         {
230                                                 typeId = TypeDescription::GetTypeId("String");
231                                                 id = values[typeId].size();
232                                                 char *str(new char[v.GetLiteral().GetString().size() + 1]);
233                                                 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
234                                                 str[v.GetLiteral().GetString().size()] = '\0';
235                                                 values[typeId].push_back(str);
236                                         }
237                                         break;
238                                 case Literal::VECTOR:
239                                         {
240                                                 typeId = TypeDescription::GetTypeId("Vector");
241                                                 id = values[typeId].size();
242                                                 const TypeDescription &td(TypeDescription::Get(typeId));
243                                                 char *object(new char[td.Size()]);
244                                                 values[typeId].push_back(new (object) Vector<int>(v.GetLiteral().GetX(), v.GetLiteral().GetY()));
245                                         }
246                                         break;
247                                 case Literal::OBJECT:
248                                         {
249                                                 typeId = TypeDescription::GetTypeId(v.GetLiteral().GetTypeName());
250                                                 const TypeDescription &td(TypeDescription::Get(typeId));
251                                                 id = values[typeId].size();
252                                                 char *object(new char[td.Size()]);
253                                                 ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
254                                         }
255                                         break;
256                         }
257                         return values[typeId][id];
258                 }
259         } else {
260                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
261                 return GetObject(typeId, v.GetIdentifier());
262         }
263 }
264
265
266 void Interpreter::ReadObject(const Definition &dfn) {
267         int typeId(TypeDescription::GetTypeId(dfn.TypeName()));
268         const TypeDescription &td(TypeDescription::Get(typeId));
269         int id(values[typeId].size());
270         char *object(new char[td.Size()]);
271         values[typeId].push_back(object);
272         ReadObject(typeId, id, object, *dfn.GetProperties());
273         parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
274 }
275
276
277 void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyList &props) {
278         const TypeDescription &td(TypeDescription::Get(typeId));
279         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
280                 const FieldDescription &fd(td.GetField(i->first));
281                 const TypeDescription &fieldType(TypeDescription::Get(fd.TypeId()));
282                 if (CanLink(*i->second)) {
283                         char *dest(object + fd.Offset());
284                         if (fd.IsAggregate()) {
285                                 if (i->second->GetLiteral().GetType() != Literal::ARRAY_PROPS) {
286                                         throw Error("unsupported aggregate type");
287                                 }
288                                 int arraySize(i->second->GetLiteral().ArraySize());
289                                 char *aggregate(new char[fieldType.Size() * arraySize]);
290                                 char *iter(aggregate);
291                                 vector<PropertyList *> list(i->second->GetLiteral().GetPropertyLists());
292                                 for (vector<PropertyList *>::const_iterator j(list.begin()), end(list.end()); j != end; ++j, iter += fieldType.Size()) {
293                                         ReadObject(fieldType.TypeId(), -1, iter, **j);
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.IsReferenced()) {
305                                         std::memcpy(dest, &src, sizeof(char *));
306                                 } else {
307                                         std::memcpy(dest, src, fieldType.Size());
308                                 }
309                         }
310                 } else {
311                         Postpone(typeId, id, fd.Offset(), i->second->GetIdentifier(), fd.TypeId());
312                 }
313         }
314 }
315
316
317 bool Interpreter::CanLink(const Value &v) const {
318         return v.IsLiteral() || source.IsDefined(v.GetIdentifier());
319 }
320
321 void Interpreter::Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType) {
322         char *str(new char[identifier.size() + 1]);
323         std::memcpy(str, identifier.c_str(), identifier.size());
324         str[identifier.size()] = '\0';
325         postponedDefinitions.push_back(PostponedDefinition(type, id, offset, str, linkedType));
326 }
327
328
329 void Interpreter::CreateTypeDescriptions() {
330         {
331                 TypeDescription &td(TypeDescription::CreateOrGet("Boolean"));
332                 td.SetSize(sizeof(bool));
333         }
334         {
335                 TypeDescription &td(TypeDescription::CreateOrGet("Color"));
336                 td.SetSize(sizeof(Color));
337         }
338         {
339                 TypeDescription &td(TypeDescription::CreateOrGet("Image"));
340                 td.SetSize(sizeof(SDL_Surface));
341         }
342         {
343                 TypeDescription &td(TypeDescription::CreateOrGet("Number"));
344                 td.SetSize(sizeof(int));
345         }
346         {
347                 int stringId(TypeDescription::GetTypeId("String"));
348                 TypeDescription &td(TypeDescription::CreateOrGet("Path"));
349                 td.SetSize(1);
350                 td.AddSupertype(stringId, 0);
351         }
352         {
353                 TypeDescription &td(TypeDescription::CreateOrGet("String"));
354                 td.SetSize(1);
355         }
356         {
357                 TypeDescription &td(TypeDescription::CreateOrGet("Vector"));
358                 td.SetSize(sizeof(Vector<int>));
359         }
360 }
361
362 }