-/*
- * ObjectFile.h
- *
- * Created on: Sep 15, 2012
- * Author: holy
- */
-
#ifndef LOADER_OBJECTFILE_H_
#define LOADER_OBJECTFILE_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;
-};
+const unsigned int FORMAT_ID = 2;
struct Export {
- int nameOffset;
+ /// Offset of the identifier in the file.
+ unsigned int nameOffset;
+ /// Type ID of referenced object.
int typeId;
- int dataOffset;
+ /// File-offset of the object's actual data.
+ unsigned int dataOffset;
};
struct External {
- int nameOffset;
- int referenceOffset;
- int inlined;
+ /// File-relative offset of the referenced object's
+ /// identifier.
+ unsigned int nameOffset;
+ /// Target position for linking/inlining.
+ unsigned int referenceOffset;
+ /// Target type.
+ int typeId;
+ /// True if the object should be copied rather that
+ /// just writing a reference.
+ bool inlined;
};
-struct ImageProperties {
- Uint32 flags;
- int width;
- int height;
- int depth;
- int pitch;
- Uint32 rmask;
- Uint32 gmask;
- Uint32 bmask;
- Uint32 amask;
+struct Image {
+ /// File-relative offset to the image's path string.
+ unsigned int pathOffset;
+ /// File-relative offset of the target position of the
+ /// image's reference.
+ unsigned int destOffset;
};
-struct LoadedObjectFile {
- char *allocPtr;
- ObjectFileHeader *fileHeader;
-
- TypeOffset *typeOffsetsBegin;
- TypeOffset *typeOffsetsEnd;
-
- Export *exportsBegin;
- Export *exportsEnd;
-
- External *externalsBegin;
- External *externalsEnd;
+struct Object {
+ int typeId;
+ unsigned int size;
- char *exportStringsBegin;
- char *exportStringsEnd;
+ char *RawObject();
+ Object *Next();
+};
- char *externalStringsBegin;
- char *externalStringsEnd;
+struct Array {
+ int typeId;
+ unsigned int size;
+ bool ref;
- char *imagesBegin;
- char *imagesEnd;
+ char *Data();
+ Array *Next();
+};
- char *objectsBegin;
- char *objectsEnd;
+struct ObjectFileHeader {
+ /// Has to be "L2E\n"
+ char ident[4];
- SDL_Surface **surfaces;
- int surfaceCount;
+ /// Version ID of the object file format
+ /// For now it must match FORMAT_ID for the loader to be
+ /// able to read it.
+ /// Backwards compatibility might be implemented at some
+ /// point in the future, but don't bet on that ever
+ /// happening.
+ unsigned int versionId;
+
+ /// File-relative offsets of the export section's begin
+ /// and end respectively.
+ /// Exports are named and typed addresses within the
+ /// file. This is essentially an array of Export structs.
+ unsigned int exportsBegin;
+ unsigned int exportsEnd;
+
+ /// File-relative offsets of the externals section's
+ /// begin and end respectively.
+ /// Each external names an entity which must be linked in
+ /// for this object file to function properly. This is
+ /// essentially an array of External structs.
+ unsigned int externalsBegin;
+ unsigned int externalsEnd;
+
+ /// File-relative offsets of the image section's begin and
+ /// end respectively.
+ /// Denotes an array of Image structs.
+ unsigned int imagesBegin;
+ unsigned int imagesEnd;
+
+ /// File-relative offsets of the objet section's begin
+ /// and end respectively.
+ /// Each object begins with its type ID followed by its
+ /// size and finally the raw object data.
+ /// All referecte type fields should contain either a
+ /// file-relative offset or zero to indicate a null
+ /// reference.
+ /// This can be sen as a linked list where the next
+ /// object for a node can be obtained by adding the size
+ /// of to int and the object to the current node's
+ /// address.
+ unsigned int objectsBegin;
+ unsigned int objectsEnd;
+
+ /// File-relative offsets of the array section's begin
+ /// and end respectively.
+ /// Each array consists of an unsigned integer indicating
+ /// its size followed by a boolean flag which is true if
+ /// the arrays consists of pointers followed by the data.
+ unsigned int arraysBegin;
+ unsigned int arraysEnd;
- LoadedObjectFile();
+ ObjectFileHeader();
- char *At(int offset) { return reinterpret_cast<char *>(fileHeader) + offset; }
+ /// Check if there are any problems with the file header.
+ /// Throws a std::runtime_error on failure.
+ void IntegrityCheck(unsigned int fileSize) const;
+ Export *ExportsBegin();
+ Export *ExportsEnd();
+ External *ExternalsBegin();
+ External *ExternalsEnd();
+ Image *ImagesBegin();
+ Image *ImagesEnd();
+ Object *ObjectsBegin();
+ Object *ObjectsEnd();
+ Array *ArraysBegin();
+ Array *ArraysEnd();
+
+private:
+ bool CheckSection(
+ unsigned int begin,
+ unsigned int end,
+ unsigned int fsize) const;
};
struct LoadedExport {
}
-#endif /* LOADER_OBJECTFILE_H_ */
+#endif