X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FInterpreter.cpp;h=7dcb41e969c0de829f87ef56c67896e0c0dfc82a;hb=cc3d698b8c1ad09d7a3f9e3f28bc84e0ac1735ea;hp=28e9b39335d2f13c13ecc7c0226d1ecf69589c6e;hpb=06b6411e5dc8fc6b905530f7adbde8bd0c2bb0ea;p=l2e.git diff --git a/src/loader/Interpreter.cpp b/src/loader/Interpreter.cpp index 28e9b39..7dcb41e 100644 --- a/src/loader/Interpreter.cpp +++ b/src/loader/Interpreter.cpp @@ -1,10 +1,3 @@ -/* - * Interpreter.cpp - * - * Created on: Aug 26, 2012 - * Author: holy - */ - #include "Interpreter.h" #include "ParsedSource.h" @@ -14,6 +7,7 @@ #include "../battle/Resources.h" #include "../common/Ikari.h" #include "../common/Item.h" +#include "../common/Script.h" #include "../common/Spell.h" #include "../common/Stats.h" #include "../common/TargetingMode.h" @@ -34,6 +28,7 @@ using battle::Monster; using battle::PartyLayout; using common::Ikari; using common::Item; +using common::Script; using common::Spell; using common::Stats; using common::TargetingMode; @@ -47,7 +42,6 @@ using graphics::SimpleAnimation; using graphics::Sprite; using geometry::Vector; using std::make_pair; -using std::map; using std::set; using std::string; using std::vector; @@ -55,32 +49,27 @@ using std::vector; namespace loader { Interpreter::~Interpreter() { - for (vector::const_iterator i(postponedDefinitions.begin()), end(postponedDefinitions.end()); i != end; ++i) { - delete i->identifier; - } - for (map::const_iterator i(imageCache.begin()), end(imageCache.end()); i != end; ++i) { + for (std::map::const_iterator i(imageCache.begin()), end(imageCache.end()); i != end; ++i) { SDL_FreeSurface(i->second); } - // TODO: maybe need to reverse the array deletion check if most objects turn out to be arrays (of char) - for (map >::const_iterator i(values.begin()), end(values.end()); i != end; ++i) { - for (vector::const_iterator j(i->second.begin()), end(i->second.end()); j != end; ++j) { - delete[] reinterpret_cast(*j); - } - } } -const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) const { - try { +const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) { + std::map::const_iterator i(parsedDefinitions.find(identifier)); + if (i != parsedDefinitions.end()) { + return i->second; + } else if (source.IsDefined(identifier)) { + ReadDefinition(source.GetDefinition(identifier)); return parsedDefinitions.at(identifier); - } catch (...) { - throw std::runtime_error("cannot find definition for " + identifier); + } else { + throw Error("access to undefined object " + identifier); } } void *Interpreter::GetObject(int typeId, const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); + std::map::const_iterator i(parsedDefinitions.find(name)); if (i != parsedDefinitions.end()) { const TypeDescription &requested(TypeDescription::Get(typeId)); const TypeDescription &actual(TypeDescription::Get(i->second.type)); @@ -125,14 +114,14 @@ void Interpreter::ReadLiteral(const Definition &dfn) { (dfn.GetLiteral()->GetType() == Literal::PATH || dfn.GetLiteral()->GetType() == Literal::STRING) ? dfn.GetLiteral()->GetString().size() : td.Size()); - char *object(new char[size]); + char *object(alloc.Alloc(size)); + values[typeId].push_back(object); + parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id))); if (dfn.GetLiteral()->GetType() == Literal::OBJECT) { ReadObject(typeId, id, object, *dfn.GetLiteral()->GetProperties()); } else { ReadLiteral(typeId, id, object, *dfn.GetLiteral()); } - values[typeId].push_back(object); - parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id))); } void Interpreter::ReadLiteral(int typeId, int id, char *object, const Literal &literal) { @@ -156,6 +145,10 @@ void Interpreter::ReadLiteral(int typeId, int id, char *object, const Literal &l std::memcpy(object, literal.GetString().c_str(), literal.GetString().size()); object[literal.GetString().size()] = '\0'; break; + case Literal::SCRIPT: + new (object) Script; + ReadScript(literal.GetScript(), reinterpret_cast