X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FInterpreter.cpp;h=8f9e9dc6eff141aa69a1d5d13ad4662293b4b329;hb=dc275497c592669dda75645604a9b35d32f63e90;hp=372a114b8fb26fd83d63493b26099bbc6eef0c85;hpb=1aa61eca26f5b4a4cb856a39e802f5899548b691;p=l2e.git diff --git a/src/loader/Interpreter.cpp b/src/loader/Interpreter.cpp index 372a114..8f9e9dc 100644 --- a/src/loader/Interpreter.cpp +++ b/src/loader/Interpreter.cpp @@ -1,19 +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" @@ -30,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; @@ -43,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; @@ -53,304 +50,41 @@ using std::vector; namespace loader { Interpreter::~Interpreter() { - for (vector::const_iterator i(complexAnimations.begin()), end(complexAnimations.end()); i != end; ++i) { - delete *i; - } - for (vector::const_iterator i(fonts.begin()), end(fonts.end()); i != end; ++i) { - delete *i; - } - for (vector::const_iterator i(frames.begin()), end(frames.end()); i != end; ++i) { - delete *i; - } - for (vector::const_iterator i(gauges.begin()), end(gauges.end()); i != end; ++i) { - delete *i; - } - for (vector::const_iterator i(heroes.begin()), end(heroes.end()); i != end; ++i) { - delete *i; - } - for (vector::const_iterator i(ikaris.begin()), end(ikaris.end()); i != end; ++i) { - delete *i; - } - for (vector::const_iterator i(images.begin()), end(images.end()); i != end; ++i) { - SDL_FreeSurface(*i); - } - for (vector::const_iterator i(items.begin()), end(items.end()); i != end; ++i) { - delete *i; - } - for (vector::const_iterator i(menuProperties.begin()), end(menuProperties.end()); i != end; ++i) { - delete *i; - } - for (vector::const_iterator i(monsters.begin()), end(monsters.end()); i != end; ++i) { - delete *i; - } - for (vector::const_iterator i(partyLayouts.begin()), end(partyLayouts.end()); i != end; ++i) { - delete *i; - } - for (vector::const_iterator i(simpleAnimations.begin()), end(simpleAnimations.end()); i != end; ++i) { - delete *i; - } - for (vector::const_iterator i(spells.begin()), end(spells.end()); i != end; ++i) { - delete *i; - } - for (vector::const_iterator i(sprites.begin()), end(sprites.end()); i != end; ++i) { - delete *i; - } - for (vector::const_iterator i(strings.begin()), end(strings.end()); i != end; ++i) { - delete *i; - } - for (vector::const_iterator i(targetingModes.begin()), end(targetingModes.end()); i != end; ++i) { - delete *i; - } -} - - -Animation *Interpreter::GetAnimation(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.type == COMPLEX_ANIMATION) { - return complexAnimations[i->second.index]; - } else if (i->second.type == SIMPLE_ANIMATION) { - return simpleAnimations[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Animation"); - } - } else { - throw Error("access to undefined Animation " + name); - } -} - -bool Interpreter::GetBoolean(const std::string &name) const { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.type == BOOLEAN) { - return booleans[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Boolean"); - } - } else { - throw Error("access to undefined Boolean " + name); - } -} - -const Color &Interpreter::GetColor(const std::string &name) const { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.type == COLOR) { - return colors[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Color"); - } - } else { - throw Error("access to undefined Color " + name); - } -} - -Font *Interpreter::GetFont(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.type == FONT) { - return fonts[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Font"); - } - } else { - throw Error("access to undefined Font " + name); - } -} - -Frame *Interpreter::GetFrame(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.type == FRAME) { - return frames[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Frame"); - } - } else { - throw Error("access to undefined Frame " + name); - } -} - -Gauge *Interpreter::GetGauge(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.type == GAUGE) { - return gauges[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Gauge"); - } - } else { - throw Error("access to undefined Gauge " + name); - } -} - -Hero *Interpreter::GetHero(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.type == HERO) { - return heroes[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Hero"); - } - } else { - throw Error("access to undefined Hero " + name); + for (std::map::const_iterator i(imageCache.begin()), end(imageCache.end()); i != end; ++i) { + SDL_FreeSurface(i->second); } } -Ikari *Interpreter::GetIkari(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.type == IKARI) { - return ikaris[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Ikari"); - } - } else { - throw Error("access to undefined Ikari " + name); - } -} -Item *Interpreter::GetItem(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); +const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) { + std::map::const_iterator i(parsedDefinitions.find(identifier)); if (i != parsedDefinitions.end()) { - if (i->second.type == ITEM) { - return items[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Item"); - } - } else { - throw Error("access to undefined Item " + name); - } -} - -graphics::MenuProperties *Interpreter::GetMenuProperties(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.type == MENU_PROPERTIES) { - return menuProperties[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to MenuProperties"); - } - } else { - throw Error("access to undefined MenuProperties " + name); - } -} - -Monster *Interpreter::GetMonster(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.type == MONSTER) { - return monsters[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Monster"); - } - } else { - throw Error("access to undefined Monster " + name); - } -} - -int Interpreter::GetNumber(const std::string &name) const { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.type == NUMBER) { - return numbers[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Number"); - } - } else { - throw Error("access to undefined Number " + name); - } -} - -PartyLayout *Interpreter::GetPartyLayout(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.type == PARTY_LAYOUT) { - return partyLayouts[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to PartyLayout"); - } - } else { - throw Error("access to undefined PartyLayout " + name); - } -} - -const char *Interpreter::GetPath(const std::string &name) const { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.type == PATH) { - return strings[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Path"); - } - } else { - throw Error("access to undefined Path " + name); - } -} - -Spell *Interpreter::GetSpell(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.type == SPELL) { - return spells[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Spell"); - } - } else { - throw Error("access to undefined Spell " + name); - } -} - -Sprite *Interpreter::GetSprite(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.type == SPRITE) { - return sprites[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Sprite"); - } - } else { - throw Error("access to undefined Sprite " + name); - } -} - -const char *Interpreter::GetString(const std::string &name) const { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - // TODO: enable path to string casting some time - if (i->second.type == STRING /* || i->second.type == PATH */) { - return strings[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to String"); - } + return i->second; + } else if (source.IsDefined(identifier)) { + ReadDefinition(source.GetDefinition(identifier)); + return parsedDefinitions.at(identifier); } else { - throw Error("access to undefined String " + name); + throw Error("access to undefined object " + identifier); } } -TargetingMode *Interpreter::GetTargetingMode(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.type == TARGETING_MODE) { - return targetingModes[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to TargetingMode"); - } - } else { - throw Error("access to undefined TargetingMode " + name); - } -} -Vector Interpreter::GetVector(const std::string &name) const { - 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()) { - if (i->second.type == VECTOR) { - return vectors[i->second.index]; + 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)); + return sub - offset; } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Vector"); + throw Error("cannot cast " + actual.TypeName() + " to " + requested.TypeName()); } } else { - throw Error("access to undefined Vector " + name); + throw Error("access to undefined object " + name); } } @@ -373,734 +107,811 @@ 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: - valueArrays.push_back(dfn.GetLiteral()->GetValues()); - parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, VALUE_ARRAY, valueArrays.size() - 1))); - break; case Literal::ARRAY_PROPS: - propertyListArrays.push_back(dfn.GetLiteral()->GetPropertyLists()); - parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, PROPERTY_LIST_ARRAY, propertyListArrays.size() - 1))); + case Literal::ARRAY_IDENTS: + throw Error("named arrays are not supported, sorry"); break; case Literal::BOOLEAN: - booleans.push_back(dfn.GetLiteral()->GetBoolean()); - parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, BOOLEAN, booleans.size() - 1))); + new (object) bool(literal.GetBoolean()); break; case Literal::COLOR: - colors.push_back(Color(dfn.GetLiteral()->GetRed(), dfn.GetLiteral()->GetGreen(), dfn.GetLiteral()->GetBlue(), dfn.GetLiteral()->GetAlpha())); - parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, COLOR, colors.size() - 1))); + new (object) Color(literal.GetRed(), literal.GetGreen(), literal.GetBlue(), literal.GetAlpha()); break; case Literal::NUMBER: - numbers.push_back(dfn.GetLiteral()->GetNumber()); - parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, NUMBER, numbers.size() - 1))); + new (object) int(literal.GetNumber()); break; case Literal::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'; - strings.push_back(str); - } - parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, PATH, strings.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