summaryrefslogtreecommitdiff
path: root/python/examples/kaitai/allegro_dat.py
blob: 1569e523ba973b21a5ac2192fab7fc228e6da7e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

from pkg_resources import parse_version
from .kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO
from enum import Enum
import collections


if parse_version(ks_version) < parse_version('0.7'):
    raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version))

class AllegroDat(KaitaiStruct):
    """Allegro library for C (mostly used for game and multimedia apps
    programming) used its own container file format.
    
    In general, it allows storage of arbitrary binary data blocks
    bundled together with some simple key-value style metadata
    ("properties") for every block. Allegro also pre-defines some simple
    formats for bitmaps, fonts, MIDI music, sound samples and
    palettes. Allegro library v4.0+ also support LZSS compression.
    
    This spec applies to Allegro data files for library versions 2.2 up
    to 4.4.
    
    .. seealso::
       Source - https://liballeg.org/stabledocs/en/datafile.html
    """

    class PackEnum(Enum):
        unpacked = 1936484398
    SEQ_FIELDS = ["pack_magic", "dat_magic", "num_objects", "objects"]
    def __init__(self, _io, _parent=None, _root=None):
        self._io = _io
        self._parent = _parent
        self._root = _root if _root else self
        self._debug = collections.defaultdict(dict)

    def _read(self):
        self._debug['pack_magic']['start'] = self._io.pos()
        self.pack_magic = KaitaiStream.resolve_enum(self._root.PackEnum, self._io.read_u4be())
        self._debug['pack_magic']['end'] = self._io.pos()
        self._debug['dat_magic']['start'] = self._io.pos()
        self.dat_magic = self._io.ensure_fixed_contents(b"\x41\x4C\x4C\x2E")
        self._debug['dat_magic']['end'] = self._io.pos()
        self._debug['num_objects']['start'] = self._io.pos()
        self.num_objects = self._io.read_u4be()
        self._debug['num_objects']['end'] = self._io.pos()
        self._debug['objects']['start'] = self._io.pos()
        self.objects = [None] * (self.num_objects)
        for i in range(self.num_objects):
            if not 'arr' in self._debug['objects']:
                self._debug['objects']['arr'] = []
            self._debug['objects']['arr'].append({'start': self._io.pos()})
            _t_objects = self._root.DatObject(self._io, self, self._root)
            _t_objects._read()
            self.objects[i] = _t_objects
            self._debug['objects']['arr'][i]['end'] = self._io.pos()

        self._debug['objects']['end'] = self._io.pos()

    class DatFont16(KaitaiStruct):
        """Simple monochrome monospaced font, 95 characters, 8x16 px
        characters.
        """
        SEQ_FIELDS = ["chars"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['chars']['start'] = self._io.pos()
            self.chars = [None] * (95)
            for i in range(95):
                if not 'arr' in self._debug['chars']:
                    self._debug['chars']['arr'] = []
                self._debug['chars']['arr'].append({'start': self._io.pos()})
                self.chars[i] = self._io.read_bytes(16)
                self._debug['chars']['arr'][i]['end'] = self._io.pos()

            self._debug['chars']['end'] = self._io.pos()


    class DatBitmap(KaitaiStruct):
        SEQ_FIELDS = ["bits_per_pixel", "width", "height", "image"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['bits_per_pixel']['start'] = self._io.pos()
            self.bits_per_pixel = self._io.read_s2be()
            self._debug['bits_per_pixel']['end'] = self._io.pos()
            self._debug['width']['start'] = self._io.pos()
            self.width = self._io.read_u2be()
            self._debug['width']['end'] = self._io.pos()
            self._debug['height']['start'] = self._io.pos()
            self.height = self._io.read_u2be()
            self._debug['height']['end'] = self._io.pos()
            self._debug['image']['start'] = self._io.pos()
            self.image = self._io.read_bytes_full()
            self._debug['image']['end'] = self._io.pos()


    class DatFont(KaitaiStruct):
        SEQ_FIELDS = ["font_size", "body"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['font_size']['start'] = self._io.pos()
            self.font_size = self._io.read_s2be()
            self._debug['font_size']['end'] = self._io.pos()
            self._debug['body']['start'] = self._io.pos()
            _on = self.font_size
            if _on == 8:
                self.body = self._root.DatFont8(self._io, self, self._root)
                self.body._read()
            elif _on == 16:
                self.body = self._root.DatFont16(self._io, self, self._root)
                self.body._read()
            elif _on == 0:
                self.body = self._root.DatFont39(self._io, self, self._root)
                self.body._read()
            self._debug['body']['end'] = self._io.pos()


    class DatFont8(KaitaiStruct):
        """Simple monochrome monospaced font, 95 characters, 8x8 px
        characters.
        """
        SEQ_FIELDS = ["chars"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['chars']['start'] = self._io.pos()
            self.chars = [None] * (95)
            for i in range(95):
                if not 'arr' in self._debug['chars']:
                    self._debug['chars']['arr'] = []
                self._debug['chars']['arr'].append({'start': self._io.pos()})
                self.chars[i] = self._io.read_bytes(8)
                self._debug['chars']['arr'][i]['end'] = self._io.pos()

            self._debug['chars']['end'] = self._io.pos()


    class DatObject(KaitaiStruct):
        SEQ_FIELDS = ["properties", "len_compressed", "len_uncompressed", "body"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['properties']['start'] = self._io.pos()
            self.properties = []
            i = 0
            while True:
                if not 'arr' in self._debug['properties']:
                    self._debug['properties']['arr'] = []
                self._debug['properties']['arr'].append({'start': self._io.pos()})
                _t_properties = self._root.Property(self._io, self, self._root)
                _t_properties._read()
                _ = _t_properties
                self.properties.append(_)
                self._debug['properties']['arr'][len(self.properties) - 1]['end'] = self._io.pos()
                if not (_.is_valid):
                    break
                i += 1
            self._debug['properties']['end'] = self._io.pos()
            self._debug['len_compressed']['start'] = self._io.pos()
            self.len_compressed = self._io.read_s4be()
            self._debug['len_compressed']['end'] = self._io.pos()
            self._debug['len_uncompressed']['start'] = self._io.pos()
            self.len_uncompressed = self._io.read_s4be()
            self._debug['len_uncompressed']['end'] = self._io.pos()
            self._debug['body']['start'] = self._io.pos()
            _on = self.type
            if _on == u"BMP ":
                self._raw_body = self._io.read_bytes(self.len_compressed)
                io = KaitaiStream(BytesIO(self._raw_body))
                self.body = self._root.DatBitmap(io, self, self._root)
                self.body._read()
            elif _on == u"RLE ":
                self._raw_body = self._io.read_bytes(self.len_compressed)
                io = KaitaiStream(BytesIO(self._raw_body))
                self.body = self._root.DatRleSprite(io, self, self._root)
                self.body._read()
            elif _on == u"FONT":
                self._raw_body = self._io.read_bytes(self.len_compressed)
                io = KaitaiStream(BytesIO(self._raw_body))
                self.body = self._root.DatFont(io, self, self._root)
                self.body._read()
            else:
                self.body = self._io.read_bytes(self.len_compressed)
            self._debug['body']['end'] = self._io.pos()

        @property
        def type(self):
            if hasattr(self, '_m_type'):
                return self._m_type if hasattr(self, '_m_type') else None

            self._m_type = self.properties[-1].magic
            return self._m_type if hasattr(self, '_m_type') else None


    class DatFont39(KaitaiStruct):
        """New bitmap font format introduced since Allegro 3.9: allows
        flexible designation of character ranges, 8-bit colored
        characters, etc.
        """
        SEQ_FIELDS = ["num_ranges", "ranges"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['num_ranges']['start'] = self._io.pos()
            self.num_ranges = self._io.read_s2be()
            self._debug['num_ranges']['end'] = self._io.pos()
            self._debug['ranges']['start'] = self._io.pos()
            self.ranges = [None] * (self.num_ranges)
            for i in range(self.num_ranges):
                if not 'arr' in self._debug['ranges']:
                    self._debug['ranges']['arr'] = []
                self._debug['ranges']['arr'].append({'start': self._io.pos()})
                _t_ranges = self._root.DatFont39.Range(self._io, self, self._root)
                _t_ranges._read()
                self.ranges[i] = _t_ranges
                self._debug['ranges']['arr'][i]['end'] = self._io.pos()

            self._debug['ranges']['end'] = self._io.pos()

        class Range(KaitaiStruct):
            SEQ_FIELDS = ["mono", "start_char", "end_char", "chars"]
            def __init__(self, _io, _parent=None, _root=None):
                self._io = _io
                self._parent = _parent
                self._root = _root if _root else self
                self._debug = collections.defaultdict(dict)

            def _read(self):
                self._debug['mono']['start'] = self._io.pos()
                self.mono = self._io.read_u1()
                self._debug['mono']['end'] = self._io.pos()
                self._debug['start_char']['start'] = self._io.pos()
                self.start_char = self._io.read_u4be()
                self._debug['start_char']['end'] = self._io.pos()
                self._debug['end_char']['start'] = self._io.pos()
                self.end_char = self._io.read_u4be()
                self._debug['end_char']['end'] = self._io.pos()
                self._debug['chars']['start'] = self._io.pos()
                self.chars = [None] * (((self.end_char - self.start_char) + 1))
                for i in range(((self.end_char - self.start_char) + 1)):
                    if not 'arr' in self._debug['chars']:
                        self._debug['chars']['arr'] = []
                    self._debug['chars']['arr'].append({'start': self._io.pos()})
                    _t_chars = self._root.DatFont39.FontChar(self._io, self, self._root)
                    _t_chars._read()
                    self.chars[i] = _t_chars
                    self._debug['chars']['arr'][i]['end'] = self._io.pos()

                self._debug['chars']['end'] = self._io.pos()


        class FontChar(KaitaiStruct):
            SEQ_FIELDS = ["width", "height", "body"]
            def __init__(self, _io, _parent=None, _root=None):
                self._io = _io
                self._parent = _parent
                self._root = _root if _root else self
                self._debug = collections.defaultdict(dict)

            def _read(self):
                self._debug['width']['start'] = self._io.pos()
                self.width = self._io.read_u2be()
                self._debug['width']['end'] = self._io.pos()
                self._debug['height']['start'] = self._io.pos()
                self.height = self._io.read_u2be()
                self._debug['height']['end'] = self._io.pos()
                self._debug['body']['start'] = self._io.pos()
                self.body = self._io.read_bytes((self.width * self.height))
                self._debug['body']['end'] = self._io.pos()



    class Property(KaitaiStruct):
        SEQ_FIELDS = ["magic", "type", "len_body", "body"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['magic']['start'] = self._io.pos()
            self.magic = (self._io.read_bytes(4)).decode(u"UTF-8")
            self._debug['magic']['end'] = self._io.pos()
            if self.is_valid:
                self._debug['type']['start'] = self._io.pos()
                self.type = (self._io.read_bytes(4)).decode(u"UTF-8")
                self._debug['type']['end'] = self._io.pos()

            if self.is_valid:
                self._debug['len_body']['start'] = self._io.pos()
                self.len_body = self._io.read_u4be()
                self._debug['len_body']['end'] = self._io.pos()

            if self.is_valid:
                self._debug['body']['start'] = self._io.pos()
                self.body = (self._io.read_bytes(self.len_body)).decode(u"UTF-8")
                self._debug['body']['end'] = self._io.pos()


        @property
        def is_valid(self):
            if hasattr(self, '_m_is_valid'):
                return self._m_is_valid if hasattr(self, '_m_is_valid') else None

            self._m_is_valid = self.magic == u"prop"
            return self._m_is_valid if hasattr(self, '_m_is_valid') else None


    class DatRleSprite(KaitaiStruct):
        SEQ_FIELDS = ["bits_per_pixel", "width", "height", "len_image", "image"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['bits_per_pixel']['start'] = self._io.pos()
            self.bits_per_pixel = self._io.read_s2be()
            self._debug['bits_per_pixel']['end'] = self._io.pos()
            self._debug['width']['start'] = self._io.pos()
            self.width = self._io.read_u2be()
            self._debug['width']['end'] = self._io.pos()
            self._debug['height']['start'] = self._io.pos()
            self.height = self._io.read_u2be()
            self._debug['height']['end'] = self._io.pos()
            self._debug['len_image']['start'] = self._io.pos()
            self.len_image = self._io.read_u4be()
            self._debug['len_image']['end'] = self._io.pos()
            self._debug['image']['start'] = self._io.pos()
            self.image = self._io.read_bytes_full()
            self._debug['image']['end'] = self._io.pos()