Skip to content

Commit

Permalink
finish serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
kralverde committed Feb 14, 2025
1 parent 2fe17e0 commit 99e61a0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 39 deletions.
7 changes: 1 addition & 6 deletions pumpkin-nbt/src/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
where
T: Deserialize<'a>,
Expand Down Expand Up @@ -205,13 +205,9 @@ impl<'de, R: Read> de::Deserializer<'de> for &mut Deserializer<R> {
let value = self.input.get_u8_be()?;
visitor.visit_u8::<Error>(value)
} else {
panic!("{:?}", self.tag_to_deserialize);

/*
Err(Error::UnsupportedType(
"u8; NBT only supports signed values".to_string(),
))
*/
}
}

Expand Down Expand Up @@ -357,4 +353,3 @@ impl<'de, R: Read> SeqAccess<'de> for ListAccess<'_, R> {
result
}
}

47 changes: 16 additions & 31 deletions pumpkin-nbt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -214,7 +214,6 @@ impl_array!(BytesArray, "byte");

#[cfg(test)]
mod test {
use std::io::Read;
use std::sync::LazyLock;

use flate2::read::GzDecoder;
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down
9 changes: 8 additions & 1 deletion pumpkin-nbt/src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ where
}

/// Serializes struct using Serde Serializer to normal NBT
pub fn to_bytes<T>(value: &T, name: String, w: impl Write) -> Result<()>
pub fn to_bytes_named<T>(value: &T, name: String, w: impl Write) -> Result<()>
where
T: Serialize,
{
Expand All @@ -166,6 +166,13 @@ where
Ok(())
}

pub fn to_bytes<T>(value: &T, w: impl Write) -> Result<()>
where
T: Serialize,
{
to_bytes_named(value, String::new(), w)
}

impl<W: Write> ser::Serializer for &mut Serializer<W> {
type Ok = ();
type Error = Error;
Expand Down
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, ReadingError>;
}
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-registry/src/jukebox_song.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 99e61a0

Please sign in to comment.