]> git.localhorst.tv Git - l2e.git/blob - src/loader/ObjectFile.h
b497e344b500ea45ba9fa4e80cbbbda1946c9629
[l2e.git] / src / loader / ObjectFile.h
1 #ifndef LOADER_OBJECTFILE_H_
2 #define LOADER_OBJECTFILE_H_
3
4 #include <SDL.h>
5
6 namespace loader {
7
8 const unsigned int FORMAT_ID = 2;
9
10 struct Export {
11         /// Offset of the identifier in the file.
12         unsigned int nameOffset;
13         /// Type ID of referenced object.
14         unsigned int typeId;
15         /// File-offset of the object's actual data.
16         unsigned int dataOffset;
17 };
18
19 struct External {
20         /// File-relative offset of the referenced object's
21         /// identifier.
22         unsigned int nameOffset;
23         /// Target position for linking/inlining.
24         unsigned int referenceOffset;
25         /// Nonzero if the object should be copied rather that
26         /// just writing a reference.
27         unsigned int inlined;
28 };
29
30 struct Object {
31         unsigned int typeId;
32         unsigned int size;
33         char *RawObject();
34         Object *Next();
35 };
36
37 struct Array {
38         unsigned int typeId;
39         unsigned int size;
40         bool ref;
41         char *Data();
42         Array *Next();
43 };
44
45 struct ObjectFileHeader {
46         /// Has to be "L2E\n"
47         char ident[4];
48
49         /// Version ID of the object file format
50         /// For now it must match FORMAT_ID for the loader to be
51         /// able to read it.
52         /// Backwards compatibility might be implemented at some
53         /// point in the future, but don't bet on that ever
54         /// happening.
55         unsigned int versionId;
56
57         /// File-relative offsets of the export section's begin
58         /// and end respectively.
59         /// Exports are named and typed addresses within the
60         /// file. This is essentially an array of Export structs.
61         unsigned int exportsBegin;
62         unsigned int exportsEnd;
63
64         /// File-relative offsets of the externals section's
65         /// begin and end respectively.
66         /// Each external names an entity which must be linked in
67         /// for this object file to function properly. This is
68         /// essentially an array of External structs.
69         unsigned int externalsBegin;
70         unsigned int externalsEnd;
71
72         /// File-relative offsets of the objet section's begin
73         /// and end respectively.
74         /// Each object begins with its type ID followed by its
75         /// size and finally the raw object data.
76         /// All referecte type fields should contain either a
77         /// file-relative offset or zero to indicate a null
78         /// reference.
79         /// This can be sen as a linked list where the next
80         /// object for a node can be obtained by adding the size
81         /// of to int and the object to the current node's
82         /// address.
83         unsigned int objectsBegin;
84         unsigned int objectsEnd;
85
86         /// File-relative offsets of the array section's begin
87         /// and end respectively.
88         /// Each array consists of an unsigned integer indicating
89         /// its size followed by a boolean flag which is true if
90         /// the arrays consists of pointers followed by the data.
91         unsigned int arraysBegin;
92         unsigned int arraysEnd;
93
94         ObjectFileHeader();
95
96         /// Check if there are any problems with the file header.
97         /// Throws a std::runtime_error on failure.
98         void IntegrityCheck(unsigned int fileSize) const;
99         Export *ExportsBegin();
100         Export *ExportsEnd();
101         External *ExternalsBegin();
102         External *ExternalsEnd();
103         Object *ObjectsBegin();
104         Object *ObjectsEnd();
105         Array *ArraysBegin();
106         Array *ArraysEnd();
107 };
108
109 struct LoadedExport {
110         char *location;
111         int typeId;
112 };
113
114 }
115
116 #endif