azalea_world/
palette.rs

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
use std::io::{Cursor, Write};

use azalea_buf::{BufReadError, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable};
use azalea_core::math;
use tracing::warn;

use crate::BitStorage;

#[derive(Clone, Debug, Copy)]
pub enum PalettedContainerKind {
    Biomes,
    BlockStates,
}

#[derive(Clone, Debug)]
pub struct PalettedContainer {
    pub bits_per_entry: u8,
    /// This is usually a list of unique values that appear in the container so
    /// they can be indexed by the bit storage.
    ///
    /// Sometimes it doesn't contain anything if there's too many unique items
    /// in the bit storage, though.
    pub palette: Palette,
    /// Compacted list of indices pointing to entry IDs in the Palette.
    pub storage: BitStorage,
    pub container_type: PalettedContainerKind,
}

impl PalettedContainer {
    pub fn new(container_type: PalettedContainerKind) -> Self {
        let palette = Palette::SingleValue(0);
        let size = container_type.size();
        let storage = BitStorage::new(0, size, Some(vec![])).unwrap();

        PalettedContainer {
            bits_per_entry: 0,
            palette,
            storage,
            container_type,
        }
    }

    pub fn read_with_type(
        buf: &mut Cursor<&[u8]>,
        container_type: &'static PalettedContainerKind,
    ) -> Result<Self, BufReadError> {
        let server_bits_per_entry = u8::read_from(buf)?;
        let palette_type = PaletteKind::from_bits_and_type(server_bits_per_entry, container_type);
        let palette = palette_type.read(buf)?;
        let size = container_type.size();
        let data = Vec::<u64>::read_from(buf)?;

        // we can only trust the bits per entry that we're sent if there's enough data
        // that it'd be global. if it's not global, then we have to calculate it
        // ourselves.
        // this almost never matters, except on some custom servers like hypixel limbo
        let calculated_bits_per_entry = math::ceil_log2(data.len() as u32) as u8;
        let calculated_bits_per_entry_palette_kind =
            PaletteKind::from_bits_and_type(calculated_bits_per_entry, container_type);
        let bits_per_entry = if calculated_bits_per_entry_palette_kind == PaletteKind::Global {
            server_bits_per_entry
        } else {
            calculated_bits_per_entry
        };

        debug_assert!(
            bits_per_entry != 0 || data.is_empty(),
            "Bits per entry is 0 but data is not empty."
        );

        let storage = match BitStorage::new(bits_per_entry.into(), size, Some(data)) {
            Ok(storage) => storage,
            Err(e) => {
                warn!("Failed to create bit storage: {:?}", e);
                return Err(BufReadError::Custom(
                    "Failed to create bit storage".to_string(),
                ));
            }
        };

        Ok(PalettedContainer {
            bits_per_entry,
            palette,
            storage,
            container_type: *container_type,
        })
    }

    /// Calculates the index of the given coordinates.
    pub fn index_from_coords(&self, x: usize, y: usize, z: usize) -> usize {
        let size_bits = self.container_type.size_bits();

        (((y << size_bits) | z) << size_bits) | x
    }

    pub fn coords_from_index(&self, index: usize) -> (usize, usize, usize) {
        let size_bits = self.container_type.size_bits();
        let mask = (1 << size_bits) - 1;
        (
            index & mask,
            (index >> size_bits >> size_bits) & mask,
            (index >> size_bits) & mask,
        )
    }

    /// Returns the value at the given index.
    ///
    /// # Panics
    ///
    /// This function panics if the index is greater than or equal to the number
    /// of things in the storage. (So for block states, it must be less than
    /// 4096).
    pub fn get_at_index(&self, index: usize) -> u32 {
        // first get the palette id
        let paletted_value = self.storage.get(index);
        // and then get the value from that id
        self.palette.value_for(paletted_value as usize)
    }

    /// Returns the value at the given coordinates.
    pub fn get(&self, x: usize, y: usize, z: usize) -> u32 {
        // let paletted_value = self.storage.get(self.get_index(x, y, z));
        // self.palette.value_for(paletted_value as usize)
        self.get_at_index(self.index_from_coords(x, y, z))
    }

    /// Sets the id at the given coordinates and return the previous id
    pub fn get_and_set(&mut self, x: usize, y: usize, z: usize, value: u32) -> u32 {
        let paletted_value = self.id_for(value);
        self.storage
            .get_and_set(self.index_from_coords(x, y, z), paletted_value as u64) as u32
    }

    /// Sets the id at the given index and return the previous id. You probably
    /// want `.set` instead.
    pub fn set_at_index(&mut self, index: usize, value: u32) {
        let paletted_value = self.id_for(value);
        self.storage.set(index, paletted_value as u64);
    }

    /// Sets the id at the given coordinates and return the previous id
    pub fn set(&mut self, x: usize, y: usize, z: usize, value: u32) {
        self.set_at_index(self.index_from_coords(x, y, z), value);
    }

    fn create_or_reuse_data(&self, bits_per_entry: u8) -> PalettedContainer {
        let new_palette_type =
            PaletteKind::from_bits_and_type(bits_per_entry, &self.container_type);

        let old_palette_type = (&self.palette).into();
        if bits_per_entry == self.bits_per_entry && new_palette_type == old_palette_type {
            return self.clone();
        }
        let storage =
            BitStorage::new(bits_per_entry as usize, self.container_type.size(), None).unwrap();

        // sanity check
        debug_assert_eq!(storage.size(), self.container_type.size());

        // let palette = new_palette_type.as_empty_palette(1usize << (bits_per_entry as
        // usize));
        let palette = new_palette_type.as_empty_palette();
        PalettedContainer {
            bits_per_entry,
            palette,
            storage,
            container_type: self.container_type,
        }
    }

    fn on_resize(&mut self, bits_per_entry: u8, value: u32) -> usize {
        // in vanilla this is always true, but it's sometimes false in purpur servers
        // assert!(bits_per_entry <= 5, "bits_per_entry must be <= 5");
        let mut new_data = self.create_or_reuse_data(bits_per_entry);
        new_data.copy_from(&self.palette, &self.storage);
        *self = new_data;
        self.id_for(value)
    }

    fn copy_from(&mut self, palette: &Palette, storage: &BitStorage) {
        for i in 0..storage.size() {
            let value = palette.value_for(storage.get(i) as usize);
            let id = self.id_for(value) as u64;
            self.storage.set(i, id);
        }
    }

    pub fn id_for(&mut self, value: u32) -> usize {
        match &mut self.palette {
            Palette::SingleValue(v) => {
                if *v != value {
                    self.on_resize(1, value)
                } else {
                    0
                }
            }
            Palette::Linear(palette) => {
                if let Some(index) = palette.iter().position(|&v| v == value) {
                    return index;
                }
                let capacity = 2usize.pow(self.bits_per_entry.into());
                if capacity > palette.len() {
                    palette.push(value);
                    palette.len() - 1
                } else {
                    self.on_resize(self.bits_per_entry + 1, value)
                }
            }
            Palette::Hashmap(palette) => {
                // TODO? vanilla keeps this in memory as a hashmap, but also i don't care
                if let Some(index) = palette.iter().position(|v| *v == value) {
                    return index;
                }
                let capacity = 2usize.pow(self.bits_per_entry.into());
                if capacity > palette.len() {
                    palette.push(value);
                    palette.len() - 1
                } else {
                    self.on_resize(self.bits_per_entry + 1, value)
                }
            }
            Palette::Global => value as usize,
        }
    }
}

impl McBufWritable for PalettedContainer {
    fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
        self.bits_per_entry.write_into(buf)?;
        self.palette.write_into(buf)?;
        self.storage.data.write_into(buf)?;
        Ok(())
    }
}

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

/// A representation of the different types of chunk palettes Minecraft uses.
#[derive(Clone, Debug)]
pub enum Palette {
    /// ID of the corresponding entry in its global palette
    SingleValue(u32),
    // 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<u32>),
    Hashmap(Vec<u32>),
    Global,
}

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

impl McBufWritable for Palette {
    fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
        match self {
            Palette::SingleValue(value) => {
                value.var_write_into(buf)?;
            }
            Palette::Linear(values) => {
                values.var_write_into(buf)?;
            }
            Palette::Hashmap(values) => {
                values.var_write_into(buf)?;
            }
            Palette::Global => {}
        }
        Ok(())
    }
}

impl PaletteKind {
    pub fn from_bits_and_type(bits_per_entry: u8, container_type: &PalettedContainerKind) -> Self {
        match container_type {
            PalettedContainerKind::BlockStates => match bits_per_entry {
                0 => PaletteKind::SingleValue,
                1..=4 => PaletteKind::Linear,
                5..=8 => PaletteKind::Hashmap,
                _ => PaletteKind::Global,
            },
            PalettedContainerKind::Biomes => match bits_per_entry {
                0 => PaletteKind::SingleValue,
                1..=3 => PaletteKind::Linear,
                _ => PaletteKind::Global,
            },
        }
    }

    pub fn read(&self, buf: &mut Cursor<&[u8]>) -> Result<Palette, BufReadError> {
        Ok(match self {
            PaletteKind::SingleValue => Palette::SingleValue(u32::var_read_from(buf)?),
            PaletteKind::Linear => Palette::Linear(Vec::<u32>::var_read_from(buf)?),
            PaletteKind::Hashmap => Palette::Hashmap(Vec::<u32>::var_read_from(buf)?),
            PaletteKind::Global => Palette::Global,
        })
    }

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

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

impl PalettedContainerKind {
    fn size_bits(&self) -> usize {
        match self {
            PalettedContainerKind::BlockStates => 4,
            PalettedContainerKind::Biomes => 2,
        }
    }

    fn size(&self) -> usize {
        1 << (self.size_bits() * 3)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_resize_0_bits_to_1() {
        let mut palette_container = PalettedContainer::new(PalettedContainerKind::BlockStates);

        assert_eq!(palette_container.bits_per_entry, 0);
        assert_eq!(palette_container.get_at_index(0), 0);
        assert_eq!(
            PaletteKind::from(&palette_container.palette),
            PaletteKind::SingleValue
        );
        palette_container.set_at_index(0, 1);
        assert_eq!(palette_container.get_at_index(0), 1);
        assert_eq!(
            PaletteKind::from(&palette_container.palette),
            PaletteKind::Linear
        );
    }

    #[test]
    fn test_resize_0_bits_to_5() {
        let mut palette_container = PalettedContainer::new(PalettedContainerKind::BlockStates);

        palette_container.set_at_index(0, 0); // 0 bits
        assert_eq!(palette_container.bits_per_entry, 0);

        palette_container.set_at_index(1, 1); // 1 bit
        assert_eq!(palette_container.bits_per_entry, 1);

        palette_container.set_at_index(2, 2); // 2 bits
        assert_eq!(palette_container.bits_per_entry, 2);
        palette_container.set_at_index(3, 3);

        palette_container.set_at_index(4, 4); // 3 bits
        assert_eq!(palette_container.bits_per_entry, 3);
        palette_container.set_at_index(5, 5);
        palette_container.set_at_index(6, 6);
        palette_container.set_at_index(7, 7);

        palette_container.set_at_index(8, 8); // 4 bits
        assert_eq!(palette_container.bits_per_entry, 4);
        palette_container.set_at_index(9, 9);
        palette_container.set_at_index(10, 10);
        palette_container.set_at_index(11, 11);
        palette_container.set_at_index(12, 12);
        palette_container.set_at_index(13, 13);
        palette_container.set_at_index(14, 14);
        palette_container.set_at_index(15, 15);
        assert_eq!(palette_container.bits_per_entry, 4);

        palette_container.set_at_index(16, 16); // 5 bits
        assert_eq!(palette_container.bits_per_entry, 5);
    }

    #[test]
    fn test_coords_from_index() {
        let palette_container = PalettedContainer::new(PalettedContainerKind::BlockStates);

        for x in 0..15 {
            for y in 0..15 {
                for z in 0..15 {
                    assert_eq!(
                        palette_container
                            .coords_from_index(palette_container.index_from_coords(x, y, z)),
                        (x, y, z)
                    );
                }
            }
        }
    }
}