X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FInterpreter.cpp;h=d13c3c6424bfbdd60ccc5cefd19d96f85a56440a;hb=b20681c1bf555c337b15796b3910d0d48488b102;hp=8b54fc177913de18a1266f885cc8e959b6eb4eef;hpb=eb2ad5ffd08128d31af32f3929a3295fcfa251e9;p=l2e.git diff --git a/src/loader/Interpreter.cpp b/src/loader/Interpreter.cpp index 8b54fc1..d13c3c6 100644 --- a/src/loader/Interpreter.cpp +++ b/src/loader/Interpreter.cpp @@ -68,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); @@ -287,11 +331,18 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis if (fd.IsAggregate()) { int arraySize(i->second->GetLiteral().ArraySize()); size_t memberSize = fd.IsReferenced() ? sizeof(char *) : fieldType.Size(); - char *aggregate = alloc.Alloc(arraySize * memberSize); - char *iter = aggregate; + 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()); + for (vector::const_iterator + j(list.begin()), end(list.end()); j != end; ++j, iter += memberSize) { char *member; if (fd.IsReferenced()) { @@ -329,11 +380,12 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis *reinterpret_cast(iter) = GetObject(fd.TypeId(), *j); } else { - Postpone(iter, *j, fd.TypeId(), false); + Postpone(reinterpret_cast(array.data), + iter, *j, fd.TypeId(), false); } } } - std::memcpy(dest, &aggregate, sizeof(char *)); + std::memcpy(dest, &array.data, sizeof(char *)); dest += sizeof(char *); std::memcpy(dest, &arraySize, sizeof(int)); } else if (i->second->IsLiteral() && !fd.IsReferenced()) { @@ -355,7 +407,7 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis } } } else { - Postpone(object, i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced(), fd.IsAggregate()); + Postpone(object, object + fd.Offset(), i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced(), fd.IsAggregate()); } } td.Load(object); @@ -855,22 +907,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( + char *object, char *dest, const std::string &identifier, int type, bool inlined, bool aggregate) { - char *str(alloc.Alloc(identifier.size() + 1)); - std::memcpy(str, identifier.c_str(), identifier.size()); - str[identifier.size()] = '\0'; postponedDefinitions.push_back( - PostponedDefinition(dest, str, type, inlined, aggregate)); + PostponedDefinition(object, dest, identifier, type, inlined, aggregate)); } @@ -898,16 +959,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"));