X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FObjectFile.h;h=98aff02918aaef733737fd2cc23765fd8f2d0ebc;hb=1970312e983541d32d4ff73c81b8d90156a7bb99;hp=6a7d6773d6403016e7547e9bcf06c13ad4a15cc3;hpb=553fb21593a9c95e37e3be98ad0e4b97bc4ff11b;p=l2e.git diff --git a/src/loader/ObjectFile.h b/src/loader/ObjectFile.h index 6a7d677..98aff02 100644 --- a/src/loader/ObjectFile.h +++ b/src/loader/ObjectFile.h @@ -1,10 +1,3 @@ -/* - * ObjectFile.h - * - * Created on: Sep 15, 2012 - * Author: holy - */ - #ifndef LOADER_OBJECTFILE_H_ #define LOADER_OBJECTFILE_H_ @@ -12,95 +5,127 @@ 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; - int typeId; - int dataOffset; + /// Offset of the identifier in the file. + unsigned int nameOffset; + /// Type ID of referenced object. + unsigned int typeId; + /// 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; + /// Nonzero if the object should be copied rather that + /// just writing a reference. + unsigned int 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; - - char *exportStringsBegin; - char *exportStringsEnd; - - char *externalStringsBegin; - char *externalStringsEnd; +struct Object { + unsigned int typeId; + unsigned int size; + char *RawObject(); + Object *Next(); +}; - char *imagesBegin; - char *imagesEnd; +struct Array { + unsigned int typeId; + unsigned int size; + bool ref; + 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(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 { @@ -110,4 +135,4 @@ struct LoadedExport { } -#endif /* LOADER_OBJECTFILE_H_ */ +#endif