X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FInterpreter.cpp;h=1888361c465dd3c75de8f8a0057e68f8025bd0ec;hb=HEAD;hp=4b0f5e0ae1228e34680a72991a3d24efd858b874;hpb=0bbc2eda56eba3ea195c2043370ff4d3fd29ca79;p=l2e.git diff --git a/src/loader/Interpreter.cpp b/src/loader/Interpreter.cpp index 4b0f5e0..1888361 100644 --- a/src/loader/Interpreter.cpp +++ b/src/loader/Interpreter.cpp @@ -1,13 +1,7 @@ -/* - * 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" @@ -47,7 +41,7 @@ using graphics::Gauge; using graphics::ComplexAnimation; using graphics::SimpleAnimation; using graphics::Sprite; -using geometry::Vector; +using math::Vector; using std::make_pair; using std::set; using std::string; @@ -74,20 +68,64 @@ const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &id } } +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) { - std::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); @@ -134,10 +172,9 @@ void Interpreter::ReadLiteral(const Definition &dfn) { 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: new (object) bool(literal.GetBoolean()); @@ -184,10 +221,9 @@ void *Interpreter::GetObject(int typeId, const Value &v) { int typeId(0), id(0); switch (v.GetLiteral().GetType()) { case Literal::ARRAY_VALUES: - throw Error("cannot copy value arrays, sorry"); - break; case Literal::ARRAY_PROPS: - throw Error("cannot copy property list arrays, sorry"); + case Literal::ARRAY_IDENTS: + throw Error("cannot copy arrays, sorry"); break; case Literal::BOOLEAN: { @@ -294,28 +330,65 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis char *dest(object + fd.Offset()); if (fd.IsAggregate()) { int arraySize(i->second->GetLiteral().ArraySize()); - char *aggregate(alloc.Alloc(fieldType.Size() * arraySize)); - char *iter(aggregate); + size_t memberSize = fd.IsReferenced() ? sizeof(char *) : fieldType.Size(); + Array array; + array.typeId = fd.TypeId(); + array.size = arraySize * memberSize; + array.data = alloc.Alloc(array.size); + array.ref = fd.IsReferenced(); + arrays.push_back(array); + std::memset(array.data, 0, array.size); + char *iter = reinterpret_cast(array.data); if (i->second->GetLiteral().GetType() == Literal::ARRAY_PROPS) { const vector &list(i->second->GetLiteral().GetPropertyLists()); - for (vector::const_iterator j(list.begin()), end(list.end()); j != end; ++j, iter += fieldType.Size()) { - fieldType.Construct(iter); - ReadObject(fieldType.TypeId(), -1, iter, **j); + for (vector::const_iterator + j(list.begin()), end(list.end()); + j != end; ++j, iter += memberSize) { + char *member; + if (fd.IsReferenced()) { + member = alloc.Alloc(fieldType.Size()); + *reinterpret_cast(iter) = member; + } else { + member = iter; + } + fieldType.Construct(member); + ReadObject(fieldType.TypeId(), -1, member, **j); } - } else { + } else if (i->second->GetLiteral().GetType() == Literal::ARRAY_VALUES) { const vector &list(i->second->GetLiteral().GetValues()); - for (vector::const_iterator j(list.begin()), end(list.end()); j != end; ++j, iter += fieldType.Size()) { - fieldType.Construct(iter); - ReadLiteral(fieldType.TypeId(), -1, iter, (*j)->GetLiteral()); + for (vector::const_iterator j(list.begin()), end(list.end()); + j != end; ++j, iter += memberSize) { + char *member; + if (fd.IsReferenced()) { + member = alloc.Alloc(fieldType.Size()); + *reinterpret_cast(iter) = member; + } else { + member = iter; + } + fieldType.Construct(member); + ReadLiteral(fieldType.TypeId(), -1, member, (*j)->GetLiteral()); } - } - if (fd.IsReferenced()) { - std::memcpy(dest, &aggregate, sizeof(char *)); - dest += sizeof(char *); - std::memcpy(dest, &arraySize, sizeof(int)); } else { - throw Error("aggregate type fields must be referenced"); + if (!fd.IsReferenced()) { + // TODO: implement inline identifier arrays + throw std::runtime_error("inline identifier arrays not implemented (yet)"); + } + const vector &list(i->second->GetLiteral().GetIdentifiers()); + for (vector::const_iterator j(list.begin()), end(list.end()); + j != end; ++j, iter += memberSize) { + if (source.IsDefined(*j)) { + *reinterpret_cast(iter) + = GetObject(fd.TypeId(), *j); + } else { + Postpone(reinterpret_cast(array.data), + iter, *j, fd.TypeId(), false); + } + } } + size_t pLen = sizeof(char *); + std::memcpy(dest, &array.data, pLen); + dest += sizeof(char *); + std::memcpy(dest, &arraySize, sizeof(int)); } else if (i->second->IsLiteral() && !fd.IsReferenced()) { // inline literals if (i->second->GetLiteral().IsObject()) { @@ -335,7 +408,7 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis } } } else { - Postpone(typeId, id, fd.Offset(), i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced()); + Postpone(object, object + fd.Offset(), i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced(), fd.IsAggregate()); } } td.Load(object); @@ -552,6 +625,8 @@ void Interpreter::ReadScript(const std::vector &s, Script *script ReadScriptVector(**i, text + cursor); cursor += sizeof(Vector); break; + default: + break; } } } else if (cmd == "add") { @@ -598,6 +673,8 @@ void Interpreter::ReadScript(const std::vector &s, Script *script ReadScriptVector(**i, text + cursor); cursor += sizeof(Vector); break; + default: + break; } } } else if (cmd == "mod") { @@ -835,16 +912,31 @@ SDL_Surface *Interpreter::GetImage(const string &path) { } } +const string &Interpreter::FindImage(SDL_Surface *img) const { + for (std::map::const_iterator + i(imageCache.begin()), end(imageCache.end()); + i != end; ++i) { + if (i->second == img) { + return i->first; + } + } + throw Error("unable to locate image"); +} + bool Interpreter::CanLink(const Value &v) const { return v.IsLiteral() || source.IsDefined(v.GetIdentifier()); } -void Interpreter::Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType, bool inlined) { - char *str(alloc.Alloc(identifier.size() + 1)); - std::memcpy(str, identifier.c_str(), identifier.size()); - str[identifier.size()] = '\0'; - postponedDefinitions.push_back(PostponedDefinition(type, id, offset, str, linkedType, inlined)); +void Interpreter::Postpone( + char *object, + char *dest, + const std::string &identifier, + int type, + bool inlined, + bool aggregate) { + postponedDefinitions.push_back( + PostponedDefinition(object, dest, identifier, type, inlined, aggregate)); } @@ -872,16 +964,18 @@ void Interpreter::CreateTypeDescriptions() { td.SetDescription("A signed integer."); td.SetSize(sizeof(int)); } - {; + { TypeDescription &td(TypeDescription::Create(PATH_ID, "Path")); td.SetDescription("A path in the filesystem which is interpreted relative to the source file's location."); td.SetSize(1); td.AddSupertype(STRING_ID, 0); } { + Script s; TypeDescription &td(TypeDescription::Create(SCRIPT_ID, "Script")); td.SetDescription("Collection of commands that define a behaviour."); td.SetSize(sizeof(Script)); + td.AddField("text", FieldDescription(((char *)&s.text) - ((char *)&s), STRING_ID).SetAggregate()); } { TypeDescription &td(TypeDescription::Create(STRING_ID, "String"));