aboutsummaryrefslogtreecommitdiff
path: root/tools/compile_assets/aseprite/unmarshal.odin
blob: 5e5295cc1ea9a185f80612dda0b70490dcbf33cb (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package aseprite_file_handler

import "base:runtime"
import "base:intrinsics"
import "core:io"
import "core:os"
import "core:log"
import "core:bytes"
import "core:bufio"
import "core:mem/virtual"


unmarshal_from_bytes_buff :: proc(doc: ^Document, r: ^bytes.Reader, alloc: runtime.Allocator = {}) -> (err: Unmarshal_Error) {
    rr, ok := io.to_reader(bytes.reader_to_stream(r))
    if !ok {
        return .Unable_Make_Reader
    }
    return unmarshal(doc, rr, alloc)
}

unmarshal_from_bufio :: proc(doc: ^Document, r: ^bufio.Reader, alloc: runtime.Allocator = {}) -> (err: Unmarshal_Error) {
    rr, ok := io.to_reader(bufio.reader_to_stream(r))
    if !ok {
        return .Unable_Make_Reader
    }
    return unmarshal(doc, rr, alloc)
}

unmarshal_from_filename :: proc(doc: ^Document, name: string, alloc: runtime.Allocator = {}) -> (err: Unmarshal_Error) {
    fd, fd_err := os.open(name, os.O_RDONLY, 0)
    if fd_err != nil {
        log.error("Unable to read because of:", fd_err)
        return fd_err
    }
    defer os.close(fd)
    return unmarshal(doc, fd, alloc)
}

unmarshal_from_handle :: proc(doc: ^Document, h: os.Handle, alloc: runtime.Allocator = {}) -> (err: Unmarshal_Error) {
    rr, ok := io.to_reader(os.stream_from_handle(h))
    if !ok {
        return .Unable_Make_Reader
    }
    return unmarshal(doc, rr, alloc)
}

unmarshal_from_slice :: proc(doc: ^Document, b: []byte, alloc: runtime.Allocator = {}) -> (err: Unmarshal_Error) {
    r: bytes.Reader
    bytes.reader_init(&r, b[:])
    return unmarshal(doc, &r, alloc)
}

unmarshal :: proc{
    unmarshal_from_bytes_buff, unmarshal_from_slice, unmarshal_from_handle, 
    unmarshal_from_filename, unmarshal_from_bufio, unmarshal_from_reader,
}

unmarshal_from_reader :: proc(doc: ^Document, r: io.Reader, alloc: runtime.Allocator = {}) -> (err: Unmarshal_Error) {
    tr: int
    defer {
        if err != nil {
            log.errorf("Failed to unmarshal at %v (%X) cause of %v", tr, tr, err)
        }
    }
    
    temp_alloc := alloc
    if alloc == {} {
        if doc.arena.curr_block == nil || doc.arena.total_reserved == 0 {
            virtual.arena_init_growing(&doc.arena) or_return
        }
        temp_alloc = virtual.arena_allocator(&doc.arena)
    }
    context.allocator = temp_alloc

    icc_warn: bool
    rt := &tr

    doc.header = read_file_header(r, rt) or_return
    frames := doc.header.frames
    color_depth := doc.header.color_depth
    doc.frames = make([]Frame, int(frames)) or_return
    flags := doc.header.flags

    for &frame in doc.frames {
        fh: Frame_Header
        //frame_size := read_dword(r, rt) or_return
        read_dword(r, rt) or_return
        frame_magic := read_word(r, rt) or_return
        if frame_magic != FRAME_MAGIC_NUM {
            return .Bad_Frame_Magic_Number
        }

        fh.old_num_of_chunks = read_word(r, rt) or_return
        fh.duration = read_word(r, rt) or_return
        if fh.duration == 0 {
            fh.duration = doc.header.speed
        }
        
        read_skip(r, 2, rt) or_return
        fh.num_of_chunks = read_dword(r, rt) or_return

        chunks := int(fh.num_of_chunks) 
        if chunks == 0 {
            chunks = int(fh.old_num_of_chunks)
        }

        frame.header = fh
        frame.chunks = make([]Chunk, chunks) or_return

        for &chunk in frame.chunks {
            c_size := int(read_dword(r, rt) or_return)
            c_type := cast(Chunk_Types)read_word(r, rt) or_return

            switch c_type {
            case .old_palette_256: chunk = read_old_palette_256(r, rt) or_return
            case .old_palette_64:  chunk = read_old_palette_64(r, rt) or_return
            case .layer:           chunk = read_layer(r, rt, (.Has_UUID in flags)) or_return
            case .cel:             chunk = read_cel(r, rt, int(color_depth), c_size) or_return
            case .cel_extra:       chunk = read_cel_extra(r, rt) or_return
            case .color_profile:   chunk = read_color_profile(r, rt, &icc_warn) or_return
            case .external_files:  chunk = read_external_files(r, rt) or_return
            case .mask:            chunk = read_mask(r, rt) or_return
            case .path:            chunk = read_path()
            case .tags:            chunk = read_tags(r, rt) or_return
            case .palette:         chunk = read_palette(r, rt) or_return
            case .user_data:       chunk = read_user_data(r, rt) or_return
            case .slice:           chunk = read_slice(r, rt) or_return
            case .tileset:         chunk = read_tileset(r, rt) or_return

            case .none: fallthrough
            case:
                log.error("Invalid Chunk Type", c_type)
                return .Invalid_Chunk_Type
            }
        }
    }
    return
}


unmarshal_chunks :: proc{ unmarshal_multi_chunks, unmarshal_single_chunk }


unmarshal_multi_chunks :: proc(r: io.Reader, buf: ^[dynamic]Chunk, chunks: Chunk_Set, alloc := context.allocator) -> (err: Unmarshal_Error) {
    context.allocator = alloc
    icc_warn: bool
    tr: int
    defer {
        if err != nil {
            log.error("Failed to unmarshal at", tr, "cause of", err)
        }
    }
    rt := &tr

    file_header := read_file_header(r, rt) or_return
    frames      := file_header.frames
    color_depth := int(file_header.color_depth)
    flags       := file_header.flags

    for _ in 0..<frames {
        read_dword(r, rt) or_return
        frame_magic := read_word(r, rt) or_return
        if frame_magic != FRAME_MAGIC_NUM {
            return .Bad_Frame_Magic_Number
        }
        old_num_of_chunks := read_word(r, rt) or_return
        read_skip(r, 4, rt) or_return
        num_of_chunks := int(read_dword(r, rt) or_return)

        if num_of_chunks == 0 {
            num_of_chunks = int(old_num_of_chunks)
        }
        
        for _ in 0..<num_of_chunks {
            c_size := int(read_dword(r, rt) or_return) - 6
            c_type := cast(Chunk_Types)read_word(r, rt) or_return

            chunk: Chunk
            switch c_type {
            case .old_palette_256:
                if .old_palette_256 in chunks {
                    chunk = read_old_palette_256(r, rt) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .old_palette_64:
                if .old_palette_64 in chunks {
                    chunk = read_old_palette_64(r, rt) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .layer:
                if .layer in chunks {
                    chunk = read_layer(r, rt, (.Has_UUID in flags)) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .cel:
                if .cel in chunks {
                    chunk = read_cel(r, rt, color_depth, c_size+6) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .cel_extra:
                if .cel_extra in chunks {
                    chunk = read_cel_extra(r, rt) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .color_profile:
                if .color_profile in chunks {
                    chunk = read_color_profile(r, rt, &icc_warn) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .external_files:
                if .external_files in chunks {
                    chunk = read_external_files(r, rt) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .mask:
                if .mask in chunks {
                    chunk = read_mask(r, rt) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .path:
                if .path in chunks {
                    chunk = read_path()
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .tags:
                if .tags in chunks {
                    chunk = read_tags(r, rt) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .palette:
                if .palette in chunks {
                    chunk = read_palette(r, rt) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .user_data:
                if .user_data in chunks {
                    chunk = read_user_data(r, rt) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .slice:
                if .slice in chunks {
                    chunk = read_slice(r, rt) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .tileset:
                if .tileset in chunks {
                    chunk = read_tileset(r, rt) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .none: fallthrough
            case:
                log.error("Invalid Chunk Type", c_type)
                return .Invalid_Chunk_Type
            }

            if chunk != nil {
                append(buf, chunk) or_return
            }
        }
    }
    return
}


unmarshal_single_chunk :: proc (
    r: io.Reader, buf: ^[dynamic]$T, alloc := context.allocator
) -> (err: Unmarshal_Error) where intrinsics.type_is_variant_of(Chunk, T) {
    
    context.allocator = alloc
    icc_warn: bool

    tr: int
    defer {
        if err != nil {
            log.error("Failed to unmarshal at", tr, "cause of", err)
        }
    }
    rt := &tr

    file_header := read_file_header(r, rt) or_return

    frames := file_header.frames
    color_depth := file_header.color_depth
    _ = color_depth
    flags := file_header.flags

    for _ in 0..<frames {
        read_dword(r, rt) or_return
        frame_magic := read_word(r, rt) or_return
        if frame_magic != FRAME_MAGIC_NUM {
            return .Bad_Frame_Magic_Number
        }

        old_num_of_chunks := read_word(r, rt) or_return
        read_skip(r, 4, rt) or_return
        num_of_chunks := int(read_dword(r, rt) or_return)

        if num_of_chunks == 0 {
            num_of_chunks = int(old_num_of_chunks)
        }
        
        for _ in 0..<num_of_chunks {
            c_size := int(read_dword(r, rt) or_return) - 6
            c_type := cast(Chunk_Types)read_word(r, rt) or_return

            // TODO: This is too dirty for my likeing. Make a proc group for it all.
            switch c_type {
            case .old_palette_256: 
                when T == Old_Palette_256_Chunk {
                    append(buf, read_old_palette_256(r, rt) or_return) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .old_palette_64:
                when T == Old_Palette_64_Chunk {
                    append(buf, read_old_palette_64(r, rt) or_return) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .layer:
                when T == Layer_Chunk {
                    append(buf, read_layer(r, rt, (.Has_UUID in flags)) or_return) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .cel:
                when T == Cel_Chunk {
                    append(buf, read_cel(r, rt, int(color_depth), c_size+6) or_return) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .cel_extra:
                when T == Cel_Extra_Chunk {
                    append(buf, read_cel_extra(r, rt) or_return) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .color_profile:
                when T == Color_Profile_Chunk {
                    append(buf, read_color_profile(r, rt, &icc_warn) or_return) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .external_files:
                when T == External_Files_Chunk {
                    append(buf, read_external_files(r, rt) or_return) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .mask:
                when T == Mask_Chunk {
                    append(buf, read_mask(r, rt) or_return) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .path:
                when T == Path_Chunk {
                    append(buf, read_path()) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .tags:
                when T == Tags_Chunk {
                    append(buf, read_tags(r, rt) or_return) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .palette:
                when T == Palette_Chunk {
                    append(buf, read_palette(r, rt) or_return) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .user_data:
                when T == User_Data_Chunk {
                    append(buf, read_user_data(r, rt) or_return) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .slice:
                when T == Slice_Chunk {
                    append(buf, read_slice(r, rt) or_return) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .tileset:
                when T == Tileset_Chunk {
                    append(buf, read_tileset(r, rt) or_return) or_return
                } else { 
                    read_skip(r, c_size, rt) or_return 
                }

            case .none: fallthrough
            case:
                log.error("Invalid Chunk Type", c_type)
                return .Invalid_Chunk_Type
            }
        }
    }

    return
}