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 (vector<PostponedDefinition>::const_iterator i(postponedDefinitions.begin()), end(postponedDefinitions.end()); i != end; ++i) {
60 for (std::map<string, SDL_Surface *>::const_iterator i(imageCache.begin()), end(imageCache.end()); i != end; ++i) {
61 SDL_FreeSurface(i->second);
63 // TODO: maybe need to reverse the array deletion check if most objects turn out to be arrays (of char)
64 for (std::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);
72 const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) const {
73 return parsedDefinitions.at(identifier);
77 void *Interpreter::GetObject(int typeId, const std::string &name) {
78 std::map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
79 if (i != parsedDefinitions.end()) {
80 const TypeDescription &requested(TypeDescription::Get(typeId));
81 const TypeDescription &actual(TypeDescription::Get(i->second.type));
82 if (requested.TypeId() == actual.TypeId()) {
83 return values[actual.TypeId()][i->second.id];
84 } else if (actual.IsSubtypeOf(requested)) {
85 char *sub(reinterpret_cast<char *>(values[actual.TypeId()][i->second.id]));
86 std::ptrdiff_t offset(actual.SupertypeOffset(requested));
89 throw Error("cannot cast " + actual.TypeName() + " to " + requested.TypeName());
92 throw Error("access to undefined object " + name);
97 void Interpreter::ReadSource() {
98 for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
99 ReadDefinition(source.GetDefinition(*i));
103 void Interpreter::ReadDefinition(const Definition &dfn) {
104 if (parsedDefinitions.find(dfn.Identifier()) != parsedDefinitions.end()) {
107 if (dfn.HasLiteralValue()) {
114 void Interpreter::ReadLiteral(const Definition &dfn) {
115 const string &typeName(dfn.GetLiteral()->IsArray() ? "Array" : dfn.GetLiteral()->GetTypeName());
116 int typeId(TypeDescription::GetTypeId(typeName));
117 int id(values[typeId].size());
118 const TypeDescription &td(TypeDescription::Get(typeId));
120 (dfn.GetLiteral()->GetType() == Literal::PATH
121 || dfn.GetLiteral()->GetType() == Literal::STRING)
122 ? dfn.GetLiteral()->GetString().size() : td.Size());
123 char *object(new char[size]);
124 if (dfn.GetLiteral()->GetType() == Literal::OBJECT) {
125 ReadObject(typeId, id, object, *dfn.GetLiteral()->GetProperties());
127 ReadLiteral(typeId, id, object, *dfn.GetLiteral());
129 values[typeId].push_back(object);
130 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
133 void Interpreter::ReadLiteral(int typeId, int id, char *object, const Literal &literal) {
134 switch (literal.GetType()) {
135 case Literal::ARRAY_VALUES:
136 throw Error("named value arrays are not supported, sorry");
138 case Literal::ARRAY_PROPS:
139 throw Error("named property list arrays are not supported, sorry");
141 case Literal::BOOLEAN:
142 new (object) bool(literal.GetBoolean());
145 new (object) Color(literal.GetRed(), literal.GetGreen(), literal.GetBlue(), literal.GetAlpha());
147 case Literal::NUMBER:
148 new (object) int(literal.GetNumber());
151 std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
152 object[literal.GetString().size()] = '\0';
154 case Literal::STRING:
155 std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
156 object[literal.GetString().size()] = '\0';
158 case Literal::VECTOR:
159 new (object) Vector<int>(literal.GetX(), literal.GetY());
161 case Literal::OBJECT:
162 throw Error("illogical branch: read literal object as non-object literal");
168 void *Interpreter::GetObject(int typeId, const Value &v) {
170 if (v.GetLiteral().IsObject()) {
171 int typeId(TypeDescription::GetTypeId(v.GetLiteral().GetTypeName()));
172 const TypeDescription &td(TypeDescription::Get(typeId));
173 char *object(new char[td.Size()]);
174 td.Construct(object);
175 int id(values[typeId].size());
176 values[typeId].push_back(object);
177 ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
180 int typeId(0), id(0);
181 switch (v.GetLiteral().GetType()) {
182 case Literal::ARRAY_VALUES:
183 throw Error("cannot copy value arrays, sorry");
185 case Literal::ARRAY_PROPS:
186 throw Error("cannot copy property list arrays, sorry");
188 case Literal::BOOLEAN:
190 typeId = TypeDescription::GetTypeId("Boolean");
191 id = values[typeId].size();
192 const TypeDescription &td(TypeDescription::Get(typeId));
193 char *object(new char[td.Size()]);
194 values[typeId].push_back(new (object) bool(v.GetLiteral().GetBoolean()));
199 typeId = TypeDescription::GetTypeId("Color");
200 id = values[typeId].size();
201 const TypeDescription &td(TypeDescription::Get(typeId));
202 char *object(new char[td.Size()]);
203 values[typeId].push_back(new (object) Color(v.GetLiteral().GetRed(), v.GetLiteral().GetGreen(), v.GetLiteral().GetBlue(), v.GetLiteral().GetAlpha()));
206 case Literal::NUMBER:
208 typeId = TypeDescription::GetTypeId("Number");
209 id = values[typeId].size();
210 const TypeDescription &td(TypeDescription::Get(typeId));
211 char *object(new char[td.Size()]);
212 values[typeId].push_back(new (object) int(v.GetLiteral().GetNumber()));
217 typeId = TypeDescription::GetTypeId("Path");
218 id = values[typeId].size();
219 char *str(new char[v.GetLiteral().GetString().size() + 1]);
220 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
221 str[v.GetLiteral().GetString().size()] = '\0';
222 values[typeId].push_back(str);
225 case Literal::STRING:
227 typeId = TypeDescription::GetTypeId("String");
228 id = values[typeId].size();
229 char *str(new char[v.GetLiteral().GetString().size() + 1]);
230 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
231 str[v.GetLiteral().GetString().size()] = '\0';
232 values[typeId].push_back(str);
235 case Literal::VECTOR:
237 typeId = TypeDescription::GetTypeId("Vector");
238 id = values[typeId].size();
239 const TypeDescription &td(TypeDescription::Get(typeId));
240 char *object(new char[td.Size()]);
241 values[typeId].push_back(new (object) Vector<int>(v.GetLiteral().GetX(), v.GetLiteral().GetY()));
244 case Literal::OBJECT:
246 typeId = TypeDescription::GetTypeId(v.GetLiteral().GetTypeName());
247 const TypeDescription &td(TypeDescription::Get(typeId));
248 id = values[typeId].size();
249 char *object(new char[td.Size()]);
250 td.Construct(object);
251 ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
255 return values[typeId][id];
258 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
259 return GetObject(typeId, v.GetIdentifier());
264 void Interpreter::ReadObject(const Definition &dfn) {
265 int typeId(TypeDescription::GetTypeId(dfn.TypeName()));
266 const TypeDescription &td(TypeDescription::Get(typeId));
267 int id(values[typeId].size());
268 char *object(new char[td.Size()]);
269 td.Construct(object);
270 values[typeId].push_back(object);
271 ReadObject(typeId, id, object, *dfn.GetProperties());
272 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
276 void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyList &props) {
277 const TypeDescription &td(TypeDescription::Get(typeId));
278 for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
279 const FieldDescription &fd(td.GetField(i->first));
280 const TypeDescription &fieldType(TypeDescription::Get(fd.TypeId()));
281 if (CanLink(*i->second)) {
282 char *dest(object + fd.Offset());
283 if (fd.IsAggregate()) {
284 int arraySize(i->second->GetLiteral().ArraySize());
285 char *aggregate(new char[fieldType.Size() * arraySize]);
286 char *iter(aggregate);
287 if (i->second->GetLiteral().GetType() == Literal::ARRAY_PROPS) {
288 const vector<PropertyList *> &list(i->second->GetLiteral().GetPropertyLists());
289 for (vector<PropertyList *>::const_iterator j(list.begin()), end(list.end()); j != end; ++j, iter += fieldType.Size()) {
290 fieldType.Construct(iter);
291 ReadObject(fieldType.TypeId(), -1, iter, **j);
294 const vector<Value *> &list(i->second->GetLiteral().GetValues());
295 for (vector<Value *>::const_iterator j(list.begin()), end(list.end()); j != end; ++j, iter += fieldType.Size()) {
296 fieldType.Construct(iter);
297 ReadLiteral(fieldType.TypeId(), -1, iter, (*j)->GetLiteral());
300 if (fd.IsReferenced()) {
301 std::memcpy(dest, &aggregate, sizeof(char *));
302 dest += sizeof(char *);
303 std::memcpy(dest, &arraySize, sizeof(int));
305 throw Error("aggregate type fields must be referenced");
308 char *src(reinterpret_cast<char *>(GetObject(fd.TypeId(), *i->second)));
309 if (fd.TypeId() == TypeDescription::GetTypeId("Image")) {
310 src = reinterpret_cast<char *>(GetImage(src));
312 if (fd.IsReferenced()) {
313 std::memcpy(dest, &src, sizeof(char *));
315 std::memcpy(dest, src, fieldType.Size());
319 Postpone(typeId, id, fd.Offset(), i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced());
326 SDL_Surface *Interpreter::GetImage(const string &path) {
327 std::map<string, SDL_Surface *>::const_iterator result(imageCache.find(path));
328 if (result != imageCache.end()) {
329 return result->second;
331 SDL_Surface *image(IMG_Load(path.c_str()));
332 imageCache.insert(make_pair(path, image));
338 bool Interpreter::CanLink(const Value &v) const {
339 return v.IsLiteral() || source.IsDefined(v.GetIdentifier());
342 void Interpreter::Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType, bool inlined) {
343 char *str(new char[identifier.size() + 1]);
344 std::memcpy(str, identifier.c_str(), identifier.size());
345 str[identifier.size()] = '\0';
346 postponedDefinitions.push_back(PostponedDefinition(type, id, offset, str, linkedType, inlined));
350 void Interpreter::CreateTypeDescriptions() {
352 TypeDescription &td(TypeDescription::CreateOrGet("Boolean"));
353 td.SetDescription("Logical value which can be either true or false.");
354 td.SetSize(sizeof(bool));
357 TypeDescription &td(TypeDescription::CreateOrGet("Color"));
359 "A color in RGB format with an optional alpha channel.\n"
360 "Components range from 0 to 255.\n"
361 "Alpha defaults to 255 if omitted.");
362 td.SetSize(sizeof(Color));
365 TypeDescription &td(TypeDescription::CreateOrGet("Image"));
366 td.SetDescription("Path to a PNG file with image data.");
367 td.SetSize(sizeof(SDL_Surface));
370 TypeDescription &td(TypeDescription::CreateOrGet("Number"));
371 td.SetDescription("A signed integer.");
372 td.SetSize(sizeof(int));
375 int stringId(TypeDescription::GetTypeId("String"));
376 TypeDescription &td(TypeDescription::CreateOrGet("Path"));
377 td.SetDescription("A path in the filesystem which is interpreted relative to the source file's location.");
379 td.AddSupertype(stringId, 0);
382 TypeDescription &td(TypeDescription::CreateOrGet("String"));
383 td.SetDescription("Some characters.");
387 TypeDescription &td(TypeDescription::CreateOrGet("Vector"));
388 td.SetDescription("A pair of numbers usually describing a 2D translation or offset.");
389 td.SetSize(sizeof(Vector<int>));