From: Daniel Karbach Date: Thu, 14 Mar 2013 22:12:14 +0000 (+0100) Subject: activated the loader X-Git-Url: http://git.localhorst.tv/?p=l2e.git;a=commitdiff_plain;h=45bb35881a10720ae26701ddf075f756419cd627 activated the loader produces segfaults en masse but actually loads some of the data --- diff --git a/src/loader/Caster.cpp b/src/loader/Caster.cpp index 050ac93..63b389a 100644 --- a/src/loader/Caster.cpp +++ b/src/loader/Caster.cpp @@ -1,5 +1,7 @@ #include "Caster.h" +#include "Interpreter.h" +#include "Loader.h" #include "TypeDescription.h" #include "../battle/Resources.h" #include "../battle/Monster.h" @@ -23,49 +25,71 @@ using std::string; namespace loader { -Caster::Caster(Interpreter &intp) -: intp(intp) { +Caster::Caster(Loader &ld, Interpreter &intp) +: ld(ld) +, intp(intp) { } battle::Resources *Caster::GetBattleResources(const string &ident) { return reinterpret_cast( - intp.GetObject(battle::Resources::TYPE_ID, ident)); + GetObject(battle::Resources::TYPE_ID, ident)); } Capsule *Caster::GetCapsule(const string &ident) { - return reinterpret_cast(intp.GetObject(Capsule::TYPE_ID, ident)); + return reinterpret_cast( + GetObject(Capsule::TYPE_ID, ident)); } Hero *Caster::GetHero(const string &ident) { - return reinterpret_cast(intp.GetObject(Hero::TYPE_ID, ident)); + return reinterpret_cast( + GetObject(Hero::TYPE_ID, ident)); } Item *Caster::GetItem(const string &ident) { - return reinterpret_cast(intp.GetObject(Item::TYPE_ID, ident)); + return reinterpret_cast( + GetObject(Item::TYPE_ID, ident)); } Map *Caster::GetMap(const string &ident) { - return reinterpret_cast(intp.GetObject(Map::TYPE_ID, ident)); + return reinterpret_cast( + GetObject(Map::TYPE_ID, ident)); } menu::Resources *Caster::GetMenuResources(const string &ident) { return reinterpret_cast( - intp.GetObject(menu::Resources::TYPE_ID, ident)); + GetObject(menu::Resources::TYPE_ID, ident)); } Monster *Caster::GetMonster(const string &ident) { - return reinterpret_cast(intp.GetObject(Monster::TYPE_ID, ident)); + return reinterpret_cast( + GetObject(Monster::TYPE_ID, ident)); } PartyLayout *Caster::GetPartyLayout(const string &ident) { return reinterpret_cast( - intp.GetObject(PartyLayout::TYPE_ID, ident)); + GetObject(PartyLayout::TYPE_ID, ident)); } Spell *Caster::GetSpell(const string &ident) { - return reinterpret_cast(intp.GetObject(Spell::TYPE_ID, ident)); + return reinterpret_cast( + GetObject(Spell::TYPE_ID, ident)); +} + + +void *Caster::GetObject(int typeId, const string &ident) { + std::map::const_iterator i( + ld.Exports().find(ident)); + if (i != ld.Exports().end()) { + if (i->second.typeId != typeId) { + throw std::runtime_error("mismatched type for " + + ident); + } else { + return i->second.location; + } + } + return intp.GetObject(typeId, ident); } } diff --git a/src/loader/Caster.h b/src/loader/Caster.h index c20f5d9..05eada4 100644 --- a/src/loader/Caster.h +++ b/src/loader/Caster.h @@ -12,6 +12,10 @@ namespace common { class Item; class Spell; } +namespace loader { + class Interpreter; + class Loader; +} namespace map { class Map; } @@ -19,8 +23,6 @@ namespace menu { struct Resources; } -#include "Interpreter.h" - #include namespace loader { @@ -28,7 +30,7 @@ namespace loader { class Caster { public: - Caster(Interpreter &intp); + Caster(Loader &ld, Interpreter &intp); ~Caster() { } private: Caster(const Caster &); @@ -46,6 +48,10 @@ public: common::Spell *GetSpell(const std::string &identifier); private: + void *GetObject(int typeId, const std::string &ident); + +private: + Loader &ld; Interpreter &intp; }; diff --git a/src/loader/Compiler.cpp b/src/loader/Compiler.cpp index d122643..bb96b28 100644 --- a/src/loader/Compiler.cpp +++ b/src/loader/Compiler.cpp @@ -75,11 +75,11 @@ void Compiler::WriteOwnStrings(ostream &out) { i(intp.ExportedIdentifiers().begin()), end(intp.ExportedIdentifiers().end()); i != end; ++i) { - addressMap.insert(make_pair(i->c_str(), cursor)); Object object; object.typeId = Interpreter::STRING_ID; object.size = i->size() + 1; Write(out, &object, sizeof(Object)); + addressMap.insert(make_pair(i->c_str(), cursor)); Write(out, i->c_str(), object.size); } for(vector::const_iterator diff --git a/src/loader/Loader.cpp b/src/loader/Loader.cpp index 2403044..9c8739a 100644 --- a/src/loader/Loader.cpp +++ b/src/loader/Loader.cpp @@ -4,7 +4,9 @@ #include #include #include +#include +using std::make_pair; using std::map; using std::string; using std::vector; @@ -52,6 +54,9 @@ void Loader::Load(const std::string &filePath) { LoadObjects(header->ident, header->ObjectsBegin(), header->ObjectsEnd()); + LoadArrays(header->ident, + header->ArraysBegin(), + header->ArraysEnd()); } catch (...) { delete[] block; throw; @@ -65,6 +70,7 @@ void Loader::LoadExports(char *src, Export *begin, Export *end) { LoadedExport &exp(exports[identifier]); exp.typeId = i->typeId; exp.location = src + i->dataOffset; + exports.insert(make_pair(identifier, exp)); } } @@ -112,4 +118,17 @@ void Loader::LoadObject(char *src, char *object, const TypeDescription &td) { } } +void Loader::LoadArrays(char *src, Array *begin, Array *end) { + for (Array *i = begin; i < end; i = i->Next()) { + if (!i->ref) { + continue; + } + for (char *j = i->Data(), *end = i->Data() + i->size; + j < end; j += sizeof(void *)) { + *reinterpret_cast(j) = + src + *reinterpret_cast(j); + } + } +} + } diff --git a/src/loader/Loader.h b/src/loader/Loader.h index cf7dd13..0a7f845 100644 --- a/src/loader/Loader.h +++ b/src/loader/Loader.h @@ -19,11 +19,14 @@ public: public: void Load(const std::string &file); + const std::map &Exports() const { return exports; } + private: void LoadExports(char *src, Export *begin, Export *end); void LoadExternals(char *src, External *begin, External *end); void LoadObjects(char *src, Object *begin, Object *end); void LoadObject(char *src, char *dest, const TypeDescription &); + void LoadArrays(char *src, Array *begin, Array *end); private: std::map objectFiles; diff --git a/src/main.cpp b/src/main.cpp index 73a9f16..c01154e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -211,7 +211,7 @@ int main(int argc, char **argv) { return 3; } - Caster caster(intp); + Caster caster(ld, intp); GameState gameState;