X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FLoader.cpp;h=dbfbe3186fdd93e8a508110c082753197778cc3c;hb=1970312e983541d32d4ff73c81b8d90156a7bb99;hp=2403044ad20fb819a23fafc0b43c2ded125a317c;hpb=8c8061a4f8b88410d6d93c039afe6affc4b69cf2;p=l2e.git diff --git a/src/loader/Loader.cpp b/src/loader/Loader.cpp index 2403044..dbfbe31 100644 --- a/src/loader/Loader.cpp +++ b/src/loader/Loader.cpp @@ -4,7 +4,10 @@ #include #include #include +#include +#include +using std::make_pair; using std::map; using std::string; using std::vector; @@ -52,6 +55,12 @@ void Loader::Load(const std::string &filePath) { LoadObjects(header->ident, header->ObjectsBegin(), header->ObjectsEnd()); + LoadArrays(header->ident, + header->ArraysBegin(), + header->ArraysEnd()); + LoadImages(header->ident, + header->ImagesBegin(), + header->ImagesEnd()); } catch (...) { delete[] block; throw; @@ -65,6 +74,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)); } } @@ -87,13 +97,27 @@ void Loader::LoadExternals(char *src, External *begin, External *end) { } } +void Loader::LoadImages(char *src, Image *begin, Image *end) { + for (Image *i = begin; i != end; ++i) { + const string path(src + i->pathOffset); + SDL_Surface **dest = reinterpret_cast(src + i->destOffset); + std::map::const_iterator + found(images.find(path)); + if (found != images.end()) { + *dest = found->second; + } else { + SDL_Surface *loaded = IMG_Load(path.c_str()); + images.insert(make_pair(path, loaded)); + *dest = loaded; + } + } +} + void Loader::LoadObjects(char *src, Object *begin, Object *end) { for (Object *i = begin; i < end; i = i->Next()) { const TypeDescription &td = TypeDescription::Get(i->typeId); - if (td.NeedsLinking()) { - LoadObject(src, i->RawObject(), td); - } + LoadObject(src, i->RawObject(), td); } } @@ -102,12 +126,35 @@ void Loader::LoadObject(char *src, char *object, const TypeDescription &td) { i(td.FieldsBegin()), end(td.FieldsEnd()); i != end; ++i) { const FieldDescription &field = i->second; - if (!field.IsReferenced() && !field.IsAggregate()) { - continue; + if (field.IsReferenced() || field.IsAggregate()) { + char **dest(reinterpret_cast(object + field.Offset())); + if (*dest) { + *dest = src + *reinterpret_cast(dest); + } + } else { + const TypeDescription &nestedType + = TypeDescription::Get(field.TypeId()); + LoadObject(src, object + field.Offset(), nestedType); } - char **dest(reinterpret_cast(object + field.Offset())); - if (*dest) { - *dest = src + *reinterpret_cast(dest); + } +} + +void Loader::LoadArrays(char *src, Array *begin, Array *end) { + for (Array *i = begin; i < end; i = i->Next()) { + if (i->ref) { + for (char *j = i->Data(), *end = i->Data() + i->size; + j < end; j += sizeof(void *)) { + unsigned int offset = *reinterpret_cast(j); + if (offset) { + *reinterpret_cast(j) = src + offset; + } + } + } else { + const TypeDescription &td = TypeDescription::Get(i->typeId); + for (char *j = i->Data(), *end = i->Data() + i->size; + j < end; j += td.Size()) { + LoadObject(src, j, td); + } } } }