aboutsummaryrefslogtreecommitdiff
path: root/azalea-world/src/palette/mod.rs
blob: 4fa81adc9006b22e0fe290cbc996354c68254048 (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
mod container;

#[cfg(test)]
mod tests;

use std::{
    fmt::Debug,
    io::{self, Cursor, Write},
};

use azalea_buf::{AzBufVar, BufReadError};
pub use container::*;

/// A representation of the different types of chunk palettes Minecraft uses.
#[derive(Clone, Debug, PartialEq)]
pub enum Palette<S: PalletedContainerKind> {
    /// ID of the corresponding entry in its global palette
    SingleValue(S),
    // in vanilla this keeps a `size` field that might be less than the length, but i'm not sure
    // it's actually needed?
    Linear(Vec<S>),
    Hashmap(Vec<S>),
    Global,
}

impl<S: PalletedContainerKind> Palette<S> {
    #[inline]
    pub fn value_for(&self, id: usize) -> S {
        match self {
            Palette::SingleValue(v) => *v,
            Palette::Linear(v) => v.get(id).copied().unwrap_or_default(),
            Palette::Hashmap(v) => v.get(id).copied().unwrap_or_default(),
            Palette::Global => S::try_from(id as u32).unwrap_or_default(),
        }
    }
}

impl<S: PalletedContainerKind> Palette<S> {
    pub fn write(&self, buf: &mut impl Write) -> io::Result<()> {
        match self {
            Palette::SingleValue(value) => {
                (*value).into().azalea_write_var(buf)?;
            }
            Palette::Linear(values) => {
                (values.len() as u32).azalea_write_var(buf)?;
                for value in values {
                    (*value).into().azalea_write_var(buf)?;
                }
            }
            Palette::Hashmap(values) => {
                (values.len() as u32).azalea_write_var(buf)?;
                for value in values {
                    (*value).into().azalea_write_var(buf)?;
                }
            }
            Palette::Global => {}
        }
        Ok(())
    }
}

impl PaletteKind {
    pub fn read<S: PalletedContainerKind>(
        &self,
        buf: &mut Cursor<&[u8]>,
    ) -> Result<Palette<S>, BufReadError> {
        Ok(match self {
            // since they're read as varints it's actually fine to just use BlockStateIntegerRepr
            // instead of the correct type (u32)
            PaletteKind::SingleValue => {
                Palette::SingleValue(S::try_from(u32::azalea_read_var(buf)?).unwrap_or_default())
            }
            PaletteKind::Linear => Palette::Linear(
                Vec::<u32>::azalea_read_var(buf)?
                    .into_iter()
                    .map(|v| S::try_from(v).unwrap_or_default())
                    .collect(),
            ),
            PaletteKind::Hashmap => Palette::Hashmap(
                Vec::<u32>::azalea_read_var(buf)?
                    .into_iter()
                    .map(|v| S::try_from(v).unwrap_or_default())
                    .collect(),
            ),
            PaletteKind::Global => Palette::Global,
        })
    }

    pub fn as_empty_palette<S: PalletedContainerKind>(&self) -> Palette<S> {
        match self {
            PaletteKind::SingleValue => Palette::SingleValue(S::default()),
            PaletteKind::Linear => Palette::Linear(Vec::new()),
            PaletteKind::Hashmap => Palette::Hashmap(Vec::new()),
            PaletteKind::Global => Palette::Global,
        }
    }
}

impl<S: PalletedContainerKind> From<&Palette<S>> for PaletteKind {
    fn from(palette: &Palette<S>) -> Self {
        match palette {
            Palette::SingleValue(_) => PaletteKind::SingleValue,
            Palette::Linear(_) => PaletteKind::Linear,
            Palette::Hashmap(_) => PaletteKind::Hashmap,
            Palette::Global => PaletteKind::Global,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PaletteKind {
    SingleValue,
    Linear,
    Hashmap,
    Global,
}