X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FInterpreter.cpp;h=a1fdc8250639ec81ee3e9613e06dcf37ddb401d5;hb=04e4e8797a63c0a89aae9882982cd864213d05ab;hp=f9f57bfb55f1fa32a4432b12b5cda29bfc586fe8;hpb=b02da898c7c8a08141df4e797774a61cf5e0163f;p=l2e.git diff --git a/src/loader/Interpreter.cpp b/src/loader/Interpreter.cpp index f9f57bf..a1fdc82 100644 --- a/src/loader/Interpreter.cpp +++ b/src/loader/Interpreter.cpp @@ -1,20 +1,16 @@ -/* - * Interpreter.cpp - * - * Created on: Aug 26, 2012 - * Author: holy - */ - #include "Interpreter.h" #include "ParsedSource.h" +#include "TypeDescription.h" #include "../battle/Hero.h" #include "../battle/Monster.h" #include "../battle/PartyLayout.h" #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" #include "../graphics/ComplexAnimation.h" #include "../graphics/Font.h" @@ -31,10 +27,11 @@ using battle::Hero; using battle::Monster; using battle::PartyLayout; -using battle::Stats; using common::Ikari; using common::Item; +using common::Script; using common::Spell; +using common::Stats; using common::TargetingMode; using graphics::Animation; using graphics::Color; @@ -44,9 +41,8 @@ using graphics::Gauge; using graphics::ComplexAnimation; using graphics::SimpleAnimation; using graphics::Sprite; -using geometry::Vector; +using math::Vector; using std::make_pair; -using std::map; using std::set; using std::string; using std::vector; @@ -54,34 +50,82 @@ 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) { + 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); + } else { + throw Error("access to undefined object " + identifier); + } +} + +const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) const { + std::map::const_iterator i(parsedDefinitions.find(identifier)); + if (i != parsedDefinitions.end()) { + return i->second; + } else { + throw Error("access to undefined object " + identifier); } } -void *Interpreter::GetObject(int typeId, const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); +void *Interpreter::GetObject( + int typeId, + const std::string &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)); + const TypeDescription &requested = + TypeDescription::Get(typeId); + const TypeDescription &actual = + TypeDescription::Get(i->second.type); if (requested.TypeId() == actual.TypeId()) { return values[actual.TypeId()][i->second.id]; } else if (actual.IsSubtypeOf(requested)) { - char *sub(reinterpret_cast(values[actual.TypeId()][i->second.id])); - std::ptrdiff_t offset(actual.SupertypeOffset(requested)); + char *sub = reinterpret_cast( + values[actual.TypeId()][i->second.id]); + std::ptrdiff_t offset = + actual.SupertypeOffset(requested); + return sub - offset; + } else { + throw Error("cannot cast " + actual.TypeName() + + " to " + requested.TypeName()); + } + } else { + throw Error("access to undefined object " + name); + } +} + +const void *Interpreter::GetObject( + int typeId, + const std::string &name) const { + 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); + if (requested.TypeId() == actual.TypeId()) { + return values.at(actual.TypeId()).at(i->second.id); + } else if (actual.IsSubtypeOf(requested)) { + char *sub = reinterpret_cast( + values.at(actual.TypeId()).at(i->second.id)); + std::ptrdiff_t offset = + actual.SupertypeOffset(requested); return sub - offset; } else { - throw Error("cannot cast " + actual.TypeName() + " to " + requested.TypeName()); + throw Error("cannot cast " + actual.TypeName() + + " to " + requested.TypeName()); } } else { throw Error("access to undefined object " + name); @@ -107,64 +151,57 @@ void Interpreter::ReadDefinition(const Definition &dfn) { } void Interpreter::ReadLiteral(const Definition &dfn) { - switch (dfn.GetLiteral()->GetType()) { + const string &typeName(dfn.GetLiteral()->IsArray() ? "Array" : dfn.GetLiteral()->GetTypeName()); + int typeId(TypeDescription::GetTypeId(typeName)); + int id(values[typeId].size()); + const TypeDescription &td(TypeDescription::Get(typeId)); + int size( + (dfn.GetLiteral()->GetType() == Literal::PATH + || dfn.GetLiteral()->GetType() == Literal::STRING) + ? dfn.GetLiteral()->GetString().size() : td.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()); + } +} + +void Interpreter::ReadLiteral(int typeId, int id, char *object, const Literal &literal) { + switch (literal.GetType()) { case Literal::ARRAY_VALUES: - throw Error("named value arrays are not supported, sorry"); - break; case Literal::ARRAY_PROPS: - throw Error("named property list arrays are not supported, sorry"); + case Literal::ARRAY_IDENTS: + throw Error("named arrays are not supported, sorry"); break; case Literal::BOOLEAN: - { - int typeId(TypeDescription::GetTypeId("Boolean")); - values[typeId].push_back(new bool(dfn.GetLiteral()->GetBoolean())); - parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, values[typeId].size() - 1))); - } + new (object) bool(literal.GetBoolean()); break; case Literal::COLOR: - { - int typeId(TypeDescription::GetTypeId("Color")); - values[typeId].push_back(new Color(dfn.GetLiteral()->GetRed(), dfn.GetLiteral()->GetGreen(), dfn.GetLiteral()->GetBlue(), dfn.GetLiteral()->GetAlpha())); - parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, values[typeId].size() - 1))); - } + new (object) Color(literal.GetRed(), literal.GetGreen(), literal.GetBlue(), literal.GetAlpha()); break; case Literal::NUMBER: - { - int typeId(TypeDescription::GetTypeId("Number")); - values[typeId].push_back(new int(dfn.GetLiteral()->GetNumber())); - parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, values[typeId].size() - 1))); - } + new (object) int(literal.GetNumber()); break; case Literal::PATH: - { - int typeId(TypeDescription::GetTypeId("Path")); - char *str(new char[dfn.GetLiteral()->GetString().size() + 1]); - std::memcpy(str, dfn.GetLiteral()->GetString().c_str(), dfn.GetLiteral()->GetString().size()); - str[dfn.GetLiteral()->GetString().size()] = '\0'; - values[typeId].push_back(str); - parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, values[typeId].size() - 1))); - } + 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