From: Daniel Karbach Date: Sat, 15 Sep 2012 17:50:57 +0000 (+0200) Subject: extracted object file structure X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=553fb21593a9c95e37e3be98ad0e4b97bc4ff11b;p=l2e.git extracted object file structure --- diff --git a/src/loader/Compiler.cpp b/src/loader/Compiler.cpp index 59bebfb..5575838 100644 --- a/src/loader/Compiler.cpp +++ b/src/loader/Compiler.cpp @@ -9,6 +9,7 @@ #include "Interpreter.h" +#include #include #include #include @@ -27,39 +28,39 @@ namespace loader { Compiler::Compiler(const Interpreter &intp) : intp(intp) , cursor(0) { - int headerSize(4 + (5 * sizeof(int)) + (intp.Values().size() * 2 * sizeof(int))); + int headerSize(sizeof(ObjectFileHeader) + (intp.Values().size() * sizeof(TypeOffset))); - exportsOffset = headerSize + Remaining(headerSize, 16); - int exportsSize(intp.ExportedIdentifiers().size() * sizeof(Export)); + fileHeader.exportsBegin = headerSize + Remaining(headerSize, 16); + fileHeader.exportsEnd = fileHeader.exportsBegin + (intp.ExportedIdentifiers().size() * sizeof(Export)); - externalsOffset = exportsOffset + exportsSize + Remaining(exportsSize, 16); - int externalsSize(intp.PostponedDefinitions().size() * sizeof(External)); + fileHeader.externalsBegin = fileHeader.exportsEnd + Remaining(fileHeader.exportsEnd, 16); + fileHeader.externalsEnd = fileHeader.externalsBegin + (intp.PostponedDefinitions().size() * sizeof(External)); - exportStringsOffset = externalsOffset + externalsSize + Remaining(externalsSize, 16); - int exportStringsSize(0); + fileHeader.exportStringsBegin = fileHeader.externalsEnd + Remaining(fileHeader.externalsEnd, 16); + fileHeader.exportStringsEnd = fileHeader.exportStringsBegin; for (set::const_iterator i(intp.ExportedIdentifiers().begin()), end(intp.ExportedIdentifiers().end()); i != end; ++i) { - exportStringsSize += i->size() + 1; + fileHeader.exportStringsEnd += i->size() + 1; } - externalStringsOffset = exportStringsOffset + exportStringsSize + Remaining(exportStringsSize, 16); - int externalStringsSize(0); + fileHeader.externalStringsBegin = fileHeader.exportStringsEnd + Remaining(fileHeader.exportStringsEnd, 16); + fileHeader.externalStringsEnd = fileHeader.externalStringsBegin; for(vector::const_iterator i(intp.PostponedDefinitions().begin()), end(intp.PostponedDefinitions().end()); i != end; ++i) { - externalStringsSize += std::strlen(i->identifier) + 1; + fileHeader.externalStringsEnd += std::strlen(i->identifier) + 1; } - imagesOffset = externalStringsOffset + externalStringsSize + Remaining(externalStringsSize, 16); - int imagesSize(intp.Images().size() * sizeof(ImageProperties)); + fileHeader.imagesBegin = fileHeader.externalStringsEnd + Remaining(fileHeader.externalStringsEnd, 16); + fileHeader.imagesEnd = fileHeader.imagesBegin + (intp.Images().size() * sizeof(ImageProperties)); for (map::const_iterator i(intp.Images().begin()), end(intp.Images().end()); i != end; ++i) { - imagesSize += i->second->w * i->second->h * i->second->format->BytesPerPixel; + fileHeader.imagesEnd += i->second->w * i->second->h * i->second->format->BytesPerPixel; } - objectsOffset = imagesOffset + imagesSize + Remaining(imagesSize, 16); - int objectsSize(0); + fileHeader.objectsBegin = fileHeader.imagesEnd + Remaining(fileHeader.imagesEnd, 16); + fileHeader.objectsEnd = fileHeader.objectsBegin; for (map >::const_iterator i(intp.Values().begin()), end(intp.Values().end()); i != end; ++i) { const TypeDescription &td(TypeDescription::Get(i->first)); - objectOffsets[i->first] = objectsOffset + objectsSize; - objectsSize += td.Size() * i->second.size(); - objectsSize += Remaining(objectsSize, 16); + objectOffsets[i->first] = fileHeader.objectsEnd; + fileHeader.objectsEnd += td.Size() * i->second.size(); + fileHeader.objectsEnd += Remaining(fileHeader.objectsEnd, 16); } } @@ -81,22 +82,23 @@ void Compiler::Write(ostream &out) { void Compiler::WriteHeader(ostream &out) { - Write(out, "L2O\n", 4); - Write(out, reinterpret_cast(&exportsOffset), sizeof(int)); - Write(out, reinterpret_cast(&externalsOffset), sizeof(int)); - Write(out, reinterpret_cast(&externalStringsOffset), sizeof(int)); - Write(out, reinterpret_cast(&exportStringsOffset), sizeof(int)); - Write(out, reinterpret_cast(&imagesOffset), sizeof(int)); - Write(out, reinterpret_cast(&objectsOffset), sizeof(int)); + Write(out, reinterpret_cast(&fileHeader), sizeof(ObjectFileHeader)); for(map >::const_iterator i(intp.Values().begin()), end(intp.Values().end()); i != end; ++i) { - Write(out, reinterpret_cast(&i->first), sizeof(int)); - int typeOffset(TypeOffset(i->first)); - Write(out, reinterpret_cast(&typeOffset), sizeof(int)); + TypeOffset to; + to.typeId = i->first; + to.begin = GetTypeOffset(i->first); + to.end = to.begin + (i->second.size() * TypeDescription::Get(i->first).Size()); + Write(out, reinterpret_cast(&to), sizeof(TypeOffset)); } + TypeOffset to; + to.typeId = 0; + to.begin = 0; + to.end = 0; + Write(out, reinterpret_cast(&to), sizeof(TypeOffset)); } void Compiler::WriteExports(ostream &out) { - int nameOffset(externalStringsOffset); + int nameOffset(fileHeader.externalStringsBegin); for (set::const_iterator i(intp.ExportedIdentifiers().begin()), end(intp.ExportedIdentifiers().end()); i != end; ++i) { const Interpreter::ParsedDefinition &dfn(intp.GetDefinition(*i)); Export exp; @@ -109,7 +111,7 @@ void Compiler::WriteExports(ostream &out) { } void Compiler::WriteExternals(ostream &out) { - int nameOffset(exportStringsOffset); + int nameOffset(fileHeader.exportStringsBegin); for(vector::const_iterator i(intp.PostponedDefinitions().begin()), end(intp.PostponedDefinitions().end()); i != end; ++i) { External ext; ext.nameOffset = nameOffset; @@ -139,13 +141,14 @@ void Compiler::WriteImages(ostream &out) { ip.width = i->second->w; ip.height = i->second->h; ip.depth = i->second->format->BitsPerPixel; + ip.pitch = i->second->pitch; ip.rmask = i->second->format->Rmask; ip.gmask = i->second->format->Gmask; ip.bmask = i->second->format->Bmask; ip.amask = i->second->format->Amask; Write(out, reinterpret_cast(&ip), sizeof(ImageProperties)); SDL_LockSurface(i->second); - Write(out, reinterpret_cast(i->second->pixels), ip.width * ip.height * i->second->format->BytesPerPixel); + Write(out, reinterpret_cast(i->second->pixels), ip.width * ip.height * (ip.depth / CHAR_BIT + (ip.depth % CHAR_BIT ? 1 : 0))); // TODO: store palette too? SDL_UnlockSurface(i->second); } @@ -186,10 +189,10 @@ int Compiler::ReferenceOffset(int typeId, int objectId, std::ptrdiff_t fieldOffs int Compiler::ObjectOffset(int typeId, int objectId) const { const TypeDescription &td(TypeDescription::Get(typeId)); - return TypeOffset(typeId) + (td.Size() * objectId); + return GetTypeOffset(typeId) + (td.Size() * objectId); } -int Compiler::TypeOffset(int typeId) const { +int Compiler::GetTypeOffset(int typeId) const { return objectOffsets.at(typeId); } diff --git a/src/loader/Compiler.h b/src/loader/Compiler.h index d2f0361..ab26cea 100644 --- a/src/loader/Compiler.h +++ b/src/loader/Compiler.h @@ -8,6 +8,8 @@ #ifndef LOADER_COMPILER_H_ #define LOADER_COMPILER_H_ +#include "ObjectFile.h" + #include #include #include @@ -29,29 +31,6 @@ private: public: void Write(std::ostream &); - struct Export { - int nameOffset; - int typeId; - int dataOffset; - }; - - struct External { - int nameOffset; - int referenceOffset; - int inlined; - }; - - struct ImageProperties { - Uint32 flags; - int width; - int height; - int depth; - Uint32 rmask; - Uint32 gmask; - Uint32 bmask; - Uint32 amask; - }; - private: void WriteHeader(std::ostream &); void WriteExports(std::ostream &); @@ -67,19 +46,14 @@ private: int ReferenceOffset(int typeId, int objectId, std::ptrdiff_t fieldOffset) const; int ObjectOffset(int typeId, int objectId) const; - int TypeOffset(int typeId) const; + int GetTypeOffset(int typeId) const; private: const Interpreter &intp; int cursor; - int exportsOffset; - int externalsOffset; - int externalStringsOffset; - int exportStringsOffset; - int imagesOffset; - int objectsOffset; + ObjectFileHeader fileHeader; std::map objectOffsets; diff --git a/src/loader/ObjectFile.cpp b/src/loader/ObjectFile.cpp new file mode 100644 index 0000000..4cb8862 --- /dev/null +++ b/src/loader/ObjectFile.cpp @@ -0,0 +1,54 @@ +/* + * ObjectFile.cpp + * + * Created on: Sep 15, 2012 + * Author: holy + */ + +#include "ObjectFile.h" + +namespace loader { + +ObjectFileHeader::ObjectFileHeader() +: versionId(FORMAT_ID) +, exportsBegin(0) +, exportsEnd(0) +, externalsBegin(0) +, externalsEnd(0) +, exportStringsBegin(0) +, exportStringsEnd(0) +, externalStringsBegin(0) +, externalStringsEnd(0) +, imagesBegin(0) +, imagesEnd(0) +, objectsBegin(0) +, objectsEnd(0) { + ident[0] = 'L'; + ident[1] = '2'; + ident[2] = 'E'; + ident[3] = '\n'; +} + +LoadedObjectFile::LoadedObjectFile() +: allocPtr(0) +, fileHeader(0) +, typeOffsetsBegin(0) +, typeOffsetsEnd(0) +, exportsBegin(0) +, exportsEnd(0) +, externalsBegin(0) +, externalsEnd(0) +, exportStringsBegin(0) +, exportStringsEnd(0) +, externalStringsBegin(0) +, externalStringsEnd(0) +, imagesBegin(0) +, imagesEnd(0) +, objectsBegin(0) +, objectsEnd(0) +, surfaces(0) +, surfaceCount(0) { + +} + +} diff --git a/src/loader/ObjectFile.h b/src/loader/ObjectFile.h new file mode 100644 index 0000000..6a7d677 --- /dev/null +++ b/src/loader/ObjectFile.h @@ -0,0 +1,113 @@ +/* + * ObjectFile.h + * + * Created on: Sep 15, 2012 + * Author: holy + */ + +#ifndef LOADER_OBJECTFILE_H_ +#define LOADER_OBJECTFILE_H_ + +#include + +namespace loader { + +const int FORMAT_ID(1); + +struct ObjectFileHeader { + char ident[4]; + + int versionId; + + int exportsBegin; + int exportsEnd; + + int externalsBegin; + int externalsEnd; + + int exportStringsBegin; + int exportStringsEnd; + + int externalStringsBegin; + int externalStringsEnd; + + int imagesBegin; + int imagesEnd; + + int objectsBegin; + int objectsEnd; + + ObjectFileHeader(); +}; + +struct TypeOffset { + int typeId; + int begin; + int end; +}; + +struct Export { + int nameOffset; + int typeId; + int dataOffset; +}; + +struct External { + int nameOffset; + int referenceOffset; + int inlined; +}; + +struct ImageProperties { + Uint32 flags; + int width; + int height; + int depth; + int pitch; + Uint32 rmask; + Uint32 gmask; + Uint32 bmask; + Uint32 amask; +}; + +struct LoadedObjectFile { + char *allocPtr; + ObjectFileHeader *fileHeader; + + TypeOffset *typeOffsetsBegin; + TypeOffset *typeOffsetsEnd; + + Export *exportsBegin; + Export *exportsEnd; + + External *externalsBegin; + External *externalsEnd; + + char *exportStringsBegin; + char *exportStringsEnd; + + char *externalStringsBegin; + char *externalStringsEnd; + + char *imagesBegin; + char *imagesEnd; + + char *objectsBegin; + char *objectsEnd; + + SDL_Surface **surfaces; + int surfaceCount; + + LoadedObjectFile(); + + char *At(int offset) { return reinterpret_cast(fileHeader) + offset; } +}; + +struct LoadedExport { + char *location; + int typeId; +}; + +} + +#endif /* LOADER_OBJECTFILE_H_ */