]> git.localhorst.tv Git - l2e.git/blob - src/loader/ObjectFile.h
relocate scripts
[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 Script {
58         unsigned int size;
59
60         char *Text();
61         Script *Next();
62 };
63
64 struct ObjectFileHeader {
65         /// Has to be "L2E\n"
66         char ident[4];
67
68         /// Version ID of the object file format
69         /// For now it must match FORMAT_ID for the loader to be
70         /// able to read it.
71         /// Backwards compatibility might be implemented at some
72         /// point in the future, but don't bet on that ever
73         /// happening.
74         unsigned int versionId;
75
76         /// File-relative offsets of the export section's begin
77         /// and end respectively.
78         /// Exports are named and typed addresses within the
79         /// file. This is essentially an array of Export structs.
80         unsigned int exportsBegin;
81         unsigned int exportsEnd;
82
83         /// File-relative offsets of the externals section's
84         /// begin and end respectively.
85         /// Each external names an entity which must be linked in
86         /// for this object file to function properly. This is
87         /// essentially an array of External structs.
88         unsigned int externalsBegin;
89         unsigned int externalsEnd;
90
91         /// File-relative offsets of the image section's begin and
92         /// end respectively.
93         /// Denotes an array of Image structs.
94         unsigned int imagesBegin;
95         unsigned int imagesEnd;
96
97         /// File-relative offsets of the objet section's begin
98         /// and end respectively.
99         /// Each object begins with its type ID followed by its
100         /// size and finally the raw object data.
101         /// All referecte type fields should contain either a
102         /// file-relative offset or zero to indicate a null
103         /// reference.
104         /// This can be sen as a linked list where the next
105         /// object for a node can be obtained by adding the size
106         /// of to int and the object to the current node's
107         /// address.
108         unsigned int objectsBegin;
109         unsigned int objectsEnd;
110
111         /// File-relative offsets of the array section's begin
112         /// and end respectively.
113         /// Each array consists of an unsigned integer indicating
114         /// its size followed by a boolean flag which is true if
115         /// the arrays consists of pointers followed by the data.
116         unsigned int arraysBegin;
117         unsigned int arraysEnd;
118
119         /// File-relative offsets of the script section's begin
120         /// and end respectively.
121         /// Each script is an unsigned int with the length in
122         /// bytes followed by the script text.
123         unsigned int scriptsBegin;
124         unsigned int scriptsEnd;
125
126         ObjectFileHeader();
127
128         /// Check if there are any problems with the file header.
129         /// Throws a std::runtime_error on failure.
130         void IntegrityCheck(unsigned int fileSize) const;
131         Export *ExportsBegin();
132         Export *ExportsEnd();
133         External *ExternalsBegin();
134         External *ExternalsEnd();
135         Image *ImagesBegin();
136         Image *ImagesEnd();
137         Object *ObjectsBegin();
138         Object *ObjectsEnd();
139         Array *ArraysBegin();
140         Array *ArraysEnd();
141         Script *ScriptsBegin();
142         Script *ScriptsEnd();
143
144 private:
145         bool CheckSection(
146                         unsigned int begin,
147                         unsigned int end,
148                         unsigned int fsize) const;
149 };
150
151 struct LoadedExport {
152         char *location;
153         int typeId;
154 };
155
156 }
157
158 #endif