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
use std::io::Read;

use crate::{
    Chunk,
    Bounded,
    Image,
    get_value,
    get_value_int,
    get_value_string,
    read_dict,
    read_int,
};

#[derive(Debug, Copy, Clone)]
pub enum Shape {
    Cube = 1,
    Cylinder = 2,
    Sphere = 0,
}

#[derive(Debug)]
pub struct Layer {
    pub base_id: i32,
    pub blocks: Vec<Chunk>,
    pub id: i32,
    pub image_path: String,
    pub material: Image,
    pub name: String,
    pub shape: Shape,
    pub transform: Vec<u8>,
    pub bounds: Option<Bounded>
}

impl Layer {
    pub fn new(stream: &mut dyn Read) -> Self {
        let mut blocks = vec![];
        let length = read_int(stream);
        let block_count = read_int(stream);
        let block_length = block_count * 20 + 4;

        for _ in 0..block_count {
            blocks.push(Chunk::new(stream));
        }

        let dict = {
            if block_length < length {
                read_dict(stream, length - block_length)
            } else {
                Default::default()
            }
        };

        let base_id = get_value_int(&dict, "base_id");
        let id = get_value_int(&dict, "id");
        let image_path = get_value_string(&dict, "img-path");
        let material = Image { buffer: get_value(&dict, "mat") };
        let name = get_value_string(&dict, "name");
        let shape = get_value_int(&dict, "shape");
        let transform = get_value(&dict, "transform");
        let bounds = Bounded::from_bytes(get_value(&dict, "box"));

        Layer {
            base_id,
            blocks,
            id,
            image_path,
            material,
            name,
            shape: {
                match shape {
                    1 => Shape::Cube,
                    2 => Shape::Cylinder,
                    _ => Shape::Sphere,
                }
            },
            transform,
            bounds,
        }
    }
}