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
|
use std::{
fmt::Debug,
io::{self, Cursor, Write},
};
use azalea_block::BlockState;
use azalea_buf::{AzBuf, BufReadError};
use azalea_core::position::{ChunkSectionBiomePos, ChunkSectionBlockPos};
use azalea_registry::data::Biome;
use tracing::{debug, warn};
use super::{Palette, PaletteKind};
use crate::BitStorage;
#[derive(Clone, Debug, PartialEq)]
pub struct PalettedContainer<S: PalletedContainerKind> {
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<S>,
/// Compacted list of indices pointing to entry IDs in the Palette.
pub storage: BitStorage,
}
pub trait PalletedContainerKind:
Copy + Clone + Debug + Default + PartialEq + TryFrom<u32> + Into<u32>
{
type SectionPos: SectionPos;
fn size_bits() -> usize;
fn size() -> usize {
1 << (Self::size_bits() * 3)
}
fn bits_per_entry_to_palette_kind(bits_per_entry: u8) -> PaletteKind;
}
impl PalletedContainerKind for BlockState {
type SectionPos = ChunkSectionBlockPos;
fn size_bits() -> usize {
4
}
fn bits_per_entry_to_palette_kind(bits_per_entry: u8) -> PaletteKind {
match bits_per_entry {
0 => PaletteKind::SingleValue,
1..=4 => PaletteKind::Linear,
5..=8 => PaletteKind::Hashmap,
_ => PaletteKind::Global,
}
}
}
impl PalletedContainerKind for Biome {
type SectionPos = ChunkSectionBiomePos;
fn size_bits() -> usize {
2
}
fn bits_per_entry_to_palette_kind(bits_per_entry: u8) -> PaletteKind {
match bits_per_entry {
0 => PaletteKind::SingleValue,
1..=3 => PaletteKind::Linear,
_ => PaletteKind::Global,
}
}
}
/// A trait for position types that are sometimes valid ways to index into a
/// chunk section.
pub trait SectionPos {
fn coords(&self) -> (usize, usize, usize);
fn new(x: usize, y: usize, z: usize) -> Self;
}
impl SectionPos for ChunkSectionBlockPos {
fn coords(&self) -> (usize, usize, usize) {
(self.x as usize, self.y as usize, self.z as usize)
}
fn new(x: usize, y: usize, z: usize) -> Self {
ChunkSectionBlockPos {
x: x as u8,
y: y as u8,
z: z as u8,
}
}
}
impl SectionPos for ChunkSectionBiomePos {
fn coords(&self) -> (usize, usize, usize) {
(self.x as usize, self.y as usize, self.z as usize)
}
fn new(x: usize, y: usize, z: usize) -> Self {
ChunkSectionBiomePos {
x: x as u8,
y: y as u8,
z: z as u8,
}
}
}
impl<S: PalletedContainerKind> PalettedContainer<S> {
pub fn new() -> Self {
let palette = Palette::SingleValue(S::default());
let size = S::size();
let storage = BitStorage::new(0, size, Some(Box::new([]))).unwrap();
PalettedContainer {
bits_per_entry: 0,
palette,
storage,
}
}
pub fn read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let bits_per_entry = u8::azalea_read(buf)?;
let palette_type = S::bits_per_entry_to_palette_kind(bits_per_entry);
let palette = palette_type.read(buf)?;
let size = S::size();
let mut storage = match BitStorage::new(
bits_per_entry as usize,
size,
if bits_per_entry == 0 {
Some(Box::new([]))
} else {
// we're going to update the data after creating the bitstorage
None
},
) {
Ok(storage) => storage,
Err(e) => {
warn!("Failed to create bit storage: {:?}", e);
return Err(BufReadError::Custom(
"Failed to create bit storage".to_owned(),
));
}
};
// now read the data
for i in 0..storage.data.len() {
storage.data[i] = u64::azalea_read(buf)?;
}
Ok(PalettedContainer {
bits_per_entry,
palette,
storage,
})
}
/// Calculates the index of the given position.
pub fn index_from_pos(&self, pos: S::SectionPos) -> usize {
let size_bits = S::size_bits();
let (x, y, z) = pos.coords();
(((y << size_bits) | z) << size_bits) | x
}
pub fn pos_from_index(&self, index: usize) -> S::SectionPos {
let size_bits = S::size_bits();
let mask = (1 << size_bits) - 1;
S::SectionPos::new(
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. For example, for block states, it must be less
/// than 4096.
#[inline]
pub fn get_at_index(&self, index: usize) -> S {
// 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 position.
pub fn get(&self, pos: S::SectionPos) -> S {
self.get_at_index(self.index_from_pos(pos))
}
/// Sets the ID at the given position and return the previous ID.
pub fn get_and_set(&mut self, pos: S::SectionPos, value: S) -> S {
let paletted_value = self.id_for(value);
let old_paletted_value = self
.storage
.get_and_set(self.index_from_pos(pos), paletted_value as u64);
self.palette.value_for(old_paletted_value as usize)
}
/// 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: S) {
let paletted_value = self.id_for(value);
self.storage.set(index, paletted_value as u64);
}
/// Sets the ID at the given position and return the previous ID.
pub fn set(&mut self, pos: S::SectionPos, value: S) {
self.set_at_index(self.index_from_pos(pos), value);
}
fn create_or_reuse_data(&self, bits_per_entry: u8) -> PalettedContainer<S> {
let new_palette_type = S::bits_per_entry_to_palette_kind(bits_per_entry);
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, S::size(), None).unwrap();
// sanity check
debug_assert_eq!(storage.size(), S::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,
}
}
fn on_resize(&mut self, bits_per_entry: u8, value: S) -> usize {
debug!(
"Resizing PalettedContainer from {} bpe to {bits_per_entry} for {value:?} with palette={:?}",
self.bits_per_entry, self.palette
);
// 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<S>, 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: S) -> usize {
match &mut self.palette {
Palette::SingleValue(v) => {
if (*v).into() != value.into() {
self.on_resize(1, value)
} else {
0
}
}
Palette::Linear(palette) => {
if let Some(index) = palette.iter().position(|&v| v.into() == value.into()) {
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 it should be benchmarked
// before changing it
if let Some(index) = palette.iter().position(|v| (*v).into() == value.into()) {
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.into() as usize,
}
}
}
impl<S: PalletedContainerKind> PalettedContainer<S> {
pub fn write(&self, buf: &mut impl Write) -> io::Result<()> {
self.bits_per_entry.azalea_write(buf)?;
self.palette.write(buf)?;
for word in &self.storage.data {
word.azalea_write(buf)?;
}
Ok(())
}
}
impl<S: PalletedContainerKind> Default for PalettedContainer<S> {
fn default() -> Self {
Self::new()
}
}
|