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