diff options
Diffstat (limited to 'azalea-core/src')
| -rwxr-xr-x | azalea-core/src/bitset.rs | 87 | ||||
| -rw-r--r--[-rwxr-xr-x] | azalea-core/src/game_type.rs | 3 | ||||
| -rwxr-xr-x | azalea-core/src/lib.rs | 2 |
3 files changed, 90 insertions, 2 deletions
diff --git a/azalea-core/src/bitset.rs b/azalea-core/src/bitset.rs index d878bbf5..41d43165 100755 --- a/azalea-core/src/bitset.rs +++ b/azalea-core/src/bitset.rs @@ -1,4 +1,6 @@ -use azalea_buf::McBuf; +use std::io::{Cursor, Read, Write}; + +use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable}; /// Represents Java's BitSet, a list of bits. #[derive(Debug, Clone, PartialEq, Eq, Hash, Default, McBuf)] @@ -105,6 +107,89 @@ impl BitSet { pub fn set(&mut self, bit_index: usize) { self.data[bit_index / 64] |= 1u64 << (bit_index % 64); } + + /// Read a BitSet with a known length. + pub fn read_fixed(buf: &mut Cursor<&[u8]>, length: usize) -> Result<Self, BufReadError> { + let mut data = vec![0; length.div_ceil(8)]; + buf.read_exact(&mut data)?; + Ok(BitSet::from(data)) + } +} + +impl From<Vec<u64>> for BitSet { + fn from(data: Vec<u64>) -> Self { + BitSet { data } + } +} + +impl From<Vec<u8>> for BitSet { + fn from(data: Vec<u8>) -> Self { + let mut words = vec![0; data.len().div_ceil(8)]; + for (i, byte) in data.iter().enumerate() { + words[i / 8] |= (*byte as u64) << ((i % 8) * 8); + } + BitSet { data: words } + } +} + +/// A list of bits with a known fixed size. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct FixedBitSet<const N: usize> +where + [(); N.div_ceil(8)]: Sized, +{ + data: [u8; N.div_ceil(8)], +} + +impl<const N: usize> FixedBitSet<N> +where + [u8; N.div_ceil(8)]: Sized, +{ + pub fn new() -> Self { + FixedBitSet { + data: [0; N.div_ceil(8)], + } + } + + pub fn index(&self, index: usize) -> bool { + (self.data[index / 8] & (1u8 << (index % 8))) != 0 + } + + pub fn set(&mut self, bit_index: usize) { + self.data[bit_index / 8] |= 1u8 << (bit_index % 8); + } +} + +impl<const N: usize> McBufReadable for FixedBitSet<N> +where + [u8; N.div_ceil(8)]: Sized, +{ + fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + let mut data = [0; N.div_ceil(8)]; + for item in data.iter_mut().take(N.div_ceil(8)) { + *item = u8::read_from(buf)?; + } + Ok(FixedBitSet { data }) + } +} +impl<const N: usize> McBufWritable for FixedBitSet<N> +where + [u8; N.div_ceil(8)]: Sized, +{ + fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + for i in 0..N.div_ceil(8) { + self.data[i].write_into(buf)?; + } + Ok(()) + } +} +impl<const N: usize> Default for FixedBitSet<N> +where + [u8; N.div_ceil(8)]: Sized, +{ + fn default() -> Self { + Self::new() + } } #[cfg(test)] diff --git a/azalea-core/src/game_type.rs b/azalea-core/src/game_type.rs index 75ee0674..c4b4cf76 100755..100644 --- a/azalea-core/src/game_type.rs +++ b/azalea-core/src/game_type.rs @@ -1,8 +1,9 @@ use azalea_buf::{BufReadError, McBufReadable, McBufWritable}; use std::io::{Cursor, Write}; -#[derive(Hash, Copy, Clone, Debug)] +#[derive(Hash, Copy, Clone, Debug, Default)] pub enum GameType { + #[default] Survival, Creative, Adventure, diff --git a/azalea-core/src/lib.rs b/azalea-core/src/lib.rs index 7c74bdcb..30af2448 100755 --- a/azalea-core/src/lib.rs +++ b/azalea-core/src/lib.rs @@ -1,6 +1,8 @@ //! Random miscellaneous things like UUIDs that don't deserve their own crate. #![feature(int_roundings)] +#![allow(incomplete_features)] +#![feature(generic_const_exprs)] mod difficulty; pub use difficulty::*; |
