-
Notifications
You must be signed in to change notification settings - Fork 3
/
read.odin
326 lines (282 loc) · 8.55 KB
/
read.odin
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
package aseprite_file_handler
import "core:io"
import "core:log"
import "core:encoding/endian"
read_bool :: proc(r: io.Reader, n: ^int) -> (data: bool, err: Read_Error) {
return bool(read_byte(r, n) or_return), nil
}
read_i8 :: proc(r: io.Reader, n: ^int) -> (data: i8, err: Read_Error) {
return i8(read_byte(r, n) or_return), nil
}
read_byte :: proc(r: io.Reader, n: ^int) -> (data: BYTE, err: Read_Error) {
data, err = io.read_byte(r, n)
if err != nil {
log.error("Failed to read byte/i8/bool", n^)
}
return
}
read_word :: proc(r: io.Reader, n: ^int) -> (data: WORD, err: Read_Error) {
buf: [2]byte
s := io.read(r, buf[:], n) or_return
if s != 2 {
log.error("Failed to read word", s, n^)
return 0, .Wrong_Read_Size
}
v, ok := endian.get_u16(buf[:], .Little)
if !ok {
err = .Unable_To_Decode_Data
}
return v, err
}
read_short :: proc(r: io.Reader, n: ^int) -> (data: SHORT, err: Read_Error) {
buf: [2]byte
s := io.read(r, buf[:], n) or_return
if s != 2 {
log.error("Failed to read short", s, n^)
return 0, .Wrong_Read_Size
}
v, ok := endian.get_i16(buf[:], .Little)
if !ok {
err = .Unable_To_Decode_Data
}
return v, err
}
read_dword :: proc(r: io.Reader, n: ^int) -> (data: DWORD, err: Read_Error) {
buf: [4]byte
s := io.read(r, buf[:], n) or_return
if s != 4 {
log.error("Failed to read dword", s, n^)
return 0, .Wrong_Read_Size
}
v, ok := endian.get_u32(buf[:], .Little)
if !ok {
err = .Unable_To_Decode_Data
} return v, err
}
read_long :: proc(r: io.Reader, n: ^int) -> (data: LONG, err: Read_Error) {
buf: [4]byte
s := io.read(r, buf[:], n) or_return
if s != 4 {
log.error("Failed to read long", s, n^)
return 0, .Wrong_Read_Size
}
v, ok := endian.get_i32(buf[:], .Little)
if !ok {
err = .Unable_To_Decode_Data
}
return v, err
}
read_fixed :: proc(r: io.Reader, n: ^int) -> (data: FIXED, err: Read_Error) {
buf: [4]byte
s := io.read(r, buf[:], n) or_return
if s != 4 {
log.error("Failed to read fixed", s, n^)
return data, .Wrong_Read_Size
}
/*vi, ok_i := endian.get_i16(buf[:2], .Little)
if !ok_i {
err = .Unable_To_Decode_Data
return
}
vf, ok_f := endian.get_i16(buf[2:], .Little)
if !ok_f {
err = .Unable_To_Decode_Data
return
}
fixed.init_from_parts(&data, i32(vi), i32(vf))*/
v, ok := endian.get_i32(buf[:], .Little)
if !ok {
err = .Unable_To_Decode_Data
return
}
data.i = v
return
}
read_float :: proc(r: io.Reader, n: ^int) -> (data: FLOAT, err: Read_Error) {
buf: [4]byte
s := io.read(r, buf[:], n) or_return
if s != 42 {
log.error("Failed to read float", s, n^)
return 0, .Wrong_Read_Size
}
v, ok := endian.get_f32(buf[:], .Little)
if !ok {
err = .Unable_To_Decode_Data
}
return v, err
}
read_double :: proc(r: io.Reader, n: ^int) -> (data: DOUBLE, err: Read_Error) {
buf: [8]byte
s := io.read(r, buf[:], n) or_return
if s != 8 {
log.error("Failed to read double", s, n^)
return 0, .Wrong_Read_Size
}
v, ok := endian.get_f64(buf[:], .Little)
if !ok {
err = .Unable_To_Decode_Data
}
return v, err
}
read_qword :: proc(r: io.Reader, n: ^int) -> (data: QWORD, err: Read_Error) {
buf: [8]byte
s := io.read(r, buf[:], n) or_return
if s != 8 {
log.error("Failed to read qword", s, n^)
return 0, .Wrong_Read_Size
}
v, ok := endian.get_u64(buf[:], .Little)
if !ok {
err = .Unable_To_Decode_Data
}
return v, err
}
read_long64 :: proc(r: io.Reader, n: ^int) -> (data: LONG64, err: Read_Error) {
buf: [8]byte
s := io.read(r, buf[:], n) or_return
if s != 8 {
log.error("Failed to read long64", s, n^)
return 0, .Wrong_Read_Size
}
v, ok := endian.get_i64(buf[:], .Little)
if !ok {
err = .Unable_To_Decode_Data
}
return v, err
}
read_string :: proc(r: io.Reader, n: ^int, alloc := context.allocator, loc := #caller_location) -> (data: STRING, err: Read_Error) {
size := int(read_word(r, n) or_return)
if size == 0 {
return
}
buf := make([]byte, size, alloc) or_return
s: int
s, err = io.read(r, buf, n)
if err != nil {
log.error("Failed to read string", size, err, n^, loc)
return
}
if s != size {
log.error("Unexpected string size", size, s, n^, loc)
err = .Wrong_Read_Size
return
}
data = string(buf)
return
}
read_point :: proc(r: io.Reader, n: ^int) -> (data: POINT, err: Read_Error) {
data.x = read_long(r, n) or_return
data.y = read_long(r, n) or_return
return
}
read_size :: proc(r: io.Reader, n: ^int) -> (data: SIZE, err: Read_Error) {
data.w = read_long(r, n) or_return
data.h = read_long(r, n) or_return
return
}
read_rect :: proc(r: io.Reader, n: ^int) -> (data: RECT, err: Read_Error) {
data.origin = read_point(r, n) or_return
data.size = read_size(r, n) or_return
return
}
read_uuid:: proc(r: io.Reader, n: ^int) -> (data: UUID, err: Read_Error) {
s := io.read(r, data[:], n) or_return
if s != 16 {
log.error("Failed to read UUID", s, data, n^)
err = .Wrong_Read_Size
}
return
}
read_pixel :: proc(r: io.Reader, n: ^int) -> (data: PIXEL, err: Read_Error) {
return read_byte(r, n)
}
read_pixels :: proc(r: io.Reader, data: []PIXEL, n: ^int) -> (err: Read_Error) {
return read_bytes(r, data[:], n)
}
read_tile :: proc(r: io.Reader, type: Tile_ID, n: ^int) -> (data: TILE, err: Read_Error) {
switch type {
case .byte:
data = read_byte(r, n) or_return
case .word:
data = read_word(r, n) or_return
case .dword:
data = read_dword(r, n) or_return
}
return
}
read_tiles :: proc(r: io.Reader, data: []TILE, type: Tile_ID, n: ^int) -> (err: Read_Error) {
size := len(data)
if len(data) == 0 {
return
}
for i in 0..<size {
data[i] = read_tile(r, type, n) or_return
}
return
}
read_bytes :: proc(r: io.Reader, data: []byte, n: ^int) -> (err: Read_Error) {
s := io.read(r, data, n) or_return
if s != len(data) {
log.error("Could read all the bytes asked.", s, len(data))
err = .Wrong_Read_Size
}
return
}
read_skip :: proc(r: io.Reader, to_skip: int, n: ^int) -> (err: Read_Error) {
// i := io.seek(r, i64(n^+to_skip), .Current) or_return
// n^ += int(i)
for _ in 0..<to_skip {
io.read_byte(r, n) or_return
}
return
}
read_ud_value :: proc(r: io.Reader, type: Property_Type, n: ^int, alloc := context.allocator) -> (val: Property_Value, err: Unmarshal_Error) {
context.allocator = alloc
switch type {
case .Null: return nil, nil
case .Bool: return read_bool(r, n)
case .I8: return read_i8(r, n)
case .U8: return read_byte(r, n)
case .I16: return read_short(r, n)
case .U16: return read_word(r, n)
case .I32: return read_long(r, n)
case .U32: return read_dword(r, n)
case .I64: return read_long64(r, n)
case .U64: return read_qword(r, n)
case .Fixed: return read_fixed(r, n)
case .F32: return read_float(r, n)
case .F64: return read_double(r, n)
case .String: return read_string(r, n)
case .Point: return read_point(r, n)
case .Size: return read_size(r, n)
case .Rect: return read_rect(r, n)
case .UUID: return read_uuid(r, n)
case .Vector:
num := int(read_dword(r, n) or_return)
val = make(UD_Vec, num) or_return
vec_type := Property_Type(read_word(r, n) or_return)
if vec_type == .Null {
for i in 0..<num {
prop_type := Property_Type(read_word(r, n) or_return)
val.(UD_Vec)[i] = read_ud_value(r, prop_type, n) or_return
}
} else {
for i in 0..<num {
val.(UD_Vec)[i] = read_ud_value(r, vec_type, n) or_return
}
}
case .Properties:
size := read_dword(r, n) or_return
val = make(Properties, size) or_return
#partial switch &v in val {
case Properties:
for _ in 0..<size {
key := read_string(r, n) or_return
defer delete(key)
prop_type := Property_Type(read_word(r, n) or_return)
v[key] = read_ud_value(r, prop_type, n) or_return
}
}
}
return
}