]> git.localhorst.tv Git - l2e.git/commitdiff
extracted object file structure
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 15 Sep 2012 17:50:57 +0000 (19:50 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 24 Feb 2013 21:33:57 +0000 (22:33 +0100)
src/loader/Compiler.cpp
src/loader/Compiler.h
src/loader/ObjectFile.cpp [new file with mode: 0644]
src/loader/ObjectFile.h [new file with mode: 0644]

index 59bebfbcdf5633a8a2b08bd485d7409cb9a1373b..5575838b83f9664bcb2c21ecbca6f47ec17d8465 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "Interpreter.h"
 
+#include <climits>
 #include <cstring>
 #include <map>
 #include <ostream>
@@ -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<string>::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<Interpreter::PostponedDefinition>::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<string, SDL_Surface *>::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<int, vector<void *> >::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<const char *>(&exportsOffset), sizeof(int));
-       Write(out, reinterpret_cast<const char *>(&externalsOffset), sizeof(int));
-       Write(out, reinterpret_cast<const char *>(&externalStringsOffset), sizeof(int));
-       Write(out, reinterpret_cast<const char *>(&exportStringsOffset), sizeof(int));
-       Write(out, reinterpret_cast<const char *>(&imagesOffset), sizeof(int));
-       Write(out, reinterpret_cast<const char *>(&objectsOffset), sizeof(int));
+       Write(out, reinterpret_cast<const char *>(&fileHeader), sizeof(ObjectFileHeader));
        for(map<int, vector<void *> >::const_iterator i(intp.Values().begin()), end(intp.Values().end()); i != end; ++i) {
-               Write(out, reinterpret_cast<const char *>(&i->first), sizeof(int));
-               int typeOffset(TypeOffset(i->first));
-               Write(out, reinterpret_cast<const char *>(&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<const char *>(&to), sizeof(TypeOffset));
        }
+       TypeOffset to;
+       to.typeId = 0;
+       to.begin = 0;
+       to.end = 0;
+       Write(out, reinterpret_cast<const char *>(&to), sizeof(TypeOffset));
 }
 
 void Compiler::WriteExports(ostream &out) {
-       int nameOffset(externalStringsOffset);
+       int nameOffset(fileHeader.externalStringsBegin);
        for (set<string>::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<Interpreter::PostponedDefinition>::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<char *>(&ip), sizeof(ImageProperties));
                SDL_LockSurface(i->second);
-               Write(out, reinterpret_cast<char *>(i->second->pixels), ip.width * ip.height * i->second->format->BytesPerPixel);
+               Write(out, reinterpret_cast<char *>(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);
 }
 
index d2f036115733d2237f8f07508166c7135ff8f92b..ab26cea41e5ed955933c4a20e0598b9f1bbfc146 100644 (file)
@@ -8,6 +8,8 @@
 #ifndef LOADER_COMPILER_H_
 #define LOADER_COMPILER_H_
 
+#include "ObjectFile.h"
+
 #include <iosfwd>
 #include <map>
 #include <memory>
@@ -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<int, int> objectOffsets;
 
diff --git a/src/loader/ObjectFile.cpp b/src/loader/ObjectFile.cpp
new file mode 100644 (file)
index 0000000..4cb8862
--- /dev/null
@@ -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 (file)
index 0000000..6a7d677
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * ObjectFile.h
+ *
+ *  Created on: Sep 15, 2012
+ *      Author: holy
+ */
+
+#ifndef LOADER_OBJECTFILE_H_
+#define LOADER_OBJECTFILE_H_
+
+#include <SDL.h>
+
+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<char *>(fileHeader) + offset; }
+};
+
+struct LoadedExport {
+       char *location;
+       int typeId;
+};
+
+}
+
+#endif /* LOADER_OBJECTFILE_H_ */