diff options
| author | mat <github@matdoes.dev> | 2022-06-23 19:17:04 -0500 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-06-23 19:17:04 -0500 |
| commit | 37c6618c16319a7f40fd2e165190407472598e84 (patch) | |
| tree | 6d712df8893041f0e49cfa6ca7adc9a411d39b84 /azalea-buf/src | |
| parent | 1089aa7961bd9af67c94dec9c5dbc6bd9f275225 (diff) | |
| download | azalea-drasl-37c6618c16319a7f40fd2e165190407472598e84.tar.xz | |
Fix everything so azalea-buf works
Diffstat (limited to 'azalea-buf/src')
| -rw-r--r-- | azalea-buf/src/definitions.rs | 23 | ||||
| -rw-r--r-- | azalea-buf/src/lib.rs | 2 | ||||
| -rw-r--r-- | azalea-buf/src/serializable_uuid.rs | 76 | ||||
| -rw-r--r-- | azalea-buf/src/write.rs | 4 |
4 files changed, 101 insertions, 4 deletions
diff --git a/azalea-buf/src/definitions.rs b/azalea-buf/src/definitions.rs index e5d8e0c0..921fb63d 100644 --- a/azalea-buf/src/definitions.rs +++ b/azalea-buf/src/definitions.rs @@ -1,5 +1,8 @@ -use buf_macros::McBuf; -use std::ops::Deref; +use crate::{McBufReadable, McBufWritable}; +use std::{ + io::{Read, Write}, + ops::Deref, +}; /// A Vec<u8> that isn't prefixed by a VarInt with the size. #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -26,7 +29,7 @@ impl From<&str> for UnsizedByteArray { } /// Represents Java's BitSet, a list of bits. -#[derive(Debug, Clone, PartialEq, Eq, Hash, McBuf)] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct BitSet { data: Vec<u64>, } @@ -37,3 +40,17 @@ impl BitSet { (self.data[index / 64] & (1u64 << (index % 64))) != 0 } } + +impl McBufReadable for BitSet { + fn read_into(buf: &mut impl Read) -> Result<Self, String> { + Ok(Self { + data: Vec::<u64>::read_into(buf)?, + }) + } +} + +impl McBufWritable for BitSet { + fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + self.data.write_into(buf) + } +} diff --git a/azalea-buf/src/lib.rs b/azalea-buf/src/lib.rs index bd9f43be..cd72f22e 100644 --- a/azalea-buf/src/lib.rs +++ b/azalea-buf/src/lib.rs @@ -5,11 +5,13 @@ mod definitions; mod read; +mod serializable_uuid; mod write; pub use buf_macros::*; pub use definitions::*; pub use read::{read_varint_async, McBufReadable, McBufVarReadable, Readable}; +pub use serializable_uuid::*; pub use write::{McBufVarWritable, McBufWritable, Writable}; // const DEFAULT_NBT_QUOTA: u32 = 2097152; diff --git a/azalea-buf/src/serializable_uuid.rs b/azalea-buf/src/serializable_uuid.rs new file mode 100644 index 00000000..10921aa6 --- /dev/null +++ b/azalea-buf/src/serializable_uuid.rs @@ -0,0 +1,76 @@ +use crate::{McBufReadable, McBufWritable, Readable}; +use std::io::{Read, Write}; +use uuid::Uuid; + +pub trait SerializableUuid { + fn to_int_array(&self) -> [u32; 4]; + fn from_int_array(array: [u32; 4]) -> Self; +} + +fn least_most_to_int_array(most: u64, least: u64) -> [u32; 4] { + [ + (most >> 32) as u32, + most as u32, + (least >> 32) as u32, + least as u32, + ] +} + +impl SerializableUuid for Uuid { + fn to_int_array(&self) -> [u32; 4] { + let most_significant_bits = (self.as_u128() >> 64) as u64; + let least_significant_bits = (self.as_u128() & 0xffffffffffffffff) as u64; + + least_most_to_int_array(most_significant_bits, least_significant_bits) + } + + fn from_int_array(array: [u32; 4]) -> Self { + let most = ((array[0] as u64) << 32) | ((array[1] as u64) & 0xFFFFFFFF); + let least = ((array[2] as u64) << 32) | ((array[3] as u64) & 0xFFFFFFFF); + + Uuid::from_u128(((most as u128) << 64) | least as u128) + } +} + +impl McBufReadable for Uuid { + fn read_into(buf: &mut impl Read) -> Result<Self, String> { + Ok(Uuid::from_int_array([ + Readable::read_int(buf)? as u32, + Readable::read_int(buf)? as u32, + Readable::read_int(buf)? as u32, + Readable::read_int(buf)? as u32, + ])) + } +} + +impl McBufWritable for Uuid { + fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + let [a, b, c, d] = self.to_int_array(); + a.write_into(buf)?; + b.write_into(buf)?; + c.write_into(buf)?; + d.write_into(buf)?; + Ok(()) + } +} + +// TODO: add a test for Uuid in McBuf +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn to_int_array() { + let u = Uuid::parse_str("6536bfed-8695-48fd-83a1-ecd24cf2a0fd").unwrap(); + assert_eq!( + u.to_int_array(), + [0x6536bfed, 0x869548fd, 0x83a1ecd2, 0x4cf2a0fd] + ); + } + + #[test] + fn from_int_array() { + let u = Uuid::from_int_array([0x6536bfed, 0x869548fd, 0x83a1ecd2, 0x4cf2a0fd]); + assert_eq!(u.to_string(), "6536bfed-8695-48fd-83a1-ecd24cf2a0fd"); + } +} diff --git a/azalea-buf/src/write.rs b/azalea-buf/src/write.rs index fdf58203..38ddcf49 100644 --- a/azalea-buf/src/write.rs +++ b/azalea-buf/src/write.rs @@ -189,7 +189,9 @@ impl McBufVarWritable for i64 { if value != 0 { buffer[0] |= 0b1000_0000; } - buf.write(&mut buffer)?; + // this only writes a single byte, so write_all isn't necessary + // the let _ = is so clippy doesn't complain + let _ = buf.write(&mut buffer)?; } Ok(()) } |
