]> git.localhorst.tv Git - l2e.git/blob - src/loader/ObjectFile.h
56319a4dd444050681fb0aef7a8886893e91c3d6
[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         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         /// Target type.
26         int typeId;
27         /// True if the object should be copied rather that
28         /// just writing a reference.
29         bool inlined;
30 };
31
32 struct Image {
33         /// File-relative offset to the image's path string.
34         unsigned int pathOffset;
35         /// File-relative offset of the target position of the
36         /// image's reference.
37         unsigned int destOffset;
38 };
39
40 struct Object {
41         int typeId;
42         unsigned int size;
43
44         char *RawObject();
45         Object *Next();
46 };
47
48 struct Array {
49         int typeId;
50         unsigned int size;
51         bool ref;
52
53         char *Data();
54         Array *Next();
55 };
56
57 struct ObjectFileHeader {
58         /// Has to be "L2E\n"
59         char ident[4];
60
61         /// Version ID of the object file format
62         /// For now it must match FORMAT_ID for the loader to be
63         /// able to read it.
64         /// Backwards compatibility might be implemented at some
65         /// point in the future, but don't bet on that ever
66         /// happening.
67         unsigned int versionId;
68
69         /// File-relative offsets of the export section's begin
70         /// and end respectively.
71         /// Exports are named and typed addresses within the
72         /// file. This is essentially an array of Export structs.
73         unsigned int exportsBegin;
74         unsigned int exportsEnd;
75
76         /// File-relative offsets of the externals section's
77         /// begin and end respectively.
78         /// Each external names an entity which must be linked in
79         /// for this object file to function properly. This is
80         /// essentially an array of External structs.
81         unsigned int externalsBegin;
82         unsigned int externalsEnd;
83
84         /// File-relative offsets of the image section's begin and
85         /// end respectively.
86         /// Denotes an array of Image structs.
87         unsigned int imagesBegin;
88         unsigned int imagesEnd;
89
90         /// File-relative offsets of the objet section's begin
91         /// and end respectively.
92         /// Each object begins with its type ID followed by its
93         /// size and finally the raw object data.
94         /// All referecte type fields should contain either a
95         /// file-relative offset or zero to indicate a null
96         /// reference.
97         /// This can be sen as a linked list where the next
98         /// object for a node can be obtained by adding the size
99         /// of to int and the object to the current node's
100         /// address.
101         unsigned int objectsBegin;
102         unsigned int objectsEnd;
103
104         /// File-relative offsets of the array section's begin
105         /// and end respectively.
106         /// Each array consists of an unsigned integer indicating
107         /// its size followed by a boolean flag which is true if
108         /// the arrays consists of pointers followed by the data.
109         unsigned int arraysBegin;
110         unsigned int arraysEnd;
111
112         ObjectFileHeader();
113
114         /// Check if there are any problems with the file header.
115         /// Throws a std::runtime_error on failure.
116         void IntegrityCheck(unsigned int fileSize) const;
117         Export *ExportsBegin();
118         Export *ExportsEnd();
119         External *ExternalsBegin();
120         External *ExternalsEnd();
121         Image *ImagesBegin();
122         Image *ImagesEnd();
123         Object *ObjectsBegin();
124         Object *ObjectsEnd();
125         Array *ArraysBegin();
126         Array *ArraysEnd();
127
128 private:
129         bool CheckSection(
130                         unsigned int begin,
131                         unsigned int end,
132                         unsigned int fsize) const;
133 };
134
135 struct LoadedExport {
136         char *location;
137         int typeId;
138 };
139
140 }
141
142 #endif