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;
57 Interpreter::~Interpreter() {
58 for (vector<PostponedDefinition>::const_iterator i(postponedDefinitions.begin()), end(postponedDefinitions.end()); i != end; ++i) {
61 for (map<string, SDL_Surface *>::const_iterator i(imageCache.begin()), end(imageCache.end()); i != end; ++i) {
62 SDL_FreeSurface(i->second);
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);
73 const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) const {
75 return parsedDefinitions.at(identifier);
77 throw std::runtime_error("cannot find definition for " + identifier);
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));
94 throw Error("cannot cast " + actual.TypeName() + " to " + requested.TypeName());
97 throw Error("access to undefined object " + name);
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));
108 void Interpreter::ReadDefinition(const Definition &dfn) {
109 if (parsedDefinitions.find(dfn.Identifier()) != parsedDefinitions.end()) {
112 if (dfn.HasLiteralValue()) {
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));
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());
132 ReadLiteral(typeId, id, object, *dfn.GetLiteral());
134 values[typeId].push_back(object);
135 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
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");
143 case Literal::ARRAY_PROPS:
144 throw Error("named property list arrays are not supported, sorry");
146 case Literal::BOOLEAN:
147 new (object) bool(literal.GetBoolean());
150 new (object) Color(literal.GetRed(), literal.GetGreen(), literal.GetBlue(), literal.GetAlpha());
152 case Literal::NUMBER:
153 new (object) int(literal.GetNumber());
156 std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
157 object[literal.GetString().size()] = '\0';
159 case Literal::STRING:
160 std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
161 object[literal.GetString().size()] = '\0';
163 case Literal::VECTOR:
164 new (object) Vector<int>(literal.GetX(), literal.GetY());
166 case Literal::OBJECT:
167 throw Error("illogical branch: read literal object as non-object literal");
173 void *Interpreter::GetObject(int typeId, const Value &v) {
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());
185 int typeId(0), id(0);
186 switch (v.GetLiteral().GetType()) {
187 case Literal::ARRAY_VALUES:
188 throw Error("cannot copy value arrays, sorry");
190 case Literal::ARRAY_PROPS:
191 throw Error("cannot copy property list arrays, sorry");
193 case Literal::BOOLEAN:
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()));
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()));
211 case Literal::NUMBER:
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()));
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);
230 case Literal::STRING:
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);
240 case Literal::VECTOR:
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()));
249 case Literal::OBJECT:
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());
260 return values[typeId][id];
263 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
264 return GetObject(typeId, v.GetIdentifier());
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)));
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);
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());
305 if (fd.IsReferenced()) {
306 std::memcpy(dest, &aggregate, sizeof(char *));
307 dest += sizeof(char *);
308 std::memcpy(dest, &arraySize, sizeof(int));
310 throw Error("aggregate type fields must be referenced");
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));
317 if (fd.IsReferenced()) {
318 std::memcpy(dest, &src, sizeof(char *));
320 std::memcpy(dest, src, fieldType.Size());
324 Postpone(typeId, id, fd.Offset(), i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced());
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;
335 SDL_Surface *image(IMG_Load(path.c_str()));
336 imageCache.insert(make_pair(path, image));
342 bool Interpreter::CanLink(const Value &v) const {
343 return v.IsLiteral() || source.IsDefined(v.GetIdentifier());
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));
354 void Interpreter::CreateTypeDescriptions() {
356 TypeDescription &td(TypeDescription::CreateOrGet("Boolean"));
357 td.SetSize(sizeof(bool));
360 TypeDescription &td(TypeDescription::CreateOrGet("Color"));
361 td.SetSize(sizeof(Color));
364 TypeDescription &td(TypeDescription::CreateOrGet("Image"));
365 td.SetSize(sizeof(SDL_Surface));
368 TypeDescription &td(TypeDescription::CreateOrGet("Number"));
369 td.SetSize(sizeof(int));
372 int stringId(TypeDescription::GetTypeId("String"));
373 TypeDescription &td(TypeDescription::CreateOrGet("Path"));
375 td.AddSupertype(stringId, 0);
378 TypeDescription &td(TypeDescription::CreateOrGet("String"));
382 TypeDescription &td(TypeDescription::CreateOrGet("Vector"));
383 td.SetSize(sizeof(Vector<int>));