4 * Created on: Aug 26, 2012
8 #include "Interpreter.h"
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"
30 #include <SDL_image.h>
33 using battle::Monster;
34 using battle::PartyLayout;
39 using common::TargetingMode;
40 using graphics::Animation;
41 using graphics::Color;
43 using graphics::Frame;
44 using graphics::Gauge;
45 using graphics::ComplexAnimation;
46 using graphics::SimpleAnimation;
47 using graphics::Sprite;
48 using geometry::Vector;
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);
63 const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) const {
64 return parsedDefinitions.at(identifier);
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));
80 throw Error("cannot cast " + actual.TypeName() + " to " + requested.TypeName());
83 throw Error("access to undefined object " + name);
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));
94 void Interpreter::ReadDefinition(const Definition &dfn) {
95 if (parsedDefinitions.find(dfn.Identifier()) != parsedDefinitions.end()) {
98 if (dfn.HasLiteralValue()) {
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));
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());
118 ReadLiteral(typeId, id, object, *dfn.GetLiteral());
120 values[typeId].push_back(object);
121 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
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");
129 case Literal::ARRAY_PROPS:
130 throw Error("named property list arrays are not supported, sorry");
132 case Literal::BOOLEAN:
133 new (object) bool(literal.GetBoolean());
136 new (object) Color(literal.GetRed(), literal.GetGreen(), literal.GetBlue(), literal.GetAlpha());
138 case Literal::NUMBER:
139 new (object) int(literal.GetNumber());
142 std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
143 object[literal.GetString().size()] = '\0';
145 case Literal::STRING:
146 std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
147 object[literal.GetString().size()] = '\0';
149 case Literal::VECTOR:
150 new (object) Vector<int>(literal.GetX(), literal.GetY());
152 case Literal::OBJECT:
153 throw Error("illogical branch: read literal object as non-object literal");
159 void *Interpreter::GetObject(int typeId, const Value &v) {
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());
171 int typeId(0), id(0);
172 switch (v.GetLiteral().GetType()) {
173 case Literal::ARRAY_VALUES:
174 throw Error("cannot copy value arrays, sorry");
176 case Literal::ARRAY_PROPS:
177 throw Error("cannot copy property list arrays, sorry");
179 case Literal::BOOLEAN:
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()));
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()));
197 case Literal::NUMBER:
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()));
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);
216 case Literal::STRING:
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);
226 case Literal::VECTOR:
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()));
235 case Literal::OBJECT:
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());
246 return values[typeId][id];
249 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
250 return GetObject(typeId, v.GetIdentifier());
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)));
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);
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());
291 if (fd.IsReferenced()) {
292 std::memcpy(dest, &aggregate, sizeof(char *));
293 dest += sizeof(char *);
294 std::memcpy(dest, &arraySize, sizeof(int));
296 throw Error("aggregate type fields must be referenced");
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));
303 if (fd.IsReferenced()) {
304 std::memcpy(dest, &src, sizeof(char *));
306 std::memcpy(dest, src, fieldType.Size());
310 Postpone(typeId, id, fd.Offset(), i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced());
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;
322 SDL_Surface *image(IMG_Load(path.c_str()));
323 imageCache.insert(make_pair(path, image));
329 bool Interpreter::CanLink(const Value &v) const {
330 return v.IsLiteral() || source.IsDefined(v.GetIdentifier());
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));
341 void Interpreter::CreateTypeDescriptions() {
343 TypeDescription &td(TypeDescription::CreateOrGet("Boolean"));
344 td.SetDescription("Logical value which can be either true or false.");
345 td.SetSize(sizeof(bool));
348 TypeDescription &td(TypeDescription::CreateOrGet("Color"));
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));
356 TypeDescription &td(TypeDescription::CreateOrGet("Image"));
357 td.SetDescription("Path to a PNG file with image data.");
358 td.SetSize(sizeof(SDL_Surface));
361 TypeDescription &td(TypeDescription::CreateOrGet("Number"));
362 td.SetDescription("A signed integer.");
363 td.SetSize(sizeof(int));
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.");
370 td.AddSupertype(stringId, 0);
373 TypeDescription &td(TypeDescription::CreateOrGet("String"));
374 td.SetDescription("Some characters.");
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>));