From 99e61a090dedafe5ad0a4f992400a2f7be882c2c Mon Sep 17 00:00:00 2001 From: kralverde Date: Thu, 13 Feb 2025 20:36:33 -1000 Subject: [PATCH] finish serialize --- pumpkin-nbt/src/deserializer.rs | 7 +---- pumpkin-nbt/src/lib.rs | 47 ++++++++++------------------ pumpkin-nbt/src/serializer.rs | 9 +++++- pumpkin-protocol/src/lib.rs | 2 ++ pumpkin-registry/src/jukebox_song.rs | 2 +- 5 files changed, 28 insertions(+), 39 deletions(-) diff --git a/pumpkin-nbt/src/deserializer.rs b/pumpkin-nbt/src/deserializer.rs index 186ee48a5..7b0afcc54 100644 --- a/pumpkin-nbt/src/deserializer.rs +++ b/pumpkin-nbt/src/deserializer.rs @@ -137,7 +137,7 @@ where T::deserialize(&mut deserializer) } -/// Deserializes struct using Serde Deserializer from normal NBT +/// Deserializes struct using Serde Deserializer from network NBT pub fn from_bytes_unnamed<'a, T>(r: impl Read) -> Result where T: Deserialize<'a>, @@ -205,13 +205,9 @@ impl<'de, R: Read> de::Deserializer<'de> for &mut Deserializer { let value = self.input.get_u8_be()?; visitor.visit_u8::(value) } else { - panic!("{:?}", self.tag_to_deserialize); - - /* Err(Error::UnsupportedType( "u8; NBT only supports signed values".to_string(), )) - */ } } @@ -357,4 +353,3 @@ impl<'de, R: Read> SeqAccess<'de> for ListAccess<'_, R> { result } } - diff --git a/pumpkin-nbt/src/lib.rs b/pumpkin-nbt/src/lib.rs index ba1fb10cd..6a8c19a85 100644 --- a/pumpkin-nbt/src/lib.rs +++ b/pumpkin-nbt/src/lib.rs @@ -20,19 +20,19 @@ pub mod tag; // This NBT crate is inspired from CrabNBT -pub const END_ID: u8 = 0; -pub const BYTE_ID: u8 = 1; -pub const SHORT_ID: u8 = 2; -pub const INT_ID: u8 = 3; -pub const LONG_ID: u8 = 4; -pub const FLOAT_ID: u8 = 5; -pub const DOUBLE_ID: u8 = 6; -pub const BYTE_ARRAY_ID: u8 = 7; -pub const STRING_ID: u8 = 8; -pub const LIST_ID: u8 = 9; -pub const COMPOUND_ID: u8 = 10; -pub const INT_ARRAY_ID: u8 = 11; -pub const LONG_ARRAY_ID: u8 = 12; +pub const END_ID: u8 = 0x00; +pub const BYTE_ID: u8 = 0x01; +pub const SHORT_ID: u8 = 0x02; +pub const INT_ID: u8 = 0x03; +pub const LONG_ID: u8 = 0x04; +pub const FLOAT_ID: u8 = 0x05; +pub const DOUBLE_ID: u8 = 0x06; +pub const BYTE_ARRAY_ID: u8 = 0x07; +pub const STRING_ID: u8 = 0x08; +pub const LIST_ID: u8 = 0x09; +pub const COMPOUND_ID: u8 = 0x0A; +pub const INT_ARRAY_ID: u8 = 0x0B; +pub const LONG_ARRAY_ID: u8 = 0x0C; #[derive(Error, Debug)] pub enum Error { @@ -214,7 +214,6 @@ impl_array!(BytesArray, "byte"); #[cfg(test)] mod test { - use std::io::Read; use std::sync::LazyLock; use flate2::read::GzDecoder; @@ -331,6 +330,8 @@ mod test { }, }); + // TODO: More robust tests + #[test] fn test_deserialize_level_dat() { let raw_compressed_nbt = include_bytes!("../assets/level.dat"); @@ -344,24 +345,8 @@ mod test { #[test] fn test_serialize_level_dat() { - let raw_compressed_nbt = include_bytes!("../assets/level.dat"); - assert!(!raw_compressed_nbt.is_empty()); - - let mut decoder = GzDecoder::new(&raw_compressed_nbt[..]); - let mut raw_bytes = Vec::new(); - decoder.read_to_end(&mut raw_bytes).unwrap(); - let mut serialized = Vec::new(); - to_bytes(&*LEVEL_DAT, "".to_string(), &mut serialized).expect("Failed to encode to bytes"); - raw_bytes - .iter() - .zip(serialized.iter()) - .enumerate() - .for_each(|(index, (expected_byte, serialized_byte))| { - if expected_byte != serialized_byte { - panic!("{} vs {} ({})", expected_byte, serialized_byte, index); - } - }); + to_bytes(&*LEVEL_DAT, &mut serialized).expect("Failed to encode to bytes"); let level_dat_again: LevelDat = from_bytes(&serialized[..]).expect("Failed to decode from bytes"); diff --git a/pumpkin-nbt/src/serializer.rs b/pumpkin-nbt/src/serializer.rs index b6819c580..2eb7b5903 100644 --- a/pumpkin-nbt/src/serializer.rs +++ b/pumpkin-nbt/src/serializer.rs @@ -154,7 +154,7 @@ where } /// Serializes struct using Serde Serializer to normal NBT -pub fn to_bytes(value: &T, name: String, w: impl Write) -> Result<()> +pub fn to_bytes_named(value: &T, name: String, w: impl Write) -> Result<()> where T: Serialize, { @@ -166,6 +166,13 @@ where Ok(()) } +pub fn to_bytes(value: &T, w: impl Write) -> Result<()> +where + T: Serialize, +{ + to_bytes_named(value, String::new(), w) +} + impl ser::Serializer for &mut Serializer { type Ok = (); type Error = Error; diff --git a/pumpkin-protocol/src/lib.rs b/pumpkin-protocol/src/lib.rs index 8d6711052..d64ababab 100644 --- a/pumpkin-protocol/src/lib.rs +++ b/pumpkin-protocol/src/lib.rs @@ -82,10 +82,12 @@ pub struct RawPacket { pub bytebuf: Bytes, } +// TODO: Have the input be `impl Write` pub trait ClientPacket: Packet { fn write(&self, bytebuf: &mut impl BufMut); } +// TODO: Have the input be `impl Read` pub trait ServerPacket: Packet + Sized { fn read(bytebuf: &mut impl Buf) -> Result; } diff --git a/pumpkin-registry/src/jukebox_song.rs b/pumpkin-registry/src/jukebox_song.rs index adcb0e440..b554ceb15 100644 --- a/pumpkin-registry/src/jukebox_song.rs +++ b/pumpkin-registry/src/jukebox_song.rs @@ -5,7 +5,7 @@ pub struct JukeboxSong { sound_event: String, description: Description, length_in_seconds: f32, - comparator_output: u32, + comparator_output: i32, } #[derive(Debug, Clone, Serialize, Deserialize)]